mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 01:16:10 +00:00
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.
62 lines
2.3 KiB
JavaScript
62 lines
2.3 KiB
JavaScript
// Repeats request() while the page is visible and passes each answer to
|
|
// onData. request() returns a jQuery promise (e.g. $.getJSON(url)); onData is
|
|
// called after the poller's own bookkeeping, so an error in it does not stop
|
|
// the polling. One request at a time: the next one starts
|
|
// interval ms after the previous one has finished, so a slow host does not
|
|
// pile up requests, each holding a web worker. A hidden page asks nothing;
|
|
// when it is shown again it asks at once. stop() pauses, start() resumes
|
|
// at once (for example while a tab of the page is shown). The first request
|
|
// goes out after firstDelay ms, the interval if it is not given.
|
|
function pollWhileVisible(interval, request, onData, firstDelay) {
|
|
var timer = null;
|
|
var running = false;
|
|
var stopped = false;
|
|
|
|
function run() {
|
|
timer = null;
|
|
if (stopped || document.hidden) {
|
|
return;
|
|
}
|
|
running = true;
|
|
request()
|
|
.always(function () {
|
|
running = false;
|
|
if (!stopped && !document.hidden) {
|
|
timer = setTimeout(run, interval);
|
|
}
|
|
})
|
|
.done(onData);
|
|
}
|
|
|
|
function resume() {
|
|
if (!stopped && !document.hidden && !running && timer === null) {
|
|
run();
|
|
}
|
|
}
|
|
|
|
document.addEventListener("visibilitychange", resume);
|
|
timer = setTimeout(run, firstDelay === undefined ? interval : firstDelay);
|
|
|
|
return {
|
|
stop: function () {
|
|
stopped = true;
|
|
clearTimeout(timer);
|
|
timer = null;
|
|
},
|
|
start: function () {
|
|
stopped = false;
|
|
resume();
|
|
},
|
|
};
|
|
}
|
|
|
|
// The user is in the page: a dialog open, a field in use, an XML edit not
|
|
// saved or VMs chosen on the VM list. A poll that would reload the page waits for its next answer.
|
|
function pageInUse() {
|
|
var active = document.activeElement;
|
|
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)
|
|
|| !!document.querySelector("button[data-busy]"); // a form being sent (submit_busy.js)
|
|
}
|