// The dashboard's controls: faders, switches, segments, searchable pickers, the colour picker. Plain JS, no frameworks. // Every control fires a bubbling "input" event when it changes, so a section can notice edits in one place. // Text goes in through textContent (never innerHTML), because server, channel and role names come from users. "use strict"; const SVG = "http://www.w3.org/2000/svg"; function h(tag, attrs, ...children) { const el = document.createElement(tag); for (const [key, value] of Object.entries(attrs || {})) { if (value === undefined || value === null || value === false) continue; if (key === "class") el.className = value; else if (key.startsWith("on")) el.addEventListener(key.slice(2), value); else if (key === "value") el.value = value; else if (key === "checked") el.checked = Boolean(value); else el.setAttribute(key, value === true ? "" : value); } for (const child of children.flat()) { if (child === null || child === undefined || child === false) continue; el.append(child instanceof Node ? child : document.createTextNode(String(child))); } return el; } const $ = (id) => document.getElementById(id); // Line icons: lucide's shapes, 24 grid, 1.5 stroke. "M…" is a path; a [cx, cy, r] triple is a circle. const ICONS = { user: ["M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2", [12, 7, 4]], hash: ["M4 9h16", "M4 15h16", "M10 3 8 21", "M16 3l-2 18"], check: ["M20 6 9 17l-5-5"], chevron: ["m6 9 6 6 6-6"], search: [[11, 11, 8], "m21 21-4.3-4.3"], x: ["M18 6 6 18", "m6 6 12 12"], plus: ["M5 12h14", "M12 5v14"], trash: ["M3 6h18", "M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6", "M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"], logout: ["M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4", "m16 17 5-5-5-5", "M21 12H9"], login: ["M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4", "m10 17 5-5-5-5", "M15 12H3"], music: ["M9 18V5l12-2v13", [6, 18, 3], [18, 16, 3]], download: ["M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4", "m7 10 5 5 5-5", "M12 15V3"], moon: ["M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"], cloud: ["M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"], shield: ["M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1Z"], message: ["M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2Z"], sliders: ["M4 21v-7", "M4 10V3", "M12 21v-9", "M12 8V3", "M20 21v-5", "M20 12V3", "M1 14h6", "M9 8h6", "M17 16h6"], list: ["M8 6h13", "M8 12h13", "M8 18h13", "M3 6h.01", "M3 12h.01", "M3 18h.01"], bot: ["M12 8V4H8", "M6 8h12a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2Z", "M2 14h2", "M20 14h2", "M15 13v2", "M9 13v2"], tag: ["M12.586 2.586A2 2 0 0 0 11.172 2H4a2 2 0 0 0-2 2v7.172a2 2 0 0 0 .586 1.414l8.704 8.704a2.426 2.426 0 0 0 3.42 0l6.58-6.58a2.426 2.426 0 0 0 0-3.42Z", [7.5, 7.5, .5]], reset: ["M3 12a9 9 0 1 0 3-6.7L3 8", "M3 3v5h5"], send: ["M14.5 21.7a.5.5 0 0 0 .9 0l6.5-19a.5.5 0 0 0-.6-.6l-19 6.5a.5.5 0 0 0 0 .9l7.9 3.2a2 2 0 0 1 1.1 1.1Z", "m21.9 2.1-10.9 10.9"], pipette: ["m2 22 1-1h3l9-9", "M3 21v-3l9-9", "m15 6 3.4-3.4a2.1 2.1 0 1 1 3 3L18 9l.4.4a2.1 2.1 0 1 1-3 3l-3.8-3.8a2.1 2.1 0 1 1 3-3l.4.4Z"], }; function icon(name) { const svg = document.createElementNS(SVG, "svg"); svg.setAttribute("viewBox", "0 0 24 24"); svg.setAttribute("class", "ic"); svg.setAttribute("aria-hidden", "true"); for (const part of ICONS[name] || []) { const shape = document.createElementNS(SVG, Array.isArray(part) ? "circle" : "path"); if (Array.isArray(part)) ["cx", "cy", "r"].forEach((a, i) => shape.setAttribute(a, part[i])); else shape.setAttribute("d", part); svg.append(shape); } return svg; } const changed = (el) => el.dispatchEvent(new Event("input", { bubbles: true })); const clamp = (n, lo, hi) => Math.min(hi, Math.max(lo, n)); // A number that rolls to its new value instead of jumping. function tween(el, to, format) { const from = Number(el.dataset.shown ?? to); el.dataset.shown = to; const show = format || ((n) => String(Math.round(n))); if (from === to || matchMedia("(prefers-reduced-motion: reduce)").matches) { el.textContent = show(to); return; } cancelAnimationFrame(el.tweening); const start = performance.now(); const step = (now) => { const t = Math.min(1, (now - start) / 320); el.textContent = show(from + (to - from) * (1 - (1 - t) ** 3)); if (t < 1) el.tweening = requestAnimationFrame(step); }; el.tweening = requestAnimationFrame(step); } // ---------------------------------------------------------------------------- fader // A slider with the logo's diamond as its thumb. Drag it, click anywhere, click a tick, or use the keyboard. // `ticks` is [[value, "label"], …]. `meter` adds a row of level lights above the track. function fader({ label, min = 0, max = 100, step = 1, value, format, ticks = [], meter = 0 }) { const show = format || ((v) => String(v)); const readout = h("output", { class: "fader-value" }); const fill = h("span", { class: "fader-fill" }); const thumb = h("span", { class: "fader-thumb" }); const track = h("div", { class: "fader-track", role: "slider", tabindex: 0, "aria-label": label, "aria-valuemin": min, "aria-valuemax": max }, fill, thumb); const lights = Array.from({ length: meter }, (_, i) => h("span", { class: "led " + (i < meter * .6 ? "low" : i < meter * .85 ? "mid" : "high") })); const wrap = h("div", { class: "fader" }, h("div", { class: "fader-head" }, h("span", {}, label), readout), meter ? h("div", { class: "leds", "aria-hidden": "true" }, lights) : null, track); let current = value; const paint = (v) => { const p = (v - min) / (max - min); wrap.style.setProperty("--p", p); track.setAttribute("aria-valuenow", v); track.setAttribute("aria-valuetext", show(v)); readout.textContent = show(v); lights.forEach((led, i) => led.classList.toggle("on", meter * p > i + .5 || (p > 0 && i === 0))); }; const set = (v, quiet) => { v = clamp(Math.round(v / step) * step, min, max); if (v === current) return; current = v; paint(v); if (!quiet) changed(wrap); }; const fromPointer = (event) => { const box = track.getBoundingClientRect(); set(min + clamp((event.clientX - box.left) / box.width, 0, 1) * (max - min)); }; track.addEventListener("pointerdown", (event) => { track.setPointerCapture(event.pointerId); fromPointer(event); requestAnimationFrame(() => wrap.classList.add("held")); }); track.addEventListener("pointermove", (event) => { if (wrap.classList.contains("held")) fromPointer(event); }); const release = () => wrap.classList.remove("held"); track.addEventListener("pointerup", release); track.addEventListener("pointercancel", release); track.addEventListener("keydown", (event) => { const big = Math.max(step, (max - min) / 10); const next = { ArrowRight: current + step, ArrowUp: current + step, ArrowLeft: current - step, ArrowDown: current - step, PageUp: current + big, PageDown: current - big, Home: min, End: max }[event.key]; if (next === undefined) return; event.preventDefault(); set(next); }); if (ticks.length) { wrap.append(h("div", { class: "fader-ticks" }, ticks.map(([v, text]) => { const tick = h("button", { type: "button", class: "tick", tabindex: -1, onclick: () => set(v) }, text); tick.style.setProperty("--at", (v - min) / (max - min)); return tick; }))); } paint(current); Object.defineProperty(wrap, "value", { get: () => current, set: (v) => set(v, true) }); return wrap; } // ---------------------------------------------------------------------------- switch function switchCtl(label, checked, note) { const input = h("input", { type: "checkbox", role: "switch", checked }); const el = h("label", { class: "switch" }, input, h("span", { class: "switch-track" }, h("span", { class: "switch-knob" })), h("span", { class: "switch-text" }, label, note ? h("small", {}, note) : null)); el.input = input; return el; } // ---------------------------------------------------------------------------- segmented control // Real radio buttons, so the keyboard works; the lit plate slides under the chosen one. let segId = 0; function segmented(options, value, aria) { const name = `seg-${++segId}`; const plate = h("span", { class: "seg-plate" }); const wrap = h("div", { class: "seg", role: "radiogroup", "aria-label": aria }, plate); wrap.style.setProperty("--n", options.length); const move = () => { const index = [...wrap.querySelectorAll("input")].findIndex((i) => i.checked); wrap.style.setProperty("--i", Math.max(0, index)); plate.classList.toggle("hidden", index < 0); }; for (const [val, text] of options) { wrap.append(h("label", {}, h("input", { type: "radio", name, value: val, checked: val === value, onchange: move }), h("span", {}, text))); } move(); wrap.value = () => (wrap.querySelector("input:checked") || {}).value; wrap.set = (val) => { for (const i of wrap.querySelectorAll("input")) i.checked = i.value === val; move(); }; return wrap; } // ---------------------------------------------------------------------------- popovers let openPop = null; function closePop() { if (!openPop) return; const { root, done } = openPop; openPop = null; root.classList.remove("open"); if (done) done(); } document.addEventListener("pointerdown", (event) => { if (openPop && !openPop.root.contains(event.target)) closePop(); }); document.addEventListener("keydown", (event) => { if (event.key !== "Escape" || !openPop) return; const back = openPop.trigger; closePop(); if (back) back.focus(); }); function showPop(root, trigger, panel, done) { closePop(); openPop = { root, trigger, done }; const below = window.innerHeight - trigger.getBoundingClientRect().bottom; root.classList.toggle("up", below < 340 && trigger.getBoundingClientRect().top > below); root.classList.remove("end"); root.classList.add("open"); const box = root.querySelector(":scope > .pop").getBoundingClientRect(); root.classList.toggle("end", box.right > document.documentElement.clientWidth - 8); panel.scrollTop = 0; } // ---------------------------------------------------------------------------- list popover // The search box and option list behind both pickers. `items()` gives { value, label, group, hint, icon, colour, disabled }. function listPop({ items, isOn, choose, allowNew, newLabel, searchLabel }) { const search = h("input", { type: "text", class: "pop-search", placeholder: searchLabel || "Search", "aria-label": searchLabel || "Search", autocomplete: "off", spellcheck: "false" }); const list = h("div", { class: "pop-list", role: "listbox" }); const panel = h("div", { class: "pop" }, h("div", { class: "pop-head" }, icon("search"), search), list); let shown = [], at = -1; const mark = (i) => { at = i; shown.forEach((row, n) => row.classList.toggle("hl", n === at)); if (shown[at]) shown[at].scrollIntoView({ block: "nearest" }); }; const draw = () => { const q = search.value.trim().toLowerCase(); const rows = []; let group; const all = items(); for (const item of all) { if (q && !item.label.toLowerCase().includes(q) && !(item.group || "").toLowerCase().includes(q)) continue; if (item.group !== undefined && item.group !== group) { group = item.group; if (group) rows.push(h("div", { class: "pop-group" }, group)); } const row = h("button", { type: "button", role: "option", class: "pop-item", disabled: item.disabled, title: item.title, "aria-selected": isOn(item) ? "true" : "false", onmousedown: (event) => event.preventDefault(), onclick: () => choose(item) }); if (item.colour !== undefined) { const dot = h("span", { class: "dot" }); if (item.colour) dot.style.backgroundColor = item.colour; row.append(dot); } else if (item.icon) row.append(icon(item.icon)); row.append(h("span", { class: "pop-label" }, item.label)); if (item.hint) row.append(h("small", {}, item.hint)); row.append(h("span", { class: "pop-on" })); rows.push(row); } const typed = search.value.trim(); if (allowNew && typed && !all.some((i) => i.label.toLowerCase() === typed.toLowerCase())) { rows.push(h("button", { type: "button", class: "pop-item", onmousedown: (event) => event.preventDefault(), onclick: () => choose({ value: typed, label: typed, fresh: true }) }, icon("plus"), h("span", { class: "pop-label" }, newLabel ? newLabel(typed) : `Use ${typed}`))); } if (!rows.length) rows.push(h("div", { class: "pop-empty" }, "No matches")); list.replaceChildren(...rows); shown = rows.filter((r) => r.tagName === "BUTTON" && !r.disabled); mark(q && shown.length ? 0 : -1); }; search.addEventListener("input", (event) => { event.stopPropagation(); draw(); }); search.addEventListener("keydown", (event) => { if (event.key === "ArrowDown" || event.key === "ArrowUp") { event.preventDefault(); if (shown.length) mark((at + (event.key === "ArrowDown" ? 1 : shown.length - 1 + (at < 0 ? 1 : 0))) % shown.length); } else if (event.key === "Enter") { event.preventDefault(); if (shown[at]) shown[at].click(); } }); return { panel, search, draw }; } // One choice from a list. The value lives here, so it can be read with `.value` like a