2020-08-14 08:46:34 +02:00
|
|
|
import browser from '../../scripts/browser';
|
|
|
|
import connectionManager from 'jellyfin-apiclient';
|
|
|
|
import playbackManager from '../playback/playbackmanager';
|
|
|
|
import dom from '../../scripts/dom';
|
|
|
|
import * as userSettings from '../../scripts/settings/userSettings';
|
|
|
|
import './backdrop.css';
|
2020-06-16 23:45:03 +03:00
|
|
|
|
|
|
|
/* eslint-disable indent */
|
2018-10-23 01:05:09 +03:00
|
|
|
|
|
|
|
function enableAnimation(elem) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (browser.slow) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function enableRotation() {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (browser.tv) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Causes high cpu usage
|
|
|
|
if (browser.firefox) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
class Backdrop {
|
|
|
|
load(url, parent, existingBackdropImage) {
|
|
|
|
const img = new Image();
|
|
|
|
const self = this;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
img.onload = () => {
|
|
|
|
if (self.isDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
const backdropImage = document.createElement('div');
|
|
|
|
backdropImage.classList.add('backdropImage');
|
|
|
|
backdropImage.classList.add('displayingBackdropImage');
|
|
|
|
backdropImage.style.backgroundImage = `url('${url}')`;
|
|
|
|
backdropImage.setAttribute('data-url', url);
|
|
|
|
|
|
|
|
backdropImage.classList.add('backdropImageFadeIn');
|
|
|
|
parent.appendChild(backdropImage);
|
|
|
|
|
|
|
|
if (!enableAnimation(backdropImage)) {
|
|
|
|
if (existingBackdropImage && existingBackdropImage.parentNode) {
|
|
|
|
existingBackdropImage.parentNode.removeChild(existingBackdropImage);
|
|
|
|
}
|
|
|
|
internalBackdrop(true);
|
|
|
|
return;
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
const onAnimationComplete = () => {
|
|
|
|
dom.removeEventListener(backdropImage, dom.whichAnimationEvent(), onAnimationComplete, {
|
|
|
|
once: true
|
|
|
|
});
|
|
|
|
if (backdropImage === self.currentAnimatingElement) {
|
|
|
|
self.currentAnimatingElement = null;
|
|
|
|
}
|
|
|
|
if (existingBackdropImage && existingBackdropImage.parentNode) {
|
|
|
|
existingBackdropImage.parentNode.removeChild(existingBackdropImage);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
dom.addEventListener(backdropImage, dom.whichAnimationEvent(), onAnimationComplete, {
|
2019-01-10 15:39:37 +03:00
|
|
|
once: true
|
|
|
|
});
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
internalBackdrop(true);
|
|
|
|
};
|
2019-10-06 19:56:06 +09:00
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
img.src = url;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
cancelAnimation() {
|
|
|
|
const elem = this.currentAnimatingElement;
|
|
|
|
if (elem) {
|
|
|
|
elem.classList.remove('backdropImageFadeIn');
|
|
|
|
this.currentAnimatingElement = null;
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
destroy() {
|
|
|
|
this.isDestroyed = true;
|
|
|
|
this.cancelAnimation();
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
let backdropContainer;
|
2018-10-23 01:05:09 +03:00
|
|
|
function getBackdropContainer() {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!backdropContainer) {
|
|
|
|
backdropContainer = document.querySelector('.backdropContainer');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!backdropContainer) {
|
|
|
|
backdropContainer = document.createElement('div');
|
|
|
|
backdropContainer.classList.add('backdropContainer');
|
|
|
|
document.body.insertBefore(backdropContainer, document.body.firstChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
return backdropContainer;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
export function clearBackdrop(clearAll) {
|
2019-01-10 15:39:37 +03:00
|
|
|
clearRotation();
|
|
|
|
|
|
|
|
if (currentLoadingBackdrop) {
|
|
|
|
currentLoadingBackdrop.destroy();
|
|
|
|
currentLoadingBackdrop = null;
|
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
const elem = getBackdropContainer();
|
2019-01-10 15:39:37 +03:00
|
|
|
elem.innerHTML = '';
|
|
|
|
|
|
|
|
if (clearAll) {
|
|
|
|
hasExternalBackdrop = false;
|
|
|
|
}
|
2019-10-06 19:56:06 +09:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
internalBackdrop(false);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
let backgroundContainer;
|
2018-10-23 01:05:09 +03:00
|
|
|
function getBackgroundContainer() {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!backgroundContainer) {
|
|
|
|
backgroundContainer = document.querySelector('.backgroundContainer');
|
|
|
|
}
|
|
|
|
return backgroundContainer;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2019-10-06 19:56:06 +09:00
|
|
|
function setBackgroundContainerBackgroundEnabled() {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (hasInternalBackdrop || hasExternalBackdrop) {
|
|
|
|
getBackgroundContainer().classList.add('withBackdrop');
|
|
|
|
} else {
|
|
|
|
getBackgroundContainer().classList.remove('withBackdrop');
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
let hasInternalBackdrop;
|
2018-10-23 01:05:09 +03:00
|
|
|
function internalBackdrop(enabled) {
|
2019-01-10 15:39:37 +03:00
|
|
|
hasInternalBackdrop = enabled;
|
|
|
|
setBackgroundContainerBackgroundEnabled();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
let hasExternalBackdrop;
|
|
|
|
export function externalBackdrop(enabled) {
|
2019-01-10 15:39:37 +03:00
|
|
|
hasExternalBackdrop = enabled;
|
|
|
|
setBackgroundContainerBackgroundEnabled();
|
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
let currentLoadingBackdrop;
|
2018-10-23 01:05:09 +03:00
|
|
|
function setBackdropImage(url) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (currentLoadingBackdrop) {
|
|
|
|
currentLoadingBackdrop.destroy();
|
|
|
|
currentLoadingBackdrop = null;
|
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
const elem = getBackdropContainer();
|
|
|
|
const existingBackdropImage = elem.querySelector('.displayingBackdropImage');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (existingBackdropImage && existingBackdropImage.getAttribute('data-url') === url) {
|
|
|
|
if (existingBackdropImage.getAttribute('data-url') === url) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
existingBackdropImage.classList.remove('displayingBackdropImage');
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
const instance = new Backdrop();
|
2019-01-10 15:39:37 +03:00
|
|
|
instance.load(url, elem, existingBackdropImage);
|
|
|
|
currentLoadingBackdrop = instance;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getItemImageUrls(item, imageOptions) {
|
|
|
|
imageOptions = imageOptions || {};
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-08-30 06:06:47 +02:00
|
|
|
const apiClient = window.connectionManager.getApiClient(item.ServerId);
|
2019-01-10 15:39:37 +03:00
|
|
|
if (item.BackdropImageTags && item.BackdropImageTags.length > 0) {
|
2020-06-16 23:45:03 +03:00
|
|
|
return item.BackdropImageTags.map((imgTag, index) => {
|
2019-01-10 15:39:37 +03:00
|
|
|
return apiClient.getScaledImageUrl(item.BackdropItemId || item.Id, Object.assign(imageOptions, {
|
2020-05-04 12:44:12 +02:00
|
|
|
type: 'Backdrop',
|
2019-01-10 15:39:37 +03:00
|
|
|
tag: imgTag,
|
2020-03-08 19:08:07 +01:00
|
|
|
maxWidth: dom.getScreenWidth(),
|
2019-01-10 15:39:37 +03:00
|
|
|
index: index
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.ParentBackdropItemId && item.ParentBackdropImageTags && item.ParentBackdropImageTags.length) {
|
2020-06-16 23:45:03 +03:00
|
|
|
return item.ParentBackdropImageTags.map((imgTag, index) => {
|
2019-01-10 15:39:37 +03:00
|
|
|
return apiClient.getScaledImageUrl(item.ParentBackdropItemId, Object.assign(imageOptions, {
|
2020-05-04 12:44:12 +02:00
|
|
|
type: 'Backdrop',
|
2019-01-10 15:39:37 +03:00
|
|
|
tag: imgTag,
|
2020-03-08 19:08:07 +01:00
|
|
|
maxWidth: dom.getScreenWidth(),
|
2019-01-10 15:39:37 +03:00
|
|
|
index: index
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getImageUrls(items, imageOptions) {
|
2020-06-16 23:45:03 +03:00
|
|
|
const list = [];
|
|
|
|
const onImg = img => {
|
2019-01-10 15:39:37 +03:00
|
|
|
list.push(img);
|
|
|
|
};
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
for (let i = 0, length = items.length; i < length; i++) {
|
|
|
|
const itemImages = getItemImageUrls(items[i], imageOptions);
|
2019-01-10 15:39:37 +03:00
|
|
|
itemImages.forEach(onImg);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
return list;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function arraysEqual(a, b) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (a === b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (a == null || b == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (a.length !== b.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you don't care about the order of the elements inside
|
|
|
|
// the array, you should sort both arrays here.
|
2020-06-16 23:45:03 +03:00
|
|
|
for (let i = 0; i < a.length; ++i) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (a[i] !== b[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-10-06 19:56:06 +09:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
return true;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-01-13 18:09:55 +03:00
|
|
|
function enabled() {
|
|
|
|
return userSettings.enableBackdrops();
|
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
let rotationInterval;
|
|
|
|
let currentRotatingImages = [];
|
|
|
|
let currentRotationIndex = -1;
|
|
|
|
export function setBackdrops(items, imageOptions, enableImageRotation) {
|
2020-01-13 18:09:55 +03:00
|
|
|
if (enabled()) {
|
2020-06-16 23:45:03 +03:00
|
|
|
const images = getImageUrls(items, imageOptions);
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-01-13 18:09:55 +03:00
|
|
|
if (images.length) {
|
|
|
|
startRotation(images, enableImageRotation);
|
|
|
|
} else {
|
|
|
|
clearBackdrop();
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function startRotation(images, enableImageRotation) {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (arraysEqual(images, currentRotatingImages)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearRotation();
|
|
|
|
|
|
|
|
currentRotatingImages = images;
|
|
|
|
currentRotationIndex = -1;
|
|
|
|
|
|
|
|
if (images.length > 1 && enableImageRotation !== false && enableRotation()) {
|
|
|
|
rotationInterval = setInterval(onRotationInterval, 24000);
|
|
|
|
}
|
2019-10-06 19:56:06 +09:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
onRotationInterval();
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onRotationInterval() {
|
2019-01-10 15:39:37 +03:00
|
|
|
if (playbackManager.isPlayingLocally(['Video'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
let newIndex = currentRotationIndex + 1;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (newIndex >= currentRotatingImages.length) {
|
|
|
|
newIndex = 0;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
currentRotationIndex = newIndex;
|
|
|
|
setBackdropImage(currentRotatingImages[newIndex]);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearRotation() {
|
2020-06-16 23:45:03 +03:00
|
|
|
const interval = rotationInterval;
|
2019-01-10 15:39:37 +03:00
|
|
|
if (interval) {
|
|
|
|
clearInterval(interval);
|
|
|
|
}
|
2019-10-06 19:56:06 +09:00
|
|
|
|
2019-01-10 15:39:37 +03:00
|
|
|
rotationInterval = null;
|
|
|
|
currentRotatingImages = [];
|
|
|
|
currentRotationIndex = -1;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
export function setBackdrop(url, imageOptions) {
|
2019-10-06 19:56:06 +09:00
|
|
|
if (url && typeof url !== 'string') {
|
|
|
|
url = getImageUrls([url], imageOptions)[0];
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
clearRotation();
|
|
|
|
setBackdropImage(url);
|
|
|
|
} else {
|
|
|
|
clearBackdrop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 23:45:03 +03:00
|
|
|
/* eslint-enable indent */
|
|
|
|
|
|
|
|
export default {
|
|
|
|
setBackdrops: setBackdrops,
|
|
|
|
setBackdrop: setBackdrop,
|
|
|
|
clearBackdrop: clearBackdrop,
|
|
|
|
externalBackdrop: externalBackdrop
|
|
|
|
};
|