| 1 | /** |
| 2 | * Searchable combobox for editor dimension picking. |
| 3 | * options: { id, label, disabled?, hint? }[] |
| 4 | */ |
| 5 | export function createDimensionCombobox ({ |
| 6 | root, |
| 7 | allowCreate = false, |
| 8 | allowUnknown = false, |
| 9 | placeholder = "Dimension…", |
| 10 | emptyLabel = "No matches", |
| 11 | onSelect, |
| 12 | onCreate |
| 13 | }) |
| 14 | { |
| 15 | root.classList.add("dim-combo"); |
| 16 | root.innerHTML = ""; |
| 17 | |
| 18 | const input = document.createElement("input"); |
| 19 | input.type = "text"; |
| 20 | input.className = "dim-combo-input"; |
| 21 | input.placeholder = placeholder; |
| 22 | input.autocomplete = "off"; |
| 23 | input.setAttribute("role", "combobox"); |
| 24 | input.setAttribute("aria-autocomplete", "list"); |
| 25 | input.setAttribute("aria-expanded", "false"); |
| 26 | |
| 27 | const toggle = document.createElement("button"); |
| 28 | toggle.type = "button"; |
| 29 | toggle.className = "dim-combo-toggle"; |
| 30 | toggle.tabIndex = -1; |
| 31 | toggle.setAttribute("aria-label", "Show dimensions"); |
| 32 | toggle.textContent = "▾"; |
| 33 | |
| 34 | const list = document.createElement("ul"); |
| 35 | list.className = "dim-combo-list"; |
| 36 | list.hidden = true; |
| 37 | list.setAttribute("role", "listbox"); |
| 38 | |
| 39 | root.append(input, toggle, list); |
| 40 | |
| 41 | /** @type {{ id: string, label: string, disabled?: boolean, hint?: string }[]} */ |
| 42 | let options = []; |
| 43 | let selectedId = null; |
| 44 | let open = false; |
| 45 | |
| 46 | function isKnownId (id) |
| 47 | { |
| 48 | return !!id && options.some((opt) => opt.id === id); |
| 49 | } |
| 50 | |
| 51 | function updateValidity () |
| 52 | { |
| 53 | const invalid = allowUnknown && !!selectedId && !isKnownId(selectedId); |
| 54 | root.classList.toggle("dim-combo-invalid", invalid); |
| 55 | if (invalid) |
| 56 | { |
| 57 | root.classList.remove("dim-combo-invalid-pulse"); |
| 58 | void root.offsetWidth; |
| 59 | root.classList.add("dim-combo-invalid-pulse"); |
| 60 | } |
| 61 | else |
| 62 | { root.classList.remove("dim-combo-invalid-pulse"); } |
| 63 | } |
| 64 | |
| 65 | function selectedOption () |
| 66 | { |
| 67 | return options.find((opt) => opt.id === selectedId) ?? null; |
| 68 | } |
| 69 | |
| 70 | function displayLabel (opt) |
| 71 | { |
| 72 | if (!opt) |
| 73 | { return ""; } |
| 74 | return opt.label && opt.label !== opt.id ? `${opt.label} (${opt.id})` : opt.id; |
| 75 | } |
| 76 | |
| 77 | function syncInputFromSelection () |
| 78 | { |
| 79 | const opt = selectedOption(); |
| 80 | input.value = opt ? displayLabel(opt) : (selectedId ?? ""); |
| 81 | } |
| 82 | |
| 83 | function setOpen (next) |
| 84 | { |
| 85 | open = next; |
| 86 | list.hidden = !open; |
| 87 | input.setAttribute("aria-expanded", open ? "true" : "false"); |
| 88 | if (open) |
| 89 | { renderList(); } |
| 90 | } |
| 91 | |
| 92 | function filtered () |
| 93 | { |
| 94 | const q = input.value.trim().toLowerCase(); |
| 95 | if (!q) |
| 96 | { return options.slice(); } |
| 97 | return options.filter((opt) => |
| 98 | opt.id.toLowerCase().includes(q) |
| 99 | || (opt.label ?? "").toLowerCase().includes(q) |
| 100 | || (opt.hint ?? "").toLowerCase().includes(q) |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | function exactMatchId (text) |
| 105 | { |
| 106 | const q = String(text ?? "").trim().toLowerCase(); |
| 107 | if (!q) |
| 108 | { return null; } |
| 109 | const hit = options.find((opt) => opt.id.toLowerCase() === q || (opt.label ?? "").toLowerCase() === q); |
| 110 | return hit?.id ?? null; |
| 111 | } |
| 112 | |
| 113 | function renderList () |
| 114 | { |
| 115 | list.innerHTML = ""; |
| 116 | const rows = filtered(); |
| 117 | const typed = input.value.trim(); |
| 118 | const typedLower = typed.toLowerCase(); |
| 119 | const hasExact = options.some((opt) => |
| 120 | opt.id.toLowerCase() === typedLower || (opt.label ?? "").toLowerCase() === typedLower |
| 121 | ); |
| 122 | |
| 123 | for (const opt of rows) |
| 124 | { |
| 125 | const li = document.createElement("li"); |
| 126 | li.className = "dim-combo-option"; |
| 127 | if (opt.disabled) |
| 128 | { li.classList.add("disabled"); } |
| 129 | if (opt.id === selectedId) |
| 130 | { li.classList.add("selected"); } |
| 131 | li.setAttribute("role", "option"); |
| 132 | li.dataset.id = opt.id; |
| 133 | const main = document.createElement("span"); |
| 134 | main.className = "dim-combo-option-label"; |
| 135 | main.textContent = displayLabel(opt); |
| 136 | li.append(main); |
| 137 | if (opt.hint) |
| 138 | { |
| 139 | const hint = document.createElement("span"); |
| 140 | hint.className = "dim-combo-option-hint"; |
| 141 | hint.textContent = opt.hint; |
| 142 | li.append(hint); |
| 143 | } |
| 144 | li.addEventListener("mousedown", (event) => |
| 145 | { |
| 146 | event.preventDefault(); |
| 147 | choose(opt.id); |
| 148 | }); |
| 149 | list.append(li); |
| 150 | } |
| 151 | |
| 152 | if (allowCreate && typed && !hasExact) |
| 153 | { |
| 154 | const li = document.createElement("li"); |
| 155 | li.className = "dim-combo-option create"; |
| 156 | li.setAttribute("role", "option"); |
| 157 | li.textContent = `Create «${typed}»`; |
| 158 | li.addEventListener("mousedown", (event) => |
| 159 | { |
| 160 | event.preventDefault(); |
| 161 | setOpen(false); |
| 162 | onCreate?.(typed); |
| 163 | }); |
| 164 | list.append(li); |
| 165 | } |
| 166 | |
| 167 | if (!list.children.length) |
| 168 | { |
| 169 | const empty = document.createElement("li"); |
| 170 | empty.className = "dim-combo-empty"; |
| 171 | empty.textContent = emptyLabel; |
| 172 | list.append(empty); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | function choose (id) |
| 177 | { |
| 178 | selectedId = id; |
| 179 | syncInputFromSelection(); |
| 180 | updateValidity(); |
| 181 | setOpen(false); |
| 182 | onSelect?.(id); |
| 183 | } |
| 184 | |
| 185 | input.addEventListener("focus", () => |
| 186 | { |
| 187 | setOpen(true); |
| 188 | }); |
| 189 | input.addEventListener("mousedown", (event) => |
| 190 | { |
| 191 | if (!open) |
| 192 | { |
| 193 | event.preventDefault(); |
| 194 | setOpen(true); |
| 195 | input.focus(); |
| 196 | } |
| 197 | }); |
| 198 | toggle.addEventListener("mousedown", (event) => |
| 199 | { |
| 200 | event.preventDefault(); |
| 201 | if (open) |
| 202 | { |
| 203 | syncInputFromSelection(); |
| 204 | setOpen(false); |
| 205 | input.blur(); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | setOpen(true); |
| 210 | input.focus(); |
| 211 | } |
| 212 | }); |
| 213 | input.addEventListener("input", () => |
| 214 | { |
| 215 | setOpen(true); |
| 216 | renderList(); |
| 217 | }); |
| 218 | input.addEventListener("keydown", (event) => |
| 219 | { |
| 220 | if (event.key === "Escape") |
| 221 | { |
| 222 | syncInputFromSelection(); |
| 223 | setOpen(false); |
| 224 | input.blur(); |
| 225 | return; |
| 226 | } |
| 227 | if (event.key === "Enter") |
| 228 | { |
| 229 | event.preventDefault(); |
| 230 | const typed = input.value.trim(); |
| 231 | const exact = exactMatchId(typed); |
| 232 | if (exact) |
| 233 | { |
| 234 | choose(exact); |
| 235 | return; |
| 236 | } |
| 237 | const rows = filtered().filter((opt) => !opt.disabled); |
| 238 | if (rows.length === 1) |
| 239 | { |
| 240 | choose(rows[0].id); |
| 241 | return; |
| 242 | } |
| 243 | if (allowCreate && typed && !exactMatchId(typed)) |
| 244 | { |
| 245 | setOpen(false); |
| 246 | onCreate?.(typed); |
| 247 | } |
| 248 | } |
| 249 | }); |
| 250 | input.addEventListener("input", () => |
| 251 | { |
| 252 | if (!allowUnknown) |
| 253 | { return; } |
| 254 | const typed = input.value.trim(); |
| 255 | const exact = exactMatchId(typed); |
| 256 | selectedId = exact ?? (typed || null); |
| 257 | updateValidity(); |
| 258 | onSelect?.(selectedId); |
| 259 | }); |
| 260 | input.addEventListener("blur", () => |
| 261 | { |
| 262 | window.setTimeout(() => |
| 263 | { |
| 264 | if (!root.contains(document.activeElement)) |
| 265 | { |
| 266 | if (allowUnknown) |
| 267 | { |
| 268 | const typed = input.value.trim(); |
| 269 | const exact = exactMatchId(typed); |
| 270 | if (exact) |
| 271 | { choose(exact); } |
| 272 | else if (typed) |
| 273 | { |
| 274 | selectedId = typed; |
| 275 | input.value = typed; |
| 276 | updateValidity(); |
| 277 | onSelect?.(typed); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | selectedId = null; |
| 282 | input.value = ""; |
| 283 | updateValidity(); |
| 284 | onSelect?.(null); |
| 285 | } |
| 286 | } |
| 287 | else |
| 288 | { syncInputFromSelection(); } |
| 289 | setOpen(false); |
| 290 | } |
| 291 | }, 0); |
| 292 | }); |
| 293 | |
| 294 | return { |
| 295 | setOptions (nextOptions) |
| 296 | { |
| 297 | options = Array.isArray(nextOptions) ? nextOptions.slice() : []; |
| 298 | if (selectedId && !options.some((opt) => opt.id === selectedId) && !allowUnknown) |
| 299 | { selectedId = options[0]?.id ?? null; } |
| 300 | syncInputFromSelection(); |
| 301 | updateValidity(); |
| 302 | if (open) |
| 303 | { renderList(); } |
| 304 | }, |
| 305 | setValue (id) |
| 306 | { |
| 307 | selectedId = id; |
| 308 | syncInputFromSelection(); |
| 309 | updateValidity(); |
| 310 | if (open) |
| 311 | { renderList(); } |
| 312 | }, |
| 313 | getValue () |
| 314 | { |
| 315 | return selectedId; |
| 316 | }, |
| 317 | close () |
| 318 | { |
| 319 | setOpen(false); |
| 320 | } |
| 321 | }; |
| 322 | } |