Files
receipt_maker/web/app.js
T
adamfandClaude Opus 5.5 ba92f1fbda
CI / test (3.10) (push) Successful in 1m20s
CI / test (3.12) (push) Successful in 1m49s
CI / test (3.13) (push) Successful in 1m5s
Add scheduled and event-driven printing with a new web UI
The web server now runs a scheduler over schedules kept in SQLite
(~/.local/share/receipt-maker, outside the clone). A schedule is a
trigger, a receipt made of blocks, and options:

- Triggers: once, recurring (rrule-based, DST-safe), sun times,
  reminders before calendar (ICS) events, RSS/JSON watchers, webhooks
  with per-schedule secrets.
- Blocks, rendered fresh as one job: templated notes ({date},
  {days_until:..}, rotating {choice}, trigger vars), QR, saved images,
  random image, calendar agenda, feed headlines, dividers.
- Rules: quiet hours (hold/skip/ignore), daily cap, pause, catch-up
  window for missed runs (never replayed), retries on connect failure
  only; a mid-job drop (new PrinterDropped) is never resent.

receipt.py renders through composable Blocks (render_note/image/qr are
wrappers, output unchanged). Print history persists, with Print again.
The page moves to web/ with Print (note/image/QR + paper preview),
Schedules (presets, editor with live preview and next runs), History
and Settings tabs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-23 23:58:26 +01:00

55 lines
1.9 KiB
JavaScript

// Hash router and the printer status pill.
import {get} from "./api.js";
import {$} from "./dom.js";
import {editorView} from "./editor.js";
import {historyView} from "./history.js";
import {printView} from "./print.js";
import {schedulesView} from "./schedules.js";
import {settingsView} from "./settings.js";
const view = $("#view");
let current = null; // what the active view returned: a cleanup function or {canLeave}
let currentHash = location.hash;
async function route() {
const hash = location.hash || "#print";
if (current?.canLeave && hash !== currentHash && !current.canLeave()) {
history.replaceState(null, "", currentHash); // stay put
return;
}
if (typeof current === "function") current();
current = null;
currentHash = hash;
const [tab, arg] = hash.slice(1).split("/");
document.querySelectorAll("nav.tabs a").forEach((a) =>
a.classList.toggle("on", a.getAttribute("href") === `#${tab}`));
view.setAttribute("aria-busy", "true");
if (tab === "schedules" && arg) current = await editorView(view, arg);
else if (tab === "schedules") current = await schedulesView(view);
else if (tab === "history") current = await historyView(view, arg);
else if (tab === "settings") current = await settingsView(view);
else current = printView(view);
view.removeAttribute("aria-busy");
window.scrollTo(0, 0);
}
window.addEventListener("hashchange", route);
window.addEventListener("beforeunload", (e) => {
if (current?.isDirty?.()) e.preventDefault();
});
async function checkStatus() {
const pill = $("#status");
pill.textContent = "checking…";
pill.className = "pill";
const r = await get("api/status");
pill.textContent = r.body.busy ? "printer busy" : r.ok ? "printer ready" : "printer offline";
pill.className = `pill ${r.ok ? "ok" : "err"}`;
pill.title = r.text;
}
$("#status").addEventListener("click", checkStatus);
checkStatus();
route();