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

636 lines
18 KiB
JavaScript
Raw Normal View History

2016-11-24 23:58:38 -07:00
define(['loading', 'viewManager', 'skinManager', 'pluginManager', 'backdrop', 'browser', 'pageJs', 'appSettings', 'apphost'], function (loading, viewManager, skinManager, pluginManager, backdrop, browser, page, appSettings, appHost) {
2016-10-17 22:06:48 -07:00
'use strict';
2016-03-14 13:13:14 -07:00
2016-03-15 21:03:49 -07:00
var embyRouter = {
showLocalLogin: function (apiClient, serverId, manualLogin) {
var pageName = manualLogin ? 'manuallogin' : 'login';
show('/startup/' + pageName + '.html?serverid=' + serverId);
},
showSelectServer: function () {
show('/startup/selectserver.html');
},
showWelcome: function () {
show('/startup/welcome.html');
},
showSettings: function () {
show('/settings/settings.html');
2016-04-09 12:04:14 -07:00
},
showSearch: function () {
skinManager.getCurrentSkin().search();
2016-05-20 12:45:04 -07:00
},
2016-07-06 10:44:44 -07:00
showGenre: function (options) {
skinManager.getCurrentSkin().showGenre(options);
},
2016-05-20 12:45:04 -07:00
showGuide: function () {
skinManager.getCurrentSkin().showGuide();
},
showLiveTV: function () {
skinManager.getCurrentSkin().showLiveTV();
2016-07-06 12:25:58 -07:00
},
showRecordedTV: function () {
skinManager.getCurrentSkin().showRecordedTV();
},
showFavorites: function () {
skinManager.getCurrentSkin().showFavorites();
2016-03-15 21:03:49 -07:00
}
};
2016-03-14 13:13:14 -07:00
var connectionManager;
2016-03-16 22:06:37 -07:00
function beginConnectionWizard() {
2016-03-14 13:13:14 -07:00
backdrop.clear();
loading.show();
2016-05-25 09:10:57 -07:00
connectionManager.connect({
enableAutoLogin: appSettings.enableAutoLogin()
}).then(function (result) {
2016-03-14 13:13:14 -07:00
handleConnectionResult(result, loading);
});
}
function handleConnectionResult(result, loading) {
switch (result.State) {
case MediaBrowser.ConnectionState.SignedIn:
{
loading.hide();
skinManager.loadUserSkin();
}
break;
case MediaBrowser.ConnectionState.ServerSignIn:
{
result.ApiClient.getPublicUsers().then(function (users) {
if (users.length) {
2016-03-15 21:03:49 -07:00
embyRouter.showLocalLogin(result.ApiClient, result.Servers[0].Id);
2016-03-14 13:13:14 -07:00
} else {
2016-03-15 21:03:49 -07:00
embyRouter.showLocalLogin(result.ApiClient, result.Servers[0].Id, true);
2016-03-14 13:13:14 -07:00
}
});
}
break;
case MediaBrowser.ConnectionState.ServerSelection:
{
2016-03-15 21:03:49 -07:00
embyRouter.showSelectServer();
2016-03-14 13:13:14 -07:00
}
break;
case MediaBrowser.ConnectionState.ConnectSignIn:
{
2016-03-15 21:03:49 -07:00
embyRouter.showWelcome();
2016-03-14 13:13:14 -07:00
}
break;
case MediaBrowser.ConnectionState.ServerUpdateNeeded:
{
require(['alert'], function (alert) {
2016-04-09 19:27:09 -07:00
alert({
2016-05-12 12:21:43 -07:00
text: Globalize.translate('sharedcomponents#ServerUpdateNeeded', 'https://emby.media'),
html: Globalize.translate('sharedcomponents#ServerUpdateNeeded', '<a href="https://emby.media">https://emby.media</a>')
2016-04-09 19:27:09 -07:00
}).then(function () {
2016-03-15 21:03:49 -07:00
embyRouter.showSelectServer();
2016-03-14 13:13:14 -07:00
});
});
}
break;
default:
break;
}
}
function loadContentUrl(ctx, next, route, request) {
var url = route.contentPath || route.path;
2016-10-17 22:06:48 -07:00
if (url.indexOf('://') === -1) {
2016-03-15 20:14:29 -07:00
2016-03-15 21:03:49 -07:00
// Put a slash at the beginning but make sure to avoid a double slash
2016-10-17 22:06:48 -07:00
if (url.indexOf('/') !== 0) {
2016-03-15 20:14:29 -07:00
url = '/' + url;
}
url = baseUrl() + url;
2016-03-14 13:13:14 -07:00
}
2016-03-15 22:33:31 -07:00
if (ctx.querystring && route.enableContentQueryString) {
url += '?' + ctx.querystring;
}
2016-08-28 11:59:14 -07:00
require(['text!' + url], function (html) {
2016-03-14 13:13:14 -07:00
2016-08-28 11:59:14 -07:00
loadContent(ctx, route, html, request);
});
2016-03-14 13:13:14 -07:00
}
function handleRoute(ctx, next, route) {
authenticate(ctx, route, function () {
initRoute(ctx, next, route);
});
}
function initRoute(ctx, next, route) {
var onInitComplete = function (controllerFactory) {
sendRouteToViewManager(ctx, next, route, controllerFactory);
};
require(route.dependencies || [], function () {
if (route.controller) {
require([route.controller], onInitComplete);
} else {
onInitComplete();
}
});
}
function cancelCurrentLoadRequest() {
var currentRequest = currentViewLoadRequest;
if (currentRequest) {
currentRequest.cancel = true;
}
}
var currentViewLoadRequest;
function sendRouteToViewManager(ctx, next, route, controllerFactory) {
2016-10-17 22:06:48 -07:00
if (isDummyBackToHome && route.type === 'home') {
2016-05-11 10:46:44 -07:00
isDummyBackToHome = false;
return;
}
2016-03-14 13:13:14 -07:00
cancelCurrentLoadRequest();
var isBackNav = ctx.isBack;
var currentRequest = {
url: baseUrl() + ctx.path,
transition: route.transition,
isBack: isBackNav,
state: ctx.state,
type: route.type,
controllerFactory: controllerFactory,
options: {
supportsThemeMedia: route.supportsThemeMedia || false
2016-03-15 13:04:29 -07:00
},
autoFocus: route.autoFocus
2016-03-14 13:13:14 -07:00
};
currentViewLoadRequest = currentRequest;
var onNewViewNeeded = function () {
if (typeof route.path === 'string') {
loadContentUrl(ctx, next, route, currentRequest);
} else {
// ? TODO
next();
}
};
if (!isBackNav) {
// Don't force a new view for home due to the back menu
2016-10-17 22:06:48 -07:00
//if (route.type !== 'home') {
2016-05-25 09:10:57 -07:00
onNewViewNeeded();
return;
2016-05-19 07:37:59 -07:00
//}
2016-03-14 13:13:14 -07:00
}
viewManager.tryRestoreView(currentRequest, function () {
2016-03-14 13:13:14 -07:00
// done
currentRouteInfo = {
route: route,
path: ctx.path
};
}).catch(function (result) {
if (!result || !result.cancelled) {
onNewViewNeeded();
}
});
2016-03-14 13:13:14 -07:00
}
var firstConnectionResult;
2016-03-15 19:46:13 -07:00
function start(options) {
2016-03-14 13:13:14 -07:00
loading.show();
require(['connectionManager'], function (connectionManagerInstance) {
connectionManager = connectionManagerInstance;
2016-05-25 09:10:57 -07:00
connectionManager.connect({
enableAutoLogin: appSettings.enableAutoLogin()
}).then(function (result) {
2016-03-14 13:13:14 -07:00
firstConnectionResult = result;
loading.hide();
2016-03-15 19:46:13 -07:00
options = options || {};
2016-03-14 13:13:14 -07:00
page({
2016-03-15 20:14:29 -07:00
click: options.click !== false,
hashbang: options.hashbang !== false,
2016-03-14 13:13:14 -07:00
enableHistory: enableHistory()
});
});
});
}
function enableHistory() {
if (browser.xboxOne || browser.edgeUwp) {
2016-03-14 13:13:14 -07:00
return false;
}
return true;
}
function enableNativeHistory() {
return page.enableNativeHistory();
}
function authenticate(ctx, route, callback) {
var firstResult = firstConnectionResult;
if (firstResult) {
firstConnectionResult = null;
2016-10-17 22:06:48 -07:00
if (firstResult.State !== MediaBrowser.ConnectionState.SignedIn && !route.anonymous) {
2016-03-27 16:22:53 -07:00
2016-03-14 13:13:14 -07:00
handleConnectionResult(firstResult, loading);
return;
}
}
2016-03-14 21:00:17 -07:00
var apiClient = connectionManager.currentApiClient();
2016-03-14 13:13:14 -07:00
var pathname = ctx.pathname.toLowerCase();
2016-04-26 11:28:04 -07:00
console.log('embyRouter - processing path request ' + pathname);
2016-03-14 13:13:14 -07:00
2016-08-04 10:15:34 -07:00
var isCurrentRouteStartup = currentRouteInfo ? currentRouteInfo.route.startup : true;
var shouldExitApp = ctx.isBack && route.isDefaultRoute && isCurrentRouteStartup;
if (!shouldExitApp && (!apiClient || !apiClient.isLoggedIn()) && !route.anonymous) {
2016-04-26 11:28:04 -07:00
console.log('embyRouter - route does not allow anonymous access, redirecting to login');
2016-03-16 22:06:37 -07:00
beginConnectionWizard();
return;
}
2016-08-04 10:15:34 -07:00
if (shouldExitApp) {
if (appHost.supports('exit')) {
appHost.exit();
return;
}
return;
}
2016-03-14 21:00:17 -07:00
if (apiClient && apiClient.isLoggedIn()) {
2016-03-14 13:13:14 -07:00
2016-04-26 11:28:04 -07:00
console.log('embyRouter - user is authenticated');
2016-03-14 13:13:14 -07:00
2016-03-27 16:22:53 -07:00
if (ctx.isBack && (route.isDefaultRoute || route.startup) && !isCurrentRouteStartup) {
2016-03-14 13:13:14 -07:00
handleBackToDefault();
2016-03-16 22:06:37 -07:00
return;
2016-03-14 13:13:14 -07:00
}
else if (route.isDefaultRoute) {
2016-04-26 11:28:04 -07:00
console.log('embyRouter - loading skin home page');
2016-03-14 13:13:14 -07:00
skinManager.loadUserSkin();
2016-03-16 22:06:37 -07:00
return;
} else if (route.roles) {
2016-04-08 20:08:50 -07:00
validateRoles(apiClient, route.roles).then(function () {
apiClient.ensureWebSocket();
callback();
}, beginConnectionWizard);
2016-03-16 22:06:37 -07:00
return;
2016-03-14 13:13:14 -07:00
}
}
2016-04-26 11:28:04 -07:00
console.log('embyRouter - proceeding to ' + pathname);
2016-03-16 22:06:37 -07:00
callback();
}
2016-03-14 13:13:14 -07:00
2016-03-16 22:06:37 -07:00
function validateRoles(apiClient, roles) {
2016-03-14 13:13:14 -07:00
2016-03-16 22:06:37 -07:00
return Promise.all(roles.split(',').map(function (role) {
return validateRole(apiClient, role);
}));
}
function validateRole(apiClient, role) {
2016-03-14 13:13:14 -07:00
2016-10-17 22:06:48 -07:00
if (role === 'admin') {
2016-03-16 22:06:37 -07:00
return apiClient.getCurrentUser().then(function (user) {
if (user.Policy.IsAdministrator) {
return Promise.resolve();
}
return Promise.reject();
});
2016-03-14 13:13:14 -07:00
}
2016-03-16 22:06:37 -07:00
// Unknown role
return Promise.resolve();
2016-03-14 13:13:14 -07:00
}
var isHandlingBackToDefault;
2016-05-11 10:46:44 -07:00
var isDummyBackToHome;
2016-03-14 13:13:14 -07:00
function handleBackToDefault() {
2016-08-23 23:13:15 -07:00
if (!appHost.supports('exitmenu') && appHost.supports('exit')) {
appHost.exit();
return;
}
2016-05-11 10:46:44 -07:00
isDummyBackToHome = true;
2016-03-14 13:13:14 -07:00
skinManager.loadUserSkin();
if (isHandlingBackToDefault) {
return;
}
// This must result in a call to either
// skinManager.loadUserSkin();
// Logout
// Or exit app
skinManager.getCurrentSkin().showBackMenu().then(function () {
isHandlingBackToDefault = false;
});
}
function loadContent(ctx, route, html, request) {
html = Globalize.translateDocument(html, route.dictionary);
request.view = html;
viewManager.loadView(request);
currentRouteInfo = {
route: route,
path: ctx.path
};
//next();
ctx.handled = true;
}
2016-03-16 11:09:58 -07:00
function getRequestFile() {
2016-03-16 11:27:50 -07:00
var path = window.location.pathname || '';
2016-03-16 11:09:58 -07:00
var index = path.lastIndexOf('/');
2016-10-17 22:06:48 -07:00
if (index !== -1) {
2016-03-16 11:27:50 -07:00
path = path.substring(index);
} else {
path = '/' + path;
}
2016-10-17 22:06:48 -07:00
if (!path || path === '/') {
2016-03-16 11:27:50 -07:00
path = '/index.html';
2016-03-16 11:09:58 -07:00
}
2016-03-16 11:27:50 -07:00
return path;
2016-03-16 11:09:58 -07:00
}
var baseRoute = window.location.href.split('?')[0].replace(getRequestFile(), '');
2016-03-14 13:13:14 -07:00
// support hashbang
baseRoute = baseRoute.split('#')[0];
2016-10-17 22:06:48 -07:00
if (baseRoute.lastIndexOf('/') === baseRoute.length - 1) {
2016-03-14 13:13:14 -07:00
baseRoute = baseRoute.substring(0, baseRoute.length - 1);
}
function baseUrl() {
return baseRoute;
}
function getHandler(route) {
return function (ctx, next) {
handleRoute(ctx, next, route);
};
}
function getWindowLocationSearch(win) {
var currentPath = currentRouteInfo ? (currentRouteInfo.path || '') : '';
var index = currentPath.indexOf('?');
var search = '';
2016-10-17 22:06:48 -07:00
if (index !== -1) {
2016-03-14 13:13:14 -07:00
search = currentPath.substring(index);
}
return search || '';
}
function param(name, url) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS, "i");
var results = regex.exec(url || getWindowLocationSearch());
2016-10-17 22:06:48 -07:00
if (results == null) {
2016-03-14 13:13:14 -07:00
return "";
2016-10-17 22:06:48 -07:00
} else {
2016-03-14 13:13:14 -07:00
return decodeURIComponent(results[1].replace(/\+/g, " "));
2016-10-17 22:06:48 -07:00
}
2016-03-14 13:13:14 -07:00
}
function back() {
page.back();
}
function canGoBack() {
var curr = current();
if (!curr) {
return false;
}
2016-10-17 22:06:48 -07:00
if (curr.type === 'home') {
2016-03-14 13:13:14 -07:00
return false;
}
return page.canGoBack();
}
function show(path, options) {
2016-07-07 14:31:40 -07:00
var baseRoute = baseUrl();
path = path.replace(baseRoute, '');
2016-03-14 13:13:14 -07:00
2016-10-17 22:06:48 -07:00
if (currentRouteInfo && currentRouteInfo.path === path) {
2016-03-14 13:13:14 -07:00
2016-07-07 14:31:40 -07:00
// can't use this with home right now due to the back menu
2016-10-17 22:06:48 -07:00
if (currentRouteInfo.route.type !== 'home') {
2016-08-04 10:15:34 -07:00
loading.hide();
2016-07-07 14:31:40 -07:00
return Promise.resolve();
2016-03-14 13:13:14 -07:00
}
2016-07-07 14:31:40 -07:00
}
2016-03-14 13:13:14 -07:00
2016-07-07 14:31:40 -07:00
return new Promise(function (resolve, reject) {
2016-09-05 22:02:05 -07:00
resolveOnNextShow = resolve;
page.show(path, options);
2016-03-14 13:13:14 -07:00
});
}
2016-09-05 22:02:05 -07:00
var resolveOnNextShow;
2016-11-24 23:58:38 -07:00
document.addEventListener('viewshow', function () {
2016-09-05 22:02:05 -07:00
var resolve = resolveOnNextShow;
if (resolve) {
resolveOnNextShow = null;
resolve();
}
});
2016-03-14 13:13:14 -07:00
var currentRouteInfo;
function current() {
return currentRouteInfo ? currentRouteInfo.route : null;
}
function goHome() {
var skin = skinManager.getCurrentSkin();
var homeRoute = skin.getRoutes().filter(function (r) {
2016-10-17 22:06:48 -07:00
return r.type === 'home';
2016-03-14 13:13:14 -07:00
})[0];
return show(pluginManager.mapRoute(skin, homeRoute));
}
2016-08-24 20:07:31 -07:00
function showItem(item, serverId, options) {
2016-03-14 13:13:14 -07:00
if (typeof (item) === 'string') {
2016-04-26 11:28:04 -07:00
require(['connectionManager'], function (connectionManager) {
2016-05-11 15:08:19 -07:00
var apiClient = serverId ? connectionManager.getApiClient(serverId) : connectionManager.currentApiClient();
2016-05-11 22:58:05 -07:00
apiClient.getItem(apiClient.getCurrentUserId(), item).then(function (item) {
2016-08-24 20:07:31 -07:00
embyRouter.showItem(item, options);
2016-05-11 22:58:05 -07:00
});
2016-04-26 11:28:04 -07:00
});
2016-03-14 13:13:14 -07:00
} else {
2016-08-24 20:07:31 -07:00
2016-10-17 22:06:48 -07:00
if (arguments.length === 2) {
2016-08-24 20:07:31 -07:00
options = arguments[1];
}
skinManager.getCurrentSkin().showItem(item, options);
2016-03-14 13:13:14 -07:00
}
}
function setTitle(title) {
skinManager.getCurrentSkin().setTitle(title);
}
function showVideoOsd() {
var skin = skinManager.getCurrentSkin();
var homeRoute = skin.getRoutes().filter(function (r) {
2016-10-17 22:06:48 -07:00
return r.type === 'video-osd';
2016-03-14 13:13:14 -07:00
})[0];
return show(pluginManager.mapRoute(skin, homeRoute));
}
var allRoutes = [];
function addRoute(path, newRoute) {
page(path, getHandler(newRoute));
allRoutes.push(newRoute);
}
function getRoutes() {
return allRoutes;
}
2016-08-20 14:58:28 -07:00
var backdropContainer;
var backgroundContainer;
2016-03-14 13:13:14 -07:00
function setTransparency(level) {
2016-08-20 14:58:28 -07:00
if (!backdropContainer) {
backdropContainer = document.querySelector('.backdropContainer');
}
if (!backgroundContainer) {
backgroundContainer = document.querySelector('.backgroundContainer');
}
2016-10-17 22:06:48 -07:00
if (level === 'full' || level === 2) {
2016-03-14 13:13:14 -07:00
backdrop.clear(true);
document.documentElement.classList.add('transparentDocument');
2016-08-20 14:58:28 -07:00
backgroundContainer.classList.add('backgroundContainer-transparent');
backdropContainer.classList.add('hide');
2016-03-14 13:13:14 -07:00
}
2016-10-17 22:06:48 -07:00
else if (level === 'backdrop' || level === 1) {
2016-03-14 13:13:14 -07:00
backdrop.externalBackdrop(true);
document.documentElement.classList.add('transparentDocument');
2016-08-20 14:58:28 -07:00
backgroundContainer.classList.add('backgroundContainer-transparent');
backdropContainer.classList.add('hide');
2016-03-14 13:13:14 -07:00
} else {
backdrop.externalBackdrop(false);
document.documentElement.classList.remove('transparentDocument');
2016-08-20 14:58:28 -07:00
backgroundContainer.classList.remove('backgroundContainer-transparent');
backdropContainer.classList.remove('hide');
2016-03-14 13:13:14 -07:00
}
}
function pushState(state, title, url) {
state.navigate = false;
page.pushState(state, title, url);
}
2016-03-14 21:00:17 -07:00
function setBaseRoute() {
2016-03-16 11:09:58 -07:00
var baseRoute = window.location.pathname.replace(getRequestFile(), '');
2016-10-17 22:06:48 -07:00
if (baseRoute.lastIndexOf('/') === baseRoute.length - 1) {
2016-03-14 21:00:17 -07:00
baseRoute = baseRoute.substring(0, baseRoute.length - 1);
}
console.log('Setting page base to ' + baseRoute);
page.base(baseRoute);
}
setBaseRoute();
2016-03-15 21:03:49 -07:00
embyRouter.addRoute = addRoute;
embyRouter.param = param;
embyRouter.back = back;
embyRouter.show = show;
embyRouter.start = start;
embyRouter.baseUrl = baseUrl;
embyRouter.canGoBack = canGoBack;
embyRouter.current = current;
2016-03-16 22:06:37 -07:00
embyRouter.beginConnectionWizard = beginConnectionWizard;
2016-03-15 21:03:49 -07:00
embyRouter.goHome = goHome;
embyRouter.showItem = showItem;
embyRouter.setTitle = setTitle;
embyRouter.setTransparency = setTransparency;
embyRouter.getRoutes = getRoutes;
embyRouter.pushState = pushState;
embyRouter.enableNativeHistory = enableNativeHistory;
2016-03-16 10:31:38 -07:00
embyRouter.showVideoOsd = showVideoOsd;
2016-03-15 21:03:49 -07:00
embyRouter.TransparencyLevel = {
None: 0,
Backdrop: 1,
Full: 2
2016-03-14 13:13:14 -07:00
};
2016-03-15 21:03:49 -07:00
return embyRouter;
2016-03-14 13:13:14 -07:00
});