BlueGit

timfa questward

Files

questward / client / src / editor / dimensionCombobox.js

dimensionCombobox.js

dimensionCombobox.js 5.1 KiB Download Raw
1/**
2 * Searchable combobox for editor dimension picking.
3 * options: { id, label, disabled?, hint? }[]
4 */
5export function createDimensionCombobox ({
6 root,
7 allowCreate = false,
8 placeholder = "Dimension…",
9 onSelect,
10 onCreate
11})
12{
13 root.classList.add("dim-combo");
14 root.innerHTML = "";
15
16 const input = document.createElement("input");
17 input.type = "text";
18 input.className = "dim-combo-input";
19 input.placeholder = placeholder;
20 input.autocomplete = "off";
21 input.setAttribute("role", "combobox");
22 input.setAttribute("aria-autocomplete", "list");
23 input.setAttribute("aria-expanded", "false");
24
25 const list = document.createElement("ul");
26 list.className = "dim-combo-list";
27 list.hidden = true;
28 list.setAttribute("role", "listbox");
29
30 root.append(input, list);
31
32 /** @type {{ id: string, label: string, disabled?: boolean, hint?: string }[]} */
33 let options = [];
34 let selectedId = null;
35 let open = false;
36
37 function selectedOption ()
38 {
39 return options.find((opt) => opt.id === selectedId) ?? null;
40 }
41
42 function displayLabel (opt)
43 {
44 if (!opt)
45 { return ""; }
46 return opt.label && opt.label !== opt.id ? `${opt.label} (${opt.id})` : opt.id;
47 }
48
49 function syncInputFromSelection ()
50 {
51 const opt = selectedOption();
52 input.value = opt ? displayLabel(opt) : (selectedId ?? "");
53 }
54
55 function setOpen (next)
56 {
57 open = next;
58 list.hidden = !open;
59 input.setAttribute("aria-expanded", open ? "true" : "false");
60 if (open)
61 { renderList(); }
62 }
63
64 function filtered ()
65 {
66 const q = input.value.trim().toLowerCase();
67 if (!q)
68 { return options.slice(); }
69 return options.filter((opt) =>
70 opt.id.toLowerCase().includes(q)
71 || (opt.label ?? "").toLowerCase().includes(q)
72 || (opt.hint ?? "").toLowerCase().includes(q)
73 );
74 }
75
76 function exactMatchId (text)
77 {
78 const q = String(text ?? "").trim().toLowerCase();
79 if (!q)
80 { return null; }
81 const hit = options.find((opt) => opt.id.toLowerCase() === q || (opt.label ?? "").toLowerCase() === q);
82 return hit?.id ?? null;
83 }
84
85 function renderList ()
86 {
87 list.innerHTML = "";
88 const rows = filtered();
89 const typed = input.value.trim();
90 const typedLower = typed.toLowerCase();
91 const hasExact = options.some((opt) =>
92 opt.id.toLowerCase() === typedLower || (opt.label ?? "").toLowerCase() === typedLower
93 );
94
95 for (const opt of rows)
96 {
97 const li = document.createElement("li");
98 li.className = "dim-combo-option";
99 if (opt.disabled)
100 { li.classList.add("disabled"); }
101 if (opt.id === selectedId)
102 { li.classList.add("selected"); }
103 li.setAttribute("role", "option");
104 li.dataset.id = opt.id;
105 const main = document.createElement("span");
106 main.className = "dim-combo-option-label";
107 main.textContent = displayLabel(opt);
108 li.append(main);
109 if (opt.hint)
110 {
111 const hint = document.createElement("span");
112 hint.className = "dim-combo-option-hint";
113 hint.textContent = opt.hint;
114 li.append(hint);
115 }
116 li.addEventListener("mousedown", (event) =>
117 {
118 event.preventDefault();
119 choose(opt.id);
120 });
121 list.append(li);
122 }
123
124 if (allowCreate && typed && !hasExact)
125 {
126 const li = document.createElement("li");
127 li.className = "dim-combo-option create";
128 li.setAttribute("role", "option");
129 li.textContent = `Create «${typed}»`;
130 li.addEventListener("mousedown", (event) =>
131 {
132 event.preventDefault();
133 setOpen(false);
134 onCreate?.(typed);
135 });
136 list.append(li);
137 }
138
139 if (!list.children.length)
140 {
141 const empty = document.createElement("li");
142 empty.className = "dim-combo-empty";
143 empty.textContent = "No dimensions";
144 list.append(empty);
145 }
146 }
147
148 function choose (id)
149 {
150 selectedId = id;
151 syncInputFromSelection();
152 setOpen(false);
153 onSelect?.(id);
154 }
155
156 input.addEventListener("focus", () =>
157 {
158 input.select();
159 setOpen(true);
160 });
161 input.addEventListener("input", () =>
162 {
163 setOpen(true);
164 renderList();
165 });
166 input.addEventListener("keydown", (event) =>
167 {
168 if (event.key === "Escape")
169 {
170 syncInputFromSelection();
171 setOpen(false);
172 input.blur();
173 return;
174 }
175 if (event.key === "Enter")
176 {
177 event.preventDefault();
178 const typed = input.value.trim();
179 const exact = exactMatchId(typed);
180 if (exact)
181 {
182 choose(exact);
183 return;
184 }
185 const rows = filtered().filter((opt) => !opt.disabled);
186 if (rows.length === 1)
187 {
188 choose(rows[0].id);
189 return;
190 }
191 if (allowCreate && typed && !exactMatchId(typed))
192 {
193 setOpen(false);
194 onCreate?.(typed);
195 }
196 }
197 });
198 input.addEventListener("blur", () =>
199 {
200 window.setTimeout(() =>
201 {
202 if (!root.contains(document.activeElement))
203 {
204 syncInputFromSelection();
205 setOpen(false);
206 }
207 }, 0);
208 });
209
210 return {
211 setOptions (nextOptions)
212 {
213 options = Array.isArray(nextOptions) ? nextOptions.slice() : [];
214 if (selectedId && !options.some((opt) => opt.id === selectedId))
215 { selectedId = options[0]?.id ?? null; }
216 syncInputFromSelection();
217 if (open)
218 { renderList(); }
219 },
220 setValue (id)
221 {
222 selectedId = id;
223 syncInputFromSelection();
224 if (open)
225 { renderList(); }
226 },
227 getValue ()
228 {
229 return selectedId;
230 },
231 close ()
232 {
233 setOpen(false);
234 }
235 };
236}