2018-01-05 09:18:19 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/dynaview/dynaview.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: startup
|
|
|
|
|
|
|
|
Zoom everything
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// Export name and synchronous status
|
|
|
|
exports.name = "dynaview";
|
|
|
|
exports.platforms = ["browser"];
|
2019-03-04 16:59:39 +00:00
|
|
|
exports.before = ["story"];
|
2018-01-05 09:18:19 +00:00
|
|
|
exports.synchronous = true;
|
|
|
|
|
2019-03-21 12:56:26 +00:00
|
|
|
var STATE_OUT_OF_VIEW = "0",
|
|
|
|
STATE_NEAR_VIEW = "1",
|
|
|
|
STATE_IN_VIEW = "2";
|
|
|
|
|
2018-03-10 10:33:34 +00:00
|
|
|
var isWaitingForAnimationFrame = 0, // Bitmask:
|
|
|
|
ANIM_FRAME_CAUSED_BY_LOAD = 1, // Animation frame was requested because of page load
|
|
|
|
ANIM_FRAME_CAUSED_BY_SCROLL = 2, // Animation frame was requested because of page scroll
|
|
|
|
ANIM_FRAME_CAUSED_BY_RESIZE = 4; // Animation frame was requested because of window resize
|
2018-01-05 09:18:19 +00:00
|
|
|
|
2019-03-04 16:59:39 +00:00
|
|
|
var LOCAL_STORAGE_KEY_PREFIX = "tw5-dynaview-scroll-position#";
|
|
|
|
|
|
|
|
var hasRestoredScrollPosition = false;
|
|
|
|
|
2019-06-21 14:19:51 +00:00
|
|
|
var localStorageHasFailed = false;
|
|
|
|
|
2018-01-05 09:18:19 +00:00
|
|
|
exports.startup = function() {
|
2019-01-11 17:50:52 +00:00
|
|
|
var topmost = null, lastScrollY;
|
2019-03-04 16:59:39 +00:00
|
|
|
$tw.boot.disableStartupNavigation = true;
|
2018-03-10 10:33:34 +00:00
|
|
|
window.addEventListener("load",onLoad,false);
|
|
|
|
window.addEventListener("scroll",onScroll,false);
|
|
|
|
window.addEventListener("resize",onResize,false);
|
2019-01-11 17:50:52 +00:00
|
|
|
$tw.hooks.addHook("th-page-refreshing",function() {
|
2019-03-04 16:59:39 +00:00
|
|
|
if(!hasRestoredScrollPosition) {
|
|
|
|
topmost = restoreScrollPosition();
|
|
|
|
} else if(shouldPreserveScrollPosition()) {
|
2019-01-11 17:50:52 +00:00
|
|
|
topmost = findTopmostTiddler();
|
|
|
|
}
|
|
|
|
lastScrollY = window.scrollY;
|
|
|
|
});
|
2018-01-05 09:18:19 +00:00
|
|
|
$tw.hooks.addHook("th-page-refreshed",function() {
|
2019-01-11 17:50:52 +00:00
|
|
|
if(lastScrollY === window.scrollY) { // Don't do scroll anchoring if the scroll position got changed
|
2019-03-04 16:59:39 +00:00
|
|
|
if(shouldPreserveScrollPosition() || !hasRestoredScrollPosition) {
|
|
|
|
scrollToTiddler(topmost);
|
|
|
|
hasRestoredScrollPosition = true;
|
|
|
|
}
|
2019-01-11 17:50:52 +00:00
|
|
|
}
|
|
|
|
updateAddressBar();
|
2019-03-04 16:59:39 +00:00
|
|
|
saveScrollPosition();
|
2018-01-05 09:18:19 +00:00
|
|
|
checkVisibility();
|
2018-03-10 10:33:34 +00:00
|
|
|
saveViewportDimensions();
|
2018-01-05 09:18:19 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-03-10 10:33:34 +00:00
|
|
|
function onLoad(event) {
|
2018-01-05 09:18:19 +00:00
|
|
|
if(!isWaitingForAnimationFrame) {
|
2018-03-10 10:33:34 +00:00
|
|
|
window.requestAnimationFrame(worker);
|
|
|
|
}
|
|
|
|
isWaitingForAnimationFrame |= ANIM_FRAME_CAUSED_BY_LOAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onScroll(event) {
|
|
|
|
if(!isWaitingForAnimationFrame) {
|
|
|
|
window.requestAnimationFrame(worker);
|
|
|
|
}
|
|
|
|
isWaitingForAnimationFrame |= ANIM_FRAME_CAUSED_BY_SCROLL;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onResize(event) {
|
|
|
|
if(!isWaitingForAnimationFrame) {
|
|
|
|
window.requestAnimationFrame(worker);
|
|
|
|
}
|
|
|
|
isWaitingForAnimationFrame |= ANIM_FRAME_CAUSED_BY_RESIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
function worker() {
|
|
|
|
if(isWaitingForAnimationFrame & (ANIM_FRAME_CAUSED_BY_RESIZE | ANIM_FRAME_CAUSED_BY_LOAD)) {
|
|
|
|
saveViewportDimensions();
|
|
|
|
}
|
|
|
|
setZoomClasses();
|
2019-01-11 17:50:52 +00:00
|
|
|
updateAddressBar();
|
2019-03-04 16:59:39 +00:00
|
|
|
saveScrollPosition();
|
2018-03-10 10:33:34 +00:00
|
|
|
checkVisibility();
|
|
|
|
isWaitingForAnimationFrame = 0;
|
|
|
|
}
|
|
|
|
|
2018-01-05 09:18:19 +00:00
|
|
|
function setZoomClasses() {
|
|
|
|
var zoomFactor = document.body.scrollWidth / window.innerWidth,
|
|
|
|
classList = document.body.classList;
|
2018-02-18 12:04:35 +00:00
|
|
|
classList.add("tc-dynaview");
|
2018-01-05 09:18:19 +00:00
|
|
|
classList.toggle("tc-dynaview-zoom-factor-1",zoomFactor <= 2);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-1-and-above",zoomFactor >= 1);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-1a-and-above",zoomFactor >= 1.14);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-1b-and-above",zoomFactor >= 1.33);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-1c-and-above",zoomFactor >= 1.6);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-2",zoomFactor >= 2 && zoomFactor <= 3);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-2-and-above",zoomFactor >= 2);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-2a-and-above",zoomFactor >= 2.66);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-3",zoomFactor >= 3 && zoomFactor <= 4);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-3-and-above",zoomFactor >= 3);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-4",zoomFactor >= 4);
|
|
|
|
classList.toggle("tc-dynaview-zoom-factor-4-and-above",zoomFactor >= 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkVisibility() {
|
2019-03-21 12:56:26 +00:00
|
|
|
var elements = document.querySelectorAll(".tc-dynaview-track-tiddler-when-visible");
|
2018-01-05 09:18:19 +00:00
|
|
|
$tw.utils.each(elements,function(element) {
|
2018-05-23 13:17:49 +00:00
|
|
|
// Calculate whether the element is visible
|
2018-01-05 09:18:19 +00:00
|
|
|
var elementRect = element.getBoundingClientRect(),
|
|
|
|
viewportWidth = window.innerWidth || document.documentElement.clientWidth,
|
|
|
|
viewportHeight = window.innerHeight || document.documentElement.clientHeight,
|
|
|
|
viewportRect = {
|
|
|
|
left: 0,
|
|
|
|
right: viewportWidth,
|
|
|
|
top: 0,
|
|
|
|
bottom: viewportHeight
|
2018-05-23 13:17:49 +00:00
|
|
|
},
|
2019-03-21 12:56:26 +00:00
|
|
|
title = element.getAttribute("data-dynaview-track-tiddler");
|
|
|
|
if(title) {
|
|
|
|
var currValue = $tw.wiki.getTiddlerText(title),
|
|
|
|
newValue = currValue;
|
|
|
|
// Within viewport
|
|
|
|
if(!(elementRect.left > viewportRect.right ||
|
|
|
|
elementRect.right < viewportRect.left ||
|
|
|
|
elementRect.top > viewportRect.bottom ||
|
|
|
|
elementRect.bottom < viewportRect.top)) {
|
|
|
|
newValue = STATE_IN_VIEW;
|
|
|
|
// Near viewport
|
|
|
|
} else if(!(elementRect.left > (viewportRect.right + viewportWidth) ||
|
|
|
|
elementRect.right < (viewportRect.left - viewportWidth) ||
|
|
|
|
elementRect.top > (viewportRect.bottom + viewportHeight) ||
|
|
|
|
elementRect.bottom < (viewportRect.top - viewportHeight))) {
|
|
|
|
newValue = STATE_NEAR_VIEW;
|
|
|
|
} else {
|
|
|
|
// Outside viewport
|
|
|
|
if(currValue !== undefined) {
|
|
|
|
newValue = STATE_OUT_OF_VIEW;
|
|
|
|
}
|
2018-05-23 13:17:49 +00:00
|
|
|
}
|
2019-03-21 12:56:26 +00:00
|
|
|
if(newValue !== currValue) {
|
|
|
|
$tw.wiki.addTiddler(new $tw.Tiddler({title: title, text: newValue}));
|
2018-01-05 09:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:50:52 +00:00
|
|
|
function updateAddressBar() {
|
2018-03-13 14:07:29 +00:00
|
|
|
if($tw.wiki.getTiddlerText("$:/config/DynaView/UpdateAddressBar") === "yes") {
|
2019-01-11 17:50:52 +00:00
|
|
|
var top = findTopmostTiddler();
|
|
|
|
if(top.element) {
|
|
|
|
var hash = "#" + encodeURIComponent(top.title) + ":" + encodeURIComponent("[list[$:/StoryList]]");
|
|
|
|
if(title && $tw.locationHash !== hash) {
|
|
|
|
$tw.locationHash = hash;
|
|
|
|
window.location.hash = hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 16:59:39 +00:00
|
|
|
function saveScrollPosition() {
|
2019-06-21 14:19:51 +00:00
|
|
|
if(!localStorageHasFailed) {
|
|
|
|
if(hasRestoredScrollPosition && $tw.wiki.getTiddlerText("$:/config/DynaView/RestoreScrollPositionAtStartup") === "yes") {
|
|
|
|
var top = findTopmostTiddler();
|
|
|
|
if(top.element) {
|
|
|
|
try {
|
|
|
|
window.localStorage.setItem(LOCAL_STORAGE_KEY_PREFIX + window.location.pathname,JSON.stringify({
|
|
|
|
title: top.title,
|
|
|
|
offset: top.offset
|
|
|
|
}));
|
|
|
|
} catch(e) {
|
|
|
|
localStorageHasFailed = true;
|
|
|
|
}
|
2019-03-04 16:59:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function restoreScrollPosition() {
|
2019-05-29 08:01:11 +00:00
|
|
|
var json;
|
2019-06-21 14:19:51 +00:00
|
|
|
if(!localStorageHasFailed) {
|
|
|
|
try {
|
|
|
|
json = JSON.parse(window.localStorage.getItem(LOCAL_STORAGE_KEY_PREFIX + window.location.pathname));
|
|
|
|
} catch(e) {
|
|
|
|
localStorageHasFailed = true;
|
|
|
|
};
|
|
|
|
}
|
2019-03-04 16:59:39 +00:00
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:50:52 +00:00
|
|
|
/*
|
|
|
|
tiddlerDetails: {title: <title of tiddler to scroll to>, offset: <offset in pixels from the top of the tiddler>}
|
|
|
|
*/
|
|
|
|
function scrollToTiddler(tiddlerDetails) {
|
2019-03-04 16:59:39 +00:00
|
|
|
if(!$tw.pageScroller.isScrolling() && tiddlerDetails) {
|
2018-03-13 14:07:29 +00:00
|
|
|
var elements = document.querySelectorAll(".tc-tiddler-frame[data-tiddler-title]"),
|
2019-01-11 17:50:52 +00:00
|
|
|
topmostTiddlerElement = null;
|
2018-03-13 14:07:29 +00:00
|
|
|
$tw.utils.each(elements,function(element) {
|
2019-01-11 17:50:52 +00:00
|
|
|
if(element.getAttribute("data-tiddler-title") === tiddlerDetails.title) {
|
|
|
|
topmostTiddlerElement = element;
|
2018-03-13 14:07:29 +00:00
|
|
|
}
|
|
|
|
});
|
2019-01-11 17:50:52 +00:00
|
|
|
if(topmostTiddlerElement) {
|
|
|
|
var rect = topmostTiddlerElement.getBoundingClientRect(),
|
|
|
|
scrollY = Math.round(window.scrollY + rect.top + tiddlerDetails.offset);
|
|
|
|
if(scrollY !== window.scrollY) {
|
|
|
|
window.scrollTo(window.scrollX,scrollY);
|
2018-03-13 14:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-11 17:50:52 +00:00
|
|
|
function shouldPreserveScrollPosition() {
|
|
|
|
return $tw.wiki.getTiddlerText("$:/config/DynaView/PreserveScrollPosition") === "yes";
|
|
|
|
}
|
|
|
|
|
|
|
|
function findTopmostTiddler() {
|
|
|
|
var elements = document.querySelectorAll(".tc-tiddler-frame[data-tiddler-title]"),
|
|
|
|
topmostElement = null,
|
|
|
|
topmostElementTop = 1 * 1000 * 1000;
|
|
|
|
$tw.utils.each(elements,function(element) {
|
|
|
|
// Check if the element is visible
|
|
|
|
var elementRect = element.getBoundingClientRect();
|
|
|
|
if((elementRect.top < topmostElementTop) && (elementRect.bottom > 0)) {
|
|
|
|
topmostElement = element;
|
|
|
|
topmostElementTop = elementRect.top;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
element: topmostElement,
|
|
|
|
offset: -topmostElementTop,
|
2019-03-16 13:25:45 +00:00
|
|
|
title: topmostElement ? topmostElement.getAttribute("data-tiddler-title") : null
|
2019-01-11 17:50:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-01 12:19:28 +00:00
|
|
|
var previousViewportWidth, previousViewportHeight;
|
|
|
|
|
2018-02-18 12:04:35 +00:00
|
|
|
function saveViewportDimensions() {
|
2018-09-01 12:19:28 +00:00
|
|
|
var viewportWidth = window.innerWidth || document.documentElement.clientWidth,
|
|
|
|
viewportHeight = window.innerHeight || document.documentElement.clientHeight;
|
|
|
|
if(document.querySelector(".tc-dynaview-request-refresh-on-resize")) {
|
|
|
|
if(previousViewportWidth !== viewportWidth || previousViewportHeight !== viewportHeight) {
|
|
|
|
var count = parseInt($tw.wiki.getTiddlerText("$:/state/DynaView/ViewportDimensions/ResizeCount","0"),10) || 0;
|
|
|
|
$tw.wiki.addTiddler(new $tw.Tiddler({title: "$:/state/DynaView/ViewportDimensions/ResizeCount", text: (count + 1) + ""}));
|
|
|
|
previousViewportWidth = viewportWidth;
|
|
|
|
previousViewportHeight = viewportHeight;
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 10:33:34 +00:00
|
|
|
if($tw.wiki.getTiddlerText("$:/config/DynaView/ViewportDimensions") === "yes") {
|
|
|
|
if($tw.wiki.getTiddlerText("$:/state/DynaView/ViewportDimensions/Width") !== viewportWidth.toString()) {
|
|
|
|
$tw.wiki.setText("$:/state/DynaView/ViewportDimensions/Width",undefined,undefined,viewportWidth.toString(),undefined);
|
|
|
|
}
|
|
|
|
if($tw.wiki.getTiddlerText("$:/state/DynaView/ViewportDimensions/Height") !== viewportHeight.toString()) {
|
|
|
|
$tw.wiki.setText("$:/state/DynaView/ViewportDimensions/Height",undefined,undefined,viewportHeight.toString(),undefined);
|
|
|
|
}
|
2018-02-18 15:39:10 +00:00
|
|
|
}
|
2018-02-18 12:04:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})();
|