|
14 | 14 | * unless it is marked `data-unconfirmed`, as a change that needs no |
15 | 15 | * confirmation, such as writing a draft announcement, is; a button of it marked |
16 | 16 | * `data-confirms`, such as the draft's "Send", still asks. |
| 17 | + * The dialog names what it confirms, from the link's or form's `data-confirming`: |
| 18 | + * the change, and what it acts on, so two pages left open are told apart. One |
| 19 | + * that names nothing gets the dialog's `data-default`, what the page acts on. |
| 20 | + * A form marked `data-submit-once` posts once: a double click sends nothing more, |
| 21 | + * so a change, or a link or secret shown only once, is not lost to it. A page |
| 22 | + * the browser restores from its back/forward cache may post it again. |
17 | 23 | * The dialog's `data-passkey-confirm` button runs the passkey ceremony for the |
18 | 24 | * dashboard's origin, which auth lists as a related origin of its RP ID: the |
19 | 25 | * dialog's `data-confirmation-start` starts it, and its `data-confirmation-finish` |
@@ -90,7 +96,14 @@ function wire(document) { |
90 | 96 | const dialog = document.getElementById('passkey-confirmation'); |
91 | 97 | const button = dialog?.querySelector('button[data-passkey-confirm]'); |
92 | 98 | const alert = dialog?.querySelector('[data-passkey-error]'); |
93 | | - if (!dialog || !(button instanceof HTMLButtonElement) || !(alert instanceof HTMLElement)) return; |
| 99 | + const confirming = dialog?.querySelector('[data-confirming]'); |
| 100 | + if ( |
| 101 | + !dialog || |
| 102 | + !(button instanceof HTMLButtonElement) || |
| 103 | + !(alert instanceof HTMLElement) || |
| 104 | + !(confirming instanceof HTMLElement) |
| 105 | + ) |
| 106 | + return; |
94 | 107 | const { confirmationStart: start = '', confirmationFinish: finish = '' } = dialog.dataset; |
95 | 108 | /** When the confirmation ends, on `performance.now()`'s clock. */ |
96 | 109 | const ends = performance.now() + Number(dialog.dataset.confirmedFor ?? 0) * 1000; |
@@ -144,15 +157,21 @@ function wire(document) { |
144 | 157 | }); |
145 | 158 | }; |
146 | 159 | /** |
147 | | - * Opens the dialog for the change `then` makes once confirmed. Answers whether |
148 | | - * it opened: where it cannot confirm, the change goes on as it is. |
| 160 | + * Opens the dialog for the change `then` makes once confirmed, which `what` |
| 161 | + * names, if anything does. Answers whether it opened: where it cannot confirm, |
| 162 | + * the change goes on as it is. |
149 | 163 | * @param {() => void} then |
| 164 | + * @param {string | undefined} what |
150 | 165 | * @returns {boolean} |
151 | 166 | */ |
152 | | - const confirm = (then) => { |
| 167 | + const confirm = (then, what) => { |
153 | 168 | if (!usable) return false; |
154 | 169 | change = { then, started: post(start) }; |
155 | 170 | unlessStarted(change); |
| 171 | + const named = confirming.firstElementChild; |
| 172 | + const text = what ?? confirming.dataset.default; |
| 173 | + if (named) named.textContent = text ?? ''; |
| 174 | + confirming.hidden = !text; |
156 | 175 | alert.hidden = true; |
157 | 176 | busy(false); |
158 | 177 | dialog.showPopover(); |
@@ -212,27 +231,50 @@ function wire(document) { |
212 | 231 | if (!link || !plain || confirmed(formTime)) return; |
213 | 232 | const opened = confirm(() => { |
214 | 233 | window.location.assign(link.href); |
215 | | - }); |
| 234 | + }, link.dataset.confirming); |
216 | 235 | if (opened) event.preventDefault(); |
217 | 236 | }); |
| 237 | + /** |
| 238 | + * Lets `form` post once, if it is marked `data-submit-once`: a later submit of |
| 239 | + * it is dropped. |
| 240 | + * @param {SubmitEvent} event |
| 241 | + * @param {HTMLFormElement} form |
| 242 | + */ |
| 243 | + const once = (event, form) => { |
| 244 | + if (!form.hasAttribute('data-submit-once')) return; |
| 245 | + if (form.dataset.submitted) event.preventDefault(); |
| 246 | + else form.dataset.submitted = 'true'; |
| 247 | + }; |
| 248 | + // A page the browser restores from its back/forward cache posts again. |
| 249 | + window.addEventListener('pageshow', (event) => { |
| 250 | + if (!event.persisted) return; |
| 251 | + for (const form of document.querySelectorAll('form[data-submitted]')) { |
| 252 | + if (form instanceof HTMLFormElement) delete form.dataset.submitted; |
| 253 | + } |
| 254 | + }); |
218 | 255 | document.addEventListener('submit', (event) => { |
219 | 256 | const form = event.target; |
220 | 257 | if (!(form instanceof HTMLFormElement) || form.method !== 'post') return; |
221 | 258 | if (form === confirmedForm) { |
222 | 259 | confirmedForm = undefined; |
| 260 | + once(/** @type {SubmitEvent} */ (event), form); |
223 | 261 | return; |
224 | 262 | } |
225 | 263 | const submitter = event.submitter; |
226 | 264 | const unconfirmed = |
227 | 265 | form.hasAttribute('data-unconfirmed') && !submitter?.hasAttribute('data-confirms'); |
228 | | - if (!form.closest('main') || unconfirmed || confirmed(0)) return; |
229 | | - const opened = confirm(() => { |
230 | | - // Its submit event runs within the call, or not at all if it is invalid. |
231 | | - confirmedForm = form; |
232 | | - form.requestSubmit(submitter); |
233 | | - confirmedForm = undefined; |
234 | | - }); |
| 266 | + const opened = |
| 267 | + !!form.closest('main') && |
| 268 | + !unconfirmed && |
| 269 | + !confirmed(0) && |
| 270 | + confirm(() => { |
| 271 | + // Its submit event runs within the call, or not at all if it is invalid. |
| 272 | + confirmedForm = form; |
| 273 | + form.requestSubmit(submitter); |
| 274 | + confirmedForm = undefined; |
| 275 | + }, form.dataset.confirming); |
235 | 276 | if (opened) event.preventDefault(); |
| 277 | + else once(/** @type {SubmitEvent} */ (event), form); |
236 | 278 | }); |
237 | 279 | } |
238 | 280 |
|
|
0 commit comments