jellyfin-web/dashboard-ui/components/viewcontainer-lite.js

263 lines
8.0 KiB
JavaScript
Raw Normal View History

define(['jQuery'], function ($) {
2016-03-14 13:27:35 -07:00
var pageContainerCount;
var animationDuration = 500;
function loadView(options) {
return new Promise(function (resolve, reject) {
var animatedPages = document.querySelector('.mainAnimatedPages');
if (options.cancel) {
return;
}
2016-03-14 22:25:47 -07:00
var selected = getSelectedIndex(animatedPages);
var pageIndex = selected + 1;
2016-03-14 13:27:35 -07:00
if (pageIndex >= pageContainerCount) {
pageIndex = 0;
}
2016-03-15 22:33:31 -07:00
var newViewInfo = normalizeNewView(options);
var newView = newViewInfo.elem;
2016-03-14 13:27:35 -07:00
2016-03-15 11:03:25 -07:00
var dependencies = typeof (newView) == 'string' ? null : newView.getAttribute('data-require');
dependencies = dependencies ? dependencies.split(',') : [];
2016-03-14 13:27:35 -07:00
2016-03-15 21:13:57 -07:00
var isPluginpage = options.url.toLowerCase().indexOf('/configurationpage?') != -1;
if (isPluginpage) {
dependencies.push('jqmpopup');
dependencies.push('jqmcollapsible');
dependencies.push('jqmcheckbox');
dependencies.push('legacy/dashboard');
2016-03-21 20:15:54 -07:00
dependencies.push('legacy/selectmenu');
2016-03-27 21:26:46 -07:00
dependencies.push('jqmcontrolgroup');
2016-03-15 21:13:57 -07:00
}
if (isPluginpage || (newView.classList && newView.classList.contains('type-interior'))) {
dependencies.push('jqmlistview');
dependencies.push('scripts/notifications');
}
2016-03-15 11:03:25 -07:00
require(dependencies, function () {
var allPages = animatedPages.querySelectorAll('.mainAnimatedPage');
var animatable = allPages[pageIndex];
2016-03-14 13:27:35 -07:00
2016-03-15 11:03:25 -07:00
var currentPage = animatable.querySelector('.page-view');
if (currentPage) {
triggerDestroy(currentPage);
}
for (var i = 0, length = allPages.length; i < length; i++) {
if (pageIndex == i) {
allPages[i].classList.remove('hide');
} else {
allPages[i].classList.add('hide');
}
}
2016-03-14 13:27:35 -07:00
2016-03-15 11:03:25 -07:00
if (typeof (newView) == 'string') {
animatable.innerHTML = newView;
2016-03-14 22:25:47 -07:00
} else {
2016-03-15 11:03:25 -07:00
animatable.innerHTML = '';
2016-03-15 22:33:31 -07:00
if (newViewInfo.hasScript) {
// TODO: figure this out without jQuery
$(newView).appendTo(animatable);
} else {
animatable.appendChild(newView);
}
2016-03-15 21:13:57 -07:00
enhanceNewView(dependencies, newView);
2016-03-14 22:25:47 -07:00
}
2016-03-14 13:27:35 -07:00
2016-03-15 11:03:25 -07:00
var view = animatable.querySelector('.page-view');
2016-03-14 13:27:35 -07:00
2016-03-15 11:03:25 -07:00
if (onBeforeChange) {
onBeforeChange(view, false, options);
}
2016-03-14 13:27:35 -07:00
2016-03-15 11:03:25 -07:00
$.mobile = $.mobile || {};
$.mobile.activePage = view;
2016-03-14 13:27:35 -07:00
2016-03-15 11:03:25 -07:00
resolve(view);
});
});
2016-03-14 13:27:35 -07:00
}
2016-03-15 21:13:57 -07:00
function enhanceNewView(dependencies, newView) {
var hasJqm = false;
for (var i = 0, length = dependencies.length; i < length; i++) {
if (dependencies[i].indexOf('jqm') == 0) {
hasJqm = true;
break;
}
}
if (hasJqm) {
$(newView).trigger('create');
}
}
2016-03-17 23:52:47 -07:00
function replaceAll(str, find, replace) {
return str.split(find).join(replace);
}
2016-03-18 09:23:55 -07:00
function parseHtml(html, hasScript) {
2016-03-15 22:33:31 -07:00
2016-03-18 09:23:55 -07:00
if (hasScript) {
html = replaceAll(html, '<!--<script', '<script');
html = replaceAll(html, '</script>-->', '</script>');
}
2016-03-17 23:52:47 -07:00
2016-03-15 22:33:31 -07:00
var wrapper = document.createElement('div');
wrapper.innerHTML = html;
return wrapper.querySelector('div[data-role="page"]');
}
2016-03-15 11:03:25 -07:00
function normalizeNewView(options) {
2016-03-14 13:27:35 -07:00
2016-03-15 11:03:25 -07:00
if (options.view.indexOf('data-role="page"') == -1) {
var html = '<div class="page-view" data-type="' + (options.type || '') + '" data-url="' + options.url + '">';
html += options.view;
html += '</div>';
return html;
}
2016-03-14 13:27:35 -07:00
2016-03-18 09:23:55 -07:00
var hasScript = options.view.indexOf('<script') != -1;
var elem = parseHtml(options.view, hasScript);
2016-03-15 11:03:25 -07:00
elem.classList.add('page-view');
elem.setAttribute('data-type', options.type || '');
elem.setAttribute('data-url', options.url);
2016-03-15 22:33:31 -07:00
return {
elem: elem,
2016-03-18 09:23:55 -07:00
hasScript: hasScript
2016-03-15 22:33:31 -07:00
};
2016-03-15 11:03:25 -07:00
}
2016-03-14 13:27:35 -07:00
2016-03-15 11:03:25 -07:00
var onBeforeChange;
function setOnBeforeChange(fn) {
onBeforeChange = fn;
2016-03-14 13:27:35 -07:00
}
2016-03-14 22:25:47 -07:00
function getSelectedIndex(animatedPages) {
var allPages = animatedPages.querySelectorAll('.mainAnimatedPage');
for (var i = 0, length = allPages.length; i < length; i++) {
if (!allPages[i].classList.contains('hide')) {
return i;
}
}
return -1;
}
2016-03-14 13:27:35 -07:00
function replaceAnimatedPages() {
var elem = document.querySelector('neon-animated-pages.mainAnimatedPages');
if (elem) {
var div = document.createElement('div');
div.classList.add('mainAnimatedPages');
div.classList.add('skinBody');
2016-03-14 22:25:47 -07:00
div.innerHTML = '<div class="mainAnimatedPage hide"></div><div class="mainAnimatedPage hide"></div><div class="mainAnimatedPage hide"></div>';
2016-03-14 13:27:35 -07:00
elem.parentNode.replaceChild(div, elem);
}
2016-03-14 22:25:47 -07:00
pageContainerCount = document.querySelectorAll('.mainAnimatedPage').length;
2016-03-14 13:27:35 -07:00
}
function tryRestoreView(options) {
return new Promise(function (resolve, reject) {
var url = options.url;
var view = document.querySelector(".page-view[data-url='" + url + "']");
var page = parentWithClass(view, 'mainAnimatedPage');
if (view) {
var index = -1;
var pages = document.querySelectorAll('.mainAnimatedPage');
for (var i = 0, length = pages.length; i < length; i++) {
if (pages[i] == page) {
index = i;
break;
}
}
if (index != -1) {
var animatedPages = document.querySelector('.mainAnimatedPages');
if (options.cancel) {
return;
}
2016-03-14 22:25:47 -07:00
var allPages = animatedPages.querySelectorAll('.mainAnimatedPage');
var animatable = allPages[index];
2016-03-14 13:27:35 -07:00
var view = animatable.querySelector('.page-view');
if (onBeforeChange) {
onBeforeChange(view, true, options);
}
2016-03-14 22:25:47 -07:00
for (var i = 0, length = allPages.length; i < length; i++) {
if (index == i) {
allPages[i].classList.remove('hide');
} else {
allPages[i].classList.add('hide');
}
}
2016-03-15 11:03:25 -07:00
$.mobile = $.mobile || {};
$.mobile.activePage = view;
resolve(view);
2016-03-14 13:27:35 -07:00
return;
}
}
reject();
});
}
function triggerDestroy(view) {
view.dispatchEvent(new CustomEvent("viewdestroy", {}));
}
function reset() {
2016-03-14 22:25:47 -07:00
var views = document.querySelectorAll(".mainAnimatedPage.hide .page-view");
2016-03-14 13:27:35 -07:00
for (var i = 0, length = views.length; i < length; i++) {
var view = views[i];
triggerDestroy(view);
view.parentNode.removeChild(view);
}
}
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
replaceAnimatedPages();
return {
loadView: loadView,
tryRestoreView: tryRestoreView,
reset: reset,
setOnBeforeChange: setOnBeforeChange
};
});