Switches in drive mode, after the Design canvas' GridSwitches: Menu → Switches (only when /api/outputs answers), 3×3 tiles per page with the output's name, On / Off / Flashing / level, the key and mode driving it and its LED colour line, a group per module, All off, More ▸, a tap toggles. The status pill above the car (pypilot's note in SidePanel): ● REC while the trip is recorded, estimated / no fix, hidden when all is well
This commit is contained in:
1 parent
82a728e024
commit
88fa2d27a3
5 files changed
+162
-5
No files matched your search
@@ -924,3 +924,99 @@ export function todayMenu(ctx) {
|
||||
ctx.routePanel?.onStopsChanged(() => render());
|
||||
return { el, render, opened: () => { page = 0; render(); } };
|
||||
}
|
||||
|
||||
// Switches (the Design canvas' GridSwitches): can.json's outputs as tiles —
|
||||
// state, mode, the key that drives it and a line in its LED colour; a tap
|
||||
// switches it (POST /api/outputs/<module>/<n>, a strobe stops). Groups are
|
||||
// the modules; nine tiles a page. Polled while open (GET /api/outputs).
|
||||
const LED = { red: "#e8453a", green: "#34b545", blue: "#3b82f6", yellow: "#f0a020", cyan: "#22c3d6", magenta: "#b45bd0", white: "#e8e6df" };
|
||||
const TILES = 9;
|
||||
export function switchesMenu(ctx) {
|
||||
const el = panel("drive-switches", "Switches", `
|
||||
<div class="drive-switches__tiles"></div>
|
||||
<div class="drive-switches__groups"></div>
|
||||
<button type="button" class="drive-switches__off" data-action="off">All off</button>
|
||||
<button type="button" class="drive-switches__page" data-action="page"></button>`);
|
||||
let data = null;
|
||||
let group = null; // a module's name, null = all
|
||||
let page = 0;
|
||||
let timer = null;
|
||||
const outputsOf = (b) => b.outputs || (b.output ? [b.output] : []);
|
||||
const short = (input) => input.slice(input.indexOf(".") + 1).replace(/ press$/, " ⏎");
|
||||
|
||||
function tiles() {
|
||||
return data.modules.filter((m) => !group || m.name === group).flatMap((m) => m.channels.map((c) => {
|
||||
const output = `${m.name}.${c.n}`;
|
||||
const bound = data.bindings.filter((b) => outputsOf(b).includes(output));
|
||||
return { m, c, output, bound };
|
||||
}));
|
||||
}
|
||||
|
||||
function render() {
|
||||
if (el.hidden || !data) return;
|
||||
const all = tiles();
|
||||
const pages = Math.max(1, Math.ceil(all.length / TILES));
|
||||
page = Math.min(page, pages - 1);
|
||||
const on = data.modules.reduce((n, m) => n + m.channels.filter((c) => c.on).length, 0);
|
||||
el.querySelector(".drive-panel__note").textContent = `${on} on${data.modules.every((m) => m.simulated) ? " · simulated" : ""}`;
|
||||
el.querySelector(".drive-switches__tiles").replaceChildren(...all.slice(page * TILES, page * TILES + TILES).map(({ m, c, output, bound }) => {
|
||||
const b = bound[0];
|
||||
const tile = document.createElement("button");
|
||||
tile.type = "button";
|
||||
tile.className = "drive-switches__tile";
|
||||
tile.setAttribute("aria-pressed", String(!!c.on));
|
||||
tile.style.setProperty("--led", LED[b?.led] || (b ? LED.red : "transparent"));
|
||||
const state = c.strobe ? "Flashing" : c.on ? `On${c.level != null ? ` · ${Math.round(c.level)} %` : ""}` : `Off${c.level != null ? ` · ${Math.round(c.level)} %` : ""}`;
|
||||
const mode = bound.map((x) => x.mode || "toggle").filter((x, i, a) => a.indexOf(x) === i).join(" · ");
|
||||
tile.innerHTML = `<span class="drive-switches__top"><span class="drive-switches__icon" aria-hidden="true">⏻</span><span class="drive-switches__key"></span></span>
|
||||
<span class="drive-switches__name"></span><span class="drive-switches__state"></span><span class="drive-switches__mode"></span>`;
|
||||
tile.querySelector(".drive-switches__key").textContent = bound.map((x) => short(x.input)).join(" · ");
|
||||
tile.querySelector(".drive-switches__name").textContent = c.label || output;
|
||||
tile.querySelector(".drive-switches__state").textContent = state;
|
||||
tile.querySelector(".drive-switches__mode").textContent = mode || (c.label ? output : "");
|
||||
tile.title = `${output}${c.label ? ` · ${c.label}` : ""}`;
|
||||
tile.addEventListener("click", () => send(`/api/outputs/${encodeURIComponent(m.name)}/${c.n}`));
|
||||
return tile;
|
||||
}));
|
||||
const groups = data.modules.length > 1 ? [null, ...data.modules.map((m) => m.name)] : [];
|
||||
el.querySelector(".drive-switches__groups").replaceChildren(...groups.map((name) => {
|
||||
const g = document.createElement("button");
|
||||
g.type = "button";
|
||||
g.textContent = name ?? "All";
|
||||
g.setAttribute("aria-pressed", String(group === name));
|
||||
g.addEventListener("click", () => { group = name; page = 0; render(); });
|
||||
return g;
|
||||
}));
|
||||
const pageButton = el.querySelector('[data-action="page"]');
|
||||
pageButton.hidden = pages < 2;
|
||||
pageButton.textContent = `More ▸ ${page + 1}/${pages}`;
|
||||
el.querySelector('[data-action="off"]').disabled = !on;
|
||||
}
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const r = await fetch("/api/outputs", { cache: "no-store" });
|
||||
data = r.ok ? await r.json() : null;
|
||||
} catch { /* offline for a moment: the last state stays */ }
|
||||
render();
|
||||
}
|
||||
async function send(url, body) {
|
||||
await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: body && JSON.stringify(body) }).catch(() => {});
|
||||
load();
|
||||
}
|
||||
|
||||
el.querySelector(".drive-panel__close").addEventListener("click", ctx.close);
|
||||
el.querySelector('[data-action="page"]').addEventListener("click", () => { page++; if (page * TILES >= tiles().length) page = 0; render(); });
|
||||
el.querySelector('[data-action="off"]').addEventListener("click", async () => {
|
||||
const on = data.modules.flatMap((m) => m.channels.filter((c) => c.on).map((c) => `/api/outputs/${encodeURIComponent(m.name)}/${c.n}`));
|
||||
await Promise.all(on.map((url) => fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: '{"on":false}' }).catch(() => {})));
|
||||
load();
|
||||
});
|
||||
return {
|
||||
el, render,
|
||||
// whether this server has outputs at all (the Menu's row shows then)
|
||||
available: () => fetch("/api/outputs", { cache: "no-store" }).then((r) => r.ok).catch(() => false),
|
||||
opened: () => { page = 0; load(); clearInterval(timer); timer = setInterval(load, 1000); },
|
||||
closed: () => clearInterval(timer),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user