// Dezio dashboard: the pages. The controls live in controls.js. "use strict"; const state = { me: null, page: "me", sections: [], spy: null, turn: 0 }; // ---------------------------------------------------------------------------- helpers function toast(text, bad) { const el = $("toast"); el.textContent = text; el.classList.toggle("bad", Boolean(bad)); el.classList.add("show"); clearTimeout(toast.timer); toast.timer = setTimeout(() => el.classList.remove("show"), 3500); } // The site may live under a path such as /dezio; the server puts it on . const BASE = document.documentElement.dataset.base || ""; async function api(method, path, body) { const options = { method, credentials: "same-origin", headers: {} }; if (body !== undefined) { options.headers["Content-Type"] = "application/json"; options.headers["X-CSRF-Token"] = state.me ? state.me.csrf : ""; options.body = JSON.stringify(body); } const response = await fetch(BASE + path, options); let data = {}; try { data = await response.json(); } catch (e) { /* empty answer */ } if (!response.ok) throw new Error(data.error || `Something went wrong (${response.status})`); return data; } const dirty = () => state.sections.filter((s) => s.collect && JSON.stringify(s.collect()) !== s.saved); function refreshSaveBar() { const count = dirty().length; $("savebar").classList.toggle("show", count > 0); if (count) $("save-text").textContent = count === 1 ? "1 section changed" : `${count} sections changed`; } async function saveAll() { const go = $("save-go"); go.disabled = true; go.textContent = "Saving"; let failed = 0; for (const s of dirty()) { const body = s.collect(); s.status.textContent = ""; s.el.classList.remove("failed"); try { const result = await api("PATCH", s.path, body); s.saved = JSON.stringify(body); if (result.notes && result.notes[0]) toast(result.notes[0]); if (s.done) s.done(result); } catch (e) { failed += 1; s.status.textContent = e.message; s.el.classList.add("failed"); s.el.scrollIntoView({ behavior: "smooth", block: "center" }); } } go.disabled = false; go.textContent = "Save changes"; refreshSaveBar(); if (!failed) toast("Saved"); } // One card on a page. With `collect` and `path` its changes go through the save bar. function section({ id, title, icn, hint, meta, body, collect, path, done, extra }) { const status = h("p", { class: "status err", role: "alert" }); const el = h("section", { class: "section", id: `s-${id}` }, h("header", { class: "section-head" }, h("span", { class: "section-ic" }, icon(icn)), h("div", {}, h("h2", {}, title), hint ? h("p", { class: "hint" }, hint) : null), meta || null), body, status, extra || null); const entry = { id, title, el, status, collect, path, done }; if (collect) entry.saved = JSON.stringify(collect()); state.sections.push(entry); const watch = () => refreshSaveBar(); el.addEventListener("input", watch); el.addEventListener("change", watch); return el; } function field(label, input, note) { // A wrapping