1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 01:16:10 +00:00
webvirtcloud/static/js/confirm_dialog.js
catborise e79817e7db fix(ui): a block or a job's cancel asks first; every delete and save says it is done
- Block in the Users list asks first: the user is signed out and their
  API tokens are deleted, which unblocking does not bring back. Block
  and Unblock then say what they did.
- Cancel on a job's page and in the job panel asks first, naming the
  job; the button says Cancel the job, apart from the dialog's Cancel.
- Deleting a user or a group, removing a host from the panel or an
  owner from a VM, and deleting an interface, a network filter or a
  secret say so, as the other deletes do.
- Renaming a tag and saving a VM's title and description say so.
2026-11-21 21:02:20 +03:00

54 lines
2.7 KiB
JavaScript

// confirmAction asks in the page's confirm dialog before act runs: action is its
// title and its confirming button's words, question its body. opener is what
// asked (or null): in another dialog, that one is hidden while asking (Bootstrap
// does not stack dialogs) and shown again after; not confirmed, it takes the focus
// back, or its menu's toggle, as a menu's item is hidden with its menu.
function confirmAction(action, question, danger, act, opener) {
var dialog = document.getElementById("confirm-dialog");
var parent = opener && opener.closest(".modal");
var ok = dialog.querySelector("[data-confirm-ok]");
var confirmed = false;
dialog.querySelector(".modal-title").textContent = action;
dialog.querySelector(".modal-body").textContent = question;
ok.textContent = action;
ok.className = "btn " + (danger ? "btn-danger" : "btn-primary");
ok.disabled = true; // until shown: Bootstrap does not hide a dialog still coming in
dialog.addEventListener("shown.bs.modal", function () { ok.disabled = false; }, {once: true});
ok.onclick = function () {
ok.disabled = true; // once: the dialog is still open while it hides
confirmed = true;
bootstrap.Modal.getInstance(dialog).hide();
act();
};
dialog.addEventListener("hidden.bs.modal", function () {
if (parent) {
// a form sent in place (the flavors) keeps its dialog; a page sent away does not care
bootstrap.Modal.getOrCreateInstance(parent).show();
} else if (!confirmed && opener && opener.isConnected) {
var menu = opener.closest(".dropdown-menu");
var toggle = menu && menu.parentElement.querySelector("[data-bs-toggle=dropdown]");
(toggle || opener).focus();
}
}, {once: true});
var ask = function () { bootstrap.Modal.getOrCreateInstance(dialog).show(); };
if (parent) {
parent.addEventListener("hidden.bs.modal", ask, {once: true});
bootstrap.Modal.getInstance(parent).hide();
} else {
ask();
}
}
// A button with data-confirm asks first, in its own words (or its accessible name,
// for an icon); confirmed, it is clicked again, so its form sends its name and value.
document.addEventListener("click", function (event) {
var button = event.target.closest("button[data-confirm]");
if (!button || button.dataset.confirmed) return;
event.preventDefault();
var action = button.textContent.trim() || button.getAttribute("aria-label") || button.title;
confirmAction(action, button.dataset.confirm, /\b(btn-|btn-outline-|text-)danger\b/.test(button.className), function () {
button.dataset.confirmed = "1";
button.click();
delete button.dataset.confirmed;
}, button);
}, true);