| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
(() => { |
| 23 |
"use strict"; |
| 24 |
|
| 25 |
|
| 26 |
const CONTROL = "data-reveal"; |
| 27 |
|
| 28 |
const WHEN = "data-reveal-when"; |
| 29 |
|
| 30 |
const VALUES = "data-reveal-values"; |
| 31 |
|
| 32 |
|
| 33 |
* What the named control is holding now, or null when it holds nothing. |
| 34 |
* |
| 35 |
* By `name` rather than by `id`, because `name` is what the description |
| 36 |
* carries: it is what a submit sends the value under and what a region |
| 37 |
* names. An id is scoped per form instance by the emitter and is therefore |
| 38 |
* not the string the description wrote. |
| 39 |
* |
| 40 |
* Searched from the region outward: the nearest enclosing form first, then |
| 41 |
* the document, which is `fill.js`'s rule and is here for the same reason. |
| 42 |
* A screen showing the same form twice -- an edit modal over a list -- |
| 43 |
* otherwise reads the first copy in the document whichever one the reader |
| 44 |
* is in. |
| 45 |
* |
| 46 |
* A tick is there by presence, the way a form submits one, so an unticked |
| 47 |
* box and an empty box are one answer. That is the convention the router |
| 48 |
* states and the other two renderers hold. |
| 49 |
|
| 50 |
const held = (region, name) => { |
| 51 |
const selector = `[name="${CSS.escape(name)}"]`; |
| 52 |
const scope = region.closest("form") ?? document; |
| 53 |
const controls = scope.querySelectorAll(selector); |
| 54 |
let value = null; |
| 55 |
for (const control of controls) { |
| 56 |
if (control.type === "checkbox" || control.type === "radio") { |
| 57 |
if (control.checked) { |
| 58 |
value = control.value === "" ? "on" : control.value; |
| 59 |
} |
| 60 |
continue; |
| 61 |
} |
| 62 |
value = control.value ?? null; |
| 63 |
} |
| 64 |
return value === "" ? null : value; |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
const satisfied = (region, value) => { |
| 69 |
const when = region.getAttribute(WHEN); |
| 70 |
if (when === "any") { |
| 71 |
return value !== null; |
| 72 |
} |
| 73 |
if (when === "none") { |
| 74 |
return value === null; |
| 75 |
} |
| 76 |
if (value === null) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
let wanted; |
| 80 |
try { |
| 81 |
wanted = JSON.parse(region.getAttribute(VALUES) ?? "[]"); |
| 82 |
} catch { |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
return false; |
| 87 |
} |
| 88 |
return Array.isArray(wanted) && wanted.includes(value); |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
* Put every conditional region where its condition says it belongs. |
| 93 |
* |
| 94 |
* A walk over the document rather than over a list captured when the page |
| 95 |
* loaded, for the reason the clock gives: a swap brings new regions and |
| 96 |
* takes old ones away, and both are found by the next pass. |
| 97 |
|
| 98 |
const settle = () => { |
| 99 |
for (const region of document.querySelectorAll(`[${CONTROL}]`)) { |
| 100 |
const value = held(region, region.getAttribute(CONTROL) ?? ""); |
| 101 |
region.hidden = !satisfied(region, value); |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
document.addEventListener("change", settle); |
| 109 |
document.addEventListener("input", settle); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
document.addEventListener("htmx:after:settle", settle); |
| 118 |
if (document.readyState === "loading") { |
| 119 |
document.addEventListener("DOMContentLoaded", settle); |
| 120 |
} else { |
| 121 |
settle(); |
| 122 |
} |
| 123 |
})(); |
| 124 |
|