2020-09-10 23:20:55 +09:00
|
|
|
import * as webSettings from 'webSettings';
|
2020-08-14 05:48:59 +02:00
|
|
|
|
2020-08-08 15:26:03 +02:00
|
|
|
export function getCurrentUser() {
|
|
|
|
return window.ApiClient.getCurrentUser(false);
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:20:55 +09:00
|
|
|
// TODO: investigate url prefix support for serverAddress function
|
|
|
|
export async function serverAddress() {
|
|
|
|
const apiClient = window.ApiClient;
|
2020-08-08 15:26:03 +02:00
|
|
|
|
2020-09-10 23:20:55 +09:00
|
|
|
if (apiClient) {
|
|
|
|
return Promise.resolve(apiClient.serverAddress());
|
2020-08-08 15:26:03 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 09:12:40 +09:00
|
|
|
const current = await window.connectionManager.getAvailableServers().then(servers => {
|
2020-09-10 23:20:55 +09:00
|
|
|
if (servers.length !== 0) {
|
|
|
|
return Promise.resolve(servers[0].ManualAddress);
|
|
|
|
}
|
|
|
|
});
|
2020-08-08 15:26:03 +02:00
|
|
|
|
2020-09-10 23:20:55 +09:00
|
|
|
if (current) return Promise.resolve(current);
|
2020-08-08 15:26:03 +02:00
|
|
|
|
2020-09-12 09:12:40 +09:00
|
|
|
const urls = [];
|
|
|
|
urls.push(window.location.origin);
|
|
|
|
urls.push(`${window.location.protocol}//${window.location.hostname}:8096`);
|
2020-10-30 22:14:45 +09:00
|
|
|
urls.push(...await webSettings.getServers());
|
2020-08-08 15:26:03 +02:00
|
|
|
|
2020-09-12 09:12:40 +09:00
|
|
|
const promises = urls.map(url => {
|
|
|
|
return fetch(`${url}/System/Info/Public`).catch(error => {
|
2020-09-10 23:20:55 +09:00
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
});
|
2020-08-08 15:26:03 +02:00
|
|
|
|
2020-09-10 23:20:55 +09:00
|
|
|
return Promise.all(promises).then(responses => {
|
|
|
|
return responses.find(response => response && response.ok);
|
2020-09-12 09:12:40 +09:00
|
|
|
}).then(response => response.url.replace('/System/Info/Public', '')).catch(error => {
|
2020-09-10 23:20:55 +09:00
|
|
|
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) {
|
|
|
|
apiClient = apiClient || window.ApiClient;
|
|
|
|
window.ApiClient = apiClient;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logout() {
|
2020-08-30 06:06:47 +02:00
|
|
|
window.connectionManager.logout().then(function () {
|
2020-09-10 23:20:55 +09:00
|
|
|
webSettings.getMultiServer().then(multi => {
|
|
|
|
multi ? navigate('selectserver.html') : navigate('login.html');
|
|
|
|
});
|
2020-08-08 15:26:03 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-02 20:50:32 +09:00
|
|
|
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 new Promise(function (resolve, reject) {
|
2020-08-14 05:48:59 +02:00
|
|
|
import('appRouter').then(({default: appRouter}) => {
|
2020-08-08 15:26:03 +02:00
|
|
|
return appRouter.show(url).then(resolve, reject);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function processPluginConfigurationUpdateResult() {
|
2020-08-14 05:48:59 +02:00
|
|
|
Promise.all([
|
|
|
|
import('loading'),
|
|
|
|
import('toast')
|
|
|
|
])
|
|
|
|
.then(([{default: loading}, {default: toast}]) => {
|
|
|
|
loading.hide();
|
2020-08-17 20:42:26 +09:00
|
|
|
toast(Globalize.translate('SettingsSaved'));
|
2020-08-14 05:48:59 +02:00
|
|
|
});
|
2020-08-08 15:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function processServerConfigurationUpdateResult(result) {
|
2020-08-14 05:48:59 +02:00
|
|
|
Promise.all([
|
|
|
|
import('loading'),
|
|
|
|
import('toast')
|
|
|
|
])
|
|
|
|
.then(([{default: loading}, {default: toast}]) => {
|
|
|
|
loading.hide();
|
2020-08-17 20:42:26 +09:00
|
|
|
toast(Globalize.translate('SettingsSaved'));
|
2020-08-14 05:48:59 +02:00
|
|
|
});
|
2020-08-08 15:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function processErrorResponse(response) {
|
2020-08-14 05:48:59 +02:00
|
|
|
import('loading').then(({default: loading}) => {
|
2020-08-08 15:26:03 +02:00
|
|
|
loading.hide();
|
|
|
|
});
|
|
|
|
|
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') {
|
2020-08-14 05:48:59 +02:00
|
|
|
return void import('toast').then(({default: toast}) => {
|
2020-08-14 17:10:27 +02:00
|
|
|
toast({
|
2020-08-08 15:26:03 +02:00
|
|
|
text: options
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-14 05:48:59 +02:00
|
|
|
import('alert').then(({default: alert}) => {
|
2020-08-14 17:10:13 +02:00
|
|
|
alert({
|
2020-08-08 15:26:03 +02:00
|
|
|
title: options.title || Globalize.translate('HeaderAlert'),
|
|
|
|
text: options.message
|
|
|
|
}).then(options.callback || function () {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function capabilities(appHost) {
|
2020-09-12 05:46:15 +09:00
|
|
|
const capabilities = {
|
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
|
|
|
|
};
|
2020-08-17 04:26:38 +02:00
|
|
|
return Object.assign(capabilities, 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() {
|
2020-08-14 05:48:59 +02:00
|
|
|
import('loading').then(({default: loading}) => {
|
2020-08-08 15:26:03 +02:00
|
|
|
loading.hide();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function showLoadingMsg() {
|
2020-08-14 05:48:59 +02:00
|
|
|
import('loading').then(({default: loading}) => {
|
2020-08-08 15:26:03 +02:00
|
|
|
loading.show();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function confirm(message, title, callback) {
|
2020-08-14 05:48:59 +02:00
|
|
|
import('confirm').then(({default: confirm}) => {
|
2020-08-08 15:26:03 +02:00
|
|
|
confirm(message, title).then(function() {
|
|
|
|
callback(!0);
|
|
|
|
}).catch(function() {
|
|
|
|
callback(!1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is used in plugins and templates, so keep it defined for now.
|
|
|
|
// TODO: Remove once plugins don't need it
|
|
|
|
window.Dashboard = {
|
|
|
|
alert,
|
|
|
|
capabilities,
|
|
|
|
confirm,
|
2020-09-02 20:50:32 +09:00
|
|
|
getPluginUrl,
|
2020-08-08 15:26:03 +02:00
|
|
|
getCurrentUser,
|
|
|
|
getCurrentUserId,
|
|
|
|
hideLoadingMsg,
|
|
|
|
logout,
|
|
|
|
navigate,
|
|
|
|
onServerChanged,
|
|
|
|
processErrorResponse,
|
|
|
|
processPluginConfigurationUpdateResult,
|
|
|
|
processServerConfigurationUpdateResult,
|
|
|
|
selectServer,
|
|
|
|
serverAddress,
|
|
|
|
showLoadingMsg
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
alert,
|
|
|
|
capabilities,
|
|
|
|
confirm,
|
2020-09-02 20:50:32 +09:00
|
|
|
getPluginUrl,
|
2020-08-08 15:26:03 +02:00
|
|
|
getCurrentUser,
|
|
|
|
getCurrentUserId,
|
|
|
|
hideLoadingMsg,
|
|
|
|
logout,
|
|
|
|
navigate,
|
|
|
|
onServerChanged,
|
|
|
|
processErrorResponse,
|
|
|
|
processPluginConfigurationUpdateResult,
|
|
|
|
processServerConfigurationUpdateResult,
|
|
|
|
selectServer,
|
|
|
|
serverAddress,
|
|
|
|
showLoadingMsg
|
|
|
|
};
|