1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

fix(ui): a form being sent shows it on its button

A form sent to the server showed nothing until the answer came: its
button stayed as it was and took a second click, though creating a
volume or starting a pool can take seconds. The button that sent it now
turns a spinner and takes no second click meanwhile, and a page's reload
or a job's result waits (submit_busy.js on every page, poll.js
pageInUse). It is as before when the page stays: its leaving asked about
unsaved work, or the browser's history brings it back. A form a script
sends itself is left alone.

The please-wait dialog of the VM start and the XML create, and the
sign-in forms' own spinner, go: they show the same as every other form.
This commit is contained in:
catborise 2026-11-21 21:13:01 +03:00
parent e79817e7db
commit fb81e7adca
20 changed files with 55 additions and 79 deletions

View file

@ -14,7 +14,7 @@
var boxes = onScreen();
var chosen = boxes.filter(function (box) { return box.checked; }).length;
var tooMany = chosen > Number(count.dataset.max); // the server takes at most that many
form.querySelectorAll("button[name='action']").forEach(function (button) { button.disabled = tooMany; });
form.querySelectorAll("button[name='action']").forEach(function (button) { button.disabled = tooMany || !!button.dataset.busy; }); // not one being sent (submit_busy.js)
count.textContent = (tooMany ? count.dataset.tooMany : count.dataset.text).replace("%(count)s", chosen);
count.classList.toggle("text-danger", tooMany);
// the bar at the window's foot, while any VM is chosen; the page ends above it

View file

@ -1,6 +1,6 @@
// The sign-in forms (login, OTP login): the eye button shows or hides the
// password, a line says when Caps Lock is on, and while the form is sent its
// button is disabled and says so.
// password, and a line says when Caps Lock is on. While the form is sent, its
// button shows it as every form's does (submit_busy.js).
document.getElementById("btn-toggle-pwd").addEventListener("click", function () {
var password = document.getElementById("id_password");
var shown = password.type === "password";
@ -20,12 +20,3 @@ document.getElementById("btn-toggle-pwd").addEventListener("click", function ()
document.getElementById("id_password").addEventListener("blur", function () {
document.getElementById("caps-lock").classList.add("d-none");
});
document.getElementById("form-signin").addEventListener("submit", function () {
var button = document.getElementById("btn-signin");
button.disabled = true;
var spinner = document.createElement("span");
spinner.className = "spinner-border spinner-border-sm me-2";
spinner.setAttribute("aria-hidden", "true");
button.replaceChildren(spinner, button.dataset.busyText);
});

View file

@ -1,12 +0,0 @@
// The please-wait dialog (pleasewaitdialog.html) of a page that submits a long request.
function showPleaseWaitDialog() {
var pleaseModal = new bootstrap.Modal(document.getElementById('pleaseWaitDialog'), {
keyboard: false
});
pleaseModal.show();
backgroundJobRunning = true;
}
// a form marked data-please-wait shows the dialog while it is sent
document.addEventListener("submit", function (event) {
if (event.target.matches("form[data-please-wait]")) showPleaseWaitDialog();
});

View file

@ -57,5 +57,6 @@ function pageInUse() {
return !!document.querySelector(".modal.show")
|| !!(active && active.matches && active.matches("input, select, textarea"))
|| !!(window.xmlEditUnsaved && window.xmlEditUnsaved())
|| (typeof bulkPowerChosen === "function" && bulkPowerChosen()); // the VM list's bulk form in use (bulk_power.js)
|| (typeof bulkPowerChosen === "function" && bulkPowerChosen()) // the VM list's bulk form in use (bulk_power.js)
|| !!document.querySelector("button[data-busy]"); // a form being sent (submit_busy.js)
}

40
static/js/submit_busy.js Normal file
View file

@ -0,0 +1,40 @@
// A form sent to the server shows it: the button that sent it turns a spinner
// and takes no second click until the answer comes; a page's reload or a job's
// result waits meanwhile (poll.js pageInUse). It is as before when the page
// stays: its leaving asked about unsaved work (an XML edit, an ISO upload) or
// the browser's history brings it back. A form a script sends itself (it
// prevents the submit) is left as it is.
function submitBusyEnd() {
document.querySelectorAll("button[data-busy]").forEach(function (button) {
button.disabled = false;
delete button.dataset.busy;
button.querySelectorAll("[data-busy-spinner]").forEach(function (spinner) { spinner.remove(); });
});
}
var submitLeaveAsked = false;
document.addEventListener("submit", function (event) {
var button = event.submitter;
if (event.target.method !== "post" || !button || button.tagName !== "BUTTON") return;
// after the submit: the form's data, which has the button's name and value, is taken by then
setTimeout(function () {
if (event.defaultPrevented || submitLeaveAsked) {
submitLeaveAsked = false;
return;
}
button.disabled = true;
button.dataset.busy = "1";
button.insertAdjacentHTML("afterbegin", '<span class="spinner-border spinner-border-sm me-1" aria-hidden="true" data-busy-spinner></span>');
}, 0);
});
// after the page's own handlers (they are added as it loads): one asks before leaving
window.addEventListener("load", function () {
window.addEventListener("beforeunload", function (event) {
if (event.defaultPrevented) {
submitLeaveAsked = true;
submitBusyEnd();
}
});
});
window.addEventListener("pageshow", function (event) {
if (event.persisted) submitBusyEnd();
});