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";
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}`) {

View File

@ -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);