1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

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