jellyfish-web/src/scripts/clientUtils.js

185 lines
4.8 KiB
JavaScript
Raw Normal View History

2020-10-12 22:17:25 +01:00
import AppInfo from '../components/AppInfo';
import ServerConnections from '../components/ServerConnections';
import toast from '../components/toast/toast';
import loading from '../components/loading/loading';
import { appRouter } from '../components/appRouter';
import baseAlert from '../components/alert';
import baseConfirm from '../components/confirm/confirm';
2020-10-18 20:00:39 +01:00
import globalize from '../scripts/globalize';
2020-10-12 22:17:25 +01:00
2020-08-08 15:26:03 +02:00
export function getCurrentUser() {
return window.ApiClient.getCurrentUser(false);
}
//TODO: investigate url prefix support for serverAddress function
export function serverAddress() {
if (AppInfo.isNativeApp) {
2020-08-14 05:48:59 +02:00
const apiClient = window.ApiClient;
2020-08-08 15:26:03 +02:00
if (apiClient) {
return apiClient.serverAddress();
}
return null;
}
2020-08-14 05:48:59 +02:00
const urlLower = window.location.href.toLowerCase();
const index = urlLower.lastIndexOf('/web');
2020-08-08 15:26:03 +02:00
if (index != -1) {
return urlLower.substring(0, index);
}
2020-08-14 05:48:59 +02:00
const loc = window.location;
let address = loc.protocol + '//' + loc.hostname;
2020-08-08 15:26:03 +02:00
if (loc.port) {
address += ':' + loc.port;
}
return address;
}
export function getCurrentUserId() {
2020-08-14 05:48:59 +02:00
const apiClient = window.ApiClient;
2020-08-08 15:26:03 +02:00
if (apiClient) {
return apiClient.getCurrentUserId();
}
return null;
}
export function onServerChanged(userId, accessToken, apiClient) {
ServerConnections.setLocalApiClient(apiClient);
2020-08-08 15:26:03 +02:00
}
export function logout() {
ServerConnections.logout().then(function () {
2020-08-14 05:48:59 +02:00
let loginPage;
2020-08-08 15:26:03 +02:00
if (AppInfo.isNativeApp) {
loginPage = 'selectserver.html';
window.ApiClient = null;
} else {
loginPage = 'login.html';
}
navigate(loginPage);
});
}
export function getPluginUrl(name) {
2020-08-08 15:26:03 +02:00
return 'configurationpage?name=' + encodeURIComponent(name);
}
export function navigate(url, preserveQueryString) {
if (!url) {
throw new Error('url cannot be null or empty');
}
2020-08-14 05:48:59 +02:00
const queryString = getWindowLocationSearch();
2020-08-08 15:26:03 +02:00
if (preserveQueryString && queryString) {
url += queryString;
}
return appRouter.show(url);
2020-08-08 15:26:03 +02:00
}
export function processPluginConfigurationUpdateResult() {
loading.hide();
toast(globalize.translate('SettingsSaved'));
2020-08-08 15:26:03 +02:00
}
export function processServerConfigurationUpdateResult(result) {
loading.hide();
toast(globalize.translate('SettingsSaved'));
2020-08-08 15:26:03 +02:00
}
export function processErrorResponse(response) {
loading.hide();
2020-08-08 15:26:03 +02:00
2020-08-14 05:48:59 +02:00
let status = '' + response.status;
2020-08-08 15:26:03 +02:00
if (response.statusText) {
status = response.statusText;
}
alert({
title: status,
message: response.headers ? response.headers.get('X-Application-Error-Code') : null
});
}
export function alert(options) {
if (typeof options == 'string') {
toast.default({
text: options
2020-08-08 15:26:03 +02:00
});
} else {
baseAlert.default({
2020-10-18 20:00:39 +01:00
title: options.title || globalize.translate('HeaderAlert'),
2020-08-08 15:26:03 +02:00
text: options.message
}).then(options.callback || function () {});
}
2020-08-08 15:26:03 +02:00
}
export function capabilities(appHost) {
return Object.assign({
2020-08-08 15:26:03 +02:00
PlayableMediaTypes: ['Audio', 'Video'],
SupportedCommands: ['MoveUp', 'MoveDown', 'MoveLeft', 'MoveRight', 'PageUp', 'PageDown', 'PreviousLetter', 'NextLetter', 'ToggleOsd', 'ToggleContextMenu', 'Select', 'Back', 'SendKey', 'SendString', 'GoHome', 'GoToSettings', 'VolumeUp', 'VolumeDown', 'Mute', 'Unmute', 'ToggleMute', 'SetVolume', 'SetAudioStreamIndex', 'SetSubtitleStreamIndex', 'DisplayContent', 'GoToSearch', 'DisplayMessage', 'SetRepeatMode', 'SetShuffleQueue', 'ChannelUp', 'ChannelDown', 'PlayMediaSource', 'PlayTrailers'],
2020-08-25 10:12:35 +09:00
SupportsPersistentIdentifier: window.appMode === 'cordova' || window.appMode === 'android',
2020-08-08 15:26:03 +02:00
SupportsMediaControl: true
}, appHost.getPushTokenInfo());
2020-08-08 15:26:03 +02:00
}
export function selectServer() {
if (window.NativeShell && typeof window.NativeShell.selectServer === 'function') {
window.NativeShell.selectServer();
} else {
navigate('selectserver.html');
}
}
export function hideLoadingMsg() {
loading.hide();
2020-08-08 15:26:03 +02:00
}
export function showLoadingMsg() {
loading.show();
2020-08-08 15:26:03 +02:00
}
export function confirm(message, title, callback) {
baseConfirm(message, title).then(function() {
2020-11-07 12:05:09 +00:00
callback(true);
}).catch(function() {
2020-11-07 12:05:09 +00:00
callback(false);
2020-08-08 15:26:03 +02:00
});
}
const Dashboard = {
2020-08-08 15:26:03 +02:00
alert,
capabilities,
confirm,
getPluginUrl,
2020-08-08 15:26:03 +02:00
getCurrentUser,
getCurrentUserId,
hideLoadingMsg,
logout,
navigate,
onServerChanged,
processErrorResponse,
processPluginConfigurationUpdateResult,
processServerConfigurationUpdateResult,
selectServer,
serverAddress,
showLoadingMsg
};
// This is used in plugins and templates, so keep it defined for now.
// TODO: Remove once plugins don't need it
window.Dashboard = Dashboard;
export default Dashboard;