2020-03-28 09:42:18 -07:00
|
|
|
/* eslint-disable indent */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module for controlling scroll behavior.
|
|
|
|
* @module components/scrollManager
|
|
|
|
*/
|
|
|
|
|
2020-05-04 03:44:12 -07:00
|
|
|
import dom from 'dom';
|
|
|
|
import browser from 'browser';
|
|
|
|
import layoutManager from 'layoutManager';
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-11-10 13:36:04 -07:00
|
|
|
/**
|
|
|
|
* Scroll time in ms.
|
|
|
|
*/
|
2020-03-28 09:42:18 -07:00
|
|
|
const ScrollTime = 270;
|
2019-11-10 13:36:04 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Epsilon for comparing values.
|
|
|
|
*/
|
2020-03-28 09:42:18 -07:00
|
|
|
const Epsilon = 1e-6;
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2019-10-19 13:36:59 -07:00
|
|
|
// FIXME: Need to scroll to top of page to fully show the top menu. This can be solved by some marker of top most elements or their containers
|
|
|
|
/**
|
|
|
|
* Returns minimum vertical scroll.
|
|
|
|
* Scroll less than that value will be zeroed.
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @return {number} Minimum vertical scroll.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
|
|
|
function minimumScrollY() {
|
2020-05-04 03:44:12 -07:00
|
|
|
const topMenu = document.querySelector('.headerTop');
|
2019-11-27 04:30:56 -07:00
|
|
|
if (topMenu) {
|
|
|
|
return topMenu.clientHeight;
|
2019-10-19 13:36:59 -07:00
|
|
|
}
|
2019-11-27 04:30:56 -07:00
|
|
|
return 0;
|
2019-10-19 13:36:59 -07:00
|
|
|
}
|
|
|
|
|
2020-05-04 03:44:12 -07:00
|
|
|
const supportsSmoothScroll = 'scrollBehavior' in document.documentElement.style;
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
let supportsScrollToOptions = false;
|
2019-10-19 13:36:59 -07:00
|
|
|
try {
|
2020-05-04 03:44:12 -07:00
|
|
|
const elem = document.createElement('div');
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2020-05-04 03:44:12 -07:00
|
|
|
const opts = Object.defineProperty({}, 'behavior', {
|
2020-02-25 23:23:28 -07:00
|
|
|
// eslint-disable-next-line getter-return
|
2019-10-19 13:36:59 -07:00
|
|
|
get: function () {
|
|
|
|
supportsScrollToOptions = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
elem.scrollTo(opts);
|
2019-11-27 04:13:16 -07:00
|
|
|
} catch (e) {
|
2020-05-04 03:44:12 -07:00
|
|
|
console.error('error checking ScrollToOptions support');
|
2019-11-27 04:13:16 -07:00
|
|
|
}
|
2019-10-19 13:36:59 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns value clamped by range [min, max].
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {number} value - Clamped value.
|
|
|
|
* @param {number} min - Begining of range.
|
|
|
|
* @param {number} max - Ending of range.
|
|
|
|
* @return {number} Clamped value.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
|
|
|
function clamp(value, min, max) {
|
|
|
|
return value <= min ? min : value >= max ? max : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the required delta to fit range 1 into range 2.
|
|
|
|
* In case of range 1 is bigger than range 2 returns delta to fit most out of range part.
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {number} begin1 - Begining of range 1.
|
|
|
|
* @param {number} end1 - Ending of range 1.
|
|
|
|
* @param {number} begin2 - Begining of range 2.
|
|
|
|
* @param {number} end2 - Ending of range 2.
|
|
|
|
* @return {number} Delta: <0 move range1 to the left, >0 - to the right.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
|
|
|
function fitRange(begin1, end1, begin2, end2) {
|
2020-03-28 09:42:18 -07:00
|
|
|
const delta1 = begin1 - begin2;
|
|
|
|
const delta2 = end2 - end1;
|
2019-10-19 13:36:59 -07:00
|
|
|
if (delta1 < 0 && delta1 < delta2) {
|
|
|
|
return -delta1;
|
|
|
|
} else if (delta2 < 0) {
|
|
|
|
return delta2;
|
|
|
|
}
|
|
|
|
return 0;
|
2019-11-27 04:13:16 -07:00
|
|
|
}
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-11-10 13:36:04 -07:00
|
|
|
/**
|
|
|
|
* Ease value.
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {number} t - Value in range [0, 1].
|
|
|
|
* @return {number} Eased value in range [0, 1].
|
2019-11-10 13:36:04 -07:00
|
|
|
*/
|
|
|
|
function ease(t) {
|
2020-05-17 09:17:47 -07:00
|
|
|
return t * (2 - t); // easeOutQuad === ease-out
|
2019-11-10 13:36:04 -07:00
|
|
|
}
|
|
|
|
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* @typedef {Object} Rect
|
|
|
|
* @property {number} left - X coordinate of top-left corner.
|
|
|
|
* @property {number} top - Y coordinate of top-left corner.
|
|
|
|
* @property {number} width - Width.
|
|
|
|
* @property {number} height - Height.
|
|
|
|
*/
|
|
|
|
|
2019-10-24 11:28:14 -07:00
|
|
|
/**
|
|
|
|
* Document scroll wrapper helps to unify scrolling and fix issues of some browsers.
|
|
|
|
*
|
|
|
|
* webOS 2 Browser: scrolls documentElement (and window), but body has a scroll size
|
|
|
|
*
|
|
|
|
* webOS 3 Browser: scrolls body (and window)
|
|
|
|
*
|
|
|
|
* webOS 4 Native: scrolls body (and window); has a document.scrollingElement
|
|
|
|
*
|
|
|
|
* Tizen 4 Browser/Native: scrolls body (and window); has a document.scrollingElement
|
|
|
|
*
|
|
|
|
* Tizen 5 Browser/Native: scrolls documentElement (and window); has a document.scrollingElement
|
|
|
|
*/
|
2020-03-28 09:42:18 -07:00
|
|
|
class DocumentScroller {
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* Horizontal scroll position.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2019-10-24 11:28:14 -07:00
|
|
|
get scrollLeft() {
|
|
|
|
return window.pageXOffset;
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
|
|
|
|
2019-10-24 11:28:14 -07:00
|
|
|
set scrollLeft(val) {
|
|
|
|
window.scroll(val, window.pageYOffset);
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
2019-10-24 11:28:14 -07:00
|
|
|
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* Vertical scroll position.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2019-10-24 11:28:14 -07:00
|
|
|
get scrollTop() {
|
|
|
|
return window.pageYOffset;
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
|
|
|
|
2019-10-24 11:28:14 -07:00
|
|
|
set scrollTop(val) {
|
|
|
|
window.scroll(window.pageXOffset, val);
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
2019-10-24 11:28:14 -07:00
|
|
|
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* Horizontal scroll size (scroll width).
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2019-10-24 11:28:14 -07:00
|
|
|
get scrollWidth() {
|
|
|
|
return Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
2019-10-24 11:28:14 -07:00
|
|
|
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* Vertical scroll size (scroll height).
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2019-10-24 11:28:14 -07:00
|
|
|
get scrollHeight() {
|
|
|
|
return Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
2019-10-24 11:28:14 -07:00
|
|
|
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* Horizontal client size (client width).
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2019-10-24 11:28:14 -07:00
|
|
|
get clientWidth() {
|
|
|
|
return Math.min(document.documentElement.clientWidth, document.body.clientWidth);
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
2019-10-24 11:28:14 -07:00
|
|
|
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* Vertical client size (client height).
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2019-10-24 11:28:14 -07:00
|
|
|
get clientHeight() {
|
|
|
|
return Math.min(document.documentElement.clientHeight, document.body.clientHeight);
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
2019-10-24 11:28:14 -07:00
|
|
|
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* Returns bounding client rect.
|
|
|
|
* @return {Rect} Bounding client rect.
|
|
|
|
*/
|
2020-03-28 09:42:18 -07:00
|
|
|
getBoundingClientRect() {
|
2019-10-24 11:28:14 -07:00
|
|
|
// Make valid viewport coordinates: documentElement.getBoundingClientRect returns rect of entire document relative to viewport
|
|
|
|
return {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: this.clientWidth,
|
|
|
|
height: this.clientHeight
|
|
|
|
};
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
2019-10-24 11:28:14 -07:00
|
|
|
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* Scrolls window.
|
|
|
|
* @param {...mixed} args See window.scrollTo.
|
|
|
|
*/
|
2020-03-28 09:42:18 -07:00
|
|
|
scrollTo() {
|
2019-10-24 11:28:14 -07:00
|
|
|
window.scrollTo.apply(window, arguments);
|
|
|
|
}
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
2019-10-24 11:28:14 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
/**
|
|
|
|
* Default (document) scroller.
|
|
|
|
*/
|
|
|
|
const documentScroller = new DocumentScroller();
|
2019-10-24 11:28:14 -07:00
|
|
|
|
2019-10-19 13:36:59 -07:00
|
|
|
/**
|
2020-03-31 08:59:12 -07:00
|
|
|
* Returns parent element that can be scrolled. If no such, returns document scroller.
|
2019-10-19 13:36:59 -07:00
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {HTMLElement} element - Element for which parent is being searched.
|
|
|
|
* @param {boolean} vertical - Search for vertical scrollable parent.
|
|
|
|
* @param {HTMLElement|DocumentScroller} Parent element that can be scrolled or document scroller.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
|
|
|
function getScrollableParent(element, vertical) {
|
|
|
|
if (element) {
|
2020-05-04 03:44:12 -07:00
|
|
|
let nameScroll = 'scrollWidth';
|
|
|
|
let nameClient = 'clientWidth';
|
|
|
|
let nameClass = 'scrollX';
|
2020-02-16 12:06:37 -07:00
|
|
|
|
|
|
|
if (vertical) {
|
2020-05-04 03:44:12 -07:00
|
|
|
nameScroll = 'scrollHeight';
|
|
|
|
nameClient = 'clientHeight';
|
|
|
|
nameClass = 'scrollY';
|
2020-02-16 12:06:37 -07:00
|
|
|
}
|
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
let parent = element.parentElement;
|
2019-10-19 13:36:59 -07:00
|
|
|
|
|
|
|
while (parent) {
|
2020-02-16 12:08:08 -07:00
|
|
|
// Skip 'emby-scroller' because it scrolls by itself
|
2020-05-04 03:44:12 -07:00
|
|
|
if (!parent.classList.contains('emby-scroller') &&
|
2020-02-16 12:08:08 -07:00
|
|
|
parent[nameScroll] > parent[nameClient] && parent.classList.contains(nameClass)) {
|
2019-10-19 13:36:59 -07:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent = parent.parentElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 11:28:14 -07:00
|
|
|
return documentScroller;
|
2019-10-19 13:36:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} ScrollerData
|
2020-03-31 08:59:12 -07:00
|
|
|
* @property {number} scrollPos - Current scroll position.
|
|
|
|
* @property {number} scrollSize - Scroll size.
|
|
|
|
* @property {number} clientSize - Client size.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2020-03-31 08:59:12 -07:00
|
|
|
* Returns scroller data for specified orientation.
|
2019-10-19 13:36:59 -07:00
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {HTMLElement} scroller - Scroller.
|
|
|
|
* @param {boolean} vertical - Vertical scroller data.
|
|
|
|
* @return {ScrollerData} Scroller data.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
|
|
|
function getScrollerData(scroller, vertical) {
|
2020-03-28 09:42:18 -07:00
|
|
|
let data = {};
|
2019-10-19 13:36:59 -07:00
|
|
|
|
|
|
|
if (!vertical) {
|
|
|
|
data.scrollPos = scroller.scrollLeft;
|
|
|
|
data.scrollSize = scroller.scrollWidth;
|
|
|
|
data.clientSize = scroller.clientWidth;
|
|
|
|
} else {
|
|
|
|
data.scrollPos = scroller.scrollTop;
|
|
|
|
data.scrollSize = scroller.scrollHeight;
|
|
|
|
data.clientSize = scroller.clientHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns position of child of scroller for specified orientation.
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {HTMLElement} scroller - Scroller.
|
|
|
|
* @param {HTMLElement} element - Child of scroller.
|
|
|
|
* @param {boolean} vertical - Vertical scroll.
|
|
|
|
* @return {number} Child position.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
|
|
|
function getScrollerChildPos(scroller, element, vertical) {
|
2020-03-28 09:42:18 -07:00
|
|
|
const elementRect = element.getBoundingClientRect();
|
|
|
|
const scrollerRect = scroller.getBoundingClientRect();
|
2019-10-19 13:36:59 -07:00
|
|
|
|
|
|
|
if (!vertical) {
|
2019-10-24 11:28:14 -07:00
|
|
|
return scroller.scrollLeft + elementRect.left - scrollerRect.left;
|
2019-10-19 13:36:59 -07:00
|
|
|
} else {
|
2019-10-24 11:28:14 -07:00
|
|
|
return scroller.scrollTop + elementRect.top - scrollerRect.top;
|
2019-10-19 13:36:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns scroll position for element.
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {ScrollerData} scrollerData - Scroller data.
|
|
|
|
* @param {number} elementPos - Child element position.
|
|
|
|
* @param {number} elementSize - Child element size.
|
|
|
|
* @param {boolean} centered - Scroll to center.
|
|
|
|
* @return {number} Scroll position.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
|
|
|
function calcScroll(scrollerData, elementPos, elementSize, centered) {
|
2020-03-28 09:42:18 -07:00
|
|
|
const maxScroll = scrollerData.scrollSize - scrollerData.clientSize;
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
let scroll;
|
2019-10-19 13:36:59 -07:00
|
|
|
|
|
|
|
if (centered) {
|
|
|
|
scroll = elementPos + (elementSize - scrollerData.clientSize) / 2;
|
|
|
|
} else {
|
2020-03-28 09:42:18 -07:00
|
|
|
const delta = fitRange(elementPos, elementPos + elementSize - 1, scrollerData.scrollPos, scrollerData.scrollPos + scrollerData.clientSize - 1);
|
2019-10-19 13:36:59 -07:00
|
|
|
scroll = scrollerData.scrollPos - delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
return clamp(Math.round(scroll), 0, maxScroll);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls scrollTo function in proper way.
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {HTMLElement} scroller - Scroller.
|
|
|
|
* @param {ScrollToOptions} options - Scroll options.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
|
|
|
function scrollToHelper(scroller, options) {
|
2020-05-04 03:44:12 -07:00
|
|
|
if ('scrollTo' in scroller) {
|
2019-10-19 13:36:59 -07:00
|
|
|
if (!supportsScrollToOptions) {
|
2020-03-28 09:42:18 -07:00
|
|
|
const scrollX = (options.left !== undefined ? options.left : scroller.scrollLeft);
|
|
|
|
const scrollY = (options.top !== undefined ? options.top : scroller.scrollTop);
|
2019-10-19 13:36:59 -07:00
|
|
|
scroller.scrollTo(scrollX, scrollY);
|
|
|
|
} else {
|
|
|
|
scroller.scrollTo(options);
|
|
|
|
}
|
2020-05-04 03:44:12 -07:00
|
|
|
} else if ('scrollLeft' in scroller) {
|
2019-10-19 13:36:59 -07:00
|
|
|
if (options.left !== undefined) {
|
|
|
|
scroller.scrollLeft = options.left;
|
|
|
|
}
|
|
|
|
if (options.top !== undefined) {
|
|
|
|
scroller.scrollTop = options.top;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 12:35:45 -07:00
|
|
|
/**
|
|
|
|
* Performs built-in scroll.
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {HTMLElement} xScroller - Horizontal scroller.
|
|
|
|
* @param {number} scrollX - Horizontal coordinate.
|
|
|
|
* @param {HTMLElement} yScroller - Vertical scroller.
|
|
|
|
* @param {number} scrollY - Vertical coordinate.
|
|
|
|
* @param {boolean} smooth - Smooth scrolling.
|
2019-10-23 12:35:45 -07:00
|
|
|
*/
|
|
|
|
function builtinScroll(xScroller, scrollX, yScroller, scrollY, smooth) {
|
2020-05-04 03:44:12 -07:00
|
|
|
const scrollBehavior = smooth ? 'smooth' : 'instant';
|
2019-10-23 12:35:45 -07:00
|
|
|
|
|
|
|
if (xScroller !== yScroller) {
|
|
|
|
scrollToHelper(xScroller, {left: scrollX, behavior: scrollBehavior});
|
|
|
|
scrollToHelper(yScroller, {top: scrollY, behavior: scrollBehavior});
|
|
|
|
} else {
|
|
|
|
scrollToHelper(xScroller, {left: scrollX, top: scrollY, behavior: scrollBehavior});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 08:59:12 -07:00
|
|
|
/**
|
|
|
|
* Requested frame for animated scroll.
|
|
|
|
*/
|
2020-03-28 09:42:18 -07:00
|
|
|
let scrollTimer;
|
2019-10-20 04:53:12 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resets scroll timer to stop scrolling.
|
|
|
|
*/
|
|
|
|
function resetScrollTimer() {
|
|
|
|
cancelAnimationFrame(scrollTimer);
|
|
|
|
scrollTimer = undefined;
|
|
|
|
}
|
|
|
|
|
2019-10-19 13:36:59 -07:00
|
|
|
/**
|
2019-10-22 12:19:43 -07:00
|
|
|
* Performs animated scroll.
|
2019-10-19 13:36:59 -07:00
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {HTMLElement} xScroller - Horizontal scroller.
|
|
|
|
* @param {number} scrollX - Horizontal coordinate.
|
|
|
|
* @param {HTMLElement} yScroller - Vertical scroller.
|
|
|
|
* @param {number} scrollY - Vertical coordinate.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
2019-10-22 12:19:43 -07:00
|
|
|
function animateScroll(xScroller, scrollX, yScroller, scrollY) {
|
2019-11-10 13:36:04 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
const ox = xScroller.scrollLeft;
|
|
|
|
const oy = yScroller.scrollTop;
|
|
|
|
const dx = scrollX - ox;
|
|
|
|
const dy = scrollY - oy;
|
2019-11-10 13:36:04 -07:00
|
|
|
|
|
|
|
if (Math.abs(dx) < Epsilon && Math.abs(dy) < Epsilon) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
let start;
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-22 12:19:43 -07:00
|
|
|
function scrollAnim(currentTimestamp) {
|
2019-11-10 13:36:04 -07:00
|
|
|
|
2019-10-22 12:19:43 -07:00
|
|
|
start = start || currentTimestamp;
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
let k = Math.min(1, (currentTimestamp - start) / ScrollTime);
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2019-11-10 13:36:04 -07:00
|
|
|
if (k === 1) {
|
2019-10-22 12:19:43 -07:00
|
|
|
resetScrollTimer();
|
2019-11-27 04:30:56 -07:00
|
|
|
builtinScroll(xScroller, scrollX, yScroller, scrollY, false);
|
2019-10-22 12:19:43 -07:00
|
|
|
return;
|
|
|
|
}
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2019-11-10 13:36:04 -07:00
|
|
|
k = ease(k);
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2020-05-17 09:17:47 -07:00
|
|
|
const x = ox + dx * k;
|
|
|
|
const y = oy + dy * k;
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2019-11-10 13:36:04 -07:00
|
|
|
builtinScroll(xScroller, x, yScroller, y, false);
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-22 12:19:43 -07:00
|
|
|
scrollTimer = requestAnimationFrame(scrollAnim);
|
2019-11-27 04:13:16 -07:00
|
|
|
}
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2019-10-22 12:19:43 -07:00
|
|
|
scrollTimer = requestAnimationFrame(scrollAnim);
|
|
|
|
}
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2019-10-22 12:19:43 -07:00
|
|
|
/**
|
|
|
|
* Performs scroll.
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {HTMLElement} xScroller - Horizontal scroller.
|
|
|
|
* @param {number} scrollX - Horizontal coordinate.
|
|
|
|
* @param {HTMLElement} yScroller - Vertical scroller.
|
|
|
|
* @param {number} scrollY - Vertical coordinate.
|
|
|
|
* @param {boolean} smooth - Smooth scrolling.
|
2019-10-22 12:19:43 -07:00
|
|
|
*/
|
|
|
|
function doScroll(xScroller, scrollX, yScroller, scrollY, smooth) {
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2019-10-22 12:19:43 -07:00
|
|
|
resetScrollTimer();
|
|
|
|
|
|
|
|
if (smooth && useAnimatedScroll()) {
|
|
|
|
animateScroll(xScroller, scrollX, yScroller, scrollY);
|
2019-10-19 13:36:59 -07:00
|
|
|
} else {
|
2019-10-23 12:35:45 -07:00
|
|
|
builtinScroll(xScroller, scrollX, yScroller, scrollY, smooth);
|
2019-10-19 13:36:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if smooth scroll must be used.
|
|
|
|
*/
|
|
|
|
function useSmoothScroll() {
|
|
|
|
|
2020-02-01 10:28:57 -07:00
|
|
|
if (browser.tizen) {
|
2019-10-19 13:36:59 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-11-27 04:13:16 -07:00
|
|
|
}
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-20 04:53:12 -07:00
|
|
|
/**
|
|
|
|
* Returns true if animated implementation of smooth scroll must be used.
|
|
|
|
*/
|
|
|
|
function useAnimatedScroll() {
|
|
|
|
// Add block to force using (or not) of animated implementation
|
|
|
|
|
|
|
|
return !supportsSmoothScroll;
|
2019-11-27 04:13:16 -07:00
|
|
|
}
|
2019-10-20 04:53:12 -07:00
|
|
|
|
2019-10-19 13:36:59 -07:00
|
|
|
/**
|
|
|
|
* Returns true if scroll manager is enabled.
|
|
|
|
*/
|
2020-03-28 09:42:18 -07:00
|
|
|
export function isEnabled() {
|
2020-01-09 14:24:45 -07:00
|
|
|
return layoutManager.tv;
|
2020-03-28 09:42:18 -07:00
|
|
|
}
|
2019-10-19 13:36:59 -07:00
|
|
|
|
|
|
|
/**
|
2019-10-22 13:46:21 -07:00
|
|
|
* Scrolls the document to a given position.
|
2019-10-19 13:36:59 -07:00
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {number} scrollX - Horizontal coordinate.
|
|
|
|
* @param {number} scrollY - Vertical coordinate.
|
|
|
|
* @param {boolean} [smooth=false] - Smooth scrolling.
|
2019-10-19 13:36:59 -07:00
|
|
|
*/
|
2020-03-28 09:42:18 -07:00
|
|
|
export function scrollTo(scrollX, scrollY, smooth) {
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-22 13:46:21 -07:00
|
|
|
smooth = !!smooth;
|
2019-10-19 13:36:59 -07:00
|
|
|
|
|
|
|
// Scroller is document itself by default
|
2020-03-28 09:42:18 -07:00
|
|
|
const scroller = getScrollableParent(null, false);
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
const xScrollerData = getScrollerData(scroller, false);
|
|
|
|
const yScrollerData = getScrollerData(scroller, true);
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-22 13:46:21 -07:00
|
|
|
scrollX = clamp(Math.round(scrollX), 0, xScrollerData.scrollSize - xScrollerData.clientSize);
|
|
|
|
scrollY = clamp(Math.round(scrollY), 0, yScrollerData.scrollSize - yScrollerData.clientSize);
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-22 13:46:21 -07:00
|
|
|
doScroll(scroller, scrollX, scroller, scrollY, smooth);
|
|
|
|
}
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-22 13:46:21 -07:00
|
|
|
/**
|
|
|
|
* Scrolls the document to a given element.
|
|
|
|
*
|
2020-03-31 08:59:12 -07:00
|
|
|
* @param {HTMLElement} element - Target element of scroll task.
|
|
|
|
* @param {boolean} [smooth=false] - Smooth scrolling.
|
2019-10-22 13:46:21 -07:00
|
|
|
*/
|
2020-03-28 09:42:18 -07:00
|
|
|
export function scrollToElement(element, smooth) {
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-22 13:46:21 -07:00
|
|
|
smooth = !!smooth;
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
let scrollCenterX = true;
|
|
|
|
let scrollCenterY = true;
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
const offsetParent = element.offsetParent;
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-23 12:38:01 -07:00
|
|
|
// In Firefox offsetParent.offsetParent is BODY
|
2020-05-04 03:44:12 -07:00
|
|
|
const isFixed = offsetParent && (!offsetParent.offsetParent || window.getComputedStyle(offsetParent).position === 'fixed');
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-22 13:46:21 -07:00
|
|
|
// Scroll fixed elements to nearest edge (or do not scroll at all)
|
|
|
|
if (isFixed) {
|
|
|
|
scrollCenterX = scrollCenterY = false;
|
|
|
|
}
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
const xScroller = getScrollableParent(element, false);
|
|
|
|
const yScroller = getScrollableParent(element, true);
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
const elementRect = element.getBoundingClientRect();
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
const xScrollerData = getScrollerData(xScroller, false);
|
|
|
|
const yScrollerData = getScrollerData(yScroller, true);
|
2019-10-22 13:46:21 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
const xPos = getScrollerChildPos(xScroller, element, false);
|
|
|
|
const yPos = getScrollerChildPos(yScroller, element, true);
|
2019-10-22 13:46:21 -07:00
|
|
|
|
2020-03-28 09:42:18 -07:00
|
|
|
const scrollX = calcScroll(xScrollerData, xPos, elementRect.width, scrollCenterX);
|
|
|
|
let scrollY = calcScroll(yScrollerData, yPos, elementRect.height, scrollCenterY);
|
2019-10-22 13:46:21 -07:00
|
|
|
|
|
|
|
// HACK: Scroll to top for top menu because it is hidden
|
|
|
|
// FIXME: Need a marker to scroll top/bottom
|
|
|
|
if (isFixed && elementRect.bottom < 0) {
|
|
|
|
scrollY = 0;
|
|
|
|
}
|
2019-10-19 13:36:59 -07:00
|
|
|
|
2019-10-22 13:46:21 -07:00
|
|
|
// HACK: Ensure we are at the top
|
|
|
|
// FIXME: Need a marker to scroll top/bottom
|
2019-11-21 11:48:35 -07:00
|
|
|
if (scrollY < minimumScrollY() && yScroller === documentScroller) {
|
2019-10-22 13:46:21 -07:00
|
|
|
scrollY = 0;
|
2019-10-19 13:36:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
doScroll(xScroller, scrollX, yScroller, scrollY, smooth);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEnabled()) {
|
2020-05-04 03:44:12 -07:00
|
|
|
dom.addEventListener(window, 'focusin', function(e) {
|
2019-10-19 13:36:59 -07:00
|
|
|
setTimeout(function() {
|
2019-10-22 13:46:21 -07:00
|
|
|
scrollToElement(e.target, useSmoothScroll());
|
2019-10-19 13:36:59 -07:00
|
|
|
}, 0);
|
|
|
|
}, {capture: true});
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:45:45 -07:00
|
|
|
/* eslint-enable indent */
|
|
|
|
|
|
|
|
export default {
|
|
|
|
isEnabled: isEnabled,
|
|
|
|
scrollTo: scrollTo,
|
|
|
|
scrollToElement: scrollToElement
|
|
|
|
};
|