Ask about a reboot in a box over the page
The reboot links on the Overview open the question instead of sending you to the Advanced page, where the button is out of sight. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
// A question in a box over the page, instead of the browser's own.
|
||||
// <form data-dialog="Reboot now?" data-dialog-ok="Reboot"> asks when it is sent;
|
||||
// <button data-dialog="Reboot now?" data-dialog-ok="Reboot" data-post="/reboot"> posts there when answered yes
|
||||
// (with the session's token, which the page names in <meta name="csrf">).
|
||||
(function () {
|
||||
function ask(message, ok, yes) {
|
||||
var box = document.createElement("div");
|
||||
box.className = "upload-overlay";
|
||||
box.setAttribute("role", "alertdialog");
|
||||
box.setAttribute("aria-modal", "true");
|
||||
var card = document.createElement("div");
|
||||
card.className = "upload-card";
|
||||
var text = document.createElement("p");
|
||||
text.className = "dialog-question";
|
||||
text.textContent = message;
|
||||
var row = document.createElement("div");
|
||||
row.className = "dialog-actions";
|
||||
var no = document.createElement("button");
|
||||
no.type = "button";
|
||||
no.className = "secondary";
|
||||
no.textContent = "Cancel";
|
||||
var go = document.createElement("button");
|
||||
go.type = "button";
|
||||
go.textContent = ok || "OK";
|
||||
row.append(no, go);
|
||||
card.append(text, row);
|
||||
box.append(card);
|
||||
|
||||
var before = document.activeElement;
|
||||
function close() {
|
||||
document.removeEventListener("keydown", key, true);
|
||||
box.remove();
|
||||
if (before && before.focus) before.focus();
|
||||
}
|
||||
function key(e) {
|
||||
if (e.key === "Escape") { e.preventDefault(); close(); }
|
||||
}
|
||||
no.onclick = close;
|
||||
go.onclick = function () { close(); yes(); };
|
||||
// A click beside the box cancels, too.
|
||||
box.onclick = function (e) { if (e.target === box) close(); };
|
||||
document.addEventListener("keydown", key, true);
|
||||
document.body.append(box);
|
||||
// The safe answer has the focus.
|
||||
no.focus();
|
||||
}
|
||||
|
||||
function post(path) {
|
||||
var meta = document.querySelector('meta[name="csrf"]');
|
||||
var form = document.createElement("form");
|
||||
form.method = "post";
|
||||
form.action = path;
|
||||
var field = document.createElement("input");
|
||||
field.type = "hidden";
|
||||
field.name = "csrf";
|
||||
field.value = meta ? meta.content : "";
|
||||
form.append(field);
|
||||
document.body.append(form);
|
||||
form.submit();
|
||||
}
|
||||
|
||||
document.addEventListener("submit", function (e) {
|
||||
var form = e.target;
|
||||
if (!form.hasAttribute("data-dialog") || form.dialogAnswered) return;
|
||||
e.preventDefault();
|
||||
ask(form.getAttribute("data-dialog"), form.getAttribute("data-dialog-ok"), function () {
|
||||
form.dialogAnswered = true;
|
||||
form.submit();
|
||||
});
|
||||
});
|
||||
|
||||
// The page can be replaced while it is open (live.js), so ask for the element at click time.
|
||||
document.addEventListener("click", function (e) {
|
||||
var el = e.target.closest ? e.target.closest("[data-post][data-dialog]") : null;
|
||||
if (!el) return;
|
||||
e.preventDefault();
|
||||
ask(el.getAttribute("data-dialog"), el.getAttribute("data-dialog-ok"), function () {
|
||||
post(el.getAttribute("data-post"));
|
||||
});
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user