@viewstem/ui 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ko.md +47 -0
- package/README.md +60 -2
- package/dist/BASECOAT-LICENSE.md +21 -0
- package/dist/basecoat-contracts.d.ts +2 -0
- package/dist/basecoat-contracts.js +358 -0
- package/dist/basecoat-controllers.js +1612 -0
- package/dist/behaviors.d.ts +29 -0
- package/dist/behaviors.js +1475 -0
- package/dist/browser.d.ts +2 -0
- package/dist/browser.js +2 -0
- package/dist/catalog-meta.d.ts +26 -0
- package/dist/catalog-meta.js +130 -0
- package/dist/component-context.d.ts +7 -0
- package/dist/component-context.js +10 -0
- package/dist/components.d.ts +2 -0
- package/dist/components.js +536 -0
- package/dist/data-table.d.ts +2 -0
- package/dist/data-table.js +260 -0
- package/dist/date-calendar.d.ts +4 -0
- package/dist/date-calendar.js +512 -0
- package/dist/dynamic-catalog.d.ts +2 -0
- package/dist/dynamic-catalog.js +118 -0
- package/dist/form-scroll-families.d.ts +7 -0
- package/dist/form-scroll-families.js +895 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +12 -0
- package/dist/markup-families.d.ts +10 -0
- package/dist/markup-families.js +492 -0
- package/dist/menus.d.ts +8 -0
- package/dist/menus.js +427 -0
- package/dist/shadcn.d.ts +13 -0
- package/dist/shadcn.js +8 -0
- package/dist/slider-values.d.ts +9 -0
- package/dist/slider-values.js +43 -0
- package/dist/slider.d.ts +2 -0
- package/dist/slider.js +148 -0
- package/dist/static-catalog.d.ts +2 -0
- package/dist/static-catalog.js +87 -0
- package/dist/style-scope.d.ts +2 -0
- package/dist/style-scope.js +3 -0
- package/dist/styles.css +9573 -0
- package/dist/styles.css.d.ts +1 -0
- package/dist/support.json +1 -0
- package/dist/widget-families.d.ts +6 -0
- package/dist/widget-families.js +739 -0
- package/package.json +59 -4
|
@@ -0,0 +1,1612 @@
|
|
|
1
|
+
// Basecoat MIT. See BASECOAT-LICENSE.md.
|
|
2
|
+
export function registerControllers(window, document, CustomEvent, getComputedStyle) {
|
|
3
|
+
(() => {
|
|
4
|
+
const states = new WeakMap();
|
|
5
|
+
|
|
6
|
+
const isDisabled = (details) => {
|
|
7
|
+
const summary = details.querySelector(':scope > summary');
|
|
8
|
+
return details.getAttribute('aria-disabled') === 'true'
|
|
9
|
+
|| details.dataset.disabled === 'true'
|
|
10
|
+
|| summary?.getAttribute('aria-disabled') === 'true';
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const isMultiple = (root) => root.hasAttribute('data-multiple');
|
|
14
|
+
|
|
15
|
+
const closeSiblings = (root, activeDetails) => {
|
|
16
|
+
if (isMultiple(root) || !activeDetails.open) return;
|
|
17
|
+
root.querySelectorAll(':scope > details[open]').forEach((details) => {
|
|
18
|
+
if (details !== activeDetails) details.open = false;
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const initAccordion = (root) => {
|
|
23
|
+
if (root.dataset.accordionInitialized) return;
|
|
24
|
+
|
|
25
|
+
const handleClick = (event) => {
|
|
26
|
+
const summary = event.target.closest('summary');
|
|
27
|
+
const details = summary?.closest('details');
|
|
28
|
+
if (!details || details.parentElement !== root || !isDisabled(details)) return;
|
|
29
|
+
event.preventDefault();
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const handleKeydown = (event) => {
|
|
33
|
+
if (event.key !== 'Enter' && event.key !== ' ') return;
|
|
34
|
+
const summary = event.target.closest('summary');
|
|
35
|
+
const details = summary?.closest('details');
|
|
36
|
+
if (!details || details.parentElement !== root || !isDisabled(details)) return;
|
|
37
|
+
event.preventDefault();
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const handleToggle = (event) => {
|
|
41
|
+
const details = event.target;
|
|
42
|
+
if (details.parentElement !== root) return;
|
|
43
|
+
if (isDisabled(details)) {
|
|
44
|
+
details.open = false;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
closeSiblings(root, details);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
root.addEventListener('click', handleClick);
|
|
51
|
+
root.addEventListener('keydown', handleKeydown);
|
|
52
|
+
root.addEventListener('toggle', handleToggle, true);
|
|
53
|
+
root.querySelectorAll(':scope > details[open]').forEach((details) => closeSiblings(root, details));
|
|
54
|
+
|
|
55
|
+
states.set(root, { handleClick, handleToggle });
|
|
56
|
+
root._destroy = () => {
|
|
57
|
+
root.removeEventListener('click', handleClick);
|
|
58
|
+
root.removeEventListener('keydown', handleKeydown);
|
|
59
|
+
root.removeEventListener('toggle', handleToggle, true);
|
|
60
|
+
states.delete(root);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
root.dataset.accordionInitialized = 'true';
|
|
64
|
+
root.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
if (window.basecoat) {
|
|
68
|
+
window.basecoat.register('accordion', '.accordion:not([data-accordion-initialized])', initAccordion);
|
|
69
|
+
}
|
|
70
|
+
})();
|
|
71
|
+
|
|
72
|
+
(() => {
|
|
73
|
+
const states = new WeakMap();
|
|
74
|
+
|
|
75
|
+
const getElements = (root) => {
|
|
76
|
+
const popover = root.querySelector(':scope > [data-popover]');
|
|
77
|
+
const input = root.querySelector(':scope > input[role="combobox"], :scope > .input-group input[role="combobox"], :scope > .combobox-chips input[role="combobox"]')
|
|
78
|
+
|| popover?.querySelector('input[role="combobox"]');
|
|
79
|
+
const chips = root.querySelector(':scope > .combobox-chips');
|
|
80
|
+
const popupTrigger = root.querySelector(':scope > button[aria-haspopup="listbox"]');
|
|
81
|
+
const trigger = popupTrigger || root.querySelector(':scope > .input-group button[aria-haspopup="listbox"]');
|
|
82
|
+
const clearButton = root.querySelector('[data-clear]');
|
|
83
|
+
const valueTarget = popupTrigger?.querySelector('[data-value]') || (popupTrigger?.matches('[data-value]') ? popupTrigger : null);
|
|
84
|
+
const listbox = popover ? popover.querySelector('[role="listbox"]') : null;
|
|
85
|
+
const hiddenInput = root.querySelector(':scope > input[type="hidden"]');
|
|
86
|
+
return { input, chips, trigger, clearButton, valueTarget, popover, listbox, hiddenInput };
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const ensureMultipleInputSurface = (root, elements) => {
|
|
90
|
+
if (elements.listbox?.getAttribute('aria-multiselectable') !== 'true' || elements.chips || !elements.input) return elements;
|
|
91
|
+
if (elements.input.parentElement !== root) return elements;
|
|
92
|
+
|
|
93
|
+
const chips = document.createElement('div');
|
|
94
|
+
chips.className = 'combobox-chips';
|
|
95
|
+
root.insertBefore(chips, elements.input);
|
|
96
|
+
chips.appendChild(elements.input);
|
|
97
|
+
|
|
98
|
+
return getElements(root);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const getValue = option => option.dataset.value ?? option.textContent.trim();
|
|
102
|
+
const getLabel = option => option.dataset.label || option.textContent.trim();
|
|
103
|
+
const getFormat = root => root.dataset.format === 'object' ? 'object' : 'value';
|
|
104
|
+
const isDisabled = option => option.getAttribute('aria-disabled') === 'true';
|
|
105
|
+
const toSelected = option => ({ value: getValue(option), label: getLabel(option) });
|
|
106
|
+
const normalizeEntry = entry => {
|
|
107
|
+
if (entry && typeof entry === 'object') {
|
|
108
|
+
const value = entry.value == null ? '' : String(entry.value);
|
|
109
|
+
return value ? { value, label: String(entry.label ?? entry.value) } : null;
|
|
110
|
+
}
|
|
111
|
+
const value = entry == null ? '' : String(entry);
|
|
112
|
+
return value ? { value, label: value } : null;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const getOptions = (listbox) => {
|
|
116
|
+
const allOptions = Array.from(listbox.querySelectorAll('[role="option"]'));
|
|
117
|
+
return {
|
|
118
|
+
allOptions,
|
|
119
|
+
options: allOptions.filter(option => !isDisabled(option)),
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const getSelection = (state) => Array.from(state.selected.values());
|
|
124
|
+
const getCanonicalValue = (state) => state.isMultiple ? getSelection(state).map(item => item.value) : (getSelection(state)[0]?.value || '');
|
|
125
|
+
const getSelectedDetail = (state) => state.isMultiple ? getSelection(state) : (getSelection(state)[0] || null);
|
|
126
|
+
|
|
127
|
+
const serializeSelection = (state) => {
|
|
128
|
+
const selected = getSelection(state);
|
|
129
|
+
if (state.format === 'object') {
|
|
130
|
+
return JSON.stringify(state.isMultiple ? selected : (selected[0] || null));
|
|
131
|
+
}
|
|
132
|
+
const value = selected.map(item => item.value);
|
|
133
|
+
return state.isMultiple ? JSON.stringify(value) : (value[0] || '');
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const parseStoredSelection = (storedValue, inputValue, state) => {
|
|
137
|
+
if (state.isMultiple) {
|
|
138
|
+
let parsed = [];
|
|
139
|
+
try {
|
|
140
|
+
parsed = JSON.parse(storedValue || '[]');
|
|
141
|
+
} catch (_) {
|
|
142
|
+
parsed = [];
|
|
143
|
+
}
|
|
144
|
+
if (!Array.isArray(parsed)) return [];
|
|
145
|
+
return parsed.map(item => {
|
|
146
|
+
const entry = normalizeEntry(state.format === 'object' ? item : { value: item, label: state.selected.get(String(item))?.label ?? item });
|
|
147
|
+
if (!entry) return null;
|
|
148
|
+
const option = state.options.find(opt => getValue(opt) === entry.value);
|
|
149
|
+
return option ? toSelected(option) : entry;
|
|
150
|
+
}).filter(Boolean);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (state.format === 'object') {
|
|
154
|
+
try {
|
|
155
|
+
const entry = normalizeEntry(JSON.parse(storedValue || 'null'));
|
|
156
|
+
if (!entry) return [];
|
|
157
|
+
const option = state.options.find(opt => getValue(opt) === entry.value);
|
|
158
|
+
return [option ? toSelected(option) : entry];
|
|
159
|
+
} catch (_) {
|
|
160
|
+
return [];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const value = storedValue || '';
|
|
165
|
+
if (!value) return [];
|
|
166
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
167
|
+
return [option ? toSelected(option) : { value, label: state.selected.get(value)?.label || inputValue || value }];
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const scrollOptionIntoListbox = (state, option) => {
|
|
171
|
+
const optionRect = option.getBoundingClientRect();
|
|
172
|
+
const listboxRect = state.listbox.getBoundingClientRect();
|
|
173
|
+
|
|
174
|
+
if (optionRect.top < listboxRect.top) {
|
|
175
|
+
state.listbox.scrollTop -= listboxRect.top - optionRect.top;
|
|
176
|
+
} else if (optionRect.bottom > listboxRect.bottom) {
|
|
177
|
+
state.listbox.scrollTop += optionRect.bottom - listboxRect.bottom;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const setActiveOption = (state, index) => {
|
|
182
|
+
if (state.activeIndex > -1 && state.options[state.activeIndex]) {
|
|
183
|
+
state.options[state.activeIndex].classList.remove('active');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
state.activeIndex = index;
|
|
187
|
+
|
|
188
|
+
if (state.activeIndex > -1) {
|
|
189
|
+
const activeOption = state.options[state.activeIndex];
|
|
190
|
+
activeOption.classList.add('active');
|
|
191
|
+
if (!activeOption.id) activeOption.id = `${state.listbox.id || state.root.id || 'combobox'}-option-${state.activeIndex}`;
|
|
192
|
+
state.input.setAttribute('aria-activedescendant', activeOption.id);
|
|
193
|
+
} else {
|
|
194
|
+
state.input.removeAttribute('aria-activedescendant');
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
const syncEmptyState = (state) => {
|
|
199
|
+
state.popover.dataset.empty = String(state.visibleOptions.length === 0);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const filterOptions = (state, { preserveActive = false, search: forcedSearch } = {}) => {
|
|
203
|
+
const previousActive = state.activeIndex > -1 ? state.options[state.activeIndex] : null;
|
|
204
|
+
state.visibleOptions = [];
|
|
205
|
+
|
|
206
|
+
if (state.manualFilter) {
|
|
207
|
+
state.visibleOptions = state.options.filter(option => option.getAttribute('aria-hidden') !== 'true');
|
|
208
|
+
|
|
209
|
+
if (preserveActive && previousActive && state.visibleOptions.includes(previousActive)) {
|
|
210
|
+
setActiveOption(state, state.options.indexOf(previousActive));
|
|
211
|
+
} else {
|
|
212
|
+
setActiveOption(state, state.autoHighlight && state.visibleOptions.length > 0 ? state.options.indexOf(state.visibleOptions[0]) : -1);
|
|
213
|
+
}
|
|
214
|
+
syncEmptyState(state);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const search = (forcedSearch ?? state.input.value).trim().toLowerCase();
|
|
219
|
+
|
|
220
|
+
state.allOptions.forEach(option => {
|
|
221
|
+
if (option.hasAttribute('data-force')) {
|
|
222
|
+
option.setAttribute('aria-hidden', 'false');
|
|
223
|
+
if (state.options.includes(option)) state.visibleOptions.push(option);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const optionText = (option.dataset.filter || option.dataset.label || option.textContent).trim().toLowerCase();
|
|
228
|
+
const keywords = (option.dataset.keywords || '').toLowerCase().split(/[\s,]+/).filter(Boolean);
|
|
229
|
+
const matches = !search || optionText.includes(search) || keywords.some(keyword => keyword.includes(search));
|
|
230
|
+
option.setAttribute('aria-hidden', String(!matches));
|
|
231
|
+
if (matches && state.options.includes(option)) state.visibleOptions.push(option);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
if (preserveActive && previousActive && state.visibleOptions.includes(previousActive)) {
|
|
235
|
+
setActiveOption(state, state.options.indexOf(previousActive));
|
|
236
|
+
} else {
|
|
237
|
+
setActiveOption(state, state.autoHighlight && state.visibleOptions.length > 0 ? state.options.indexOf(state.visibleOptions[0]) : -1);
|
|
238
|
+
}
|
|
239
|
+
syncEmptyState(state);
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const renderChips = (root) => {
|
|
243
|
+
const state = states.get(root);
|
|
244
|
+
if (!state.chips) return;
|
|
245
|
+
|
|
246
|
+
state.chips.querySelectorAll('.combobox-chip').forEach(chip => chip.remove());
|
|
247
|
+
|
|
248
|
+
getSelection(state).forEach(entry => {
|
|
249
|
+
const chip = document.createElement('span');
|
|
250
|
+
chip.className = 'combobox-chip';
|
|
251
|
+
chip.dataset.value = entry.value;
|
|
252
|
+
|
|
253
|
+
const label = document.createElement('span');
|
|
254
|
+
label.textContent = entry.label;
|
|
255
|
+
|
|
256
|
+
const remove = document.createElement('button');
|
|
257
|
+
remove.type = 'button';
|
|
258
|
+
remove.className = 'combobox-chip-remove btn';
|
|
259
|
+
remove.dataset.variant = 'ghost';
|
|
260
|
+
remove.dataset.size = 'icon-xs';
|
|
261
|
+
remove.setAttribute('aria-label', `Remove ${entry.label}`);
|
|
262
|
+
remove.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>';
|
|
263
|
+
remove.addEventListener('click', (event) => {
|
|
264
|
+
event.stopPropagation();
|
|
265
|
+
root.deselect(entry.value);
|
|
266
|
+
state.input.focus();
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
chip.appendChild(label);
|
|
270
|
+
chip.appendChild(remove);
|
|
271
|
+
state.chips.insertBefore(chip, state.input);
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const syncTriggerValue = (state) => {
|
|
276
|
+
if (!state.valueTarget) return;
|
|
277
|
+
const selected = getSelection(state);
|
|
278
|
+
state.valueTarget.textContent = state.isMultiple
|
|
279
|
+
? selected.map(entry => entry.label).join(', ')
|
|
280
|
+
: (selected[0]?.label || state.valueTarget.dataset.placeholder || '');
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const syncClearButton = (state) => {
|
|
284
|
+
if (!state.clearButton) return;
|
|
285
|
+
state.clearButton.hidden = getSelection(state).length === 0 && state.input.value === '';
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const syncSelectedOptions = (state) => {
|
|
289
|
+
state.options.forEach(option => {
|
|
290
|
+
if (state.selected.has(getValue(option))) {
|
|
291
|
+
option.setAttribute('aria-selected', 'true');
|
|
292
|
+
} else {
|
|
293
|
+
option.removeAttribute('aria-selected');
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
const setSelected = (root, entries, triggerEvent = true) => {
|
|
299
|
+
const state = states.get(root);
|
|
300
|
+
const normalized = (Array.isArray(entries) ? entries : [entries]).map(normalizeEntry).filter(Boolean);
|
|
301
|
+
|
|
302
|
+
state.selected.clear();
|
|
303
|
+
if (state.isMultiple) {
|
|
304
|
+
normalized.forEach(entry => state.selected.set(entry.value, entry));
|
|
305
|
+
state.input.value = '';
|
|
306
|
+
} else if (normalized[0]) {
|
|
307
|
+
state.selected.set(normalized[0].value, normalized[0]);
|
|
308
|
+
state.input.value = state.valueTarget ? '' : normalized[0].label;
|
|
309
|
+
} else {
|
|
310
|
+
state.input.value = '';
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
state.hiddenInput.value = serializeSelection(state);
|
|
314
|
+
syncSelectedOptions(state);
|
|
315
|
+
if (state.isMultiple) {
|
|
316
|
+
renderChips(root);
|
|
317
|
+
filterOptions(state, { preserveActive: true });
|
|
318
|
+
}
|
|
319
|
+
syncTriggerValue(state);
|
|
320
|
+
syncClearButton(state);
|
|
321
|
+
|
|
322
|
+
if (triggerEvent) {
|
|
323
|
+
root.dispatchEvent(new CustomEvent('change', {
|
|
324
|
+
detail: { value: getCanonicalValue(state), selected: getSelectedDetail(state) },
|
|
325
|
+
bubbles: true,
|
|
326
|
+
}));
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const closePopover = (state, focusInput = false) => {
|
|
331
|
+
if (state.popover.getAttribute('aria-hidden') === 'true') return;
|
|
332
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
333
|
+
state.input.setAttribute('aria-expanded', 'false');
|
|
334
|
+
state.trigger?.setAttribute('aria-expanded', 'false');
|
|
335
|
+
setActiveOption(state, -1);
|
|
336
|
+
if (focusInput) {
|
|
337
|
+
state.skipOpenOnFocus = true;
|
|
338
|
+
state.input.focus();
|
|
339
|
+
requestAnimationFrame(() => { state.skipOpenOnFocus = false; });
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const openPopover = (state) => {
|
|
344
|
+
const { root } = state;
|
|
345
|
+
if (state.popover.getAttribute('aria-hidden') === 'false') return;
|
|
346
|
+
document.dispatchEvent(new CustomEvent('basecoat:popover', { detail: { source: root } }));
|
|
347
|
+
root.refresh();
|
|
348
|
+
const selected = getSelection(state)[0];
|
|
349
|
+
if (state.valueTarget) state.input.value = '';
|
|
350
|
+
const search = !state.isMultiple && selected && state.input.value === selected.label ? '' : undefined;
|
|
351
|
+
filterOptions(state, { search });
|
|
352
|
+
state.popover.setAttribute('aria-hidden', 'false');
|
|
353
|
+
state.input.setAttribute('aria-expanded', 'true');
|
|
354
|
+
state.trigger?.setAttribute('aria-expanded', 'true');
|
|
355
|
+
if (state.trigger) state.input.focus();
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const refreshCombobox = (root) => {
|
|
359
|
+
const state = states.get(root);
|
|
360
|
+
if (!state) return;
|
|
361
|
+
|
|
362
|
+
let elements = getElements(root);
|
|
363
|
+
if (!elements.input || !elements.popover || !elements.listbox || !elements.hiddenInput) {
|
|
364
|
+
const missing = [];
|
|
365
|
+
if (!elements.input) missing.push('input');
|
|
366
|
+
if (!elements.popover) missing.push('popover');
|
|
367
|
+
if (!elements.listbox) missing.push('listbox');
|
|
368
|
+
if (!elements.hiddenInput) missing.push('hidden input');
|
|
369
|
+
console.error(`Combobox refresh failed. Missing element(s): ${missing.join(', ')}`, root);
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
elements = ensureMultipleInputSurface(root, elements);
|
|
374
|
+
const previousValue = elements.hiddenInput.value;
|
|
375
|
+
const previousInputValue = elements.input.value;
|
|
376
|
+
Object.assign(state, elements, getOptions(elements.listbox));
|
|
377
|
+
state.isMultiple = state.listbox.getAttribute('aria-multiselectable') === 'true';
|
|
378
|
+
state.closeOnSelect = root.dataset.closeOnSelect === 'true';
|
|
379
|
+
state.autoHighlight = root.dataset.autoHighlight === 'true';
|
|
380
|
+
state.manualFilter = root.dataset.filter === 'manual';
|
|
381
|
+
state.format = getFormat(root);
|
|
382
|
+
|
|
383
|
+
const stored = parseStoredSelection(previousValue, previousInputValue, state);
|
|
384
|
+
if (stored.length > 0) {
|
|
385
|
+
setSelected(root, stored, false);
|
|
386
|
+
} else {
|
|
387
|
+
const selectedOptions = state.options.filter(option => option.getAttribute('aria-selected') === 'true').map(toSelected);
|
|
388
|
+
if (selectedOptions.length > 0) {
|
|
389
|
+
setSelected(root, state.isMultiple ? selectedOptions : selectedOptions[0], false);
|
|
390
|
+
} else if (state.isMultiple) {
|
|
391
|
+
setSelected(root, [], false);
|
|
392
|
+
} else {
|
|
393
|
+
state.input.value = previousInputValue;
|
|
394
|
+
state.selected.clear();
|
|
395
|
+
state.hiddenInput.value = '';
|
|
396
|
+
syncSelectedOptions(state);
|
|
397
|
+
filterOptions(state, { preserveActive: true });
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
syncTriggerValue(state);
|
|
401
|
+
syncClearButton(state);
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
const resolveEntry = (state, value) => {
|
|
405
|
+
if (value && typeof value === 'object') {
|
|
406
|
+
const entry = normalizeEntry(value);
|
|
407
|
+
if (!entry) return null;
|
|
408
|
+
const option = state.options.find(opt => getValue(opt) === entry.value);
|
|
409
|
+
return option ? toSelected(option) : entry;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
413
|
+
if (option) return toSelected(option);
|
|
414
|
+
const existing = state.selected.get(String(value));
|
|
415
|
+
return existing || null;
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
const selectValue = (root, value) => {
|
|
419
|
+
const state = states.get(root);
|
|
420
|
+
const entry = resolveEntry(state, value);
|
|
421
|
+
if (!entry) return;
|
|
422
|
+
|
|
423
|
+
if (state.isMultiple) {
|
|
424
|
+
setSelected(root, [...getSelection(state).filter(item => item.value !== entry.value), entry]);
|
|
425
|
+
if (state.closeOnSelect) root.close(true);
|
|
426
|
+
} else {
|
|
427
|
+
setSelected(root, entry);
|
|
428
|
+
root.close(state.trigger ? false : true);
|
|
429
|
+
state.trigger?.focus();
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const deselectValue = (root, value) => {
|
|
434
|
+
const state = states.get(root);
|
|
435
|
+
if (!state.isMultiple) return;
|
|
436
|
+
const normalized = String(value);
|
|
437
|
+
if (!state.selected.has(normalized)) return;
|
|
438
|
+
setSelected(root, getSelection(state).filter(item => item.value !== normalized));
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
const clearValue = (root) => {
|
|
442
|
+
const state = states.get(root);
|
|
443
|
+
setSelected(root, [], true);
|
|
444
|
+
state.input.value = '';
|
|
445
|
+
filterOptions(state);
|
|
446
|
+
syncClearButton(state);
|
|
447
|
+
if (state.popover.getAttribute('aria-hidden') === 'false') state.input.focus();
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
const handleKeydown = (event, root) => {
|
|
451
|
+
const state = states.get(root);
|
|
452
|
+
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End', 'Escape', 'Backspace'].includes(event.key)) return;
|
|
453
|
+
|
|
454
|
+
const isOpen = state.popover.getAttribute('aria-hidden') === 'false';
|
|
455
|
+
|
|
456
|
+
if (event.key === 'Backspace' && state.isMultiple && state.input.value === '') {
|
|
457
|
+
const selected = getSelection(state);
|
|
458
|
+
const last = selected[selected.length - 1];
|
|
459
|
+
if (last) root.deselect(last.value);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
if (event.key === 'Escape') {
|
|
464
|
+
if (isOpen) {
|
|
465
|
+
event.preventDefault();
|
|
466
|
+
event.stopPropagation();
|
|
467
|
+
root.close(true);
|
|
468
|
+
}
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (!isOpen && ['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) {
|
|
473
|
+
event.preventDefault();
|
|
474
|
+
openPopover(state);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
if (state.popover.getAttribute('aria-hidden') === 'true') return;
|
|
478
|
+
|
|
479
|
+
if (event.key === 'Enter') {
|
|
480
|
+
if (state.activeIndex > -1) {
|
|
481
|
+
event.preventDefault();
|
|
482
|
+
const option = state.options[state.activeIndex];
|
|
483
|
+
state.isMultiple ? root.toggle(getValue(option)) : root.select(getValue(option));
|
|
484
|
+
}
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key) || state.visibleOptions.length === 0) return;
|
|
489
|
+
event.preventDefault();
|
|
490
|
+
|
|
491
|
+
const currentVisibleIndex = state.activeIndex > -1 ? state.visibleOptions.indexOf(state.options[state.activeIndex]) : -1;
|
|
492
|
+
let nextVisibleIndex = currentVisibleIndex;
|
|
493
|
+
|
|
494
|
+
if (event.key === 'ArrowDown') nextVisibleIndex = Math.min(currentVisibleIndex + 1, state.visibleOptions.length - 1);
|
|
495
|
+
if (event.key === 'ArrowUp') nextVisibleIndex = currentVisibleIndex <= 0 ? 0 : currentVisibleIndex - 1;
|
|
496
|
+
if (event.key === 'Home') nextVisibleIndex = 0;
|
|
497
|
+
if (event.key === 'End') nextVisibleIndex = state.visibleOptions.length - 1;
|
|
498
|
+
|
|
499
|
+
const nextOption = state.visibleOptions[nextVisibleIndex];
|
|
500
|
+
setActiveOption(state, state.options.indexOf(nextOption));
|
|
501
|
+
scrollOptionIntoListbox(state, nextOption);
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const initCombobox = (root) => {
|
|
505
|
+
if (root.dataset.comboboxInitialized) return;
|
|
506
|
+
|
|
507
|
+
const state = { root, activeIndex: -1, allOptions: [], options: [], visibleOptions: [], selected: new Map(), format: 'value', manualFilter: false, skipOpenOnFocus: false };
|
|
508
|
+
states.set(root, state);
|
|
509
|
+
root.refresh = () => refreshCombobox(root);
|
|
510
|
+
|
|
511
|
+
refreshCombobox(root);
|
|
512
|
+
if (!state.input || !state.popover || !state.listbox || !state.hiddenInput) {
|
|
513
|
+
states.delete(root);
|
|
514
|
+
delete root.refresh;
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
root.close = (focusInput = false) => closePopover(state, focusInput);
|
|
519
|
+
|
|
520
|
+
root.select = (value) => selectValue(root, value);
|
|
521
|
+
root.selectByValue = root.select;
|
|
522
|
+
root.setValue = (value) => {
|
|
523
|
+
const entries = state.isMultiple ? (Array.isArray(value) ? value : (value == null ? [] : [value])) : [value];
|
|
524
|
+
const resolved = entries.map(entry => resolveEntry(state, entry) || normalizeEntry(entry)).filter(Boolean);
|
|
525
|
+
setSelected(root, state.isMultiple ? resolved : resolved[0]);
|
|
526
|
+
};
|
|
527
|
+
if (state.isMultiple) {
|
|
528
|
+
root.deselect = (value) => deselectValue(root, value);
|
|
529
|
+
root.toggle = (value) => {
|
|
530
|
+
const entry = resolveEntry(state, value);
|
|
531
|
+
if (!entry) return;
|
|
532
|
+
state.selected.has(entry.value) ? root.deselect(entry.value) : root.select(entry);
|
|
533
|
+
};
|
|
534
|
+
root.selectAll = () => setSelected(root, state.options.map(toSelected));
|
|
535
|
+
root.selectNone = () => setSelected(root, []);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
const handleInputFocus = () => {
|
|
539
|
+
if (state.skipOpenOnFocus) return;
|
|
540
|
+
openPopover(state);
|
|
541
|
+
};
|
|
542
|
+
const handleInputClick = () => openPopover(state);
|
|
543
|
+
const handleInput = () => {
|
|
544
|
+
openPopover(state);
|
|
545
|
+
filterOptions(state);
|
|
546
|
+
if (!state.isMultiple && !state.trigger) {
|
|
547
|
+
state.hiddenInput.value = '';
|
|
548
|
+
state.selected.clear();
|
|
549
|
+
syncSelectedOptions(state);
|
|
550
|
+
syncTriggerValue(state);
|
|
551
|
+
syncClearButton(state);
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
const handleTriggerClick = () => {
|
|
555
|
+
if (state.popover.getAttribute('aria-hidden') === 'false') {
|
|
556
|
+
root.close(false);
|
|
557
|
+
} else {
|
|
558
|
+
openPopover(state);
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
const handleClearClick = (event) => {
|
|
562
|
+
event.preventDefault();
|
|
563
|
+
event.stopPropagation();
|
|
564
|
+
clearValue(root);
|
|
565
|
+
};
|
|
566
|
+
const handleInputKeydown = (event) => handleKeydown(event, root);
|
|
567
|
+
const handleListboxMousemove = (event) => {
|
|
568
|
+
const option = event.target.closest('[role="option"]');
|
|
569
|
+
if (option && state.visibleOptions.includes(option)) setActiveOption(state, state.options.indexOf(option));
|
|
570
|
+
};
|
|
571
|
+
const handleListboxClick = (event) => {
|
|
572
|
+
const option = event.target.closest('[role="option"]');
|
|
573
|
+
if (!option || !state.options.includes(option)) return;
|
|
574
|
+
state.isMultiple ? root.toggle(getValue(option)) : root.select(getValue(option));
|
|
575
|
+
if (state.isMultiple && !state.closeOnSelect) state.input.focus();
|
|
576
|
+
};
|
|
577
|
+
const handleDocumentClick = (event) => {
|
|
578
|
+
if (!root.contains(event.target)) root.close(false);
|
|
579
|
+
};
|
|
580
|
+
const handleDocumentPopover = (event) => {
|
|
581
|
+
if (!root.contains(event.detail.source)) root.close(false);
|
|
582
|
+
};
|
|
583
|
+
|
|
584
|
+
state.input.addEventListener('focus', handleInputFocus);
|
|
585
|
+
state.input.addEventListener('click', handleInputClick);
|
|
586
|
+
state.input.addEventListener('input', handleInput);
|
|
587
|
+
state.input.addEventListener('keydown', handleInputKeydown);
|
|
588
|
+
state.trigger?.addEventListener('click', handleTriggerClick);
|
|
589
|
+
state.clearButton?.addEventListener('click', handleClearClick);
|
|
590
|
+
state.listbox.addEventListener('mousemove', handleListboxMousemove);
|
|
591
|
+
state.listbox.addEventListener('click', handleListboxClick);
|
|
592
|
+
document.addEventListener('click', handleDocumentClick);
|
|
593
|
+
document.addEventListener('basecoat:popover', handleDocumentPopover);
|
|
594
|
+
|
|
595
|
+
root._destroy = () => {
|
|
596
|
+
state.input.removeEventListener('focus', handleInputFocus);
|
|
597
|
+
state.input.removeEventListener('click', handleInputClick);
|
|
598
|
+
state.input.removeEventListener('input', handleInput);
|
|
599
|
+
state.input.removeEventListener('keydown', handleInputKeydown);
|
|
600
|
+
state.trigger?.removeEventListener('click', handleTriggerClick);
|
|
601
|
+
state.clearButton?.removeEventListener('click', handleClearClick);
|
|
602
|
+
state.listbox.removeEventListener('mousemove', handleListboxMousemove);
|
|
603
|
+
state.listbox.removeEventListener('click', handleListboxClick);
|
|
604
|
+
document.removeEventListener('click', handleDocumentClick);
|
|
605
|
+
document.removeEventListener('basecoat:popover', handleDocumentPopover);
|
|
606
|
+
state.chips?.querySelectorAll('.combobox-chip').forEach(chip => chip.remove());
|
|
607
|
+
states.delete(root);
|
|
608
|
+
delete root.refresh;
|
|
609
|
+
delete root.close;
|
|
610
|
+
delete root.select;
|
|
611
|
+
delete root.selectByValue;
|
|
612
|
+
delete root.setValue;
|
|
613
|
+
delete root.clear;
|
|
614
|
+
delete root.deselect;
|
|
615
|
+
delete root.toggle;
|
|
616
|
+
delete root.selectAll;
|
|
617
|
+
delete root.selectNone;
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
621
|
+
state.input.setAttribute('aria-expanded', 'false');
|
|
622
|
+
state.trigger?.setAttribute('aria-expanded', 'false');
|
|
623
|
+
root.clear = () => clearValue(root);
|
|
624
|
+
|
|
625
|
+
Object.defineProperty(root, 'value', {
|
|
626
|
+
configurable: true,
|
|
627
|
+
get: () => getCanonicalValue(state),
|
|
628
|
+
set: (value) => root.setValue(value),
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
Object.defineProperty(root, 'selected', {
|
|
632
|
+
configurable: true,
|
|
633
|
+
get: () => getSelectedDetail(state),
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
root.dataset.comboboxInitialized = 'true';
|
|
637
|
+
root.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
if (window.basecoat) {
|
|
641
|
+
window.basecoat.register('combobox', {
|
|
642
|
+
selector: '.combobox:not([data-combobox-initialized])',
|
|
643
|
+
init: initCombobox,
|
|
644
|
+
refresh: refreshCombobox,
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
})();
|
|
648
|
+
|
|
649
|
+
(() => {
|
|
650
|
+
const states = new WeakMap();
|
|
651
|
+
|
|
652
|
+
const isDisabled = (item) =>
|
|
653
|
+
item.hasAttribute('disabled') ||
|
|
654
|
+
item.getAttribute('aria-disabled') === 'true' ||
|
|
655
|
+
item.getAttribute('data-disabled') === 'true';
|
|
656
|
+
|
|
657
|
+
const getElements = (container) => ({
|
|
658
|
+
input: container.querySelector('header input'),
|
|
659
|
+
menu: container.querySelector('[role="menu"]'),
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
const getItems = (menu) => {
|
|
663
|
+
const allItems = Array.from(menu.querySelectorAll('[role="menuitem"]'));
|
|
664
|
+
return {
|
|
665
|
+
allItems,
|
|
666
|
+
items: allItems.filter(item => !isDisabled(item)),
|
|
667
|
+
};
|
|
668
|
+
};
|
|
669
|
+
|
|
670
|
+
const scrollItemIntoMenu = (state, item) => {
|
|
671
|
+
const itemRect = item.getBoundingClientRect();
|
|
672
|
+
const menuRect = state.menu.getBoundingClientRect();
|
|
673
|
+
|
|
674
|
+
if (itemRect.top < menuRect.top) {
|
|
675
|
+
state.menu.scrollTop -= menuRect.top - itemRect.top;
|
|
676
|
+
} else if (itemRect.bottom > menuRect.bottom) {
|
|
677
|
+
state.menu.scrollTop += itemRect.bottom - menuRect.bottom;
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
|
|
681
|
+
const setActiveItem = (state, index) => {
|
|
682
|
+
if (state.activeIndex > -1 && state.items[state.activeIndex]) {
|
|
683
|
+
state.items[state.activeIndex].classList.remove('active');
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
state.activeIndex = index;
|
|
687
|
+
|
|
688
|
+
if (state.activeIndex > -1) {
|
|
689
|
+
const activeItem = state.items[state.activeIndex];
|
|
690
|
+
activeItem.classList.add('active');
|
|
691
|
+
if (activeItem.id) {
|
|
692
|
+
state.input.setAttribute('aria-activedescendant', activeItem.id);
|
|
693
|
+
} else {
|
|
694
|
+
state.input.removeAttribute('aria-activedescendant');
|
|
695
|
+
}
|
|
696
|
+
} else {
|
|
697
|
+
state.input.removeAttribute('aria-activedescendant');
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
const filterItems = (state) => {
|
|
702
|
+
if (state.manualFilter) {
|
|
703
|
+
setActiveItem(state, -1);
|
|
704
|
+
state.visibleItems = state.items.filter(item => item.getAttribute('aria-hidden') !== 'true');
|
|
705
|
+
if (state.visibleItems.length > 0) {
|
|
706
|
+
setActiveItem(state, state.items.indexOf(state.visibleItems[0]));
|
|
707
|
+
}
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
const searchTerm = state.input.value.trim().toLowerCase();
|
|
712
|
+
|
|
713
|
+
setActiveItem(state, -1);
|
|
714
|
+
state.visibleItems = [];
|
|
715
|
+
|
|
716
|
+
state.allItems.forEach(item => {
|
|
717
|
+
if (item.hasAttribute('data-force')) {
|
|
718
|
+
item.setAttribute('aria-hidden', 'false');
|
|
719
|
+
if (state.items.includes(item)) state.visibleItems.push(item);
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
const itemText = (item.dataset.filter || item.textContent).trim().toLowerCase();
|
|
724
|
+
const keywordList = (item.dataset.keywords || '')
|
|
725
|
+
.toLowerCase()
|
|
726
|
+
.split(/[\s,]+/)
|
|
727
|
+
.filter(Boolean);
|
|
728
|
+
const matchesKeyword = keywordList.some(keyword => keyword.includes(searchTerm));
|
|
729
|
+
const matches = itemText.includes(searchTerm) || matchesKeyword;
|
|
730
|
+
item.setAttribute('aria-hidden', String(!matches));
|
|
731
|
+
if (matches && state.items.includes(item)) state.visibleItems.push(item);
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
if (state.visibleItems.length > 0) {
|
|
735
|
+
setActiveItem(state, state.items.indexOf(state.visibleItems[0]));
|
|
736
|
+
scrollItemIntoMenu(state, state.visibleItems[0]);
|
|
737
|
+
}
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
const refreshCommand = (container) => {
|
|
741
|
+
const state = states.get(container);
|
|
742
|
+
if (!state) return;
|
|
743
|
+
|
|
744
|
+
const elements = getElements(container);
|
|
745
|
+
if (!elements.input || !elements.menu) {
|
|
746
|
+
const missing = [];
|
|
747
|
+
if (!elements.input) missing.push('input');
|
|
748
|
+
if (!elements.menu) missing.push('menu');
|
|
749
|
+
console.error(`Command component refresh failed. Missing element(s): ${missing.join(', ')}`, container);
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
Object.assign(state, elements, getItems(elements.menu));
|
|
754
|
+
state.manualFilter = container.dataset.filter === 'manual';
|
|
755
|
+
filterItems(state);
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
const handleKeyNavigation = (event, state) => {
|
|
759
|
+
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End'].includes(event.key)) return;
|
|
760
|
+
|
|
761
|
+
if (event.key === 'Enter') {
|
|
762
|
+
event.preventDefault();
|
|
763
|
+
if (state.activeIndex > -1) state.items[state.activeIndex]?.click();
|
|
764
|
+
return;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
if (state.visibleItems.length === 0) return;
|
|
768
|
+
|
|
769
|
+
event.preventDefault();
|
|
770
|
+
|
|
771
|
+
const currentVisibleIndex = state.activeIndex > -1 ? state.visibleItems.indexOf(state.items[state.activeIndex]) : -1;
|
|
772
|
+
let nextVisibleIndex = currentVisibleIndex;
|
|
773
|
+
|
|
774
|
+
if (event.key === 'ArrowDown' && currentVisibleIndex < state.visibleItems.length - 1) nextVisibleIndex = currentVisibleIndex + 1;
|
|
775
|
+
if (event.key === 'ArrowUp') nextVisibleIndex = currentVisibleIndex > 0 ? currentVisibleIndex - 1 : 0;
|
|
776
|
+
if (event.key === 'Home') nextVisibleIndex = 0;
|
|
777
|
+
if (event.key === 'End') nextVisibleIndex = state.visibleItems.length - 1;
|
|
778
|
+
|
|
779
|
+
if (nextVisibleIndex !== currentVisibleIndex) {
|
|
780
|
+
const newActiveItem = state.visibleItems[nextVisibleIndex];
|
|
781
|
+
setActiveItem(state, state.items.indexOf(newActiveItem));
|
|
782
|
+
scrollItemIntoMenu(state, newActiveItem);
|
|
783
|
+
}
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
const initCommand = (container) => {
|
|
787
|
+
if (container.dataset.commandInitialized) return;
|
|
788
|
+
|
|
789
|
+
const state = { activeIndex: -1, allItems: [], items: [], visibleItems: [], manualFilter: false };
|
|
790
|
+
states.set(container, state);
|
|
791
|
+
|
|
792
|
+
container.refresh = () => refreshCommand(container);
|
|
793
|
+
container.select = (item) => {
|
|
794
|
+
if (!state.visibleItems.includes(item)) return;
|
|
795
|
+
setActiveItem(state, state.items.indexOf(item));
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
const elements = getElements(container);
|
|
799
|
+
if (!elements.input || !elements.menu) {
|
|
800
|
+
const missing = [];
|
|
801
|
+
if (!elements.input) missing.push('input');
|
|
802
|
+
if (!elements.menu) missing.push('menu');
|
|
803
|
+
console.error(`Command component initialization failed. Missing element(s): ${missing.join(', ')}`, container);
|
|
804
|
+
states.delete(container);
|
|
805
|
+
delete container.refresh;
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
Object.assign(state, elements);
|
|
809
|
+
|
|
810
|
+
const handleInput = () => filterItems(state);
|
|
811
|
+
const handleInputKeydown = (event) => handleKeyNavigation(event, state);
|
|
812
|
+
const handleMenuMousemove = (event) => {
|
|
813
|
+
const menuItem = event.target.closest('[role="menuitem"]');
|
|
814
|
+
if (menuItem && state.visibleItems.includes(menuItem)) {
|
|
815
|
+
const index = state.items.indexOf(menuItem);
|
|
816
|
+
if (index !== state.activeIndex) setActiveItem(state, index);
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
const handleMenuClick = (event) => {
|
|
820
|
+
const clickedItem = event.target.closest('[role="menuitem"]');
|
|
821
|
+
if (clickedItem && state.visibleItems.includes(clickedItem)) {
|
|
822
|
+
const dialog = container.closest('dialog.command-dialog');
|
|
823
|
+
if (dialog && !clickedItem.hasAttribute('data-keep-command-open')) dialog.close();
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
state.input.addEventListener('input', handleInput);
|
|
828
|
+
state.input.addEventListener('keydown', handleInputKeydown);
|
|
829
|
+
state.menu.addEventListener('mousemove', handleMenuMousemove);
|
|
830
|
+
state.menu.addEventListener('click', handleMenuClick);
|
|
831
|
+
|
|
832
|
+
container._destroy = () => {
|
|
833
|
+
state.input.removeEventListener('input', handleInput);
|
|
834
|
+
state.input.removeEventListener('keydown', handleInputKeydown);
|
|
835
|
+
state.menu.removeEventListener('mousemove', handleMenuMousemove);
|
|
836
|
+
state.menu.removeEventListener('click', handleMenuClick);
|
|
837
|
+
states.delete(container);
|
|
838
|
+
delete container.refresh;
|
|
839
|
+
delete container.select;
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
refreshCommand(container);
|
|
843
|
+
container.dataset.commandInitialized = 'true';
|
|
844
|
+
container.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
845
|
+
};
|
|
846
|
+
|
|
847
|
+
if (window.basecoat) {
|
|
848
|
+
window.basecoat.register('command', {
|
|
849
|
+
selector: '.command:not([data-command-initialized])',
|
|
850
|
+
init: initCommand,
|
|
851
|
+
refresh: refreshCommand,
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
})();
|
|
855
|
+
|
|
856
|
+
(() => {
|
|
857
|
+
const toMs = (value) => {
|
|
858
|
+
if (!value) return 0;
|
|
859
|
+
const trimmed = value.trim();
|
|
860
|
+
if (trimmed.endsWith('ms')) return parseFloat(trimmed) || 0;
|
|
861
|
+
if (trimmed.endsWith('s')) return (parseFloat(trimmed) || 0) * 1000;
|
|
862
|
+
return parseFloat(trimmed) || 0;
|
|
863
|
+
};
|
|
864
|
+
|
|
865
|
+
const maxTransitionMs = (element) => {
|
|
866
|
+
if (!element) return 0;
|
|
867
|
+
const styles = getComputedStyle(element);
|
|
868
|
+
const durations = styles.transitionDuration.split(',').map(toMs);
|
|
869
|
+
const delays = styles.transitionDelay.split(',').map(toMs);
|
|
870
|
+
return Math.max(0, ...durations.map((duration, index) => duration + (delays[index] || delays[0] || 0)));
|
|
871
|
+
};
|
|
872
|
+
|
|
873
|
+
const initDrawer = (drawer) => {
|
|
874
|
+
if (drawer.dataset.drawerInitialized) return;
|
|
875
|
+
|
|
876
|
+
const nativeClose = drawer.close.bind(drawer);
|
|
877
|
+
let pointerStartedOnBackdrop = false;
|
|
878
|
+
let closeTimer = null;
|
|
879
|
+
|
|
880
|
+
const finishClose = (returnValue) => {
|
|
881
|
+
window.clearTimeout(closeTimer);
|
|
882
|
+
closeTimer = null;
|
|
883
|
+
drawer.removeAttribute('data-closing');
|
|
884
|
+
nativeClose(returnValue);
|
|
885
|
+
};
|
|
886
|
+
|
|
887
|
+
drawer.close = (returnValue = '') => {
|
|
888
|
+
if (!drawer.open) return;
|
|
889
|
+
if (drawer.dataset.closing) return;
|
|
890
|
+
|
|
891
|
+
const content = drawer.firstElementChild;
|
|
892
|
+
const duration = maxTransitionMs(content);
|
|
893
|
+
|
|
894
|
+
if (duration === 0 || window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
|
895
|
+
finishClose(returnValue);
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
drawer.dataset.closing = 'true';
|
|
900
|
+
closeTimer = window.setTimeout(() => finishClose(returnValue), duration + 50);
|
|
901
|
+
};
|
|
902
|
+
|
|
903
|
+
const handleCancel = (event) => {
|
|
904
|
+
event.preventDefault();
|
|
905
|
+
drawer.close();
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
const handlePointerDown = (event) => {
|
|
909
|
+
pointerStartedOnBackdrop = event.target === drawer;
|
|
910
|
+
};
|
|
911
|
+
|
|
912
|
+
const handlePointerUp = (event) => {
|
|
913
|
+
if (pointerStartedOnBackdrop && event.target === drawer) {
|
|
914
|
+
drawer.close();
|
|
915
|
+
}
|
|
916
|
+
pointerStartedOnBackdrop = false;
|
|
917
|
+
};
|
|
918
|
+
|
|
919
|
+
drawer.addEventListener('cancel', handleCancel);
|
|
920
|
+
drawer.addEventListener('pointerdown', handlePointerDown);
|
|
921
|
+
drawer.addEventListener('pointerup', handlePointerUp);
|
|
922
|
+
|
|
923
|
+
drawer._destroy = () => {
|
|
924
|
+
window.clearTimeout(closeTimer);
|
|
925
|
+
drawer.removeEventListener('cancel', handleCancel);
|
|
926
|
+
drawer.removeEventListener('pointerdown', handlePointerDown);
|
|
927
|
+
drawer.removeEventListener('pointerup', handlePointerUp);
|
|
928
|
+
drawer.close = nativeClose;
|
|
929
|
+
delete drawer._destroy;
|
|
930
|
+
};
|
|
931
|
+
|
|
932
|
+
drawer.dataset.drawerInitialized = 'true';
|
|
933
|
+
drawer.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
if (window.basecoat) {
|
|
937
|
+
window.basecoat.register('drawer', '.drawer:not([data-drawer-initialized])', initDrawer);
|
|
938
|
+
}
|
|
939
|
+
})();
|
|
940
|
+
|
|
941
|
+
(() => {
|
|
942
|
+
function updateRange(element) {
|
|
943
|
+
const min = parseFloat(element.min || '0');
|
|
944
|
+
const max = parseFloat(element.max || '100');
|
|
945
|
+
const value = parseFloat(element.value || '0');
|
|
946
|
+
const percent = max === min ? 0 : ((value - min) / (max - min)) * 100;
|
|
947
|
+
element.style.setProperty('--viewstem-ui--slider-value', `${percent}%`);
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
function initRange(element) {
|
|
951
|
+
if (element.dataset.rangeInitialized) return;
|
|
952
|
+
|
|
953
|
+
updateRange(element);
|
|
954
|
+
const handleInput = () => updateRange(element);
|
|
955
|
+
element.addEventListener('input', handleInput);
|
|
956
|
+
element._destroy = () => {
|
|
957
|
+
element.removeEventListener('input', handleInput);
|
|
958
|
+
};
|
|
959
|
+
element.dataset.rangeInitialized = 'true';
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
if (window.basecoat) {
|
|
963
|
+
window.basecoat.register('range', 'input[type="range"]:not([data-range-initialized])', initRange);
|
|
964
|
+
}
|
|
965
|
+
})();
|
|
966
|
+
|
|
967
|
+
(() => {
|
|
968
|
+
const states = new WeakMap();
|
|
969
|
+
|
|
970
|
+
const getElements = (root) => {
|
|
971
|
+
const trigger = root.querySelector(':scope > button');
|
|
972
|
+
const selectedLabel = trigger?.querySelector(':scope > span') || null;
|
|
973
|
+
const popover = root.querySelector(':scope > [data-popover]');
|
|
974
|
+
const listbox = popover ? popover.querySelector('[role="listbox"]') : null;
|
|
975
|
+
const input = root.querySelector(':scope > input[type="hidden"]');
|
|
976
|
+
return { trigger, selectedLabel, popover, listbox, input };
|
|
977
|
+
};
|
|
978
|
+
|
|
979
|
+
const getValue = (option) => option.dataset.value ?? option.textContent.trim();
|
|
980
|
+
const getLabel = (option) => option.dataset.label || option.textContent.trim();
|
|
981
|
+
const getFormat = (root) => root.dataset.format === 'object' ? 'object' : 'value';
|
|
982
|
+
const isDisabled = (option) => option.getAttribute('aria-disabled') === 'true';
|
|
983
|
+
const toSelected = (option) => ({ value: getValue(option), label: getLabel(option) });
|
|
984
|
+
|
|
985
|
+
const getOptions = (listbox) => {
|
|
986
|
+
const allOptions = Array.from(listbox.querySelectorAll('[role="option"]'));
|
|
987
|
+
return {
|
|
988
|
+
allOptions,
|
|
989
|
+
options: allOptions.filter(option => !isDisabled(option)),
|
|
990
|
+
};
|
|
991
|
+
};
|
|
992
|
+
|
|
993
|
+
const parseStoredValues = (storedValue, { isMultiple, format }) => {
|
|
994
|
+
if (isMultiple) {
|
|
995
|
+
try {
|
|
996
|
+
const parsed = JSON.parse(storedValue || '[]');
|
|
997
|
+
if (!Array.isArray(parsed)) return [];
|
|
998
|
+
return parsed
|
|
999
|
+
.map(item => format === 'object' && item && typeof item === 'object' ? item.value : item)
|
|
1000
|
+
.filter(value => value != null)
|
|
1001
|
+
.map(String);
|
|
1002
|
+
} catch (_) {
|
|
1003
|
+
return [];
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
if (format === 'object') {
|
|
1008
|
+
try {
|
|
1009
|
+
const parsed = JSON.parse(storedValue || 'null');
|
|
1010
|
+
return parsed && typeof parsed === 'object' && parsed.value != null ? String(parsed.value) : '';
|
|
1011
|
+
} catch (_) {
|
|
1012
|
+
return '';
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
return storedValue || '';
|
|
1017
|
+
};
|
|
1018
|
+
|
|
1019
|
+
const serializeSelection = (state, selected) => {
|
|
1020
|
+
if (state.format === 'object') {
|
|
1021
|
+
return JSON.stringify(state.isMultiple ? selected : (selected[0] || null));
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
const value = selected.map(item => item.value);
|
|
1025
|
+
return state.isMultiple ? JSON.stringify(value) : (value[0] || '');
|
|
1026
|
+
};
|
|
1027
|
+
|
|
1028
|
+
const showPlaceholder = (state) => {
|
|
1029
|
+
state.selectedLabel.textContent = state.placeholder || '';
|
|
1030
|
+
state.selectedLabel.classList.toggle('text-muted-foreground', Boolean(state.placeholder));
|
|
1031
|
+
state.input.value = state.isMultiple ? serializeSelection(state, []) : '';
|
|
1032
|
+
};
|
|
1033
|
+
|
|
1034
|
+
const scrollOptionIntoListbox = (state, option) => {
|
|
1035
|
+
const optionRect = option.getBoundingClientRect();
|
|
1036
|
+
const listboxRect = state.listbox.getBoundingClientRect();
|
|
1037
|
+
|
|
1038
|
+
if (optionRect.top < listboxRect.top) {
|
|
1039
|
+
state.listbox.scrollTop -= listboxRect.top - optionRect.top;
|
|
1040
|
+
} else if (optionRect.bottom > listboxRect.bottom) {
|
|
1041
|
+
state.listbox.scrollTop += optionRect.bottom - listboxRect.bottom;
|
|
1042
|
+
}
|
|
1043
|
+
};
|
|
1044
|
+
|
|
1045
|
+
const setActiveOption = (state, index) => {
|
|
1046
|
+
if (state.activeIndex > -1 && state.options[state.activeIndex]) {
|
|
1047
|
+
state.options[state.activeIndex].classList.remove('active');
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
state.activeIndex = index;
|
|
1051
|
+
|
|
1052
|
+
if (state.activeIndex > -1) {
|
|
1053
|
+
const activeOption = state.options[state.activeIndex];
|
|
1054
|
+
activeOption.classList.add('active');
|
|
1055
|
+
if (activeOption.id) {
|
|
1056
|
+
state.trigger.setAttribute('aria-activedescendant', activeOption.id);
|
|
1057
|
+
} else {
|
|
1058
|
+
state.trigger.removeAttribute('aria-activedescendant');
|
|
1059
|
+
}
|
|
1060
|
+
} else {
|
|
1061
|
+
state.trigger.removeAttribute('aria-activedescendant');
|
|
1062
|
+
}
|
|
1063
|
+
};
|
|
1064
|
+
|
|
1065
|
+
const updateValue = (root, optionOrOptions, triggerEvent = true) => {
|
|
1066
|
+
const state = states.get(root);
|
|
1067
|
+
let value;
|
|
1068
|
+
let selectedDetail;
|
|
1069
|
+
|
|
1070
|
+
if (state.isMultiple) {
|
|
1071
|
+
const selected = Array.isArray(optionOrOptions) ? optionOrOptions : [];
|
|
1072
|
+
state.selectedOptions.clear();
|
|
1073
|
+
selected.forEach(option => state.selectedOptions.add(option));
|
|
1074
|
+
|
|
1075
|
+
const selectedInOrder = state.options.filter(option => state.selectedOptions.has(option));
|
|
1076
|
+
selectedDetail = selectedInOrder.map(toSelected);
|
|
1077
|
+
if (selectedInOrder.length === 0) {
|
|
1078
|
+
state.selectedLabel.textContent = state.placeholder;
|
|
1079
|
+
state.selectedLabel.classList.add('text-muted-foreground');
|
|
1080
|
+
} else {
|
|
1081
|
+
state.selectedLabel.textContent = selectedDetail.map(item => item.label).join(', ');
|
|
1082
|
+
state.selectedLabel.classList.remove('text-muted-foreground');
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
value = selectedDetail.map(item => item.value);
|
|
1086
|
+
state.input.value = serializeSelection(state, selectedDetail);
|
|
1087
|
+
} else {
|
|
1088
|
+
const option = optionOrOptions;
|
|
1089
|
+
if (!option) {
|
|
1090
|
+
state.options.forEach(option => option.removeAttribute('aria-selected'));
|
|
1091
|
+
showPlaceholder(state);
|
|
1092
|
+
selectedDetail = null;
|
|
1093
|
+
value = '';
|
|
1094
|
+
} else {
|
|
1095
|
+
if (option.dataset.label) {
|
|
1096
|
+
state.selectedLabel.textContent = option.dataset.label;
|
|
1097
|
+
} else {
|
|
1098
|
+
state.selectedLabel.innerHTML = option.innerHTML;
|
|
1099
|
+
}
|
|
1100
|
+
state.selectedLabel.classList.remove('text-muted-foreground');
|
|
1101
|
+
selectedDetail = toSelected(option);
|
|
1102
|
+
value = selectedDetail.value;
|
|
1103
|
+
state.input.value = serializeSelection(state, [selectedDetail]);
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
state.options.forEach(option => {
|
|
1108
|
+
const isSelected = state.isMultiple ? state.selectedOptions.has(option) : optionOrOptions && option === optionOrOptions;
|
|
1109
|
+
if (isSelected) {
|
|
1110
|
+
option.setAttribute('aria-selected', 'true');
|
|
1111
|
+
} else {
|
|
1112
|
+
option.removeAttribute('aria-selected');
|
|
1113
|
+
}
|
|
1114
|
+
});
|
|
1115
|
+
|
|
1116
|
+
if (triggerEvent) {
|
|
1117
|
+
root.dispatchEvent(new CustomEvent('change', {
|
|
1118
|
+
detail: { value, selected: selectedDetail },
|
|
1119
|
+
bubbles: true,
|
|
1120
|
+
}));
|
|
1121
|
+
}
|
|
1122
|
+
};
|
|
1123
|
+
|
|
1124
|
+
const closePopover = (state, focusOnTrigger = true) => {
|
|
1125
|
+
if (state.popover.getAttribute('aria-hidden') === 'true') return;
|
|
1126
|
+
if (focusOnTrigger) state.trigger.focus();
|
|
1127
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
1128
|
+
state.trigger.setAttribute('aria-expanded', 'false');
|
|
1129
|
+
setActiveOption(state, -1);
|
|
1130
|
+
};
|
|
1131
|
+
|
|
1132
|
+
const refreshSelect = (root) => {
|
|
1133
|
+
const state = states.get(root);
|
|
1134
|
+
if (!state) return;
|
|
1135
|
+
|
|
1136
|
+
const elements = getElements(root);
|
|
1137
|
+
if (!elements.trigger || !elements.selectedLabel || !elements.popover || !elements.listbox || !elements.input) {
|
|
1138
|
+
const missing = [];
|
|
1139
|
+
if (!elements.trigger) missing.push('trigger');
|
|
1140
|
+
if (!elements.selectedLabel) missing.push('selected label');
|
|
1141
|
+
if (!elements.popover) missing.push('popover');
|
|
1142
|
+
if (!elements.listbox) missing.push('listbox');
|
|
1143
|
+
if (!elements.input) missing.push('input');
|
|
1144
|
+
console.error(`Select component refresh failed. Missing element(s): ${missing.join(', ')}`, root);
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
const previousValue = elements.input.value;
|
|
1149
|
+
const previousActive = state.options[state.activeIndex];
|
|
1150
|
+
previousActive?.classList.remove('active');
|
|
1151
|
+
state.activeIndex = -1;
|
|
1152
|
+
Object.assign(state, elements, getOptions(elements.listbox));
|
|
1153
|
+
state.visibleOptions = [...state.options];
|
|
1154
|
+
state.isMultiple = state.listbox.getAttribute('aria-multiselectable') === 'true';
|
|
1155
|
+
state.format = getFormat(root);
|
|
1156
|
+
state.placeholder = root.dataset.placeholder || '';
|
|
1157
|
+
state.closeOnSelect = root.dataset.closeOnSelect === 'true';
|
|
1158
|
+
|
|
1159
|
+
if (state.isMultiple) {
|
|
1160
|
+
if (!state.selectedOptions) state.selectedOptions = new Set();
|
|
1161
|
+
const values = parseStoredValues(previousValue, state);
|
|
1162
|
+
const selected = values.length
|
|
1163
|
+
? values.map(value => state.options.find(option => getValue(option) === value)).filter(Boolean)
|
|
1164
|
+
: state.options.filter(option => option.getAttribute('aria-selected') === 'true');
|
|
1165
|
+
updateValue(root, selected, false);
|
|
1166
|
+
} else {
|
|
1167
|
+
const value = parseStoredValues(previousValue, state);
|
|
1168
|
+
const selected = value === '' && state.placeholder
|
|
1169
|
+
? null
|
|
1170
|
+
: state.options.find(option => getValue(option) === value)
|
|
1171
|
+
|| state.options.find(option => option.getAttribute('aria-selected') === 'true');
|
|
1172
|
+
state.options.forEach(option => option.removeAttribute('aria-selected'));
|
|
1173
|
+
updateValue(root, selected || null, false);
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
const selectedOption = state.listbox.querySelector('[role="option"][aria-selected="true"]');
|
|
1177
|
+
if (state.trigger.getAttribute('aria-expanded') === 'true') {
|
|
1178
|
+
const active = state.options.includes(previousActive) ? previousActive : selectedOption ?? state.options[0];
|
|
1179
|
+
setActiveOption(state, state.options.indexOf(active));
|
|
1180
|
+
}
|
|
1181
|
+
};
|
|
1182
|
+
|
|
1183
|
+
const toggleMultipleValue = (root, option) => {
|
|
1184
|
+
const state = states.get(root);
|
|
1185
|
+
if (state.selectedOptions.has(option)) {
|
|
1186
|
+
state.selectedOptions.delete(option);
|
|
1187
|
+
} else {
|
|
1188
|
+
state.selectedOptions.add(option);
|
|
1189
|
+
}
|
|
1190
|
+
updateValue(root, state.options.filter(opt => state.selectedOptions.has(opt)));
|
|
1191
|
+
};
|
|
1192
|
+
|
|
1193
|
+
const selectValue = (root, value) => {
|
|
1194
|
+
const state = states.get(root);
|
|
1195
|
+
if (state.isMultiple) {
|
|
1196
|
+
const option = state.options.find(opt => getValue(opt) === value && !state.selectedOptions.has(opt));
|
|
1197
|
+
if (!option) return;
|
|
1198
|
+
state.selectedOptions.add(option);
|
|
1199
|
+
updateValue(root, state.options.filter(opt => state.selectedOptions.has(opt)));
|
|
1200
|
+
} else {
|
|
1201
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
1202
|
+
if (!option) return;
|
|
1203
|
+
if (state.placeholder && getValue(option) === '') {
|
|
1204
|
+
updateValue(root, null);
|
|
1205
|
+
closePopover(state);
|
|
1206
|
+
return;
|
|
1207
|
+
}
|
|
1208
|
+
if (root.value !== value) updateValue(root, option);
|
|
1209
|
+
closePopover(state);
|
|
1210
|
+
}
|
|
1211
|
+
};
|
|
1212
|
+
|
|
1213
|
+
const deselectValue = (root, value) => {
|
|
1214
|
+
const state = states.get(root);
|
|
1215
|
+
if (!state.isMultiple) return;
|
|
1216
|
+
const option = state.options.find(opt => getValue(opt) === value && state.selectedOptions.has(opt));
|
|
1217
|
+
if (!option) return;
|
|
1218
|
+
state.selectedOptions.delete(option);
|
|
1219
|
+
updateValue(root, state.options.filter(opt => state.selectedOptions.has(opt)));
|
|
1220
|
+
};
|
|
1221
|
+
|
|
1222
|
+
const handleKeyNavigation = (event, root) => {
|
|
1223
|
+
const state = states.get(root);
|
|
1224
|
+
const isPopoverOpen = state.popover.getAttribute('aria-hidden') === 'false';
|
|
1225
|
+
|
|
1226
|
+
if (event.key === 'Tab' && isPopoverOpen) {
|
|
1227
|
+
root.close(false);
|
|
1228
|
+
return;
|
|
1229
|
+
}
|
|
1230
|
+
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End', 'Escape'].includes(event.key)) return;
|
|
1231
|
+
|
|
1232
|
+
if (!isPopoverOpen) {
|
|
1233
|
+
if (event.key !== 'Enter' && event.key !== 'Escape') {
|
|
1234
|
+
event.preventDefault();
|
|
1235
|
+
root.open(event.key);
|
|
1236
|
+
}
|
|
1237
|
+
return;
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
event.preventDefault();
|
|
1241
|
+
|
|
1242
|
+
if (event.key === 'Escape') {
|
|
1243
|
+
root.close();
|
|
1244
|
+
return;
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
if (event.key === 'Enter') {
|
|
1248
|
+
if (state.activeIndex > -1) {
|
|
1249
|
+
const option = state.options[state.activeIndex];
|
|
1250
|
+
if (state.isMultiple) {
|
|
1251
|
+
toggleMultipleValue(root, option);
|
|
1252
|
+
if (state.closeOnSelect) root.close();
|
|
1253
|
+
} else {
|
|
1254
|
+
if (state.placeholder && getValue(option) === '') {
|
|
1255
|
+
updateValue(root, null);
|
|
1256
|
+
} else if (root.value !== getValue(option)) {
|
|
1257
|
+
updateValue(root, option);
|
|
1258
|
+
}
|
|
1259
|
+
root.close();
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
if (state.visibleOptions.length === 0) return;
|
|
1266
|
+
|
|
1267
|
+
const currentVisibleIndex = state.activeIndex > -1 ? state.visibleOptions.indexOf(state.options[state.activeIndex]) : -1;
|
|
1268
|
+
let nextVisibleIndex = currentVisibleIndex;
|
|
1269
|
+
|
|
1270
|
+
if (event.key === 'ArrowDown' && currentVisibleIndex < state.visibleOptions.length - 1) nextVisibleIndex = currentVisibleIndex + 1;
|
|
1271
|
+
if (event.key === 'ArrowUp') nextVisibleIndex = currentVisibleIndex > 0 ? currentVisibleIndex - 1 : 0;
|
|
1272
|
+
if (event.key === 'Home') nextVisibleIndex = 0;
|
|
1273
|
+
if (event.key === 'End') nextVisibleIndex = state.visibleOptions.length - 1;
|
|
1274
|
+
|
|
1275
|
+
if (nextVisibleIndex !== currentVisibleIndex) {
|
|
1276
|
+
const newActiveOption = state.visibleOptions[nextVisibleIndex];
|
|
1277
|
+
setActiveOption(state, state.options.indexOf(newActiveOption));
|
|
1278
|
+
scrollOptionIntoListbox(state, newActiveOption);
|
|
1279
|
+
}
|
|
1280
|
+
};
|
|
1281
|
+
|
|
1282
|
+
const initSelect = (root) => {
|
|
1283
|
+
if (root.dataset.selectInitialized) return;
|
|
1284
|
+
|
|
1285
|
+
const state = { activeIndex: -1, selectedOptions: null, options: [], allOptions: [], visibleOptions: [], format: 'value' };
|
|
1286
|
+
states.set(root, state);
|
|
1287
|
+
root.refresh = () => refreshSelect(root);
|
|
1288
|
+
|
|
1289
|
+
refreshSelect(root);
|
|
1290
|
+
if (!state.trigger || !state.selectedLabel || !state.popover || !state.listbox || !state.input) {
|
|
1291
|
+
states.delete(root);
|
|
1292
|
+
delete root.refresh;
|
|
1293
|
+
return;
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
root.open = (key) => {
|
|
1297
|
+
document.dispatchEvent(new CustomEvent('basecoat:popover', { detail: { source: root } }));
|
|
1298
|
+
root.refresh();
|
|
1299
|
+
state.popover.setAttribute('aria-hidden', 'false');
|
|
1300
|
+
state.trigger.setAttribute('aria-expanded', 'true');
|
|
1301
|
+
|
|
1302
|
+
const selectedOption = state.listbox.querySelector('[role="option"][aria-selected="true"]');
|
|
1303
|
+
const active = selectedOption ?? (key === 'ArrowUp' ? state.options.at(-1) : state.options[0]);
|
|
1304
|
+
if (active) {
|
|
1305
|
+
setActiveOption(state, state.options.indexOf(active));
|
|
1306
|
+
scrollOptionIntoListbox(state, active);
|
|
1307
|
+
}
|
|
1308
|
+
};
|
|
1309
|
+
root.close = (focusOnTrigger = true) => closePopover(state, focusOnTrigger);
|
|
1310
|
+
root.togglePopover = () => state.trigger.getAttribute('aria-expanded') === 'true' ? root.close() : root.open();
|
|
1311
|
+
|
|
1312
|
+
const handleTriggerKeydown = (event) => handleKeyNavigation(event, root);
|
|
1313
|
+
const handleTriggerClick = root.togglePopover;
|
|
1314
|
+
const handleListboxMousemove = (event) => {
|
|
1315
|
+
const option = event.target.closest('[role="option"]');
|
|
1316
|
+
if (option && state.visibleOptions.includes(option)) {
|
|
1317
|
+
const index = state.options.indexOf(option);
|
|
1318
|
+
if (index !== state.activeIndex) setActiveOption(state, index);
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1321
|
+
const handleListboxClick = (event) => {
|
|
1322
|
+
const clickedOption = event.target.closest('[role="option"]');
|
|
1323
|
+
if (!clickedOption) return;
|
|
1324
|
+
|
|
1325
|
+
const option = state.options.find(opt => opt === clickedOption);
|
|
1326
|
+
if (!option) return;
|
|
1327
|
+
|
|
1328
|
+
if (state.isMultiple) {
|
|
1329
|
+
toggleMultipleValue(root, option);
|
|
1330
|
+
if (state.closeOnSelect) {
|
|
1331
|
+
root.close();
|
|
1332
|
+
} else {
|
|
1333
|
+
setActiveOption(state, state.options.indexOf(option));
|
|
1334
|
+
state.trigger.focus();
|
|
1335
|
+
}
|
|
1336
|
+
} else {
|
|
1337
|
+
if (state.placeholder && getValue(option) === '') {
|
|
1338
|
+
updateValue(root, null);
|
|
1339
|
+
} else if (root.value !== getValue(option)) {
|
|
1340
|
+
updateValue(root, option);
|
|
1341
|
+
}
|
|
1342
|
+
root.close();
|
|
1343
|
+
}
|
|
1344
|
+
};
|
|
1345
|
+
const handleDocumentClick = (event) => {
|
|
1346
|
+
if (!root.contains(event.target)) root.close(false);
|
|
1347
|
+
};
|
|
1348
|
+
const handleDocumentPopover = (event) => {
|
|
1349
|
+
if (!root.contains(event.detail.source)) root.close(false);
|
|
1350
|
+
};
|
|
1351
|
+
|
|
1352
|
+
state.trigger.addEventListener('keydown', handleTriggerKeydown);
|
|
1353
|
+
state.trigger.addEventListener('click', handleTriggerClick);
|
|
1354
|
+
state.listbox.addEventListener('mousemove', handleListboxMousemove);
|
|
1355
|
+
state.listbox.addEventListener('click', handleListboxClick);
|
|
1356
|
+
document.addEventListener('click', handleDocumentClick);
|
|
1357
|
+
document.addEventListener('basecoat:popover', handleDocumentPopover);
|
|
1358
|
+
|
|
1359
|
+
root._destroy = () => {
|
|
1360
|
+
state.trigger.removeEventListener('keydown', handleTriggerKeydown);
|
|
1361
|
+
state.trigger.removeEventListener('click', handleTriggerClick);
|
|
1362
|
+
state.listbox.removeEventListener('mousemove', handleListboxMousemove);
|
|
1363
|
+
state.listbox.removeEventListener('click', handleListboxClick);
|
|
1364
|
+
document.removeEventListener('click', handleDocumentClick);
|
|
1365
|
+
document.removeEventListener('basecoat:popover', handleDocumentPopover);
|
|
1366
|
+
states.delete(root);
|
|
1367
|
+
delete root.refresh;
|
|
1368
|
+
delete root.open;
|
|
1369
|
+
delete root.close;
|
|
1370
|
+
delete root.togglePopover;
|
|
1371
|
+
delete root.select;
|
|
1372
|
+
delete root.selectByValue;
|
|
1373
|
+
delete root.deselect;
|
|
1374
|
+
delete root.toggle;
|
|
1375
|
+
delete root.selectAll;
|
|
1376
|
+
delete root.selectNone;
|
|
1377
|
+
};
|
|
1378
|
+
|
|
1379
|
+
Object.defineProperty(root, 'value', {
|
|
1380
|
+
configurable: true,
|
|
1381
|
+
get: () => state.isMultiple ? state.options.filter(option => state.selectedOptions.has(option)).map(getValue) : parseStoredValues(state.input.value, state),
|
|
1382
|
+
set: (value) => {
|
|
1383
|
+
if (state.isMultiple) {
|
|
1384
|
+
const values = Array.isArray(value) ? value : (value != null ? [value] : []);
|
|
1385
|
+
updateValue(root, values.map(v => state.options.find(option => getValue(option) === v)).filter(Boolean));
|
|
1386
|
+
} else {
|
|
1387
|
+
if (value == null || value === '') {
|
|
1388
|
+
updateValue(root, null);
|
|
1389
|
+
root.close();
|
|
1390
|
+
return;
|
|
1391
|
+
}
|
|
1392
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
1393
|
+
if (option) {
|
|
1394
|
+
updateValue(root, option);
|
|
1395
|
+
root.close();
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
},
|
|
1399
|
+
});
|
|
1400
|
+
|
|
1401
|
+
Object.defineProperty(root, 'selected', {
|
|
1402
|
+
configurable: true,
|
|
1403
|
+
get: () => {
|
|
1404
|
+
if (state.isMultiple) return state.options.filter(option => state.selectedOptions.has(option)).map(toSelected);
|
|
1405
|
+
const value = root.value;
|
|
1406
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
1407
|
+
return option ? toSelected(option) : null;
|
|
1408
|
+
},
|
|
1409
|
+
});
|
|
1410
|
+
|
|
1411
|
+
root.select = (value) => selectValue(root, value);
|
|
1412
|
+
root.selectByValue = root.select;
|
|
1413
|
+
if (state.isMultiple) {
|
|
1414
|
+
root.deselect = (value) => deselectValue(root, value);
|
|
1415
|
+
root.toggle = (value) => {
|
|
1416
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
1417
|
+
if (option) toggleMultipleValue(root, option);
|
|
1418
|
+
};
|
|
1419
|
+
root.selectAll = () => updateValue(root, state.options);
|
|
1420
|
+
root.selectNone = () => updateValue(root, []);
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
1424
|
+
state.trigger.setAttribute('aria-expanded', 'false');
|
|
1425
|
+
root.dataset.selectInitialized = 'true';
|
|
1426
|
+
root.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
1427
|
+
};
|
|
1428
|
+
|
|
1429
|
+
if (window.basecoat) {
|
|
1430
|
+
window.basecoat.register('select', {
|
|
1431
|
+
selector: 'div.select:not([data-select-initialized])',
|
|
1432
|
+
init: initSelect,
|
|
1433
|
+
refresh: refreshSelect,
|
|
1434
|
+
});
|
|
1435
|
+
}
|
|
1436
|
+
})();
|
|
1437
|
+
|
|
1438
|
+
(() => {
|
|
1439
|
+
const initSidebar = (sidebarComponent) => {
|
|
1440
|
+
if (sidebarComponent.dataset.sidebarInitialized && typeof sidebarComponent.toggle === 'function') return;
|
|
1441
|
+
|
|
1442
|
+
const initialOpen = sidebarComponent.dataset.initialOpen !== 'false';
|
|
1443
|
+
const initialMobileOpen = sidebarComponent.dataset.initialMobileOpen === 'true';
|
|
1444
|
+
const breakpoint = parseInt(sidebarComponent.dataset.breakpoint) || 768;
|
|
1445
|
+
|
|
1446
|
+
let open = breakpoint > 0
|
|
1447
|
+
? (window.innerWidth >= breakpoint ? initialOpen : initialMobileOpen)
|
|
1448
|
+
: initialOpen;
|
|
1449
|
+
|
|
1450
|
+
const updateState = () => {
|
|
1451
|
+
sidebarComponent.setAttribute('aria-hidden', String(!open));
|
|
1452
|
+
if (open) {
|
|
1453
|
+
sidebarComponent.removeAttribute('inert');
|
|
1454
|
+
} else {
|
|
1455
|
+
sidebarComponent.setAttribute('inert', '');
|
|
1456
|
+
}
|
|
1457
|
+
};
|
|
1458
|
+
|
|
1459
|
+
const setState = (state) => {
|
|
1460
|
+
open = Boolean(state);
|
|
1461
|
+
updateState();
|
|
1462
|
+
};
|
|
1463
|
+
|
|
1464
|
+
sidebarComponent.open = () => setState(true);
|
|
1465
|
+
sidebarComponent.close = () => setState(false);
|
|
1466
|
+
sidebarComponent.toggle = () => setState(!open);
|
|
1467
|
+
|
|
1468
|
+
const handleClick = (event) => {
|
|
1469
|
+
const target = event.target;
|
|
1470
|
+
const nav = sidebarComponent.querySelector(':scope > nav');
|
|
1471
|
+
const isMobile = window.innerWidth < breakpoint;
|
|
1472
|
+
|
|
1473
|
+
if (isMobile && target.closest('a, button') && !target.closest('[data-keep-mobile-sidebar-open]')) {
|
|
1474
|
+
if (document.activeElement) document.activeElement.blur();
|
|
1475
|
+
sidebarComponent.close();
|
|
1476
|
+
return;
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
if (target === sidebarComponent || (nav && !nav.contains(target))) {
|
|
1480
|
+
if (document.activeElement) document.activeElement.blur();
|
|
1481
|
+
sidebarComponent.close();
|
|
1482
|
+
}
|
|
1483
|
+
};
|
|
1484
|
+
|
|
1485
|
+
sidebarComponent.addEventListener('click', handleClick);
|
|
1486
|
+
|
|
1487
|
+
sidebarComponent._destroy = () => {
|
|
1488
|
+
sidebarComponent.removeEventListener('click', handleClick);
|
|
1489
|
+
delete sidebarComponent.open;
|
|
1490
|
+
delete sidebarComponent.close;
|
|
1491
|
+
delete sidebarComponent.toggle;
|
|
1492
|
+
};
|
|
1493
|
+
|
|
1494
|
+
updateState();
|
|
1495
|
+
sidebarComponent.dataset.sidebarInitialized = 'true';
|
|
1496
|
+
sidebarComponent.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
1497
|
+
};
|
|
1498
|
+
|
|
1499
|
+
if (window.basecoat) {
|
|
1500
|
+
window.basecoat.register('sidebar', '.sidebar', initSidebar);
|
|
1501
|
+
}
|
|
1502
|
+
})();
|
|
1503
|
+
|
|
1504
|
+
(() => {
|
|
1505
|
+
const states = new WeakMap();
|
|
1506
|
+
|
|
1507
|
+
const isDisabled = (tab) => tab.disabled || tab.getAttribute('aria-disabled') === 'true';
|
|
1508
|
+
|
|
1509
|
+
const getElements = (root) => {
|
|
1510
|
+
const tablist = root.querySelector('[role="tablist"]');
|
|
1511
|
+
const tabs = tablist ? Array.from(tablist.querySelectorAll('[role="tab"]')) : [];
|
|
1512
|
+
const panels = tabs.map(tab => document.getElementById(tab.getAttribute('aria-controls'))).filter(Boolean);
|
|
1513
|
+
return { tablist, tabs, panels };
|
|
1514
|
+
};
|
|
1515
|
+
|
|
1516
|
+
const refreshTabs = (root) => {
|
|
1517
|
+
const state = states.get(root);
|
|
1518
|
+
if (!state) return;
|
|
1519
|
+
|
|
1520
|
+
Object.assign(state, getElements(root));
|
|
1521
|
+
if (!state.tablist) return;
|
|
1522
|
+
|
|
1523
|
+
const selected = state.tabs.find(tab => tab.getAttribute('aria-selected') === 'true' && !isDisabled(tab))
|
|
1524
|
+
|| state.tabs.find(tab => !isDisabled(tab));
|
|
1525
|
+
if (selected) root.select(selected, false);
|
|
1526
|
+
};
|
|
1527
|
+
|
|
1528
|
+
const initTabs = (root) => {
|
|
1529
|
+
if (root.dataset.tabsInitialized) return;
|
|
1530
|
+
|
|
1531
|
+
const state = {};
|
|
1532
|
+
states.set(root, state);
|
|
1533
|
+
root.refresh = () => refreshTabs(root);
|
|
1534
|
+
|
|
1535
|
+
const selectTab = (tabToSelect, focus = false) => {
|
|
1536
|
+
if (!tabToSelect || isDisabled(tabToSelect)) return;
|
|
1537
|
+
|
|
1538
|
+
state.tabs.forEach((tab) => {
|
|
1539
|
+
tab.setAttribute('aria-selected', 'false');
|
|
1540
|
+
tab.setAttribute('tabindex', '-1');
|
|
1541
|
+
const panel = document.getElementById(tab.getAttribute('aria-controls'));
|
|
1542
|
+
if (panel) panel.hidden = true;
|
|
1543
|
+
});
|
|
1544
|
+
|
|
1545
|
+
tabToSelect.setAttribute('aria-selected', 'true');
|
|
1546
|
+
tabToSelect.setAttribute('tabindex', '0');
|
|
1547
|
+
const activePanel = document.getElementById(tabToSelect.getAttribute('aria-controls'));
|
|
1548
|
+
if (activePanel) activePanel.hidden = false;
|
|
1549
|
+
if (focus) tabToSelect.focus();
|
|
1550
|
+
};
|
|
1551
|
+
|
|
1552
|
+
root.select = selectTab;
|
|
1553
|
+
refreshTabs(root);
|
|
1554
|
+
if (!state.tablist) {
|
|
1555
|
+
states.delete(root);
|
|
1556
|
+
delete root.refresh;
|
|
1557
|
+
delete root.select;
|
|
1558
|
+
return;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
const handleClick = (event) => {
|
|
1562
|
+
const clickedTab = event.target.closest('[role="tab"]');
|
|
1563
|
+
if (clickedTab) root.select(clickedTab);
|
|
1564
|
+
};
|
|
1565
|
+
|
|
1566
|
+
const handleKeydown = (event) => {
|
|
1567
|
+
const currentTab = event.target;
|
|
1568
|
+
if (!state.tabs.includes(currentTab)) return;
|
|
1569
|
+
|
|
1570
|
+
const enabledTabs = state.tabs.filter(tab => !isDisabled(tab));
|
|
1571
|
+
const currentIndex = enabledTabs.indexOf(currentTab);
|
|
1572
|
+
const orientation = state.tablist.getAttribute('aria-orientation') || 'horizontal';
|
|
1573
|
+
if (currentIndex === -1) return;
|
|
1574
|
+
|
|
1575
|
+
let nextTab;
|
|
1576
|
+
if (event.key === 'ArrowRight' && orientation === 'horizontal') nextTab = enabledTabs[(currentIndex + 1) % enabledTabs.length];
|
|
1577
|
+
if (event.key === 'ArrowLeft' && orientation === 'horizontal') nextTab = enabledTabs[(currentIndex - 1 + enabledTabs.length) % enabledTabs.length];
|
|
1578
|
+
if (event.key === 'ArrowDown' && orientation === 'vertical') nextTab = enabledTabs[(currentIndex + 1) % enabledTabs.length];
|
|
1579
|
+
if (event.key === 'ArrowUp' && orientation === 'vertical') nextTab = enabledTabs[(currentIndex - 1 + enabledTabs.length) % enabledTabs.length];
|
|
1580
|
+
if (event.key === 'Home') nextTab = enabledTabs[0];
|
|
1581
|
+
if (event.key === 'End') nextTab = enabledTabs[enabledTabs.length - 1];
|
|
1582
|
+
if (!nextTab) return;
|
|
1583
|
+
|
|
1584
|
+
event.preventDefault();
|
|
1585
|
+
root.select(nextTab, true);
|
|
1586
|
+
};
|
|
1587
|
+
|
|
1588
|
+
state.tablist.addEventListener('click', handleClick);
|
|
1589
|
+
state.tablist.addEventListener('keydown', handleKeydown);
|
|
1590
|
+
|
|
1591
|
+
root._destroy = () => {
|
|
1592
|
+
state.tablist.removeEventListener('click', handleClick);
|
|
1593
|
+
state.tablist.removeEventListener('keydown', handleKeydown);
|
|
1594
|
+
states.delete(root);
|
|
1595
|
+
delete root.refresh;
|
|
1596
|
+
delete root.select;
|
|
1597
|
+
};
|
|
1598
|
+
|
|
1599
|
+
root.dataset.tabsInitialized = 'true';
|
|
1600
|
+
root.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
1601
|
+
};
|
|
1602
|
+
|
|
1603
|
+
if (window.basecoat) {
|
|
1604
|
+
window.basecoat.register('tabs', {
|
|
1605
|
+
selector: '.tabs:not([data-tabs-initialized])',
|
|
1606
|
+
init: initTabs,
|
|
1607
|
+
refresh: refreshTabs,
|
|
1608
|
+
});
|
|
1609
|
+
}
|
|
1610
|
+
})();
|
|
1611
|
+
|
|
1612
|
+
}
|