// deck-it - the page shell: tabs and polling // Copyright (C) 2026 Danilo M. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as // published by the Free Software Foundation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. // The shell: discover modules, sort them into tabs, mount each panel and poll // its state. A module with a tab is static/modules/.js exporting default // {title, tab, interval, mount(el), update(state, el)}. The Dashboard tab is // static/dashboard.js, which draws the modules without a tab. const tabBar = document.getElementById("tabs"); document.getElementById("reload").onclick = () => location.reload(); const tabBody = document.getElementById("tab-body"); const refreshers = []; const TAB_KEY = "deckit.tab"; const tabs = {}; // Tab icons, Font Awesome glyphs in the Nerd Font the server serves; a tab // not listed shows its name. The name also shows next to the active one. const TAB_ICONS = { Dashboard: "\uf0e4", Windows: "\uf2d2", OBS: "\uf03d", Assistant: "\uf086", K1C: "\uf02f" }; function ensureTab(name) { if (tabs[name]) return tabs[name]; const button = document.createElement("button"); button.dataset.tab = name; button.setAttribute("aria-label", name); button.title = name; button.innerHTML = ``; button.firstElementChild.textContent = TAB_ICONS[name] ?? ""; button.lastElementChild.textContent = name; if (!TAB_ICONS[name]) button.classList.add("named"); button.onclick = () => showTab(name); tabBar.append(button); const section = document.createElement("section"); section.className = "tab-panel"; section.dataset.tab = name; section.hidden = true; tabBody.append(section); tabs[name] = { button, section }; return tabs[name]; } const TAB_ORDER = ["Dashboard", "Windows", "OBS"]; function sortTabs() { const rank = (name) => { const at = TAB_ORDER.indexOf(name); return at === -1 ? TAB_ORDER.length : at; }; for (const name of Object.keys(tabs).sort((a, b) => rank(a) - rank(b))) { tabBar.append(tabs[name].button); tabBody.append(tabs[name].section); } } function showTab(name) { for (const [n, tab] of Object.entries(tabs)) { tab.section.hidden = n !== name; tab.button.classList.toggle("active", n === name); } try { localStorage.setItem(TAB_KEY, name); } catch {} } const dashboard = ensureTab("Dashboard"); dashboard.section.hidden = false; dashboard.button.classList.add("active"); // Polls /api/ every `seconds` and hands the state to update. The first // poll is not awaited: a slow source (the build VM over ssh, a printer that // is off) must not hold back the others. function poll(name, seconds, update) { const tick = async () => { try { const r = await fetch(`/api/${name}`); if (r.headers.get("X-Deckit-Sleep")) return goodnight("The PC is asleep"); awake(); if (r.ok) update(await r.json()); } catch { // keep the last state; the next tick retries } }; refreshers.push(tick); tick(); setInterval(tick, seconds * 1000); } function mountTab(name, panel, section) { const wrap = document.createElement("div"); wrap.className = "tab-content"; section.append(wrap); panel.mount?.(wrap); poll(name, panel.interval ?? 1, (st) => panel.update?.(st, wrap)); } async function boot() { const r = await fetch("/api/modules"); if (!r.ok) return; // Every panel's code at once; then mount in the server's order, so the // tabs do not shuffle with whichever import lands first. const names = (await r.json()).map(({ name }) => name); const loaded = await Promise.all(names.map((name) => import(`./modules/${name}.js`).then((m) => m.default, () => null))); names.forEach((name, i) => { const panel = loaded[i]; if (!panel?.tab) return; // no tab: drawn by the Dashboard, if at all try { mountTab(name, panel, ensureTab(panel.tab).section); } catch { // a broken panel must not take down the rest of the page } }); try { const { default: dash } = await import("./dashboard.js"); dash.mount(dashboard.section, names, { poll, open: showTab }); } catch { // the other tabs still work } sortTabs(); const saved = localStorage.getItem(TAB_KEY); showTab(tabs[saved] ? saved : "Dashboard"); } function stepTab(dir) { const names = [...tabBar.children].map((button) => button.dataset.tab); const at = names.findIndex((name) => !tabs[name]?.section.hidden); const next = at + dir; if (at !== -1 && next >= 0 && next < names.length) showTab(names[next]); } // Swipe the content sideways to change tab, from anywhere but sliders and // text fields, whose sideways drags are theirs. // #tab-body is `touch-action: pan-y`: otherwise the browser claims the moving // finger as a pan and sends pointercancel, which is why swipes never worked. // A swipe that started on a button must not also click it. let swipe = null; let swallowClick = false; tabBody.addEventListener("pointerdown", (e) => { if (e.pointerType === "mouse" && e.buttons !== 1) return; if (e.target.closest("input, select, textarea")) { swipe = null; return; } swipe = { x: e.clientX, y: e.clientY }; }); tabBody.addEventListener("pointerup", (e) => { if (!swipe) return; const dx = e.clientX - swipe.x; const dy = e.clientY - swipe.y; swipe = null; if (Math.abs(dx) < 70 || Math.abs(dx) < Math.abs(dy) * 1.5) return; swallowClick = true; setTimeout(() => { swallowClick = false; }, 0); stepTab(dx < 0 ? 1 : -1); }); tabBody.addEventListener("click", (e) => { if (swallowClick) { e.stopPropagation(); e.preventDefault(); } }, true); tabBody.addEventListener("pointercancel", () => { swipe = null; }); // theme.css is rendered per request from UDT's live files, so re-reading it // carries a scheme, wallpaper or font-size change to an open page. The