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

305 lines
7.8 KiB
JavaScript
Raw Normal View History

2016-05-13 21:48:59 -07:00
define(['browser'], function (browser) {
var allPages = document.querySelectorAll('.mainAnimatedPage');
2016-05-15 22:38:17 -07:00
var currentUrls = [];
2016-05-13 21:48:59 -07:00
var pageContainerCount = allPages.length;
var animationDuration = 500;
var allowAnimation = true;
2016-05-15 18:22:22 -07:00
var selectedPageIndex = -1;
2016-05-13 21:48:59 -07:00
function enableAnimation() {
if (!allowAnimation) {
return false;
}
if (browser.tv) {
return false;
}
return true;
}
function loadView(options) {
if (options.cancel) {
return;
}
cancelActiveAnimations();
var selected = getSelectedIndex(allPages);
var previousAnimatable = selected == -1 ? null : allPages[selected];
var pageIndex = selected + 1;
if (pageIndex >= pageContainerCount) {
pageIndex = 0;
}
2016-05-15 18:22:22 -07:00
var view = document.createElement('div');
view.classList.add('page-view');
if (options.type) {
view.setAttribute('data-type', options.type);
}
view.innerHTML = options.view;
2016-05-13 21:48:59 -07:00
var animatable = allPages[pageIndex];
var currentPage = animatable.querySelector('.page-view');
if (currentPage) {
triggerDestroy(currentPage);
2016-05-15 18:22:22 -07:00
animatable.replaceChild(view, currentPage);
} else {
animatable.appendChild(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++) {
if (newPageIndex == i || oldPageIndex == i) {
//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++) {
if (newPageIndex == i) {
//allPages[i].classList.remove('hide');
} else {
allPages[i].classList.add('hide');
}
}
}
function animate(newAnimatedPage, oldAnimatedPage, transition, isBack) {
if (enableAnimation() && newAnimatedPage.animate) {
if (transition == 'slide') {
return slide(newAnimatedPage, oldAnimatedPage, transition, isBack);
} else if (transition == 'fade') {
return fade(newAnimatedPage, oldAnimatedPage, transition, isBack);
}
}
return nullAnimation(newAnimatedPage, oldAnimatedPage, transition, isBack);
}
function nullAnimation(newAnimatedPage, oldAnimatedPage, transition, isBack) {
newAnimatedPage.classList.remove('hide');
return Promise.resolve();
}
function slide(newAnimatedPage, oldAnimatedPage, transition, isBack) {
var timings = {
duration: 450,
iterations: 1,
easing: 'ease-out',
fill: 'both'
}
var animations = [];
if (oldAnimatedPage) {
var destination = isBack ? '100%' : '-100%';
animations.push(oldAnimatedPage.animate([
{ transform: 'none', offset: 0 },
2016-05-15 18:22:22 -07:00
{ transform: 'translate3d(' + destination + ', 0, 0)', offset: 1 }
2016-05-13 21:48:59 -07:00
], timings));
}
newAnimatedPage.classList.remove('hide');
var start = isBack ? '-100%' : '100%';
animations.push(newAnimatedPage.animate([
2016-05-15 18:22:22 -07:00
{ transform: 'translate3d(' + start + ', 0, 0)', offset: 0 },
2016-05-13 21:48:59 -07:00
{ transform: 'none', offset: 1 }
], timings));
currentAnimations = animations;
return new Promise(function (resolve, reject) {
animations[animations.length - 1].onfinish = resolve;
});
}
function fade(newAnimatedPage, oldAnimatedPage, transition, isBack) {
var timings = {
duration: animationDuration,
iterations: 1,
easing: 'ease-out',
fill: 'both'
}
var animations = [];
if (oldAnimatedPage) {
animations.push(oldAnimatedPage.animate([
{ opacity: 1, offset: 0 },
{ opacity: 0, offset: 1 }
], timings));
}
newAnimatedPage.classList.remove('hide');
animations.push(newAnimatedPage.animate([
{ opacity: 0, offset: 0 },
{ opacity: 1, offset: 1 }
], timings));
currentAnimations = animations;
return new Promise(function (resolve, reject) {
animations[animations.length - 1].onfinish = resolve;
});
}
var currentAnimations = [];
function cancelActiveAnimations() {
var animations = currentAnimations;
for (var i = 0, length = animations.length; i < length; i++) {
cancelAnimation(animations[i]);
}
}
function cancelAnimation(animation) {
try {
animation.cancel();
} catch (err) {
console.log('Error canceling animation: ' + err);
}
}
var onBeforeChange;
function setOnBeforeChange(fn) {
onBeforeChange = fn;
}
function sendResolve(resolve, view) {
// Don't report completion until the animation has finished, otherwise rendering may not perform well
setTimeout(function () {
resolve(view);
}, animationDuration);
}
function getSelectedIndex(allPages) {
2016-05-15 18:22:22 -07:00
return selectedPageIndex;
2016-05-13 21:48:59 -07:00
}
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-05-15 22:38:17 -07:00
if (index != -1) {
var page = allPages[index];
var view = page.querySelector(".page-view");
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();
var animatable = allPages[index];
var selected = getSelectedIndex(allPages);
var previousAnimatable = selected == -1 ? null : allPages[selected];
if (onBeforeChange) {
onBeforeChange(view, true, options);
}
beforeAnimate(allPages, index, selected);
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) {
view.dispatchEvent(new CustomEvent("viewdestroy", {}));
}
function reset() {
2016-05-15 22:38:17 -07:00
currentUrls = [];
2016-05-13 21:48:59 -07:00
}
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
function init(isAnimationAllowed) {
if (allowAnimation && enableAnimation() && !browser.animate) {
require(['webAnimations']);
}
}
return {
loadView: loadView,
tryRestoreView: tryRestoreView,
reset: reset,
setOnBeforeChange: setOnBeforeChange,
init: init
};
});