Stage 0, steps 3-5, in one commit: the three were built together (the kit
page is what exercises the partials and the shell) and do not split into
states that build on their own. Written by Sonnet, reviewed by Opus.
- static/panel.css: shared/brand.css + panel/theme.css, rule for rule.
wordmark.svg and perf-edge.svg beside it; the stamp is logo.svg.
- templates/components.html: every partial of the plan's Three layers,
with its typed input in components.go. Wrappers are open/close pairs
(box_open/box_close, body_open/body_close, record_open/record_close).
Rich text is a Text value built from escaped runs - no field is
trusted HTML. status_tag and wbr_at are template functions; copy_field
is a partial whose button panel.js wires through data-copy.
- templates/layout.html: the shell as build.py draws it, the menu paths
written out literally, visibility from a typed Shell (role, feature
flags, the user's reach); the signed-out screen as a second define.
Not in the 2.0 menu: Inbound Log / Queue / Quarantine / Filter lists
and Preflight.
- GET /server/components (global role, 404 otherwise): the mockup
components page rebuilt from fixtures (kit.go) by the real templates.
- The old panel steps aside unchanged: panel.css -> legacy.css,
layout.html -> layout_legacy.html; a page uses the new layout iff it
is off legacy_pages.txt.
- legacy_pages.txt: @kit removed - the kit is under the guards for good.
Differences from the mockup components page, for the owner:
- the head button is Help, not "Cover sheet"; two "Example" links are
dropped (no fixed URL), two point at /outbound/domains and /login;
- the page sits in the Server group, so the sibling strip shows;
- Sign out is a POST form whose button carries navbar-item: same size
and type as the links, but without Bulma's hover tint;
- a head's actions are always wrapped in div.buttons.
Evidence
- docs/assets/panel-redesign/evidence/components.png: 1440 px wide, full
height, headless Edge. Taken from the real templates served locally,
NOT from the container (no Docker on this machine); the page reads
only its own fixtures and embedded assets.
- panel/audit.js on the kit page, 1440 and 1280:
{"pages":1,"widths":[1440,1280],"textsChecked":516,"problems":[]}
- go test ./internal/web/... (with @kit removed)
ok github.com/mixeme/selfpost/internal/web
ok github.com/mixeme/selfpost/internal/web/auth
ok github.com/mixeme/selfpost/internal/web/handlers
ok github.com/mixeme/selfpost/internal/web/validate
ok github.com/mixeme/selfpost/internal/web/view
TestRoutesFollowNavigation stays skipped behind @routes (stage 1).
Roadmap 10/27.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
266 lines
8.8 KiB
JavaScript
266 lines
8.8 KiB
JavaScript
// Panel progressive enhancement. Everything here is optional convenience: the
|
|
// pages are fully usable with JavaScript disabled or blocked, and nothing is
|
|
// sent to the server from this file.
|
|
(function () {
|
|
"use strict";
|
|
|
|
// --- Copy buttons on .code values ------------------------------------
|
|
// Values that get carried into another interface (a DNS panel, a mail
|
|
// client) sit in a .code-row wrapper next to a Copy button. The text is read
|
|
// from the .code element itself, so it can never drift from what is shown.
|
|
// navigator.clipboard needs a secure context (HTTPS or localhost); over plain
|
|
// HTTP in development it is simply absent, in which case the value stays
|
|
// selectable by hand.
|
|
document.addEventListener("click", function (ev) {
|
|
var button = ev.target.closest("button.copy");
|
|
if (!button) {
|
|
return;
|
|
}
|
|
var row = button.closest(".code-row");
|
|
var code = row && row.querySelector(".code");
|
|
if (!code || !navigator.clipboard) {
|
|
return;
|
|
}
|
|
navigator.clipboard.writeText(code.textContent).then(function () {
|
|
var original = button.textContent;
|
|
button.textContent = "Copied";
|
|
setTimeout(function () {
|
|
button.textContent = original;
|
|
}, 1500);
|
|
}, function () {
|
|
/* Clipboard refused (permissions, insecure context): leave the page be. */
|
|
});
|
|
});
|
|
|
|
// --- Copy buttons of the component kit ------------------------------
|
|
// copy_field joins a read-only input or textarea to a Copy button marked
|
|
// data-copy; the two sit together in one .has-addons field, and the value is
|
|
// read from the field itself, so it can never drift from what is shown. Where
|
|
// the clipboard API is absent (plain HTTP outside localhost) the value is
|
|
// selected instead, ready for the operator's own Ctrl+C. The button's icon
|
|
// stays; only its word changes for a moment.
|
|
document.addEventListener("click", function (ev) {
|
|
var button = ev.target.closest("button[data-copy]");
|
|
if (!button) {
|
|
return;
|
|
}
|
|
var group = button.closest(".has-addons");
|
|
var field = group && group.querySelector("input, textarea");
|
|
if (!field) {
|
|
return;
|
|
}
|
|
if (!navigator.clipboard) {
|
|
field.focus();
|
|
field.select();
|
|
return;
|
|
}
|
|
navigator.clipboard.writeText(field.value).then(function () {
|
|
var label = button.lastChild;
|
|
if (!label || label.nodeType !== Node.TEXT_NODE) {
|
|
return;
|
|
}
|
|
var original = label.nodeValue;
|
|
label.nodeValue = "Copied";
|
|
setTimeout(function () {
|
|
label.nodeValue = original;
|
|
}, 1500);
|
|
}, function () {
|
|
field.focus();
|
|
field.select();
|
|
});
|
|
});
|
|
|
|
// --- Confirmation on destructive forms --------------------------------
|
|
// Forms that delete something or invalidate a working credential carry a
|
|
// data-confirm message. The prompt lives here rather than in an inline
|
|
// onsubmit attribute because the panel's Content-Security-Policy allows no
|
|
// inline script. The listener is delegated from the document,
|
|
// so it also covers markup swapped in by HTMX. With JavaScript disabled the
|
|
// form submits without asking — exactly as the inline handler behaved.
|
|
document.addEventListener("submit", function (ev) {
|
|
var form = ev.target.closest("form[data-confirm]");
|
|
if (form && !window.confirm(form.dataset.confirm)) {
|
|
ev.preventDefault();
|
|
}
|
|
});
|
|
|
|
// --- Conditional field visibility ------------------------------------
|
|
// Several forms hide a block until a select, checkbox or file input says it
|
|
// applies. One rule table drives them all so the five near-identical helpers
|
|
// do not drift.
|
|
var showWhenRules = [
|
|
{
|
|
match: "select[data-list-mode]",
|
|
target: "[data-addresses]",
|
|
visible: function (el) { return el.value === el.dataset.listMode; }
|
|
},
|
|
{
|
|
match: "select[data-custom-mode]",
|
|
target: "[data-custom-address]",
|
|
visible: function (el) { return el.value === el.dataset.customMode; }
|
|
},
|
|
{
|
|
match: "select[data-ratelimit-mode]",
|
|
target: "[data-manual-fields]",
|
|
visible: function (el) { return el.value === "manual"; }
|
|
},
|
|
{
|
|
match: "select[data-ratelimit-mode]",
|
|
target: "[data-auto-fields]",
|
|
visible: function (el) { return el.value === "auto"; }
|
|
},
|
|
{
|
|
match: "select[data-global-role]",
|
|
target: "[data-domain-pick]",
|
|
visible: function (el) { return el.value !== el.dataset.globalRole; }
|
|
},
|
|
{
|
|
match: "input[data-encrypt-toggle]",
|
|
target: "[data-encrypt-fields]",
|
|
visible: function (el) { return el.checked; },
|
|
clearWhenHidden: true
|
|
},
|
|
{
|
|
match: "input[data-import-file]",
|
|
target: "[data-import-password-fields]",
|
|
visible: function (el) {
|
|
var name = (el.files && el.files[0] && el.files[0].name || "").toLowerCase();
|
|
return name !== "" && !/\.json$/.test(name);
|
|
},
|
|
clearWhenHidden: true
|
|
}
|
|
];
|
|
|
|
function syncShowWhen(control) {
|
|
var form = control.closest("form");
|
|
if (!form) {
|
|
return;
|
|
}
|
|
for (var i = 0; i < showWhenRules.length; i++) {
|
|
var rule = showWhenRules[i];
|
|
if (!control.matches(rule.match)) {
|
|
continue;
|
|
}
|
|
var target = form.querySelector(rule.target);
|
|
if (!target) {
|
|
return;
|
|
}
|
|
var show = rule.visible(control);
|
|
target.hidden = !show;
|
|
if (!show && rule.clearWhenHidden) {
|
|
target.querySelectorAll("input").forEach(function (input) {
|
|
input.value = "";
|
|
});
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
function initShowWhen(root) {
|
|
showWhenRules.forEach(function (rule) {
|
|
root.querySelectorAll(rule.match).forEach(function (control) {
|
|
syncShowWhen(control);
|
|
control.addEventListener("change", function () {
|
|
syncShowWhen(control);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
initShowWhen(document);
|
|
});
|
|
|
|
// --- Adaptive monitoring polling ---------------------------------------
|
|
// The four monitoring fragments carry data-poll and hx-trigger="load" for
|
|
// the first fetch only. panel.js schedules the rest: 5 s while the operator
|
|
// is active on the page, 30 s when the tab is visible but idle, and nothing
|
|
// while the tab is hidden. hx-trigger="every Ns [expr]" could express some
|
|
// of that, but the filter is evaluated with `new Function`, which the
|
|
// panel's CSP (default-src 'self', no 'unsafe-eval') would silently break.
|
|
var pollActiveMs = 5000;
|
|
var pollIdleMs = 30000;
|
|
// No pointer/keyboard/scroll input for this long → treat the tab as idle.
|
|
var userIdleMs = 30000;
|
|
var lastActivity = Date.now();
|
|
var pollTimers = Object.create(null);
|
|
|
|
["mousedown", "mousemove", "keydown", "scroll", "touchstart"].forEach(function (evt) {
|
|
document.addEventListener(evt, function () {
|
|
lastActivity = Date.now();
|
|
}, { passive: true });
|
|
});
|
|
|
|
function pollDelayMs() {
|
|
return Date.now() - lastActivity < userIdleMs ? pollActiveMs : pollIdleMs;
|
|
}
|
|
|
|
function triggerPoll(el) {
|
|
htmx.ajax("GET", el.getAttribute("hx-get"), {
|
|
target: "#" + el.id,
|
|
swap: el.getAttribute("hx-swap") || "outerHTML"
|
|
});
|
|
}
|
|
|
|
function schedulePoll(el) {
|
|
if (!el || !el.id || !el.hasAttribute("data-poll")) {
|
|
return;
|
|
}
|
|
if (pollTimers[el.id]) {
|
|
clearTimeout(pollTimers[el.id]);
|
|
delete pollTimers[el.id];
|
|
}
|
|
if (document.hidden) {
|
|
return;
|
|
}
|
|
var id = el.id;
|
|
pollTimers[id] = setTimeout(function () {
|
|
delete pollTimers[id];
|
|
var current = document.getElementById(id);
|
|
if (!current || !current.hasAttribute("data-poll")) {
|
|
return;
|
|
}
|
|
if (document.hidden) {
|
|
schedulePoll(current);
|
|
return;
|
|
}
|
|
triggerPoll(current);
|
|
}, pollDelayMs());
|
|
}
|
|
|
|
function onPollElementReady(el) {
|
|
if (!el || !el.hasAttribute("data-poll")) {
|
|
return;
|
|
}
|
|
// Swapped-in markup still carries hx-trigger="load"; strip it so htmx does
|
|
// not issue a duplicate GET on top of the response we just received.
|
|
el.removeAttribute("hx-trigger");
|
|
schedulePoll(el);
|
|
}
|
|
|
|
document.body.addEventListener("htmx:afterSwap", function (ev) {
|
|
onPollElementReady(ev.detail.elt);
|
|
});
|
|
|
|
document.body.addEventListener("htmx:responseError", function (ev) {
|
|
onPollElementReady(ev.detail.elt);
|
|
});
|
|
|
|
document.body.addEventListener("htmx:beforeRequest", function (ev) {
|
|
if (document.hidden && ev.target.hasAttribute && ev.target.hasAttribute("data-poll")) {
|
|
ev.preventDefault();
|
|
}
|
|
});
|
|
|
|
document.addEventListener("visibilitychange", function () {
|
|
if (document.hidden) {
|
|
Object.keys(pollTimers).forEach(function (id) {
|
|
clearTimeout(pollTimers[id]);
|
|
delete pollTimers[id];
|
|
});
|
|
return;
|
|
}
|
|
document.querySelectorAll("[data-poll]").forEach(schedulePoll);
|
|
});
|
|
})();
|