2020-07-24 10:23:14 +02:00
|
|
|
<<<<<<< HEAD
|
2020-07-08 16:40:30 +01:00
|
|
|
import layoutManager from 'layoutManager';
|
|
|
|
import dom from 'dom';
|
|
|
|
import 'css!./emby-scrollbuttons';
|
|
|
|
import 'registerElement';
|
|
|
|
import 'paper-icon-button-light';
|
2020-07-24 10:23:14 +02:00
|
|
|
=======
|
2020-07-05 12:06:53 +02:00
|
|
|
define(['layoutManager', 'dom', 'css!./emby-scrollbuttons', 'webcomponents', 'paper-icon-button-light'], function (layoutManager, dom) {
|
2019-01-10 15:39:37 +03:00
|
|
|
'use strict';
|
2020-07-24 10:23:14 +02:00
|
|
|
>>>>>>> upstream/master
|
2020-07-08 16:40:30 +01:00
|
|
|
|
|
|
|
/* eslint-disable indent */
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
const EmbyScrollButtonsPrototype = Object.create(HTMLDivElement.prototype);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2019-05-09 17:55:00 -07:00
|
|
|
EmbyScrollButtonsPrototype.createdCallback = function () {};
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2019-05-09 17:55:00 -07:00
|
|
|
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
|
|
|
|
2019-05-09 17:55:00 -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>';
|
2019-01-10 15:39:37 +03:00
|
|
|
html += '</button>';
|
|
|
|
|
|
|
|
return html;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getScrollPosition(parent) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (parent.getScrollPosition) {
|
|
|
|
return parent.getScrollPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getScrollWidth(parent) {
|
2019-01-10 15:39:37 +03:00
|
|
|
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) {
|
2019-07-02 00:12:04 -07:00
|
|
|
// 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) {
|
2019-05-09 17:55:00 -07:00
|
|
|
scrollButtons.scrollButtonsLeft.disabled = false;
|
2019-01-10 15:39:37 +03:00
|
|
|
} else {
|
2019-05-09 17:55:00 -07:00
|
|
|
scrollButtons.scrollButtonsLeft.disabled = true;
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
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;
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
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-01-10 15:39:37 +03:00
|
|
|
|
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);
|
2019-01-10 15:39:37 +03:00
|
|
|
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);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
let paddingLeft = getStyleValue(style, 'padding-left');
|
2019-01-10 15:39:37 +03:00
|
|
|
if (paddingLeft) {
|
|
|
|
scrollSize -= paddingLeft;
|
|
|
|
}
|
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
let paddingRight = getStyleValue(style, 'padding-right');
|
2019-01-10 15:39:37 +03:00
|
|
|
if (paddingRight) {
|
|
|
|
scrollSize -= paddingRight;
|
|
|
|
}
|
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
const slider = elem.getScrollSlider();
|
2019-01-10 15:39:37 +03:00
|
|
|
style = window.getComputedStyle(slider, null);
|
|
|
|
|
|
|
|
paddingLeft = getStyleValue(style, 'padding-left');
|
|
|
|
if (paddingLeft) {
|
|
|
|
scrollSize -= paddingLeft;
|
|
|
|
}
|
|
|
|
|
2019-05-09 17:55:00 -07:00
|
|
|
paddingRight = getStyleValue(style, 'padding-right');
|
2019-01-10 15:39:37 +03:00
|
|
|
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;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
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);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
let newPos;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (direction === 'left') {
|
2019-05-23 00:04:48 -07:00
|
|
|
newPos = Math.max(0, scrollPos - scrollSize);
|
2019-01-10 15:39:37 +03:00
|
|
|
} else {
|
2019-05-23 00:04:48 -07:00
|
|
|
newPos = scrollPos + scrollSize;
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2019-05-09 17:55:00 -07:00
|
|
|
scroller.scrollToPosition(newPos, false);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
EmbyScrollButtonsPrototype.attachedCallback = function () {
|
2020-07-11 09:52:35 +01:00
|
|
|
const scroller = this.nextSibling;
|
2019-05-09 17:55:00 -07:00
|
|
|
this.scroller = scroller;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
const parent = this.parentNode;
|
2019-05-09 17:55:00 -07:00
|
|
|
parent.classList.add('emby-scroller-container');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2019-05-09 17:55:00 -07:00
|
|
|
this.innerHTML = getScrollButtonHtml('left') + getScrollButtonHtml('right');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
const buttons = this.querySelectorAll('.emby-scrollbuttons-button');
|
2019-01-10 15:39:37 +03:00
|
|
|
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);
|
2019-10-03 01:34:20 +09:00
|
|
|
this.scrollHandler = scrollHandler;
|
2019-05-09 17:55:00 -07:00
|
|
|
scroller.addScrollEventListener(scrollHandler, {
|
2019-01-10 15:39:37 +03:00
|
|
|
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;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
let scrollHandler = this.scrollHandler;
|
2019-01-10 15:39:37 +03:00
|
|
|
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,
|
2019-01-10 15:39:37 +03:00
|
|
|
extends: 'div'
|
|
|
|
});
|
2020-07-08 16:40:30 +01:00
|
|
|
|
|
|
|
/* eslint-enable indent */
|