| index 550a843..4f07a5a 100644 |
| --- a/busy/busy.js |
| +++ b/busy/busy.js |
| @@ -9,11 +9,26 @@ |
| What a submitted button does: its label gives way to a spinner (the |
| label stays, beside the spinner, under reduced motion or forced |
| colours — tokens.css), it goes aria-busy and then disabled, and its |
| - form refuses a second submit. The spinner shows for at least |
| - HOLD_MS: a response faster than that is held back until then, so a |
| - quick save reads as a deliberate beat rather than a flicker. Going |
| - Back to the page hands every busy form back, and a submit still being |
| - held when the page is left is dropped rather than sent later. |
| + form refuses a second submit. When depends on the method, the |
| + submitter's formmethod beating the form's: |
| + |
| + POST the spinner shows at once and for at least HOLD_MS: a |
| + response faster than that is held back until then, so a |
| + quick save reads as a deliberate beat rather than a flicker. |
| + A save changes something, and the beat says it happened. |
| + GET the submit goes at once, and the button shows nothing |
| + unless the page is still here SHOW_AFTER_MS later. A GET |
| + only looks something up (a search, a filter, a view): |
| + holding it made every look slower for a spinner nobody |
| + needed, and one that flashes on its way out is a glitch. |
| + The form refuses a second submit from the start. |
| + |
| + Going Back to the page hands every busy form back. A submit still |
| + being held when the page is left is dropped rather than sent |
| + later, and a GET's spinner still waiting is never shown. Left means |
| + a navigation has started, not that it has finished. A form pow.js |
| + protects is pow's to hold: this file arms only pow's re-send and |
| + never holds it. |
| |
| Vocabulary: |
| data-busy="false" on a <form> or on one submit button: opt OUT |
| @@ -22,6 +37,10 @@ |
| included — changes nothing |
| data-busy-label="…" on the form or on the button: replacement |
| button text while it works |
| + data-live-form on a <form>: live.js's to send, and to show |
| + busy, whenever it claims the submit (marking |
| + the event e.liveForm); this file arms it only |
| + when live.js lets it go natively |
| |
| A script that handles a submit itself (a fetch, say) cancels it in a |
| listener on the form or the document, as it would anyway: those run |
| @@ -34,10 +53,15 @@ |
| (function () { |
| "use strict"; |
| |
| - // How long the spinner shows, at least. Long enough to read as an |
| - // answer to the click, short enough never to feel like waiting. |
| + // How long a POST's spinner shows, at least. Long enough to read as |
| + // an answer to the click, short enough never to feel like waiting. |
| var HOLD_MS = 650; |
| |
| + // How long a GET may take before its button says it is working. |
| + // Under half a second a click and its answer read as one event; past |
| + // it, a still button reads as a missed click. |
| + var SHOW_AFTER_MS = 500; |
| + |
| // ── The busy rule ──────────────────────────────────────────────── |
| // |
| // A button that CHANGES something says so while it works; a button |
| @@ -71,6 +95,8 @@ |
| // Every busy element in the DOCUMENT, then the ownership test: a |
| // form="id" submit button is owned by a form it does not live in. |
| function busyOff(form) { |
| + clearTimeout(reveals.get(form)); |
| + reveals.delete(form); |
| form.removeAttribute("aria-busy"); |
| document.querySelectorAll('[aria-busy="true"]').forEach(function (b) { |
| if (b.form !== form && !form.contains(b)) return; |
| @@ -85,9 +111,25 @@ |
| }); |
| } |
| |
| - function busySubmit(e) { |
| + function busySubmit(e, late) { |
| var form = e.target; |
| if (!form || form.tagName !== "FORM") return; |
| + // A swapping form is live.js's whenever live.js claims it: arming it |
| + // here too would hand it back a tick after live.js cancels the |
| + // submit, clearing live.js's busy state mid-request. Whether this |
| + // capture listener runs before live.js's is down to script order, so |
| + // it is armed late, from the window listener, and only if live.js |
| + // did not claim it (e.liveForm, set in live.js's capture listener) |
| + // and nobody cancelled it. One live.js leaves to the browser (a GET, |
| + // another origin, a page live.js is not running on) keeps the rule. |
| + if (form.hasAttribute("data-live-form") && !late) return; |
| + // pow.js owns every submit of a form it protects: it cancels it, |
| + // shows its own busy state (this file's vocabulary) while it waits, |
| + // and re-sends it marked data-pow-released. Arming here before that |
| + // would hand the form back a tick later and wipe pow's feedback, and |
| + // a guard armed here would refuse pow's re-send. Arm on the re-send |
| + // only. |
| + if (form.hasAttribute("data-pow-form") && !form.hasAttribute("data-pow-released")) return; |
| if (form.getAttribute("data-busy") === "false") return; |
| // The button the browser submitted with: the one clicked, or — for |
| // Enter in a field — the default one it implicitly clicked. Only |
| @@ -109,48 +151,91 @@ |
| // shadowable, and under strict mode assigning to a shadowed one throws. |
| var to = (btn && btn.getAttribute("formtarget")) || form.getAttribute("target"); |
| if (to && to !== "_self") return; |
| - if (/^dialog$/i.test(btn && btn.getAttribute("formmethod") || |
| - form.getAttribute("method"))) return; |
| + var method = btn && btn.getAttribute("formmethod") || form.getAttribute("method"); |
| + if (/^dialog$/i.test(method)) return; |
| if (form === releasing) return; // the hold sending the submit it held |
| if (form.getAttribute("aria-busy") === "true") { e.preventDefault(); return; } |
| form.setAttribute("aria-busy", "true"); |
| if (!btn || btn.getAttribute("data-busy") === "false") btn = null; |
| + // Anything but post is a GET: a missing method, and one HTML does |
| + // not know, both submit as one. Its button waits for the reveal, and |
| + // with no busy button the hold below leaves its submit alone. |
| var label = btn && (btn.getAttribute("data-busy-label") || |
| form.getAttribute("data-busy-label")); |
| - if (btn) { |
| - btn.setAttribute("aria-busy", "true"); |
| - if (btn.tagName === "BUTTON") { |
| - if (label) { |
| - btn.setAttribute("data-idle-label", btn.textContent); |
| - btn.textContent = label; |
| - } |
| - // A child element, not a pseudo-element: the shim has to be |
| - // able to take it away again. An <input type="submit"> has no |
| - // children, which is the one shape this cannot draw a spinner |
| - // on. tokens.css stops it rotating under reduced motion. |
| - var spin = document.createElement("span"); |
| - spin.setAttribute("rst-spin", ""); |
| - spin.setAttribute("aria-hidden", "true"); |
| - btn.insertBefore(spin, btn.firstChild); |
| - } |
| + if (!/^post$/i.test(method || "")) { |
| + if (btn) reveals.set(form, setTimeout(function () { reveal(form, btn, label); }, SHOW_AFTER_MS)); |
| + setTimeout(function () { |
| + // Cancelled downstream, as below: hand the form back, and |
| + // cancel the reveal with it. |
| + if (e.defaultPrevented) busyOff(form); |
| + }, 0); |
| + return; |
| } |
| + if (btn) dress(btn, label); |
| setTimeout(function () { |
| // Someone downstream cancelled the submit — an app handler doing |
| // the work itself, most likely. Nothing is on its way anywhere, |
| // so hand the form back, guard included, and let whoever took the |
| // job own the feedback too. |
| if (e.defaultPrevented && !held.has(e)) { busyOff(form); return; } |
| - if (!btn) return; |
| - if (label && btn.tagName === "INPUT") { |
| - btn.setAttribute("data-idle-label", btn.value); |
| - btn.value = label; |
| - } |
| - btn.disabled = true; |
| + if (btn) harden(btn, label); |
| }, 0); |
| } |
| |
| + // The half of the busy state that cannot touch the payload. The |
| + // spinner is a child element, not a pseudo-element, so busyOff can |
| + // take it away again. An <input type="submit"> has no children, |
| + // which is the one shape this cannot draw a spinner on. tokens.css |
| + // stops it rotating under reduced motion. |
| + function dress(btn, label) { |
| + btn.setAttribute("aria-busy", "true"); |
| + if (btn.tagName !== "BUTTON") return; |
| + if (label) { |
| + btn.setAttribute("data-idle-label", btn.textContent); |
| + btn.textContent = label; |
| + } |
| + var spin = document.createElement("span"); |
| + spin.setAttribute("rst-spin", ""); |
| + spin.setAttribute("aria-hidden", "true"); |
| + btn.insertBefore(spin, btn.firstChild); |
| + } |
| + |
| + // The half that can: a disabled button drops its name and value, and |
| + // an <input type="submit">'s value is what it submits. |
| + function harden(btn, label) { |
| + if (label && btn.tagName === "INPUT") { |
| + btn.setAttribute("data-idle-label", btn.value); |
| + btn.value = label; |
| + } |
| + btn.disabled = true; |
| + } |
| + |
| document.addEventListener("submit", busySubmit, true); |
| |
| + // ── The reveal ─────────────────────────────────────────────────── |
| + // |
| + // A GET's page is still here SHOW_AFTER_MS after the click, so the |
| + // answer is slow, and the button says so now: the busy state a POST |
| + // shows at once. Nothing here can change the payload any more; the |
| + // entry list was built long ago. A button that has left its form |
| + // meanwhile (a polled fragment replacing it, a form= changed) is not |
| + // the one working, and busyOff would never find it to hand it back. |
| + var reveals = new Map(); // form -> the timer that will reveal its button |
| + function reveal(form, btn, label) { |
| + reveals.delete(form); |
| + if (form.getAttribute("aria-busy") !== "true") return; |
| + if (!btn.isConnected || btn.form !== form) return; |
| + dress(btn, label); |
| + harden(btn, label); |
| + } |
| + |
| + // Every reveal still waiting is dropped. Not on beforeunload: a GET's |
| + // own navigation fires it, and a slow one is exactly when to show. |
| + function dropReveals() { |
| + reveals.forEach(function (timer) { clearTimeout(timer); }); |
| + reveals.clear(); |
| + } |
| + |
| // ── The hold ───────────────────────────────────────────────────── |
| // |
| // A spinner can only be promised a minimum time by holding the |
| @@ -174,6 +259,16 @@ |
| var form = e.target; |
| if (!form || form.tagName !== "FORM") return; |
| if (form === releasing) { releasing = null; return; } |
| + if (form.hasAttribute("data-live-form")) { |
| + if (e.defaultPrevented || e.liveForm) return; |
| + busySubmit(e, true); |
| + } |
| + // Never hold a pow form: pow has already held it while it solved, |
| + // and a second hold is a second wait for nothing. Worse, holding it |
| + // here and re-sending later would send it back through pow, which |
| + // would hold it again with busy.js's guard still armed, and the form |
| + // could never submit. |
| + if (form.hasAttribute("data-pow-form")) return; |
| if (e.defaultPrevented || form.getAttribute("aria-busy") !== "true") return; |
| // Only a submit with a spinner to show is held: the hold is for the |
| // spinner. A script's requestSubmit() with no submitter, or a button |
| @@ -184,6 +279,13 @@ |
| e.preventDefault(); |
| held.add(e); |
| pending.set(form, setTimeout(function () { release(form, btn); }, HOLD_MS)); |
| + // pagehide fires only once a navigation commits, and a slow |
| + // destination leaves the hold running until then: a held submit |
| + // released meanwhile replaces the navigation the visitor chose. |
| + // beforeunload fires as the navigation starts. Attached only while |
| + // something is held, so it keeps no page out of the back-forward |
| + // cache. |
| + if (pending.size === 1) window.addEventListener("beforeunload", dropHeld); |
| }); |
| |
| // Send the held submit. The submitter goes with it, so its name and |
| @@ -195,6 +297,7 @@ |
| // must be handed back with its errors showing, not left spinning. |
| function release(form, btn) { |
| pending.delete(form); |
| + if (pending.size === 0) window.removeEventListener("beforeunload", dropHeld); |
| if (!form.isConnected) return; |
| var skip = form.hasAttribute("novalidate") || |
| (btn && btn.hasAttribute("formnovalidate")); |
| @@ -234,19 +337,32 @@ |
| } |
| } |
| |
| + // A navigation started while a submit was held: drop every held |
| + // submit and hand the forms back, so a navigation that is then |
| + // cancelled (a download, a beforeunload prompt) leaves no form |
| + // spinning. |
| + function dropHeld() { |
| + pending.forEach(function (timer, form) { |
| + clearTimeout(timer); |
| + busyOff(form); |
| + }); |
| + pending.clear(); |
| + window.removeEventListener("beforeunload", dropHeld); |
| + } |
| + |
| // Leaving the page — a link, Back — while a submit is held drops it: |
| // the visitor has gone somewhere else, and a timer surviving into the |
| - // back/forward cache would otherwise send it on their return. |
| - window.addEventListener("pagehide", function () { |
| - pending.forEach(function (timer) { clearTimeout(timer); }); |
| - pending.clear(); |
| - }); |
| + // back/forward cache would otherwise send it on their return, or |
| + // dress a form they walked away from as busy. |
| + window.addEventListener("pagehide", function () { dropHeld(); dropReveals(); }); |
| |
| // The back/forward cache restores a page's DOM exactly as it was left |
| // — busy buttons still disabled, still wearing the busy label and the |
| // spinner — so a visitor who navigates back finds a dead form. Hand |
| // every busy form back. |
| window.addEventListener("pageshow", function (e) { |
| - if (e.persisted) document.querySelectorAll("form[aria-busy]").forEach(busyOff); |
| + if (!e.persisted) return; |
| + document.querySelectorAll("form[aria-busy]").forEach(busyOff); |
| + dropReveals(); |
| }); |
| })(); |