1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-14 22:04:51 +00:00

PageScroller: scroll in new windows (#3537)

* make pageScroller work in new windows

* update getScrollPosition to work for new windows

* Update dom.js
This commit is contained in:
BurningTreeC 2018-11-13 19:07:55 +01:00 committed by Jeremy Ruston
parent 8ae62c90df
commit 55b5b6dd56
2 changed files with 19 additions and 17 deletions

View File

@ -82,11 +82,12 @@ Returns:
y: vertical scroll position in pixels y: vertical scroll position in pixels
} }
*/ */
exports.getScrollPosition = function() { exports.getScrollPosition = function(srcWindow) {
if("scrollX" in window) { var scrollWindow = srcWindow || window;
return {x: window.scrollX, y: window.scrollY}; if("scrollX" in scrollWindow) {
return {x: scrollWindow.scrollX, y: scrollWindow.scrollY};
} else { } else {
return {x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop}; return {x: scrollWindow.document.documentElement.scrollLeft, y: scrollWindow.document.documentElement.scrollTop};
} }
}; };
@ -119,7 +120,7 @@ exports.resizeTextAreaToFit = function(domNode,minHeight) {
Gets the bounding rectangle of an element in absolute page coordinates Gets the bounding rectangle of an element in absolute page coordinates
*/ */
exports.getBoundingPageRect = function(element) { exports.getBoundingPageRect = function(element) {
var scrollPos = $tw.utils.getScrollPosition(), var scrollPos = $tw.utils.getScrollPosition(element.ownerDocument.defaultView),
clientRect = element.getBoundingClientRect(); clientRect = element.getBoundingClientRect();
return { return {
left: clientRect.left + scrollPos.x, left: clientRect.left + scrollPos.x,

View File

@ -33,9 +33,9 @@ var PageScroller = function() {
}; };
}; };
PageScroller.prototype.cancelScroll = function() { PageScroller.prototype.cancelScroll = function(srcWindow) {
if(this.idRequestFrame) { if(this.idRequestFrame) {
this.cancelAnimationFrame.call(window,this.idRequestFrame); this.cancelAnimationFrame.call(srcWindow,this.idRequestFrame);
this.idRequestFrame = null; this.idRequestFrame = null;
} }
}; };
@ -55,12 +55,13 @@ Handle a scroll event hitting the page document
*/ */
PageScroller.prototype.scrollIntoView = function(element) { PageScroller.prototype.scrollIntoView = function(element) {
var self = this, var self = this,
duration = $tw.utils.getAnimationDuration(); duration = $tw.utils.getAnimationDuration(),
srcWindow = element.ownerDocument.defaultView;
// Now get ready to scroll the body // Now get ready to scroll the body
this.cancelScroll(); this.cancelScroll(srcWindow);
this.startTime = Date.now(); this.startTime = Date.now();
// Get the height of any position:fixed toolbars // Get the height of any position:fixed toolbars
var toolbar = document.querySelector(".tc-adjust-top-of-scroll"), var toolbar = srcWindow.document.querySelector(".tc-adjust-top-of-scroll"),
offset = 0; offset = 0;
if(toolbar) { if(toolbar) {
offset = toolbar.offsetHeight; offset = toolbar.offsetHeight;
@ -68,7 +69,7 @@ PageScroller.prototype.scrollIntoView = function(element) {
// Get the client bounds of the element and adjust by the scroll position // Get the client bounds of the element and adjust by the scroll position
var getBounds = function() { var getBounds = function() {
var clientBounds = element.getBoundingClientRect(), var clientBounds = element.getBoundingClientRect(),
scrollPosition = $tw.utils.getScrollPosition(); scrollPosition = $tw.utils.getScrollPosition(srcWindow);
return { return {
left: clientBounds.left + scrollPosition.x, left: clientBounds.left + scrollPosition.x,
top: clientBounds.top + scrollPosition.y - offset, top: clientBounds.top + scrollPosition.y - offset,
@ -96,17 +97,17 @@ PageScroller.prototype.scrollIntoView = function(element) {
t = ((Date.now()) - self.startTime) / duration; t = ((Date.now()) - self.startTime) / duration;
} }
if(t >= 1) { if(t >= 1) {
self.cancelScroll(); self.cancelScroll(srcWindow);
t = 1; t = 1;
} }
t = $tw.utils.slowInSlowOut(t); t = $tw.utils.slowInSlowOut(t);
var scrollPosition = $tw.utils.getScrollPosition(), var scrollPosition = $tw.utils.getScrollPosition(srcWindow),
bounds = getBounds(), bounds = getBounds(),
endX = getEndPos(bounds.left,bounds.width,scrollPosition.x,window.innerWidth), endX = getEndPos(bounds.left,bounds.width,scrollPosition.x,srcWindow.innerWidth),
endY = getEndPos(bounds.top,bounds.height,scrollPosition.y,window.innerHeight); endY = getEndPos(bounds.top,bounds.height,scrollPosition.y,srcWindow.innerHeight);
window.scrollTo(scrollPosition.x + (endX - scrollPosition.x) * t,scrollPosition.y + (endY - scrollPosition.y) * t); srcWindow.scrollTo(scrollPosition.x + (endX - scrollPosition.x) * t,scrollPosition.y + (endY - scrollPosition.y) * t);
if(t < 1) { if(t < 1) {
self.idRequestFrame = self.requestAnimationFrame.call(window,drawFrame); self.idRequestFrame = self.requestAnimationFrame.call(srcWindow,drawFrame);
} }
}; };
drawFrame(); drawFrame();