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

178 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-01-23 11:33:34 +00:00
define(['layoutManager', 'dom', 'css!./emby-scrollbuttons', 'registerElement', 'paper-icon-button-light'], function (layoutManager, dom) {
'use strict';
var EmbyScrollButtonsPrototype = Object.create(HTMLDivElement.prototype);
EmbyScrollButtonsPrototype.createdCallback = function () {};
function getScrollButtonHtml(direction) {
var html = '';
var icon = direction === 'left' ? '' : '';
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">';
html += '<i class="md-icon">' + icon + '</i>';
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;
}
2019-05-23 00:04:48 -07:00
var scrollPosEnd = scrollPos + scrollSize;
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) {
var scrollButtons = this;
var scroller = this.scroller;
2019-05-23 00:04:48 -07:00
var scrollSize = getScrollSize(scroller);
var scrollPos = getScrollPosition(scroller);
var 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) {
var 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) {
var scrollSize = elem.offsetWidth;
var style = window.getComputedStyle(elem, null);
var paddingLeft = getStyleValue(style, 'padding-left');
if (paddingLeft) {
scrollSize -= paddingLeft;
}
var paddingRight = getStyleValue(style, 'padding-right');
if (paddingRight) {
scrollSize -= paddingRight;
}
2018-10-23 01:05:09 +03:00
var 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) {
var scroller = this.parentNode.nextSibling;
var direction = this.getAttribute('data-direction');
var scrollSize = getScrollSize(scroller);
2019-05-23 00:04:48 -07:00
var scrollPos = getScrollPosition(scroller);
var scrollWidth = getScrollWidth(scroller);
var 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 () {
var scroller = this.nextSibling;
this.scroller = scroller;
var parent = this.parentNode;
parent.classList.add('emby-scroller-container');
this.innerHTML = getScrollButtonHtml('left') + getScrollButtonHtml('right');
var buttons = this.querySelectorAll('.emby-scrollbuttons-button');
buttons[0].addEventListener('click', onScrollButtonClick);
buttons[1].addEventListener('click', onScrollButtonClick);
this.scrollButtonsLeft = buttons[0];
this.scrollButtonsRight = buttons[1];
var scrollHandler = onScroll.bind(this);
this.scrollHandler = scrollHandler;
scroller.addScrollEventListener(scrollHandler, {
capture: false,
passive: true
});
};
EmbyScrollButtonsPrototype.detachedCallback = function () {
2018-10-23 01:05:09 +03:00
var parent = this.scroller;
this.scroller = null;
2018-10-23 01:05:09 +03:00
var 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'
});
2018-10-23 01:05:09 +03:00
});