2019-02-27 17:32:37 -07:00
|
|
|
define(['loading', 'globalize', 'events', 'viewManager', 'layoutManager', 'skinManager', 'pluginManager', 'backdrop', 'browser', 'page', 'appSettings', 'apphost', 'connectionManager'], function (loading, globalize, events, viewManager, layoutManager, skinManager, pluginManager, backdrop, browser, page, appSettings, appHost, connectionManager) {
|
2019-01-10 05:39:37 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var appRouter = {
|
|
|
|
showLocalLogin: function (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');
|
2020-02-14 17:47:07 -07:00
|
|
|
},
|
|
|
|
showNowPlaying: function () {
|
2020-05-04 03:44:12 -07:00
|
|
|
show('/nowplaying.html');
|
2019-01-10 05:39:37 -07:00
|
|
|
}
|
|
|
|
};
|
2018-10-22 15:05:09 -07:00
|
|
|
|
|
|
|
function beginConnectionWizard() {
|
2019-01-10 05:39:37 -07:00
|
|
|
backdrop.clear();
|
|
|
|
loading.show();
|
|
|
|
connectionManager.connect({
|
2018-10-22 15:05:09 -07:00
|
|
|
enableAutoLogin: appSettings.enableAutoLogin()
|
2019-01-10 05:39:37 -07:00
|
|
|
}).then(function (result) {
|
|
|
|
handleConnectionResult(result, loading);
|
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleConnectionResult(result, loading) {
|
|
|
|
switch (result.State) {
|
2019-01-10 05:39:37 -07:00
|
|
|
case 'SignedIn':
|
2019-01-16 02:30:15 -07:00
|
|
|
loading.hide();
|
|
|
|
skinManager.loadUserSkin();
|
2019-01-10 05:39:37 -07:00
|
|
|
break;
|
|
|
|
case 'ServerSignIn':
|
2019-01-16 02:30:15 -07:00
|
|
|
result.ApiClient.getPublicUsers().then(function (users) {
|
|
|
|
if (users.length) {
|
|
|
|
appRouter.showLocalLogin(result.Servers[0].Id);
|
|
|
|
} else {
|
|
|
|
appRouter.showLocalLogin(result.Servers[0].Id, true);
|
|
|
|
}
|
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
break;
|
2019-01-10 05:39:37 -07:00
|
|
|
case 'ServerSelection':
|
2019-01-16 02:30:15 -07:00
|
|
|
appRouter.showSelectServer();
|
2018-10-22 15:05:09 -07:00
|
|
|
break;
|
2019-01-19 08:39:31 -07:00
|
|
|
case 'ConnectSignIn':
|
|
|
|
appRouter.showWelcome();
|
|
|
|
break;
|
2019-01-10 05:39:37 -07:00
|
|
|
case 'ServerUpdateNeeded':
|
2019-01-16 02:30:15 -07:00
|
|
|
require(['alert'], function (alert) {
|
|
|
|
alert({
|
2019-02-02 10:41:16 -07:00
|
|
|
text: globalize.translate('ServerUpdateNeeded', 'https://github.com/jellyfin/jellyfin'),
|
|
|
|
html: globalize.translate('ServerUpdateNeeded', '<a href="https://github.com/jellyfin/jellyfin">https://github.com/jellyfin/jellyfin</a>')
|
2019-01-16 02:30:15 -07:00
|
|
|
}).then(function () {
|
|
|
|
appRouter.showSelectServer();
|
2019-01-10 05:39:37 -07:00
|
|
|
});
|
2019-01-16 02:30:15 -07:00
|
|
|
});
|
2019-01-10 05:39:37 -07:00
|
|
|
break;
|
|
|
|
default:
|
2018-10-22 15:05:09 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadContentUrl(ctx, next, route, request) {
|
|
|
|
var url;
|
2019-01-10 05:39:37 -07:00
|
|
|
if (route.contentPath && typeof (route.contentPath) === 'function') {
|
|
|
|
url = route.contentPath(ctx.querystring);
|
|
|
|
} else {
|
|
|
|
url = route.contentPath || route.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.indexOf('://') === -1) {
|
|
|
|
// Put a slash at the beginning but make sure to avoid a double slash
|
|
|
|
if (url.indexOf('/') !== 0) {
|
|
|
|
url = '/' + url;
|
|
|
|
}
|
|
|
|
|
|
|
|
url = baseUrl() + url;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.querystring && route.enableContentQueryString) {
|
|
|
|
url += '?' + ctx.querystring;
|
|
|
|
}
|
|
|
|
|
|
|
|
require(['text!' + url], function (html) {
|
|
|
|
loadContent(ctx, route, html, request);
|
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleRoute(ctx, next, route) {
|
2019-01-10 05:39:37 -07:00
|
|
|
authenticate(ctx, route, function () {
|
|
|
|
initRoute(ctx, next, route);
|
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function initRoute(ctx, next, route) {
|
2019-01-10 05:39:37 -07:00
|
|
|
var onInitComplete = function (controllerFactory) {
|
|
|
|
sendRouteToViewManager(ctx, next, route, controllerFactory);
|
2018-10-22 15:05:09 -07:00
|
|
|
};
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2019-02-24 13:07:41 -07:00
|
|
|
if (route.controller) {
|
2019-03-25 15:18:13 -07:00
|
|
|
require(['controllers/' + route.controller], onInitComplete);
|
2019-02-24 13:07:41 -07:00
|
|
|
} else {
|
|
|
|
onInitComplete();
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function cancelCurrentLoadRequest() {
|
|
|
|
var currentRequest = currentViewLoadRequest;
|
2019-01-10 05:39:37 -07:00
|
|
|
if (currentRequest) {
|
|
|
|
currentRequest.cancel = true;
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
var currentViewLoadRequest;
|
2018-10-22 15:05:09 -07:00
|
|
|
function sendRouteToViewManager(ctx, next, route, controllerFactory) {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (isDummyBackToHome && route.type === 'home') {
|
|
|
|
isDummyBackToHome = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
cancelCurrentLoadRequest();
|
2019-01-10 05:39:37 -07:00
|
|
|
var isBackNav = ctx.isBack;
|
|
|
|
|
|
|
|
var currentRequest = {
|
|
|
|
url: baseUrl() + ctx.path,
|
|
|
|
transition: route.transition,
|
|
|
|
isBack: isBackNav,
|
|
|
|
state: ctx.state,
|
|
|
|
type: route.type,
|
|
|
|
fullscreen: route.fullscreen,
|
|
|
|
controllerFactory: controllerFactory,
|
|
|
|
options: {
|
|
|
|
supportsThemeMedia: route.supportsThemeMedia || false,
|
|
|
|
enableMediaControl: route.enableMediaControl !== false
|
|
|
|
},
|
|
|
|
autoFocus: route.autoFocus
|
|
|
|
};
|
2018-10-22 15:05:09 -07:00
|
|
|
currentViewLoadRequest = currentRequest;
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
var onNewViewNeeded = function () {
|
|
|
|
if (typeof route.path === 'string') {
|
|
|
|
loadContentUrl(ctx, next, route, currentRequest);
|
|
|
|
} else {
|
|
|
|
// ? TODO
|
|
|
|
next();
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
};
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
if (!isBackNav) {
|
|
|
|
// Don't force a new view for home due to the back menu
|
|
|
|
//if (route.type !== 'home') {
|
|
|
|
onNewViewNeeded();
|
|
|
|
return;
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
viewManager.tryRestoreView(currentRequest, function () {
|
|
|
|
|
|
|
|
// done
|
2018-10-22 15:05:09 -07:00
|
|
|
currentRouteInfo = {
|
|
|
|
route: route,
|
|
|
|
path: ctx.path
|
2019-01-10 05:39:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
}).catch(function (result) {
|
|
|
|
if (!result || !result.cancelled) {
|
|
|
|
onNewViewNeeded();
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
2019-01-10 05:39:37 -07:00
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
var msgTimeout;
|
|
|
|
var forcedLogoutMsg;
|
2018-10-22 15:05:09 -07:00
|
|
|
function onForcedLogoutMessageTimeout() {
|
|
|
|
var msg = forcedLogoutMsg;
|
2019-01-10 05:39:37 -07:00
|
|
|
forcedLogoutMsg = null;
|
|
|
|
|
|
|
|
if (msg) {
|
|
|
|
require(['alert'], function (alert) {
|
|
|
|
alert(msg);
|
|
|
|
});
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function showForcedLogoutMessage(msg) {
|
2019-01-10 05:39:37 -07:00
|
|
|
forcedLogoutMsg = msg;
|
|
|
|
if (msgTimeout) {
|
|
|
|
clearTimeout(msgTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
msgTimeout = setTimeout(onForcedLogoutMessageTimeout, 100);
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function onRequestFail(e, data) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
var apiClient = this;
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-04-13 12:37:57 -07:00
|
|
|
if (data.status === 403) {
|
2020-05-04 03:44:12 -07:00
|
|
|
if (data.errorCode === 'ParentalControl') {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
var isCurrentAllowed = currentRouteInfo ? (currentRouteInfo.route.anonymous || currentRouteInfo.route.startup) : true;
|
|
|
|
|
|
|
|
// Bounce to the login screen, but not if a password entry fails, obviously
|
|
|
|
if (!isCurrentAllowed) {
|
2019-02-02 10:41:16 -07:00
|
|
|
showForcedLogoutMessage(globalize.translate('AccessRestrictedTryAgainLater'));
|
2019-01-11 04:37:32 -07:00
|
|
|
appRouter.showLocalLogin(apiClient.serverId());
|
2019-01-10 05:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onBeforeExit(e) {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (browser.web0s) {
|
|
|
|
page.restorePreviousState();
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeImageOptions(options) {
|
2019-01-10 05:39:37 -07:00
|
|
|
var scaleFactor = browser.tv ? 0.8 : 1;
|
|
|
|
|
|
|
|
var setQuality;
|
|
|
|
if (options.maxWidth) {
|
|
|
|
options.maxWidth = Math.round(options.maxWidth * scaleFactor);
|
|
|
|
setQuality = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.width) {
|
|
|
|
options.width = Math.round(options.width * scaleFactor);
|
|
|
|
setQuality = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.maxHeight) {
|
|
|
|
options.maxHeight = Math.round(options.maxHeight * scaleFactor);
|
|
|
|
setQuality = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.height) {
|
|
|
|
options.height = Math.round(options.height * scaleFactor);
|
|
|
|
setQuality = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setQuality) {
|
|
|
|
|
|
|
|
var quality = 100;
|
|
|
|
|
|
|
|
var type = options.type || 'Primary';
|
|
|
|
|
|
|
|
if (browser.tv || browser.slow) {
|
|
|
|
|
|
|
|
if (browser.chrome) {
|
|
|
|
// webp support
|
|
|
|
quality = type === 'Primary' ? 40 : 50;
|
|
|
|
} else {
|
|
|
|
quality = type === 'Backdrop' ? 60 : 50;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quality = type === 'Backdrop' ? 70 : 90;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.quality = quality;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getMaxBandwidth() {
|
2020-04-04 12:29:53 -07:00
|
|
|
/* eslint-disable compat/compat */
|
2018-10-22 15:05:09 -07:00
|
|
|
if (navigator.connection) {
|
|
|
|
var max = navigator.connection.downlinkMax;
|
2019-01-10 05:39:37 -07:00
|
|
|
if (max && max > 0 && max < Number.POSITIVE_INFINITY) {
|
|
|
|
|
|
|
|
max /= 8;
|
|
|
|
max *= 1000000;
|
|
|
|
max *= 0.7;
|
|
|
|
max = parseInt(max);
|
|
|
|
return max;
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
2020-04-04 12:29:53 -07:00
|
|
|
/* eslint-enable compat/compat */
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
return null;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function getMaxBandwidthIOS() {
|
2019-01-10 05:39:37 -07:00
|
|
|
return 800000;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function onApiClientCreated(e, newApiClient) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
newApiClient.normalizeImageOptions = normalizeImageOptions;
|
|
|
|
|
|
|
|
if (browser.iOS) {
|
|
|
|
newApiClient.getMaxBandwidth = getMaxBandwidthIOS;
|
|
|
|
} else {
|
|
|
|
newApiClient.getMaxBandwidth = getMaxBandwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
events.off(newApiClient, 'requestfail', onRequestFail);
|
|
|
|
events.on(newApiClient, 'requestfail', onRequestFail);
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function initApiClient(apiClient) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
onApiClientCreated({}, apiClient);
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function initApiClients() {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
connectionManager.getApiClients().forEach(initApiClient);
|
|
|
|
|
|
|
|
events.on(connectionManager, 'apiclientcreated', onApiClientCreated);
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function onAppResume() {
|
|
|
|
var apiClient = connectionManager.currentApiClient();
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
if (apiClient) {
|
|
|
|
apiClient.ensureWebSocket();
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
var firstConnectionResult;
|
2018-10-22 15:05:09 -07:00
|
|
|
function start(options) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
loading.show();
|
|
|
|
|
|
|
|
initApiClients();
|
|
|
|
|
|
|
|
events.on(appHost, 'beforeexit', onBeforeExit);
|
|
|
|
events.on(appHost, 'resume', onAppResume);
|
|
|
|
|
|
|
|
connectionManager.connect({
|
2018-10-22 15:05:09 -07:00
|
|
|
enableAutoLogin: appSettings.enableAutoLogin()
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
}).then(function (result) {
|
|
|
|
|
|
|
|
firstConnectionResult = result;
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
page({
|
|
|
|
click: options.click !== false,
|
|
|
|
hashbang: options.hashbang !== false,
|
2018-10-22 15:05:09 -07:00
|
|
|
enableHistory: enableHistory()
|
2019-01-10 05:39:37 -07:00
|
|
|
});
|
2019-05-07 22:17:30 -07:00
|
|
|
}).catch().then(function() {
|
2019-02-27 14:54:48 -07:00
|
|
|
loading.hide();
|
2019-01-10 05:39:37 -07:00
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function enableHistory() {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
//if (browser.edgeUwp) {
|
|
|
|
// return false;
|
|
|
|
//}
|
|
|
|
|
|
|
|
// shows status bar on navigation
|
|
|
|
if (browser.xboxOne) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does not support history
|
|
|
|
if (browser.orsay) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function enableNativeHistory() {
|
2020-03-02 11:36:17 -07:00
|
|
|
return false;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function authenticate(ctx, route, callback) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
var firstResult = firstConnectionResult;
|
2019-01-10 05:39:37 -07:00
|
|
|
if (firstResult) {
|
|
|
|
|
|
|
|
firstConnectionResult = null;
|
|
|
|
|
|
|
|
if (firstResult.State !== 'SignedIn' && !route.anonymous) {
|
|
|
|
|
|
|
|
handleConnectionResult(firstResult, loading);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var apiClient = connectionManager.currentApiClient();
|
|
|
|
var pathname = ctx.pathname.toLowerCase();
|
|
|
|
|
2020-02-15 19:44:43 -07:00
|
|
|
console.debug('appRouter - processing path request ' + pathname);
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
var isCurrentRouteStartup = currentRouteInfo ? currentRouteInfo.route.startup : true;
|
|
|
|
var shouldExitApp = ctx.isBack && route.isDefaultRoute && isCurrentRouteStartup;
|
|
|
|
|
|
|
|
if (!shouldExitApp && (!apiClient || !apiClient.isLoggedIn()) && !route.anonymous) {
|
2020-02-15 19:44:43 -07:00
|
|
|
console.debug('appRouter - route does not allow anonymous access, redirecting to login');
|
2019-01-10 05:39:37 -07:00
|
|
|
beginConnectionWizard();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
if (shouldExitApp) {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (appHost.supports('exit')) {
|
|
|
|
appHost.exit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiClient && apiClient.isLoggedIn()) {
|
|
|
|
|
2020-02-15 19:44:43 -07:00
|
|
|
console.debug('appRouter - user is authenticated');
|
2019-03-24 05:15:13 -07:00
|
|
|
|
2019-02-24 16:44:41 -07:00
|
|
|
if (route.isDefaultRoute) {
|
2020-02-15 19:44:43 -07:00
|
|
|
console.debug('appRouter - loading skin home page');
|
2019-01-10 05:39:37 -07:00
|
|
|
loadUserSkinWithOptions(ctx);
|
|
|
|
return;
|
|
|
|
} else if (route.roles) {
|
|
|
|
|
|
|
|
validateRoles(apiClient, route.roles).then(function () {
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
|
|
|
}, beginConnectionWizard);
|
|
|
|
return;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 05:39:37 -07:00
|
|
|
|
2020-02-15 19:44:43 -07:00
|
|
|
console.debug('appRouter - proceeding to ' + pathname);
|
2019-01-10 05:39:37 -07:00
|
|
|
callback();
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function loadUserSkinWithOptions(ctx) {
|
2019-01-10 05:39:37 -07:00
|
|
|
require(['queryString'], function (queryString) {
|
2018-10-22 15:05:09 -07:00
|
|
|
var params = queryString.parse(ctx.querystring);
|
|
|
|
skinManager.loadUserSkin({
|
|
|
|
start: params.start
|
2019-01-10 05:39:37 -07:00
|
|
|
});
|
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function validateRoles(apiClient, roles) {
|
2019-01-10 05:39:37 -07:00
|
|
|
return Promise.all(roles.split(',').map(function (role) {
|
|
|
|
return validateRole(apiClient, role);
|
|
|
|
}));
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function validateRole(apiClient, role) {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (role === 'admin') {
|
|
|
|
return apiClient.getCurrentUser().then(function (user) {
|
|
|
|
if (user.Policy.IsAdministrator) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return Promise.reject();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown role
|
|
|
|
return Promise.resolve();
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
var isHandlingBackToDefault;
|
|
|
|
var isDummyBackToHome;
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
function loadContent(ctx, route, html, request) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
html = globalize.translateDocument(html, route.dictionary);
|
|
|
|
request.view = html;
|
|
|
|
|
|
|
|
viewManager.loadView(request);
|
|
|
|
|
|
|
|
currentRouteInfo = {
|
2018-10-22 15:05:09 -07:00
|
|
|
route: route,
|
|
|
|
path: ctx.path
|
2019-01-10 05:39:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
ctx.handled = true;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRequestFile() {
|
2019-01-10 05:39:37 -07:00
|
|
|
var path = self.location.pathname || '';
|
|
|
|
|
|
|
|
var index = path.lastIndexOf('/');
|
|
|
|
if (index !== -1) {
|
|
|
|
path = path.substring(index);
|
|
|
|
} else {
|
|
|
|
path = '/' + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!path || path === '/') {
|
|
|
|
path = '/index.html';
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function endsWith(str, srch) {
|
2019-01-10 05:39:37 -07:00
|
|
|
return str.lastIndexOf(srch) === srch.length - 1;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
var baseRoute = self.location.href.split('?')[0].replace(getRequestFile(), '');
|
|
|
|
// support hashbang
|
|
|
|
baseRoute = baseRoute.split('#')[0];
|
|
|
|
if (endsWith(baseRoute, '/') && !endsWith(baseRoute, '://')) {
|
|
|
|
baseRoute = baseRoute.substring(0, baseRoute.length - 1);
|
|
|
|
}
|
2019-09-30 08:51:56 -07:00
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
function baseUrl() {
|
2019-01-10 05:39:37 -07:00
|
|
|
return baseRoute;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2020-03-24 00:03:51 -07:00
|
|
|
var popstateOccurred = false;
|
|
|
|
window.addEventListener('popstate', function () {
|
|
|
|
popstateOccurred = true;
|
|
|
|
});
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
function getHandler(route) {
|
2019-01-10 05:39:37 -07:00
|
|
|
return function (ctx, next) {
|
2020-03-24 00:03:51 -07:00
|
|
|
ctx.isBack = popstateOccurred;
|
2019-01-10 05:39:37 -07:00
|
|
|
handleRoute(ctx, next, route);
|
2020-03-24 00:03:51 -07:00
|
|
|
popstateOccurred = false;
|
2019-01-10 05:39:37 -07:00
|
|
|
};
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function getWindowLocationSearch(win) {
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
var currentPath = currentRouteInfo ? (currentRouteInfo.path || '') : '';
|
|
|
|
|
|
|
|
var index = currentPath.indexOf('?');
|
|
|
|
var search = '';
|
|
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
search = currentPath.substring(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return search || '';
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function param(name, url) {
|
2020-05-04 03:44:12 -07:00
|
|
|
name = name.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
|
|
|
|
var regexS = '[\\?&]' + name + '=([^&#]*)';
|
|
|
|
var regex = new RegExp(regexS, 'i');
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
var results = regex.exec(url || getWindowLocationSearch());
|
|
|
|
if (results == null) {
|
2020-05-04 03:44:12 -07:00
|
|
|
return '';
|
2019-01-10 05:39:37 -07:00
|
|
|
} else {
|
2020-05-04 03:44:12 -07:00
|
|
|
return decodeURIComponent(results[1].replace(/\+/g, ' '));
|
2019-01-10 05:39:37 -07:00
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function back() {
|
2019-01-10 05:39:37 -07:00
|
|
|
page.back();
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2020-01-24 15:18:07 -07:00
|
|
|
/**
|
2020-01-27 06:47:26 -07:00
|
|
|
* Pages of "no return" (when "Go back" should behave differently, probably quitting the application).
|
2020-01-24 15:18:07 -07:00
|
|
|
*/
|
2020-01-25 05:32:24 -07:00
|
|
|
var startPages = ['home', 'login', 'selectserver'];
|
2020-01-24 15:18:07 -07:00
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
function canGoBack() {
|
|
|
|
var curr = current();
|
2019-01-10 05:39:37 -07:00
|
|
|
if (!curr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:18:07 -07:00
|
|
|
if (!document.querySelector('.dialogContainer') && startPages.indexOf(curr.type) !== -1) {
|
2019-01-10 05:39:37 -07:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-02 15:42:22 -07:00
|
|
|
if (enableHistory()) {
|
2020-03-01 15:15:49 -07:00
|
|
|
return history.length > 1;
|
|
|
|
}
|
|
|
|
return (page.len || 0) > 0;
|
2019-01-10 05:39:37 -07:00
|
|
|
}
|
2019-01-11 05:40:36 -07:00
|
|
|
|
|
|
|
function showDirect(path) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
2020-04-06 13:03:09 -07:00
|
|
|
resolveOnNextShow = resolve;
|
2020-04-06 14:43:03 -07:00
|
|
|
page.show(baseUrl() + path);
|
2020-04-05 04:48:10 -07:00
|
|
|
});
|
2019-01-11 05:40:36 -07:00
|
|
|
}
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
function show(path, options) {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (path.indexOf('/') !== 0 && path.indexOf('://') === -1) {
|
|
|
|
path = '/' + path;
|
|
|
|
}
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
var baseRoute = baseUrl();
|
2019-01-10 05:39:37 -07:00
|
|
|
path = path.replace(baseRoute, '');
|
|
|
|
|
|
|
|
if (currentRouteInfo && currentRouteInfo.path === path) {
|
|
|
|
// can't use this with home right now due to the back menu
|
|
|
|
if (currentRouteInfo.route.type !== 'home') {
|
|
|
|
loading.hide();
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
resolveOnNextShow = resolve;
|
|
|
|
page.show(path, options);
|
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
var resolveOnNextShow;
|
|
|
|
document.addEventListener('viewshow', function () {
|
|
|
|
var resolve = resolveOnNextShow;
|
|
|
|
if (resolve) {
|
|
|
|
resolveOnNextShow = null;
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var currentRouteInfo;
|
2018-10-22 15:05:09 -07:00
|
|
|
function current() {
|
2019-01-10 05:39:37 -07:00
|
|
|
return currentRouteInfo ? currentRouteInfo.route : null;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function showItem(item, serverId, options) {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (typeof (item) === 'string') {
|
2018-10-22 15:05:09 -07:00
|
|
|
var apiClient = serverId ? connectionManager.getApiClient(serverId) : connectionManager.currentApiClient();
|
2019-01-10 05:39:37 -07:00
|
|
|
apiClient.getItem(apiClient.getCurrentUserId(), item).then(function (item) {
|
|
|
|
appRouter.showItem(item, options);
|
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
} else {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (arguments.length === 2) {
|
|
|
|
options = arguments[1];
|
|
|
|
}
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
var url = appRouter.getRouteUrl(item, options);
|
|
|
|
appRouter.show(url, {
|
|
|
|
item: item
|
2019-01-10 05:39:37 -07:00
|
|
|
});
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
var allRoutes = [];
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
function addRoute(path, newRoute) {
|
2019-01-10 05:39:37 -07:00
|
|
|
page(path, getHandler(newRoute));
|
|
|
|
allRoutes.push(newRoute);
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function getRoutes() {
|
2019-01-10 05:39:37 -07:00
|
|
|
return allRoutes;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
var backdropContainer;
|
|
|
|
var backgroundContainer;
|
2018-10-22 15:05:09 -07:00
|
|
|
function setTransparency(level) {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (!backdropContainer) {
|
|
|
|
backdropContainer = document.querySelector('.backdropContainer');
|
|
|
|
}
|
|
|
|
if (!backgroundContainer) {
|
|
|
|
backgroundContainer = document.querySelector('.backgroundContainer');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (level === 'full' || level === 2) {
|
|
|
|
backdrop.clear(true);
|
|
|
|
document.documentElement.classList.add('transparentDocument');
|
|
|
|
backgroundContainer.classList.add('backgroundContainer-transparent');
|
|
|
|
backdropContainer.classList.add('hide');
|
2019-09-30 08:51:56 -07:00
|
|
|
} else if (level === 'backdrop' || level === 1) {
|
2019-01-10 05:39:37 -07:00
|
|
|
backdrop.externalBackdrop(true);
|
|
|
|
document.documentElement.classList.add('transparentDocument');
|
|
|
|
backgroundContainer.classList.add('backgroundContainer-transparent');
|
|
|
|
backdropContainer.classList.add('hide');
|
|
|
|
} else {
|
|
|
|
backdrop.externalBackdrop(false);
|
|
|
|
document.documentElement.classList.remove('transparentDocument');
|
|
|
|
backgroundContainer.classList.remove('backgroundContainer-transparent');
|
|
|
|
backdropContainer.classList.remove('hide');
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function pushState(state, title, url) {
|
2019-01-10 05:39:37 -07:00
|
|
|
state.navigate = false;
|
2020-03-01 15:15:49 -07:00
|
|
|
history.pushState(state, title, url);
|
|
|
|
|
2019-01-10 05:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function setBaseRoute() {
|
|
|
|
var baseRoute = self.location.pathname.replace(getRequestFile(), '');
|
|
|
|
if (baseRoute.lastIndexOf('/') === baseRoute.length - 1) {
|
|
|
|
baseRoute = baseRoute.substring(0, baseRoute.length - 1);
|
|
|
|
}
|
|
|
|
|
2020-02-15 19:44:43 -07:00
|
|
|
console.debug('setting page base to ' + baseRoute);
|
2019-01-10 05:39:37 -07:00
|
|
|
page.base(baseRoute);
|
|
|
|
}
|
|
|
|
|
|
|
|
setBaseRoute();
|
|
|
|
|
2018-10-22 15:05:09 -07:00
|
|
|
function invokeShortcut(id) {
|
2019-01-10 05:39:37 -07:00
|
|
|
if (id.indexOf('library-') === 0) {
|
|
|
|
id = id.replace('library-', '');
|
|
|
|
id = id.split('_');
|
|
|
|
|
|
|
|
appRouter.showItem(id[0], id[1]);
|
|
|
|
} else if (id.indexOf('item-') === 0) {
|
|
|
|
id = id.replace('item-', '');
|
|
|
|
id = id.split('_');
|
|
|
|
|
|
|
|
appRouter.showItem(id[0], id[1]);
|
|
|
|
} else {
|
|
|
|
id = id.split('_');
|
|
|
|
appRouter.show(appRouter.getRouteUrl(id[0], {
|
|
|
|
serverId: id[1]
|
|
|
|
}));
|
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
2019-01-10 05:39:37 -07:00
|
|
|
|
|
|
|
appRouter.addRoute = addRoute;
|
|
|
|
appRouter.param = param;
|
|
|
|
appRouter.back = back;
|
|
|
|
appRouter.show = show;
|
2019-01-11 05:40:36 -07:00
|
|
|
appRouter.showDirect = showDirect;
|
2019-01-10 05:39:37 -07:00
|
|
|
appRouter.start = start;
|
|
|
|
appRouter.baseUrl = baseUrl;
|
|
|
|
appRouter.canGoBack = canGoBack;
|
|
|
|
appRouter.current = current;
|
|
|
|
appRouter.beginConnectionWizard = beginConnectionWizard;
|
2019-09-30 08:51:56 -07:00
|
|
|
appRouter.invokeShortcut = invokeShortcut;
|
2019-01-10 05:39:37 -07:00
|
|
|
appRouter.showItem = showItem;
|
|
|
|
appRouter.setTransparency = setTransparency;
|
|
|
|
appRouter.getRoutes = getRoutes;
|
|
|
|
appRouter.pushState = pushState;
|
|
|
|
appRouter.enableNativeHistory = enableNativeHistory;
|
2020-03-01 15:15:49 -07:00
|
|
|
appRouter.handleAnchorClick = page.clickHandler;
|
2019-01-10 05:39:37 -07:00
|
|
|
appRouter.TransparencyLevel = {
|
2018-10-22 15:05:09 -07:00
|
|
|
None: 0,
|
|
|
|
Backdrop: 1,
|
|
|
|
Full: 2
|
2019-01-10 05:39:37 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return appRouter;
|
2018-12-10 23:31:08 -07:00
|
|
|
});
|