From 55b5b6dd56d9146b39482fbd2ae0353adce037f1 Mon Sep 17 00:00:00 2001 From: BurningTreeC Date: Tue, 13 Nov 2018 19:07:55 +0100 Subject: [PATCH] PageScroller: scroll in new windows (#3537) * make pageScroller work in new windows * update getScrollPosition to work for new windows * Update dom.js --- core/modules/utils/dom/dom.js | 11 ++++++----- core/modules/utils/dom/scroller.js | 25 +++++++++++++------------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/core/modules/utils/dom/dom.js b/core/modules/utils/dom/dom.js index be3d1bc47..b45745ae3 100644 --- a/core/modules/utils/dom/dom.js +++ b/core/modules/utils/dom/dom.js @@ -82,11 +82,12 @@ Returns: y: vertical scroll position in pixels } */ -exports.getScrollPosition = function() { - if("scrollX" in window) { - return {x: window.scrollX, y: window.scrollY}; +exports.getScrollPosition = function(srcWindow) { + var scrollWindow = srcWindow || window; + if("scrollX" in scrollWindow) { + return {x: scrollWindow.scrollX, y: scrollWindow.scrollY}; } 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 */ exports.getBoundingPageRect = function(element) { - var scrollPos = $tw.utils.getScrollPosition(), + var scrollPos = $tw.utils.getScrollPosition(element.ownerDocument.defaultView), clientRect = element.getBoundingClientRect(); return { left: clientRect.left + scrollPos.x, diff --git a/core/modules/utils/dom/scroller.js b/core/modules/utils/dom/scroller.js index 417bc1b33..e34e6b032 100644 --- a/core/modules/utils/dom/scroller.js +++ b/core/modules/utils/dom/scroller.js @@ -33,9 +33,9 @@ var PageScroller = function() { }; }; -PageScroller.prototype.cancelScroll = function() { +PageScroller.prototype.cancelScroll = function(srcWindow) { if(this.idRequestFrame) { - this.cancelAnimationFrame.call(window,this.idRequestFrame); + this.cancelAnimationFrame.call(srcWindow,this.idRequestFrame); this.idRequestFrame = null; } }; @@ -55,12 +55,13 @@ Handle a scroll event hitting the page document */ PageScroller.prototype.scrollIntoView = function(element) { var self = this, - duration = $tw.utils.getAnimationDuration(); + duration = $tw.utils.getAnimationDuration(), + srcWindow = element.ownerDocument.defaultView; // Now get ready to scroll the body - this.cancelScroll(); + this.cancelScroll(srcWindow); this.startTime = Date.now(); // 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; if(toolbar) { 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 var getBounds = function() { var clientBounds = element.getBoundingClientRect(), - scrollPosition = $tw.utils.getScrollPosition(); + scrollPosition = $tw.utils.getScrollPosition(srcWindow); return { left: clientBounds.left + scrollPosition.x, top: clientBounds.top + scrollPosition.y - offset, @@ -96,17 +97,17 @@ PageScroller.prototype.scrollIntoView = function(element) { t = ((Date.now()) - self.startTime) / duration; } if(t >= 1) { - self.cancelScroll(); + self.cancelScroll(srcWindow); t = 1; } t = $tw.utils.slowInSlowOut(t); - var scrollPosition = $tw.utils.getScrollPosition(), + var scrollPosition = $tw.utils.getScrollPosition(srcWindow), bounds = getBounds(), - endX = getEndPos(bounds.left,bounds.width,scrollPosition.x,window.innerWidth), - endY = getEndPos(bounds.top,bounds.height,scrollPosition.y,window.innerHeight); - window.scrollTo(scrollPosition.x + (endX - scrollPosition.x) * t,scrollPosition.y + (endY - scrollPosition.y) * t); + endX = getEndPos(bounds.left,bounds.width,scrollPosition.x,srcWindow.innerWidth), + endY = getEndPos(bounds.top,bounds.height,scrollPosition.y,srcWindow.innerHeight); + srcWindow.scrollTo(scrollPosition.x + (endX - scrollPosition.x) * t,scrollPosition.y + (endY - scrollPosition.y) * t); if(t < 1) { - self.idRequestFrame = self.requestAnimationFrame.call(window,drawFrame); + self.idRequestFrame = self.requestAnimationFrame.call(srcWindow,drawFrame); } }; drawFrame();