access: Minor JS improvements

This commit is contained in:
2025-01-05 14:41:11 +01:00
parent c69d9c11ec
commit a6072fa465
2 changed files with 11 additions and 23 deletions

View File

@ -1,5 +1,7 @@
"use strict"; "use strict";
window.CLIENTS = window.CLIENTS || [];
function getCredentialsUsername(client) { function getCredentialsUsername(client) {
return window.localStorage.getItem(`${CLIENT}/${client}/username`); return window.localStorage.getItem(`${CLIENT}/${client}/username`);
} }
@ -11,7 +13,7 @@ function getCredentialsPassword(client) {
function getBasicAuth(client) { function getBasicAuth(client) {
return { return {
'Authorization': 'Basic ' + btoa(getCredentialsUsername(client) + ':' + getCredentialsPassword(client)), 'Authorization': 'Basic ' + btoa(getCredentialsUsername(client) + ':' + getCredentialsPassword(client)),
} };
} }
async function _get(client, path) { async function _get(client, path) {
@ -128,15 +130,9 @@ function render() {
const nav = document.getElementsByTagName("nav")[0].getElementsByTagName("ul")[0]; const nav = document.getElementsByTagName("nav")[0].getElementsByTagName("ul")[0];
for (const li of nav.children) li.className = ''; for (const li of nav.children) li.className = '';
let client = null; const client = Object.keys(CLIENTS).find(id => hash.startsWith(`#/${id}/`) || hash === `#/${id}`);
for (const id in CLIENTS) { if (client === undefined) {
if (hash.startsWith(`#/${id}/`) || hash === `#/${id}`) { window.location.hash = `#/${Object.keys(CLIENTS).find(id => !!getCredentialsUsername(id) && !!getCredentialsPassword(id)) || Object.keys(CLIENTS)[0]}`;
client = id;
}
}
if (client === null) {
window.location.hash = `#/${Object.keys(CLIENTS)[0]}`;
return; return;
} }
nav.children[Object.keys(CLIENTS).indexOf(client)].className = 'active'; nav.children[Object.keys(CLIENTS).indexOf(client)].className = 'active';
@ -168,14 +164,8 @@ function render() {
function update() { function update() {
const hash = window.location.hash; const hash = window.location.hash;
let client = null; const client = Object.keys(CLIENTS).find(id => hash.startsWith(`#/${id}/`) || hash === `#/${id}`);
for (const id in CLIENTS) { if (document.hidden || client === undefined) {
if (hash.startsWith(`#/${id}/`) || hash === `#/${id}`) {
client = id;
}
}
if (document.hidden) {
// do nothing // do nothing
} else { } else {
if (hash === `#/${client}`) { if (hash === `#/${client}`) {

View File

@ -1,5 +1,7 @@
"use strict"; "use strict";
window.ELWIG_API = window.ELWIG_API || null;
function getCredentialsUsername() { function getCredentialsUsername() {
return window.localStorage.getItem(`${CLIENT}/username`); return window.localStorage.getItem(`${CLIENT}/username`);
} }
@ -11,7 +13,7 @@ function getCredentialsPassword() {
function getBasicAuth() { function getBasicAuth() {
return { return {
'Authorization': 'Basic ' + btoa(getCredentialsUsername() + ':' + getCredentialsPassword()), 'Authorization': 'Basic ' + btoa(getCredentialsUsername() + ':' + getCredentialsPassword()),
} };
} }
async function _get(path) { async function _get(path) {
@ -311,10 +313,6 @@ window.addEventListener('hashchange', () => {
render(); render();
}); });
window.addEventListener('pageshow', update)
document.addEventListener('visibilitychange', update);
function actionLogin(form) { function actionLogin(form) {
window.localStorage.setItem(`${CLIENT}/username`, form.username.value); window.localStorage.setItem(`${CLIENT}/username`, form.username.value);
window.localStorage.setItem(`${CLIENT}/password`, form.password.value); window.localStorage.setItem(`${CLIENT}/password`, form.password.value);