| 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 NAME = "data-repeat"; |
| 27 |
|
| 28 |
const LABEL = "data-repeat-label"; |
| 29 |
|
| 30 |
const LEAST = "data-repeat-least"; |
| 31 |
|
| 32 |
const MOST = "data-repeat-most"; |
| 33 |
|
| 34 |
const SLOTS = "data-repeat-slots"; |
| 35 |
|
| 36 |
const AT = "data-repeat-at"; |
| 37 |
|
| 38 |
const ADD = "data-repeat-add"; |
| 39 |
|
| 40 |
const REMOVE = "data-repeat-remove"; |
| 41 |
|
| 42 |
const FROM = "data-repeat-add-from"; |
| 43 |
|
| 44 |
const NAMED = "data-repeat-named"; |
| 45 |
|
| 46 |
|
| 47 |
const NAMED = ["name", "id", "for", "aria-describedby", "aria-labelledby"]; |
| 48 |
|
| 49 |
|
| 50 |
const groupOf = (element) => element.closest(`[${NAME}]`); |
| 51 |
|
| 52 |
|
| 53 |
const boxOf = (group) => group.querySelector(`[${SLOTS}]`); |
| 54 |
|
| 55 |
|
| 56 |
const slotsOf = (group) => [ |
| 57 |
...(boxOf(group)?.querySelectorAll(`:scope > [${AT}]`) ?? []), |
| 58 |
]; |
| 59 |
|
| 60 |
|
| 61 |
const count = (group, attribute, fallback) => { |
| 62 |
const written = group.getAttribute(attribute); |
| 63 |
if (written === null) { |
| 64 |
return fallback; |
| 65 |
} |
| 66 |
const value = Number.parseInt(written, 10); |
| 67 |
return Number.isNaN(value) ? fallback : value; |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
* Move one slot to a place in the order. |
| 72 |
* |
| 73 |
* The index is in the slot's own attribute, in every name it submits under |
| 74 |
* and in the ordinal a person reads. All three are rewritten together, or a |
| 75 |
* removed slot leaves the ones after it answering under names nobody asked |
| 76 |
* for. |
| 77 |
* |
| 78 |
* The rename is a replacement of `question[old]` wherever it appears, which |
| 79 |
* covers the derived ids beside the name itself -- `question[0]-hint`, |
| 80 |
* `question[0]-error` -- without this having to know what the field |
| 81 |
* emitter derives. |
| 82 |
|
| 83 |
const renumber = (slot, question, label, to) => { |
| 84 |
const from = slot.getAttribute(AT); |
| 85 |
slot.setAttribute(AT, String(to)); |
| 86 |
if (from === null) { |
| 87 |
return; |
| 88 |
} |
| 89 |
const was = `${question}[${from}]`; |
| 90 |
const now = `${question}[${to}]`; |
| 91 |
if (was !== now) { |
| 92 |
for (const element of [slot, ...slot.querySelectorAll("*")]) { |
| 93 |
for (const attribute of NAMED) { |
| 94 |
const value = element.getAttribute(attribute); |
| 95 |
if (value !== null && value.includes(was)) { |
| 96 |
element.setAttribute(attribute, value.split(was).join(now)); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
if (label !== null && !slot.hasAttribute(NAMED)) { |
| 109 |
const named = slot.querySelector("label[for]"); |
| 110 |
if (named !== null) { |
| 111 |
named.textContent = `${label} ${to + 1}`; |
| 112 |
} |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
* Put a group's slots and its two controls back in agreement. |
| 118 |
* |
| 119 |
* Called after every change and on arrival, so a group the reader has not |
| 120 |
* touched is in the same state as one they have. |
| 121 |
|
| 122 |
const settle = (group) => { |
| 123 |
const question = group.getAttribute(NAME) ?? ""; |
| 124 |
const label = group.getAttribute(LABEL); |
| 125 |
const slots = slotsOf(group); |
| 126 |
const least = count(group, LEAST, 0); |
| 127 |
const most = count(group, MOST, Number.POSITIVE_INFINITY); |
| 128 |
slots.forEach((slot, at) => renumber(slot, question, label, at)); |
| 129 |
for (const control of group.querySelectorAll(`[${REMOVE}]`)) { |
| 130 |
control.disabled = slots.length <= least; |
| 131 |
} |
| 132 |
for (const control of group.querySelectorAll(`[${ADD}]`)) { |
| 133 |
control.disabled = slots.length >= most; |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
const settleAll = () => { |
| 139 |
for (const group of document.querySelectorAll(`[${NAME}]`)) { |
| 140 |
settle(group); |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
* Clone the blank slot onto the end, if the ceiling allows another. |
| 146 |
* |
| 147 |
* `focus` is false for a slot nobody asked for by pressing add: a picker |
| 148 |
* that made seven of them would otherwise leave the caret in the last. |
| 149 |
* Answers the new slot, or null when the ceiling refused it. |
| 150 |
|
| 151 |
const add = (group, focus = true) => { |
| 152 |
const blank = group.querySelector("template"); |
| 153 |
const box = boxOf(group); |
| 154 |
if (blank === null || box === null) { |
| 155 |
return; |
| 156 |
} |
| 157 |
const most = count(group, MOST, Number.POSITIVE_INFINITY); |
| 158 |
if (slotsOf(group).length >= most) { |
| 159 |
return; |
| 160 |
} |
| 161 |
const made = blank.content.cloneNode(true); |
| 162 |
const slot = made.firstElementChild; |
| 163 |
box.append(made); |
| 164 |
settle(group); |
| 165 |
if (focus) { |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
slot?.querySelector("input, select, textarea")?.focus(); |
| 170 |
} |
| 171 |
return slot ?? null; |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
const remove = (group, slot) => { |
| 176 |
const least = count(group, LEAST, 0); |
| 177 |
if (slotsOf(group).length <= least) { |
| 178 |
return; |
| 179 |
} |
| 180 |
slot.remove(); |
| 181 |
settle(group); |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
const sourceOf = (group) => { |
| 186 |
const name = group.getAttribute(FROM); |
| 187 |
if (name === null) { |
| 188 |
return null; |
| 189 |
} |
| 190 |
return ( |
| 191 |
document.getElementById(name) ?? |
| 192 |
document.querySelector(`[name="${CSS.escape(name)}"]`) |
| 193 |
); |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
* A slot per file the reader picked, for a question whose slots come from |
| 198 |
* another control. |
| 199 |
* |
| 200 |
* What goes *in* the slot is not this file's business: only the control |
| 201 |
* that made it knows what a file is called or what to guess for it. Each |
| 202 |
* slot is announced as it arrives and whoever owns the surface fills it. |
| 203 |
|
| 204 |
const took = (group, source) => { |
| 205 |
const files = [...(source.files ?? [])]; |
| 206 |
for (const [at, file] of files.entries()) { |
| 207 |
const slot = add(group, false); |
| 208 |
if (slot === null) { |
| 209 |
|
| 210 |
|
| 211 |
break; |
| 212 |
} |
| 213 |
slot.dispatchEvent( |
| 214 |
new CustomEvent("quasi:repeat:took", { |
| 215 |
bubbles: true, |
| 216 |
detail: { slot, file, at, source }, |
| 217 |
}), |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
source.value = ""; |
| 223 |
}; |
| 224 |
|
| 225 |
document.addEventListener("change", (event) => { |
| 226 |
const target = event.target; |
| 227 |
if (!(target instanceof Element)) { |
| 228 |
return; |
| 229 |
} |
| 230 |
for (const group of document.querySelectorAll(`[${FROM}]`)) { |
| 231 |
if (sourceOf(group) === target) { |
| 232 |
took(group, target); |
| 233 |
} |
| 234 |
} |
| 235 |
}); |
| 236 |
|
| 237 |
|
| 238 |
document.addEventListener("click", (event) => { |
| 239 |
const target = event.target; |
| 240 |
if (!(target instanceof Element)) { |
| 241 |
return; |
| 242 |
} |
| 243 |
const adder = target.closest(`[${ADD}]`); |
| 244 |
if (adder !== null) { |
| 245 |
const group = groupOf(adder); |
| 246 |
if (group !== null) { |
| 247 |
event.preventDefault(); |
| 248 |
add(group); |
| 249 |
} |
| 250 |
return; |
| 251 |
} |
| 252 |
const remover = target.closest(`[${REMOVE}]`); |
| 253 |
if (remover === null) { |
| 254 |
return; |
| 255 |
} |
| 256 |
const group = groupOf(remover); |
| 257 |
const slot = remover.closest(`[${AT}]`); |
| 258 |
if (group !== null && slot !== null) { |
| 259 |
event.preventDefault(); |
| 260 |
remove(group, slot); |
| 261 |
} |
| 262 |
}); |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
document.addEventListener("htmx:after:settle", settleAll); |
| 268 |
if (document.readyState === "loading") { |
| 269 |
document.addEventListener("DOMContentLoaded", settleAll); |
| 270 |
} else { |
| 271 |
settleAll(); |
| 272 |
} |
| 273 |
})(); |
| 274 |
|