1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/viewContainer.js

265 lines
8.4 KiB
JavaScript
Raw Normal View History

2019-04-02 22:10:22 +01:00
define(["browser", "dom", "layoutManager", "css!components/viewManager/viewContainer"], function (browser, dom, layoutManager) {
2018-10-23 01:05:09 +03:00
"use strict";
function setControllerClass(view, options) {
2019-04-02 22:10:22 +01:00
if (options.controllerFactory) {
return Promise.resolve();
}
2018-10-23 01:05:09 +03:00
var controllerUrl = view.getAttribute("data-controller");
2019-04-02 22:10:22 +01:00
if (controllerUrl) {
if (0 === controllerUrl.indexOf("__plugin/")) {
controllerUrl = controllerUrl.substring("__plugin/".length);
}
controllerUrl = Dashboard.getConfigurationResourceUrl(controllerUrl);
return getRequirePromise([controllerUrl]).then(function (ControllerFactory) {
options.controllerFactory = ControllerFactory;
});
}
return Promise.resolve();
2018-10-23 01:05:09 +03:00
}
function getRequirePromise(deps) {
2019-04-02 22:10:22 +01:00
return new Promise(function (resolve, reject) {
require(deps, resolve);
});
2018-10-23 01:05:09 +03:00
}
function loadView(options) {
if (!options.cancel) {
2019-04-02 22:10:22 +01:00
var selected = selectedPageIndex;
var previousAnimatable = -1 === selected ? null : allPages[selected];
var pageIndex = selected + 1;
if (pageIndex >= pageContainerCount) {
pageIndex = 0;
}
var isPluginpage = -1 !== options.url.toLowerCase().indexOf("/configurationpage");
var newViewInfo = normalizeNewView(options, isPluginpage);
var newView = newViewInfo.elem;
2020-01-04 01:31:35 +01:00
var modulesToLoad = [];
2019-04-02 22:10:22 +01:00
if (isPluginpage) {
2020-01-04 01:31:35 +01:00
modulesToLoad.push("legacyDashboard");
2019-04-02 22:10:22 +01:00
}
if (newViewInfo.hasjQuerySelect) {
2020-01-04 01:31:35 +01:00
modulesToLoad.push("legacySelectMenu");
2019-04-02 22:10:22 +01:00
}
if (newViewInfo.hasjQueryChecked) {
2020-01-04 01:31:35 +01:00
modulesToLoad.push("fnchecked");
2019-04-02 22:10:22 +01:00
}
return new Promise(function (resolve) {
2020-01-04 01:31:35 +01:00
require(modulesToLoad, function () {
var currentPage = allPages[pageIndex];
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
if (currentPage) {
triggerDestroy(currentPage);
}
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
var view = newView;
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
if ("string" == typeof view) {
view = document.createElement("div");
view.innerHTML = newView;
}
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
view.classList.add("mainAnimatedPage");
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
if (currentPage) {
if (newViewInfo.hasScript && window.$) {
view = $(view).appendTo(mainAnimatedPages)[0];
mainAnimatedPages.removeChild(currentPage);
} else {
mainAnimatedPages.replaceChild(view, currentPage);
}
} else {
2020-01-04 01:31:35 +01:00
if (newViewInfo.hasScript && window.$) {
view = $(view).appendTo(mainAnimatedPages)[0];
} else {
mainAnimatedPages.appendChild(view);
}
2019-04-02 22:10:22 +01:00
}
2020-01-04 01:31:35 +01:00
if (options.type) {
view.setAttribute("data-type", options.type);
}
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
var properties = [];
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
if (options.fullscreen) {
properties.push("fullscreen");
}
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
if (properties.length) {
view.setAttribute("data-properties", properties.join(","));
}
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
allPages[pageIndex] = view;
setControllerClass(view, options).then(function () {
if (onBeforeChange) {
onBeforeChange(view, false, options);
}
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
beforeAnimate(allPages, pageIndex, selected);
selectedPageIndex = pageIndex;
currentUrls[pageIndex] = options.url;
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
if (!options.cancel && previousAnimatable) {
afterAnimate(allPages, pageIndex);
}
if (window.$) {
$.mobile = $.mobile || {};
$.mobile.activePage = view;
}
2019-04-02 22:10:22 +01:00
2020-01-04 01:31:35 +01:00
resolve(view);
});
2019-04-02 22:10:22 +01:00
});
});
2018-10-23 01:05:09 +03:00
}
}
function replaceAll(str, find, replace) {
2019-04-02 22:10:22 +01:00
return str.split(find).join(replace);
2018-10-23 01:05:09 +03:00
}
function parseHtml(html, hasScript) {
2019-04-02 22:10:22 +01:00
if (hasScript) {
html = replaceAll(html, "\x3c!--<script", "<script");
html = replaceAll(html, "<\/script>--\x3e", "<\/script>");
}
2018-10-23 01:05:09 +03:00
var wrapper = document.createElement("div");
2019-04-02 22:10:22 +01:00
wrapper.innerHTML = html;
return wrapper.querySelector('div[data-role="page"]');
2018-10-23 01:05:09 +03:00
}
function normalizeNewView(options, isPluginpage) {
var viewHtml = options.view;
2019-04-02 22:10:22 +01:00
if (-1 === viewHtml.indexOf('data-role="page"')) {
return viewHtml;
}
var hasScript = -1 !== viewHtml.indexOf("<script");
var elem = parseHtml(viewHtml, hasScript);
if (hasScript) {
hasScript = null != elem.querySelector("script");
}
var hasjQuery = false;
var hasjQuerySelect = false;
var hasjQueryChecked = false;
if (isPluginpage) {
hasjQuery = -1 != viewHtml.indexOf("jQuery") || -1 != viewHtml.indexOf("$(") || -1 != viewHtml.indexOf("$.");
hasjQueryChecked = -1 != viewHtml.indexOf(".checked(");
hasjQuerySelect = -1 != viewHtml.indexOf(".selectmenu(");
}
return {
2018-10-23 01:05:09 +03:00
elem: elem,
hasScript: hasScript,
hasjQuerySelect: hasjQuerySelect,
hasjQueryChecked: hasjQueryChecked,
hasjQuery: hasjQuery
2019-04-02 22:10:22 +01:00
};
2018-10-23 01:05:09 +03:00
}
function beforeAnimate(allPages, newPageIndex, oldPageIndex) {
for (var index = 0, length = allPages.length; index < length; index++) {
if (newPageIndex !== index && oldPageIndex !== index) {
allPages[index].classList.add("hide");
2019-04-02 22:10:22 +01:00
}
}
2018-10-23 01:05:09 +03:00
}
function afterAnimate(allPages, newPageIndex) {
for (var index = 0, length = allPages.length; index < length; index++) {
if (newPageIndex !== index) {
allPages[index].classList.add("hide");
2019-04-02 22:10:22 +01:00
}
}
2018-10-23 01:05:09 +03:00
}
function setOnBeforeChange(fn) {
2019-04-02 22:10:22 +01:00
onBeforeChange = fn;
2018-10-23 01:05:09 +03:00
}
function tryRestoreView(options) {
2019-04-02 22:10:22 +01:00
var url = options.url;
var index = currentUrls.indexOf(url);
2018-10-23 01:05:09 +03:00
if (-1 !== index) {
2019-04-02 22:10:22 +01:00
var animatable = allPages[index];
var view = animatable;
2018-10-23 01:05:09 +03:00
if (view) {
2019-04-02 22:10:22 +01:00
if (options.cancel) {
return;
}
var selected = selectedPageIndex;
var previousAnimatable = -1 === selected ? null : allPages[selected];
return setControllerClass(view, options).then(function () {
if (onBeforeChange) {
onBeforeChange(view, true, options);
}
beforeAnimate(allPages, index, selected);
animatable.classList.remove("hide");
selectedPageIndex = index;
if (!options.cancel && previousAnimatable) {
afterAnimate(allPages, index);
}
if (window.$) {
$.mobile = $.mobile || {};
$.mobile.activePage = view;
}
return view;
});
2018-10-23 01:05:09 +03:00
}
}
2019-04-02 22:10:22 +01:00
return Promise.reject();
2018-10-23 01:05:09 +03:00
}
function triggerDestroy(view) {
2019-04-02 22:10:22 +01:00
view.dispatchEvent(new CustomEvent("viewdestroy", {}));
2018-10-23 01:05:09 +03:00
}
function reset() {
2019-04-02 22:10:22 +01:00
allPages = [];
currentUrls = [];
mainAnimatedPages.innerHTML = "";
2018-10-23 01:05:09 +03:00
selectedPageIndex = -1;
2019-04-02 22:10:22 +01:00
}
var onBeforeChange;
var mainAnimatedPages = document.querySelector(".mainAnimatedPages");
var allPages = [];
var currentUrls = [];
var pageContainerCount = 3;
var selectedPageIndex = -1;
reset();
mainAnimatedPages.classList.remove("hide");
return {
2018-10-23 01:05:09 +03:00
loadView: loadView,
tryRestoreView: tryRestoreView,
reset: reset,
setOnBeforeChange: setOnBeforeChange
2019-04-02 22:10:22 +01:00
};
});