| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
(() => { |
| 15 |
"use strict"; |
| 16 |
|
| 17 |
const TICK = ".row-select"; |
| 18 |
const COMMIT = "[data-over]"; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
const LABEL = "data-over-label"; |
| 23 |
|
| 24 |
|
| 25 |
const commits = (root) => root.querySelectorAll(COMMIT); |
| 26 |
|
| 27 |
|
| 28 |
const chosen = () => document.querySelectorAll(`${TICK}:checked`).length; |
| 29 |
|
| 30 |
|
| 31 |
* A control's own words, remembered the first time they are seen. |
| 32 |
* |
| 33 |
* A button carries them as its text. A field group carries none of its own |
| 34 |
* -- its words are the question inside it -- so it is left alone and only |
| 35 |
* disabled. |
| 36 |
|
| 37 |
const words = (control) => { |
| 38 |
if (!control.hasAttribute(LABEL)) { |
| 39 |
control.setAttribute(LABEL, control.textContent.trim()); |
| 40 |
} |
| 41 |
return control.getAttribute(LABEL); |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
* Disable the control, or the controls inside it. |
| 46 |
* |
| 47 |
* A commit act is a button and takes the attribute itself. A picker over a |
| 48 |
* selection is a wrapper around a control makeover emitted, so the wrapper |
| 49 |
* is not the thing a browser can disable and the input inside it is. |
| 50 |
|
| 51 |
const inert = (control, off) => { |
| 52 |
const inputs = control.querySelectorAll("input, select, textarea, button"); |
| 53 |
if (inputs.length === 0) { |
| 54 |
control.toggleAttribute("disabled", off); |
| 55 |
control.setAttribute("aria-disabled", String(off)); |
| 56 |
return; |
| 57 |
} |
| 58 |
for (const input of inputs) { |
| 59 |
input.toggleAttribute("disabled", off); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
const sync = () => { |
| 64 |
const count = chosen(); |
| 65 |
for (const control of commits(document)) { |
| 66 |
inert(control, count === 0); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
if (control.tagName === "BUTTON") { |
| 71 |
const label = words(control); |
| 72 |
control.textContent = count === 0 ? label : `${label} (${count})`; |
| 73 |
} |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
document.addEventListener("change", (event) => { |
| 81 |
if (event.target instanceof Element && event.target.matches(TICK)) { |
| 82 |
sync(); |
| 83 |
} |
| 84 |
}); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
document.addEventListener("htmx:after:settle", sync); |
| 92 |
if (document.readyState === "loading") { |
| 93 |
document.addEventListener("DOMContentLoaded", sync); |
| 94 |
} else { |
| 95 |
sync(); |
| 96 |
} |
| 97 |
})(); |
| 98 |
|