From a6072fa465e066992a939669daf10e5603bb231d Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sun, 5 Jan 2025 14:41:11 +0100 Subject: [PATCH] access: Minor JS improvements --- www/res/access-multiple.js | 26 ++++++++------------------ www/res/access-single.js | 8 +++----- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/www/res/access-multiple.js b/www/res/access-multiple.js index 0617a0f..d2791cf 100644 --- a/www/res/access-multiple.js +++ b/www/res/access-multiple.js @@ -1,5 +1,7 @@ "use strict"; +window.CLIENTS = window.CLIENTS || []; + function getCredentialsUsername(client) { return window.localStorage.getItem(`${CLIENT}/${client}/username`); } @@ -11,7 +13,7 @@ function getCredentialsPassword(client) { function getBasicAuth(client) { return { 'Authorization': 'Basic ' + btoa(getCredentialsUsername(client) + ':' + getCredentialsPassword(client)), - } + }; } async function _get(client, path) { @@ -128,15 +130,9 @@ function render() { const nav = document.getElementsByTagName("nav")[0].getElementsByTagName("ul")[0]; for (const li of nav.children) li.className = ''; - let client = null; - for (const id in CLIENTS) { - if (hash.startsWith(`#/${id}/`) || hash === `#/${id}`) { - client = id; - } - } - - if (client === null) { - window.location.hash = `#/${Object.keys(CLIENTS)[0]}`; + const client = Object.keys(CLIENTS).find(id => hash.startsWith(`#/${id}/`) || hash === `#/${id}`); + if (client === undefined) { + window.location.hash = `#/${Object.keys(CLIENTS).find(id => !!getCredentialsUsername(id) && !!getCredentialsPassword(id)) || Object.keys(CLIENTS)[0]}`; return; } nav.children[Object.keys(CLIENTS).indexOf(client)].className = 'active'; @@ -168,14 +164,8 @@ function render() { function update() { const hash = window.location.hash; - let client = null; - for (const id in CLIENTS) { - if (hash.startsWith(`#/${id}/`) || hash === `#/${id}`) { - client = id; - } - } - - if (document.hidden) { + const client = Object.keys(CLIENTS).find(id => hash.startsWith(`#/${id}/`) || hash === `#/${id}`); + if (document.hidden || client === undefined) { // do nothing } else { if (hash === `#/${client}`) { diff --git a/www/res/access-single.js b/www/res/access-single.js index 17ea3d8..5a79c62 100644 --- a/www/res/access-single.js +++ b/www/res/access-single.js @@ -1,5 +1,7 @@ "use strict"; +window.ELWIG_API = window.ELWIG_API || null; + function getCredentialsUsername() { return window.localStorage.getItem(`${CLIENT}/username`); } @@ -11,7 +13,7 @@ function getCredentialsPassword() { function getBasicAuth() { return { 'Authorization': 'Basic ' + btoa(getCredentialsUsername() + ':' + getCredentialsPassword()), - } + }; } async function _get(path) { @@ -311,10 +313,6 @@ window.addEventListener('hashchange', () => { render(); }); -window.addEventListener('pageshow', update) - -document.addEventListener('visibilitychange', update); - function actionLogin(form) { window.localStorage.setItem(`${CLIENT}/username`, form.username.value); window.localStorage.setItem(`${CLIENT}/password`, form.password.value);