| 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 |
|
| 43 |
const NAMED = ["name", "id", "for", "aria-describedby", "aria-labelledby"]; |
| 44 |
|
| 45 |
|
| 46 |
const groupOf = (element) => element.closest(`[${NAME}]`); |
| 47 |
|
| 48 |
|
| 49 |
const boxOf = (group) => group.querySelector(`[${SLOTS}]`); |
| 50 |
|
| 51 |
|
| 52 |
const slotsOf = (group) => [ |
| 53 |
...(boxOf(group)?.querySelectorAll(`:scope > [${AT}]`) ?? []), |
| 54 |
]; |
| 55 |
|
| 56 |
|
| 57 |
const count = (group, attribute, fallback) => { |
| 58 |
const written = group.getAttribute(attribute); |
| 59 |
if (written === null) { |
| 60 |
return fallback; |
| 61 |
} |
| 62 |
const value = Number.parseInt(written, 10); |
| 63 |
return Number.isNaN(value) ? fallback : value; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
* Move one slot to a place in the order. |
| 68 |
* |
| 69 |
* The index is in the slot's own attribute, in every name it submits under |
| 70 |
* and in the ordinal a person reads. All three are rewritten together, or a |
| 71 |
* removed slot leaves the ones after it answering under names nobody asked |
| 72 |
* for. |
| 73 |
* |
| 74 |
* The rename is a replacement of `question[old]` wherever it appears, which |
| 75 |
* covers the derived ids beside the name itself -- `question[0]-hint`, |
| 76 |
* `question[0]-error` -- without this having to know what the field |
| 77 |
* emitter derives. |
| 78 |
|
| 79 |
const renumber = (slot, question, label, to) => { |
| 80 |
const from = slot.getAttribute(AT); |
| 81 |
slot.setAttribute(AT, String(to)); |
| 82 |
if (from === null) { |
| 83 |
return; |
| 84 |
} |
| 85 |
const was = `${question}[${from}]`; |
| 86 |
const now = `${question}[${to}]`; |
| 87 |
if (was !== now) { |
| 88 |
for (const element of [slot, ...slot.querySelectorAll("*")]) { |
| 89 |
for (const attribute of NAMED) { |
| 90 |
const value = element.getAttribute(attribute); |
| 91 |
if (value !== null && value.includes(was)) { |
| 92 |
element.setAttribute(attribute, value.split(was).join(now)); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if (label !== null) { |
| 102 |
const named = slot.querySelector("label[for]"); |
| 103 |
if (named !== null) { |
| 104 |
named.textContent = `${label} ${to + 1}`; |
| 105 |
} |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
* Put a group's slots and its two controls back in agreement. |
| 111 |
* |
| 112 |
* Called after every change and on arrival, so a group the reader has not |
| 113 |
* touched is in the same state as one they have. |
| 114 |
|
| 115 |
const settle = (group) => { |
| 116 |
const question = group.getAttribute(NAME) ?? ""; |
| 117 |
const label = group.getAttribute(LABEL); |
| 118 |
const slots = slotsOf(group); |
| 119 |
const least = count(group, LEAST, 0); |
| 120 |
const most = count(group, MOST, Number.POSITIVE_INFINITY); |
| 121 |
slots.forEach((slot, at) => renumber(slot, question, label, at)); |
| 122 |
for (const control of group.querySelectorAll(`[${REMOVE}]`)) { |
| 123 |
control.disabled = slots.length <= least; |
| 124 |
} |
| 125 |
for (const control of group.querySelectorAll(`[${ADD}]`)) { |
| 126 |
control.disabled = slots.length >= most; |
| 127 |
} |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
const settleAll = () => { |
| 132 |
for (const group of document.querySelectorAll(`[${NAME}]`)) { |
| 133 |
settle(group); |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
const add = (group) => { |
| 139 |
const blank = group.querySelector("template"); |
| 140 |
const box = boxOf(group); |
| 141 |
if (blank === null || box === null) { |
| 142 |
return; |
| 143 |
} |
| 144 |
const most = count(group, MOST, Number.POSITIVE_INFINITY); |
| 145 |
if (slotsOf(group).length >= most) { |
| 146 |
return; |
| 147 |
} |
| 148 |
const made = blank.content.cloneNode(true); |
| 149 |
const slot = made.firstElementChild; |
| 150 |
box.append(made); |
| 151 |
settle(group); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
slot?.querySelector("input, select, textarea")?.focus(); |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
const remove = (group, slot) => { |
| 160 |
const least = count(group, LEAST, 0); |
| 161 |
if (slotsOf(group).length <= least) { |
| 162 |
return; |
| 163 |
} |
| 164 |
slot.remove(); |
| 165 |
settle(group); |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
document.addEventListener("click", (event) => { |
| 170 |
const target = event.target; |
| 171 |
if (!(target instanceof Element)) { |
| 172 |
return; |
| 173 |
} |
| 174 |
const adder = target.closest(`[${ADD}]`); |
| 175 |
if (adder !== null) { |
| 176 |
const group = groupOf(adder); |
| 177 |
if (group !== null) { |
| 178 |
event.preventDefault(); |
| 179 |
add(group); |
| 180 |
} |
| 181 |
return; |
| 182 |
} |
| 183 |
const remover = target.closest(`[${REMOVE}]`); |
| 184 |
if (remover === null) { |
| 185 |
return; |
| 186 |
} |
| 187 |
const group = groupOf(remover); |
| 188 |
const slot = remover.closest(`[${AT}]`); |
| 189 |
if (group !== null && slot !== null) { |
| 190 |
event.preventDefault(); |
| 191 |
remove(group, slot); |
| 192 |
} |
| 193 |
}); |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
document.addEventListener("htmx:after:settle", settleAll); |
| 199 |
if (document.readyState === "loading") { |
| 200 |
document.addEventListener("DOMContentLoaded", settleAll); |
| 201 |
} else { |
| 202 |
settleAll(); |
| 203 |
} |
| 204 |
})(); |
| 205 |
|