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

185 lines
5.4 KiB
JavaScript
Raw Normal View History

import layoutManager from 'layoutManager';
import dom from 'dom';
import 'css!./emby-scrollbuttons';
import 'registerElement';
import 'paper-icon-button-light';
/* eslint-disable indent */
2020-07-11 09:52:35 +01:00
const EmbyScrollButtonsPrototype = Object.create(HTMLDivElement.prototype);
EmbyScrollButtonsPrototype.createdCallback = function () {};
function getScrollButtonHtml(direction) {
2020-07-11 09:52:35 +01:00
let html = '';
const icon = direction === 'left' ? 'chevron_left' : 'chevron_right';
2019-05-23 00:04:48 -07:00
html += '<button type="button" is="paper-icon-button-light" data-ripple="false" data-direction="' + direction + '" class="emby-scrollbuttons-button">';
2020-04-26 02:37:28 +03:00
html += '<span class="material-icons ' + icon + '"></span>';
html += '</button>';
return html;
2018-10-23 01:05:09 +03:00
}
function getScrollPosition(parent) {
if (parent.getScrollPosition) {
return parent.getScrollPosition();
}
return 0;
2018-10-23 01:05:09 +03:00
}
function getScrollWidth(parent) {
if (parent.getScrollSize) {
return parent.getScrollSize();
}
return 0;
2018-10-23 01:05:09 +03:00
}
2019-05-23 00:04:48 -07:00
function updateScrollButtons(scrollButtons, scrollSize, scrollPos, scrollWidth) {
// hack alert add twenty for rounding errors
if (scrollWidth <= scrollSize + 20) {
2019-05-23 00:04:48 -07:00
scrollButtons.scrollButtonsLeft.classList.add('hide');
scrollButtons.scrollButtonsRight.classList.add('hide');
}
if (scrollPos > 0) {
scrollButtons.scrollButtonsLeft.disabled = false;
} else {
scrollButtons.scrollButtonsLeft.disabled = true;
}
2020-07-11 09:52:35 +01:00
const scrollPosEnd = scrollPos + scrollSize;
2019-05-23 00:04:48 -07:00
if (scrollWidth > 0 && scrollPosEnd >= scrollWidth) {
scrollButtons.scrollButtonsRight.disabled = true;
} else {
scrollButtons.scrollButtonsRight.disabled = false;
}
2018-10-23 01:05:09 +03:00
}
function onScroll(e) {
2020-07-11 09:52:35 +01:00
const scrollButtons = this;
const scroller = this.scroller;
2019-05-23 00:04:48 -07:00
2020-07-11 09:52:35 +01:00
const scrollSize = getScrollSize(scroller);
const scrollPos = getScrollPosition(scroller);
const scrollWidth = getScrollWidth(scroller);
2019-05-23 00:04:48 -07:00
updateScrollButtons(scrollButtons, scrollSize, scrollPos, scrollWidth);
2018-10-23 01:05:09 +03:00
}
function getStyleValue(style, name) {
2020-07-11 09:52:35 +01:00
let value = style.getPropertyValue(name);
if (!value) {
return 0;
}
value = value.replace('px', '');
if (!value) {
return 0;
}
value = parseInt(value);
if (isNaN(value)) {
return 0;
}
return value;
2018-10-23 01:05:09 +03:00
}
function getScrollSize(elem) {
2020-07-11 09:52:35 +01:00
let scrollSize = elem.offsetWidth;
let style = window.getComputedStyle(elem, null);
2020-07-11 09:52:35 +01:00
let paddingLeft = getStyleValue(style, 'padding-left');
if (paddingLeft) {
scrollSize -= paddingLeft;
}
2020-07-11 09:52:35 +01:00
let paddingRight = getStyleValue(style, 'padding-right');
if (paddingRight) {
scrollSize -= paddingRight;
}
2020-07-11 09:52:35 +01:00
const slider = elem.getScrollSlider();
style = window.getComputedStyle(slider, null);
paddingLeft = getStyleValue(style, 'padding-left');
if (paddingLeft) {
scrollSize -= paddingLeft;
}
paddingRight = getStyleValue(style, 'padding-right');
if (paddingRight) {
scrollSize -= paddingRight;
}
return scrollSize;
2018-10-23 01:05:09 +03:00
}
function onScrollButtonClick(e) {
2020-07-11 09:52:35 +01:00
let scroller = this.parentNode.nextSibling;
2020-07-11 09:52:35 +01:00
const direction = this.getAttribute('data-direction');
const scrollSize = getScrollSize(scroller);
const scrollPos = getScrollPosition(scroller);
const scrollWidth = getScrollWidth(scroller);
2020-07-11 09:52:35 +01:00
let newPos;
if (direction === 'left') {
2019-05-23 00:04:48 -07:00
newPos = Math.max(0, scrollPos - scrollSize);
} else {
2019-05-23 00:04:48 -07:00
newPos = scrollPos + scrollSize;
}
scroller.scrollToPosition(newPos, false);
2018-10-23 01:05:09 +03:00
}
EmbyScrollButtonsPrototype.attachedCallback = function () {
2020-07-11 09:52:35 +01:00
const scroller = this.nextSibling;
this.scroller = scroller;
2020-07-11 09:52:35 +01:00
const parent = this.parentNode;
parent.classList.add('emby-scroller-container');
this.innerHTML = getScrollButtonHtml('left') + getScrollButtonHtml('right');
2020-07-11 09:52:35 +01:00
const buttons = this.querySelectorAll('.emby-scrollbuttons-button');
buttons[0].addEventListener('click', onScrollButtonClick);
buttons[1].addEventListener('click', onScrollButtonClick);
this.scrollButtonsLeft = buttons[0];
this.scrollButtonsRight = buttons[1];
2020-07-11 09:52:35 +01:00
const scrollHandler = onScroll.bind(this);
this.scrollHandler = scrollHandler;
scroller.addScrollEventListener(scrollHandler, {
capture: false,
passive: true
});
};
EmbyScrollButtonsPrototype.detachedCallback = function () {
2020-07-11 09:52:35 +01:00
const parent = this.scroller;
2018-10-23 01:05:09 +03:00
this.scroller = null;
2020-07-11 09:52:35 +01:00
let scrollHandler = this.scrollHandler;
if (parent && scrollHandler) {
parent.removeScrollEventListener(scrollHandler, {
capture: false,
passive: true
});
}
this.scrollHandler = null;
this.scrollButtonsLeft = null;
this.scrollButtonsRight = null;
};
document.registerElement('emby-scrollbuttons', {
2018-10-23 01:05:09 +03:00
prototype: EmbyScrollButtonsPrototype,
extends: 'div'
});
/* eslint-enable indent */