jellyfish-web/src/components/slideshow/slideshow.js

507 lines
16 KiB
JavaScript
Raw Normal View History

2020-03-21 23:45:23 +01:00
define(['dialogHelper', 'inputManager', 'connectionManager', 'layoutManager', 'focusManager', 'browser', 'apphost', 'css!./style', 'material-icons', 'paper-icon-button-light'], function (dialogHelper, inputManager, connectionManager, layoutManager, focusManager, browser, appHost) {
'use strict';
2018-10-23 01:05:09 +03:00
function getImageUrl(item, options, apiClient) {
options = options || {};
options.type = options.type || "Primary";
if (typeof (item) === 'string') {
return apiClient.getScaledImageUrl(item, options);
}
if (item.ImageTags && item.ImageTags[options.type]) {
options.tag = item.ImageTags[options.type];
return apiClient.getScaledImageUrl(item.Id, options);
}
if (options.type === 'Primary') {
if (item.AlbumId && item.AlbumPrimaryImageTag) {
options.tag = item.AlbumPrimaryImageTag;
return apiClient.getScaledImageUrl(item.AlbumId, options);
}
}
return null;
2018-10-23 01:05:09 +03:00
}
function getBackdropImageUrl(item, options, apiClient) {
options = options || {};
options.type = options.type || "Backdrop";
// If not resizing, get the original image
if (!options.maxWidth && !options.width && !options.maxHeight && !options.height) {
options.quality = 100;
}
if (item.BackdropImageTags && item.BackdropImageTags.length) {
options.tag = item.BackdropImageTags[0];
return apiClient.getScaledImageUrl(item.Id, options);
}
return null;
2018-10-23 01:05:09 +03:00
}
2020-03-21 23:45:23 +01:00
function getImgUrl(item) {
var apiClient = connectionManager.getApiClient(item.ServerId);
var imageOptions = {};
if (item.BackdropImageTags && item.BackdropImageTags.length) {
return getBackdropImageUrl(item, imageOptions, apiClient);
} else {
2020-03-21 23:45:23 +01:00
if (item.MediaType === 'Photo') {
return apiClient.getItemDownloadUrl(item.Id);
}
imageOptions.type = "Primary";
return getImageUrl(item, imageOptions, apiClient);
}
2018-10-23 01:05:09 +03:00
}
function getIcon(icon, cssClass, canFocus, autoFocus) {
var tabIndex = canFocus ? '' : ' tabindex="-1"';
autoFocus = autoFocus ? ' autofocus' : '';
2020-03-10 12:57:09 +03:00
return '<button is="paper-icon-button-light" class="autoSize ' + cssClass + '"' + tabIndex + autoFocus + '><i class="material-icons slideshowButtonIcon ' + icon + '"></i></button>';
2018-10-23 01:05:09 +03:00
}
function setUserScalable(scalable) {
try {
appHost.setUserScalable(scalable);
} catch (err) {
2020-02-16 03:44:43 +01:00
console.error('error in appHost.setUserScalable: ' + err);
2018-10-23 01:05:09 +03:00
}
}
return function (options) {
var self = this;
var swiperInstance;
var dlg;
var currentTimeout;
var currentOptions;
2020-03-21 23:45:23 +01:00
var _osdOpen = false;
// small hack since this is not possible anyway
if (browser.chromecast) {
options.interactive = false;
}
2018-10-23 01:05:09 +03:00
function createElements(options) {
2020-03-21 23:45:23 +01:00
currentOptions = options;
2018-10-23 01:05:09 +03:00
dlg = dialogHelper.createDialog({
exitAnimationDuration: options.interactive ? 400 : 800,
size: 'fullscreen',
autoFocus: false,
scrollY: false,
exitAnimation: 'fadeout',
removeOnClose: true
});
dlg.classList.add('slideshowDialog');
var html = '';
2018-10-23 01:05:09 +03:00
if (options.interactive) {
var actionButtonsOnTop = layoutManager.mobile;
html += '<div class="slideshowSwiperContainer"><div class="swiper-wrapper"></div></div>';
html += getIcon('keyboard_arrow_left', 'btnSlideshowPrevious slideshowButton hide-mouse-idle-tv', false);
html += getIcon('keyboard_arrow_right', 'btnSlideshowNext slideshowButton hide-mouse-idle-tv', false);
html += '<div class="topActionButtons">';
if (actionButtonsOnTop) {
if (appHost.supports('filedownload')) {
html += getIcon('file_download', 'btnDownload slideshowButton', true);
}
if (appHost.supports('sharing')) {
html += getIcon('share', 'btnShare slideshowButton', true);
}
}
html += getIcon('close', 'slideshowButton btnSlideshowExit hide-mouse-idle-tv', false);
html += '</div>';
if (!actionButtonsOnTop) {
html += '<div class="slideshowBottomBar hide">';
2020-03-21 23:45:23 +01:00
html += getIcon('play_arrow', 'btnSlideshowPause slideshowButton', true, true);
if (appHost.supports('filedownload')) {
html += getIcon('file_download', 'btnDownload slideshowButton', true);
}
if (appHost.supports('sharing')) {
html += getIcon('share', 'btnShare slideshowButton', true);
}
html += '</div>';
}
} else {
html += '<div class="slideshowImage"></div><h1 class="slideshowImageText"></h1>';
}
dlg.innerHTML = html;
if (options.interactive) {
dlg.querySelector('.btnSlideshowExit').addEventListener('click', function (e) {
dialogHelper.close(dlg);
});
var btnPause = dlg.querySelector('.btnSlideshowPause');
if (btnPause) {
btnPause.addEventListener('click', playPause);
}
var btnDownload = dlg.querySelector('.btnDownload');
if (btnDownload) {
btnDownload.addEventListener('click', download);
}
var btnShare = dlg.querySelector('.btnShare');
if (btnShare) {
btnShare.addEventListener('click', share);
}
}
setUserScalable(true);
dialogHelper.open(dlg).then(function () {
setUserScalable(false);
stopInterval();
});
2019-10-03 02:36:33 +09:00
inputManager.on(window, onInputCommand);
document.addEventListener((window.PointerEvent ? 'pointermove' : 'mousemove'), onPointerMove);
dlg.addEventListener('close', onDialogClosed);
if (options.interactive) {
loadSwiper(dlg);
2018-10-23 01:05:09 +03:00
}
}
function onAutoplayStart() {
var btnSlideshowPause = dlg.querySelector('.btnSlideshowPause i');
if (btnSlideshowPause) {
2020-03-21 23:45:23 +01:00
btnSlideshowPause.classList.replace("play_arrow", "pause");
}
}
function onAutoplayStop() {
var btnSlideshowPause = dlg.querySelector('.btnSlideshowPause i');
if (btnSlideshowPause) {
2020-03-21 23:45:23 +01:00
btnSlideshowPause.classList.replace("pause", "play_arrow");
}
}
2018-10-23 01:05:09 +03:00
function loadSwiper(dlg) {
2020-03-21 23:45:23 +01:00
var slides;
if (currentOptions.slides) {
2020-03-21 23:45:23 +01:00
slides = currentOptions.slides;
} else {
2020-03-21 23:45:23 +01:00
slides = currentOptions.items;
}
require(['swiper'], function (Swiper) {
swiperInstance = new Swiper(dlg.querySelector('.slideshowSwiperContainer'), {
// Optional parameters
direction: 'horizontal',
2020-03-21 23:45:23 +01:00
loop: false,
autoplay: false,
keyboard: {
enabled: true
2020-02-09 13:39:19 +01:00
},
2020-03-21 23:45:23 +01:00
preloadImages: true,
slidesPerView: 1,
slidesPerColumn: 1,
2018-10-23 01:05:09 +03:00
initialSlide: options.startIndex || 0,
2020-03-21 23:45:23 +01:00
speed: 240,
navigation: {
nextEl: '.btnSlideshowNext',
prevEl: '.btnSlideshowPrevious'
},
virtual: {
slides: slides,
cache: true,
renderSlide: getSwiperSlideHtml,
addSlidesBefore: 1,
addSlidesAfter: 1
}
});
swiperInstance.on('autoplayStart', onAutoplayStart);
swiperInstance.on('autoplayStop', onAutoplayStop);
});
2018-10-23 01:05:09 +03:00
}
2020-03-21 23:45:23 +01:00
function getSwiperSlideHtml(item, index) {
if (currentOptions.slides) {
return getSwiperSlideHtmlFromSlide(item);
} else {
return getSwiperSlideHtmlFromItem(item);
}
}
2020-03-21 23:45:23 +01:00
function getSwiperSlideHtmlFromItem(item) {
2018-10-23 01:05:09 +03:00
return getSwiperSlideHtmlFromSlide({
imageUrl: getImgUrl(item),
originalImage: getImgUrl(item, true),
//title: item.Name,
//description: item.Overview
2018-10-23 01:05:09 +03:00
Id: item.Id,
ServerId: item.ServerId
});
2018-10-23 01:05:09 +03:00
}
function getSwiperSlideHtmlFromSlide(item) {
var html = '';
2020-03-21 23:45:23 +01:00
html += '<div class="swiper-slide" data-original="' + item.originalImage + '" data-itemid="' + item.Id + '" data-serverid="' + item.ServerId + '">';
html += '<div class="slider-zoom-container">';
html += '<img src="' + item.originalImage + '" class="swiper-slide-img">';
html += '</div>';
if (item.title || item.subtitle) {
html += '<div class="slideText">';
html += '<div class="slideTextInner">';
if (item.title) {
html += '<h1 class="slideTitle">';
html += item.title;
html += '</h1>';
}
if (item.description) {
html += '<div class="slideSubtitle">';
html += item.description;
html += '</div>';
}
html += '</div>';
html += '</div>';
}
html += '</div>';
return html;
2018-10-23 01:05:09 +03:00
}
function getCurrentImageInfo() {
if (swiperInstance) {
var slide = document.querySelector('.swiper-slide-active');
if (slide) {
return {
url: slide.getAttribute('data-original'),
2020-03-21 23:45:23 +01:00
shareUrl: slide.getAttribute('data-original'),
itemId: slide.getAttribute('data-itemid'),
serverId: slide.getAttribute('data-serverid')
};
}
return null;
} else {
return null;
2018-10-23 01:05:09 +03:00
}
}
function download() {
var imageInfo = getCurrentImageInfo();
require(['fileDownloader'], function (fileDownloader) {
fileDownloader.download([imageInfo]);
});
2018-10-23 01:05:09 +03:00
}
function share() {
var imageInfo = getCurrentImageInfo();
2018-10-23 01:05:09 +03:00
navigator.share({
url: imageInfo.shareUrl
});
2018-10-23 01:05:09 +03:00
}
function play() {
2020-02-23 10:52:03 +03:00
if (swiperInstance.autoplay) {
swiperInstance.autoplay.start();
}
2018-10-23 01:05:09 +03:00
}
function pause() {
2020-02-23 10:52:03 +03:00
if (swiperInstance.autoplay) {
swiperInstance.autoplay.stop();
}
2018-10-23 01:05:09 +03:00
}
function playPause() {
2020-03-10 12:57:09 +03:00
var paused = !dlg.querySelector('.btnSlideshowPause i').classList.contains("pause");
if (paused) {
play();
} else {
pause();
}
2018-10-23 01:05:09 +03:00
}
function onDialogClosed() {
var swiper = swiperInstance;
if (swiper) {
swiper.destroy(true, true);
swiperInstance = null;
}
2019-10-03 02:36:33 +09:00
inputManager.off(window, onInputCommand);
document.removeEventListener((window.PointerEvent ? 'pointermove' : 'mousemove'), onPointerMove);
2018-10-23 01:05:09 +03:00
}
function getOsdBottom() {
return dlg.querySelector('.slideshowBottomBar');
2018-10-23 01:05:09 +03:00
}
function showOsd() {
var bottom = getOsdBottom();
if (bottom) {
slideUpToShow(bottom);
startHideTimer();
}
2018-10-23 01:05:09 +03:00
}
function hideOsd() {
var bottom = getOsdBottom();
if (bottom) {
slideDownToHide(bottom);
}
2018-10-23 01:05:09 +03:00
}
var hideTimeout;
2018-10-23 01:05:09 +03:00
function startHideTimer() {
stopHideTimer();
hideTimeout = setTimeout(hideOsd, 4000);
2018-10-23 01:05:09 +03:00
}
function stopHideTimer() {
if (hideTimeout) {
clearTimeout(hideTimeout);
hideTimeout = null;
}
2018-10-23 01:05:09 +03:00
}
function slideUpToShow(elem) {
if (!elem.classList.contains('hide')) {
return;
}
_osdOpen = true;
elem.classList.remove('hide');
var onFinish = function () {
focusManager.focus(elem.querySelector('.btnSlideshowPause'));
};
if (!elem.animate) {
onFinish();
return;
2018-10-23 01:05:09 +03:00
}
requestAnimationFrame(function () {
var keyframes = [
{ transform: 'translate3d(0,' + elem.offsetHeight + 'px,0)', opacity: '.3', offset: 0 },
{ transform: 'translate3d(0,0,0)', opacity: '1', offset: 1 }
];
var timing = { duration: 300, iterations: 1, easing: 'ease-out' };
elem.animate(keyframes, timing).onfinish = onFinish;
});
2018-10-23 01:05:09 +03:00
}
function slideDownToHide(elem) {
if (elem.classList.contains('hide')) {
return;
}
var onFinish = function () {
elem.classList.add('hide');
_osdOpen = false;
};
if (!elem.animate) {
onFinish();
return;
2018-10-23 01:05:09 +03:00
}
requestAnimationFrame(function () {
var keyframes = [
{ transform: 'translate3d(0,0,0)', opacity: '1', offset: 0 },
{ transform: 'translate3d(0,' + elem.offsetHeight + 'px,0)', opacity: '.3', offset: 1 }
];
var timing = { duration: 300, iterations: 1, easing: 'ease-out' };
elem.animate(keyframes, timing).onfinish = onFinish;
});
2018-10-23 01:05:09 +03:00
}
var lastMouseMoveData;
2018-10-23 01:05:09 +03:00
function onPointerMove(e) {
var pointerType = e.pointerType || (layoutManager.mobile ? 'touch' : 'mouse');
if (pointerType === 'mouse') {
var eventX = e.screenX || 0;
var eventY = e.screenY || 0;
var obj = lastMouseMoveData;
if (!obj) {
lastMouseMoveData = {
x: eventX,
y: eventY
};
return;
}
// if coord are same, it didn't move
if (Math.abs(eventX - obj.x) < 10 && Math.abs(eventY - obj.y) < 10) {
return;
}
obj.x = eventX;
obj.y = eventY;
showOsd();
2018-10-23 01:05:09 +03:00
}
}
function onInputCommand(e) {
switch (e.detail.command) {
case 'up':
case 'down':
case 'select':
case 'menu':
case 'info':
case 'play':
case 'playpause':
case 'pause':
showOsd();
2018-10-23 01:05:09 +03:00
break;
default:
2018-10-23 01:05:09 +03:00
break;
}
}
function stopInterval() {
if (currentTimeout) {
clearTimeout(currentTimeout);
currentTimeout = null;
}
2018-10-23 01:05:09 +03:00
}
self.show = function () {
2020-03-21 23:45:23 +01:00
createElements(options);
};
self.hide = function () {
var dialog = dlg;
if (dialog) {
dialogHelper.close(dialog);
}
};
};
});