Dezio/web/dashboard/static/controls.js
astar76 dfb4f1b7e0 Dashboard and website redesign; role menus keep servers' own roles
Dashboard: faders, searchable channel and role pickers, a colour picker,
segmented controls, one save bar, a live embed preview. Website: a split
hero with a working player preview. Fonts are self-hosted for the CSP.

Bot: publishing a role menu no longer marks roles it didn't create as its
own, so removing one can't delete a server's role. Zero ids no longer panic
D!ban, announcement channels are pickable, and static files revalidate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-06 19:32:57 -06:00

462 lines
23 KiB
JavaScript

// 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 <select>.
function pick({ options, value, empty, label, allowNew, newLabel, searchLabel, leading }) {
const text = h("span", { class: "pick-text" });
const lead = h("span", { class: "pick-lead" });
const trigger = h("button", { type: "button", class: "pick-btn", "aria-haspopup": "listbox", "aria-expanded": "false", "aria-label": label }, lead, text, icon("chevron"));
const root = h("div", { class: "pick" }, trigger);
const emptyItem = empty ? [{ value: "", label: empty, group: "" }] : [];
let current = value ?? "";
const items = () => [...emptyItem, ...options];
const paint = () => {
const item = items().find((i) => i.value === current) || (current ? { label: current } : null);
text.textContent = item ? item.label : (empty || "Choose");
trigger.classList.toggle("unset", !current);
lead.replaceChildren();
if (item && item.colour) { const dot = h("span", { class: "dot" }); dot.style.backgroundColor = item.colour; lead.append(dot); }
else if (leading && current) lead.append(icon(leading));
};
const pop = listPop({ items, isOn: (i) => i.value === current, allowNew, newLabel, searchLabel, choose: (item) => {
closePop();
trigger.focus();
if (item.value === current) return;
current = item.value;
paint();
changed(root);
} });
root.append(pop.panel);
const open = () => {
pop.search.value = "";
pop.draw();
trigger.setAttribute("aria-expanded", "true");
showPop(root, trigger, pop.panel.querySelector(".pop-list"), () => trigger.setAttribute("aria-expanded", "false"));
pop.search.focus({ preventScroll: true });
};
trigger.addEventListener("click", () => (root.classList.contains("open") ? closePop() : open()));
trigger.addEventListener("keydown", (event) => { if (event.key === "ArrowDown") { event.preventDefault(); open(); } });
paint();
Object.defineProperty(root, "value", { get: () => current, set: (v) => { current = v ?? ""; paint(); } });
return root;
}
function channelOptions(channels) {
return channels.map((c) => ({ value: c.id, label: c.name, group: c.category || "", icon: "hash", hint: c.can_send ? null : "Dezio can't post here" }));
}
function channelPick(channels, value, empty, label) {
return pick({ options: channelOptions(channels), value, empty: empty || "None", label, leading: "hash", searchLabel: "Search channels" });
}
// Several choices, shown as chips. `allowNew` lets people type a role that doesn't exist yet.
function multiPick({ options, values, label, addLabel, allowNew, newLabel }) {
const chosen = new Set(values);
const known = [...options];
const chips = h("div", { class: "chips" });
const add = h("button", { type: "button", class: "chip-add", "aria-haspopup": "listbox" }, icon("plus"), addLabel || "Add role");
const root = h("div", { class: "pick multi", role: "group", "aria-label": label }, chips);
const paint = () => {
chips.replaceChildren(...known.filter((o) => chosen.has(o.value)).map((o) => {
const dot = o.colour !== undefined ? h("span", { class: "dot" }) : null;
if (dot && o.colour) dot.style.backgroundColor = o.colour;
return h("span", { class: "chip" }, dot, o.label, h("button", { type: "button", class: "chip-x", "aria-label": `Remove ${o.label}`, onclick: () => { chosen.delete(o.value); paint(); changed(root); } }, icon("x")));
}), add);
};
const pop = listPop({ items: () => known, isOn: (o) => chosen.has(o.value), allowNew, newLabel, searchLabel: "Search roles", choose: (item) => {
if (item.fresh) known.push(item);
if (chosen.has(item.value)) chosen.delete(item.value); else chosen.add(item.value);
paint();
changed(root);
pop.search.value = "";
pop.draw();
pop.search.focus({ preventScroll: true });
} });
root.append(pop.panel);
add.addEventListener("click", () => {
if (root.classList.contains("open")) return closePop();
pop.search.value = "";
pop.draw();
showPop(root, add, pop.panel.querySelector(".pop-list"));
pop.search.focus({ preventScroll: true });
});
paint();
root.values = () => known.filter((o) => chosen.has(o.value)).map((o) => o.value);
return root;
}
// ---------------------------------------------------------------------------- colour picker
function hsvToHex(h6, s, v) {
const f = (n) => { const k = (n + h6 / 60) % 6; return Math.round(255 * (v - v * s * clamp(Math.min(k, 4 - k), 0, 1))).toString(16).padStart(2, "0"); };
return `#${f(5)}${f(3)}${f(1)}`;
}
function hexToHsv(hex) {
const [r, g, b] = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255);
const max = Math.max(r, g, b), d = max - Math.min(r, g, b);
let hue = 0;
if (d) hue = 60 * (max === r ? ((g - b) / d + 6) % 6 : max === g ? (b - r) / d + 2 : (r - g) / d + 4);
return { h: hue, s: max ? d / max : 0, v: max };
}
const SWATCHES = ["#000000", "#ffffff", "#8a8a8a", "#b657d0", "#6f52d6", "#2549c9", "#23a55a", "#f0b232", "#ed4245", "#eb459e"];
function colourPick(value, label) {
let current = value || "#000000";
let hsv = hexToHsv(current);
const swatch = h("span", { class: "cp-swatch" });
const code = h("span", { class: "cp-code" });
const trigger = h("button", { type: "button", class: "cp-btn", "aria-label": label }, swatch, code);
const dot = h("span", { class: "cp-dot" });
const sv = h("div", { class: "cp-sv", tabindex: 0, role: "slider", "aria-label": `${label}: shade` }, dot);
const hueThumb = h("span", { class: "cp-hue-thumb" });
const hue = h("div", { class: "cp-hue", tabindex: 0, role: "slider", "aria-label": `${label}: hue`, "aria-valuemin": 0, "aria-valuemax": 360 }, hueThumb);
const hex = h("input", { type: "text", class: "cp-hex", maxlength: 7, spellcheck: "false", "aria-label": `${label}: hex value` });
const tools = h("div", { class: "cp-row" }, hex);
if (window.EyeDropper) {
tools.append(h("button", { type: "button", class: "btn ghost cp-drop", "aria-label": "Pick a colour from the screen", onclick: async () => {
try { const { sRGBHex } = await new EyeDropper().open(); take(sRGBHex.slice(0, 7), true); } catch (e) { /* cancelled */ }
} }, icon("pipette")));
}
const panel = h("div", { class: "pop cp-pop" }, sv, hue, tools,
h("div", { class: "cp-swatches" }, SWATCHES.map((c) => { const b = h("button", { type: "button", "aria-label": c, onclick: () => take(c, true) }); b.style.backgroundColor = c; return b; })));
const root = h("div", { class: "pick cp" }, trigger, panel);
const paint = (typing) => {
swatch.style.backgroundColor = current;
code.textContent = current;
if (!typing) hex.value = current;
sv.style.backgroundColor = `hsl(${hsv.h} 100% 50%)`;
dot.style.setProperty("--x", hsv.s);
dot.style.setProperty("--y", 1 - hsv.v);
dot.style.backgroundColor = current;
hueThumb.style.setProperty("--x", hsv.h / 360);
hue.setAttribute("aria-valuenow", Math.round(hsv.h));
};
const take = (hexValue, fromSwatch, typing) => {
if (fromSwatch) hsv = hexToHsv(hexValue);
if (hexValue === current && !fromSwatch) return paint(typing);
current = hexValue;
paint(typing);
changed(root);
};
const commit = () => take(hsvToHex(hsv.h, hsv.s, hsv.v));
const dragger = (el, onMove) => {
let held = false;
el.addEventListener("pointerdown", (event) => { el.setPointerCapture(event.pointerId); held = true; onMove(event); });
el.addEventListener("pointermove", (event) => { if (held) onMove(event); });
el.addEventListener("pointerup", () => { held = false; });
el.addEventListener("pointercancel", () => { held = false; });
};
dragger(sv, (event) => {
const box = sv.getBoundingClientRect();
hsv.s = clamp((event.clientX - box.left) / box.width, 0, 1);
hsv.v = 1 - clamp((event.clientY - box.top) / box.height, 0, 1);
commit();
});
dragger(hue, (event) => {
const box = hue.getBoundingClientRect();
hsv.h = clamp((event.clientX - box.left) / box.width, 0, 1) * 359;
commit();
});
sv.addEventListener("keydown", (event) => {
const move = { ArrowRight: ["s", .02], ArrowLeft: ["s", -.02], ArrowUp: ["v", .02], ArrowDown: ["v", -.02] }[event.key];
if (!move) return;
event.preventDefault();
hsv[move[0]] = clamp(hsv[move[0]] + move[1], 0, 1);
commit();
});
hue.addEventListener("keydown", (event) => {
const move = { ArrowRight: 4, ArrowUp: 4, ArrowLeft: -4, ArrowDown: -4 }[event.key];
if (!move) return;
event.preventDefault();
hsv.h = clamp(hsv.h + move, 0, 359);
commit();
});
hex.addEventListener("input", (event) => {
event.stopPropagation();
let t = hex.value.trim().toLowerCase();
if (t && !t.startsWith("#")) t = "#" + t;
if (/^#[0-9a-f]{3}$/.test(t)) t = "#" + [...t.slice(1)].map((c) => c + c).join("");
if (/^#[0-9a-f]{6}$/.test(t)) take(t, true, true);
});
hex.addEventListener("keydown", (event) => { if (event.key === "Enter") { event.preventDefault(); closePop(); trigger.focus(); } });
trigger.addEventListener("click", () => {
if (root.classList.contains("open")) return closePop();
showPop(root, trigger, panel, () => paint());
});
paint();
Object.defineProperty(root, "value", { get: () => current, set: (v) => { current = v || "#000000"; hsv = hexToHsv(current); paint(); } });
return root;
}
// ---------------------------------------------------------------------------- a yes/no question
function ask(title, text, action, stay) {
return new Promise((resolve) => {
const done = (answer) => { dialog.close(); dialog.remove(); resolve(answer); };
const dialog = h("dialog", { class: "ask" }, h("h2", {}, title), h("p", {}, text),
h("div", { class: "actions" }, h("button", { type: "button", class: "btn danger", onclick: () => done(true) }, action),
h("button", { type: "button", class: "btn", onclick: () => done(false) }, stay || "Keep it")));
dialog.addEventListener("cancel", (event) => { event.preventDefault(); done(false); });
document.body.append(dialog);
dialog.showModal();
});
}