jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/viewmanager/viewcontainer-lite.js

275 lines
7.7 KiB
JavaScript
Raw Normal View History

define(['browser', 'dom', 'css!./viewcontainer-lite'], function (browser, dom) {
2016-10-03 22:15:39 -07:00
'use strict';
2016-05-13 21:48:59 -07:00
2016-05-24 09:58:12 -07:00
var mainAnimatedPages = document.querySelector('.mainAnimatedPages');
var allPages = [];
2016-05-15 22:38:17 -07:00
var currentUrls = [];
2016-05-24 09:58:12 -07:00
var pageContainerCount = 3;
2016-05-15 18:22:22 -07:00
var selectedPageIndex = -1;
2016-05-13 21:48:59 -07:00
function enableAnimation() {
2016-08-20 14:58:28 -07:00
if (browser.animate) {
return true;
}
2016-05-13 21:48:59 -07:00
if (browser.tv) {
return false;
}
2016-05-31 20:02:29 -07:00
if (browser.operaTv) {
return false;
}
2016-08-19 23:27:48 -07:00
return browser.edge && !browser.mobile;
2016-05-13 21:48:59 -07:00
}
function loadView(options) {
if (options.cancel) {
return;
}
cancelActiveAnimations();
2016-05-24 09:58:12 -07:00
var selected = selectedPageIndex;
2016-10-03 22:15:39 -07:00
var previousAnimatable = selected === -1 ? null : allPages[selected];
2016-05-13 21:48:59 -07:00
var pageIndex = selected + 1;
if (pageIndex >= pageContainerCount) {
pageIndex = 0;
}
2016-05-15 18:22:22 -07:00
var view = document.createElement('div');
2016-05-24 09:58:12 -07:00
2016-05-15 18:22:22 -07:00
if (options.type) {
view.setAttribute('data-type', options.type);
}
view.innerHTML = options.view;
2016-05-13 21:48:59 -07:00
2016-05-24 09:58:12 -07:00
var currentPage = allPages[pageIndex];
var animatable = view;
2016-05-13 21:48:59 -07:00
2016-05-24 09:58:12 -07:00
view.classList.add('mainAnimatedPage');
2016-05-13 21:48:59 -07:00
if (currentPage) {
triggerDestroy(currentPage);
2016-05-24 09:58:12 -07:00
mainAnimatedPages.replaceChild(view, currentPage);
2016-05-15 18:22:22 -07:00
} else {
2016-05-24 09:58:12 -07:00
mainAnimatedPages.appendChild(view);
2016-05-13 21:48:59 -07:00
}
2016-05-24 09:58:12 -07:00
allPages[pageIndex] = view;
2016-05-13 21:48:59 -07:00
if (onBeforeChange) {
onBeforeChange(view, false, options);
}
beforeAnimate(allPages, pageIndex, selected);
// animate here
return animate(animatable, previousAnimatable, options.transition, options.isBack).then(function () {
2016-05-15 18:22:22 -07:00
selectedPageIndex = pageIndex;
2016-05-15 22:38:17 -07:00
currentUrls[pageIndex] = options.url;
2016-05-13 21:48:59 -07:00
if (!options.cancel && previousAnimatable) {
afterAnimate(allPages, pageIndex);
}
return view;
});
}
function beforeAnimate(allPages, newPageIndex, oldPageIndex) {
for (var i = 0, length = allPages.length; i < length; i++) {
2016-10-03 22:15:39 -07:00
if (newPageIndex === i || oldPageIndex === i) {
2016-05-13 21:48:59 -07:00
//allPages[i].classList.remove('hide');
} else {
allPages[i].classList.add('hide');
}
}
}
function afterAnimate(allPages, newPageIndex) {
for (var i = 0, length = allPages.length; i < length; i++) {
2016-10-03 22:15:39 -07:00
if (newPageIndex === i) {
2016-05-13 21:48:59 -07:00
//allPages[i].classList.remove('hide');
} else {
allPages[i].classList.add('hide');
}
}
}
function animate(newAnimatedPage, oldAnimatedPage, transition, isBack) {
2016-05-19 22:36:57 -07:00
if (enableAnimation() && oldAnimatedPage && newAnimatedPage.animate) {
2016-10-03 22:15:39 -07:00
if (transition === 'slide') {
2016-05-13 21:48:59 -07:00
return slide(newAnimatedPage, oldAnimatedPage, transition, isBack);
2016-10-03 22:15:39 -07:00
} else if (transition === 'fade') {
2016-05-13 21:48:59 -07:00
return fade(newAnimatedPage, oldAnimatedPage, transition, isBack);
}
}
return Promise.resolve();
}
function slide(newAnimatedPage, oldAnimatedPage, transition, isBack) {
2016-05-19 07:37:59 -07:00
return new Promise(function (resolve, reject) {
2016-10-15 15:12:16 -07:00
var duration = 450;
2016-05-13 21:48:59 -07:00
2016-05-19 07:37:59 -07:00
var animations = [];
2016-05-13 21:48:59 -07:00
2016-05-19 07:37:59 -07:00
if (oldAnimatedPage) {
2016-10-15 15:12:16 -07:00
if (isBack) {
oldAnimatedPage.style.animation = 'view-slideright-r ' + duration + 'ms ease-out normal both';
setAnimation(oldAnimatedPage, 'view-slideright-r ' + duration + 'ms ease-out normal both');
} else {
setAnimation(oldAnimatedPage, 'view-slideleft-r ' + duration + 'ms ease-out normal both');
}
animations.push(oldAnimatedPage);
2016-05-19 07:37:59 -07:00
}
2016-05-13 21:48:59 -07:00
2016-10-15 15:12:16 -07:00
if (isBack) {
setAnimation(newAnimatedPage, 'view-slideright ' + duration + 'ms ease-out normal both');
} else {
setAnimation(newAnimatedPage, 'view-slideleft ' + duration + 'ms ease-out normal both');
}
animations.push(newAnimatedPage);
2016-05-13 21:48:59 -07:00
2016-05-19 07:37:59 -07:00
currentAnimations = animations;
2016-05-13 21:48:59 -07:00
var onAnimationComplete = function () {
2016-11-26 22:00:20 -07:00
dom.removeEventListener(newAnimatedPage, dom.whichAnimationEvent(), onAnimationComplete, {
once: true
});
resolve();
};
2016-11-26 22:00:20 -07:00
dom.addEventListener(newAnimatedPage, dom.whichAnimationEvent(), onAnimationComplete, {
once: true
});
2016-05-13 21:48:59 -07:00
});
}
function fade(newAnimatedPage, oldAnimatedPage, transition, isBack) {
2016-05-19 07:37:59 -07:00
return new Promise(function (resolve, reject) {
2016-05-13 21:48:59 -07:00
2016-10-15 15:12:16 -07:00
var duration = 400;
2016-05-19 07:37:59 -07:00
var animations = [];
2016-05-13 21:48:59 -07:00
2016-05-19 07:37:59 -07:00
if (oldAnimatedPage) {
2016-10-15 15:12:16 -07:00
setAnimation(oldAnimatedPage, 'view-fadeout ' + duration + 'ms ease-out normal both');
animations.push(oldAnimatedPage);
2016-05-19 07:37:59 -07:00
}
2016-05-13 21:48:59 -07:00
2016-10-15 15:12:16 -07:00
setAnimation(newAnimatedPage, 'view-fadein ' + duration + 'ms ease-in normal both');
animations.push(newAnimatedPage);
2016-05-13 21:48:59 -07:00
2016-10-15 15:12:16 -07:00
currentAnimations = animations;
2016-05-13 21:48:59 -07:00
var onAnimationComplete = function () {
2016-11-26 22:00:20 -07:00
dom.removeEventListener(newAnimatedPage, dom.whichAnimationEvent(), onAnimationComplete, {
once: true
});
resolve();
};
2016-11-26 22:00:20 -07:00
dom.addEventListener(newAnimatedPage, dom.whichAnimationEvent(), onAnimationComplete, {
once: true
});
2016-10-15 15:12:16 -07:00
});
}
2016-05-13 21:48:59 -07:00
2016-10-15 15:12:16 -07:00
function setAnimation(elem, value) {
2016-05-13 21:48:59 -07:00
2016-10-15 15:12:16 -07:00
requestAnimationFrame(function () {
elem.style.animation = value;
2016-05-13 21:48:59 -07:00
});
}
var currentAnimations = [];
function cancelActiveAnimations() {
var animations = currentAnimations;
for (var i = 0, length = animations.length; i < length; i++) {
2016-10-15 15:12:16 -07:00
animations[i].animation = 'none';
2016-05-13 21:48:59 -07:00
}
}
var onBeforeChange;
function setOnBeforeChange(fn) {
onBeforeChange = fn;
}
function tryRestoreView(options) {
2016-05-15 22:38:17 -07:00
var url = options.url;
var index = currentUrls.indexOf(url);
2016-05-13 21:48:59 -07:00
2016-10-03 22:15:39 -07:00
if (index !== -1) {
2016-05-24 09:58:12 -07:00
var animatable = allPages[index];
var view = animatable;
2016-05-13 21:48:59 -07:00
2016-05-15 22:38:17 -07:00
if (view) {
2016-05-13 21:48:59 -07:00
if (options.cancel) {
return;
}
cancelActiveAnimations();
2016-05-24 09:58:12 -07:00
var selected = selectedPageIndex;
2016-10-03 22:15:39 -07:00
var previousAnimatable = selected === -1 ? null : allPages[selected];
2016-05-13 21:48:59 -07:00
if (onBeforeChange) {
onBeforeChange(view, true, options);
}
beforeAnimate(allPages, index, selected);
2016-05-24 09:58:12 -07:00
animatable.classList.remove('hide');
2016-05-13 21:48:59 -07:00
return animate(animatable, previousAnimatable, options.transition, options.isBack).then(function () {
2016-05-15 18:22:22 -07:00
selectedPageIndex = index;
2016-05-13 21:48:59 -07:00
if (!options.cancel && previousAnimatable) {
afterAnimate(allPages, index);
}
return view;
});
}
}
return Promise.reject();
}
function triggerDestroy(view) {
2016-08-07 12:43:52 -07:00
view.dispatchEvent(new CustomEvent('viewdestroy', {
cancelable: false
}));
2016-05-13 21:48:59 -07:00
}
function reset() {
2016-05-24 09:58:12 -07:00
allPages = [];
2016-05-15 22:38:17 -07:00
currentUrls = [];
2016-05-24 09:58:12 -07:00
mainAnimatedPages.innerHTML = '';
selectedPageIndex = -1;
2016-05-13 21:48:59 -07:00
}
2016-08-20 14:58:28 -07:00
if (enableAnimation()) {
2016-05-16 13:48:56 -07:00
require(['webAnimations']);
2016-05-13 21:48:59 -07:00
}
return {
loadView: loadView,
tryRestoreView: tryRestoreView,
reset: reset,
2016-05-16 13:48:56 -07:00
setOnBeforeChange: setOnBeforeChange
2016-05-13 21:48:59 -07:00
};
});