CI / Build and test (push) Successful in 5m11s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 8m57s
CI / PostgreSQL 16 integration (push) Successful in 3m10s
CI / PostgreSQL 17 integration (push) Successful in 3m30s
CI / Docker image (push) Successful in 49s
Release / Server image (push) Successful in 1m4s
- :latest follows the highest stable release, or the highest pre-release while none is stable (Chronos.Tools release-channels); bad tags are refused before anything is built. - Unraid: CHRONOS_SPACE_PATH and the template's Array view show the array's free space; the write volume (cache) is shown apart; completions check the archive volume (507). - Settings is a gear, docked at the foot of the admin sidebar; About is the last part of Settings on the desktop and the panel. - Import > Folder > Whole folder: one sequence per format (desktop only). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
336 lines
14 KiB
JavaScript
336 lines
14 KiB
JavaScript
// Chronos Server admin panel — vanilla ES modules, no build step (BRIEF §14.8).
|
|
import { api, ApiFailure } from "./api.js";
|
|
import { t, plural, loadStrings, applyStrings, setLanguage, languages, culture } from "./i18n.js";
|
|
import { icons } from "./icons.js";
|
|
import { escapeHtml, attr, errorText, nativeName, formatNumber, fitColumns } from "./ui.js";
|
|
|
|
const app = document.getElementById("app");
|
|
// The menu: its pages from the top, then the ones docked at its foot above the account (owner review 2).
|
|
const topPages = ["dashboard", "users", "archive", "jobs", "logs"];
|
|
const bottomPages = ["settings"];
|
|
const pages = [...topPages, ...bottomPages];
|
|
/** Routes of old bookmarks and where they open now: About is the last group of Settings (owner review 2). */
|
|
const MOVED = { about: "settings?group=about" };
|
|
const THEME_KEY = "chronos.admin.theme";
|
|
const SIDEBAR_KEY = "chronos.admin.sidebar";
|
|
/** The narrow layout of admin.css, where the menu is the top bar and does not collapse. */
|
|
const NARROW = window.matchMedia("(max-width: 900px)");
|
|
|
|
let info = null;
|
|
let events = null;
|
|
const listeners = new Set();
|
|
/** The sidebar choice made on this page, kept here too: a browser that refuses storage still keeps it across pages. */
|
|
let sidebarChoice = null;
|
|
|
|
applyTheme(storedTheme());
|
|
|
|
async function start() {
|
|
await loadStrings();
|
|
info = await api.get("/api/v1/server/info").catch(() => null);
|
|
await api.resume();
|
|
window.addEventListener("hashchange", () => render("title"));
|
|
// The menu's names or its icons alone, as on the desktop (owner review 1). A layout whose B key types another letter
|
|
// (Cyrillic, Greek, Hebrew, Arabic) is matched on the physical key, so the tooltips' Ctrl+B holds in every language. In the
|
|
// narrow layout the menu is the top bar with no toggle: the browser keeps its own Ctrl+B there.
|
|
document.addEventListener("keydown", (e) => {
|
|
if (!(e.ctrlKey || e.metaKey) || e.shiftKey || e.altKey || e.repeat || !app.querySelector(".shell") || NARROW.matches) return;
|
|
const key = e.key.toLowerCase();
|
|
if (key !== "b" && !(e.code === "KeyB" && !/^[a-z]$/.test(key))) return;
|
|
e.preventDefault();
|
|
toggleSidebar();
|
|
});
|
|
window.addEventListener("chronos:signed-out", () => {
|
|
events?.stop();
|
|
events = null;
|
|
render();
|
|
});
|
|
render();
|
|
}
|
|
|
|
function currentPage() {
|
|
let p = location.hash.replace(/^#\/?/, "").split("?")[0];
|
|
if (Object.hasOwn(MOVED, p)) {
|
|
// The address shows the new route; replaceState fires no hashchange.
|
|
history.replaceState(null, "", `#/${MOVED[p]}`);
|
|
p = MOVED[p].split("?")[0];
|
|
}
|
|
return pages.includes(p) ? p : "dashboard";
|
|
}
|
|
|
|
/** Draws the page for the session state; focus names the shell element that gets the keyboard focus. */
|
|
function render(focus = null) {
|
|
const user = api.user();
|
|
if (!user) return renderLogin();
|
|
if (user.mustChangePassword) return renderPasswordChange();
|
|
renderShell(currentPage(), focus);
|
|
}
|
|
|
|
function brand() {
|
|
return `<div class="brand"><img src="favicon.svg" alt=""><div><strong>Chronos Server</strong><span class="num">${escapeHtml(info?.name ?? "")} · v${escapeHtml(info?.version ?? "")}</span></div></div>`;
|
|
}
|
|
|
|
function renderLogin(message = "") {
|
|
document.title = `${t("Admin.Login.SignIn")} · Chronos Server`;
|
|
app.innerHTML = `
|
|
<main class="login">
|
|
<form novalidate>
|
|
${brand()}
|
|
<label><span data-i18n="Admin.Login.User"></span><input class="input" name="username" autocomplete="username" required dir="auto" data-i18n-title="Admin.Login.User.Tip"></label>
|
|
<label><span data-i18n="Admin.Login.Password"></span><input class="input" name="password" type="password" autocomplete="current-password" required data-i18n-title="Admin.Login.Password.Tip"></label>
|
|
<div class="error" role="alert">${escapeHtml(message)}</div>
|
|
<button class="btn primary" type="submit" data-i18n="Admin.Login.SignIn" data-i18n-title="Admin.Login.SignIn.Tip"></button>
|
|
</form>
|
|
</main>`;
|
|
applyStrings(app);
|
|
const form = app.querySelector("form");
|
|
const error = form.querySelector(".error");
|
|
form.addEventListener("submit", async (e) => {
|
|
e.preventDefault();
|
|
error.textContent = "";
|
|
const data = new FormData(form);
|
|
try {
|
|
const r = await api.signIn(String(data.get("username")), String(data.get("password")));
|
|
if (r.user.role !== "admin") {
|
|
await api.signOut();
|
|
error.textContent = t("Admin.Login.NotAdmin");
|
|
return;
|
|
}
|
|
render();
|
|
} catch (err) {
|
|
error.textContent = err instanceof ApiFailure && err.status === 401 ? t("Admin.Login.Failed") : errorText(err);
|
|
}
|
|
});
|
|
form.querySelector("input").focus();
|
|
}
|
|
|
|
function renderPasswordChange() {
|
|
document.title = `${t("Admin.Password.Title")} · Chronos Server`;
|
|
app.innerHTML = `
|
|
<main class="login">
|
|
<form novalidate>
|
|
${brand()}
|
|
<h1 data-i18n="Admin.Password.Title"></h1>
|
|
<label><span data-i18n="Admin.Password.Current"></span><input class="input" name="current" type="password" autocomplete="current-password" required data-i18n-title="Admin.Password.Current.Tip"></label>
|
|
<label><span data-i18n="Admin.Password.New"></span><input class="input" name="next" type="password" autocomplete="new-password" required data-i18n-title="Admin.Password.New.Tip"></label>
|
|
<label><span data-i18n="Admin.Password.Repeat"></span><input class="input" name="repeat" type="password" autocomplete="new-password" required data-i18n-title="Admin.Password.Repeat.Tip"></label>
|
|
<div class="error" role="alert"></div>
|
|
<div class="row">
|
|
<button class="btn" type="button" id="cancel" data-i18n="Admin.SignOut" data-i18n-title="Admin.SignOut.Tip"></button>
|
|
<span class="spacer"></span>
|
|
<button class="btn primary" type="submit" data-i18n="Admin.Password.Save" data-i18n-title="Admin.Password.Save.Tip"></button>
|
|
</div>
|
|
</form>
|
|
</main>`;
|
|
applyStrings(app);
|
|
const form = app.querySelector("form");
|
|
const error = form.querySelector(".error");
|
|
app.querySelector("#cancel").addEventListener("click", async () => {
|
|
await api.signOut();
|
|
render();
|
|
});
|
|
form.addEventListener("submit", async (e) => {
|
|
e.preventDefault();
|
|
const data = new FormData(form);
|
|
if (data.get("next") !== data.get("repeat")) {
|
|
error.textContent = t("Admin.Password.Mismatch");
|
|
return;
|
|
}
|
|
try {
|
|
await api.changePassword(String(data.get("current")), String(data.get("next")));
|
|
render();
|
|
} catch (err) {
|
|
error.textContent = errorText(err);
|
|
}
|
|
});
|
|
form.querySelector("input").focus();
|
|
}
|
|
|
|
function renderShell(page, focus) {
|
|
const links = (group) => group.map((p) => `
|
|
<a href="#/${p}" ${p === page ? 'aria-current="page"' : ""} data-i18n-title="Admin.Nav.${cap(p)}.Tip">${icons[p]}<span data-i18n="Admin.Nav.${cap(p)}"></span>
|
|
<span class="sr-only" id="badge-text-${p}"></span><span class="badge" id="badge-${p}" aria-hidden="true" hidden></span></a>`).join("");
|
|
const user = api.user();
|
|
const options = languages().map((code) => `<option value="${attr(code)}" ${code === culture() ? "selected" : ""}>${escapeHtml(nativeName(code))}</option>`).join("");
|
|
app.innerHTML = `
|
|
<div class="shell">
|
|
<aside class="sidebar">
|
|
<button class="btn icon sidebar-toggle" id="sidebar-toggle">${icons.menu}</button>
|
|
${brand()}
|
|
<nav class="nav"><div class="nav-group">${links(topPages)}</div><div class="nav-group bottom">${links(bottomPages)}</div></nav>
|
|
<div class="user">
|
|
<div class="avatar" aria-hidden="true">${escapeHtml(user.username.slice(0, 1).toUpperCase())}</div>
|
|
<div class="who"><strong class="ellipsis" dir="auto" title="${attr(user.username)}">${escapeHtml(user.username)}</strong><span data-i18n="Admin.Role.Admin"></span></div>
|
|
<button class="btn icon" id="signout" data-i18n-title="Admin.SignOut.Tip">${icons.power}</button>
|
|
</div>
|
|
</aside>
|
|
<div class="main">
|
|
<header class="header">
|
|
<h1 id="title" tabindex="-1" data-i18n="Admin.Nav.${cap(page)}"></h1>
|
|
<span class="meta" id="meta"></span>
|
|
<span class="spacer"></span>
|
|
<span class="actions" id="actions"></span>
|
|
<select class="input language" id="language" data-i18n-title="Admin.Language.Tip">${options}</select>
|
|
<button class="btn icon" id="theme" aria-pressed="${isDark()}" data-i18n-title="Admin.Theme.Tip">${icons.moon}</button>
|
|
</header>
|
|
<main class="content" id="content"></main>
|
|
</div>
|
|
</div>`;
|
|
applyStrings(app);
|
|
applySidebar(sidebarCollapsed());
|
|
app.querySelector("#sidebar-toggle").addEventListener("click", toggleSidebar);
|
|
document.title = `${t(`Admin.Nav.${cap(page)}`)} · Chronos Server`;
|
|
app.querySelector("#signout").addEventListener("click", async () => {
|
|
events?.stop();
|
|
events = null;
|
|
await api.signOut();
|
|
render();
|
|
});
|
|
app.querySelector("#language").addEventListener("change", async (e) => {
|
|
await setLanguage(e.target.value);
|
|
render("language");
|
|
});
|
|
app.querySelector("#theme").addEventListener("click", (e) => {
|
|
const theme = isDark() ? "light" : "dark";
|
|
applyTheme(theme);
|
|
try {
|
|
localStorage.setItem(THEME_KEY, theme);
|
|
} catch {
|
|
// Only this page keeps the theme then.
|
|
}
|
|
e.currentTarget.setAttribute("aria-pressed", String(theme === "dark"));
|
|
});
|
|
if (focus) app.querySelector(`#${focus}`)?.focus();
|
|
|
|
events ??= api.events((e) => listeners.forEach((fn) => fn(e)));
|
|
listeners.clear();
|
|
pages.forEach(showBadge);
|
|
updateBadges();
|
|
listeners.add((e) => {
|
|
if (e.type === "jobState") updateBadges();
|
|
});
|
|
|
|
const meta = app.querySelector("#meta");
|
|
const content = app.querySelector("#content");
|
|
const context = {
|
|
api,
|
|
info,
|
|
/** Header text after the title (one line, full text in the tooltip); paths and file names stay left to right. */
|
|
setMeta(text, { ltr = false } = {}) {
|
|
meta.textContent = text;
|
|
meta.title = text;
|
|
meta.dir = ltr ? "ltr" : "auto";
|
|
},
|
|
actions: app.querySelector("#actions"),
|
|
onEvent: (fn) => listeners.add(fn),
|
|
};
|
|
import(`./pages/${page}.js`)
|
|
.then((m) => m.render(content, context))
|
|
.catch((err) => {
|
|
content.innerHTML = `<div class="panel empty"><span>${escapeHtml(errorText(err))}</span></div>`;
|
|
});
|
|
}
|
|
|
|
/** The menu shows icons alone: their tooltips then name the page, and the choice is kept in this browser. */
|
|
function applySidebar(collapsed) {
|
|
const shell = app.querySelector(".shell");
|
|
if (!shell) return;
|
|
shell.classList.toggle("collapsed", collapsed);
|
|
const toggle = shell.querySelector("#sidebar-toggle");
|
|
const tip = t(collapsed ? "Admin.Nav.Expand.Tip" : "Admin.Nav.Collapse.Tip");
|
|
toggle.title = tip;
|
|
toggle.setAttribute("aria-label", tip);
|
|
toggle.setAttribute("aria-expanded", String(!collapsed));
|
|
shell.querySelectorAll(".nav a").forEach((a) => {
|
|
const page = a.getAttribute("href").replace("#/", "");
|
|
const tip = t(`Admin.Nav.${cap(page)}.Tip`);
|
|
a.title = collapsed ? t("Admin.Nav.Collapsed", t(`Admin.Nav.${cap(page)}`), tip) : tip;
|
|
// Screen readers already have the name (the hidden label): their description stays the tip alone.
|
|
if (collapsed) a.setAttribute("aria-description", tip);
|
|
else a.removeAttribute("aria-description");
|
|
});
|
|
}
|
|
|
|
function sidebarCollapsed() {
|
|
if (sidebarChoice !== null) return sidebarChoice;
|
|
try {
|
|
return localStorage.getItem(SIDEBAR_KEY) === "collapsed";
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function toggleSidebar() {
|
|
const collapsed = !app.querySelector(".shell")?.classList.contains("collapsed");
|
|
sidebarChoice = collapsed;
|
|
applySidebar(collapsed);
|
|
// The page's width changed by 200 px without a window resize: the tables fit their columns again.
|
|
document.querySelectorAll(".table[data-levels]").forEach(fitColumns);
|
|
try {
|
|
localStorage.setItem(SIDEBAR_KEY, collapsed ? "collapsed" : "expanded");
|
|
} catch {
|
|
// Only this page keeps the choice then (sidebarChoice).
|
|
}
|
|
}
|
|
|
|
const badgeCounts = new Map();
|
|
let badgeTimer = null;
|
|
let badgeAgain = false;
|
|
|
|
/** Running jobs and accounts as nav badges: fetched at most every two seconds (then once more if asked meanwhile). */
|
|
function updateBadges() {
|
|
if (badgeTimer) {
|
|
badgeAgain = true;
|
|
return;
|
|
}
|
|
badgeTimer = setTimeout(() => {
|
|
badgeTimer = null;
|
|
if (badgeAgain) {
|
|
badgeAgain = false;
|
|
updateBadges();
|
|
}
|
|
}, 2000);
|
|
api.get("/api/v1/jobs?state=running&limit=100").then((page) => setBadge("jobs", page.items.length)).catch(() => {});
|
|
api.get("/api/v1/users").then((users) => setBadge("users", users.length)).catch(() => {});
|
|
}
|
|
|
|
function setBadge(page, count) {
|
|
badgeCounts.set(page, count);
|
|
showBadge(page);
|
|
}
|
|
|
|
/** The last known count on the page's nav link: a pill for the eye, a phrase for screen readers. */
|
|
function showBadge(page) {
|
|
const badge = document.getElementById(`badge-${page}`);
|
|
const count = badgeCounts.get(page);
|
|
if (!badge || count === undefined) return;
|
|
badge.hidden = count === 0;
|
|
badge.textContent = formatNumber(count);
|
|
badge.classList.toggle("quiet", page !== "jobs");
|
|
document.getElementById(`badge-text-${page}`).textContent =
|
|
count === 0 ? "" : plural(page === "jobs" ? "Admin.Jobs.RunningCount" : "Admin.Users.Accounts", count);
|
|
}
|
|
|
|
function storedTheme() {
|
|
try {
|
|
return localStorage.getItem(THEME_KEY);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** Light or dark as chosen in this browser; without a choice the operating system's setting applies (admin.css). */
|
|
function applyTheme(theme) {
|
|
if (theme === "light" || theme === "dark") document.documentElement.dataset.theme = theme;
|
|
}
|
|
|
|
function isDark() {
|
|
const chosen = document.documentElement.dataset.theme;
|
|
return chosen ? chosen === "dark" : matchMedia("(prefers-color-scheme: dark)").matches;
|
|
}
|
|
|
|
function cap(s) {
|
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
}
|
|
|
|
start();
|