jellyfish-web/src/scripts/clientUtils.js

231 lines
6.5 KiB
JavaScript
Raw Normal View History

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';
import * as webSettings from './settings/webSettings';
2021-06-11 03:32:34 +03:00
import datetime from '../scripts/datetime';
2021-06-11 20:29:07 +03:00
import { DirectoryBrowser } from '../components/directorybrowser/directorybrowser';
2021-06-11 03:32:34 +03:00
import dialogHelper from '../components/dialogHelper/dialogHelper';
import itemIdentifier from '../components/itemidentifier/itemidentifier';
2021-06-11 03:32:34 +03:00
import { Events } from 'jellyfin-apiclient';
2020-08-14 05:48:59 +02: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 async function serverAddress() {
const apiClient = window.ApiClient;
2020-08-08 15:26:03 +02:00
if (apiClient) {
return Promise.resolve(apiClient.serverAddress());
2020-08-08 15:26:03 +02:00
}
2020-11-22 04:07:29 +01:00
// Use servers specified in config.json
const urls = await webSettings.getServers();
// Otherwise use computed base URL
2020-11-22 04:07:29 +01:00
if (urls.length == 0) {
const index = window.location.href.toLowerCase().lastIndexOf('/web');
if (index != -1) {
urls.push(window.location.href.substring(0, index));
} else {
// fallback to location without path
2020-11-22 04:07:29 +01:00
urls.push(window.location.origin);
}
}
2020-11-24 20:44:14 +01:00
console.debug('URL candidates:', urls);
const promises = urls.map(url => {
return fetch(`${url}/System/Info/Public`).then(resp => {
return {
url: url,
2020-11-22 16:54:24 +01:00
response: resp
};
2021-01-26 22:20:12 -05:00
}).catch(() => {
return Promise.resolve();
});
});
2020-08-08 15:26:03 +02:00
return Promise.all(promises).then(responses => {
responses = responses.filter(obj => obj && obj.response.ok);
return Promise.all(responses.map(obj => {
return {
url: obj.url,
2020-11-22 16:54:24 +01:00
config: obj.response.json()
};
}));
}).then(configs => {
2020-11-24 21:20:05 +01:00
const selection = configs.find(obj => !obj.config.StartupWizardCompleted) || configs[0];
return Promise.resolve(selection?.url);
}).catch(error => {
console.log(error);
return Promise.resolve();
});
2020-08-08 15:26:03 +02:00
}
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 () {
webSettings.getMultiServer().then(multi => {
multi ? navigate('selectserver.html') : navigate('login.html');
});
2020-08-08 15:26:03 +02:00
});
}
export function getPluginUrl(name) {
2020-12-06 22:33:00 +03:00
return 'configurationpage?name=' + encodeURIComponent(name);
2020-08-08 15:26:03 +02:00
}
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
}
2021-01-26 22:20:12 -05:00
export function processServerConfigurationUpdateResult() {
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;
}
2021-05-26 13:17:32 -04:00
baseAlert({
2020-08-08 15:26:03 +02:00
title: status,
2021-05-26 13:17:32 -04:00
text: response.headers ? response.headers.get('X-Application-Error-Code') : null
2020-08-08 15:26:03 +02:00
});
}
export function alert(options) {
if (typeof options == 'string') {
toast({
text: options
2020-08-08 15:26:03 +02:00
});
} else {
baseAlert({
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
});
}
export const pageClassOn = function(eventName, className, fn) {
document.addEventListener(eventName, function (event) {
const target = event.target;
if (target.classList.contains(className)) {
fn.call(target, event);
}
});
};
export const pageIdOn = function(eventName, id, fn) {
document.addEventListener(eventName, function (event) {
const target = event.target;
if (target.id === id) {
fn.call(target, event);
}
});
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,
2021-06-11 03:32:34 +03:00
showLoadingMsg,
datetime,
Events,
2021-06-11 20:29:07 +03:00
DirectoryBrowser,
dialogHelper,
itemIdentifier
2020-08-08 15:26:03 +02:00
};
// 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;