jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/slideshow/slideshow.js

619 lines
19 KiB
JavaScript
Raw Normal View History

2016-06-15 23:20:12 -07:00
define(['dialogHelper', 'inputManager', 'connectionManager', 'layoutManager', 'focusManager', 'apphost', 'loading', 'css!./style', 'material-icons', 'paper-icon-button-light'], function (dialogHelper, inputmanager, connectionManager, layoutManager, focusManager, appHost, loading) {
2016-04-17 22:58:08 -07: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;
}
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;
}
function getImgUrl(item, original) {
var apiClient = connectionManager.getApiClient(item.ServerId);
var imageOptions = {};
if (!original) {
imageOptions.maxWidth = screen.availWidth;
}
if (item.BackdropImageTags && item.BackdropImageTags.length) {
return getBackdropImageUrl(item, imageOptions, apiClient);
} else {
if (item.MediaType == 'Photo' && original) {
return apiClient.getUrl("Items/" + item.Id + "/Download", {
api_key: apiClient.accessToken()
});
}
imageOptions.type = "Primary";
return getImageUrl(item, imageOptions, apiClient);
}
}
2016-01-30 23:03:40 -07:00
2016-05-06 10:49:58 -07:00
function getIcon(icon, cssClass, canFocus, autoFocus) {
var tabIndex = canFocus ? '' : ' tabindex="-1"';
autoFocus = autoFocus ? ' autofocus' : '';
2016-08-04 16:48:54 -07:00
return '<button is="paper-icon-button-light" class="autoSize ' + cssClass + '"' + tabIndex + autoFocus + '><i class="md-icon slideshowButtonIcon">' + icon + '</i></button>';
2016-05-06 10:49:58 -07:00
}
2016-01-30 23:03:40 -07:00
return function (options) {
var self = this;
var swiperInstance;
var dlg;
2016-04-17 22:58:08 -07:00
var currentTimeout;
var currentIntervalMs;
var currentOptions;
var currentIndex;
2016-01-30 23:03:40 -07:00
function createElements(options) {
2016-03-23 12:03:17 -07:00
dlg = dialogHelper.createDialog({
2016-02-01 10:02:17 -07:00
exitAnimationDuration: options.interactive ? 400 : 800,
2016-04-17 22:58:08 -07:00
size: 'fullscreen',
2016-05-12 23:22:02 -07:00
autoFocus: false,
2016-05-28 23:43:00 -07:00
scrollY: false,
2016-09-08 13:32:30 -07:00
exitAnimation: 'fadeout',
removeOnClose: true
2016-01-30 23:03:40 -07:00
});
dlg.classList.add('slideshowDialog');
var html = '';
if (options.interactive) {
2016-04-27 18:43:09 -07:00
var actionButtonsOnTop = layoutManager.mobile;
2016-01-30 23:03:40 -07:00
html += '<div>';
html += '<div class="slideshowSwiperContainer"><div class="swiper-wrapper"></div></div>';
2016-06-09 23:54:03 -07:00
html += getIcon('keyboard_arrow_left', 'btnSlideshowPrevious slideshowButton', false);
html += getIcon('keyboard_arrow_right', 'btnSlideshowNext slideshowButton', false);
2016-04-17 22:58:08 -07:00
2016-04-27 18:43:09 -07:00
html += '<div class="topActionButtons">';
if (actionButtonsOnTop) {
if (appHost.supports('filedownload')) {
2016-06-09 23:54:03 -07:00
html += getIcon('file_download', 'btnDownload slideshowButton', true);
2016-04-27 18:43:09 -07:00
}
2016-04-27 21:53:11 -07:00
if (appHost.supports('sharing')) {
2016-06-09 23:54:03 -07:00
html += getIcon('share', 'btnShare slideshowButton', true);
2016-04-27 21:53:11 -07:00
}
2016-04-27 18:43:09 -07:00
}
2016-06-09 23:54:03 -07:00
html += getIcon('close', 'slideshowButton btnSlideshowExit', false);
2016-04-27 18:43:09 -07:00
html += '</div>';
2016-01-30 23:03:40 -07:00
2016-04-27 18:43:09 -07:00
if (!actionButtonsOnTop) {
html += '<div class="slideshowBottomBar hide">';
2016-04-17 22:58:08 -07:00
2016-06-09 23:54:03 -07:00
html += getIcon('pause', 'btnSlideshowPause slideshowButton', true, true);
2016-04-27 18:43:09 -07:00
if (appHost.supports('filedownload')) {
2016-06-09 23:54:03 -07:00
html += getIcon('file_download', 'btnDownload slideshowButton', true);
2016-04-27 18:43:09 -07:00
}
2016-04-27 21:53:11 -07:00
if (appHost.supports('sharing')) {
2016-06-09 23:54:03 -07:00
html += getIcon('share', 'btnShare slideshowButton', true);
2016-04-27 21:53:11 -07:00
}
2016-04-17 22:58:08 -07:00
2016-04-27 18:43:09 -07:00
html += '</div>';
}
2016-04-17 22:58:08 -07:00
2016-01-30 23:03:40 -07:00
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) {
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2016-01-30 23:03:40 -07:00
});
dlg.querySelector('.btnSlideshowNext').addEventListener('click', nextImage);
dlg.querySelector('.btnSlideshowPrevious').addEventListener('click', previousImage);
2016-04-17 22:58:08 -07:00
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);
}
2016-01-30 23:03:40 -07:00
}
2016-03-23 12:03:17 -07:00
dialogHelper.open(dlg).then(function () {
2016-01-30 23:03:40 -07:00
stopInterval();
});
inputmanager.on(window, onInputCommand);
2016-04-17 22:58:08 -07:00
document.addEventListener('mousemove', onMouseMove);
2016-01-30 23:03:40 -07:00
2016-03-22 10:46:57 -07:00
dlg.addEventListener('close', onDialogClosed);
2016-01-30 23:03:40 -07:00
if (options.interactive) {
loadSwiper(dlg);
}
}
function loadSwiper(dlg) {
if (currentOptions.slides) {
dlg.querySelector('.swiper-wrapper').innerHTML = currentOptions.slides.map(getSwiperSlideHtmlFromSlide).join('');
} else {
dlg.querySelector('.swiper-wrapper').innerHTML = currentOptions.items.map(getSwiperSlideHtmlFromItem).join('');
}
require(['swiper'], function (swiper) {
swiperInstance = new Swiper(dlg.querySelector('.slideshowSwiperContainer'), {
// Optional parameters
direction: 'horizontal',
2016-02-01 10:02:17 -07:00
loop: options.loop !== false,
2016-01-30 23:03:40 -07:00
autoplay: options.interval || 8000,
// Disable preloading of all images
preloadImages: false,
// Enable lazy loading
lazyLoading: true,
2016-05-10 11:13:26 -07:00
lazyLoadingInPrevNext: true,
2016-01-30 23:03:40 -07:00
autoplayDisableOnInteraction: false,
2016-05-28 23:43:00 -07:00
initialSlide: options.startIndex || 0,
speed: 240
2016-01-30 23:03:40 -07:00
});
2016-02-01 10:52:23 -07:00
swiperInstance.on('onLazyImageLoad', onSlideChangeStart);
swiperInstance.on('onLazyImageReady', onSlideChangeEnd);
2016-04-10 08:17:35 -07:00
if (layoutManager.mobile) {
2016-01-30 23:03:40 -07:00
pause();
} else {
play();
}
});
}
function getSwiperSlideHtmlFromItem(item) {
return getSwiperSlideHtmlFromSlide({
2016-04-17 22:58:08 -07:00
imageUrl: getImgUrl(item),
2016-04-22 09:09:08 -07:00
originalImage: getImgUrl(item, true),
2016-01-30 23:03:40 -07:00
//title: item.Name,
//description: item.Overview
2016-04-27 21:53:11 -07:00
Id: item.Id,
ServerId: item.ServerId
2016-01-30 23:03:40 -07:00
});
}
2016-02-01 10:52:23 -07:00
function onSlideChangeStart(swiper, slide, image) {
2016-06-15 23:20:12 -07:00
//loading.show();
2016-02-01 10:52:23 -07:00
}
function onSlideChangeEnd(swiper, slide, image) {
2016-06-15 23:20:12 -07:00
//loading.hide();
2016-02-01 10:52:23 -07:00
}
2016-01-30 23:03:40 -07:00
function getSwiperSlideHtmlFromSlide(item) {
var html = '';
2016-04-27 21:53:11 -07:00
html += '<div class="swiper-slide" data-original="' + item.originalImage + '" data-itemid="' + item.Id + '" data-serverid="' + item.ServerId + '">';
2016-08-04 16:48:54 -07:00
html += '<img data-src="' + item.imageUrl + '" class="swiper-lazy swiper-slide-img">';
2016-01-30 23:03:40 -07:00
if (item.title || item.subtitle) {
html += '<div class="slideText">';
html += '<div class="slideTextInner">';
if (item.title) {
html += '<div class="slideTitle">';
html += item.title;
html += '</div>';
}
if (item.description) {
html += '<div class="slideSubtitle">';
html += item.description;
html += '</div>';
}
html += '</div>';
html += '</div>';
}
html += '</div>';
return html;
}
function previousImage() {
if (swiperInstance) {
swiperInstance.slidePrev();
} else {
stopInterval();
showNextImage(currentIndex - 1);
}
}
function nextImage() {
if (swiperInstance) {
2016-02-01 10:02:17 -07:00
if (options.loop === false) {
if (swiperInstance.activeIndex >= swiperInstance.slides.length - 1) {
2016-03-23 12:03:17 -07:00
dialogHelper.close(dlg);
2016-02-01 10:02:17 -07:00
return;
}
}
2016-01-30 23:03:40 -07:00
swiperInstance.slideNext();
} else {
stopInterval();
showNextImage(currentIndex + 1);
}
}
2016-04-27 21:53:11 -07:00
function getCurrentImageInfo() {
2016-04-22 09:09:08 -07:00
if (swiperInstance) {
2016-04-27 21:53:11 -07:00
var slide = document.querySelector('.swiper-slide-active');
if (slide) {
return {
url: slide.getAttribute('data-original'),
itemId: slide.getAttribute('data-itemid'),
serverId: slide.getAttribute('data-serverid')
};
}
2016-04-22 09:09:08 -07:00
return null;
2016-04-17 22:58:08 -07:00
} else {
return null;
}
}
function download() {
2016-04-27 21:53:11 -07:00
var imageInfo = getCurrentImageInfo();
2016-04-27 18:43:09 -07:00
2016-04-17 22:58:08 -07:00
require(['fileDownloader'], function (fileDownloader) {
2016-04-27 21:53:11 -07:00
fileDownloader.download([imageInfo]);
2016-04-17 22:58:08 -07:00
});
}
function share() {
2016-04-27 21:53:11 -07:00
var imageInfo = getCurrentImageInfo();
require(['sharingmanager'], function (sharingManager) {
sharingManager.showMenu(imageInfo);
});
2016-04-17 22:58:08 -07:00
}
2016-01-30 23:03:40 -07:00
function play() {
2016-06-09 23:54:03 -07:00
var btnSlideshowPause = dlg.querySelector('.btnSlideshowPause i');
2016-04-27 18:43:09 -07:00
if (btnSlideshowPause) {
2016-06-09 23:54:03 -07:00
btnSlideshowPause.innerHTML = "pause";
2016-04-27 18:43:09 -07:00
}
2016-01-30 23:03:40 -07:00
swiperInstance.startAutoplay();
}
function pause() {
2016-06-09 23:54:03 -07:00
var btnSlideshowPause = dlg.querySelector('.btnSlideshowPause i');
2016-04-27 18:43:09 -07:00
if (btnSlideshowPause) {
2016-06-09 23:54:03 -07:00
btnSlideshowPause.innerHTML = "play_arrow";
2016-04-27 18:43:09 -07:00
}
2016-01-30 23:03:40 -07:00
swiperInstance.stopAutoplay();
}
function playPause() {
2016-06-09 23:54:03 -07:00
var paused = dlg.querySelector('.btnSlideshowPause i').innerHTML != "pause";
2016-01-30 23:03:40 -07:00
if (paused) {
play();
} else {
pause();
}
}
function onDialogClosed() {
var swiper = swiperInstance;
if (swiper) {
2016-02-01 10:52:23 -07:00
swiper.off('onLazyImageLoad');
swiper.off('onLazyImageReady');
2016-01-30 23:03:40 -07:00
swiper.destroy(true, true);
swiperInstance = null;
}
inputmanager.off(window, onInputCommand);
2016-04-17 22:58:08 -07:00
document.removeEventListener('mousemove', onMouseMove);
2016-01-30 23:03:40 -07:00
}
function startInterval(options) {
currentOptions = options;
stopInterval();
createElements(options);
if (!options.interactive) {
currentIntervalMs = options.interval || 8000;
showNextImage(options.startIndex || 0, true);
}
}
2016-04-17 22:58:08 -07:00
var _osdOpen = false;
function isOsdOpen() {
return _osdOpen;
}
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
function getOsdBottom() {
return dlg.querySelector('.slideshowBottomBar');
2016-01-30 23:03:40 -07:00
}
2016-04-17 22:58:08 -07:00
function showOsd() {
2016-01-30 23:03:40 -07:00
2016-04-27 18:43:09 -07:00
var bottom = getOsdBottom();
if (bottom) {
slideUpToShow(bottom);
startHideTimer();
}
2016-04-17 22:58:08 -07:00
}
function hideOsd() {
2016-01-30 23:03:40 -07:00
2016-04-27 18:43:09 -07:00
var bottom = getOsdBottom();
if (bottom) {
slideDownToHide(bottom);
}
2016-04-17 22:58:08 -07:00
}
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
var hideTimeout;
function startHideTimer() {
stopHideTimer();
hideTimeout = setTimeout(hideOsd, 4000);
}
function stopHideTimer() {
if (hideTimeout) {
clearTimeout(hideTimeout);
hideTimeout = null;
2016-01-30 23:03:40 -07:00
}
2016-04-17 22:58:08 -07:00
}
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
function slideUpToShow(elem) {
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
if (!elem.classList.contains('hide')) {
return;
2016-01-30 23:03:40 -07:00
}
2016-04-17 22:58:08 -07:00
_osdOpen = true;
elem.classList.remove('hide');
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07: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 = function () {
focusManager.focus(elem.querySelector('.btnSlideshowPause'));
};
});
}
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
function slideDownToHide(elem) {
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
if (elem.classList.contains('hide')) {
return;
2016-01-30 23:03:40 -07:00
}
2016-04-17 22:58:08 -07:00
requestAnimationFrame(function () {
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
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 = function () {
elem.classList.add('hide');
_osdOpen = false;
};
});
}
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
var lastMouseMoveData;
function onMouseMove(e) {
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
var eventX = e.screenX || 0;
var eventY = e.screenY || 0;
var obj = lastMouseMoveData;
if (!obj) {
lastMouseMoveData = {
x: eventX,
y: eventY
};
return;
}
2016-01-30 23:03:40 -07:00
2016-04-17 22:58:08 -07:00
// if coord are same, it didn't move
if (Math.abs(eventX - obj.x) < 10 && Math.abs(eventY - obj.y) < 10) {
return;
2016-01-30 23:03:40 -07:00
}
2016-04-17 22:58:08 -07:00
obj.x = eventX;
obj.y = eventY;
showOsd();
}
function onInputCommand(e) {
switch (e.detail.command) {
case 'left':
if (!isOsdOpen()) {
e.preventDefault();
2016-09-29 05:55:49 -07:00
e.stopPropagation();
2016-04-17 22:58:08 -07:00
previousImage();
}
break;
case 'right':
if (!isOsdOpen()) {
e.preventDefault();
2016-09-29 05:55:49 -07:00
e.stopPropagation();
2016-04-17 22:58:08 -07:00
nextImage();
}
break;
case 'up':
case 'down':
case 'select':
case 'menu':
case 'info':
case 'play':
case 'playpause':
case 'pause':
case 'fastforward':
case 'rewind':
case 'next':
case 'previous':
showOsd();
break;
default:
break;
}
2016-01-30 23:03:40 -07:00
}
function showNextImage(index, skipPreload) {
index = Math.max(0, index);
if (index >= currentOptions.items.length) {
index = 0;
}
currentIndex = index;
var options = currentOptions;
var items = options.items;
var item = items[index];
var imgUrl = getImgUrl(item);
var onSrcLoaded = function () {
var cardImageContainer = dlg.querySelector('.slideshowImage');
var newCardImageContainer = document.createElement('div');
newCardImageContainer.className = cardImageContainer.className;
if (options.cover) {
2016-08-04 16:48:54 -07:00
newCardImageContainer.classList.add('slideshowImage-cover');
2016-01-30 23:03:40 -07:00
}
newCardImageContainer.style.backgroundImage = "url('" + imgUrl + "')";
newCardImageContainer.classList.add('hide');
cardImageContainer.parentNode.appendChild(newCardImageContainer);
if (options.showTitle) {
dlg.querySelector('.slideshowImageText').innerHTML = item.Name;
} else {
dlg.querySelector('.slideshowImageText').innerHTML = '';
}
newCardImageContainer.classList.remove('hide');
var onAnimationFinished = function () {
var parentNode = cardImageContainer.parentNode;
if (parentNode) {
parentNode.removeChild(cardImageContainer);
}
};
if (newCardImageContainer.animate) {
var keyframes = [
{ opacity: '0', offset: 0 },
{ opacity: '1', offset: 1 }];
var timing = { duration: 1200, iterations: 1 };
newCardImageContainer.animate(keyframes, timing).onfinish = onAnimationFinished;
} else {
onAnimationFinished();
}
stopInterval();
currentTimeout = setTimeout(function () {
showNextImage(index + 1, true);
}, currentIntervalMs);
};
if (!skipPreload) {
var img = new Image();
img.onload = onSrcLoaded;
img.src = imgUrl;
} else {
onSrcLoaded();
}
}
function stopInterval() {
if (currentTimeout) {
clearTimeout(currentTimeout);
currentTimeout = null;
}
}
self.show = function () {
startInterval(options);
};
self.hide = function () {
var dialog = dlg;
if (dialog) {
2016-03-23 12:03:17 -07:00
dialogHelper.close(dialog);
2016-01-30 23:03:40 -07:00
}
};
}
});