Split 'scrollTo' function

This commit is contained in:
Dmitry Lyzo 2019-10-22 23:46:21 +03:00
parent c0fbce32ce
commit fe87abc5a8

View File

@ -316,28 +316,38 @@ define(["dom", "browser", "layoutManager"], function (dom, browser, layoutManage
};
/**
* Scrolls the document to a given position or element.
* Scrolls the document to a given position.
*
* @param {Object} options scroll options
* @param {number} [options.x] horizontal coordinate
* @param {number} [options.y] vertical coordinate
* @param {HTMLElement} [options.element] target element of scroll task
* @param {boolean} [options.smooth=false] smooth scrolling
* @param {number} scrollX horizontal coordinate
* @param {number} scrollY vertical coordinate
* @param {boolean} [smooth=false] smooth scrolling
*/
var scrollTo = function(options) {
var scrollTo = function(scrollX, scrollY, smooth) {
var element = options.element;
var smooth = !!options.smooth;
var xScroller;
var yScroller;
var scrollX;
var scrollY;
smooth = !!smooth;
// Scroller is document itself by default
xScroller = yScroller = getScrollableParent(null, false);
var scroller = getScrollableParent(null, false);
var xScrollerData = getScrollerData(scroller, false);
var yScrollerData = getScrollerData(scroller, true);
scrollX = clamp(Math.round(scrollX), 0, xScrollerData.scrollSize - xScrollerData.clientSize);
scrollY = clamp(Math.round(scrollY), 0, yScrollerData.scrollSize - yScrollerData.clientSize);
doScroll(scroller, scrollX, scroller, scrollY, smooth);
}
/**
* Scrolls the document to a given element.
*
* @param {HTMLElement} element target element of scroll task
* @param {boolean} [smooth=false] smooth scrolling
*/
var scrollToElement = function(element, smooth) {
smooth = !!smooth;
if (options.element !== undefined) {
var scrollCenterX = true;
var scrollCenterY = true;
@ -350,7 +360,8 @@ define(["dom", "browser", "layoutManager"], function (dom, browser, layoutManage
scrollCenterX = scrollCenterY = false;
}
xScroller = getScrollableParent(element, false);
var xScroller = getScrollableParent(element, false);
var yScroller = getScrollableParent(element, true);
var elementRect = element.getBoundingClientRect();
@ -360,8 +371,8 @@ define(["dom", "browser", "layoutManager"], function (dom, browser, layoutManage
var xPos = getScrollerChildPos(xScroller, element, false);
var yPos = getScrollerChildPos(yScroller, element, true);
scrollX = calcScroll(xScrollerData, xPos, elementRect.width, scrollCenterX);
scrollY = calcScroll(yScrollerData, yPos, elementRect.height, scrollCenterY);
var scrollX = calcScroll(xScrollerData, xPos, elementRect.width, scrollCenterX);
var scrollY = calcScroll(yScrollerData, yPos, elementRect.height, scrollCenterY);
// HACK: Scroll to top for top menu because it is hidden
// FIXME: Need a marker to scroll top/bottom
@ -374,16 +385,6 @@ define(["dom", "browser", "layoutManager"], function (dom, browser, layoutManage
if (scrollY < minimumScrollY()) {
scrollY = 0;
}
} else {
scrollX = (options.x !== undefined ? Math.round(options.x) : xScroller.scrollLeft);
scrollY = (options.y !== undefined ? Math.round(options.y) : yScroller.scrollTop);
var xScrollerData = getScrollerData(xScroller, false);
var yScrollerData = getScrollerData(yScroller, true);
scrollX = clamp(scrollX, 0, xScrollerData.scrollSize - xScrollerData.clientSize);
scrollY = clamp(scrollY, 0, yScrollerData.scrollSize - yScrollerData.clientSize);
}
doScroll(xScroller, scrollX, yScroller, scrollY, smooth);
}
@ -391,13 +392,14 @@ define(["dom", "browser", "layoutManager"], function (dom, browser, layoutManage
if (isEnabled()) {
dom.addEventListener(window, "focusin", function(e) {
setTimeout(function() {
scrollTo({element: e.target, smooth: useSmoothScroll()});
scrollToElement(e.target, useSmoothScroll());
}, 0);
}, {capture: true});
}
return {
isEnabled: isEnabled,
scrollTo: scrollTo
scrollTo: scrollTo,
scrollToElement: scrollToElement
};
});