From 034a7a2fdb19890f42be5eace9410756344d1708 Mon Sep 17 00:00:00 2001 From: James Lyne Date: Thu, 27 May 2021 01:41:58 +0100 Subject: [PATCH] Split up Util.ts --- src/App.vue | 3 +- src/main.ts | 3 +- src/util.ts | 175 +-------------------------------------------- src/util/config.ts | 115 +++++++++++++++++++++++++++++ src/util/splash.ts | 57 +++++++++++++++ 5 files changed, 177 insertions(+), 176 deletions(-) create mode 100644 src/util/config.ts create mode 100644 src/util/splash.ts diff --git a/src/App.vue b/src/App.vue index 2e22f54..8907a99 100644 --- a/src/App.vue +++ b/src/App.vue @@ -28,7 +28,8 @@ import Sidebar from './components/Sidebar.vue'; import ChatBox from './components/ChatBox.vue'; import {useStore} from "@/store"; import {ActionTypes} from "@/store/action-types"; -import {hideSplash, parseUrl, showSplash, showSplashError} from '@/util'; +import {parseUrl} from '@/util'; +import {hideSplash, showSplash, showSplashError} from '@/util/splash'; import {MutationTypes} from "@/store/mutation-types"; import {LiveAtlasServerDefinition} from "@/index"; diff --git a/src/main.ts b/src/main.ts index fd09aec..6d0e488 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,7 +24,8 @@ import '@/scss/style.scss'; import 'focus-visible'; import {MutationTypes} from "@/store/mutation-types"; -import {showSplashError, validateConfiguration} from "@/util"; +import {validateConfiguration} from "@/util/config"; +import {showSplashError} from "@/util/splash"; import { VueClipboard } from '@soerenmartius/vue3-clipboard'; import Notifications from '@kyvg/vue3-notification' diff --git a/src/util.ts b/src/util.ts index d4453fd..784be00 100644 --- a/src/util.ts +++ b/src/util.ts @@ -15,10 +15,8 @@ */ import API from '@/api'; -import {DynmapPlayer, DynmapUrlConfig, DynmapWorld, DynmapWorldMap} from "@/dynmap"; +import {DynmapPlayer, DynmapWorld, DynmapWorldMap} from "@/dynmap"; import {useStore} from "@/store"; -import {LiveAtlasDynmapServerDefinition, LiveAtlasServerDefinition} from "@/index"; -import ConfigurationError from "@/errors/ConfigurationError"; interface HeadQueueEntry { cacheKey: string; @@ -206,119 +204,6 @@ export const parseMapSearchParams = (query: URLSearchParams) => { } } - -const validateLiveAtlasConfiguration = (config: any): Map => { - const check = '\nCheck your server configuration in index.html is correct.', - result = new Map(); - - if (!Object.keys(config).length) { - throw new ConfigurationError(`No servers defined. ${check}`); - } - - for (const server in config) { - if (!Object.hasOwnProperty.call(config, server)) { - continue; - } - - const serverConfig = config[server]; - - if (!serverConfig || serverConfig.constructor !== Object || !Object.keys(serverConfig).length) { - throw new ConfigurationError(`Server '${server}': Configuration missing. ${check}`); - } - - serverConfig.id = server; - serverConfig.type = serverConfig.type || 'dynmap'; - - switch(serverConfig.type) { - case 'dynmap': - if (!serverConfig.dynmap || serverConfig.dynmap.constructor !== Object) { - throw new ConfigurationError(`Server '${server}': Dynmap configuration object missing. ${check}`); - } - - if (!serverConfig.dynmap.configuration) { - throw new ConfigurationError(`Server '${server}': Dynmap configuration URL missing. ${check}`); - } - - if (!serverConfig.dynmap.update) { - throw new ConfigurationError(`Server '${server}': Dynmap update URL missing. ${check}`); - } - - if (!serverConfig.dynmap.markers) { - throw new ConfigurationError(`Server '${server}': Dynmap markers URL missing. ${check}`); - } - - if (!serverConfig.dynmap.tiles) { - throw new ConfigurationError(`Server '${server}': Dynmap tiles URL missing. ${check}`); - } - - if (!serverConfig.dynmap.sendmessage) { - throw new ConfigurationError(`Server '${server}': Dynmap sendmessage URL missing. ${check}`); - } - break; - - case 'pl3xmap': - case 'plexmap': - if (!serverConfig.plexmap || serverConfig.plexmap.constructor !== Object) { - throw new ConfigurationError(`Server '${server}': Pl3xmap configuration object missing. ${check}`); - } - } - - result.set(server, serverConfig); - } - - return result; -}; - -const validateDynmapConfiguration = (config: DynmapUrlConfig): Map => { - const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.'; - - if (!config) { - throw new ConfigurationError(`Dynmap configuration is missing. ${check}`); - } - - if (!config.configuration) { - throw new ConfigurationError(`Dynmap configuration URL is missing. ${check}`); - } - - if (!config.update) { - throw new ConfigurationError(`Dynmap update URL is missing. ${check}`); - } - - if (!config.markers) { - throw new ConfigurationError(`Dynmap markers URL is missing. ${check}`); - } - - if (!config.tiles) { - throw new ConfigurationError(`Dynmap tiles URL is missing. ${check}`); - } - - if (!config.sendmessage) { - throw new ConfigurationError(`Dynmap sendmessage URL is missing. ${check}`); - } - - const result = new Map(); - result.set('dynmap', { - id: 'dynmap', - label: 'dynmap', - type: 'dynmap', - dynmap: config - }); - - return result; -}; - -export const validateConfiguration = (): Map => { - if (!window.liveAtlasConfig) { - throw new ConfigurationError(`Configuration object is missing`); - } - - if (typeof window.liveAtlasConfig.servers !== 'undefined') { - return validateLiveAtlasConfiguration(window.liveAtlasConfig.servers || {}); - } - - return validateDynmapConfiguration(window.config?.url || null); -}; - export const getAPI = () => { const store = useStore(); @@ -344,61 +229,3 @@ export const getUrlForLocation = (world: DynmapWorld, map: DynmapWorldMap, locat return `#${world.name};${map.name};${locationString};${zoom}`; } - -const app = document.getElementById('app'), - splash = document.getElementById('splash'), - splashSpinner = document.getElementById('splash__spinner'), - splashError = document.getElementById('splash__error'), - splashErrorMessage = document.getElementById('splash__error-message'), - splashRetry = document.getElementById('splash__error-retry'); - -export const showSplash = function() { - if(!splash || !app) { - return; - } - - splash.hidden = false; - app.setAttribute('aria-hidden', 'true'); - - requestAnimationFrame(function() { - splash.style.opacity = '1'; - }); -}; - -export const hideSplash = () => { - if(!splash || !app) { - return; - } - - requestAnimationFrame(() => { - splash.style.opacity = '0'; - app.removeAttribute('aria-hidden'); - - if(window.getComputedStyle(splash).opacity === '0') { - splash.hidden = true; - } - }); -}; - -export const showSplashError = (message: string, fatal: boolean, attempts?: number) => { - if(splashError) { - splashError.setAttribute('aria-hidden', 'false'); - } - - if(splashErrorMessage) { - splashErrorMessage.innerText = message || 'Unknown error'; - } - - if(splashSpinner && fatal) { - splashSpinner.style.visibility = 'hidden'; - } - - if(splashRetry) { - if(fatal) { - splashRetry.hidden = true; - } else if(attempts) { - splashRetry.hidden = false; - splashRetry.textContent = `Retrying... (${attempts.toString()})`; - } - } -}; \ No newline at end of file diff --git a/src/util/config.ts b/src/util/config.ts new file mode 100644 index 0000000..10e2a2c --- /dev/null +++ b/src/util/config.ts @@ -0,0 +1,115 @@ +import {LiveAtlasDynmapServerDefinition, LiveAtlasServerDefinition} from "@/index"; +import ConfigurationError from "@/errors/ConfigurationError"; +import {DynmapUrlConfig} from "@/dynmap"; + +const validateLiveAtlasConfiguration = (config: any): Map => { + const check = '\nCheck your server configuration in index.html is correct.', + result = new Map(); + + if (!Object.keys(config).length) { + throw new ConfigurationError(`No servers defined. ${check}`); + } + + for (const server in config) { + if (!Object.hasOwnProperty.call(config, server)) { + continue; + } + + const serverConfig = config[server]; + + if (!serverConfig || serverConfig.constructor !== Object || !Object.keys(serverConfig).length) { + throw new ConfigurationError(`Server '${server}': Configuration missing. ${check}`); + } + + serverConfig.id = server; + serverConfig.type = serverConfig.type || 'dynmap'; + + switch(serverConfig.type) { + case 'dynmap': + if (!serverConfig.dynmap || serverConfig.dynmap.constructor !== Object) { + throw new ConfigurationError(`Server '${server}': Dynmap configuration object missing. ${check}`); + } + + if (!serverConfig.dynmap.configuration) { + throw new ConfigurationError(`Server '${server}': Dynmap configuration URL missing. ${check}`); + } + + if (!serverConfig.dynmap.update) { + throw new ConfigurationError(`Server '${server}': Dynmap update URL missing. ${check}`); + } + + if (!serverConfig.dynmap.markers) { + throw new ConfigurationError(`Server '${server}': Dynmap markers URL missing. ${check}`); + } + + if (!serverConfig.dynmap.tiles) { + throw new ConfigurationError(`Server '${server}': Dynmap tiles URL missing. ${check}`); + } + + if (!serverConfig.dynmap.sendmessage) { + throw new ConfigurationError(`Server '${server}': Dynmap sendmessage URL missing. ${check}`); + } + break; + + case 'pl3xmap': + case 'plexmap': + if (!serverConfig.plexmap || serverConfig.plexmap.constructor !== Object) { + throw new ConfigurationError(`Server '${server}': Pl3xmap configuration object missing. ${check}`); + } + } + + result.set(server, serverConfig); + } + + return result; +}; + +const validateDynmapConfiguration = (config: DynmapUrlConfig): Map => { + const check = '\nCheck your standalone/config.js file exists and is being loaded correctly.'; + + if (!config) { + throw new ConfigurationError(`Dynmap configuration is missing. ${check}`); + } + + if (!config.configuration) { + throw new ConfigurationError(`Dynmap configuration URL is missing. ${check}`); + } + + if (!config.update) { + throw new ConfigurationError(`Dynmap update URL is missing. ${check}`); + } + + if (!config.markers) { + throw new ConfigurationError(`Dynmap markers URL is missing. ${check}`); + } + + if (!config.tiles) { + throw new ConfigurationError(`Dynmap tiles URL is missing. ${check}`); + } + + if (!config.sendmessage) { + throw new ConfigurationError(`Dynmap sendmessage URL is missing. ${check}`); + } + + const result = new Map(); + result.set('dynmap', { + id: 'dynmap', + label: 'dynmap', + type: 'dynmap', + dynmap: config + }); + + return result; +}; + +export const validateConfiguration = (): Map => { + if (!window.liveAtlasConfig) { + throw new ConfigurationError(`Configuration object is missing`); + } + + if (typeof window.liveAtlasConfig.servers !== 'undefined') { + return validateLiveAtlasConfiguration(window.liveAtlasConfig.servers || {}); + } + + return validateDynmapConfiguration(window.config?.url || null); +}; diff --git a/src/util/splash.ts b/src/util/splash.ts new file mode 100644 index 0000000..7531825 --- /dev/null +++ b/src/util/splash.ts @@ -0,0 +1,57 @@ +const app = document.getElementById('app'), + splash = document.getElementById('splash'), + splashSpinner = document.getElementById('splash__spinner'), + splashError = document.getElementById('splash__error'), + splashErrorMessage = document.getElementById('splash__error-message'), + splashRetry = document.getElementById('splash__error-retry'); + +export const showSplash = function() { + if(!splash || !app) { + return; + } + + splash.hidden = false; + app.setAttribute('aria-hidden', 'true'); + + requestAnimationFrame(function() { + splash.style.opacity = '1'; + }); +}; + +export const hideSplash = () => { + if(!splash || !app) { + return; + } + + requestAnimationFrame(() => { + splash.style.opacity = '0'; + app.removeAttribute('aria-hidden'); + + if(window.getComputedStyle(splash).opacity === '0') { + splash.hidden = true; + } + }); +}; + +export const showSplashError = (message: string, fatal: boolean, attempts?: number) => { + if(splashError) { + splashError.setAttribute('aria-hidden', 'false'); + } + + if(splashErrorMessage) { + splashErrorMessage.innerText = message || 'Unknown error'; + } + + if(splashSpinner && fatal) { + splashSpinner.style.visibility = 'hidden'; + } + + if(splashRetry) { + if(fatal) { + splashRetry.hidden = true; + } else if(attempts) { + splashRetry.hidden = false; + splashRetry.textContent = `Retrying... (${attempts.toString()})`; + } + } +}; \ No newline at end of file