From 81c6dc6907f94ab7e438c8c17e8d6a13db481473 Mon Sep 17 00:00:00 2001 From: MrTimscampi Date: Sat, 8 Aug 2020 15:26:03 +0200 Subject: [PATCH] Move Dashboard to a new module --- package.json | 1 + src/components/appRouter.js | 8 -- src/scripts/clientUtils.js | 228 ++++++++++++++++++++++++++++++++ src/scripts/site.js | 254 ++++++------------------------------ 4 files changed, 266 insertions(+), 225 deletions(-) create mode 100644 src/scripts/clientUtils.js diff --git a/package.json b/package.json index bd2605372..7326dfa1f 100644 --- a/package.json +++ b/package.json @@ -288,6 +288,7 @@ "src/scripts/alphanumericshortcuts.js", "src/scripts/autoBackdrops.js", "src/scripts/browser.js", + "src/scripts/clientUtils.js", "src/scripts/datetime.js", "src/scripts/deleteHelper.js", "src/scripts/dfnshelper.js", diff --git a/src/components/appRouter.js b/src/components/appRouter.js index e7b697daf..9be762fc8 100644 --- a/src/components/appRouter.js +++ b/src/components/appRouter.js @@ -485,13 +485,6 @@ define(['loading', 'globalize', 'events', 'viewManager', 'skinManager', 'backdro return (page.len || 0) > 0; } - function showDirect(path) { - return new Promise(function(resolve, reject) { - resolveOnNextShow = resolve; - page.show(baseUrl() + path); - }); - } - function show(path, options) { if (path.indexOf('/') !== 0 && path.indexOf('://') === -1) { path = '/' + path; @@ -625,7 +618,6 @@ define(['loading', 'globalize', 'events', 'viewManager', 'skinManager', 'backdro appRouter.param = param; appRouter.back = back; appRouter.show = show; - appRouter.showDirect = showDirect; appRouter.start = start; appRouter.baseUrl = baseUrl; appRouter.canGoBack = canGoBack; diff --git a/src/scripts/clientUtils.js b/src/scripts/clientUtils.js new file mode 100644 index 000000000..b576f0504 --- /dev/null +++ b/src/scripts/clientUtils.js @@ -0,0 +1,228 @@ +export function getCurrentUser() { + return window.ApiClient.getCurrentUser(false); +} + +//TODO: investigate url prefix support for serverAddress function +export function serverAddress() { + if (AppInfo.isNativeApp) { + var apiClient = window.ApiClient; + + if (apiClient) { + return apiClient.serverAddress(); + } + + return null; + } + + var urlLower = window.location.href.toLowerCase(); + var index = urlLower.lastIndexOf('/web'); + + if (index != -1) { + return urlLower.substring(0, index); + } + + var loc = window.location; + var address = loc.protocol + '//' + loc.hostname; + + if (loc.port) { + address += ':' + loc.port; + } + + return address; +} + +export function getCurrentUserId() { + var apiClient = window.ApiClient; + + if (apiClient) { + return apiClient.getCurrentUserId(); + } + + return null; +} + +export function onServerChanged(userId, accessToken, apiClient) { + apiClient = apiClient || window.ApiClient; + window.ApiClient = apiClient; +} + +export function logout() { + ConnectionManager.logout().then(function () { + var loginPage; + + if (AppInfo.isNativeApp) { + loginPage = 'selectserver.html'; + window.ApiClient = null; + } else { + loginPage = 'login.html'; + } + + navigate(loginPage); + }); +} + +export function getConfigurationPageUrl(name) { + return 'configurationpage?name=' + encodeURIComponent(name); +} + +export function getConfigurationResourceUrl(name) { + if (AppInfo.isNativeApp) { + return ApiClient.getUrl('web/ConfigurationPage', { + name: name + }); + } + + return getConfigurationPageUrl(name); +} + +export function navigate(url, preserveQueryString) { + if (!url) { + throw new Error('url cannot be null or empty'); + } + + var queryString = getWindowLocationSearch(); + + if (preserveQueryString && queryString) { + url += queryString; + } + + return new Promise(function (resolve, reject) { + require(['appRouter'], function (appRouter) { + return appRouter.show(url).then(resolve, reject); + }); + }); +} + +export function processPluginConfigurationUpdateResult() { + require(['loading', 'toast'], function (loading, toast) { + loading.hide(); + toast.default(Globalize.translate('MessageSettingsSaved')); + }); +} + +export function processServerConfigurationUpdateResult(result) { + require(['loading', 'toast'], function (loading, toast) { + loading.hide(); + toast.default(Globalize.translate('MessageSettingsSaved')); + }); +} + +export function processErrorResponse(response) { + require(['loading'], function (loading) { + loading.hide(); + }); + + var status = '' + response.status; + + 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') { + return void require(['toast'], function (toast) { + toast.default({ + text: options + }); + }); + } + + require(['alert'], function (alert) { + alert.default({ + title: options.title || Globalize.translate('HeaderAlert'), + text: options.message + }).then(options.callback || function () {}); + }); +} + +export function capabilities(appHost) { + var capabilities = { + 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'], + SupportsPersistentIdentifier: self.appMode === 'cordova' || self.appMode === 'android', + SupportsMediaControl: true + }; + appHost.getPushTokenInfo(); + return capabilities = Object.assign(capabilities, appHost.getPushTokenInfo()); +} + +export function selectServer() { + if (window.NativeShell && typeof window.NativeShell.selectServer === 'function') { + window.NativeShell.selectServer(); + } else { + navigate('selectserver.html'); + } +} + +export function hideLoadingMsg() { + 'use strict'; + require(['loading'], function(loading) { + loading.hide(); + }); +} + +export function showLoadingMsg() { + 'use strict'; + require(['loading'], function(loading) { + loading.show(); + }); +} + +export function confirm(message, title, callback) { + 'use strict'; + require(['confirm'], function(confirm) { + 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, + getConfigurationPageUrl, + getConfigurationResourceUrl, + getCurrentUser, + getCurrentUserId, + hideLoadingMsg, + logout, + navigate, + onServerChanged, + processErrorResponse, + processPluginConfigurationUpdateResult, + processServerConfigurationUpdateResult, + selectServer, + serverAddress, + showLoadingMsg +}; + +export default { + alert, + capabilities, + confirm, + getConfigurationPageUrl, + getConfigurationResourceUrl, + getCurrentUser, + getCurrentUserId, + hideLoadingMsg, + logout, + navigate, + onServerChanged, + processErrorResponse, + processPluginConfigurationUpdateResult, + processServerConfigurationUpdateResult, + selectServer, + serverAddress, + showLoadingMsg +}; diff --git a/src/scripts/site.js b/src/scripts/site.js index aa74411af..02e24a8b4 100644 --- a/src/scripts/site.js +++ b/src/scripts/site.js @@ -1,4 +1,4 @@ -function getWindowLocationSearch(win) { +window.getWindowLocationSearch = function(win) { 'use strict'; var search = (win || window).location.search; @@ -12,9 +12,9 @@ function getWindowLocationSearch(win) { } return search || ''; -} +}; -window.getParameterByName = function (name, url) { +window.getParameterByName = function(name, url) { 'use strict'; name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); @@ -29,7 +29,7 @@ window.getParameterByName = function (name, url) { return decodeURIComponent(results[1].replace(/\+/g, ' ')); }; -function pageClassOn(eventName, className, fn) { +window.pageClassOn = function(eventName, className, fn) { 'use strict'; document.addEventListener(eventName, function (event) { @@ -39,7 +39,7 @@ function pageClassOn(eventName, className, fn) { fn.call(target, event); } }); -} +}; window.pageIdOn = function(eventName, id, fn) { 'use strict'; @@ -53,187 +53,6 @@ window.pageIdOn = function(eventName, id, fn) { }); }; -var Dashboard = { - getCurrentUser: function () { - return window.ApiClient.getCurrentUser(false); - }, - - //TODO: investigate url prefix support for serverAddress function - serverAddress: function () { - if (AppInfo.isNativeApp) { - var apiClient = window.ApiClient; - - if (apiClient) { - return apiClient.serverAddress(); - } - - return null; - } - - var urlLower = window.location.href.toLowerCase(); - var index = urlLower.lastIndexOf('/web'); - - if (index != -1) { - return urlLower.substring(0, index); - } - - var loc = window.location; - var address = loc.protocol + '//' + loc.hostname; - - if (loc.port) { - address += ':' + loc.port; - } - - return address; - }, - getCurrentUserId: function () { - var apiClient = window.ApiClient; - - if (apiClient) { - return apiClient.getCurrentUserId(); - } - - return null; - }, - onServerChanged: function (userId, accessToken, apiClient) { - apiClient = apiClient || window.ApiClient; - window.ApiClient = apiClient; - }, - logout: function () { - ConnectionManager.logout().then(function () { - var loginPage; - - if (AppInfo.isNativeApp) { - loginPage = 'selectserver.html'; - window.ApiClient = null; - } else { - loginPage = 'login.html'; - } - - Dashboard.navigate(loginPage); - }); - }, - getConfigurationPageUrl: function (name) { - return 'configurationpage?name=' + encodeURIComponent(name); - }, - getConfigurationResourceUrl: function (name) { - if (AppInfo.isNativeApp) { - return ApiClient.getUrl('web/ConfigurationPage', { - name: name - }); - } - - return Dashboard.getConfigurationPageUrl(name); - }, - navigate: function (url, preserveQueryString) { - if (!url) { - throw new Error('url cannot be null or empty'); - } - - var queryString = getWindowLocationSearch(); - - if (preserveQueryString && queryString) { - url += queryString; - } - - return new Promise(function (resolve, reject) { - require(['appRouter'], function (appRouter) { - return appRouter.show(url).then(resolve, reject); - }); - }); - }, - navigate_direct: function (path) { - return new Promise(function (resolve, reject) { - require(['appRouter'], function (appRouter) { - return appRouter.showDirect(path).then(resolve, reject); - }); - }); - }, - processPluginConfigurationUpdateResult: function () { - require(['loading', 'toast'], function (loading, toast) { - loading.hide(); - toast.default(Globalize.translate('MessageSettingsSaved')); - }); - }, - processServerConfigurationUpdateResult: function (result) { - require(['loading', 'toast'], function (loading, toast) { - loading.hide(); - toast.default(Globalize.translate('MessageSettingsSaved')); - }); - }, - processErrorResponse: function (response) { - require(['loading'], function (loading) { - loading.hide(); - }); - - var status = '' + response.status; - - if (response.statusText) { - status = response.statusText; - } - - Dashboard.alert({ - title: status, - message: response.headers ? response.headers.get('X-Application-Error-Code') : null - }); - }, - alert: function (options) { - if (typeof options == 'string') { - return void require(['toast'], function (toast) { - toast.default({ - text: options - }); - }); - } - - require(['alert'], function (alert) { - alert.default({ - title: options.title || Globalize.translate('HeaderAlert'), - text: options.message - }).then(options.callback || function () {}); - }); - }, - capabilities: function (appHost) { - var capabilities = { - 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'], - SupportsPersistentIdentifier: self.appMode === 'cordova' || self.appMode === 'android', - SupportsMediaControl: true - }; - appHost.getPushTokenInfo(); - return capabilities = Object.assign(capabilities, appHost.getPushTokenInfo()); - }, - selectServer: function () { - if (window.NativeShell && typeof window.NativeShell.selectServer === 'function') { - window.NativeShell.selectServer(); - } else { - Dashboard.navigate('selectserver.html'); - } - }, - hideLoadingMsg: function() { - 'use strict'; - require(['loading'], function(loading) { - loading.hide(); - }); - }, - showLoadingMsg: function() { - 'use strict'; - require(['loading'], function(loading) { - loading.show(); - }); - }, - confirm: function(message, title, callback) { - 'use strict'; - require(['confirm'], function(confirm) { - confirm(message, title).then(function() { - callback(!0); - }).catch(function() { - callback(!1); - }); - }); - } -}; - var AppInfo = {}; function initClient() { @@ -289,7 +108,7 @@ function initClient() { if (!AppInfo.isNativeApp) { console.debug('loading ApiClient singleton'); - return require(['apiclient'], function (apiClientFactory) { + return require(['apiclient', 'clientUtils'], function (apiClientFactory, clientUtils) { console.debug('creating ApiClient singleton'); var apiClient = new apiClientFactory(Dashboard.serverAddress(), apphost.appName(), apphost.appVersion(), apphost.deviceName(), apphost.deviceId()); @@ -376,36 +195,12 @@ function initClient() { } } - function initRequireWithBrowser() { - var componentsPath = getComponentsPath(); - var scriptsPath = getScriptsPath(); - - define('filesystem', [scriptsPath + '/filesystem'], returnFirstDependency); - - define('lazyLoader', [componentsPath + '/lazyLoader/lazyLoaderIntersectionObserver'], returnFirstDependency); - define('shell', [scriptsPath + '/shell'], returnFirstDependency); - - define('alert', [componentsPath + '/alert'], returnFirstDependency); - - defineResizeObserver(); - - define('dialog', [componentsPath + '/dialog/dialog'], returnFirstDependency); - - define('confirm', [componentsPath + '/confirm/confirm'], returnFirstDependency); - - define('prompt', [componentsPath + '/prompt/prompt'], returnFirstDependency); - - define('loading', [componentsPath + '/loading/loading'], returnFirstDependency); - define('multi-download', [scriptsPath + '/multiDownload'], returnFirstDependency); - define('fileDownloader', [scriptsPath + '/fileDownloader'], returnFirstDependency); - - define('castSenderApiLoader', [componentsPath + '/castSenderApi'], returnFirstDependency); - } - function init() { define('livetvcss', ['css!assets/css/livetv.css'], returnFirstDependency); define('detailtablecss', ['css!assets/css/detailtable.css'], returnFirstDependency); + require(['clientUtils']); + var promises = []; if (!window.fetch) { promises.push(require(['fetch'])); @@ -605,7 +400,29 @@ function initClient() { } function onWebComponentsReady() { - initRequireWithBrowser(); + var componentsPath = getComponentsPath(); + var scriptsPath = getScriptsPath(); + + define('filesystem', [scriptsPath + '/filesystem'], returnFirstDependency); + + define('lazyLoader', [componentsPath + '/lazyLoader/lazyLoaderIntersectionObserver'], returnFirstDependency); + define('shell', [scriptsPath + '/shell'], returnFirstDependency); + + define('alert', [componentsPath + '/alert'], returnFirstDependency); + + defineResizeObserver(); + + define('dialog', [componentsPath + '/dialog/dialog'], returnFirstDependency); + + define('confirm', [componentsPath + '/confirm/confirm'], returnFirstDependency); + + define('prompt', [componentsPath + '/prompt/prompt'], returnFirstDependency); + + define('loading', [componentsPath + '/loading/loading'], returnFirstDependency); + define('multi-download', [scriptsPath + '/multiDownload'], returnFirstDependency); + define('fileDownloader', [scriptsPath + '/fileDownloader'], returnFirstDependency); + + define('castSenderApiLoader', [componentsPath + '/castSenderApi'], returnFirstDependency); if (self.appMode === 'cordova' || self.appMode === 'android' || self.appMode === 'standalone') { AppInfo.isNativeApp = true; @@ -617,7 +434,7 @@ function initClient() { var localApiClient; let promise; - (function () { + function initRequireJs() { var urlArgs = 'v=' + (window.dashboardVersion || new Date().getDate()); var bowerPath = getBowerPath(); @@ -648,7 +465,8 @@ function initClient() { nowPlayingHelper: componentsPath + '/playback/nowplayinghelper', pluginManager: componentsPath + '/pluginManager', packageManager: componentsPath + '/packageManager', - screensaverManager: componentsPath + '/screensavermanager' + screensaverManager: componentsPath + '/screensavermanager', + clientUtils: scriptsPath + '/clientUtils' }; requirejs.onError = onRequireJsError; @@ -1106,7 +924,9 @@ function initClient() { appRouter.showItem = showItem; return appRouter; }); - })(); + } + + initRequireJs(); promise.then(onWebComponentsReady); }