basecoat-css 0.3.10 → 1.0.0-beta.0
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/README.md +8 -1
- package/dist/base/base.css +150 -0
- package/dist/basecoat-base.cdn.css +2942 -0
- package/dist/basecoat-base.cdn.min.css +2 -0
- package/dist/basecoat-base.css +2 -0
- package/dist/basecoat-components.css +32 -0
- package/dist/basecoat-luma.cdn.css +6073 -0
- package/dist/basecoat-luma.cdn.min.css +2 -0
- package/dist/basecoat-luma.css +2 -0
- package/dist/basecoat-lyra.cdn.css +6004 -0
- package/dist/basecoat-lyra.cdn.min.css +2 -0
- package/dist/basecoat-lyra.css +2 -0
- package/dist/basecoat-maia.cdn.css +6012 -0
- package/dist/basecoat-maia.cdn.min.css +2 -0
- package/dist/basecoat-maia.css +2 -0
- package/dist/basecoat-mira.cdn.css +6063 -0
- package/dist/basecoat-mira.cdn.min.css +2 -0
- package/dist/basecoat-mira.css +2 -0
- package/dist/basecoat-nova.cdn.css +6067 -0
- package/dist/basecoat-nova.cdn.min.css +2 -0
- package/dist/basecoat-nova.css +2 -0
- package/dist/basecoat-rhea.cdn.css +6092 -0
- package/dist/basecoat-rhea.cdn.min.css +2 -0
- package/dist/basecoat-rhea.css +2 -0
- package/dist/basecoat-sera.cdn.css +5840 -0
- package/dist/basecoat-sera.cdn.min.css +2 -0
- package/dist/basecoat-sera.css +2 -0
- package/dist/basecoat-vega.cdn.css +6023 -0
- package/dist/basecoat-vega.cdn.min.css +2 -0
- package/dist/basecoat-vega.css +2 -0
- package/dist/basecoat.all.css +1 -0
- package/dist/basecoat.cdn.css +5084 -2646
- package/dist/basecoat.cdn.min.css +1 -1
- package/dist/basecoat.css +1 -1311
- package/dist/components/alert.css +21 -0
- package/dist/components/badge.css +12 -0
- package/dist/components/button-group.css +54 -0
- package/dist/components/button.css +61 -0
- package/dist/components/card.css +22 -0
- package/dist/components/checkbox.css +12 -0
- package/dist/components/collapsible.css +21 -0
- package/dist/components/combobox.css +78 -0
- package/dist/components/command.css +133 -0
- package/dist/components/dialog.css +52 -0
- package/dist/components/dropdown-menu.css +48 -0
- package/dist/components/empty.css +26 -0
- package/dist/components/field.css +86 -0
- package/dist/components/form.css +9 -0
- package/dist/components/input-group.css +50 -0
- package/dist/components/input.css +32 -0
- package/dist/components/item.css +50 -0
- package/dist/components/kbd.css +10 -0
- package/dist/components/label.css +12 -0
- package/dist/components/native-select.css +12 -0
- package/dist/components/popover.css +81 -0
- package/dist/components/radio.css +11 -0
- package/dist/components/range.css +50 -0
- package/dist/components/select.css +64 -0
- package/dist/components/sidebar.css +101 -0
- package/dist/components/skeleton.css +6 -0
- package/dist/components/switch.css +8 -0
- package/dist/components/table.css +34 -0
- package/dist/components/tabs.css +31 -0
- package/dist/components/textarea.css +7 -0
- package/dist/components/toast.css +57 -0
- package/dist/components/tooltip.css +82 -0
- package/dist/js/all.js +1435 -789
- package/dist/js/all.min.js +1 -1
- package/dist/js/basecoat.js +95 -40
- package/dist/js/basecoat.min.js +1 -1
- package/dist/js/combobox.js +483 -0
- package/dist/js/combobox.min.js +1 -0
- package/dist/js/command.js +165 -126
- package/dist/js/command.min.js +1 -1
- package/dist/js/dropdown-menu.js +158 -133
- package/dist/js/dropdown-menu.min.js +1 -1
- package/dist/js/popover.js +22 -8
- package/dist/js/popover.min.js +1 -1
- package/dist/js/range.js +25 -0
- package/dist/js/range.min.js +1 -0
- package/dist/js/select.js +347 -359
- package/dist/js/select.min.js +1 -1
- package/dist/js/sidebar.js +27 -66
- package/dist/js/sidebar.min.js +1 -1
- package/dist/js/tabs.js +82 -38
- package/dist/js/tabs.min.js +1 -1
- package/dist/js/toast.js +42 -37
- package/dist/js/toast.min.js +1 -1
- package/dist/styles/luma.css +1308 -0
- package/dist/styles/lyra.css +1289 -0
- package/dist/styles/maia.css +1296 -0
- package/dist/styles/mira.css +1297 -0
- package/dist/styles/nova.css +1293 -0
- package/dist/styles/rhea.css +1305 -0
- package/dist/styles/sera.css +1316 -0
- package/dist/styles/vega.css +1293 -0
- package/package.json +47 -3
- package/dist/js/carousel.js +0 -192
- package/dist/js/carousel.min.js +0 -1
package/dist/js/all.js
CHANGED
|
@@ -2,435 +2,1042 @@
|
|
|
2
2
|
const componentRegistry = {};
|
|
3
3
|
let observer = null;
|
|
4
4
|
|
|
5
|
-
const registerComponent = (name,
|
|
5
|
+
const registerComponent = (name, selectorOrOptions, initFunction) => {
|
|
6
|
+
const options = typeof selectorOrOptions === 'object'
|
|
7
|
+
? selectorOrOptions
|
|
8
|
+
: { selector: selectorOrOptions, init: initFunction };
|
|
9
|
+
|
|
6
10
|
componentRegistry[name] = {
|
|
7
|
-
selector,
|
|
8
|
-
init:
|
|
11
|
+
selector: options.selector,
|
|
12
|
+
init: options.init,
|
|
13
|
+
refresh: options.refresh,
|
|
9
14
|
};
|
|
10
15
|
};
|
|
11
16
|
|
|
12
17
|
const initComponent = (element, componentName) => {
|
|
13
18
|
const component = componentRegistry[componentName];
|
|
14
19
|
if (!component) return;
|
|
15
|
-
|
|
20
|
+
|
|
16
21
|
try {
|
|
17
22
|
component.init(element);
|
|
23
|
+
if (element.hasAttribute(`data-${componentName}-initialized`)) {
|
|
24
|
+
element.dataset.basecoatComponent = componentName;
|
|
25
|
+
}
|
|
18
26
|
} catch (error) {
|
|
19
27
|
console.error(`Failed to initialize ${componentName}:`, error);
|
|
28
|
+
if (typeof element._destroy === 'function') {
|
|
29
|
+
try {
|
|
30
|
+
element._destroy();
|
|
31
|
+
} catch (destroyError) {
|
|
32
|
+
console.error(`Failed to clean up ${componentName} after initialization error:`, destroyError);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
delete element._destroy;
|
|
36
|
+
element.removeAttribute(`data-${componentName}-initialized`);
|
|
37
|
+
delete element.dataset.basecoatComponent;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const destroyComponent = (element) => {
|
|
42
|
+
if (!element || element.nodeType !== Node.ELEMENT_NODE) return;
|
|
43
|
+
const componentName = element.dataset?.basecoatComponent;
|
|
44
|
+
|
|
45
|
+
if (typeof element._destroy === 'function') {
|
|
46
|
+
try {
|
|
47
|
+
element._destroy();
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error('Failed to destroy Basecoat component:', error);
|
|
50
|
+
}
|
|
20
51
|
}
|
|
52
|
+
|
|
53
|
+
delete element._destroy;
|
|
54
|
+
if (componentName) element.removeAttribute(`data-${componentName}-initialized`);
|
|
55
|
+
delete element.dataset.basecoatComponent;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const destroyRemovedComponents = (node) => {
|
|
59
|
+
if (node.nodeType !== Node.ELEMENT_NODE) return;
|
|
60
|
+
if (node.isConnected) return;
|
|
61
|
+
|
|
62
|
+
if (node.dataset?.basecoatComponent) destroyComponent(node);
|
|
63
|
+
node.querySelectorAll('[data-basecoat-component]').forEach(destroyComponent);
|
|
21
64
|
};
|
|
22
65
|
|
|
23
66
|
const initAllComponents = () => {
|
|
24
|
-
Object.entries(componentRegistry).forEach(([name, { selector
|
|
25
|
-
document.querySelectorAll(selector).forEach(
|
|
67
|
+
Object.entries(componentRegistry).forEach(([name, { selector }]) => {
|
|
68
|
+
document.querySelectorAll(selector).forEach(element => initComponent(element, name));
|
|
26
69
|
});
|
|
27
70
|
};
|
|
28
71
|
|
|
29
72
|
const initNewComponents = (node) => {
|
|
30
73
|
if (node.nodeType !== Node.ELEMENT_NODE) return;
|
|
31
|
-
|
|
32
|
-
Object.entries(componentRegistry).forEach(([name, { selector
|
|
33
|
-
if (node.matches(selector))
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
node.querySelectorAll(selector).forEach(init);
|
|
74
|
+
|
|
75
|
+
Object.entries(componentRegistry).forEach(([name, { selector }]) => {
|
|
76
|
+
if (node.matches(selector)) initComponent(node, name);
|
|
77
|
+
node.querySelectorAll(selector).forEach(element => initComponent(element, name));
|
|
37
78
|
});
|
|
38
79
|
};
|
|
39
80
|
|
|
81
|
+
const refreshComponent = (element) => {
|
|
82
|
+
if (!element) return;
|
|
83
|
+
if (typeof element.refresh === 'function') {
|
|
84
|
+
element.refresh();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const componentName = element.dataset?.basecoatComponent;
|
|
89
|
+
const component = componentName ? componentRegistry[componentName] : null;
|
|
90
|
+
if (component?.refresh) {
|
|
91
|
+
component.refresh(element);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
40
95
|
const startObserver = () => {
|
|
41
96
|
if (observer) return;
|
|
42
|
-
|
|
97
|
+
|
|
43
98
|
observer = new MutationObserver((mutations) => {
|
|
44
99
|
mutations.forEach((mutation) => {
|
|
45
100
|
mutation.addedNodes.forEach(initNewComponents);
|
|
101
|
+
mutation.removedNodes.forEach(destroyRemovedComponents);
|
|
46
102
|
});
|
|
47
103
|
});
|
|
48
|
-
|
|
104
|
+
|
|
49
105
|
observer.observe(document.body, { childList: true, subtree: true });
|
|
50
106
|
};
|
|
51
107
|
|
|
52
108
|
const stopObserver = () => {
|
|
53
|
-
if (observer)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
109
|
+
if (!observer) return;
|
|
110
|
+
observer.disconnect();
|
|
111
|
+
observer = null;
|
|
57
112
|
};
|
|
58
113
|
|
|
59
|
-
const
|
|
114
|
+
const initRegisteredComponent = (componentName) => {
|
|
60
115
|
const component = componentRegistry[componentName];
|
|
61
116
|
if (!component) {
|
|
62
117
|
console.warn(`Component '${componentName}' not found in registry`);
|
|
63
118
|
return;
|
|
64
119
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const flag = `data-${componentName}-initialized`;
|
|
68
|
-
document.querySelectorAll(`[${flag}]`).forEach(el => {
|
|
69
|
-
el.removeAttribute(flag);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
document.querySelectorAll(component.selector).forEach(component.init);
|
|
120
|
+
|
|
121
|
+
document.querySelectorAll(component.selector).forEach(element => initComponent(element, componentName));
|
|
73
122
|
};
|
|
74
123
|
|
|
75
|
-
const
|
|
76
|
-
// Clear all initialization flags using the registry
|
|
77
|
-
Object.entries(componentRegistry).forEach(([name, { selector }]) => {
|
|
78
|
-
const flag = `data-${name}-initialized`;
|
|
79
|
-
document.querySelectorAll(`[${flag}]`).forEach(el => {
|
|
80
|
-
el.removeAttribute(flag);
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
124
|
+
const initAllRegisteredComponents = () => {
|
|
84
125
|
initAllComponents();
|
|
85
126
|
};
|
|
86
127
|
|
|
128
|
+
const setTheme = (mode) => {
|
|
129
|
+
const dark = mode === 'dark';
|
|
130
|
+
document.documentElement.classList.toggle('dark', dark);
|
|
131
|
+
try { localStorage.setItem('themeMode', dark ? 'dark' : 'light'); } catch (_) {}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const getTheme = () => document.documentElement.classList.contains('dark') ? 'dark' : 'light';
|
|
135
|
+
|
|
87
136
|
window.basecoat = {
|
|
88
137
|
register: registerComponent,
|
|
89
|
-
init:
|
|
90
|
-
initAll:
|
|
138
|
+
init: initRegisteredComponent,
|
|
139
|
+
initAll: initAllRegisteredComponents,
|
|
140
|
+
refresh: refreshComponent,
|
|
91
141
|
start: startObserver,
|
|
92
|
-
stop: stopObserver
|
|
142
|
+
stop: stopObserver,
|
|
143
|
+
theme: {
|
|
144
|
+
get: getTheme,
|
|
145
|
+
set: setTheme,
|
|
146
|
+
toggle: () => setTheme(getTheme() === 'dark' ? 'light' : 'dark'),
|
|
147
|
+
},
|
|
93
148
|
};
|
|
94
149
|
|
|
95
150
|
document.addEventListener('DOMContentLoaded', () => {
|
|
96
151
|
initAllComponents();
|
|
97
152
|
startObserver();
|
|
98
153
|
});
|
|
99
|
-
})();
|
|
154
|
+
})();
|
|
155
|
+
|
|
100
156
|
(() => {
|
|
157
|
+
const states = new WeakMap();
|
|
158
|
+
|
|
159
|
+
const isDisabled = (item) =>
|
|
160
|
+
item.hasAttribute('disabled') || item.getAttribute('aria-disabled') === 'true';
|
|
161
|
+
|
|
162
|
+
const getElements = (container) => ({
|
|
163
|
+
input: container.querySelector('header input'),
|
|
164
|
+
menu: container.querySelector('[role="menu"]'),
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const getItems = (menu) => {
|
|
168
|
+
const allItems = Array.from(menu.querySelectorAll('[role="menuitem"]'));
|
|
169
|
+
return {
|
|
170
|
+
allItems,
|
|
171
|
+
items: allItems.filter(item => !isDisabled(item)),
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
const scrollItemIntoMenu = (state, item) => {
|
|
176
|
+
const itemRect = item.getBoundingClientRect();
|
|
177
|
+
const menuRect = state.menu.getBoundingClientRect();
|
|
178
|
+
|
|
179
|
+
if (itemRect.top < menuRect.top) {
|
|
180
|
+
state.menu.scrollTop -= menuRect.top - itemRect.top;
|
|
181
|
+
} else if (itemRect.bottom > menuRect.bottom) {
|
|
182
|
+
state.menu.scrollTop += itemRect.bottom - menuRect.bottom;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const setActiveItem = (state, index) => {
|
|
187
|
+
if (state.activeIndex > -1 && state.items[state.activeIndex]) {
|
|
188
|
+
state.items[state.activeIndex].classList.remove('active');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
state.activeIndex = index;
|
|
192
|
+
|
|
193
|
+
if (state.activeIndex > -1) {
|
|
194
|
+
const activeItem = state.items[state.activeIndex];
|
|
195
|
+
activeItem.classList.add('active');
|
|
196
|
+
if (activeItem.id) {
|
|
197
|
+
state.input.setAttribute('aria-activedescendant', activeItem.id);
|
|
198
|
+
} else {
|
|
199
|
+
state.input.removeAttribute('aria-activedescendant');
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
state.input.removeAttribute('aria-activedescendant');
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const filterItems = (state) => {
|
|
207
|
+
if (state.manualFilter) {
|
|
208
|
+
setActiveItem(state, -1);
|
|
209
|
+
state.visibleItems = state.items.filter(item => item.getAttribute('aria-hidden') !== 'true');
|
|
210
|
+
if (state.visibleItems.length > 0) {
|
|
211
|
+
setActiveItem(state, state.items.indexOf(state.visibleItems[0]));
|
|
212
|
+
}
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const searchTerm = state.input.value.trim().toLowerCase();
|
|
217
|
+
|
|
218
|
+
setActiveItem(state, -1);
|
|
219
|
+
state.visibleItems = [];
|
|
220
|
+
|
|
221
|
+
state.allItems.forEach(item => {
|
|
222
|
+
if (item.hasAttribute('data-force')) {
|
|
223
|
+
item.setAttribute('aria-hidden', 'false');
|
|
224
|
+
if (state.items.includes(item)) state.visibleItems.push(item);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const itemText = (item.dataset.filter || item.textContent).trim().toLowerCase();
|
|
229
|
+
const keywordList = (item.dataset.keywords || '')
|
|
230
|
+
.toLowerCase()
|
|
231
|
+
.split(/[\s,]+/)
|
|
232
|
+
.filter(Boolean);
|
|
233
|
+
const matchesKeyword = keywordList.some(keyword => keyword.includes(searchTerm));
|
|
234
|
+
const matches = itemText.includes(searchTerm) || matchesKeyword;
|
|
235
|
+
item.setAttribute('aria-hidden', String(!matches));
|
|
236
|
+
if (matches && state.items.includes(item)) state.visibleItems.push(item);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
if (state.visibleItems.length > 0) {
|
|
240
|
+
setActiveItem(state, state.items.indexOf(state.visibleItems[0]));
|
|
241
|
+
scrollItemIntoMenu(state, state.visibleItems[0]);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const refreshCommand = (container) => {
|
|
246
|
+
const state = states.get(container);
|
|
247
|
+
if (!state) return;
|
|
248
|
+
|
|
249
|
+
const elements = getElements(container);
|
|
250
|
+
if (!elements.input || !elements.menu) {
|
|
251
|
+
const missing = [];
|
|
252
|
+
if (!elements.input) missing.push('input');
|
|
253
|
+
if (!elements.menu) missing.push('menu');
|
|
254
|
+
console.error(`Command component refresh failed. Missing element(s): ${missing.join(', ')}`, container);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
Object.assign(state, elements, getItems(elements.menu));
|
|
259
|
+
state.manualFilter = container.dataset.filter === 'manual';
|
|
260
|
+
filterItems(state);
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const handleKeyNavigation = (event, state) => {
|
|
264
|
+
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End'].includes(event.key)) return;
|
|
265
|
+
|
|
266
|
+
if (event.key === 'Enter') {
|
|
267
|
+
event.preventDefault();
|
|
268
|
+
if (state.activeIndex > -1) state.items[state.activeIndex]?.click();
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
if (state.visibleItems.length === 0) return;
|
|
273
|
+
|
|
274
|
+
event.preventDefault();
|
|
275
|
+
|
|
276
|
+
const currentVisibleIndex = state.activeIndex > -1 ? state.visibleItems.indexOf(state.items[state.activeIndex]) : -1;
|
|
277
|
+
let nextVisibleIndex = currentVisibleIndex;
|
|
278
|
+
|
|
279
|
+
if (event.key === 'ArrowDown' && currentVisibleIndex < state.visibleItems.length - 1) nextVisibleIndex = currentVisibleIndex + 1;
|
|
280
|
+
if (event.key === 'ArrowUp') nextVisibleIndex = currentVisibleIndex > 0 ? currentVisibleIndex - 1 : 0;
|
|
281
|
+
if (event.key === 'Home') nextVisibleIndex = 0;
|
|
282
|
+
if (event.key === 'End') nextVisibleIndex = state.visibleItems.length - 1;
|
|
283
|
+
|
|
284
|
+
if (nextVisibleIndex !== currentVisibleIndex) {
|
|
285
|
+
const newActiveItem = state.visibleItems[nextVisibleIndex];
|
|
286
|
+
setActiveItem(state, state.items.indexOf(newActiveItem));
|
|
287
|
+
scrollItemIntoMenu(state, newActiveItem);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
|
|
101
291
|
const initCommand = (container) => {
|
|
102
|
-
|
|
103
|
-
const menu = container.querySelector('[role="menu"]');
|
|
292
|
+
if (container.dataset.commandInitialized) return;
|
|
104
293
|
|
|
105
|
-
|
|
294
|
+
const state = { activeIndex: -1, allItems: [], items: [], visibleItems: [], manualFilter: false };
|
|
295
|
+
states.set(container, state);
|
|
296
|
+
|
|
297
|
+
container.refresh = () => refreshCommand(container);
|
|
298
|
+
|
|
299
|
+
const elements = getElements(container);
|
|
300
|
+
if (!elements.input || !elements.menu) {
|
|
106
301
|
const missing = [];
|
|
107
|
-
if (!input) missing.push('input');
|
|
108
|
-
if (!menu) missing.push('menu');
|
|
302
|
+
if (!elements.input) missing.push('input');
|
|
303
|
+
if (!elements.menu) missing.push('menu');
|
|
109
304
|
console.error(`Command component initialization failed. Missing element(s): ${missing.join(', ')}`, container);
|
|
305
|
+
states.delete(container);
|
|
306
|
+
delete container.refresh;
|
|
110
307
|
return;
|
|
111
308
|
}
|
|
309
|
+
Object.assign(state, elements);
|
|
112
310
|
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const setActiveItem = (index) => {
|
|
122
|
-
if (activeIndex > -1 && menuItems[activeIndex]) {
|
|
123
|
-
menuItems[activeIndex].classList.remove('active');
|
|
311
|
+
const handleInput = () => filterItems(state);
|
|
312
|
+
const handleInputKeydown = (event) => handleKeyNavigation(event, state);
|
|
313
|
+
const handleMenuMousemove = (event) => {
|
|
314
|
+
const menuItem = event.target.closest('[role="menuitem"]');
|
|
315
|
+
if (menuItem && state.visibleItems.includes(menuItem)) {
|
|
316
|
+
const index = state.items.indexOf(menuItem);
|
|
317
|
+
if (index !== state.activeIndex) setActiveItem(state, index);
|
|
124
318
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
if (activeItem.id) {
|
|
132
|
-
input.setAttribute('aria-activedescendant', activeItem.id);
|
|
133
|
-
} else {
|
|
134
|
-
input.removeAttribute('aria-activedescendant');
|
|
135
|
-
}
|
|
136
|
-
} else {
|
|
137
|
-
input.removeAttribute('aria-activedescendant');
|
|
319
|
+
};
|
|
320
|
+
const handleMenuClick = (event) => {
|
|
321
|
+
const clickedItem = event.target.closest('[role="menuitem"]');
|
|
322
|
+
if (clickedItem && state.visibleItems.includes(clickedItem)) {
|
|
323
|
+
const dialog = container.closest('dialog.command-dialog');
|
|
324
|
+
if (dialog && !clickedItem.hasAttribute('data-keep-command-open')) dialog.close();
|
|
138
325
|
}
|
|
139
326
|
};
|
|
140
327
|
|
|
141
|
-
|
|
142
|
-
|
|
328
|
+
state.input.addEventListener('input', handleInput);
|
|
329
|
+
state.input.addEventListener('keydown', handleInputKeydown);
|
|
330
|
+
state.menu.addEventListener('mousemove', handleMenuMousemove);
|
|
331
|
+
state.menu.addEventListener('click', handleMenuClick);
|
|
332
|
+
|
|
333
|
+
container._destroy = () => {
|
|
334
|
+
state.input.removeEventListener('input', handleInput);
|
|
335
|
+
state.input.removeEventListener('keydown', handleInputKeydown);
|
|
336
|
+
state.menu.removeEventListener('mousemove', handleMenuMousemove);
|
|
337
|
+
state.menu.removeEventListener('click', handleMenuClick);
|
|
338
|
+
states.delete(container);
|
|
339
|
+
delete container.refresh;
|
|
340
|
+
};
|
|
143
341
|
|
|
144
|
-
|
|
342
|
+
refreshCommand(container);
|
|
343
|
+
container.dataset.commandInitialized = 'true';
|
|
344
|
+
container.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
345
|
+
};
|
|
145
346
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
347
|
+
if (window.basecoat) {
|
|
348
|
+
window.basecoat.register('command', {
|
|
349
|
+
selector: '.command:not([data-command-initialized])',
|
|
350
|
+
init: initCommand,
|
|
351
|
+
refresh: refreshCommand,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
})();
|
|
155
355
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
.toLowerCase()
|
|
159
|
-
.split(/[\s,]+/)
|
|
160
|
-
.filter(Boolean);
|
|
161
|
-
const matchesKeyword = keywordList.some(keyword => keyword.includes(searchTerm));
|
|
162
|
-
const matches = itemText.includes(searchTerm) || matchesKeyword;
|
|
163
|
-
item.setAttribute('aria-hidden', String(!matches));
|
|
164
|
-
if (matches && menuItems.includes(item)) {
|
|
165
|
-
visibleMenuItems.push(item);
|
|
166
|
-
}
|
|
167
|
-
});
|
|
356
|
+
(() => {
|
|
357
|
+
const states = new WeakMap();
|
|
168
358
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
359
|
+
const getElements = (root) => {
|
|
360
|
+
const input = root.querySelector(':scope > input[role="combobox"], :scope > .combobox-chips input[role="combobox"]');
|
|
361
|
+
const chips = root.querySelector(':scope > .combobox-chips');
|
|
362
|
+
const popover = root.querySelector(':scope > [data-popover]');
|
|
363
|
+
const listbox = popover ? popover.querySelector('[role="listbox"]') : null;
|
|
364
|
+
const hiddenInput = root.querySelector(':scope > input[type="hidden"]');
|
|
365
|
+
return { input, chips, popover, listbox, hiddenInput };
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
const getValue = option => option.dataset.value ?? option.textContent.trim();
|
|
369
|
+
const getLabel = option => option.dataset.label || option.textContent.trim();
|
|
370
|
+
const getFormat = root => root.dataset.format === 'object' ? 'object' : 'value';
|
|
371
|
+
const isDisabled = option => option.getAttribute('aria-disabled') === 'true';
|
|
372
|
+
const toSelected = option => ({ value: getValue(option), label: getLabel(option) });
|
|
373
|
+
const normalizeEntry = entry => {
|
|
374
|
+
if (entry && typeof entry === 'object') {
|
|
375
|
+
const value = entry.value == null ? '' : String(entry.value);
|
|
376
|
+
return value ? { value, label: String(entry.label ?? entry.value) } : null;
|
|
377
|
+
}
|
|
378
|
+
const value = entry == null ? '' : String(entry);
|
|
379
|
+
return value ? { value, label: value } : null;
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
const getOptions = (listbox) => {
|
|
383
|
+
const allOptions = Array.from(listbox.querySelectorAll('[role="option"]'));
|
|
384
|
+
return {
|
|
385
|
+
allOptions,
|
|
386
|
+
options: allOptions.filter(option => !isDisabled(option)),
|
|
173
387
|
};
|
|
388
|
+
};
|
|
174
389
|
|
|
175
|
-
|
|
390
|
+
const getSelection = (state) => Array.from(state.selected.values());
|
|
391
|
+
const getCanonicalValue = (state) => state.isMultiple ? getSelection(state).map(item => item.value) : (getSelection(state)[0]?.value || '');
|
|
392
|
+
const getSelectedDetail = (state) => state.isMultiple ? getSelection(state) : (getSelection(state)[0] || null);
|
|
176
393
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
394
|
+
const serializeSelection = (state) => {
|
|
395
|
+
const selected = getSelection(state);
|
|
396
|
+
if (state.format === 'object') {
|
|
397
|
+
return JSON.stringify(state.isMultiple ? selected : (selected[0] || null));
|
|
398
|
+
}
|
|
399
|
+
const value = selected.map(item => item.value);
|
|
400
|
+
return state.isMultiple ? JSON.stringify(value) : (value[0] || '');
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
const parseStoredSelection = (storedValue, inputValue, state) => {
|
|
404
|
+
if (state.isMultiple) {
|
|
405
|
+
let parsed = [];
|
|
406
|
+
try {
|
|
407
|
+
parsed = JSON.parse(storedValue || '[]');
|
|
408
|
+
} catch (_) {
|
|
409
|
+
parsed = [];
|
|
180
410
|
}
|
|
411
|
+
if (!Array.isArray(parsed)) return [];
|
|
412
|
+
return parsed.map(item => {
|
|
413
|
+
const entry = normalizeEntry(state.format === 'object' ? item : { value: item, label: state.selected.get(String(item))?.label ?? item });
|
|
414
|
+
if (!entry) return null;
|
|
415
|
+
const option = state.options.find(opt => getValue(opt) === entry.value);
|
|
416
|
+
return option ? toSelected(option) : entry;
|
|
417
|
+
}).filter(Boolean);
|
|
418
|
+
}
|
|
181
419
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
return;
|
|
420
|
+
if (state.format === 'object') {
|
|
421
|
+
try {
|
|
422
|
+
const entry = normalizeEntry(JSON.parse(storedValue || 'null'));
|
|
423
|
+
if (!entry) return [];
|
|
424
|
+
const option = state.options.find(opt => getValue(opt) === entry.value);
|
|
425
|
+
return [option ? toSelected(option) : entry];
|
|
426
|
+
} catch (_) {
|
|
427
|
+
return [];
|
|
188
428
|
}
|
|
429
|
+
}
|
|
189
430
|
|
|
190
|
-
|
|
431
|
+
const value = storedValue || '';
|
|
432
|
+
if (!value) return [];
|
|
433
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
434
|
+
return [option ? toSelected(option) : { value, label: state.selected.get(value)?.label || inputValue || value }];
|
|
435
|
+
};
|
|
191
436
|
|
|
192
|
-
|
|
437
|
+
const scrollOptionIntoListbox = (state, option) => {
|
|
438
|
+
const optionRect = option.getBoundingClientRect();
|
|
439
|
+
const listboxRect = state.listbox.getBoundingClientRect();
|
|
193
440
|
|
|
194
|
-
|
|
195
|
-
|
|
441
|
+
if (optionRect.top < listboxRect.top) {
|
|
442
|
+
state.listbox.scrollTop -= listboxRect.top - optionRect.top;
|
|
443
|
+
} else if (optionRect.bottom > listboxRect.bottom) {
|
|
444
|
+
state.listbox.scrollTop += optionRect.bottom - listboxRect.bottom;
|
|
445
|
+
}
|
|
446
|
+
};
|
|
196
447
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
case 'ArrowUp':
|
|
204
|
-
if (currentVisibleIndex > 0) {
|
|
205
|
-
nextVisibleIndex = currentVisibleIndex - 1;
|
|
206
|
-
} else if (currentVisibleIndex === -1) {
|
|
207
|
-
nextVisibleIndex = 0;
|
|
208
|
-
}
|
|
209
|
-
break;
|
|
210
|
-
case 'Home':
|
|
211
|
-
nextVisibleIndex = 0;
|
|
212
|
-
break;
|
|
213
|
-
case 'End':
|
|
214
|
-
nextVisibleIndex = visibleMenuItems.length - 1;
|
|
215
|
-
break;
|
|
216
|
-
}
|
|
448
|
+
const setActiveOption = (state, index) => {
|
|
449
|
+
if (state.activeIndex > -1 && state.options[state.activeIndex]) {
|
|
450
|
+
state.options[state.activeIndex].classList.remove('active');
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
state.activeIndex = index;
|
|
217
454
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
455
|
+
if (state.activeIndex > -1) {
|
|
456
|
+
const activeOption = state.options[state.activeIndex];
|
|
457
|
+
activeOption.classList.add('active');
|
|
458
|
+
if (!activeOption.id) activeOption.id = `${state.listbox.id || state.root.id || 'combobox'}-option-${state.activeIndex}`;
|
|
459
|
+
state.input.setAttribute('aria-activedescendant', activeOption.id);
|
|
460
|
+
} else {
|
|
461
|
+
state.input.removeAttribute('aria-activedescendant');
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
const syncEmptyState = (state) => {
|
|
466
|
+
state.popover.dataset.empty = String(state.visibleOptions.length === 0);
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
const filterOptions = (state, { preserveActive = false } = {}) => {
|
|
470
|
+
const previousActive = state.activeIndex > -1 ? state.options[state.activeIndex] : null;
|
|
471
|
+
state.visibleOptions = [];
|
|
472
|
+
|
|
473
|
+
if (state.manualFilter) {
|
|
474
|
+
state.visibleOptions = state.options.filter(option => option.getAttribute('aria-hidden') !== 'true');
|
|
475
|
+
|
|
476
|
+
if (preserveActive && previousActive && state.visibleOptions.includes(previousActive)) {
|
|
477
|
+
setActiveOption(state, state.options.indexOf(previousActive));
|
|
478
|
+
} else {
|
|
479
|
+
setActiveOption(state, state.autoHighlight && state.visibleOptions.length > 0 ? state.options.indexOf(state.visibleOptions[0]) : -1);
|
|
222
480
|
}
|
|
223
|
-
|
|
481
|
+
syncEmptyState(state);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
224
484
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
485
|
+
const search = state.input.value.trim().toLowerCase();
|
|
486
|
+
|
|
487
|
+
state.allOptions.forEach(option => {
|
|
488
|
+
if (option.hasAttribute('data-force')) {
|
|
489
|
+
option.setAttribute('aria-hidden', 'false');
|
|
490
|
+
if (state.options.includes(option)) state.visibleOptions.push(option);
|
|
491
|
+
return;
|
|
232
492
|
}
|
|
493
|
+
|
|
494
|
+
const optionText = (option.dataset.filter || option.dataset.label || option.textContent).trim().toLowerCase();
|
|
495
|
+
const keywords = (option.dataset.keywords || '').toLowerCase().split(/[\s,]+/).filter(Boolean);
|
|
496
|
+
const matches = !search || optionText.includes(search) || keywords.some(keyword => keyword.includes(search));
|
|
497
|
+
option.setAttribute('aria-hidden', String(!matches));
|
|
498
|
+
if (matches && state.options.includes(option)) state.visibleOptions.push(option);
|
|
233
499
|
});
|
|
234
500
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
501
|
+
if (preserveActive && previousActive && state.visibleOptions.includes(previousActive)) {
|
|
502
|
+
setActiveOption(state, state.options.indexOf(previousActive));
|
|
503
|
+
} else {
|
|
504
|
+
setActiveOption(state, state.autoHighlight && state.visibleOptions.length > 0 ? state.options.indexOf(state.visibleOptions[0]) : -1);
|
|
505
|
+
}
|
|
506
|
+
syncEmptyState(state);
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
const renderChips = (root) => {
|
|
510
|
+
const state = states.get(root);
|
|
511
|
+
if (!state.chips) return;
|
|
512
|
+
|
|
513
|
+
state.chips.querySelectorAll('.combobox-chip').forEach(chip => chip.remove());
|
|
514
|
+
|
|
515
|
+
getSelection(state).forEach(entry => {
|
|
516
|
+
const chip = document.createElement('span');
|
|
517
|
+
chip.className = 'combobox-chip';
|
|
518
|
+
chip.dataset.value = entry.value;
|
|
519
|
+
chip.textContent = entry.label;
|
|
520
|
+
|
|
521
|
+
const remove = document.createElement('button');
|
|
522
|
+
remove.type = 'button';
|
|
523
|
+
remove.className = 'combobox-chip-remove';
|
|
524
|
+
remove.setAttribute('aria-label', `Remove ${entry.label}`);
|
|
525
|
+
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>';
|
|
526
|
+
remove.addEventListener('click', () => root.deselect(entry.value));
|
|
527
|
+
|
|
528
|
+
chip.appendChild(remove);
|
|
529
|
+
state.chips.insertBefore(chip, state.input);
|
|
530
|
+
});
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
const syncSelectedOptions = (state) => {
|
|
534
|
+
state.options.forEach(option => {
|
|
535
|
+
if (state.selected.has(getValue(option))) {
|
|
536
|
+
option.setAttribute('aria-selected', 'true');
|
|
537
|
+
} else {
|
|
538
|
+
option.removeAttribute('aria-selected');
|
|
242
539
|
}
|
|
243
540
|
});
|
|
541
|
+
};
|
|
244
542
|
|
|
245
|
-
|
|
543
|
+
const setSelected = (root, entries, triggerEvent = true) => {
|
|
544
|
+
const state = states.get(root);
|
|
545
|
+
const normalized = (Array.isArray(entries) ? entries : [entries]).map(normalizeEntry).filter(Boolean);
|
|
546
|
+
|
|
547
|
+
state.selected.clear();
|
|
548
|
+
if (state.isMultiple) {
|
|
549
|
+
normalized.forEach(entry => state.selected.set(entry.value, entry));
|
|
550
|
+
state.input.value = '';
|
|
551
|
+
} else if (normalized[0]) {
|
|
552
|
+
state.selected.set(normalized[0].value, normalized[0]);
|
|
553
|
+
state.input.value = normalized[0].label;
|
|
554
|
+
} else {
|
|
555
|
+
state.input.value = '';
|
|
556
|
+
}
|
|
246
557
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
558
|
+
state.hiddenInput.value = serializeSelection(state);
|
|
559
|
+
syncSelectedOptions(state);
|
|
560
|
+
if (state.isMultiple) {
|
|
561
|
+
renderChips(root);
|
|
562
|
+
filterOptions(state, { preserveActive: true });
|
|
250
563
|
}
|
|
251
564
|
|
|
252
|
-
|
|
253
|
-
|
|
565
|
+
if (triggerEvent) {
|
|
566
|
+
root.dispatchEvent(new CustomEvent('change', {
|
|
567
|
+
detail: { value: getCanonicalValue(state), selected: getSelectedDetail(state) },
|
|
568
|
+
bubbles: true,
|
|
569
|
+
}));
|
|
570
|
+
}
|
|
254
571
|
};
|
|
255
572
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
573
|
+
const closePopover = (state, focusInput = false) => {
|
|
574
|
+
if (state.popover.getAttribute('aria-hidden') === 'true') return;
|
|
575
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
576
|
+
state.input.setAttribute('aria-expanded', 'false');
|
|
577
|
+
setActiveOption(state, -1);
|
|
578
|
+
if (focusInput) state.input.focus();
|
|
579
|
+
};
|
|
260
580
|
|
|
261
|
-
(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
if (!trigger || !menu || !popover) {
|
|
581
|
+
const refreshCombobox = (root) => {
|
|
582
|
+
const state = states.get(root);
|
|
583
|
+
if (!state) return;
|
|
584
|
+
|
|
585
|
+
const elements = getElements(root);
|
|
586
|
+
if (!elements.input || !elements.popover || !elements.listbox || !elements.hiddenInput) {
|
|
268
587
|
const missing = [];
|
|
269
|
-
if (!
|
|
270
|
-
if (!
|
|
271
|
-
if (!
|
|
272
|
-
|
|
588
|
+
if (!elements.input) missing.push('input');
|
|
589
|
+
if (!elements.popover) missing.push('popover');
|
|
590
|
+
if (!elements.listbox) missing.push('listbox');
|
|
591
|
+
if (!elements.hiddenInput) missing.push('hidden input');
|
|
592
|
+
console.error(`Combobox refresh failed. Missing element(s): ${missing.join(', ')}`, root);
|
|
273
593
|
return;
|
|
274
594
|
}
|
|
275
595
|
|
|
276
|
-
|
|
277
|
-
|
|
596
|
+
const previousValue = elements.hiddenInput.value;
|
|
597
|
+
const previousInputValue = elements.input.value;
|
|
598
|
+
Object.assign(state, elements, getOptions(elements.listbox));
|
|
599
|
+
state.isMultiple = state.listbox.getAttribute('aria-multiselectable') === 'true';
|
|
600
|
+
state.closeOnSelect = root.dataset.closeOnSelect === 'true';
|
|
601
|
+
state.autoHighlight = root.dataset.autoHighlight === 'true';
|
|
602
|
+
state.manualFilter = root.dataset.filter === 'manual';
|
|
603
|
+
state.format = getFormat(root);
|
|
604
|
+
|
|
605
|
+
const stored = parseStoredSelection(previousValue, previousInputValue, state);
|
|
606
|
+
if (stored.length > 0) {
|
|
607
|
+
setSelected(root, stored, false);
|
|
608
|
+
} else {
|
|
609
|
+
const selectedOptions = state.options.filter(option => option.getAttribute('aria-selected') === 'true').map(toSelected);
|
|
610
|
+
if (selectedOptions.length > 0) {
|
|
611
|
+
setSelected(root, state.isMultiple ? selectedOptions : selectedOptions[0], false);
|
|
612
|
+
} else if (state.isMultiple) {
|
|
613
|
+
setSelected(root, [], false);
|
|
614
|
+
} else {
|
|
615
|
+
state.input.value = previousInputValue;
|
|
616
|
+
state.selected.clear();
|
|
617
|
+
state.hiddenInput.value = '';
|
|
618
|
+
syncSelectedOptions(state);
|
|
619
|
+
filterOptions(state, { preserveActive: true });
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
};
|
|
278
623
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
624
|
+
const resolveEntry = (state, value) => {
|
|
625
|
+
if (value && typeof value === 'object') {
|
|
626
|
+
const entry = normalizeEntry(value);
|
|
627
|
+
if (!entry) return null;
|
|
628
|
+
const option = state.options.find(opt => getValue(opt) === entry.value);
|
|
629
|
+
return option ? toSelected(option) : entry;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
633
|
+
if (option) return toSelected(option);
|
|
634
|
+
const existing = state.selected.get(String(value));
|
|
635
|
+
return existing || null;
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
const selectValue = (root, value) => {
|
|
639
|
+
const state = states.get(root);
|
|
640
|
+
const entry = resolveEntry(state, value);
|
|
641
|
+
if (!entry) return;
|
|
642
|
+
|
|
643
|
+
if (state.isMultiple) {
|
|
644
|
+
setSelected(root, [...getSelection(state).filter(item => item.value !== entry.value), entry]);
|
|
645
|
+
if (state.closeOnSelect) root.close(true);
|
|
646
|
+
} else {
|
|
647
|
+
setSelected(root, entry);
|
|
648
|
+
root.close(true);
|
|
649
|
+
}
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
const deselectValue = (root, value) => {
|
|
653
|
+
const state = states.get(root);
|
|
654
|
+
if (!state.isMultiple) return;
|
|
655
|
+
const normalized = String(value);
|
|
656
|
+
if (!state.selected.has(normalized)) return;
|
|
657
|
+
setSelected(root, getSelection(state).filter(item => item.value !== normalized));
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
const handleKeydown = (event, root) => {
|
|
661
|
+
const state = states.get(root);
|
|
662
|
+
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End', 'Escape', 'Backspace'].includes(event.key)) return;
|
|
663
|
+
|
|
664
|
+
const isOpen = state.popover.getAttribute('aria-hidden') === 'false';
|
|
665
|
+
|
|
666
|
+
if (event.key === 'Backspace' && state.isMultiple && state.input.value === '') {
|
|
667
|
+
const selected = getSelection(state);
|
|
668
|
+
const last = selected[selected.length - 1];
|
|
669
|
+
if (last) root.deselect(last.value);
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
if (event.key === 'Escape') {
|
|
674
|
+
root.close(true);
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
if (!isOpen && ['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) {
|
|
679
|
+
event.preventDefault();
|
|
680
|
+
root.open();
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
if (state.popover.getAttribute('aria-hidden') === 'true') return;
|
|
684
|
+
|
|
685
|
+
if (event.key === 'Enter') {
|
|
686
|
+
if (state.activeIndex > -1) {
|
|
687
|
+
event.preventDefault();
|
|
688
|
+
const option = state.options[state.activeIndex];
|
|
689
|
+
state.isMultiple ? root.toggle(getValue(option)) : root.select(getValue(option));
|
|
287
690
|
}
|
|
288
|
-
|
|
289
|
-
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key) || state.visibleOptions.length === 0) return;
|
|
695
|
+
event.preventDefault();
|
|
696
|
+
|
|
697
|
+
const currentVisibleIndex = state.activeIndex > -1 ? state.visibleOptions.indexOf(state.options[state.activeIndex]) : -1;
|
|
698
|
+
let nextVisibleIndex = currentVisibleIndex;
|
|
699
|
+
|
|
700
|
+
if (event.key === 'ArrowDown') nextVisibleIndex = Math.min(currentVisibleIndex + 1, state.visibleOptions.length - 1);
|
|
701
|
+
if (event.key === 'ArrowUp') nextVisibleIndex = currentVisibleIndex <= 0 ? 0 : currentVisibleIndex - 1;
|
|
702
|
+
if (event.key === 'Home') nextVisibleIndex = 0;
|
|
703
|
+
if (event.key === 'End') nextVisibleIndex = state.visibleOptions.length - 1;
|
|
704
|
+
|
|
705
|
+
const nextOption = state.visibleOptions[nextVisibleIndex];
|
|
706
|
+
setActiveOption(state, state.options.indexOf(nextOption));
|
|
707
|
+
scrollOptionIntoListbox(state, nextOption);
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
const initCombobox = (root) => {
|
|
711
|
+
if (root.dataset.comboboxInitialized) return;
|
|
712
|
+
|
|
713
|
+
const state = { root, activeIndex: -1, allOptions: [], options: [], visibleOptions: [], selected: new Map(), format: 'value', manualFilter: false };
|
|
714
|
+
states.set(root, state);
|
|
715
|
+
root.refresh = () => refreshCombobox(root);
|
|
716
|
+
|
|
717
|
+
refreshCombobox(root);
|
|
718
|
+
if (!state.input || !state.popover || !state.listbox || !state.hiddenInput) {
|
|
719
|
+
states.delete(root);
|
|
720
|
+
delete root.refresh;
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
root.open = () => {
|
|
725
|
+
if (state.popover.getAttribute('aria-hidden') === 'false') return;
|
|
726
|
+
document.dispatchEvent(new CustomEvent('basecoat:popover', { detail: { source: root } }));
|
|
727
|
+
root.refresh();
|
|
728
|
+
filterOptions(state);
|
|
729
|
+
state.popover.setAttribute('aria-hidden', 'false');
|
|
730
|
+
state.input.setAttribute('aria-expanded', 'true');
|
|
290
731
|
};
|
|
732
|
+
root.close = (focusInput = false) => closePopover(state, focusInput);
|
|
733
|
+
|
|
734
|
+
root.select = (value) => selectValue(root, value);
|
|
735
|
+
root.selectByValue = root.select;
|
|
736
|
+
root.setValue = (value) => {
|
|
737
|
+
const entries = state.isMultiple ? (Array.isArray(value) ? value : (value == null ? [] : [value])) : [value];
|
|
738
|
+
const resolved = entries.map(entry => resolveEntry(state, entry) || normalizeEntry(entry)).filter(Boolean);
|
|
739
|
+
setSelected(root, state.isMultiple ? resolved : resolved[0]);
|
|
740
|
+
};
|
|
741
|
+
if (state.isMultiple) {
|
|
742
|
+
root.deselect = (value) => deselectValue(root, value);
|
|
743
|
+
root.toggle = (value) => {
|
|
744
|
+
const entry = resolveEntry(state, value);
|
|
745
|
+
if (!entry) return;
|
|
746
|
+
state.selected.has(entry.value) ? root.deselect(entry.value) : root.select(entry);
|
|
747
|
+
};
|
|
748
|
+
root.selectAll = () => setSelected(root, state.options.map(toSelected));
|
|
749
|
+
root.selectNone = () => setSelected(root, []);
|
|
750
|
+
}
|
|
291
751
|
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
item.getAttribute('aria-disabled') !== 'true'
|
|
302
|
-
);
|
|
303
|
-
|
|
304
|
-
if (menuItems.length > 0 && initialSelection) {
|
|
305
|
-
if (initialSelection === 'first') {
|
|
306
|
-
setActiveItem(0);
|
|
307
|
-
} else if (initialSelection === 'last') {
|
|
308
|
-
setActiveItem(menuItems.length - 1);
|
|
309
|
-
}
|
|
752
|
+
const handleInputFocus = root.open;
|
|
753
|
+
const handleInputClick = root.open;
|
|
754
|
+
const handleInput = () => {
|
|
755
|
+
root.open();
|
|
756
|
+
filterOptions(state);
|
|
757
|
+
if (!state.isMultiple) {
|
|
758
|
+
state.hiddenInput.value = '';
|
|
759
|
+
state.selected.clear();
|
|
760
|
+
syncSelectedOptions(state);
|
|
310
761
|
}
|
|
311
762
|
};
|
|
763
|
+
const handleInputKeydown = (event) => handleKeydown(event, root);
|
|
764
|
+
const handleListboxMousemove = (event) => {
|
|
765
|
+
const option = event.target.closest('[role="option"]');
|
|
766
|
+
if (option && state.visibleOptions.includes(option)) setActiveOption(state, state.options.indexOf(option));
|
|
767
|
+
};
|
|
768
|
+
const handleListboxClick = (event) => {
|
|
769
|
+
const option = event.target.closest('[role="option"]');
|
|
770
|
+
if (!option || !state.options.includes(option)) return;
|
|
771
|
+
state.isMultiple ? root.toggle(getValue(option)) : root.select(getValue(option));
|
|
772
|
+
if (state.isMultiple && !state.closeOnSelect) state.input.focus();
|
|
773
|
+
};
|
|
774
|
+
const handleDocumentClick = (event) => {
|
|
775
|
+
if (!root.contains(event.target)) root.close(false);
|
|
776
|
+
};
|
|
777
|
+
const handleDocumentPopover = (event) => {
|
|
778
|
+
if (event.detail.source !== root) root.close(false);
|
|
779
|
+
};
|
|
312
780
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
781
|
+
state.input.addEventListener('focus', handleInputFocus);
|
|
782
|
+
state.input.addEventListener('click', handleInputClick);
|
|
783
|
+
state.input.addEventListener('input', handleInput);
|
|
784
|
+
state.input.addEventListener('keydown', handleInputKeydown);
|
|
785
|
+
state.listbox.addEventListener('mousemove', handleListboxMousemove);
|
|
786
|
+
state.listbox.addEventListener('click', handleListboxClick);
|
|
787
|
+
document.addEventListener('click', handleDocumentClick);
|
|
788
|
+
document.addEventListener('basecoat:popover', handleDocumentPopover);
|
|
789
|
+
|
|
790
|
+
root._destroy = () => {
|
|
791
|
+
state.input.removeEventListener('focus', handleInputFocus);
|
|
792
|
+
state.input.removeEventListener('click', handleInputClick);
|
|
793
|
+
state.input.removeEventListener('input', handleInput);
|
|
794
|
+
state.input.removeEventListener('keydown', handleInputKeydown);
|
|
795
|
+
state.listbox.removeEventListener('mousemove', handleListboxMousemove);
|
|
796
|
+
state.listbox.removeEventListener('click', handleListboxClick);
|
|
797
|
+
document.removeEventListener('click', handleDocumentClick);
|
|
798
|
+
document.removeEventListener('basecoat:popover', handleDocumentPopover);
|
|
799
|
+
state.chips?.querySelectorAll('.combobox-chip').forEach(chip => chip.remove());
|
|
800
|
+
states.delete(root);
|
|
801
|
+
delete root.refresh;
|
|
802
|
+
delete root.open;
|
|
803
|
+
delete root.close;
|
|
804
|
+
delete root.select;
|
|
805
|
+
delete root.selectByValue;
|
|
806
|
+
delete root.setValue;
|
|
807
|
+
delete root.deselect;
|
|
808
|
+
delete root.toggle;
|
|
809
|
+
delete root.selectAll;
|
|
810
|
+
delete root.selectNone;
|
|
325
811
|
};
|
|
326
812
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
813
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
814
|
+
state.input.setAttribute('aria-expanded', 'false');
|
|
815
|
+
|
|
816
|
+
Object.defineProperty(root, 'value', {
|
|
817
|
+
configurable: true,
|
|
818
|
+
get: () => getCanonicalValue(state),
|
|
819
|
+
set: (value) => root.setValue(value),
|
|
334
820
|
});
|
|
335
821
|
|
|
336
|
-
|
|
337
|
-
|
|
822
|
+
Object.defineProperty(root, 'selected', {
|
|
823
|
+
configurable: true,
|
|
824
|
+
get: () => getSelectedDetail(state),
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
root.dataset.comboboxInitialized = 'true';
|
|
828
|
+
root.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
if (window.basecoat) {
|
|
832
|
+
window.basecoat.register('combobox', {
|
|
833
|
+
selector: '.combobox:not([data-combobox-initialized])',
|
|
834
|
+
init: initCombobox,
|
|
835
|
+
refresh: refreshCombobox,
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
})();
|
|
839
|
+
|
|
840
|
+
(() => {
|
|
841
|
+
const states = new WeakMap();
|
|
842
|
+
|
|
843
|
+
const isDisabled = (item) =>
|
|
844
|
+
item.hasAttribute('disabled') || item.getAttribute('aria-disabled') === 'true';
|
|
845
|
+
|
|
846
|
+
const getElements = (root) => {
|
|
847
|
+
const trigger = root.querySelector(':scope > button');
|
|
848
|
+
const popover = root.querySelector(':scope > [data-popover]');
|
|
849
|
+
const menu = popover ? popover.querySelector('[role="menu"]') : null;
|
|
850
|
+
return { trigger, popover, menu };
|
|
851
|
+
};
|
|
852
|
+
|
|
853
|
+
const getItems = (menu) => Array.from(menu.querySelectorAll('[role^="menuitem"]')).filter(item => !isDisabled(item));
|
|
854
|
+
|
|
855
|
+
const setActiveItem = (state, index) => {
|
|
856
|
+
if (state.activeIndex > -1 && state.items[state.activeIndex]) {
|
|
857
|
+
state.items[state.activeIndex].classList.remove('active');
|
|
858
|
+
}
|
|
859
|
+
state.activeIndex = index;
|
|
860
|
+
if (state.activeIndex > -1 && state.items[state.activeIndex]) {
|
|
861
|
+
const activeItem = state.items[state.activeIndex];
|
|
862
|
+
activeItem.classList.add('active');
|
|
863
|
+
if (activeItem.id) state.trigger.setAttribute('aria-activedescendant', activeItem.id);
|
|
864
|
+
} else {
|
|
865
|
+
state.trigger.removeAttribute('aria-activedescendant');
|
|
866
|
+
}
|
|
867
|
+
};
|
|
868
|
+
|
|
869
|
+
const refreshDropdownMenu = (root) => {
|
|
870
|
+
const state = states.get(root);
|
|
871
|
+
if (!state) return;
|
|
872
|
+
|
|
873
|
+
const elements = getElements(root);
|
|
874
|
+
if (!elements.trigger || !elements.popover || !elements.menu) {
|
|
875
|
+
const missing = [];
|
|
876
|
+
if (!elements.trigger) missing.push('trigger');
|
|
877
|
+
if (!elements.popover) missing.push('popover');
|
|
878
|
+
if (!elements.menu) missing.push('menu');
|
|
879
|
+
console.error(`Dropdown menu refresh failed. Missing element(s): ${missing.join(', ')}`, root);
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
Object.assign(state, elements);
|
|
884
|
+
state.items = getItems(state.menu);
|
|
885
|
+
if (state.activeIndex > -1 && !state.items[state.activeIndex]) setActiveItem(state, -1);
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
const closePopover = (state, focusOnTrigger = true) => {
|
|
889
|
+
if (state.trigger.getAttribute('aria-expanded') === 'false') return;
|
|
890
|
+
state.trigger.setAttribute('aria-expanded', 'false');
|
|
891
|
+
state.trigger.removeAttribute('aria-activedescendant');
|
|
892
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
893
|
+
if (focusOnTrigger) state.trigger.focus();
|
|
894
|
+
setActiveItem(state, -1);
|
|
895
|
+
};
|
|
896
|
+
|
|
897
|
+
const openPopover = (root, state, initialSelection = false) => {
|
|
898
|
+
document.dispatchEvent(new CustomEvent('basecoat:popover', { detail: { source: root } }));
|
|
899
|
+
root.refresh();
|
|
900
|
+
state.trigger.setAttribute('aria-expanded', 'true');
|
|
901
|
+
state.popover.setAttribute('aria-hidden', 'false');
|
|
902
|
+
|
|
903
|
+
if (state.items.length > 0 && initialSelection) {
|
|
904
|
+
setActiveItem(state, initialSelection === 'last' ? state.items.length - 1 : 0);
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
const initDropdownMenu = (root) => {
|
|
909
|
+
if (root.dataset.dropdownMenuInitialized) return;
|
|
910
|
+
|
|
911
|
+
const state = { activeIndex: -1, items: [] };
|
|
912
|
+
states.set(root, state);
|
|
913
|
+
root.refresh = () => refreshDropdownMenu(root);
|
|
914
|
+
|
|
915
|
+
refreshDropdownMenu(root);
|
|
916
|
+
if (!state.trigger || !state.popover || !state.menu) {
|
|
917
|
+
states.delete(root);
|
|
918
|
+
delete root.refresh;
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
root.open = (initialSelection = false) => openPopover(root, state, initialSelection);
|
|
923
|
+
root.close = (focusOnTrigger = true) => closePopover(state, focusOnTrigger);
|
|
924
|
+
root.toggle = () => state.trigger.getAttribute('aria-expanded') === 'true' ? root.close() : root.open(false);
|
|
925
|
+
|
|
926
|
+
const handleTriggerClick = root.toggle;
|
|
927
|
+
|
|
928
|
+
const handleKeydown = (event) => {
|
|
929
|
+
const isExpanded = state.trigger.getAttribute('aria-expanded') === 'true';
|
|
338
930
|
|
|
339
931
|
if (event.key === 'Escape') {
|
|
340
|
-
if (isExpanded)
|
|
932
|
+
if (isExpanded) root.close();
|
|
341
933
|
return;
|
|
342
934
|
}
|
|
343
|
-
|
|
935
|
+
|
|
344
936
|
if (!isExpanded) {
|
|
345
937
|
if (['Enter', ' '].includes(event.key)) {
|
|
346
938
|
event.preventDefault();
|
|
347
|
-
|
|
939
|
+
root.open(false);
|
|
348
940
|
} else if (event.key === 'ArrowDown') {
|
|
349
941
|
event.preventDefault();
|
|
350
|
-
|
|
942
|
+
root.open('first');
|
|
351
943
|
} else if (event.key === 'ArrowUp') {
|
|
352
944
|
event.preventDefault();
|
|
353
|
-
|
|
945
|
+
root.open('last');
|
|
354
946
|
}
|
|
355
947
|
return;
|
|
356
948
|
}
|
|
357
949
|
|
|
358
|
-
if (
|
|
359
|
-
|
|
360
|
-
let nextIndex = activeIndex;
|
|
950
|
+
if (state.items.length === 0) return;
|
|
361
951
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
menuItems[activeIndex]?.click();
|
|
383
|
-
closePopover();
|
|
384
|
-
return;
|
|
952
|
+
let nextIndex = state.activeIndex;
|
|
953
|
+
if (event.key === 'ArrowDown') {
|
|
954
|
+
event.preventDefault();
|
|
955
|
+
nextIndex = state.activeIndex === -1 ? 0 : Math.min(state.activeIndex + 1, state.items.length - 1);
|
|
956
|
+
} else if (event.key === 'ArrowUp') {
|
|
957
|
+
event.preventDefault();
|
|
958
|
+
nextIndex = state.activeIndex === -1 ? state.items.length - 1 : Math.max(state.activeIndex - 1, 0);
|
|
959
|
+
} else if (event.key === 'Home') {
|
|
960
|
+
event.preventDefault();
|
|
961
|
+
nextIndex = 0;
|
|
962
|
+
} else if (event.key === 'End') {
|
|
963
|
+
event.preventDefault();
|
|
964
|
+
nextIndex = state.items.length - 1;
|
|
965
|
+
} else if (['Enter', ' '].includes(event.key)) {
|
|
966
|
+
event.preventDefault();
|
|
967
|
+
state.items[state.activeIndex]?.click();
|
|
968
|
+
root.close();
|
|
969
|
+
return;
|
|
970
|
+
} else {
|
|
971
|
+
return;
|
|
385
972
|
}
|
|
386
973
|
|
|
387
|
-
if (nextIndex !== activeIndex)
|
|
388
|
-
|
|
389
|
-
}
|
|
390
|
-
});
|
|
974
|
+
if (nextIndex !== state.activeIndex) setActiveItem(state, nextIndex);
|
|
975
|
+
};
|
|
391
976
|
|
|
392
|
-
|
|
977
|
+
const handleMenuMousemove = (event) => {
|
|
393
978
|
const menuItem = event.target.closest('[role^="menuitem"]');
|
|
394
|
-
if (menuItem &&
|
|
395
|
-
const index =
|
|
396
|
-
if (index !== activeIndex)
|
|
397
|
-
setActiveItem(index);
|
|
398
|
-
}
|
|
979
|
+
if (menuItem && !isDisabled(menuItem) && state.items.includes(menuItem)) {
|
|
980
|
+
const index = state.items.indexOf(menuItem);
|
|
981
|
+
if (index !== state.activeIndex) setActiveItem(state, index);
|
|
399
982
|
}
|
|
400
|
-
}
|
|
983
|
+
};
|
|
401
984
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
985
|
+
const handleMenuMouseleave = () => setActiveItem(state, -1);
|
|
986
|
+
const handleMenuClick = (event) => {
|
|
987
|
+
const menuItem = event.target.closest('[role^="menuitem"]');
|
|
988
|
+
if (menuItem && !isDisabled(menuItem)) root.close();
|
|
989
|
+
};
|
|
405
990
|
|
|
406
|
-
|
|
407
|
-
if (event.target.
|
|
408
|
-
|
|
409
|
-
}
|
|
410
|
-
});
|
|
991
|
+
const handleDocumentClick = (event) => {
|
|
992
|
+
if (!root.contains(event.target)) root.close(false);
|
|
993
|
+
};
|
|
411
994
|
|
|
412
|
-
|
|
413
|
-
if (
|
|
414
|
-
|
|
415
|
-
}
|
|
416
|
-
});
|
|
995
|
+
const handleDocumentPopover = (event) => {
|
|
996
|
+
if (event.detail.source !== root) root.close(false);
|
|
997
|
+
};
|
|
417
998
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
999
|
+
state.trigger.addEventListener('click', handleTriggerClick);
|
|
1000
|
+
root.addEventListener('keydown', handleKeydown);
|
|
1001
|
+
state.menu.addEventListener('mousemove', handleMenuMousemove);
|
|
1002
|
+
state.menu.addEventListener('mouseleave', handleMenuMouseleave);
|
|
1003
|
+
state.menu.addEventListener('click', handleMenuClick);
|
|
1004
|
+
document.addEventListener('click', handleDocumentClick);
|
|
1005
|
+
document.addEventListener('basecoat:popover', handleDocumentPopover);
|
|
1006
|
+
|
|
1007
|
+
root._destroy = () => {
|
|
1008
|
+
state.trigger.removeEventListener('click', handleTriggerClick);
|
|
1009
|
+
root.removeEventListener('keydown', handleKeydown);
|
|
1010
|
+
state.menu.removeEventListener('mousemove', handleMenuMousemove);
|
|
1011
|
+
state.menu.removeEventListener('mouseleave', handleMenuMouseleave);
|
|
1012
|
+
state.menu.removeEventListener('click', handleMenuClick);
|
|
1013
|
+
document.removeEventListener('click', handleDocumentClick);
|
|
1014
|
+
document.removeEventListener('basecoat:popover', handleDocumentPopover);
|
|
1015
|
+
states.delete(root);
|
|
1016
|
+
delete root.refresh;
|
|
1017
|
+
delete root.open;
|
|
1018
|
+
delete root.close;
|
|
1019
|
+
delete root.toggle;
|
|
1020
|
+
};
|
|
423
1021
|
|
|
424
|
-
|
|
425
|
-
|
|
1022
|
+
state.trigger.setAttribute('aria-expanded', 'false');
|
|
1023
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
1024
|
+
root.dataset.dropdownMenuInitialized = 'true';
|
|
1025
|
+
root.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
426
1026
|
};
|
|
427
1027
|
|
|
428
1028
|
if (window.basecoat) {
|
|
429
|
-
window.basecoat.register('dropdown-menu',
|
|
1029
|
+
window.basecoat.register('dropdown-menu', {
|
|
1030
|
+
selector: '.dropdown-menu:not([data-dropdown-menu-initialized])',
|
|
1031
|
+
init: initDropdownMenu,
|
|
1032
|
+
refresh: refreshDropdownMenu,
|
|
1033
|
+
});
|
|
430
1034
|
}
|
|
431
1035
|
})();
|
|
1036
|
+
|
|
432
1037
|
(() => {
|
|
433
1038
|
const initPopover = (popoverComponent) => {
|
|
1039
|
+
if (popoverComponent.dataset.popoverInitialized) return;
|
|
1040
|
+
|
|
434
1041
|
const trigger = popoverComponent.querySelector(':scope > button');
|
|
435
1042
|
const content = popoverComponent.querySelector(':scope > [data-popover]');
|
|
436
1043
|
|
|
@@ -467,32 +1074,44 @@
|
|
|
467
1074
|
content.setAttribute('aria-hidden', 'false');
|
|
468
1075
|
};
|
|
469
1076
|
|
|
470
|
-
|
|
1077
|
+
const handleTriggerClick = () => {
|
|
471
1078
|
const isExpanded = trigger.getAttribute('aria-expanded') === 'true';
|
|
472
1079
|
if (isExpanded) {
|
|
473
1080
|
closePopover();
|
|
474
1081
|
} else {
|
|
475
1082
|
openPopover();
|
|
476
1083
|
}
|
|
477
|
-
}
|
|
1084
|
+
};
|
|
478
1085
|
|
|
479
|
-
|
|
1086
|
+
const handleKeydown = (event) => {
|
|
480
1087
|
if (event.key === 'Escape') {
|
|
481
1088
|
closePopover();
|
|
482
1089
|
}
|
|
483
|
-
}
|
|
1090
|
+
};
|
|
484
1091
|
|
|
485
|
-
|
|
1092
|
+
const handleDocumentClick = (event) => {
|
|
486
1093
|
if (!popoverComponent.contains(event.target)) {
|
|
487
1094
|
closePopover();
|
|
488
1095
|
}
|
|
489
|
-
}
|
|
1096
|
+
};
|
|
490
1097
|
|
|
491
|
-
|
|
1098
|
+
const handleDocumentPopover = (event) => {
|
|
492
1099
|
if (event.detail.source !== popoverComponent) {
|
|
493
1100
|
closePopover(false);
|
|
494
1101
|
}
|
|
495
|
-
}
|
|
1102
|
+
};
|
|
1103
|
+
|
|
1104
|
+
trigger.addEventListener('click', handleTriggerClick);
|
|
1105
|
+
popoverComponent.addEventListener('keydown', handleKeydown);
|
|
1106
|
+
document.addEventListener('click', handleDocumentClick);
|
|
1107
|
+
document.addEventListener('basecoat:popover', handleDocumentPopover);
|
|
1108
|
+
|
|
1109
|
+
popoverComponent._destroy = () => {
|
|
1110
|
+
trigger.removeEventListener('click', handleTriggerClick);
|
|
1111
|
+
popoverComponent.removeEventListener('keydown', handleKeydown);
|
|
1112
|
+
document.removeEventListener('click', handleDocumentClick);
|
|
1113
|
+
document.removeEventListener('basecoat:popover', handleDocumentPopover);
|
|
1114
|
+
};
|
|
496
1115
|
|
|
497
1116
|
popoverComponent.dataset.popoverInitialized = true;
|
|
498
1117
|
popoverComponent.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
@@ -504,489 +1123,474 @@
|
|
|
504
1123
|
})();
|
|
505
1124
|
|
|
506
1125
|
(() => {
|
|
507
|
-
|
|
508
|
-
const
|
|
509
|
-
const
|
|
510
|
-
const
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
|
|
1126
|
+
function updateRange(element) {
|
|
1127
|
+
const min = parseFloat(element.min || '0');
|
|
1128
|
+
const max = parseFloat(element.max || '100');
|
|
1129
|
+
const value = parseFloat(element.value || '0');
|
|
1130
|
+
const percent = max === min ? 0 : ((value - min) / (max - min)) * 100;
|
|
1131
|
+
element.style.setProperty('--slider-value', `${percent}%`);
|
|
1132
|
+
}
|
|
514
1133
|
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
if (!trigger) missing.push('trigger');
|
|
518
|
-
if (!popover) missing.push('popover');
|
|
519
|
-
if (!listbox) missing.push('listbox');
|
|
520
|
-
if (!input) missing.push('input');
|
|
521
|
-
console.error(`Select component initialisation failed. Missing element(s): ${missing.join(', ')}`, selectComponent);
|
|
522
|
-
return;
|
|
523
|
-
}
|
|
1134
|
+
function initRange(element) {
|
|
1135
|
+
if (element.dataset.rangeInitialized) return;
|
|
524
1136
|
|
|
525
|
-
|
|
526
|
-
const
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
const getValue = (opt) => opt.dataset.value ?? opt.textContent.trim();
|
|
535
|
-
|
|
536
|
-
const setActiveOption = (index) => {
|
|
537
|
-
if (activeIndex > -1 && options[activeIndex]) {
|
|
538
|
-
options[activeIndex].classList.remove('active');
|
|
539
|
-
}
|
|
1137
|
+
updateRange(element);
|
|
1138
|
+
const handleInput = () => updateRange(element);
|
|
1139
|
+
element.addEventListener('input', handleInput);
|
|
1140
|
+
element._destroy = () => {
|
|
1141
|
+
element.removeEventListener('input', handleInput);
|
|
1142
|
+
};
|
|
1143
|
+
element.dataset.rangeInitialized = 'true';
|
|
1144
|
+
}
|
|
540
1145
|
|
|
541
|
-
|
|
1146
|
+
if (window.basecoat) {
|
|
1147
|
+
window.basecoat.register('range', 'input[type="range"]:not([data-range-initialized])', initRange);
|
|
1148
|
+
}
|
|
1149
|
+
})();
|
|
542
1150
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
activeOption.classList.add('active');
|
|
546
|
-
if (activeOption.id) {
|
|
547
|
-
trigger.setAttribute('aria-activedescendant', activeOption.id);
|
|
548
|
-
} else {
|
|
549
|
-
trigger.removeAttribute('aria-activedescendant');
|
|
550
|
-
}
|
|
551
|
-
} else {
|
|
552
|
-
trigger.removeAttribute('aria-activedescendant');
|
|
553
|
-
}
|
|
554
|
-
};
|
|
1151
|
+
(() => {
|
|
1152
|
+
const states = new WeakMap();
|
|
555
1153
|
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
1154
|
+
const getElements = (root) => {
|
|
1155
|
+
const trigger = root.querySelector(':scope > button');
|
|
1156
|
+
const selectedLabel = trigger?.querySelector(':scope > span') || null;
|
|
1157
|
+
const popover = root.querySelector(':scope > [data-popover]');
|
|
1158
|
+
const listbox = popover ? popover.querySelector('[role="listbox"]') : null;
|
|
1159
|
+
const input = root.querySelector(':scope > input[type="hidden"]');
|
|
1160
|
+
return { trigger, selectedLabel, popover, listbox, input };
|
|
1161
|
+
};
|
|
560
1162
|
|
|
561
|
-
|
|
562
|
-
|
|
1163
|
+
const getValue = (option) => option.dataset.value ?? option.textContent.trim();
|
|
1164
|
+
const getLabel = (option) => option.dataset.label || option.textContent.trim();
|
|
1165
|
+
const getFormat = (root) => root.dataset.format === 'object' ? 'object' : 'value';
|
|
1166
|
+
const isDisabled = (option) => option.getAttribute('aria-disabled') === 'true';
|
|
1167
|
+
const toSelected = (option) => ({ value: getValue(option), label: getLabel(option) });
|
|
563
1168
|
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
1169
|
+
const getOptions = (listbox) => {
|
|
1170
|
+
const allOptions = Array.from(listbox.querySelectorAll('[role="option"]'));
|
|
1171
|
+
return {
|
|
1172
|
+
allOptions,
|
|
1173
|
+
options: allOptions.filter(option => !isDisabled(option)),
|
|
1174
|
+
};
|
|
1175
|
+
};
|
|
568
1176
|
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
1177
|
+
const parseStoredValues = (storedValue, { isMultiple, format }) => {
|
|
1178
|
+
if (isMultiple) {
|
|
1179
|
+
try {
|
|
1180
|
+
const parsed = JSON.parse(storedValue || '[]');
|
|
1181
|
+
if (!Array.isArray(parsed)) return [];
|
|
1182
|
+
return parsed
|
|
1183
|
+
.map(item => format === 'object' && item && typeof item === 'object' ? item.value : item)
|
|
1184
|
+
.filter(value => value != null)
|
|
1185
|
+
.map(String);
|
|
1186
|
+
} catch (_) {
|
|
1187
|
+
return [];
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
578
1190
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
value = getValue(option);
|
|
586
|
-
input.value = value;
|
|
1191
|
+
if (format === 'object') {
|
|
1192
|
+
try {
|
|
1193
|
+
const parsed = JSON.parse(storedValue || 'null');
|
|
1194
|
+
return parsed && typeof parsed === 'object' && parsed.value != null ? String(parsed.value) : '';
|
|
1195
|
+
} catch (_) {
|
|
1196
|
+
return '';
|
|
587
1197
|
}
|
|
1198
|
+
}
|
|
588
1199
|
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
if (isSelected) {
|
|
592
|
-
opt.setAttribute('aria-selected', 'true');
|
|
593
|
-
} else {
|
|
594
|
-
opt.removeAttribute('aria-selected');
|
|
595
|
-
}
|
|
596
|
-
});
|
|
1200
|
+
return storedValue || '';
|
|
1201
|
+
};
|
|
597
1202
|
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
}));
|
|
603
|
-
}
|
|
604
|
-
};
|
|
1203
|
+
const serializeSelection = (state, selected) => {
|
|
1204
|
+
if (state.format === 'object') {
|
|
1205
|
+
return JSON.stringify(state.isMultiple ? selected : (selected[0] || null));
|
|
1206
|
+
}
|
|
605
1207
|
|
|
606
|
-
const
|
|
607
|
-
|
|
1208
|
+
const value = selected.map(item => item.value);
|
|
1209
|
+
return state.isMultiple ? JSON.stringify(value) : (value[0] || '');
|
|
1210
|
+
};
|
|
608
1211
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
visibleOptions = [...options];
|
|
613
|
-
allOptions.forEach(opt => opt.setAttribute('aria-hidden', 'false'));
|
|
614
|
-
};
|
|
1212
|
+
const scrollOptionIntoListbox = (state, option) => {
|
|
1213
|
+
const optionRect = option.getBoundingClientRect();
|
|
1214
|
+
const listboxRect = state.listbox.getBoundingClientRect();
|
|
615
1215
|
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
1216
|
+
if (optionRect.top < listboxRect.top) {
|
|
1217
|
+
state.listbox.scrollTop -= listboxRect.top - optionRect.top;
|
|
1218
|
+
} else if (optionRect.bottom > listboxRect.bottom) {
|
|
1219
|
+
state.listbox.scrollTop += optionRect.bottom - listboxRect.bottom;
|
|
1220
|
+
}
|
|
1221
|
+
};
|
|
622
1222
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
};
|
|
1223
|
+
const setActiveOption = (state, index) => {
|
|
1224
|
+
if (state.activeIndex > -1 && state.options[state.activeIndex]) {
|
|
1225
|
+
state.options[state.activeIndex].classList.remove('active');
|
|
1226
|
+
}
|
|
628
1227
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
1228
|
+
state.activeIndex = index;
|
|
1229
|
+
|
|
1230
|
+
if (state.activeIndex > -1) {
|
|
1231
|
+
const activeOption = state.options[state.activeIndex];
|
|
1232
|
+
activeOption.classList.add('active');
|
|
1233
|
+
if (activeOption.id) {
|
|
1234
|
+
state.trigger.setAttribute('aria-activedescendant', activeOption.id);
|
|
632
1235
|
} else {
|
|
633
|
-
|
|
1236
|
+
state.trigger.removeAttribute('aria-activedescendant');
|
|
634
1237
|
}
|
|
635
|
-
|
|
636
|
-
|
|
1238
|
+
} else {
|
|
1239
|
+
state.trigger.removeAttribute('aria-activedescendant');
|
|
1240
|
+
}
|
|
1241
|
+
};
|
|
637
1242
|
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
1243
|
+
const updateValue = (root, optionOrOptions, triggerEvent = true) => {
|
|
1244
|
+
const state = states.get(root);
|
|
1245
|
+
let value;
|
|
1246
|
+
let selectedDetail;
|
|
1247
|
+
|
|
1248
|
+
if (state.isMultiple) {
|
|
1249
|
+
const selected = Array.isArray(optionOrOptions) ? optionOrOptions : [];
|
|
1250
|
+
state.selectedOptions.clear();
|
|
1251
|
+
selected.forEach(option => state.selectedOptions.add(option));
|
|
1252
|
+
|
|
1253
|
+
const selectedInOrder = state.options.filter(option => state.selectedOptions.has(option));
|
|
1254
|
+
selectedDetail = selectedInOrder.map(toSelected);
|
|
1255
|
+
if (selectedInOrder.length === 0) {
|
|
1256
|
+
state.selectedLabel.textContent = state.placeholder;
|
|
1257
|
+
state.selectedLabel.classList.add('text-muted-foreground');
|
|
644
1258
|
} else {
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
if (input.value !== value) {
|
|
648
|
-
updateValue(option);
|
|
649
|
-
}
|
|
650
|
-
closePopover();
|
|
1259
|
+
state.selectedLabel.textContent = selectedDetail.map(item => item.label).join(', ');
|
|
1260
|
+
state.selectedLabel.classList.remove('text-muted-foreground');
|
|
651
1261
|
}
|
|
652
|
-
};
|
|
653
1262
|
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
1263
|
+
value = selectedDetail.map(item => item.value);
|
|
1264
|
+
state.input.value = serializeSelection(state, selectedDetail);
|
|
1265
|
+
} else {
|
|
1266
|
+
const option = optionOrOptions;
|
|
657
1267
|
if (!option) return;
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
1268
|
+
state.selectedLabel.innerHTML = option.innerHTML;
|
|
1269
|
+
selectedDetail = toSelected(option);
|
|
1270
|
+
value = selectedDetail.value;
|
|
1271
|
+
state.input.value = serializeSelection(state, [selectedDetail]);
|
|
1272
|
+
}
|
|
661
1273
|
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
1274
|
+
state.options.forEach(option => {
|
|
1275
|
+
const isSelected = state.isMultiple ? state.selectedOptions.has(option) : option === optionOrOptions;
|
|
1276
|
+
if (isSelected) {
|
|
1277
|
+
option.setAttribute('aria-selected', 'true');
|
|
1278
|
+
} else {
|
|
1279
|
+
option.removeAttribute('aria-selected');
|
|
1280
|
+
}
|
|
1281
|
+
});
|
|
668
1282
|
|
|
669
|
-
if (
|
|
670
|
-
|
|
671
|
-
|
|
1283
|
+
if (triggerEvent) {
|
|
1284
|
+
root.dispatchEvent(new CustomEvent('change', {
|
|
1285
|
+
detail: { value, selected: selectedDetail },
|
|
1286
|
+
bubbles: true,
|
|
1287
|
+
}));
|
|
1288
|
+
}
|
|
1289
|
+
};
|
|
672
1290
|
|
|
673
|
-
|
|
1291
|
+
const closePopover = (state, focusOnTrigger = true) => {
|
|
1292
|
+
if (state.popover.getAttribute('aria-hidden') === 'true') return;
|
|
1293
|
+
if (focusOnTrigger) state.trigger.focus();
|
|
1294
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
1295
|
+
state.trigger.setAttribute('aria-expanded', 'false');
|
|
1296
|
+
setActiveOption(state, -1);
|
|
1297
|
+
};
|
|
674
1298
|
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
option.setAttribute('aria-hidden', 'false');
|
|
679
|
-
if (options.includes(option)) {
|
|
680
|
-
visibleOptions.push(option);
|
|
681
|
-
}
|
|
682
|
-
return;
|
|
683
|
-
}
|
|
1299
|
+
const refreshSelect = (root) => {
|
|
1300
|
+
const state = states.get(root);
|
|
1301
|
+
if (!state) return;
|
|
684
1302
|
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
});
|
|
697
|
-
};
|
|
1303
|
+
const elements = getElements(root);
|
|
1304
|
+
if (!elements.trigger || !elements.selectedLabel || !elements.popover || !elements.listbox || !elements.input) {
|
|
1305
|
+
const missing = [];
|
|
1306
|
+
if (!elements.trigger) missing.push('trigger');
|
|
1307
|
+
if (!elements.selectedLabel) missing.push('selected label');
|
|
1308
|
+
if (!elements.popover) missing.push('popover');
|
|
1309
|
+
if (!elements.listbox) missing.push('listbox');
|
|
1310
|
+
if (!elements.input) missing.push('input');
|
|
1311
|
+
console.error(`Select component refresh failed. Missing element(s): ${missing.join(', ')}`, root);
|
|
1312
|
+
return;
|
|
1313
|
+
}
|
|
698
1314
|
|
|
699
|
-
|
|
1315
|
+
const previousValue = elements.input.value;
|
|
1316
|
+
Object.assign(state, elements, getOptions(elements.listbox));
|
|
1317
|
+
state.visibleOptions = [...state.options];
|
|
1318
|
+
state.isMultiple = state.listbox.getAttribute('aria-multiselectable') === 'true';
|
|
1319
|
+
state.format = getFormat(root);
|
|
1320
|
+
state.placeholder = state.isMultiple ? (root.dataset.placeholder || '') : null;
|
|
1321
|
+
state.closeOnSelect = root.dataset.closeOnSelect === 'true';
|
|
1322
|
+
|
|
1323
|
+
if (state.isMultiple) {
|
|
1324
|
+
if (!state.selectedOptions) state.selectedOptions = new Set();
|
|
1325
|
+
const values = parseStoredValues(previousValue, state);
|
|
1326
|
+
const selected = values.length
|
|
1327
|
+
? values.map(value => state.options.find(option => getValue(option) === value)).filter(Boolean)
|
|
1328
|
+
: state.options.filter(option => option.getAttribute('aria-selected') === 'true');
|
|
1329
|
+
updateValue(root, selected, false);
|
|
1330
|
+
} else {
|
|
1331
|
+
const value = parseStoredValues(previousValue, state);
|
|
1332
|
+
const selected = state.options.find(option => getValue(option) === value)
|
|
1333
|
+
|| state.options.find(option => option.getAttribute('aria-selected') === 'true');
|
|
1334
|
+
state.options.forEach(option => option.removeAttribute('aria-selected'));
|
|
1335
|
+
if (selected) updateValue(root, selected, false);
|
|
700
1336
|
}
|
|
701
1337
|
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
try {
|
|
706
|
-
const parsed = JSON.parse(input.value || '[]');
|
|
707
|
-
const validValues = new Set(options.map(getValue));
|
|
708
|
-
const initialValues = Array.isArray(parsed) ? parsed.filter(v => validValues.has(v)) : [];
|
|
709
|
-
|
|
710
|
-
const initialOptions = [];
|
|
711
|
-
if (initialValues.length > 0) {
|
|
712
|
-
// Match values to options in order, allowing duplicates
|
|
713
|
-
initialValues.forEach(val => {
|
|
714
|
-
const opt = options.find(o => getValue(o) === val && !initialOptions.includes(o));
|
|
715
|
-
if (opt) initialOptions.push(opt);
|
|
716
|
-
});
|
|
717
|
-
} else {
|
|
718
|
-
initialOptions.push(...ariaSelected);
|
|
719
|
-
}
|
|
1338
|
+
const selectedOption = state.listbox.querySelector('[role="option"][aria-selected="true"]');
|
|
1339
|
+
setActiveOption(state, selectedOption ? state.options.indexOf(selectedOption) : -1);
|
|
1340
|
+
};
|
|
720
1341
|
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
1342
|
+
const toggleMultipleValue = (root, option) => {
|
|
1343
|
+
const state = states.get(root);
|
|
1344
|
+
if (state.selectedOptions.has(option)) {
|
|
1345
|
+
state.selectedOptions.delete(option);
|
|
725
1346
|
} else {
|
|
726
|
-
|
|
727
|
-
if (initialOption) updateValue(initialOption, false);
|
|
1347
|
+
state.selectedOptions.add(option);
|
|
728
1348
|
}
|
|
1349
|
+
updateValue(root, state.options.filter(opt => state.selectedOptions.has(opt)));
|
|
1350
|
+
};
|
|
729
1351
|
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
1352
|
+
const selectValue = (root, value) => {
|
|
1353
|
+
const state = states.get(root);
|
|
1354
|
+
if (state.isMultiple) {
|
|
1355
|
+
const option = state.options.find(opt => getValue(opt) === value && !state.selectedOptions.has(opt));
|
|
1356
|
+
if (!option) return;
|
|
1357
|
+
state.selectedOptions.add(option);
|
|
1358
|
+
updateValue(root, state.options.filter(opt => state.selectedOptions.has(opt)));
|
|
1359
|
+
} else {
|
|
1360
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
1361
|
+
if (!option) return;
|
|
1362
|
+
if (root.value !== value) updateValue(root, option);
|
|
1363
|
+
closePopover(state);
|
|
1364
|
+
}
|
|
1365
|
+
};
|
|
736
1366
|
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
1367
|
+
const deselectValue = (root, value) => {
|
|
1368
|
+
const state = states.get(root);
|
|
1369
|
+
if (!state.isMultiple) return;
|
|
1370
|
+
const option = state.options.find(opt => getValue(opt) === value && state.selectedOptions.has(opt));
|
|
1371
|
+
if (!option) return;
|
|
1372
|
+
state.selectedOptions.delete(option);
|
|
1373
|
+
updateValue(root, state.options.filter(opt => state.selectedOptions.has(opt)));
|
|
1374
|
+
};
|
|
744
1375
|
|
|
745
|
-
|
|
1376
|
+
const handleKeyNavigation = (event, root) => {
|
|
1377
|
+
const state = states.get(root);
|
|
1378
|
+
const isPopoverOpen = state.popover.getAttribute('aria-hidden') === 'false';
|
|
746
1379
|
|
|
747
|
-
|
|
748
|
-
closePopover();
|
|
749
|
-
return;
|
|
750
|
-
}
|
|
1380
|
+
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End', 'Escape'].includes(event.key)) return;
|
|
751
1381
|
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
toggleMultipleValue(option);
|
|
757
|
-
if (closeOnSelect) {
|
|
758
|
-
closePopover();
|
|
759
|
-
}
|
|
760
|
-
} else {
|
|
761
|
-
if (input.value !== getValue(option)) {
|
|
762
|
-
updateValue(option);
|
|
763
|
-
}
|
|
764
|
-
closePopover();
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
return;
|
|
1382
|
+
if (!isPopoverOpen) {
|
|
1383
|
+
if (event.key !== 'Enter' && event.key !== 'Escape') {
|
|
1384
|
+
event.preventDefault();
|
|
1385
|
+
root.open();
|
|
768
1386
|
}
|
|
1387
|
+
return;
|
|
1388
|
+
}
|
|
769
1389
|
|
|
770
|
-
|
|
1390
|
+
event.preventDefault();
|
|
771
1391
|
|
|
772
|
-
|
|
773
|
-
|
|
1392
|
+
if (event.key === 'Escape') {
|
|
1393
|
+
root.close();
|
|
1394
|
+
return;
|
|
1395
|
+
}
|
|
774
1396
|
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
if (
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
nextVisibleIndex = 0;
|
|
786
|
-
}
|
|
787
|
-
break;
|
|
788
|
-
case 'Home':
|
|
789
|
-
nextVisibleIndex = 0;
|
|
790
|
-
break;
|
|
791
|
-
case 'End':
|
|
792
|
-
nextVisibleIndex = visibleOptions.length - 1;
|
|
793
|
-
break;
|
|
1397
|
+
if (event.key === 'Enter') {
|
|
1398
|
+
if (state.activeIndex > -1) {
|
|
1399
|
+
const option = state.options[state.activeIndex];
|
|
1400
|
+
if (state.isMultiple) {
|
|
1401
|
+
toggleMultipleValue(root, option);
|
|
1402
|
+
if (state.closeOnSelect) root.close();
|
|
1403
|
+
} else {
|
|
1404
|
+
if (root.value !== getValue(option)) updateValue(root, option);
|
|
1405
|
+
root.close();
|
|
1406
|
+
}
|
|
794
1407
|
}
|
|
1408
|
+
return;
|
|
1409
|
+
}
|
|
795
1410
|
|
|
796
|
-
|
|
797
|
-
const newActiveOption = visibleOptions[nextVisibleIndex];
|
|
798
|
-
setActiveOption(options.indexOf(newActiveOption));
|
|
799
|
-
newActiveOption.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
|
800
|
-
}
|
|
801
|
-
};
|
|
1411
|
+
if (state.visibleOptions.length === 0) return;
|
|
802
1412
|
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
if (option && visibleOptions.includes(option)) {
|
|
806
|
-
const index = options.indexOf(option);
|
|
807
|
-
if (index !== activeIndex) {
|
|
808
|
-
setActiveOption(index);
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
});
|
|
1413
|
+
const currentVisibleIndex = state.activeIndex > -1 ? state.visibleOptions.indexOf(state.options[state.activeIndex]) : -1;
|
|
1414
|
+
let nextVisibleIndex = currentVisibleIndex;
|
|
812
1415
|
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
} else {
|
|
818
|
-
setActiveOption(-1);
|
|
819
|
-
}
|
|
820
|
-
});
|
|
1416
|
+
if (event.key === 'ArrowDown' && currentVisibleIndex < state.visibleOptions.length - 1) nextVisibleIndex = currentVisibleIndex + 1;
|
|
1417
|
+
if (event.key === 'ArrowUp') nextVisibleIndex = currentVisibleIndex > 0 ? currentVisibleIndex - 1 : 0;
|
|
1418
|
+
if (event.key === 'Home') nextVisibleIndex = 0;
|
|
1419
|
+
if (event.key === 'End') nextVisibleIndex = state.visibleOptions.length - 1;
|
|
821
1420
|
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
1421
|
+
if (nextVisibleIndex !== currentVisibleIndex) {
|
|
1422
|
+
const newActiveOption = state.visibleOptions[nextVisibleIndex];
|
|
1423
|
+
setActiveOption(state, state.options.indexOf(newActiveOption));
|
|
1424
|
+
scrollOptionIntoListbox(state, newActiveOption);
|
|
825
1425
|
}
|
|
1426
|
+
};
|
|
826
1427
|
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
detail: { source: selectComponent }
|
|
830
|
-
}));
|
|
1428
|
+
const initSelect = (root) => {
|
|
1429
|
+
if (root.dataset.selectInitialized) return;
|
|
831
1430
|
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
filter.focus();
|
|
836
|
-
}, { once: true });
|
|
837
|
-
} else {
|
|
838
|
-
filter.focus();
|
|
839
|
-
}
|
|
840
|
-
}
|
|
1431
|
+
const state = { activeIndex: -1, selectedOptions: null, options: [], allOptions: [], visibleOptions: [], format: 'value' };
|
|
1432
|
+
states.set(root, state);
|
|
1433
|
+
root.refresh = () => refreshSelect(root);
|
|
841
1434
|
|
|
842
|
-
|
|
843
|
-
|
|
1435
|
+
refreshSelect(root);
|
|
1436
|
+
if (!state.trigger || !state.selectedLabel || !state.popover || !state.listbox || !state.input) {
|
|
1437
|
+
states.delete(root);
|
|
1438
|
+
delete root.refresh;
|
|
1439
|
+
return;
|
|
1440
|
+
}
|
|
844
1441
|
|
|
845
|
-
|
|
1442
|
+
root.open = () => {
|
|
1443
|
+
document.dispatchEvent(new CustomEvent('basecoat:popover', { detail: { source: root } }));
|
|
1444
|
+
root.refresh();
|
|
1445
|
+
state.popover.setAttribute('aria-hidden', 'false');
|
|
1446
|
+
state.trigger.setAttribute('aria-expanded', 'true');
|
|
1447
|
+
|
|
1448
|
+
const selectedOption = state.listbox.querySelector('[role="option"][aria-selected="true"]');
|
|
846
1449
|
if (selectedOption) {
|
|
847
|
-
setActiveOption(options.indexOf(selectedOption));
|
|
848
|
-
|
|
1450
|
+
setActiveOption(state, state.options.indexOf(selectedOption));
|
|
1451
|
+
scrollOptionIntoListbox(state, selectedOption);
|
|
849
1452
|
}
|
|
850
1453
|
};
|
|
1454
|
+
root.close = (focusOnTrigger = true) => closePopover(state, focusOnTrigger);
|
|
1455
|
+
root.togglePopover = () => state.trigger.getAttribute('aria-expanded') === 'true' ? root.close() : root.open();
|
|
851
1456
|
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
1457
|
+
const handleTriggerKeydown = (event) => handleKeyNavigation(event, root);
|
|
1458
|
+
const handleTriggerClick = root.togglePopover;
|
|
1459
|
+
const handleListboxMousemove = (event) => {
|
|
1460
|
+
const option = event.target.closest('[role="option"]');
|
|
1461
|
+
if (option && state.visibleOptions.includes(option)) {
|
|
1462
|
+
const index = state.options.indexOf(option);
|
|
1463
|
+
if (index !== state.activeIndex) setActiveOption(state, index);
|
|
858
1464
|
}
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
|
|
1465
|
+
};
|
|
1466
|
+
const handleListboxMouseleave = () => {
|
|
1467
|
+
const selectedOption = state.listbox.querySelector('[role="option"][aria-selected="true"]');
|
|
1468
|
+
setActiveOption(state, selectedOption ? state.options.indexOf(selectedOption) : -1);
|
|
1469
|
+
};
|
|
1470
|
+
const handleListboxClick = (event) => {
|
|
862
1471
|
const clickedOption = event.target.closest('[role="option"]');
|
|
863
1472
|
if (!clickedOption) return;
|
|
864
1473
|
|
|
865
|
-
const option = options.find(opt => opt === clickedOption);
|
|
1474
|
+
const option = state.options.find(opt => opt === clickedOption);
|
|
866
1475
|
if (!option) return;
|
|
867
1476
|
|
|
868
|
-
if (isMultiple) {
|
|
869
|
-
toggleMultipleValue(option);
|
|
870
|
-
if (closeOnSelect) {
|
|
871
|
-
|
|
1477
|
+
if (state.isMultiple) {
|
|
1478
|
+
toggleMultipleValue(root, option);
|
|
1479
|
+
if (state.closeOnSelect) {
|
|
1480
|
+
root.close();
|
|
872
1481
|
} else {
|
|
873
|
-
setActiveOption(options.indexOf(option));
|
|
874
|
-
|
|
875
|
-
filter.focus();
|
|
876
|
-
} else {
|
|
877
|
-
trigger.focus();
|
|
878
|
-
}
|
|
1482
|
+
setActiveOption(state, state.options.indexOf(option));
|
|
1483
|
+
state.trigger.focus();
|
|
879
1484
|
}
|
|
880
1485
|
} else {
|
|
881
|
-
if (
|
|
882
|
-
|
|
883
|
-
}
|
|
884
|
-
closePopover();
|
|
885
|
-
}
|
|
886
|
-
});
|
|
887
|
-
|
|
888
|
-
document.addEventListener('click', (event) => {
|
|
889
|
-
if (!selectComponent.contains(event.target)) {
|
|
890
|
-
closePopover(false);
|
|
891
|
-
}
|
|
892
|
-
});
|
|
893
|
-
|
|
894
|
-
document.addEventListener('basecoat:popover', (event) => {
|
|
895
|
-
if (event.detail.source !== selectComponent) {
|
|
896
|
-
closePopover(false);
|
|
1486
|
+
if (root.value !== getValue(option)) updateValue(root, option);
|
|
1487
|
+
root.close();
|
|
897
1488
|
}
|
|
898
|
-
}
|
|
1489
|
+
};
|
|
1490
|
+
const handleDocumentClick = (event) => {
|
|
1491
|
+
if (!root.contains(event.target)) root.close(false);
|
|
1492
|
+
};
|
|
1493
|
+
const handleDocumentPopover = (event) => {
|
|
1494
|
+
if (event.detail.source !== root) root.close(false);
|
|
1495
|
+
};
|
|
899
1496
|
|
|
900
|
-
|
|
1497
|
+
state.trigger.addEventListener('keydown', handleTriggerKeydown);
|
|
1498
|
+
state.trigger.addEventListener('click', handleTriggerClick);
|
|
1499
|
+
state.listbox.addEventListener('mousemove', handleListboxMousemove);
|
|
1500
|
+
state.listbox.addEventListener('mouseleave', handleListboxMouseleave);
|
|
1501
|
+
state.listbox.addEventListener('click', handleListboxClick);
|
|
1502
|
+
document.addEventListener('click', handleDocumentClick);
|
|
1503
|
+
document.addEventListener('basecoat:popover', handleDocumentPopover);
|
|
1504
|
+
|
|
1505
|
+
root._destroy = () => {
|
|
1506
|
+
state.trigger.removeEventListener('keydown', handleTriggerKeydown);
|
|
1507
|
+
state.trigger.removeEventListener('click', handleTriggerClick);
|
|
1508
|
+
state.listbox.removeEventListener('mousemove', handleListboxMousemove);
|
|
1509
|
+
state.listbox.removeEventListener('mouseleave', handleListboxMouseleave);
|
|
1510
|
+
state.listbox.removeEventListener('click', handleListboxClick);
|
|
1511
|
+
document.removeEventListener('click', handleDocumentClick);
|
|
1512
|
+
document.removeEventListener('basecoat:popover', handleDocumentPopover);
|
|
1513
|
+
states.delete(root);
|
|
1514
|
+
delete root.refresh;
|
|
1515
|
+
delete root.open;
|
|
1516
|
+
delete root.close;
|
|
1517
|
+
delete root.togglePopover;
|
|
1518
|
+
delete root.select;
|
|
1519
|
+
delete root.selectByValue;
|
|
1520
|
+
delete root.deselect;
|
|
1521
|
+
delete root.toggle;
|
|
1522
|
+
delete root.selectAll;
|
|
1523
|
+
delete root.selectNone;
|
|
1524
|
+
};
|
|
901
1525
|
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
get: () =>
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
}
|
|
910
|
-
},
|
|
911
|
-
set: (val) => {
|
|
912
|
-
if (isMultiple) {
|
|
913
|
-
const values = Array.isArray(val) ? val : (val != null ? [val] : []);
|
|
914
|
-
const opts = [];
|
|
915
|
-
values.forEach(v => {
|
|
916
|
-
const opt = options.find(o => getValue(o) === v && !opts.includes(o));
|
|
917
|
-
if (opt) opts.push(opt);
|
|
918
|
-
});
|
|
919
|
-
updateValue(opts);
|
|
1526
|
+
Object.defineProperty(root, 'value', {
|
|
1527
|
+
configurable: true,
|
|
1528
|
+
get: () => state.isMultiple ? state.options.filter(option => state.selectedOptions.has(option)).map(getValue) : parseStoredValues(state.input.value, state),
|
|
1529
|
+
set: (value) => {
|
|
1530
|
+
if (state.isMultiple) {
|
|
1531
|
+
const values = Array.isArray(value) ? value : (value != null ? [value] : []);
|
|
1532
|
+
updateValue(root, values.map(v => state.options.find(option => getValue(option) === v)).filter(Boolean));
|
|
920
1533
|
} else {
|
|
921
|
-
const option = options.find(opt => getValue(opt) ===
|
|
1534
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
922
1535
|
if (option) {
|
|
923
|
-
updateValue(option);
|
|
924
|
-
|
|
1536
|
+
updateValue(root, option);
|
|
1537
|
+
root.close();
|
|
925
1538
|
}
|
|
926
1539
|
}
|
|
927
|
-
}
|
|
1540
|
+
},
|
|
928
1541
|
});
|
|
929
1542
|
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
1543
|
+
Object.defineProperty(root, 'selected', {
|
|
1544
|
+
configurable: true,
|
|
1545
|
+
get: () => {
|
|
1546
|
+
if (state.isMultiple) return state.options.filter(option => state.selectedOptions.has(option)).map(toSelected);
|
|
1547
|
+
const value = root.value;
|
|
1548
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
1549
|
+
return option ? toSelected(option) : null;
|
|
1550
|
+
},
|
|
1551
|
+
});
|
|
1552
|
+
|
|
1553
|
+
root.select = (value) => selectValue(root, value);
|
|
1554
|
+
root.selectByValue = root.select;
|
|
1555
|
+
if (state.isMultiple) {
|
|
1556
|
+
root.deselect = (value) => deselectValue(root, value);
|
|
1557
|
+
root.toggle = (value) => {
|
|
1558
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
1559
|
+
if (option) toggleMultipleValue(root, option);
|
|
1560
|
+
};
|
|
1561
|
+
root.selectAll = () => updateValue(root, state.options);
|
|
1562
|
+
root.selectNone = () => updateValue(root, []);
|
|
937
1563
|
}
|
|
938
|
-
|
|
939
|
-
|
|
1564
|
+
|
|
1565
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
1566
|
+
state.trigger.setAttribute('aria-expanded', 'false');
|
|
1567
|
+
root.dataset.selectInitialized = 'true';
|
|
1568
|
+
root.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
940
1569
|
};
|
|
941
1570
|
|
|
942
1571
|
if (window.basecoat) {
|
|
943
|
-
window.basecoat.register('select',
|
|
1572
|
+
window.basecoat.register('select', {
|
|
1573
|
+
selector: 'div.select:not([data-select-initialized])',
|
|
1574
|
+
init: initSelect,
|
|
1575
|
+
refresh: refreshSelect,
|
|
1576
|
+
});
|
|
944
1577
|
}
|
|
945
1578
|
})();
|
|
946
1579
|
|
|
947
1580
|
(() => {
|
|
948
|
-
// Monkey patching the history API to detect client-side navigation
|
|
949
|
-
if (!window.history.__basecoatPatched) {
|
|
950
|
-
const originalPushState = window.history.pushState;
|
|
951
|
-
window.history.pushState = function(...args) {
|
|
952
|
-
originalPushState.apply(this, args);
|
|
953
|
-
window.dispatchEvent(new Event('basecoat:locationchange'));
|
|
954
|
-
};
|
|
955
|
-
|
|
956
|
-
const originalReplaceState = window.history.replaceState;
|
|
957
|
-
window.history.replaceState = function(...args) {
|
|
958
|
-
originalReplaceState.apply(this, args);
|
|
959
|
-
window.dispatchEvent(new Event('basecoat:locationchange'));
|
|
960
|
-
};
|
|
961
|
-
|
|
962
|
-
window.history.__basecoatPatched = true;
|
|
963
|
-
}
|
|
964
|
-
|
|
965
1581
|
const initSidebar = (sidebarComponent) => {
|
|
1582
|
+
if (sidebarComponent.dataset.sidebarInitialized) return;
|
|
1583
|
+
|
|
966
1584
|
const initialOpen = sidebarComponent.dataset.initialOpen !== 'false';
|
|
967
1585
|
const initialMobileOpen = sidebarComponent.dataset.initialMobileOpen === 'true';
|
|
968
1586
|
const breakpoint = parseInt(sidebarComponent.dataset.breakpoint) || 768;
|
|
969
|
-
|
|
970
|
-
let open = breakpoint > 0
|
|
1587
|
+
|
|
1588
|
+
let open = breakpoint > 0
|
|
971
1589
|
? (window.innerWidth >= breakpoint ? initialOpen : initialMobileOpen)
|
|
972
1590
|
: initialOpen;
|
|
973
|
-
|
|
974
|
-
const updateCurrentPageLinks = () => {
|
|
975
|
-
const currentPath = window.location.pathname.replace(/\/$/, '');
|
|
976
|
-
sidebarComponent.querySelectorAll('a').forEach(link => {
|
|
977
|
-
if (link.hasAttribute('data-ignore-current')) return;
|
|
978
|
-
|
|
979
|
-
const linkPath = new URL(link.href).pathname.replace(/\/$/, '');
|
|
980
|
-
if (linkPath === currentPath) {
|
|
981
|
-
link.setAttribute('aria-current', 'page');
|
|
982
|
-
} else {
|
|
983
|
-
link.removeAttribute('aria-current');
|
|
984
|
-
}
|
|
985
|
-
});
|
|
986
|
-
};
|
|
987
|
-
|
|
1591
|
+
|
|
988
1592
|
const updateState = () => {
|
|
989
|
-
sidebarComponent.setAttribute('aria-hidden', !open);
|
|
1593
|
+
sidebarComponent.setAttribute('aria-hidden', String(!open));
|
|
990
1594
|
if (open) {
|
|
991
1595
|
sidebarComponent.removeAttribute('inert');
|
|
992
1596
|
} else {
|
|
@@ -995,52 +1599,42 @@
|
|
|
995
1599
|
};
|
|
996
1600
|
|
|
997
1601
|
const setState = (state) => {
|
|
998
|
-
open = state;
|
|
1602
|
+
open = Boolean(state);
|
|
999
1603
|
updateState();
|
|
1000
1604
|
};
|
|
1001
1605
|
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
switch (event.detail?.action) {
|
|
1008
|
-
case 'open':
|
|
1009
|
-
setState(true);
|
|
1010
|
-
break;
|
|
1011
|
-
case 'close':
|
|
1012
|
-
setState(false);
|
|
1013
|
-
break;
|
|
1014
|
-
default:
|
|
1015
|
-
setState(!open);
|
|
1016
|
-
break;
|
|
1017
|
-
}
|
|
1018
|
-
});
|
|
1019
|
-
|
|
1020
|
-
sidebarComponent.addEventListener('click', (event) => {
|
|
1606
|
+
sidebarComponent.open = () => setState(true);
|
|
1607
|
+
sidebarComponent.close = () => setState(false);
|
|
1608
|
+
sidebarComponent.toggle = () => setState(!open);
|
|
1609
|
+
|
|
1610
|
+
const handleClick = (event) => {
|
|
1021
1611
|
const target = event.target;
|
|
1022
1612
|
const nav = sidebarComponent.querySelector('nav');
|
|
1023
|
-
|
|
1024
1613
|
const isMobile = window.innerWidth < breakpoint;
|
|
1025
|
-
|
|
1026
|
-
if (isMobile &&
|
|
1614
|
+
|
|
1615
|
+
if (isMobile && target.closest('a, button') && !target.closest('[data-keep-mobile-sidebar-open]')) {
|
|
1027
1616
|
if (document.activeElement) document.activeElement.blur();
|
|
1028
|
-
|
|
1617
|
+
sidebarComponent.close();
|
|
1029
1618
|
return;
|
|
1030
1619
|
}
|
|
1031
|
-
|
|
1620
|
+
|
|
1032
1621
|
if (target === sidebarComponent || (nav && !nav.contains(target))) {
|
|
1033
1622
|
if (document.activeElement) document.activeElement.blur();
|
|
1034
|
-
|
|
1623
|
+
sidebarComponent.close();
|
|
1035
1624
|
}
|
|
1036
|
-
}
|
|
1625
|
+
};
|
|
1626
|
+
|
|
1627
|
+
sidebarComponent.addEventListener('click', handleClick);
|
|
1037
1628
|
|
|
1038
|
-
|
|
1039
|
-
|
|
1629
|
+
sidebarComponent._destroy = () => {
|
|
1630
|
+
sidebarComponent.removeEventListener('click', handleClick);
|
|
1631
|
+
delete sidebarComponent.open;
|
|
1632
|
+
delete sidebarComponent.close;
|
|
1633
|
+
delete sidebarComponent.toggle;
|
|
1634
|
+
};
|
|
1040
1635
|
|
|
1041
1636
|
updateState();
|
|
1042
|
-
|
|
1043
|
-
sidebarComponent.dataset.sidebarInitialized = true;
|
|
1637
|
+
sidebarComponent.dataset.sidebarInitialized = 'true';
|
|
1044
1638
|
sidebarComponent.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
1045
1639
|
};
|
|
1046
1640
|
|
|
@@ -1048,69 +1642,115 @@
|
|
|
1048
1642
|
window.basecoat.register('sidebar', '.sidebar:not([data-sidebar-initialized])', initSidebar);
|
|
1049
1643
|
}
|
|
1050
1644
|
})();
|
|
1645
|
+
|
|
1051
1646
|
(() => {
|
|
1052
|
-
const
|
|
1053
|
-
const tablist = tabsComponent.querySelector('[role="tablist"]');
|
|
1054
|
-
if (!tablist) return;
|
|
1647
|
+
const states = new WeakMap();
|
|
1055
1648
|
|
|
1056
|
-
|
|
1649
|
+
const isDisabled = (tab) => tab.disabled || tab.getAttribute('aria-disabled') === 'true';
|
|
1650
|
+
|
|
1651
|
+
const getElements = (root) => {
|
|
1652
|
+
const tablist = root.querySelector('[role="tablist"]');
|
|
1653
|
+
const tabs = tablist ? Array.from(tablist.querySelectorAll('[role="tab"]')) : [];
|
|
1057
1654
|
const panels = tabs.map(tab => document.getElementById(tab.getAttribute('aria-controls'))).filter(Boolean);
|
|
1655
|
+
return { tablist, tabs, panels };
|
|
1656
|
+
};
|
|
1657
|
+
|
|
1658
|
+
const refreshTabs = (root) => {
|
|
1659
|
+
const state = states.get(root);
|
|
1660
|
+
if (!state) return;
|
|
1661
|
+
|
|
1662
|
+
Object.assign(state, getElements(root));
|
|
1663
|
+
if (!state.tablist) return;
|
|
1058
1664
|
|
|
1059
|
-
const
|
|
1060
|
-
tabs.
|
|
1665
|
+
const selected = state.tabs.find(tab => tab.getAttribute('aria-selected') === 'true' && !isDisabled(tab))
|
|
1666
|
+
|| state.tabs.find(tab => !isDisabled(tab));
|
|
1667
|
+
if (selected) root.select(selected, false);
|
|
1668
|
+
};
|
|
1669
|
+
|
|
1670
|
+
const initTabs = (root) => {
|
|
1671
|
+
if (root.dataset.tabsInitialized) return;
|
|
1672
|
+
|
|
1673
|
+
const state = {};
|
|
1674
|
+
states.set(root, state);
|
|
1675
|
+
root.refresh = () => refreshTabs(root);
|
|
1676
|
+
|
|
1677
|
+
const selectTab = (tabToSelect, focus = false) => {
|
|
1678
|
+
if (!tabToSelect || isDisabled(tabToSelect)) return;
|
|
1679
|
+
|
|
1680
|
+
state.tabs.forEach((tab) => {
|
|
1061
1681
|
tab.setAttribute('aria-selected', 'false');
|
|
1062
1682
|
tab.setAttribute('tabindex', '-1');
|
|
1063
|
-
|
|
1683
|
+
const panel = document.getElementById(tab.getAttribute('aria-controls'));
|
|
1684
|
+
if (panel) panel.hidden = true;
|
|
1064
1685
|
});
|
|
1065
1686
|
|
|
1066
1687
|
tabToSelect.setAttribute('aria-selected', 'true');
|
|
1067
1688
|
tabToSelect.setAttribute('tabindex', '0');
|
|
1068
1689
|
const activePanel = document.getElementById(tabToSelect.getAttribute('aria-controls'));
|
|
1069
1690
|
if (activePanel) activePanel.hidden = false;
|
|
1691
|
+
if (focus) tabToSelect.focus();
|
|
1070
1692
|
};
|
|
1071
1693
|
|
|
1072
|
-
|
|
1694
|
+
root.select = selectTab;
|
|
1695
|
+
refreshTabs(root);
|
|
1696
|
+
if (!state.tablist) {
|
|
1697
|
+
states.delete(root);
|
|
1698
|
+
delete root.refresh;
|
|
1699
|
+
delete root.select;
|
|
1700
|
+
return;
|
|
1701
|
+
}
|
|
1702
|
+
|
|
1703
|
+
const handleClick = (event) => {
|
|
1073
1704
|
const clickedTab = event.target.closest('[role="tab"]');
|
|
1074
|
-
if (clickedTab)
|
|
1075
|
-
}
|
|
1705
|
+
if (clickedTab) root.select(clickedTab);
|
|
1706
|
+
};
|
|
1076
1707
|
|
|
1077
|
-
|
|
1708
|
+
const handleKeydown = (event) => {
|
|
1078
1709
|
const currentTab = event.target;
|
|
1079
|
-
if (!tabs.includes(currentTab)) return;
|
|
1710
|
+
if (!state.tabs.includes(currentTab)) return;
|
|
1711
|
+
|
|
1712
|
+
const enabledTabs = state.tabs.filter(tab => !isDisabled(tab));
|
|
1713
|
+
const currentIndex = enabledTabs.indexOf(currentTab);
|
|
1714
|
+
const orientation = state.tablist.getAttribute('aria-orientation') || 'horizontal';
|
|
1715
|
+
if (currentIndex === -1) return;
|
|
1080
1716
|
|
|
1081
1717
|
let nextTab;
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
nextTab = tabs[(currentIndex - 1 + tabs.length) % tabs.length];
|
|
1090
|
-
break;
|
|
1091
|
-
case 'Home':
|
|
1092
|
-
nextTab = tabs[0];
|
|
1093
|
-
break;
|
|
1094
|
-
case 'End':
|
|
1095
|
-
nextTab = tabs[tabs.length - 1];
|
|
1096
|
-
break;
|
|
1097
|
-
default:
|
|
1098
|
-
return;
|
|
1099
|
-
}
|
|
1718
|
+
if (event.key === 'ArrowRight' && orientation === 'horizontal') nextTab = enabledTabs[(currentIndex + 1) % enabledTabs.length];
|
|
1719
|
+
if (event.key === 'ArrowLeft' && orientation === 'horizontal') nextTab = enabledTabs[(currentIndex - 1 + enabledTabs.length) % enabledTabs.length];
|
|
1720
|
+
if (event.key === 'ArrowDown' && orientation === 'vertical') nextTab = enabledTabs[(currentIndex + 1) % enabledTabs.length];
|
|
1721
|
+
if (event.key === 'ArrowUp' && orientation === 'vertical') nextTab = enabledTabs[(currentIndex - 1 + enabledTabs.length) % enabledTabs.length];
|
|
1722
|
+
if (event.key === 'Home') nextTab = enabledTabs[0];
|
|
1723
|
+
if (event.key === 'End') nextTab = enabledTabs[enabledTabs.length - 1];
|
|
1724
|
+
if (!nextTab) return;
|
|
1100
1725
|
|
|
1101
1726
|
event.preventDefault();
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1727
|
+
root.select(nextTab, true);
|
|
1728
|
+
};
|
|
1729
|
+
|
|
1730
|
+
state.tablist.addEventListener('click', handleClick);
|
|
1731
|
+
state.tablist.addEventListener('keydown', handleKeydown);
|
|
1732
|
+
|
|
1733
|
+
root._destroy = () => {
|
|
1734
|
+
state.tablist.removeEventListener('click', handleClick);
|
|
1735
|
+
state.tablist.removeEventListener('keydown', handleKeydown);
|
|
1736
|
+
states.delete(root);
|
|
1737
|
+
delete root.refresh;
|
|
1738
|
+
delete root.select;
|
|
1739
|
+
};
|
|
1740
|
+
|
|
1741
|
+
root.dataset.tabsInitialized = 'true';
|
|
1742
|
+
root.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
1108
1743
|
};
|
|
1109
1744
|
|
|
1110
1745
|
if (window.basecoat) {
|
|
1111
|
-
window.basecoat.register('tabs',
|
|
1746
|
+
window.basecoat.register('tabs', {
|
|
1747
|
+
selector: '.tabs:not([data-tabs-initialized])',
|
|
1748
|
+
init: initTabs,
|
|
1749
|
+
refresh: refreshTabs,
|
|
1750
|
+
});
|
|
1112
1751
|
}
|
|
1113
1752
|
})();
|
|
1753
|
+
|
|
1114
1754
|
(() => {
|
|
1115
1755
|
let toaster;
|
|
1116
1756
|
const toasts = new WeakMap();
|
|
@@ -1126,17 +1766,38 @@
|
|
|
1126
1766
|
if (toasterElement.dataset.toasterInitialized) return;
|
|
1127
1767
|
toaster = toasterElement;
|
|
1128
1768
|
|
|
1129
|
-
|
|
1130
|
-
toaster.addEventListener('mouseleave', resumeAllTimeouts);
|
|
1131
|
-
toaster.addEventListener('click', (event) => {
|
|
1769
|
+
const handleClick = (event) => {
|
|
1132
1770
|
const actionLink = event.target.closest('.toast footer a');
|
|
1133
1771
|
const actionButton = event.target.closest('.toast footer button');
|
|
1134
1772
|
if (actionLink || actionButton) {
|
|
1135
1773
|
closeToast(event.target.closest('.toast'));
|
|
1136
1774
|
}
|
|
1137
|
-
}
|
|
1775
|
+
};
|
|
1776
|
+
|
|
1777
|
+
toaster.addEventListener('mouseenter', pauseAllTimeouts);
|
|
1778
|
+
toaster.addEventListener('mouseleave', resumeAllTimeouts);
|
|
1779
|
+
toaster.addEventListener('click', handleClick);
|
|
1780
|
+
|
|
1781
|
+
toasterElement.toast = (config = {}) => {
|
|
1782
|
+
const toastElement = createToast(config);
|
|
1783
|
+
toasterElement.appendChild(toastElement);
|
|
1784
|
+
initToast(toastElement);
|
|
1785
|
+
return toastElement;
|
|
1786
|
+
};
|
|
1787
|
+
toasterElement.closeAll = () => {
|
|
1788
|
+
toasterElement.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(closeToast);
|
|
1789
|
+
};
|
|
1138
1790
|
|
|
1139
1791
|
toaster.querySelectorAll('.toast:not([data-toast-initialized])').forEach(initToast);
|
|
1792
|
+
toaster._destroy = () => {
|
|
1793
|
+
toaster.removeEventListener('mouseenter', pauseAllTimeouts);
|
|
1794
|
+
toaster.removeEventListener('mouseleave', resumeAllTimeouts);
|
|
1795
|
+
toaster.removeEventListener('click', handleClick);
|
|
1796
|
+
toaster.querySelectorAll('.toast[data-toast-initialized]').forEach(toast => toast._destroy?.());
|
|
1797
|
+
delete toaster.toast;
|
|
1798
|
+
delete toaster.closeAll;
|
|
1799
|
+
if (toaster === toasterElement) toaster = null;
|
|
1800
|
+
};
|
|
1140
1801
|
toaster.dataset.toasterInitialized = 'true';
|
|
1141
1802
|
toaster.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
1142
1803
|
}
|
|
@@ -1148,13 +1809,13 @@
|
|
|
1148
1809
|
const timeoutDuration = duration !== -1
|
|
1149
1810
|
? duration || (element.dataset.category === 'error' ? 5000 : 3000)
|
|
1150
1811
|
: -1;
|
|
1151
|
-
|
|
1812
|
+
|
|
1152
1813
|
const state = {
|
|
1153
1814
|
remainingTime: timeoutDuration,
|
|
1154
1815
|
timeoutId: null,
|
|
1155
1816
|
startTime: null,
|
|
1156
1817
|
};
|
|
1157
|
-
|
|
1818
|
+
|
|
1158
1819
|
if (timeoutDuration !== -1) {
|
|
1159
1820
|
if (isPaused) {
|
|
1160
1821
|
state.timeoutId = null;
|
|
@@ -1164,18 +1825,24 @@
|
|
|
1164
1825
|
}
|
|
1165
1826
|
}
|
|
1166
1827
|
toasts.set(element, state);
|
|
1167
|
-
|
|
1828
|
+
|
|
1829
|
+
element.close = () => closeToast(element);
|
|
1830
|
+
element._destroy = () => {
|
|
1831
|
+
clearTimeout(state.timeoutId);
|
|
1832
|
+
toasts.delete(element);
|
|
1833
|
+
delete element.close;
|
|
1834
|
+
};
|
|
1168
1835
|
element.dataset.toastInitialized = 'true';
|
|
1169
1836
|
}
|
|
1170
1837
|
|
|
1171
1838
|
function pauseAllTimeouts() {
|
|
1172
|
-
if (isPaused) return;
|
|
1839
|
+
if (isPaused || !toaster) return;
|
|
1173
1840
|
|
|
1174
1841
|
isPaused = true;
|
|
1175
|
-
|
|
1842
|
+
|
|
1176
1843
|
toaster.querySelectorAll('.toast:not([aria-hidden="true"])').forEach(element => {
|
|
1177
1844
|
if (!toasts.has(element)) return;
|
|
1178
|
-
|
|
1845
|
+
|
|
1179
1846
|
const state = toasts.get(element);
|
|
1180
1847
|
if (state.timeoutId) {
|
|
1181
1848
|
clearTimeout(state.timeoutId);
|
|
@@ -1186,7 +1853,7 @@
|
|
|
1186
1853
|
}
|
|
1187
1854
|
|
|
1188
1855
|
function resumeAllTimeouts() {
|
|
1189
|
-
if (!isPaused) return;
|
|
1856
|
+
if (!isPaused || !toaster) return;
|
|
1190
1857
|
|
|
1191
1858
|
isPaused = false;
|
|
1192
1859
|
|
|
@@ -1206,28 +1873,17 @@
|
|
|
1206
1873
|
}
|
|
1207
1874
|
|
|
1208
1875
|
function closeToast(element) {
|
|
1209
|
-
if (!toasts.has(element)) return;
|
|
1876
|
+
if (!element || !toasts.has(element)) return;
|
|
1210
1877
|
|
|
1211
1878
|
const state = toasts.get(element);
|
|
1212
1879
|
clearTimeout(state.timeoutId);
|
|
1213
1880
|
toasts.delete(element);
|
|
1214
|
-
|
|
1881
|
+
|
|
1215
1882
|
if (element.contains(document.activeElement)) document.activeElement.blur();
|
|
1216
1883
|
element.setAttribute('aria-hidden', 'true');
|
|
1217
1884
|
element.addEventListener('transitionend', () => element.remove(), { once: true });
|
|
1218
1885
|
}
|
|
1219
1886
|
|
|
1220
|
-
function executeAction(button, toast) {
|
|
1221
|
-
const actionString = button.dataset.toastAction;
|
|
1222
|
-
if (!actionString) return;
|
|
1223
|
-
try {
|
|
1224
|
-
const func = new Function('close', actionString);
|
|
1225
|
-
func(() => closeToast(toast));
|
|
1226
|
-
} catch (event) {
|
|
1227
|
-
console.error('Error executing toast action:', event);
|
|
1228
|
-
}
|
|
1229
|
-
}
|
|
1230
|
-
|
|
1231
1887
|
function createToast(config) {
|
|
1232
1888
|
const {
|
|
1233
1889
|
category = 'info',
|
|
@@ -1248,7 +1904,7 @@
|
|
|
1248
1904
|
? `<button type="button" class="btn" data-toast-action onclick="${action.onclick}">${action.label}</button>`
|
|
1249
1905
|
: '';
|
|
1250
1906
|
const cancelHtml = cancel
|
|
1251
|
-
? `<button type="button" class="btn-outline h-6 text-xs px-2.5 rounded-sm" data-toast-cancel onclick="${cancel?.onclick}">${cancel.label}</button>`
|
|
1907
|
+
? `<button type="button" class="btn-outline h-6 text-xs px-2.5 rounded-sm" data-toast-cancel onclick="${cancel?.onclick || ''}">${cancel.label}</button>`
|
|
1252
1908
|
: '';
|
|
1253
1909
|
|
|
1254
1910
|
const footerHtml = actionHtml || cancelHtml ? `<footer>${actionHtml}${cancelHtml}</footer>` : '';
|
|
@@ -1268,7 +1924,6 @@
|
|
|
1268
1924
|
${descriptionHtml}
|
|
1269
1925
|
</section>
|
|
1270
1926
|
${footerHtml}
|
|
1271
|
-
</div>
|
|
1272
1927
|
</div>
|
|
1273
1928
|
</div>
|
|
1274
1929
|
`;
|
|
@@ -1277,18 +1932,9 @@
|
|
|
1277
1932
|
return template.content.firstChild;
|
|
1278
1933
|
}
|
|
1279
1934
|
|
|
1280
|
-
document.addEventListener('basecoat:toast', (event) => {
|
|
1281
|
-
if (!toaster) {
|
|
1282
|
-
console.error('Cannot create toast: toaster container not found on page.');
|
|
1283
|
-
return;
|
|
1284
|
-
}
|
|
1285
|
-
const config = event.detail?.config || {};
|
|
1286
|
-
const toastElement = createToast(config);
|
|
1287
|
-
toaster.appendChild(toastElement);
|
|
1288
|
-
});
|
|
1289
|
-
|
|
1290
1935
|
if (window.basecoat) {
|
|
1291
1936
|
window.basecoat.register('toaster', '#toaster:not([data-toaster-initialized])', initToaster);
|
|
1292
1937
|
window.basecoat.register('toast', '.toast:not([data-toast-initialized])', initToast);
|
|
1293
1938
|
}
|
|
1294
1939
|
})();
|
|
1940
|
+
|