@smilodon/core 1.0.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/dist/index.cjs +3685 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +3643 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +2 -0
- package/dist/index.min.js.map +1 -0
- package/dist/index.umd.js +3691 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/index.umd.min.js +2 -0
- package/dist/index.umd.min.js.map +1 -0
- package/dist/types/src/components/enhanced-select.d.ts +136 -0
- package/dist/types/src/components/native-select.d.ts +41 -0
- package/dist/types/src/components/select-option.d.ts +77 -0
- package/dist/types/src/config/global-config.d.ts +194 -0
- package/dist/types/src/index.d.ts +14 -0
- package/dist/types/src/renderers/contracts.d.ts +5 -0
- package/dist/types/src/themes/importer.d.ts +112 -0
- package/dist/types/src/types.d.ts +85 -0
- package/dist/types/src/utils/csp-styles.d.ts +74 -0
- package/dist/types/src/utils/data-source.d.ts +37 -0
- package/dist/types/src/utils/dom-pool.d.ts +79 -0
- package/dist/types/src/utils/fenwick-tree.d.ts +71 -0
- package/dist/types/src/utils/security.d.ts +131 -0
- package/dist/types/src/utils/telemetry.d.ts +95 -0
- package/dist/types/src/utils/virtualizer.d.ts +53 -0
- package/dist/types/src/utils/worker-manager.d.ts +106 -0
- package/dist/types/tests/csp.spec.d.ts +5 -0
- package/dist/types/tests/interaction.spec.d.ts +1 -0
- package/dist/types/tests/setup.d.ts +6 -0
- package/dist/types/tests/shadow-dom.spec.d.ts +5 -0
- package/dist/types/tests/smoke.spec.d.ts +1 -0
- package/dist/types/tests/stress.spec.d.ts +5 -0
- package/dist/types/tests/virtualizer.spec.d.ts +1 -0
- package/package.json +52 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,3685 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const createRendererHelpers = (onSelect) => ({
|
|
4
|
+
onSelect,
|
|
5
|
+
getIndex: (node) => {
|
|
6
|
+
const el = node?.closest?.('[data-selectable]');
|
|
7
|
+
if (!el)
|
|
8
|
+
return null;
|
|
9
|
+
const idx = Number(el.dataset.index);
|
|
10
|
+
return Number.isFinite(idx) ? idx : null;
|
|
11
|
+
},
|
|
12
|
+
keyboardFocus: (index) => {
|
|
13
|
+
const el = document.querySelector(`[data-selectable][data-index="${index}"]`);
|
|
14
|
+
el?.focus?.();
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
// Fast template render path: string -> DOM via DocumentFragment, with delegation markers
|
|
18
|
+
function renderTemplate(container, items, optionTemplate) {
|
|
19
|
+
const frag = document.createDocumentFragment();
|
|
20
|
+
for (let i = 0; i < items.length; i++) {
|
|
21
|
+
const item = items[i];
|
|
22
|
+
const wrapper = document.createElement('div');
|
|
23
|
+
wrapper.innerHTML = optionTemplate(item, i);
|
|
24
|
+
let el = wrapper.firstElementChild;
|
|
25
|
+
if (!el) {
|
|
26
|
+
// Edge case: template produced multiple or zero root nodes; wrap children
|
|
27
|
+
const wrap = document.createElement('div');
|
|
28
|
+
wrap.setAttribute('data-selectable', '');
|
|
29
|
+
wrap.setAttribute('data-index', String(i));
|
|
30
|
+
while (wrapper.firstChild)
|
|
31
|
+
wrap.appendChild(wrapper.firstChild);
|
|
32
|
+
frag.appendChild(wrap);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
// Allow override if developer sets data-smilodon-handled="true"
|
|
36
|
+
if (!el.hasAttribute('data-smilodon-handled')) {
|
|
37
|
+
el.setAttribute('data-selectable', '');
|
|
38
|
+
el.setAttribute('data-index', String(i));
|
|
39
|
+
}
|
|
40
|
+
frag.appendChild(el);
|
|
41
|
+
}
|
|
42
|
+
container.replaceChildren(frag);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Fenwick Tree (Binary Indexed Tree) for O(log n) prefix sum queries and updates.
|
|
47
|
+
* Used for cumulative height calculations in variable-height virtualization.
|
|
48
|
+
*
|
|
49
|
+
* Space: O(n)
|
|
50
|
+
* Time: O(log n) for both query and update
|
|
51
|
+
*
|
|
52
|
+
* @see https://en.wikipedia.org/wiki/Fenwick_tree
|
|
53
|
+
*/
|
|
54
|
+
class FenwickTree {
|
|
55
|
+
constructor(size) {
|
|
56
|
+
this.size = size;
|
|
57
|
+
// 1-indexed for cleaner bit manipulation
|
|
58
|
+
this.tree = new Float64Array(size + 1);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Add delta to index i (0-indexed input)
|
|
62
|
+
* Complexity: O(log n)
|
|
63
|
+
*/
|
|
64
|
+
add(index, delta) {
|
|
65
|
+
// Convert to 1-indexed
|
|
66
|
+
let i = index + 1;
|
|
67
|
+
while (i <= this.size) {
|
|
68
|
+
this.tree[i] += delta;
|
|
69
|
+
// Move to next relevant index: i + LSB(i)
|
|
70
|
+
i += i & -i;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get prefix sum from 0 to index (inclusive, 0-indexed input)
|
|
75
|
+
* Complexity: O(log n)
|
|
76
|
+
*/
|
|
77
|
+
sum(index) {
|
|
78
|
+
if (index < 0)
|
|
79
|
+
return 0;
|
|
80
|
+
// Convert to 1-indexed
|
|
81
|
+
let i = index + 1;
|
|
82
|
+
let result = 0;
|
|
83
|
+
while (i > 0) {
|
|
84
|
+
result += this.tree[i];
|
|
85
|
+
// Move to parent: i - LSB(i)
|
|
86
|
+
i -= i & -i;
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get sum in range [left, right] (inclusive, 0-indexed)
|
|
92
|
+
* Complexity: O(log n)
|
|
93
|
+
*/
|
|
94
|
+
rangeSum(left, right) {
|
|
95
|
+
if (left > right)
|
|
96
|
+
return 0;
|
|
97
|
+
return this.sum(right) - (left > 0 ? this.sum(left - 1) : 0);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Update value at index to newValue (handles delta internally)
|
|
101
|
+
* Requires tracking current values externally or use add() directly
|
|
102
|
+
* Complexity: O(log n)
|
|
103
|
+
*/
|
|
104
|
+
update(index, oldValue, newValue) {
|
|
105
|
+
this.add(index, newValue - oldValue);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Binary search to find the smallest index where sum(index) >= target
|
|
109
|
+
* Useful for finding scroll position -> item index mapping
|
|
110
|
+
* Complexity: O(log² n) or O(log n) with optimization
|
|
111
|
+
*
|
|
112
|
+
* @returns index (0-indexed) or -1 if not found
|
|
113
|
+
*/
|
|
114
|
+
lowerBound(target) {
|
|
115
|
+
if (target <= 0)
|
|
116
|
+
return 0;
|
|
117
|
+
// Optimized binary search on Fenwick tree structure
|
|
118
|
+
let pos = 0;
|
|
119
|
+
let sum = 0;
|
|
120
|
+
// Start from highest power of 2 <= size
|
|
121
|
+
let bit = 1 << Math.floor(Math.log2(this.size));
|
|
122
|
+
while (bit > 0) {
|
|
123
|
+
if (pos + bit <= this.size) {
|
|
124
|
+
const nextSum = sum + this.tree[pos + bit];
|
|
125
|
+
if (nextSum < target) {
|
|
126
|
+
pos += bit;
|
|
127
|
+
sum = nextSum;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
bit >>= 1;
|
|
131
|
+
}
|
|
132
|
+
// Convert back to 0-indexed
|
|
133
|
+
return pos;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Reset all values to 0
|
|
137
|
+
* Complexity: O(n)
|
|
138
|
+
*/
|
|
139
|
+
reset() {
|
|
140
|
+
this.tree.fill(0);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Resize the tree (useful for dynamic item lists)
|
|
144
|
+
* Complexity: O(n)
|
|
145
|
+
*/
|
|
146
|
+
resize(newSize) {
|
|
147
|
+
if (newSize === this.size)
|
|
148
|
+
return;
|
|
149
|
+
const oldTree = this.tree;
|
|
150
|
+
const oldSize = this.size;
|
|
151
|
+
this.size = newSize;
|
|
152
|
+
this.tree = new Float64Array(newSize + 1);
|
|
153
|
+
// Copy existing values up to min(oldSize, newSize)
|
|
154
|
+
const copySize = Math.min(oldSize, newSize);
|
|
155
|
+
for (let i = 1; i <= copySize; i++) {
|
|
156
|
+
this.tree[i] = oldTree[i];
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get current size
|
|
161
|
+
*/
|
|
162
|
+
getSize() {
|
|
163
|
+
return this.size;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Export state for serialization (e.g., to SharedArrayBuffer)
|
|
167
|
+
*/
|
|
168
|
+
exportState() {
|
|
169
|
+
return {
|
|
170
|
+
size: this.size,
|
|
171
|
+
tree: Array.from(this.tree),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Import state from serialization
|
|
176
|
+
*/
|
|
177
|
+
static fromState(state) {
|
|
178
|
+
const ft = new FenwickTree(state.size);
|
|
179
|
+
ft.tree = new Float64Array(state.tree);
|
|
180
|
+
return ft;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* DOM Node Pool with LRU eviction and aggressive cleanup.
|
|
186
|
+
* Reuses nodes to minimize GC pressure and layout thrashing.
|
|
187
|
+
*
|
|
188
|
+
* Features:
|
|
189
|
+
* - LRU eviction when pool exceeds maxSize
|
|
190
|
+
* - Style reset to avoid style pollution
|
|
191
|
+
* - Event listener cleanup
|
|
192
|
+
* - Performance telemetry integration
|
|
193
|
+
*/
|
|
194
|
+
class DOMPool {
|
|
195
|
+
constructor(options) {
|
|
196
|
+
this.pool = [];
|
|
197
|
+
// Telemetry
|
|
198
|
+
this.hits = 0;
|
|
199
|
+
this.misses = 0;
|
|
200
|
+
this.evictions = 0;
|
|
201
|
+
this.factory = options.factory;
|
|
202
|
+
this.reset = options.reset || this.defaultReset.bind(this);
|
|
203
|
+
this.maxSize = options.maxSize || 64;
|
|
204
|
+
this.telemetry = options.telemetry || false;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Acquire a node from pool or create new one
|
|
208
|
+
* Complexity: O(n) for LRU scan, amortized O(1) for small pools
|
|
209
|
+
*/
|
|
210
|
+
acquire() {
|
|
211
|
+
const now = performance.now();
|
|
212
|
+
// Find first available node
|
|
213
|
+
for (let i = 0; i < this.pool.length; i++) {
|
|
214
|
+
const poolNode = this.pool[i];
|
|
215
|
+
if (!poolNode.inUse) {
|
|
216
|
+
poolNode.inUse = true;
|
|
217
|
+
poolNode.lastUsed = now;
|
|
218
|
+
this.reset(poolNode.node);
|
|
219
|
+
if (this.telemetry)
|
|
220
|
+
this.hits++;
|
|
221
|
+
return poolNode.node;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// No available nodes, create new one
|
|
225
|
+
const node = this.factory();
|
|
226
|
+
this.pool.push({
|
|
227
|
+
node,
|
|
228
|
+
lastUsed: now,
|
|
229
|
+
inUse: true,
|
|
230
|
+
});
|
|
231
|
+
if (this.telemetry)
|
|
232
|
+
this.misses++;
|
|
233
|
+
// Evict if pool too large
|
|
234
|
+
if (this.pool.length > this.maxSize) {
|
|
235
|
+
this.evictLRU();
|
|
236
|
+
}
|
|
237
|
+
return node;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Release node back to pool
|
|
241
|
+
* Complexity: O(n) to find node
|
|
242
|
+
*/
|
|
243
|
+
release(node) {
|
|
244
|
+
const poolNode = this.pool.find(p => p.node === node);
|
|
245
|
+
if (poolNode) {
|
|
246
|
+
poolNode.inUse = false;
|
|
247
|
+
poolNode.lastUsed = performance.now();
|
|
248
|
+
// Aggressive cleanup
|
|
249
|
+
this.reset(poolNode.node);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Evict least recently used node
|
|
254
|
+
* Complexity: O(n)
|
|
255
|
+
*/
|
|
256
|
+
evictLRU() {
|
|
257
|
+
// Find LRU node that's not in use
|
|
258
|
+
let lruIndex = -1;
|
|
259
|
+
let oldestTime = Infinity;
|
|
260
|
+
for (let i = 0; i < this.pool.length; i++) {
|
|
261
|
+
const poolNode = this.pool[i];
|
|
262
|
+
if (!poolNode.inUse && poolNode.lastUsed < oldestTime) {
|
|
263
|
+
lruIndex = i;
|
|
264
|
+
oldestTime = poolNode.lastUsed;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
if (lruIndex >= 0) {
|
|
268
|
+
const evicted = this.pool.splice(lruIndex, 1)[0];
|
|
269
|
+
// Full cleanup before GC
|
|
270
|
+
this.deepCleanup(evicted.node);
|
|
271
|
+
if (this.telemetry)
|
|
272
|
+
this.evictions++;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Default reset: clear content, remove inline styles, remove attributes
|
|
277
|
+
*/
|
|
278
|
+
defaultReset(node) {
|
|
279
|
+
// Clear content
|
|
280
|
+
node.textContent = '';
|
|
281
|
+
// Remove inline styles
|
|
282
|
+
node.removeAttribute('style');
|
|
283
|
+
// Remove data attributes (preserve data-index, data-selectable for delegation)
|
|
284
|
+
const attrs = Array.from(node.attributes);
|
|
285
|
+
for (const attr of attrs) {
|
|
286
|
+
if (attr.name.startsWith('data-') &&
|
|
287
|
+
attr.name !== 'data-index' &&
|
|
288
|
+
attr.name !== 'data-selectable') {
|
|
289
|
+
node.removeAttribute(attr.name);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// Remove ARIA attributes that might pollute reuse
|
|
293
|
+
const ariaAttrs = ['aria-selected', 'aria-checked', 'aria-disabled'];
|
|
294
|
+
ariaAttrs.forEach(attr => node.removeAttribute(attr));
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Deep cleanup before eviction/destruction
|
|
298
|
+
*/
|
|
299
|
+
deepCleanup(node) {
|
|
300
|
+
// Remove all event listeners by cloning (nuclear option)
|
|
301
|
+
const clone = node.cloneNode(true);
|
|
302
|
+
node.parentNode?.replaceChild(clone, node);
|
|
303
|
+
// Clear references
|
|
304
|
+
node.textContent = '';
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Clear entire pool
|
|
308
|
+
*/
|
|
309
|
+
clear() {
|
|
310
|
+
for (const poolNode of this.pool) {
|
|
311
|
+
this.deepCleanup(poolNode.node);
|
|
312
|
+
}
|
|
313
|
+
this.pool = [];
|
|
314
|
+
this.resetTelemetry();
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Adjust max pool size dynamically
|
|
318
|
+
*/
|
|
319
|
+
setMaxSize(size) {
|
|
320
|
+
this.maxSize = size;
|
|
321
|
+
// Evict excess nodes
|
|
322
|
+
while (this.pool.length > this.maxSize) {
|
|
323
|
+
this.evictLRU();
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Get pool statistics
|
|
328
|
+
*/
|
|
329
|
+
getStats() {
|
|
330
|
+
const available = this.pool.filter(p => !p.inUse).length;
|
|
331
|
+
const hitRate = this.hits + this.misses > 0
|
|
332
|
+
? this.hits / (this.hits + this.misses)
|
|
333
|
+
: 0;
|
|
334
|
+
return {
|
|
335
|
+
total: this.pool.length,
|
|
336
|
+
available,
|
|
337
|
+
inUse: this.pool.length - available,
|
|
338
|
+
maxSize: this.maxSize,
|
|
339
|
+
hits: this.hits,
|
|
340
|
+
misses: this.misses,
|
|
341
|
+
evictions: this.evictions,
|
|
342
|
+
hitRate,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Reset telemetry counters
|
|
347
|
+
*/
|
|
348
|
+
resetTelemetry() {
|
|
349
|
+
this.hits = 0;
|
|
350
|
+
this.misses = 0;
|
|
351
|
+
this.evictions = 0;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
class Virtualizer {
|
|
356
|
+
constructor(container, itemsLength, itemGetter, options) {
|
|
357
|
+
this.measuredHeights = new Map();
|
|
358
|
+
this.activeNodes = new Map(); // index -> node mapping
|
|
359
|
+
this.container = container;
|
|
360
|
+
this.itemsLength = itemsLength;
|
|
361
|
+
this.itemGetter = itemGetter;
|
|
362
|
+
this.options = options;
|
|
363
|
+
this.averageHeight = options.estimatedItemHeight;
|
|
364
|
+
// Initialize DOM pool with LRU eviction
|
|
365
|
+
const maxPoolSize = Math.ceil((options.buffer * 2 + 10) * 1.5) + (options.maxPoolExtra ?? 32);
|
|
366
|
+
this.pool = new DOMPool({
|
|
367
|
+
maxSize: maxPoolSize,
|
|
368
|
+
factory: () => {
|
|
369
|
+
const div = document.createElement('div');
|
|
370
|
+
div.setAttribute('data-selectable', '');
|
|
371
|
+
return div;
|
|
372
|
+
},
|
|
373
|
+
reset: (node) => {
|
|
374
|
+
// Clear content but preserve data-selectable
|
|
375
|
+
node.textContent = '';
|
|
376
|
+
node.removeAttribute('style');
|
|
377
|
+
// Remove aria attributes
|
|
378
|
+
node.removeAttribute('aria-selected');
|
|
379
|
+
node.removeAttribute('aria-checked');
|
|
380
|
+
},
|
|
381
|
+
telemetry: options.enableTelemetry || false,
|
|
382
|
+
});
|
|
383
|
+
// Use Fenwick tree for large lists (>5000 items)
|
|
384
|
+
if (itemsLength > 5000) {
|
|
385
|
+
this.fenwick = new FenwickTree(itemsLength);
|
|
386
|
+
// Initialize with estimated heights
|
|
387
|
+
for (let i = 0; i < itemsLength; i++) {
|
|
388
|
+
this.fenwick.add(i, options.estimatedItemHeight);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
this.container.style.willChange = 'transform';
|
|
392
|
+
}
|
|
393
|
+
computeWindow(scrollTop, viewportHeight) {
|
|
394
|
+
const { buffer } = this.options;
|
|
395
|
+
const startIndex = Math.max(Math.floor(scrollTop / this.averageHeight) - buffer, 0);
|
|
396
|
+
const endIndex = Math.min(Math.ceil((scrollTop + viewportHeight) / this.averageHeight) + buffer, this.itemsLength - 1);
|
|
397
|
+
const windowSize = Math.max(0, endIndex - startIndex + 1);
|
|
398
|
+
// Adjust pool size dynamically based on window
|
|
399
|
+
const newPoolSize = windowSize + (this.options.maxPoolExtra ?? 32);
|
|
400
|
+
this.pool.setMaxSize(newPoolSize);
|
|
401
|
+
return { startIndex, endIndex, windowSize };
|
|
402
|
+
}
|
|
403
|
+
cumulativeOffset(index) {
|
|
404
|
+
if (index <= 0)
|
|
405
|
+
return 0;
|
|
406
|
+
// Use Fenwick tree for O(log n) prefix sum
|
|
407
|
+
if (this.fenwick) {
|
|
408
|
+
return this.fenwick.sum(index - 1);
|
|
409
|
+
}
|
|
410
|
+
// Fallback: approximate using average height with sparse adjustments
|
|
411
|
+
let total = 0;
|
|
412
|
+
for (let i = 0; i < index; i++) {
|
|
413
|
+
total += this.measuredHeights.get(i) ?? this.averageHeight;
|
|
414
|
+
}
|
|
415
|
+
return total;
|
|
416
|
+
}
|
|
417
|
+
acquireNode(index) {
|
|
418
|
+
// Check if we already have this node active (reuse)
|
|
419
|
+
const existing = this.activeNodes.get(index);
|
|
420
|
+
if (existing)
|
|
421
|
+
return existing;
|
|
422
|
+
const node = this.pool.acquire();
|
|
423
|
+
node.setAttribute('data-index', String(index));
|
|
424
|
+
this.activeNodes.set(index, node);
|
|
425
|
+
return node;
|
|
426
|
+
}
|
|
427
|
+
releaseNode(index) {
|
|
428
|
+
const node = this.activeNodes.get(index);
|
|
429
|
+
if (node) {
|
|
430
|
+
this.pool.release(node);
|
|
431
|
+
this.activeNodes.delete(index);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
releaseExcess(activeIndices) {
|
|
435
|
+
// Release nodes not in active set
|
|
436
|
+
for (const [index, node] of this.activeNodes) {
|
|
437
|
+
if (!activeIndices.has(index)) {
|
|
438
|
+
this.pool.release(node);
|
|
439
|
+
this.activeNodes.delete(index);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
render(startIndex, endIndex, updateNode) {
|
|
444
|
+
const frag = document.createDocumentFragment();
|
|
445
|
+
const activeIndices = new Set();
|
|
446
|
+
for (let i = startIndex; i <= endIndex; i++) {
|
|
447
|
+
const node = this.acquireNode(i);
|
|
448
|
+
updateNode(node, this.itemGetter(i), i);
|
|
449
|
+
frag.appendChild(node);
|
|
450
|
+
activeIndices.add(i);
|
|
451
|
+
// Measure on appear (deferred)
|
|
452
|
+
queueMicrotask(() => this.measureOnAppear(node, i));
|
|
453
|
+
}
|
|
454
|
+
// Translate container by cumulative offset of startIndex
|
|
455
|
+
const topOffset = this.cumulativeOffset(startIndex);
|
|
456
|
+
this.container.style.transform = `translate3d(0, ${Math.round(topOffset * 100) / 100}px, 0)`;
|
|
457
|
+
this.container.replaceChildren(frag);
|
|
458
|
+
// Recycle nodes not in use
|
|
459
|
+
requestAnimationFrame(() => this.releaseExcess(activeIndices));
|
|
460
|
+
}
|
|
461
|
+
measureOnAppear(node, index) {
|
|
462
|
+
const h = node.offsetHeight;
|
|
463
|
+
const prev = this.measuredHeights.get(index);
|
|
464
|
+
const threshold = this.options.measurementThreshold ?? 5;
|
|
465
|
+
if (prev === undefined || Math.abs(prev - h) > threshold) {
|
|
466
|
+
const oldValue = prev ?? this.averageHeight;
|
|
467
|
+
this.measuredHeights.set(index, h);
|
|
468
|
+
// Update Fenwick tree with new measurement
|
|
469
|
+
if (this.fenwick) {
|
|
470
|
+
this.fenwick.update(index, oldValue, h);
|
|
471
|
+
}
|
|
472
|
+
// Update running average (weighted by measured count)
|
|
473
|
+
const count = this.measuredHeights.size;
|
|
474
|
+
const total = Array.from(this.measuredHeights.values()).reduce((a, b) => a + b, 0);
|
|
475
|
+
this.averageHeight = Math.round((total / count) * 100) / 100;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Get pool statistics (if telemetry enabled)
|
|
480
|
+
*/
|
|
481
|
+
getPoolStats() {
|
|
482
|
+
return this.pool.getStats();
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Update items length and resize Fenwick tree if needed
|
|
486
|
+
*/
|
|
487
|
+
setItemsLength(newLength) {
|
|
488
|
+
this.itemsLength = newLength;
|
|
489
|
+
if (this.fenwick) {
|
|
490
|
+
this.fenwick.resize(newLength);
|
|
491
|
+
}
|
|
492
|
+
else if (newLength > 5000 && !this.fenwick) {
|
|
493
|
+
// Activate Fenwick tree when crossing threshold
|
|
494
|
+
this.fenwick = new FenwickTree(newLength);
|
|
495
|
+
// Populate with existing measurements
|
|
496
|
+
for (const [index, height] of this.measuredHeights) {
|
|
497
|
+
this.fenwick.update(index, this.averageHeight, height);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Cleanup resources
|
|
503
|
+
*/
|
|
504
|
+
destroy() {
|
|
505
|
+
this.pool.clear();
|
|
506
|
+
this.activeNodes.clear();
|
|
507
|
+
this.measuredHeights.clear();
|
|
508
|
+
if (this.fenwick) {
|
|
509
|
+
this.fenwick.reset();
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
class NativeSelectElement extends HTMLElement {
|
|
515
|
+
static get observedAttributes() {
|
|
516
|
+
return ['placement', 'strategy', 'portal'];
|
|
517
|
+
}
|
|
518
|
+
constructor() {
|
|
519
|
+
super();
|
|
520
|
+
this._options = {};
|
|
521
|
+
this._items = [];
|
|
522
|
+
// Multi-select & interaction state
|
|
523
|
+
this._selectedSet = new Set(); // indices
|
|
524
|
+
this._selectedItems = new Map(); // index -> item
|
|
525
|
+
this._activeIndex = -1;
|
|
526
|
+
this._multi = false;
|
|
527
|
+
this._typeBuffer = '';
|
|
528
|
+
this._shadow = this.attachShadow({ mode: 'open' });
|
|
529
|
+
this._listRoot = document.createElement('div');
|
|
530
|
+
this._listRoot.setAttribute('role', 'listbox');
|
|
531
|
+
this._listRoot.setAttribute('tabindex', '0');
|
|
532
|
+
this._shadow.appendChild(this._listRoot);
|
|
533
|
+
// Live region for screen reader announcements
|
|
534
|
+
this._liveRegion = document.createElement('div');
|
|
535
|
+
this._liveRegion.setAttribute('role', 'status');
|
|
536
|
+
this._liveRegion.setAttribute('aria-live', 'polite');
|
|
537
|
+
this._liveRegion.style.cssText = 'position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden;';
|
|
538
|
+
this._shadow.appendChild(this._liveRegion);
|
|
539
|
+
this._helpers = createRendererHelpers((item, index) => this._onSelect(item, index));
|
|
540
|
+
// Delegated click
|
|
541
|
+
this._listRoot.addEventListener('click', (e) => {
|
|
542
|
+
const el = e.target.closest('[data-selectable]');
|
|
543
|
+
if (!el)
|
|
544
|
+
return;
|
|
545
|
+
const idx = Number(el.dataset.index);
|
|
546
|
+
const item = this._items[idx];
|
|
547
|
+
this._onSelect(item, idx);
|
|
548
|
+
});
|
|
549
|
+
// Keyboard navigation
|
|
550
|
+
this._listRoot.addEventListener('keydown', (e) => this._onKeydown(e));
|
|
551
|
+
}
|
|
552
|
+
connectedCallback() {
|
|
553
|
+
// Initialize ARIA roles and open event
|
|
554
|
+
this._listRoot.setAttribute('role', 'listbox');
|
|
555
|
+
this._listRoot.setAttribute('aria-label', 'Options list');
|
|
556
|
+
if (this._multi)
|
|
557
|
+
this._listRoot.setAttribute('aria-multiselectable', 'true');
|
|
558
|
+
this._emit('open', {});
|
|
559
|
+
}
|
|
560
|
+
disconnectedCallback() {
|
|
561
|
+
this._emit('close', {});
|
|
562
|
+
// Cleanup: remove listeners if any were added outside constructor
|
|
563
|
+
if (this._typeTimeout)
|
|
564
|
+
window.clearTimeout(this._typeTimeout);
|
|
565
|
+
}
|
|
566
|
+
attributeChangedCallback(name, _oldValue, newValue) {
|
|
567
|
+
switch (name) {
|
|
568
|
+
case 'placement':
|
|
569
|
+
this._options.placement = (newValue ?? undefined);
|
|
570
|
+
break;
|
|
571
|
+
case 'strategy':
|
|
572
|
+
this._options.strategy = (newValue ?? undefined);
|
|
573
|
+
break;
|
|
574
|
+
case 'portal':
|
|
575
|
+
this._options.portal = newValue === 'true' ? true : newValue === 'false' ? false : undefined;
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
set items(items) {
|
|
580
|
+
this._items = items ?? [];
|
|
581
|
+
// initialize virtualizer with estimated height (default 48) and buffer
|
|
582
|
+
this._virtualizer = new Virtualizer(this._listRoot, this._items.length, (i) => this._items[i], { estimatedItemHeight: 48, buffer: 5 });
|
|
583
|
+
this.render();
|
|
584
|
+
}
|
|
585
|
+
get items() {
|
|
586
|
+
return this._items;
|
|
587
|
+
}
|
|
588
|
+
set multi(value) {
|
|
589
|
+
this._multi = value;
|
|
590
|
+
if (value) {
|
|
591
|
+
this._listRoot.setAttribute('aria-multiselectable', 'true');
|
|
592
|
+
}
|
|
593
|
+
else {
|
|
594
|
+
this._listRoot.removeAttribute('aria-multiselectable');
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
get multi() {
|
|
598
|
+
return this._multi;
|
|
599
|
+
}
|
|
600
|
+
get selectedIndices() {
|
|
601
|
+
return Array.from(this._selectedSet);
|
|
602
|
+
}
|
|
603
|
+
get selectedItems() {
|
|
604
|
+
return Array.from(this._selectedItems.values());
|
|
605
|
+
}
|
|
606
|
+
set optionTemplate(template) {
|
|
607
|
+
this._options.optionTemplate = template;
|
|
608
|
+
this.render();
|
|
609
|
+
}
|
|
610
|
+
set optionRenderer(renderer) {
|
|
611
|
+
this._options.optionRenderer = renderer;
|
|
612
|
+
this.render();
|
|
613
|
+
}
|
|
614
|
+
render() {
|
|
615
|
+
const { optionTemplate, optionRenderer } = this._options;
|
|
616
|
+
const viewportHeight = this.getBoundingClientRect().height || 300;
|
|
617
|
+
const scrollTop = this.scrollTop || 0;
|
|
618
|
+
// Update aria-activedescendant
|
|
619
|
+
if (this._activeIndex >= 0) {
|
|
620
|
+
this._listRoot.setAttribute('aria-activedescendant', `option-${this._activeIndex}`);
|
|
621
|
+
}
|
|
622
|
+
else {
|
|
623
|
+
this._listRoot.removeAttribute('aria-activedescendant');
|
|
624
|
+
}
|
|
625
|
+
if (this._virtualizer) {
|
|
626
|
+
const { startIndex, endIndex } = this._virtualizer.computeWindow(scrollTop, viewportHeight);
|
|
627
|
+
this._virtualizer.render(startIndex, endIndex, (node, item, i) => {
|
|
628
|
+
this._applyOptionAttrs(node, i);
|
|
629
|
+
if (optionRenderer) {
|
|
630
|
+
const el = optionRenderer(item, i, this._helpers);
|
|
631
|
+
// replace node contents
|
|
632
|
+
node.replaceChildren(el);
|
|
633
|
+
}
|
|
634
|
+
else if (optionTemplate) {
|
|
635
|
+
const wrapper = document.createElement('div');
|
|
636
|
+
wrapper.innerHTML = optionTemplate(item, i);
|
|
637
|
+
const el = wrapper.firstElementChild;
|
|
638
|
+
node.replaceChildren(el ?? document.createTextNode(String(item)));
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
node.textContent = String(item);
|
|
642
|
+
}
|
|
643
|
+
});
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
const frag = document.createDocumentFragment();
|
|
647
|
+
for (let i = 0; i < this._items.length; i++) {
|
|
648
|
+
const item = this._items[i];
|
|
649
|
+
if (optionRenderer) {
|
|
650
|
+
const el = optionRenderer(item, i, this._helpers);
|
|
651
|
+
if (!el.hasAttribute('data-selectable')) {
|
|
652
|
+
el.setAttribute('data-selectable', '');
|
|
653
|
+
el.setAttribute('data-index', String(i));
|
|
654
|
+
}
|
|
655
|
+
this._applyOptionAttrs(el, i);
|
|
656
|
+
frag.appendChild(el);
|
|
657
|
+
}
|
|
658
|
+
else if (optionTemplate) {
|
|
659
|
+
// Fast path: render all via DocumentFragment in one call
|
|
660
|
+
renderTemplate(this._listRoot, this._items, optionTemplate);
|
|
661
|
+
// Apply ARIA attrs after template render
|
|
662
|
+
this._applyAriaToAll();
|
|
663
|
+
return; // rendering complete
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
const el = document.createElement('div');
|
|
667
|
+
el.textContent = String(item);
|
|
668
|
+
el.setAttribute('data-selectable', '');
|
|
669
|
+
el.setAttribute('data-index', String(i));
|
|
670
|
+
this._applyOptionAttrs(el, i);
|
|
671
|
+
frag.appendChild(el);
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
this._listRoot.replaceChildren(frag);
|
|
675
|
+
}
|
|
676
|
+
_applyOptionAttrs(node, index) {
|
|
677
|
+
node.setAttribute('role', 'option');
|
|
678
|
+
node.id = `option-${index}`;
|
|
679
|
+
if (this._selectedSet.has(index)) {
|
|
680
|
+
node.setAttribute('aria-selected', 'true');
|
|
681
|
+
}
|
|
682
|
+
else {
|
|
683
|
+
node.setAttribute('aria-selected', 'false');
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
_applyAriaToAll() {
|
|
687
|
+
const children = Array.from(this._listRoot.children);
|
|
688
|
+
for (const child of children) {
|
|
689
|
+
const idx = Number(child.dataset.index);
|
|
690
|
+
if (Number.isFinite(idx))
|
|
691
|
+
this._applyOptionAttrs(child, idx);
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
_emit(name, detail) {
|
|
695
|
+
this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true }));
|
|
696
|
+
}
|
|
697
|
+
// Multi-select and interaction methods
|
|
698
|
+
_onSelect(item, index) {
|
|
699
|
+
if (this._multi) {
|
|
700
|
+
if (this._selectedSet.has(index)) {
|
|
701
|
+
this._selectedSet.delete(index);
|
|
702
|
+
this._selectedItems.delete(index);
|
|
703
|
+
}
|
|
704
|
+
else {
|
|
705
|
+
this._selectedSet.add(index);
|
|
706
|
+
this._selectedItems.set(index, item);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
else {
|
|
710
|
+
this._selectedSet.clear();
|
|
711
|
+
this._selectedItems.clear();
|
|
712
|
+
this._selectedSet.add(index);
|
|
713
|
+
this._selectedItems.set(index, item);
|
|
714
|
+
}
|
|
715
|
+
this._activeIndex = index;
|
|
716
|
+
this.render();
|
|
717
|
+
// Emit with all required fields
|
|
718
|
+
const selected = this._selectedSet.has(index);
|
|
719
|
+
this._emit('select', {
|
|
720
|
+
item,
|
|
721
|
+
index,
|
|
722
|
+
value: item?.value ?? item,
|
|
723
|
+
label: item?.label ?? String(item),
|
|
724
|
+
selected,
|
|
725
|
+
multi: this._multi
|
|
726
|
+
});
|
|
727
|
+
this._announce(`Selected ${String(item)}`);
|
|
728
|
+
}
|
|
729
|
+
_onKeydown(e) {
|
|
730
|
+
switch (e.key) {
|
|
731
|
+
case 'ArrowDown':
|
|
732
|
+
e.preventDefault();
|
|
733
|
+
this._moveActive(1);
|
|
734
|
+
break;
|
|
735
|
+
case 'ArrowUp':
|
|
736
|
+
e.preventDefault();
|
|
737
|
+
this._moveActive(-1);
|
|
738
|
+
break;
|
|
739
|
+
case 'Home':
|
|
740
|
+
e.preventDefault();
|
|
741
|
+
this._setActive(0);
|
|
742
|
+
break;
|
|
743
|
+
case 'End':
|
|
744
|
+
e.preventDefault();
|
|
745
|
+
this._setActive(this._items.length - 1);
|
|
746
|
+
break;
|
|
747
|
+
case 'PageDown':
|
|
748
|
+
e.preventDefault();
|
|
749
|
+
this._moveActive(10);
|
|
750
|
+
break;
|
|
751
|
+
case 'PageUp':
|
|
752
|
+
e.preventDefault();
|
|
753
|
+
this._moveActive(-10);
|
|
754
|
+
break;
|
|
755
|
+
case 'Enter':
|
|
756
|
+
case ' ':
|
|
757
|
+
e.preventDefault();
|
|
758
|
+
if (this._activeIndex >= 0) {
|
|
759
|
+
const item = this._items[this._activeIndex];
|
|
760
|
+
this._onSelect(item, this._activeIndex);
|
|
761
|
+
}
|
|
762
|
+
break;
|
|
763
|
+
case 'Escape':
|
|
764
|
+
e.preventDefault();
|
|
765
|
+
this._emit('close', {});
|
|
766
|
+
break;
|
|
767
|
+
default:
|
|
768
|
+
// Type-ahead buffer
|
|
769
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey) {
|
|
770
|
+
this._onType(e.key);
|
|
771
|
+
}
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
_moveActive(delta) {
|
|
776
|
+
const next = Math.max(0, Math.min(this._items.length - 1, this._activeIndex + delta));
|
|
777
|
+
this._setActive(next);
|
|
778
|
+
}
|
|
779
|
+
_setActive(index) {
|
|
780
|
+
this._activeIndex = index;
|
|
781
|
+
this.render();
|
|
782
|
+
this._scrollToActive();
|
|
783
|
+
this._announce(`Navigated to ${String(this._items[index])}`);
|
|
784
|
+
}
|
|
785
|
+
_scrollToActive() {
|
|
786
|
+
const el = this._shadow.getElementById(`option-${this._activeIndex}`);
|
|
787
|
+
el?.scrollIntoView({ block: 'nearest', inline: 'nearest' });
|
|
788
|
+
}
|
|
789
|
+
_onType(char) {
|
|
790
|
+
if (this._typeTimeout)
|
|
791
|
+
window.clearTimeout(this._typeTimeout);
|
|
792
|
+
this._typeBuffer += char.toLowerCase();
|
|
793
|
+
this._typeTimeout = window.setTimeout(() => {
|
|
794
|
+
this._typeBuffer = '';
|
|
795
|
+
}, 400);
|
|
796
|
+
// Find first matching item
|
|
797
|
+
const match = this._items.findIndex((item) => String(item).toLowerCase().startsWith(this._typeBuffer));
|
|
798
|
+
if (match >= 0) {
|
|
799
|
+
this._setActive(match);
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
_announce(msg) {
|
|
803
|
+
if (this._liveRegion) {
|
|
804
|
+
this._liveRegion.textContent = msg;
|
|
805
|
+
setTimeout(() => {
|
|
806
|
+
if (this._liveRegion)
|
|
807
|
+
this._liveRegion.textContent = '';
|
|
808
|
+
}, 1000);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
// Focus management
|
|
812
|
+
focus() {
|
|
813
|
+
this._listRoot.focus();
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
customElements.define('smilodon-select', NativeSelectElement);
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* Global Configuration System for Select Components
|
|
820
|
+
* Allows users to define default behaviors that can be overridden at component level
|
|
821
|
+
*/
|
|
822
|
+
/**
|
|
823
|
+
* Default global configuration
|
|
824
|
+
*/
|
|
825
|
+
const defaultConfig = {
|
|
826
|
+
selection: {
|
|
827
|
+
mode: 'single',
|
|
828
|
+
allowDeselect: false,
|
|
829
|
+
maxSelections: 0,
|
|
830
|
+
showRemoveButton: true,
|
|
831
|
+
closeOnSelect: true,
|
|
832
|
+
},
|
|
833
|
+
scrollToSelected: {
|
|
834
|
+
enabled: true,
|
|
835
|
+
multiSelectTarget: 'first',
|
|
836
|
+
behavior: 'smooth',
|
|
837
|
+
block: 'nearest',
|
|
838
|
+
},
|
|
839
|
+
loadMore: {
|
|
840
|
+
enabled: false,
|
|
841
|
+
itemsPerLoad: 3,
|
|
842
|
+
threshold: 100,
|
|
843
|
+
showLoader: true,
|
|
844
|
+
},
|
|
845
|
+
busyBucket: {
|
|
846
|
+
enabled: true,
|
|
847
|
+
showSpinner: true,
|
|
848
|
+
message: 'Loading...',
|
|
849
|
+
minDisplayTime: 200,
|
|
850
|
+
},
|
|
851
|
+
styles: {
|
|
852
|
+
classNames: {},
|
|
853
|
+
},
|
|
854
|
+
serverSide: {
|
|
855
|
+
enabled: false,
|
|
856
|
+
getValueFromItem: (item) => item?.value ?? item,
|
|
857
|
+
getLabelFromItem: (item) => item?.label ?? String(item),
|
|
858
|
+
},
|
|
859
|
+
infiniteScroll: {
|
|
860
|
+
enabled: false,
|
|
861
|
+
pageSize: 20,
|
|
862
|
+
initialPage: 1,
|
|
863
|
+
cachePages: true,
|
|
864
|
+
maxCachedPages: 10,
|
|
865
|
+
preloadAdjacent: true,
|
|
866
|
+
scrollRestoration: 'auto',
|
|
867
|
+
},
|
|
868
|
+
callbacks: {},
|
|
869
|
+
enabled: true,
|
|
870
|
+
searchable: false,
|
|
871
|
+
placeholder: 'Select an option...',
|
|
872
|
+
virtualize: true,
|
|
873
|
+
estimatedItemHeight: 48,
|
|
874
|
+
};
|
|
875
|
+
/**
|
|
876
|
+
* Global configuration instance
|
|
877
|
+
*/
|
|
878
|
+
class SelectConfigManager {
|
|
879
|
+
constructor() {
|
|
880
|
+
this.config = this.deepClone(defaultConfig);
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Get current global configuration
|
|
884
|
+
*/
|
|
885
|
+
getConfig() {
|
|
886
|
+
return this.config;
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Update global configuration (deep merge)
|
|
890
|
+
*/
|
|
891
|
+
updateConfig(updates) {
|
|
892
|
+
this.config = this.deepMerge(this.config, updates);
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* Reset to default configuration
|
|
896
|
+
*/
|
|
897
|
+
resetConfig() {
|
|
898
|
+
this.config = this.deepClone(defaultConfig);
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Merge component-level config with global config
|
|
902
|
+
* Component-level config takes precedence
|
|
903
|
+
*/
|
|
904
|
+
mergeWithComponentConfig(componentConfig) {
|
|
905
|
+
return this.deepMerge(this.deepClone(this.config), componentConfig);
|
|
906
|
+
}
|
|
907
|
+
deepClone(obj) {
|
|
908
|
+
return JSON.parse(JSON.stringify(obj));
|
|
909
|
+
}
|
|
910
|
+
deepMerge(target, source) {
|
|
911
|
+
const result = { ...target };
|
|
912
|
+
for (const key in source) {
|
|
913
|
+
if (source.hasOwnProperty(key)) {
|
|
914
|
+
const sourceValue = source[key];
|
|
915
|
+
const targetValue = result[key];
|
|
916
|
+
if (sourceValue && typeof sourceValue === 'object' && !Array.isArray(sourceValue)) {
|
|
917
|
+
result[key] = this.deepMerge(targetValue && typeof targetValue === 'object' ? targetValue : {}, sourceValue);
|
|
918
|
+
}
|
|
919
|
+
else {
|
|
920
|
+
result[key] = sourceValue;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
return result;
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* Singleton instance
|
|
929
|
+
*/
|
|
930
|
+
const selectConfig = new SelectConfigManager();
|
|
931
|
+
/**
|
|
932
|
+
* Helper function to configure select globally
|
|
933
|
+
*/
|
|
934
|
+
function configureSelect(config) {
|
|
935
|
+
selectConfig.updateConfig(config);
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* Helper function to reset select configuration
|
|
939
|
+
*/
|
|
940
|
+
function resetSelectConfig() {
|
|
941
|
+
selectConfig.resetConfig();
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* Enhanced Select Component
|
|
946
|
+
* Implements all advanced features: infinite scroll, load more, busy state,
|
|
947
|
+
* server-side selection, and full customization
|
|
948
|
+
*/
|
|
949
|
+
class EnhancedSelect extends HTMLElement {
|
|
950
|
+
constructor() {
|
|
951
|
+
super();
|
|
952
|
+
this._pageCache = {};
|
|
953
|
+
this._typeBuffer = '';
|
|
954
|
+
this._hasError = false;
|
|
955
|
+
this._errorMessage = '';
|
|
956
|
+
this._boundArrowClick = null;
|
|
957
|
+
this._shadow = this.attachShadow({ mode: 'open' });
|
|
958
|
+
this._uniqueId = `enhanced-select-${Math.random().toString(36).substr(2, 9)}`;
|
|
959
|
+
// Merge global config with component-level config
|
|
960
|
+
this._config = selectConfig.getConfig();
|
|
961
|
+
// Initialize state
|
|
962
|
+
this._state = {
|
|
963
|
+
isOpen: false,
|
|
964
|
+
isBusy: false,
|
|
965
|
+
isSearching: false,
|
|
966
|
+
currentPage: this._config.infiniteScroll.initialPage || 1,
|
|
967
|
+
totalPages: 1,
|
|
968
|
+
selectedIndices: new Set(),
|
|
969
|
+
selectedItems: new Map(),
|
|
970
|
+
activeIndex: -1,
|
|
971
|
+
searchQuery: '',
|
|
972
|
+
loadedItems: [],
|
|
973
|
+
groupedItems: [],
|
|
974
|
+
preserveScrollPosition: false,
|
|
975
|
+
lastScrollPosition: 0,
|
|
976
|
+
lastNotifiedQuery: null,
|
|
977
|
+
lastNotifiedResultCount: 0,
|
|
978
|
+
};
|
|
979
|
+
// Create DOM structure
|
|
980
|
+
this._container = this._createContainer();
|
|
981
|
+
this._inputContainer = this._createInputContainer();
|
|
982
|
+
this._input = this._createInput();
|
|
983
|
+
this._arrowContainer = this._createArrowContainer();
|
|
984
|
+
this._dropdown = this._createDropdown();
|
|
985
|
+
this._optionsContainer = this._createOptionsContainer();
|
|
986
|
+
this._liveRegion = this._createLiveRegion();
|
|
987
|
+
this._assembleDOM();
|
|
988
|
+
this._initializeStyles();
|
|
989
|
+
this._attachEventListeners();
|
|
990
|
+
this._initializeObservers();
|
|
991
|
+
}
|
|
992
|
+
connectedCallback() {
|
|
993
|
+
// Load initial data if server-side is enabled
|
|
994
|
+
if (this._config.serverSide.enabled && this._config.serverSide.initialSelectedValues) {
|
|
995
|
+
this._loadInitialSelectedItems();
|
|
996
|
+
}
|
|
997
|
+
// Emit open event if configured to start open
|
|
998
|
+
if (this._config.callbacks.onOpen) {
|
|
999
|
+
this._config.callbacks.onOpen();
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
disconnectedCallback() {
|
|
1003
|
+
// Cleanup observers
|
|
1004
|
+
this._resizeObserver?.disconnect();
|
|
1005
|
+
this._intersectionObserver?.disconnect();
|
|
1006
|
+
if (this._busyTimeout)
|
|
1007
|
+
clearTimeout(this._busyTimeout);
|
|
1008
|
+
if (this._typeTimeout)
|
|
1009
|
+
clearTimeout(this._typeTimeout);
|
|
1010
|
+
if (this._searchTimeout)
|
|
1011
|
+
clearTimeout(this._searchTimeout);
|
|
1012
|
+
// Cleanup arrow click listener
|
|
1013
|
+
if (this._boundArrowClick && this._arrowContainer) {
|
|
1014
|
+
this._arrowContainer.removeEventListener('click', this._boundArrowClick);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
_createContainer() {
|
|
1018
|
+
const container = document.createElement('div');
|
|
1019
|
+
container.className = 'select-container';
|
|
1020
|
+
if (this._config.styles.classNames?.container) {
|
|
1021
|
+
container.className += ' ' + this._config.styles.classNames.container;
|
|
1022
|
+
}
|
|
1023
|
+
if (this._config.styles.container) {
|
|
1024
|
+
Object.assign(container.style, this._config.styles.container);
|
|
1025
|
+
}
|
|
1026
|
+
return container;
|
|
1027
|
+
}
|
|
1028
|
+
_createInputContainer() {
|
|
1029
|
+
const container = document.createElement('div');
|
|
1030
|
+
container.className = 'input-container';
|
|
1031
|
+
return container;
|
|
1032
|
+
}
|
|
1033
|
+
_createInput() {
|
|
1034
|
+
const input = document.createElement('input');
|
|
1035
|
+
input.type = 'text';
|
|
1036
|
+
input.className = 'select-input';
|
|
1037
|
+
input.placeholder = this._config.placeholder || 'Select an option...';
|
|
1038
|
+
input.disabled = !this._config.enabled;
|
|
1039
|
+
input.readOnly = !this._config.searchable;
|
|
1040
|
+
// Update readonly when input is focused if searchable
|
|
1041
|
+
input.addEventListener('focus', () => {
|
|
1042
|
+
if (this._config.searchable) {
|
|
1043
|
+
input.readOnly = false;
|
|
1044
|
+
}
|
|
1045
|
+
});
|
|
1046
|
+
if (this._config.styles.classNames?.input) {
|
|
1047
|
+
input.className += ' ' + this._config.styles.classNames.input;
|
|
1048
|
+
}
|
|
1049
|
+
if (this._config.styles.input) {
|
|
1050
|
+
Object.assign(input.style, this._config.styles.input);
|
|
1051
|
+
}
|
|
1052
|
+
input.setAttribute('role', 'combobox');
|
|
1053
|
+
input.setAttribute('aria-expanded', 'false');
|
|
1054
|
+
input.setAttribute('aria-haspopup', 'listbox');
|
|
1055
|
+
input.setAttribute('aria-autocomplete', this._config.searchable ? 'list' : 'none');
|
|
1056
|
+
return input;
|
|
1057
|
+
}
|
|
1058
|
+
_createDropdown() {
|
|
1059
|
+
const dropdown = document.createElement('div');
|
|
1060
|
+
dropdown.className = 'select-dropdown';
|
|
1061
|
+
dropdown.style.display = 'none';
|
|
1062
|
+
if (this._config.styles.classNames?.dropdown) {
|
|
1063
|
+
dropdown.className += ' ' + this._config.styles.classNames.dropdown;
|
|
1064
|
+
}
|
|
1065
|
+
if (this._config.styles.dropdown) {
|
|
1066
|
+
Object.assign(dropdown.style, this._config.styles.dropdown);
|
|
1067
|
+
}
|
|
1068
|
+
dropdown.setAttribute('role', 'listbox');
|
|
1069
|
+
if (this._config.selection.mode === 'multi') {
|
|
1070
|
+
dropdown.setAttribute('aria-multiselectable', 'true');
|
|
1071
|
+
}
|
|
1072
|
+
return dropdown;
|
|
1073
|
+
}
|
|
1074
|
+
_createOptionsContainer() {
|
|
1075
|
+
const container = document.createElement('div');
|
|
1076
|
+
container.className = 'options-container';
|
|
1077
|
+
return container;
|
|
1078
|
+
}
|
|
1079
|
+
_createLiveRegion() {
|
|
1080
|
+
const liveRegion = document.createElement('div');
|
|
1081
|
+
liveRegion.setAttribute('role', 'status');
|
|
1082
|
+
liveRegion.setAttribute('aria-live', 'polite');
|
|
1083
|
+
liveRegion.setAttribute('aria-atomic', 'true');
|
|
1084
|
+
liveRegion.style.cssText = 'position:absolute;left:-9999px;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0;';
|
|
1085
|
+
return liveRegion;
|
|
1086
|
+
}
|
|
1087
|
+
_createArrowContainer() {
|
|
1088
|
+
const container = document.createElement('div');
|
|
1089
|
+
container.className = 'dropdown-arrow-container';
|
|
1090
|
+
container.innerHTML = `
|
|
1091
|
+
<svg class="dropdown-arrow" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
1092
|
+
<path d="M4 6L8 10L12 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
|
1093
|
+
</svg>
|
|
1094
|
+
`;
|
|
1095
|
+
return container;
|
|
1096
|
+
}
|
|
1097
|
+
_assembleDOM() {
|
|
1098
|
+
this._inputContainer.appendChild(this._input);
|
|
1099
|
+
if (this._arrowContainer) {
|
|
1100
|
+
this._inputContainer.appendChild(this._arrowContainer);
|
|
1101
|
+
}
|
|
1102
|
+
this._container.appendChild(this._inputContainer);
|
|
1103
|
+
this._dropdown.appendChild(this._optionsContainer);
|
|
1104
|
+
this._container.appendChild(this._dropdown);
|
|
1105
|
+
this._shadow.appendChild(this._container);
|
|
1106
|
+
if (this._liveRegion) {
|
|
1107
|
+
this._shadow.appendChild(this._liveRegion);
|
|
1108
|
+
}
|
|
1109
|
+
// Set ARIA relationships
|
|
1110
|
+
const listboxId = `${this._uniqueId}-listbox`;
|
|
1111
|
+
this._dropdown.id = listboxId;
|
|
1112
|
+
this._input.setAttribute('aria-controls', listboxId);
|
|
1113
|
+
this._input.setAttribute('aria-owns', listboxId);
|
|
1114
|
+
}
|
|
1115
|
+
_initializeStyles() {
|
|
1116
|
+
const style = document.createElement('style');
|
|
1117
|
+
style.textContent = `
|
|
1118
|
+
:host {
|
|
1119
|
+
display: block;
|
|
1120
|
+
position: relative;
|
|
1121
|
+
width: 100%;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
.select-container {
|
|
1125
|
+
position: relative;
|
|
1126
|
+
width: 100%;
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
.input-container {
|
|
1130
|
+
position: relative;
|
|
1131
|
+
width: 100%;
|
|
1132
|
+
display: flex;
|
|
1133
|
+
align-items: center;
|
|
1134
|
+
flex-wrap: wrap;
|
|
1135
|
+
gap: 6px;
|
|
1136
|
+
padding: 6px 52px 6px 8px;
|
|
1137
|
+
min-height: 44px;
|
|
1138
|
+
background: white;
|
|
1139
|
+
border: 1px solid #d1d5db;
|
|
1140
|
+
border-radius: 6px;
|
|
1141
|
+
box-sizing: border-box;
|
|
1142
|
+
transition: all 0.2s ease;
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
.input-container:focus-within {
|
|
1146
|
+
border-color: #667eea;
|
|
1147
|
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
/* Gradient separator before arrow */
|
|
1151
|
+
.input-container::after {
|
|
1152
|
+
content: '';
|
|
1153
|
+
position: absolute;
|
|
1154
|
+
top: 50%;
|
|
1155
|
+
right: 40px;
|
|
1156
|
+
transform: translateY(-50%);
|
|
1157
|
+
width: 1px;
|
|
1158
|
+
height: 60%;
|
|
1159
|
+
background: linear-gradient(
|
|
1160
|
+
to bottom,
|
|
1161
|
+
transparent 0%,
|
|
1162
|
+
rgba(0, 0, 0, 0.1) 20%,
|
|
1163
|
+
rgba(0, 0, 0, 0.1) 80%,
|
|
1164
|
+
transparent 100%
|
|
1165
|
+
);
|
|
1166
|
+
pointer-events: none;
|
|
1167
|
+
z-index: 1;
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
.dropdown-arrow-container {
|
|
1171
|
+
position: absolute;
|
|
1172
|
+
top: 0;
|
|
1173
|
+
right: 0;
|
|
1174
|
+
bottom: 0;
|
|
1175
|
+
width: 40px;
|
|
1176
|
+
display: flex;
|
|
1177
|
+
align-items: center;
|
|
1178
|
+
justify-content: center;
|
|
1179
|
+
cursor: pointer;
|
|
1180
|
+
transition: background-color 0.2s ease;
|
|
1181
|
+
border-radius: 0 4px 4px 0;
|
|
1182
|
+
z-index: 2;
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
.dropdown-arrow-container:hover {
|
|
1186
|
+
background-color: rgba(102, 126, 234, 0.08);
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
.dropdown-arrow {
|
|
1190
|
+
width: 16px;
|
|
1191
|
+
height: 16px;
|
|
1192
|
+
color: #667eea;
|
|
1193
|
+
transition: transform 0.2s ease, color 0.2s ease;
|
|
1194
|
+
transform: translateY(0);
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
.dropdown-arrow-container:hover .dropdown-arrow {
|
|
1198
|
+
color: #667eea;
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
.dropdown-arrow.open {
|
|
1202
|
+
transform: rotate(180deg);
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
.select-input {
|
|
1206
|
+
flex: 1;
|
|
1207
|
+
min-width: 120px;
|
|
1208
|
+
padding: 4px;
|
|
1209
|
+
border: none;
|
|
1210
|
+
font-size: 14px;
|
|
1211
|
+
line-height: 1.5;
|
|
1212
|
+
color: #1f2937;
|
|
1213
|
+
background: transparent;
|
|
1214
|
+
box-sizing: border-box;
|
|
1215
|
+
outline: none;
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
.select-input::placeholder {
|
|
1219
|
+
color: #9ca3af;
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
.selection-badge {
|
|
1223
|
+
display: inline-flex;
|
|
1224
|
+
align-items: center;
|
|
1225
|
+
gap: 4px;
|
|
1226
|
+
padding: 4px 8px;
|
|
1227
|
+
margin: 2px;
|
|
1228
|
+
background: #667eea;
|
|
1229
|
+
color: white;
|
|
1230
|
+
border-radius: 4px;
|
|
1231
|
+
font-size: 13px;
|
|
1232
|
+
line-height: 1;
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
.badge-remove {
|
|
1236
|
+
display: inline-flex;
|
|
1237
|
+
align-items: center;
|
|
1238
|
+
justify-content: center;
|
|
1239
|
+
width: 16px;
|
|
1240
|
+
height: 16px;
|
|
1241
|
+
padding: 0;
|
|
1242
|
+
margin-left: 4px;
|
|
1243
|
+
background: rgba(255, 255, 255, 0.3);
|
|
1244
|
+
border: none;
|
|
1245
|
+
border-radius: 50%;
|
|
1246
|
+
color: white;
|
|
1247
|
+
font-size: 16px;
|
|
1248
|
+
line-height: 1;
|
|
1249
|
+
cursor: pointer;
|
|
1250
|
+
transition: background 0.2s;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
.badge-remove:hover {
|
|
1254
|
+
background: rgba(255, 255, 255, 0.5);
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
.select-input:disabled {
|
|
1258
|
+
background-color: var(--select-disabled-bg, #f5f5f5);
|
|
1259
|
+
cursor: not-allowed;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
.select-dropdown {
|
|
1263
|
+
position: absolute;
|
|
1264
|
+
scroll-behavior: smooth;
|
|
1265
|
+
top: 100%;
|
|
1266
|
+
left: 0;
|
|
1267
|
+
right: 0;
|
|
1268
|
+
margin-top: 4px;
|
|
1269
|
+
max-height: 300px;
|
|
1270
|
+
overflow-y: auto;
|
|
1271
|
+
background: var(--select-dropdown-bg, white);
|
|
1272
|
+
border: 1px solid var(--select-dropdown-border, #ccc);
|
|
1273
|
+
border-radius: var(--select-border-radius, 4px);
|
|
1274
|
+
box-shadow: var(--select-dropdown-shadow, 0 4px 6px rgba(0,0,0,0.1));
|
|
1275
|
+
z-index: var(--select-dropdown-z-index, 1000);
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
.options-container {
|
|
1279
|
+
position: relative;
|
|
1280
|
+
transition: opacity 0.2s ease-in-out;
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
.option {
|
|
1284
|
+
padding: 8px 12px;
|
|
1285
|
+
cursor: pointer;
|
|
1286
|
+
color: inherit;
|
|
1287
|
+
transition: background-color 0.15s ease;
|
|
1288
|
+
user-select: none;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
.option:hover {
|
|
1292
|
+
background-color: #f3f4f6;
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
.option.selected {
|
|
1296
|
+
background-color: #e0e7ff;
|
|
1297
|
+
color: #4338ca;
|
|
1298
|
+
font-weight: 500;
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
.option.active {
|
|
1302
|
+
background-color: #f3f4f6;
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
.load-more-container {
|
|
1306
|
+
padding: 12px;
|
|
1307
|
+
text-align: center;
|
|
1308
|
+
border-top: 1px solid var(--select-divider-color, #e0e0e0);
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
.load-more-button {
|
|
1312
|
+
padding: 8px 16px;
|
|
1313
|
+
border: 1px solid var(--select-button-border, #1976d2);
|
|
1314
|
+
background: var(--select-button-bg, white);
|
|
1315
|
+
color: var(--select-button-color, #1976d2);
|
|
1316
|
+
border-radius: 4px;
|
|
1317
|
+
cursor: pointer;
|
|
1318
|
+
font-size: 14px;
|
|
1319
|
+
transition: all 0.2s ease;
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
.load-more-button:hover {
|
|
1323
|
+
background: var(--select-button-hover-bg, #1976d2);
|
|
1324
|
+
color: var(--select-button-hover-color, white);
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
.load-more-button:disabled {
|
|
1328
|
+
opacity: 0.5;
|
|
1329
|
+
cursor: not-allowed;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
.busy-bucket {
|
|
1333
|
+
padding: 16px;
|
|
1334
|
+
text-align: center;
|
|
1335
|
+
color: var(--select-busy-color, #666);
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
.spinner {
|
|
1339
|
+
display: inline-block;
|
|
1340
|
+
width: 20px;
|
|
1341
|
+
height: 20px;
|
|
1342
|
+
border: 2px solid var(--select-spinner-color, #ccc);
|
|
1343
|
+
border-top-color: var(--select-spinner-active-color, #1976d2);
|
|
1344
|
+
border-radius: 50%;
|
|
1345
|
+
animation: spin 0.6s linear infinite;
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
@keyframes spin {
|
|
1349
|
+
to { transform: rotate(360deg); }
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
.empty-state {
|
|
1353
|
+
padding: 24px;
|
|
1354
|
+
text-align: center;
|
|
1355
|
+
color: var(--select-empty-color, #999);
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
.searching-state {
|
|
1359
|
+
padding: 24px;
|
|
1360
|
+
text-align: center;
|
|
1361
|
+
color: #667eea;
|
|
1362
|
+
font-style: italic;
|
|
1363
|
+
animation: pulse 1.5s ease-in-out infinite;
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
@keyframes pulse {
|
|
1367
|
+
0%, 100% { opacity: 1; }
|
|
1368
|
+
50% { opacity: 0.5; }
|
|
1369
|
+
}
|
|
1370
|
+
|
|
1371
|
+
/* Error states */
|
|
1372
|
+
.select-input[aria-invalid="true"] {
|
|
1373
|
+
border-color: var(--select-error-border, #dc2626);
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
.select-input[aria-invalid="true"]:focus {
|
|
1377
|
+
border-color: var(--select-error-border, #dc2626);
|
|
1378
|
+
box-shadow: 0 0 0 2px var(--select-error-shadow, rgba(220, 38, 38, 0.1));
|
|
1379
|
+
outline-color: var(--select-error-border, #dc2626);
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
/* Accessibility: Reduced motion */
|
|
1383
|
+
@media (prefers-reduced-motion: reduce) {
|
|
1384
|
+
* {
|
|
1385
|
+
animation-duration: 0.01ms !important;
|
|
1386
|
+
animation-iteration-count: 1 !important;
|
|
1387
|
+
transition-duration: 0.01ms !important;
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
/* Accessibility: Dark mode */
|
|
1392
|
+
@media (prefers-color-scheme: dark) {
|
|
1393
|
+
.select-input {
|
|
1394
|
+
background: var(--select-dark-bg, #1f2937);
|
|
1395
|
+
color: var(--select-dark-text, #f9fafb);
|
|
1396
|
+
border-color: var(--select-dark-border, #4b5563);
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
.select-dropdown {
|
|
1400
|
+
background: var(--select-dark-dropdown-bg, #1f2937);
|
|
1401
|
+
border-color: var(--select-dark-dropdown-border, #4b5563);
|
|
1402
|
+
color: var(--select-dark-text, #f9fafb);
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
.option:hover {
|
|
1406
|
+
background-color: var(--select-dark-option-hover-bg, #374151);
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
.option.selected {
|
|
1410
|
+
background-color: var(--select-dark-option-selected-bg, #3730a3);
|
|
1411
|
+
color: var(--select-dark-option-selected-text, #e0e7ff);
|
|
1412
|
+
}
|
|
1413
|
+
|
|
1414
|
+
.option.active {
|
|
1415
|
+
background-color: var(--select-dark-option-active-bg, #374151);
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
.busy-bucket {
|
|
1419
|
+
color: var(--select-dark-busy-color, #9ca3af);
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
/* Accessibility: High contrast mode */
|
|
1424
|
+
@media (prefers-contrast: high) {
|
|
1425
|
+
.select-input:focus {
|
|
1426
|
+
outline-width: 3px;
|
|
1427
|
+
outline-color: Highlight;
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
.select-input {
|
|
1431
|
+
border-width: 2px;
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
/* Touch targets (WCAG 2.5.5) */
|
|
1436
|
+
.load-more-button,
|
|
1437
|
+
select-option {
|
|
1438
|
+
min-height: 44px;
|
|
1439
|
+
}
|
|
1440
|
+
`;
|
|
1441
|
+
this._shadow.appendChild(style);
|
|
1442
|
+
}
|
|
1443
|
+
_attachEventListeners() {
|
|
1444
|
+
// Arrow click handler
|
|
1445
|
+
if (this._arrowContainer) {
|
|
1446
|
+
this._boundArrowClick = (e) => {
|
|
1447
|
+
e.stopPropagation();
|
|
1448
|
+
e.preventDefault();
|
|
1449
|
+
const wasOpen = this._state.isOpen;
|
|
1450
|
+
this._state.isOpen = !this._state.isOpen;
|
|
1451
|
+
this._updateDropdownVisibility();
|
|
1452
|
+
this._updateArrowRotation();
|
|
1453
|
+
if (this._state.isOpen && this._config.callbacks.onOpen) {
|
|
1454
|
+
this._config.callbacks.onOpen();
|
|
1455
|
+
}
|
|
1456
|
+
else if (!this._state.isOpen && this._config.callbacks.onClose) {
|
|
1457
|
+
this._config.callbacks.onClose();
|
|
1458
|
+
}
|
|
1459
|
+
// Scroll to selected when opening
|
|
1460
|
+
if (!wasOpen && this._state.isOpen && this._state.selectedIndices.size > 0) {
|
|
1461
|
+
setTimeout(() => this._scrollToSelected(), 50);
|
|
1462
|
+
}
|
|
1463
|
+
};
|
|
1464
|
+
this._arrowContainer.addEventListener('click', this._boundArrowClick);
|
|
1465
|
+
}
|
|
1466
|
+
// Input container click - prevent event from reaching document listener
|
|
1467
|
+
this._container.addEventListener('click', (e) => {
|
|
1468
|
+
e.stopPropagation();
|
|
1469
|
+
});
|
|
1470
|
+
// Input focus/blur
|
|
1471
|
+
this._input.addEventListener('focus', () => this._handleOpen());
|
|
1472
|
+
this._input.addEventListener('blur', (e) => {
|
|
1473
|
+
// Delay to allow option click
|
|
1474
|
+
setTimeout(() => {
|
|
1475
|
+
if (!this._dropdown.contains(document.activeElement)) {
|
|
1476
|
+
this._handleClose();
|
|
1477
|
+
}
|
|
1478
|
+
}, 200);
|
|
1479
|
+
});
|
|
1480
|
+
// Input search
|
|
1481
|
+
this._input.addEventListener('input', (e) => {
|
|
1482
|
+
if (!this._config.searchable)
|
|
1483
|
+
return;
|
|
1484
|
+
console.log('[EnhancedSelect] Input event fired', e.target.value);
|
|
1485
|
+
const query = e.target.value;
|
|
1486
|
+
this._handleSearch(query);
|
|
1487
|
+
});
|
|
1488
|
+
// Keyboard navigation
|
|
1489
|
+
this._input.addEventListener('keydown', (e) => this._handleKeydown(e));
|
|
1490
|
+
// Click outside to close
|
|
1491
|
+
document.addEventListener('click', (e) => {
|
|
1492
|
+
const target = e.target;
|
|
1493
|
+
// Check if click is outside shadow root
|
|
1494
|
+
if (!this._shadow.contains(target) && !this._container.contains(target)) {
|
|
1495
|
+
this._handleClose();
|
|
1496
|
+
}
|
|
1497
|
+
});
|
|
1498
|
+
}
|
|
1499
|
+
_initializeObservers() {
|
|
1500
|
+
// Disconnect existing observer if any
|
|
1501
|
+
if (this._intersectionObserver) {
|
|
1502
|
+
this._intersectionObserver.disconnect();
|
|
1503
|
+
this._intersectionObserver = undefined;
|
|
1504
|
+
}
|
|
1505
|
+
// Intersection observer for infinite scroll
|
|
1506
|
+
if (this._config.infiniteScroll.enabled) {
|
|
1507
|
+
this._intersectionObserver = new IntersectionObserver((entries) => {
|
|
1508
|
+
entries.forEach((entry) => {
|
|
1509
|
+
if (entry.isIntersecting) {
|
|
1510
|
+
console.log('[InfiniteScroll] Sentinel intersected. isBusy:', this._state.isBusy);
|
|
1511
|
+
if (!this._state.isBusy) {
|
|
1512
|
+
this._loadMoreItems();
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
});
|
|
1516
|
+
}, { threshold: 0.1 });
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
async _loadInitialSelectedItems() {
|
|
1520
|
+
if (!this._config.serverSide.fetchSelectedItems || !this._config.serverSide.initialSelectedValues) {
|
|
1521
|
+
return;
|
|
1522
|
+
}
|
|
1523
|
+
this._setBusy(true);
|
|
1524
|
+
try {
|
|
1525
|
+
const items = await this._config.serverSide.fetchSelectedItems(this._config.serverSide.initialSelectedValues);
|
|
1526
|
+
// Add to state
|
|
1527
|
+
items.forEach((item, index) => {
|
|
1528
|
+
this._state.selectedItems.set(index, item);
|
|
1529
|
+
this._state.selectedIndices.add(index);
|
|
1530
|
+
});
|
|
1531
|
+
this._updateInputDisplay();
|
|
1532
|
+
}
|
|
1533
|
+
catch (error) {
|
|
1534
|
+
this._handleError(error);
|
|
1535
|
+
}
|
|
1536
|
+
finally {
|
|
1537
|
+
this._setBusy(false);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
_handleOpen() {
|
|
1541
|
+
if (!this._config.enabled || this._state.isOpen)
|
|
1542
|
+
return;
|
|
1543
|
+
this._state.isOpen = true;
|
|
1544
|
+
this._dropdown.style.display = 'block';
|
|
1545
|
+
this._input.setAttribute('aria-expanded', 'true');
|
|
1546
|
+
this._updateArrowRotation();
|
|
1547
|
+
// Clear search query when opening to show all options
|
|
1548
|
+
// This ensures we can scroll to selected item
|
|
1549
|
+
if (this._config.searchable) {
|
|
1550
|
+
this._state.searchQuery = '';
|
|
1551
|
+
// Don't clear input value if it represents selection
|
|
1552
|
+
// But if we want to search, we might want to clear it?
|
|
1553
|
+
// Standard behavior: input keeps value (label), but dropdown shows all options
|
|
1554
|
+
// until user types.
|
|
1555
|
+
// However, our filtering logic uses _state.searchQuery.
|
|
1556
|
+
// So clearing it here resets the filter.
|
|
1557
|
+
}
|
|
1558
|
+
// Render options when opening
|
|
1559
|
+
this._renderOptions();
|
|
1560
|
+
this._emit('open', {});
|
|
1561
|
+
this._config.callbacks.onOpen?.();
|
|
1562
|
+
// Scroll to selected if configured
|
|
1563
|
+
if (this._config.scrollToSelected.enabled) {
|
|
1564
|
+
// Use setTimeout to allow render to complete
|
|
1565
|
+
setTimeout(() => this._scrollToSelected(), 0);
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
_handleClose() {
|
|
1569
|
+
if (!this._state.isOpen)
|
|
1570
|
+
return;
|
|
1571
|
+
this._state.isOpen = false;
|
|
1572
|
+
this._dropdown.style.display = 'none';
|
|
1573
|
+
this._input.setAttribute('aria-expanded', 'false');
|
|
1574
|
+
this._updateArrowRotation();
|
|
1575
|
+
this._emit('close', {});
|
|
1576
|
+
this._config.callbacks.onClose?.();
|
|
1577
|
+
}
|
|
1578
|
+
_updateDropdownVisibility() {
|
|
1579
|
+
if (this._state.isOpen) {
|
|
1580
|
+
this._dropdown.style.display = 'block';
|
|
1581
|
+
this._input.setAttribute('aria-expanded', 'true');
|
|
1582
|
+
}
|
|
1583
|
+
else {
|
|
1584
|
+
this._dropdown.style.display = 'none';
|
|
1585
|
+
this._input.setAttribute('aria-expanded', 'false');
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
_updateArrowRotation() {
|
|
1589
|
+
if (this._arrowContainer) {
|
|
1590
|
+
const arrow = this._arrowContainer.querySelector('.dropdown-arrow');
|
|
1591
|
+
if (arrow) {
|
|
1592
|
+
if (this._state.isOpen) {
|
|
1593
|
+
arrow.classList.add('open');
|
|
1594
|
+
}
|
|
1595
|
+
else {
|
|
1596
|
+
arrow.classList.remove('open');
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
_handleSearch(query) {
|
|
1602
|
+
console.log('[EnhancedSelect] _handleSearch called with:', JSON.stringify(query));
|
|
1603
|
+
this._state.searchQuery = query;
|
|
1604
|
+
// Clear previous search timeout
|
|
1605
|
+
if (this._searchTimeout) {
|
|
1606
|
+
clearTimeout(this._searchTimeout);
|
|
1607
|
+
}
|
|
1608
|
+
// Search immediately - no debouncing for better responsiveness
|
|
1609
|
+
// Users expect instant feedback as they type
|
|
1610
|
+
this._state.isSearching = false;
|
|
1611
|
+
// Ensure dropdown is open when searching
|
|
1612
|
+
if (!this._state.isOpen) {
|
|
1613
|
+
console.log('[EnhancedSelect] Opening dropdown for search');
|
|
1614
|
+
this._handleOpen();
|
|
1615
|
+
}
|
|
1616
|
+
else {
|
|
1617
|
+
// Filter and render options immediately
|
|
1618
|
+
console.log('[EnhancedSelect] Dropdown already open, re-rendering options');
|
|
1619
|
+
this._renderOptions();
|
|
1620
|
+
}
|
|
1621
|
+
// Get filtered items based on search query - searches ENTIRE phrase
|
|
1622
|
+
const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
1623
|
+
// FIX: Do not trim query to allow searching for phrases with spaces
|
|
1624
|
+
const searchQuery = query.toLowerCase();
|
|
1625
|
+
const filteredItems = searchQuery
|
|
1626
|
+
? this._state.loadedItems.filter((item) => {
|
|
1627
|
+
try {
|
|
1628
|
+
const label = String(getLabel(item)).toLowerCase();
|
|
1629
|
+
// Match the entire search phrase
|
|
1630
|
+
return label.includes(searchQuery);
|
|
1631
|
+
}
|
|
1632
|
+
catch (e) {
|
|
1633
|
+
return false;
|
|
1634
|
+
}
|
|
1635
|
+
})
|
|
1636
|
+
: this._state.loadedItems;
|
|
1637
|
+
const count = filteredItems.length;
|
|
1638
|
+
console.log(`[EnhancedSelect] Search results: ${count} items found for query "${searchQuery}"`);
|
|
1639
|
+
// Announce search results for accessibility
|
|
1640
|
+
if (searchQuery) {
|
|
1641
|
+
this._announce(`${count} result${count !== 1 ? 's' : ''} found for "${query}"`);
|
|
1642
|
+
}
|
|
1643
|
+
// Only notify if query or result count changed to prevent infinite loops
|
|
1644
|
+
if (query !== this._state.lastNotifiedQuery || count !== this._state.lastNotifiedResultCount) {
|
|
1645
|
+
this._state.lastNotifiedQuery = query;
|
|
1646
|
+
this._state.lastNotifiedResultCount = count;
|
|
1647
|
+
// Use setTimeout to avoid synchronous state updates during render
|
|
1648
|
+
setTimeout(() => {
|
|
1649
|
+
this._emit('search', { query, results: filteredItems, count });
|
|
1650
|
+
this._config.callbacks.onSearch?.(query);
|
|
1651
|
+
}, 0);
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
_handleKeydown(e) {
|
|
1655
|
+
switch (e.key) {
|
|
1656
|
+
case 'ArrowDown':
|
|
1657
|
+
e.preventDefault();
|
|
1658
|
+
if (!this._state.isOpen) {
|
|
1659
|
+
this._handleOpen();
|
|
1660
|
+
}
|
|
1661
|
+
else {
|
|
1662
|
+
this._moveActive(1);
|
|
1663
|
+
}
|
|
1664
|
+
break;
|
|
1665
|
+
case 'ArrowUp':
|
|
1666
|
+
e.preventDefault();
|
|
1667
|
+
if (!this._state.isOpen) {
|
|
1668
|
+
this._handleOpen();
|
|
1669
|
+
}
|
|
1670
|
+
else {
|
|
1671
|
+
this._moveActive(-1);
|
|
1672
|
+
}
|
|
1673
|
+
break;
|
|
1674
|
+
case 'Home':
|
|
1675
|
+
e.preventDefault();
|
|
1676
|
+
if (this._state.isOpen) {
|
|
1677
|
+
this._setActive(0);
|
|
1678
|
+
}
|
|
1679
|
+
break;
|
|
1680
|
+
case 'End':
|
|
1681
|
+
e.preventDefault();
|
|
1682
|
+
if (this._state.isOpen) {
|
|
1683
|
+
const options = Array.from(this._optionsContainer.children);
|
|
1684
|
+
this._setActive(options.length - 1);
|
|
1685
|
+
}
|
|
1686
|
+
break;
|
|
1687
|
+
case 'PageDown':
|
|
1688
|
+
e.preventDefault();
|
|
1689
|
+
if (this._state.isOpen) {
|
|
1690
|
+
this._moveActive(10);
|
|
1691
|
+
}
|
|
1692
|
+
break;
|
|
1693
|
+
case 'PageUp':
|
|
1694
|
+
e.preventDefault();
|
|
1695
|
+
if (this._state.isOpen) {
|
|
1696
|
+
this._moveActive(-10);
|
|
1697
|
+
}
|
|
1698
|
+
break;
|
|
1699
|
+
case 'Enter':
|
|
1700
|
+
e.preventDefault();
|
|
1701
|
+
if (this._state.activeIndex >= 0) {
|
|
1702
|
+
this._selectOption(this._state.activeIndex);
|
|
1703
|
+
}
|
|
1704
|
+
break;
|
|
1705
|
+
case 'Escape':
|
|
1706
|
+
e.preventDefault();
|
|
1707
|
+
this._handleClose();
|
|
1708
|
+
break;
|
|
1709
|
+
case 'a':
|
|
1710
|
+
case 'A':
|
|
1711
|
+
if ((e.ctrlKey || e.metaKey) && this._config.selection.mode === 'multi') {
|
|
1712
|
+
e.preventDefault();
|
|
1713
|
+
this._selectAll();
|
|
1714
|
+
}
|
|
1715
|
+
break;
|
|
1716
|
+
default:
|
|
1717
|
+
// Type-ahead search
|
|
1718
|
+
if (e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey) {
|
|
1719
|
+
this._handleTypeAhead(e.key);
|
|
1720
|
+
}
|
|
1721
|
+
break;
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1724
|
+
_moveActive(delta) {
|
|
1725
|
+
const options = Array.from(this._optionsContainer.children);
|
|
1726
|
+
const next = Math.max(0, Math.min(options.length - 1, this._state.activeIndex + delta));
|
|
1727
|
+
this._setActive(next);
|
|
1728
|
+
}
|
|
1729
|
+
_setActive(index) {
|
|
1730
|
+
const options = Array.from(this._optionsContainer.children);
|
|
1731
|
+
// Clear previous active state
|
|
1732
|
+
if (this._state.activeIndex >= 0 && options[this._state.activeIndex]) {
|
|
1733
|
+
options[this._state.activeIndex].setActive(false);
|
|
1734
|
+
}
|
|
1735
|
+
this._state.activeIndex = index;
|
|
1736
|
+
// Set new active state
|
|
1737
|
+
if (options[index]) {
|
|
1738
|
+
options[index].setActive(true);
|
|
1739
|
+
options[index].scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
|
1740
|
+
// Announce position for screen readers
|
|
1741
|
+
const total = options.length;
|
|
1742
|
+
this._announce(`Item ${index + 1} of ${total}`);
|
|
1743
|
+
// Update aria-activedescendant
|
|
1744
|
+
const optionId = `${this._uniqueId}-option-${index}`;
|
|
1745
|
+
this._input.setAttribute('aria-activedescendant', optionId);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
_handleTypeAhead(char) {
|
|
1749
|
+
if (this._typeTimeout)
|
|
1750
|
+
clearTimeout(this._typeTimeout);
|
|
1751
|
+
this._typeBuffer += char.toLowerCase();
|
|
1752
|
+
this._typeTimeout = window.setTimeout(() => {
|
|
1753
|
+
this._typeBuffer = '';
|
|
1754
|
+
}, 500);
|
|
1755
|
+
// Find first matching option
|
|
1756
|
+
const getValue = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
1757
|
+
const matchIndex = this._state.loadedItems.findIndex((item) => getValue(item).toLowerCase().startsWith(this._typeBuffer));
|
|
1758
|
+
if (matchIndex >= 0) {
|
|
1759
|
+
this._setActive(matchIndex);
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
_selectAll() {
|
|
1763
|
+
if (this._config.selection.mode !== 'multi')
|
|
1764
|
+
return;
|
|
1765
|
+
const options = Array.from(this._optionsContainer.children);
|
|
1766
|
+
const maxSelections = this._config.selection.maxSelections || 0;
|
|
1767
|
+
options.forEach((option, index) => {
|
|
1768
|
+
if (maxSelections > 0 && this._state.selectedIndices.size >= maxSelections) {
|
|
1769
|
+
return;
|
|
1770
|
+
}
|
|
1771
|
+
if (!this._state.selectedIndices.has(index)) {
|
|
1772
|
+
const config = option.getConfig();
|
|
1773
|
+
this._state.selectedIndices.add(index);
|
|
1774
|
+
this._state.selectedItems.set(index, config.item);
|
|
1775
|
+
option.setSelected(true);
|
|
1776
|
+
}
|
|
1777
|
+
});
|
|
1778
|
+
this._updateInputDisplay();
|
|
1779
|
+
this._emitChange();
|
|
1780
|
+
this._announce(`Selected all ${options.length} items`);
|
|
1781
|
+
}
|
|
1782
|
+
_announce(message) {
|
|
1783
|
+
if (this._liveRegion) {
|
|
1784
|
+
this._liveRegion.textContent = message;
|
|
1785
|
+
setTimeout(() => {
|
|
1786
|
+
if (this._liveRegion)
|
|
1787
|
+
this._liveRegion.textContent = '';
|
|
1788
|
+
}, 1000);
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
_selectOption(index) {
|
|
1792
|
+
// FIX: Do not rely on this._optionsContainer.children[index] because filtering changes the children
|
|
1793
|
+
// Instead, use the index to update state directly
|
|
1794
|
+
const item = this._state.loadedItems[index];
|
|
1795
|
+
if (!item)
|
|
1796
|
+
return;
|
|
1797
|
+
const isCurrentlySelected = this._state.selectedIndices.has(index);
|
|
1798
|
+
if (this._config.selection.mode === 'single') {
|
|
1799
|
+
// Single select: clear previous and select new
|
|
1800
|
+
const wasSelected = this._state.selectedIndices.has(index);
|
|
1801
|
+
this._state.selectedIndices.clear();
|
|
1802
|
+
this._state.selectedItems.clear();
|
|
1803
|
+
if (!wasSelected) {
|
|
1804
|
+
// Select this option
|
|
1805
|
+
this._state.selectedIndices.add(index);
|
|
1806
|
+
this._state.selectedItems.set(index, item);
|
|
1807
|
+
}
|
|
1808
|
+
// Re-render to update all option styles
|
|
1809
|
+
this._renderOptions();
|
|
1810
|
+
if (this._config.selection.closeOnSelect) {
|
|
1811
|
+
this._handleClose();
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
else {
|
|
1815
|
+
// Multi select with toggle
|
|
1816
|
+
const maxSelections = this._config.selection.maxSelections || 0;
|
|
1817
|
+
if (isCurrentlySelected) {
|
|
1818
|
+
// Deselect (toggle off)
|
|
1819
|
+
this._state.selectedIndices.delete(index);
|
|
1820
|
+
this._state.selectedItems.delete(index);
|
|
1821
|
+
}
|
|
1822
|
+
else {
|
|
1823
|
+
// Select (toggle on)
|
|
1824
|
+
if (maxSelections > 0 && this._state.selectedIndices.size >= maxSelections) {
|
|
1825
|
+
this._announce(`Maximum ${maxSelections} selections allowed`);
|
|
1826
|
+
return; // Max selections reached
|
|
1827
|
+
}
|
|
1828
|
+
this._state.selectedIndices.add(index);
|
|
1829
|
+
this._state.selectedItems.set(index, item);
|
|
1830
|
+
}
|
|
1831
|
+
// Re-render to update styles (safer than trying to find the element in filtered list)
|
|
1832
|
+
this._renderOptions();
|
|
1833
|
+
}
|
|
1834
|
+
this._updateInputDisplay();
|
|
1835
|
+
this._emitChange();
|
|
1836
|
+
// Call user callback
|
|
1837
|
+
const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
|
|
1838
|
+
const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
1839
|
+
this._config.callbacks.onSelect?.({
|
|
1840
|
+
item: item,
|
|
1841
|
+
index,
|
|
1842
|
+
value: getValue(item),
|
|
1843
|
+
label: getLabel(item),
|
|
1844
|
+
selected: this._state.selectedIndices.has(index),
|
|
1845
|
+
});
|
|
1846
|
+
}
|
|
1847
|
+
_handleOptionRemove(index) {
|
|
1848
|
+
const option = this._optionsContainer.children[index];
|
|
1849
|
+
if (!option)
|
|
1850
|
+
return;
|
|
1851
|
+
this._state.selectedIndices.delete(index);
|
|
1852
|
+
this._state.selectedItems.delete(index);
|
|
1853
|
+
option.setSelected(false);
|
|
1854
|
+
this._updateInputDisplay();
|
|
1855
|
+
this._emitChange();
|
|
1856
|
+
const config = option.getConfig();
|
|
1857
|
+
this._emit('remove', { item: config.item, index });
|
|
1858
|
+
}
|
|
1859
|
+
_updateInputDisplay() {
|
|
1860
|
+
const selectedItems = Array.from(this._state.selectedItems.values());
|
|
1861
|
+
const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
1862
|
+
if (selectedItems.length === 0) {
|
|
1863
|
+
this._input.value = '';
|
|
1864
|
+
this._input.placeholder = this._config.placeholder || 'Select an option...';
|
|
1865
|
+
// Clear any badges
|
|
1866
|
+
const existingBadges = this._inputContainer.querySelectorAll('.selection-badge');
|
|
1867
|
+
existingBadges.forEach(badge => badge.remove());
|
|
1868
|
+
}
|
|
1869
|
+
else if (this._config.selection.mode === 'single') {
|
|
1870
|
+
this._input.value = getLabel(selectedItems[0]);
|
|
1871
|
+
}
|
|
1872
|
+
else {
|
|
1873
|
+
// Multi-select: show badges instead of text in input
|
|
1874
|
+
this._input.value = '';
|
|
1875
|
+
this._input.placeholder = '';
|
|
1876
|
+
// Clear existing badges
|
|
1877
|
+
const existingBadges = this._inputContainer.querySelectorAll('.selection-badge');
|
|
1878
|
+
existingBadges.forEach(badge => badge.remove());
|
|
1879
|
+
// Create badges for each selected item
|
|
1880
|
+
const selectedEntries = Array.from(this._state.selectedItems.entries());
|
|
1881
|
+
selectedEntries.forEach(([index, item]) => {
|
|
1882
|
+
const badge = document.createElement('span');
|
|
1883
|
+
badge.className = 'selection-badge';
|
|
1884
|
+
badge.textContent = getLabel(item);
|
|
1885
|
+
// Add remove button to badge
|
|
1886
|
+
const removeBtn = document.createElement('button');
|
|
1887
|
+
removeBtn.className = 'badge-remove';
|
|
1888
|
+
removeBtn.innerHTML = '×';
|
|
1889
|
+
removeBtn.setAttribute('aria-label', `Remove ${getLabel(item)}`);
|
|
1890
|
+
removeBtn.addEventListener('click', (e) => {
|
|
1891
|
+
e.stopPropagation();
|
|
1892
|
+
this._state.selectedIndices.delete(index);
|
|
1893
|
+
this._state.selectedItems.delete(index);
|
|
1894
|
+
this._updateInputDisplay();
|
|
1895
|
+
this._renderOptions();
|
|
1896
|
+
this._emitChange();
|
|
1897
|
+
});
|
|
1898
|
+
badge.appendChild(removeBtn);
|
|
1899
|
+
this._inputContainer.insertBefore(badge, this._input);
|
|
1900
|
+
});
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
_renderOptionsWithAnimation() {
|
|
1904
|
+
// Add fade-out animation
|
|
1905
|
+
this._optionsContainer.style.opacity = '0';
|
|
1906
|
+
this._optionsContainer.style.transition = 'opacity 0.15s ease-out';
|
|
1907
|
+
setTimeout(() => {
|
|
1908
|
+
this._renderOptions();
|
|
1909
|
+
// Fade back in
|
|
1910
|
+
this._optionsContainer.style.opacity = '1';
|
|
1911
|
+
this._optionsContainer.style.transition = 'opacity 0.2s ease-in';
|
|
1912
|
+
}, 150);
|
|
1913
|
+
}
|
|
1914
|
+
_scrollToSelected() {
|
|
1915
|
+
if (this._state.selectedIndices.size === 0)
|
|
1916
|
+
return;
|
|
1917
|
+
const target = this._config.scrollToSelected.multiSelectTarget;
|
|
1918
|
+
const indices = Array.from(this._state.selectedIndices).sort((a, b) => a - b);
|
|
1919
|
+
const targetIndex = target === 'first' ? indices[0] : indices[indices.length - 1];
|
|
1920
|
+
// FIX: Find the option element by ID instead of index in children
|
|
1921
|
+
// because children list might be filtered or reordered
|
|
1922
|
+
const optionId = `${this._uniqueId}-option-${targetIndex}`;
|
|
1923
|
+
// We need to search in shadow root or options container
|
|
1924
|
+
// Since options are custom elements, we can find them by ID if we set it (we do)
|
|
1925
|
+
// But wait, we set ID on the element instance, but is it in the DOM?
|
|
1926
|
+
// If filtered out, it won't be in the DOM.
|
|
1927
|
+
// If we are searching, we might not want to scroll to selected if it's not visible
|
|
1928
|
+
// But if we just opened the dropdown, we usually want to see the selected item.
|
|
1929
|
+
// If the selected item is filtered out, we can't scroll to it.
|
|
1930
|
+
// Try to find the element in the options container
|
|
1931
|
+
// Note: querySelector on shadowRoot works if we set the ID attribute
|
|
1932
|
+
// In _renderOptions we set: option.id = ...
|
|
1933
|
+
const option = this._optionsContainer.querySelector(`[id="${optionId}"]`);
|
|
1934
|
+
if (option) {
|
|
1935
|
+
option.scrollIntoView({
|
|
1936
|
+
block: this._config.scrollToSelected.block || 'center',
|
|
1937
|
+
behavior: 'smooth',
|
|
1938
|
+
});
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
async _loadMoreItems() {
|
|
1942
|
+
if (this._state.isBusy)
|
|
1943
|
+
return;
|
|
1944
|
+
console.log('[InfiniteScroll] _loadMoreItems triggered');
|
|
1945
|
+
this._setBusy(true);
|
|
1946
|
+
// Save scroll position before loading
|
|
1947
|
+
if (this._dropdown) {
|
|
1948
|
+
this._state.lastScrollPosition = this._dropdown.scrollTop;
|
|
1949
|
+
this._state.preserveScrollPosition = true;
|
|
1950
|
+
// Update dropdown to show loading indicator but keep the
|
|
1951
|
+
// same scrollTop so the visible items don't move.
|
|
1952
|
+
this._renderOptions();
|
|
1953
|
+
this._dropdown.scrollTop = this._state.lastScrollPosition;
|
|
1954
|
+
}
|
|
1955
|
+
try {
|
|
1956
|
+
// Emit event for parent to handle
|
|
1957
|
+
this._state.currentPage++;
|
|
1958
|
+
console.log(`[InfiniteScroll] Emitting loadMore event for page ${this._state.currentPage}`);
|
|
1959
|
+
this._emit('loadMore', { page: this._state.currentPage, items: [] });
|
|
1960
|
+
this._config.callbacks.onLoadMore?.(this._state.currentPage);
|
|
1961
|
+
// NOTE: We do NOT set isBusy = false here.
|
|
1962
|
+
// The parent component MUST call setItems() or similar to clear the busy state.
|
|
1963
|
+
// This prevents the sentinel from reappearing before new items are loaded.
|
|
1964
|
+
}
|
|
1965
|
+
catch (error) {
|
|
1966
|
+
this._handleError(error);
|
|
1967
|
+
this._setBusy(false); // Only clear on error
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
_setBusy(busy) {
|
|
1971
|
+
this._state.isBusy = busy;
|
|
1972
|
+
// Trigger re-render to show/hide busy indicator
|
|
1973
|
+
// We use _renderOptions to handle the UI update
|
|
1974
|
+
this._renderOptions();
|
|
1975
|
+
}
|
|
1976
|
+
_showBusyBucket() {
|
|
1977
|
+
// Deprecated: Logic moved to _renderOptions
|
|
1978
|
+
}
|
|
1979
|
+
_hideBusyBucket() {
|
|
1980
|
+
// Deprecated: Logic moved to _renderOptions
|
|
1981
|
+
}
|
|
1982
|
+
_handleError(error) {
|
|
1983
|
+
this._emit('error', { message: error.message, cause: error });
|
|
1984
|
+
this._config.callbacks.onError?.(error);
|
|
1985
|
+
}
|
|
1986
|
+
_emit(name, detail) {
|
|
1987
|
+
this.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true }));
|
|
1988
|
+
}
|
|
1989
|
+
_emitChange() {
|
|
1990
|
+
const selectedItems = Array.from(this._state.selectedItems.values());
|
|
1991
|
+
const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
|
|
1992
|
+
const selectedValues = selectedItems.map(getValue);
|
|
1993
|
+
const selectedIndices = Array.from(this._state.selectedIndices);
|
|
1994
|
+
this._emit('change', { selectedItems, selectedValues, selectedIndices });
|
|
1995
|
+
this._config.callbacks.onChange?.(selectedItems, selectedValues);
|
|
1996
|
+
}
|
|
1997
|
+
// Public API
|
|
1998
|
+
/**
|
|
1999
|
+
* Set items to display in the select
|
|
2000
|
+
*/
|
|
2001
|
+
setItems(items) {
|
|
2002
|
+
const previousLength = this._state.loadedItems.length;
|
|
2003
|
+
this._state.loadedItems = items;
|
|
2004
|
+
// If grouped items exist, flatten them to items
|
|
2005
|
+
if (this._state.groupedItems.length > 0) {
|
|
2006
|
+
this._state.loadedItems = this._state.groupedItems.flatMap(group => group.options);
|
|
2007
|
+
}
|
|
2008
|
+
const newLength = this._state.loadedItems.length;
|
|
2009
|
+
// When infinite scroll is active (preserveScrollPosition = true),
|
|
2010
|
+
// we need to maintain scroll position during the update
|
|
2011
|
+
if (this._state.preserveScrollPosition && this._dropdown) {
|
|
2012
|
+
const targetScrollTop = this._state.lastScrollPosition;
|
|
2013
|
+
console.log('[InfiniteScroll] setItems: before render', {
|
|
2014
|
+
previousLength,
|
|
2015
|
+
newLength,
|
|
2016
|
+
lastScrollPosition: this._state.lastScrollPosition,
|
|
2017
|
+
scrollTop: this._dropdown.scrollTop,
|
|
2018
|
+
scrollHeight: this._dropdown.scrollHeight,
|
|
2019
|
+
clientHeight: this._dropdown.clientHeight
|
|
2020
|
+
});
|
|
2021
|
+
// Only clear loading if we actually got more items
|
|
2022
|
+
if (newLength > previousLength) {
|
|
2023
|
+
this._state.isBusy = false;
|
|
2024
|
+
}
|
|
2025
|
+
this._renderOptions();
|
|
2026
|
+
// Restore the exact scrollTop we had before loading
|
|
2027
|
+
// so the previously visible items stay in place and
|
|
2028
|
+
// new ones simply appear below.
|
|
2029
|
+
this._dropdown.scrollTop = targetScrollTop;
|
|
2030
|
+
// Ensure it sticks after layout
|
|
2031
|
+
requestAnimationFrame(() => {
|
|
2032
|
+
if (this._dropdown) {
|
|
2033
|
+
this._dropdown.scrollTop = targetScrollTop;
|
|
2034
|
+
console.log('[InfiniteScroll] setItems: after render', {
|
|
2035
|
+
newLength,
|
|
2036
|
+
lastScrollPosition: this._state.lastScrollPosition,
|
|
2037
|
+
scrollTop: this._dropdown.scrollTop,
|
|
2038
|
+
scrollHeight: this._dropdown.scrollHeight,
|
|
2039
|
+
clientHeight: this._dropdown.clientHeight
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
});
|
|
2043
|
+
// Only clear preserveScrollPosition if we got new items
|
|
2044
|
+
if (newLength > previousLength) {
|
|
2045
|
+
this._state.preserveScrollPosition = false;
|
|
2046
|
+
}
|
|
2047
|
+
}
|
|
2048
|
+
else {
|
|
2049
|
+
// Normal update - just render normally
|
|
2050
|
+
this._state.isBusy = false;
|
|
2051
|
+
this._renderOptions();
|
|
2052
|
+
}
|
|
2053
|
+
}
|
|
2054
|
+
/**
|
|
2055
|
+
* Set grouped items
|
|
2056
|
+
*/
|
|
2057
|
+
setGroupedItems(groupedItems) {
|
|
2058
|
+
this._state.groupedItems = groupedItems;
|
|
2059
|
+
this._state.loadedItems = groupedItems.flatMap(group => group.options);
|
|
2060
|
+
this._renderOptions();
|
|
2061
|
+
}
|
|
2062
|
+
/**
|
|
2063
|
+
* Get currently selected items
|
|
2064
|
+
*/
|
|
2065
|
+
getSelectedItems() {
|
|
2066
|
+
return Array.from(this._state.selectedItems.values());
|
|
2067
|
+
}
|
|
2068
|
+
/**
|
|
2069
|
+
* Get all loaded items
|
|
2070
|
+
*/
|
|
2071
|
+
get loadedItems() {
|
|
2072
|
+
return this._state.loadedItems;
|
|
2073
|
+
}
|
|
2074
|
+
/**
|
|
2075
|
+
* Get currently selected values
|
|
2076
|
+
*/
|
|
2077
|
+
getSelectedValues() {
|
|
2078
|
+
const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
|
|
2079
|
+
return this.getSelectedItems().map(getValue);
|
|
2080
|
+
}
|
|
2081
|
+
/**
|
|
2082
|
+
* Set selected items by value
|
|
2083
|
+
*/
|
|
2084
|
+
async setSelectedValues(values) {
|
|
2085
|
+
if (this._config.serverSide.enabled && this._config.serverSide.fetchSelectedItems) {
|
|
2086
|
+
await this._loadSelectedItemsByValues(values);
|
|
2087
|
+
}
|
|
2088
|
+
else {
|
|
2089
|
+
// Select from loaded items
|
|
2090
|
+
const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
|
|
2091
|
+
this._state.selectedIndices.clear();
|
|
2092
|
+
this._state.selectedItems.clear();
|
|
2093
|
+
this._state.loadedItems.forEach((item, index) => {
|
|
2094
|
+
if (values.includes(getValue(item))) {
|
|
2095
|
+
this._state.selectedIndices.add(index);
|
|
2096
|
+
this._state.selectedItems.set(index, item);
|
|
2097
|
+
}
|
|
2098
|
+
});
|
|
2099
|
+
this._renderOptions();
|
|
2100
|
+
this._updateInputDisplay();
|
|
2101
|
+
this._emitChange();
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
/**
|
|
2105
|
+
* Load and select items by their values (for infinite scroll scenario)
|
|
2106
|
+
*/
|
|
2107
|
+
async _loadSelectedItemsByValues(values) {
|
|
2108
|
+
if (!this._config.serverSide.fetchSelectedItems)
|
|
2109
|
+
return;
|
|
2110
|
+
this._setBusy(true);
|
|
2111
|
+
try {
|
|
2112
|
+
const items = await this._config.serverSide.fetchSelectedItems(values);
|
|
2113
|
+
this._state.selectedIndices.clear();
|
|
2114
|
+
this._state.selectedItems.clear();
|
|
2115
|
+
items.forEach((item, index) => {
|
|
2116
|
+
this._state.selectedIndices.add(index);
|
|
2117
|
+
this._state.selectedItems.set(index, item);
|
|
2118
|
+
});
|
|
2119
|
+
this._renderOptions();
|
|
2120
|
+
this._updateInputDisplay();
|
|
2121
|
+
this._emitChange();
|
|
2122
|
+
// Scroll to selected if configured
|
|
2123
|
+
if (this._config.scrollToSelected.enabled) {
|
|
2124
|
+
this._scrollToSelected();
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
catch (error) {
|
|
2128
|
+
this._handleError(error);
|
|
2129
|
+
}
|
|
2130
|
+
finally {
|
|
2131
|
+
this._setBusy(false);
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
/**
|
|
2135
|
+
* Clear all selections
|
|
2136
|
+
*/
|
|
2137
|
+
clear() {
|
|
2138
|
+
this._state.selectedIndices.clear();
|
|
2139
|
+
this._state.selectedItems.clear();
|
|
2140
|
+
this._renderOptions();
|
|
2141
|
+
this._updateInputDisplay();
|
|
2142
|
+
this._emitChange();
|
|
2143
|
+
}
|
|
2144
|
+
/**
|
|
2145
|
+
* Open dropdown
|
|
2146
|
+
*/
|
|
2147
|
+
open() {
|
|
2148
|
+
this._handleOpen();
|
|
2149
|
+
}
|
|
2150
|
+
/**
|
|
2151
|
+
* Close dropdown
|
|
2152
|
+
*/
|
|
2153
|
+
close() {
|
|
2154
|
+
this._handleClose();
|
|
2155
|
+
}
|
|
2156
|
+
/**
|
|
2157
|
+
* Update component configuration
|
|
2158
|
+
*/
|
|
2159
|
+
updateConfig(config) {
|
|
2160
|
+
this._config = selectConfig.mergeWithComponentConfig(config);
|
|
2161
|
+
// Update input state based on new config
|
|
2162
|
+
if (this._input) {
|
|
2163
|
+
this._input.readOnly = !this._config.searchable;
|
|
2164
|
+
this._input.setAttribute('aria-autocomplete', this._config.searchable ? 'list' : 'none');
|
|
2165
|
+
}
|
|
2166
|
+
// Re-initialize observers in case infinite scroll was enabled/disabled
|
|
2167
|
+
this._initializeObservers();
|
|
2168
|
+
this._renderOptions();
|
|
2169
|
+
}
|
|
2170
|
+
/**
|
|
2171
|
+
* Set error state
|
|
2172
|
+
*/
|
|
2173
|
+
setError(message) {
|
|
2174
|
+
this._hasError = true;
|
|
2175
|
+
this._errorMessage = message;
|
|
2176
|
+
this._input.setAttribute('aria-invalid', 'true');
|
|
2177
|
+
this._announce(`Error: ${message}`);
|
|
2178
|
+
}
|
|
2179
|
+
/**
|
|
2180
|
+
* Clear error state
|
|
2181
|
+
*/
|
|
2182
|
+
clearError() {
|
|
2183
|
+
this._hasError = false;
|
|
2184
|
+
this._errorMessage = '';
|
|
2185
|
+
this._input.removeAttribute('aria-invalid');
|
|
2186
|
+
}
|
|
2187
|
+
/**
|
|
2188
|
+
* Set required state
|
|
2189
|
+
*/
|
|
2190
|
+
setRequired(required) {
|
|
2191
|
+
if (required) {
|
|
2192
|
+
this._input.setAttribute('aria-required', 'true');
|
|
2193
|
+
this._input.setAttribute('required', '');
|
|
2194
|
+
}
|
|
2195
|
+
else {
|
|
2196
|
+
this._input.removeAttribute('aria-required');
|
|
2197
|
+
this._input.removeAttribute('required');
|
|
2198
|
+
}
|
|
2199
|
+
}
|
|
2200
|
+
/**
|
|
2201
|
+
* Validate selection (for required fields)
|
|
2202
|
+
*/
|
|
2203
|
+
validate() {
|
|
2204
|
+
const isRequired = this._input.hasAttribute('required');
|
|
2205
|
+
if (isRequired && this._state.selectedIndices.size === 0) {
|
|
2206
|
+
this.setError('Selection is required');
|
|
2207
|
+
return false;
|
|
2208
|
+
}
|
|
2209
|
+
this.clearError();
|
|
2210
|
+
return true;
|
|
2211
|
+
}
|
|
2212
|
+
/**
|
|
2213
|
+
* Render options based on current state
|
|
2214
|
+
*/
|
|
2215
|
+
_renderOptions() {
|
|
2216
|
+
console.log('[EnhancedSelect] _renderOptions called');
|
|
2217
|
+
// Cleanup observer
|
|
2218
|
+
if (this._loadMoreTrigger && this._intersectionObserver) {
|
|
2219
|
+
this._intersectionObserver.unobserve(this._loadMoreTrigger);
|
|
2220
|
+
}
|
|
2221
|
+
// Clear options container
|
|
2222
|
+
this._optionsContainer.innerHTML = '';
|
|
2223
|
+
// Ensure dropdown only contains options container (cleanup legacy direct children)
|
|
2224
|
+
// We need to preserve optionsContainer, so we can't just clear dropdown.innerHTML
|
|
2225
|
+
// But we can check if there are other children and remove them
|
|
2226
|
+
Array.from(this._dropdown.children).forEach(child => {
|
|
2227
|
+
if (child !== this._optionsContainer) {
|
|
2228
|
+
this._dropdown.removeChild(child);
|
|
2229
|
+
}
|
|
2230
|
+
});
|
|
2231
|
+
// Ensure dropdown is visible if we are rendering options
|
|
2232
|
+
if (this._state.isOpen && this._dropdown.style.display === 'none') {
|
|
2233
|
+
this._dropdown.style.display = 'block';
|
|
2234
|
+
}
|
|
2235
|
+
// Show searching state (exclusive state)
|
|
2236
|
+
if (this._state.isSearching) {
|
|
2237
|
+
const searching = document.createElement('div');
|
|
2238
|
+
searching.className = 'searching-state';
|
|
2239
|
+
searching.textContent = 'Searching...';
|
|
2240
|
+
this._optionsContainer.appendChild(searching);
|
|
2241
|
+
return;
|
|
2242
|
+
}
|
|
2243
|
+
const getValue = this._config.serverSide.getValueFromItem || ((item) => item?.value ?? item);
|
|
2244
|
+
const getLabel = this._config.serverSide.getLabelFromItem || ((item) => item?.label ?? String(item));
|
|
2245
|
+
// Filter items by search query
|
|
2246
|
+
const query = this._state.searchQuery.toLowerCase();
|
|
2247
|
+
// Handle Grouped Items Rendering (when no search query)
|
|
2248
|
+
if (this._state.groupedItems.length > 0 && !query) {
|
|
2249
|
+
this._state.groupedItems.forEach(group => {
|
|
2250
|
+
const header = document.createElement('div');
|
|
2251
|
+
header.className = 'group-header';
|
|
2252
|
+
header.textContent = group.label;
|
|
2253
|
+
Object.assign(header.style, {
|
|
2254
|
+
padding: '8px 12px',
|
|
2255
|
+
fontWeight: '600',
|
|
2256
|
+
color: '#6b7280',
|
|
2257
|
+
backgroundColor: '#f3f4f6',
|
|
2258
|
+
fontSize: '12px',
|
|
2259
|
+
textTransform: 'uppercase',
|
|
2260
|
+
letterSpacing: '0.05em',
|
|
2261
|
+
position: 'sticky',
|
|
2262
|
+
top: '0',
|
|
2263
|
+
zIndex: '1',
|
|
2264
|
+
borderBottom: '1px solid #e5e7eb'
|
|
2265
|
+
});
|
|
2266
|
+
this._optionsContainer.appendChild(header);
|
|
2267
|
+
group.options.forEach(item => {
|
|
2268
|
+
this._renderSingleOption(item, getValue, getLabel);
|
|
2269
|
+
});
|
|
2270
|
+
});
|
|
2271
|
+
}
|
|
2272
|
+
else {
|
|
2273
|
+
// Normal rendering (flat list or filtered)
|
|
2274
|
+
const itemsToRender = query
|
|
2275
|
+
? this._state.loadedItems.filter((item) => {
|
|
2276
|
+
try {
|
|
2277
|
+
const label = String(getLabel(item)).toLowerCase();
|
|
2278
|
+
return label.includes(query);
|
|
2279
|
+
}
|
|
2280
|
+
catch (e) {
|
|
2281
|
+
return false;
|
|
2282
|
+
}
|
|
2283
|
+
})
|
|
2284
|
+
: this._state.loadedItems;
|
|
2285
|
+
if (itemsToRender.length === 0 && !this._state.isBusy) {
|
|
2286
|
+
const empty = document.createElement('div');
|
|
2287
|
+
empty.className = 'empty-state';
|
|
2288
|
+
if (query) {
|
|
2289
|
+
empty.textContent = `No results found for "${this._state.searchQuery}"`;
|
|
2290
|
+
}
|
|
2291
|
+
else {
|
|
2292
|
+
empty.textContent = 'No options available';
|
|
2293
|
+
}
|
|
2294
|
+
this._optionsContainer.appendChild(empty);
|
|
2295
|
+
}
|
|
2296
|
+
else {
|
|
2297
|
+
itemsToRender.forEach((item) => {
|
|
2298
|
+
this._renderSingleOption(item, getValue, getLabel);
|
|
2299
|
+
});
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
// Append Busy Indicator if busy
|
|
2303
|
+
if (this._state.isBusy && this._config.busyBucket.enabled) {
|
|
2304
|
+
const busyBucket = document.createElement('div');
|
|
2305
|
+
busyBucket.className = 'busy-bucket';
|
|
2306
|
+
if (this._config.busyBucket.showSpinner) {
|
|
2307
|
+
const spinner = document.createElement('div');
|
|
2308
|
+
spinner.className = 'spinner';
|
|
2309
|
+
busyBucket.appendChild(spinner);
|
|
2310
|
+
}
|
|
2311
|
+
if (this._config.busyBucket.message) {
|
|
2312
|
+
const message = document.createElement('div');
|
|
2313
|
+
message.textContent = this._config.busyBucket.message;
|
|
2314
|
+
busyBucket.appendChild(message);
|
|
2315
|
+
}
|
|
2316
|
+
this._optionsContainer.appendChild(busyBucket);
|
|
2317
|
+
}
|
|
2318
|
+
// Append Load More Trigger (Button or Sentinel) if enabled and not busy
|
|
2319
|
+
else if ((this._config.loadMore.enabled || this._config.infiniteScroll.enabled) && this._state.loadedItems.length > 0) {
|
|
2320
|
+
this._addLoadMoreTrigger();
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
_renderSingleOption(item, getValue, getLabel) {
|
|
2324
|
+
const option = document.createElement('div');
|
|
2325
|
+
option.className = 'option';
|
|
2326
|
+
const value = getValue(item);
|
|
2327
|
+
const label = getLabel(item);
|
|
2328
|
+
option.textContent = label;
|
|
2329
|
+
option.dataset.value = String(value);
|
|
2330
|
+
// Check if selected using selectedItems map
|
|
2331
|
+
const isSelected = Array.from(this._state.selectedItems.values()).some(selectedItem => {
|
|
2332
|
+
const selectedValue = getValue(selectedItem);
|
|
2333
|
+
return selectedValue === value;
|
|
2334
|
+
});
|
|
2335
|
+
if (isSelected) {
|
|
2336
|
+
option.classList.add('selected');
|
|
2337
|
+
}
|
|
2338
|
+
option.addEventListener('click', () => {
|
|
2339
|
+
const index = this._state.loadedItems.indexOf(item);
|
|
2340
|
+
if (index !== -1) {
|
|
2341
|
+
this._selectOption(index);
|
|
2342
|
+
}
|
|
2343
|
+
});
|
|
2344
|
+
this._optionsContainer.appendChild(option);
|
|
2345
|
+
}
|
|
2346
|
+
_addLoadMoreTrigger() {
|
|
2347
|
+
const container = document.createElement('div');
|
|
2348
|
+
container.className = 'load-more-container';
|
|
2349
|
+
if (this._config.infiniteScroll.enabled) {
|
|
2350
|
+
// Infinite Scroll: Render an invisible sentinel
|
|
2351
|
+
// It must have some height to be intersected
|
|
2352
|
+
const sentinel = document.createElement('div');
|
|
2353
|
+
sentinel.className = 'infinite-scroll-sentinel';
|
|
2354
|
+
sentinel.style.height = '10px';
|
|
2355
|
+
sentinel.style.width = '100%';
|
|
2356
|
+
sentinel.style.opacity = '0'; // Invisible
|
|
2357
|
+
this._loadMoreTrigger = sentinel;
|
|
2358
|
+
container.appendChild(sentinel);
|
|
2359
|
+
}
|
|
2360
|
+
else {
|
|
2361
|
+
// Manual Load More: Render a button
|
|
2362
|
+
const button = document.createElement('button');
|
|
2363
|
+
button.className = 'load-more-button';
|
|
2364
|
+
button.textContent = `Load ${this._config.loadMore.itemsPerLoad} more`;
|
|
2365
|
+
button.addEventListener('click', () => this._loadMoreItems());
|
|
2366
|
+
this._loadMoreTrigger = button;
|
|
2367
|
+
container.appendChild(button);
|
|
2368
|
+
}
|
|
2369
|
+
this._optionsContainer.appendChild(container);
|
|
2370
|
+
// Setup intersection observer for auto-load
|
|
2371
|
+
if (this._intersectionObserver && this._loadMoreTrigger) {
|
|
2372
|
+
console.log('[InfiniteScroll] Observing sentinel');
|
|
2373
|
+
this._intersectionObserver.observe(this._loadMoreTrigger);
|
|
2374
|
+
}
|
|
2375
|
+
}
|
|
2376
|
+
}
|
|
2377
|
+
// Register custom element
|
|
2378
|
+
if (!customElements.get('enhanced-select')) {
|
|
2379
|
+
customElements.define('enhanced-select', EnhancedSelect);
|
|
2380
|
+
}
|
|
2381
|
+
|
|
2382
|
+
/**
|
|
2383
|
+
* Independent Option Component
|
|
2384
|
+
* High cohesion, low coupling - handles its own selection state and events
|
|
2385
|
+
*/
|
|
2386
|
+
class SelectOption extends HTMLElement {
|
|
2387
|
+
constructor(config) {
|
|
2388
|
+
super();
|
|
2389
|
+
this._config = config;
|
|
2390
|
+
this._shadow = this.attachShadow({ mode: 'open' });
|
|
2391
|
+
this._container = document.createElement('div');
|
|
2392
|
+
this._container.className = 'option-container';
|
|
2393
|
+
this._initializeStyles();
|
|
2394
|
+
this._render();
|
|
2395
|
+
this._attachEventListeners();
|
|
2396
|
+
this._shadow.appendChild(this._container);
|
|
2397
|
+
}
|
|
2398
|
+
_initializeStyles() {
|
|
2399
|
+
const style = document.createElement('style');
|
|
2400
|
+
style.textContent = `
|
|
2401
|
+
:host {
|
|
2402
|
+
display: block;
|
|
2403
|
+
position: relative;
|
|
2404
|
+
}
|
|
2405
|
+
|
|
2406
|
+
.option-container {
|
|
2407
|
+
display: flex;
|
|
2408
|
+
align-items: center;
|
|
2409
|
+
justify-content: space-between;
|
|
2410
|
+
padding: 8px 12px;
|
|
2411
|
+
cursor: pointer;
|
|
2412
|
+
user-select: none;
|
|
2413
|
+
transition: background-color 0.2s ease;
|
|
2414
|
+
}
|
|
2415
|
+
|
|
2416
|
+
.option-container:hover {
|
|
2417
|
+
background-color: var(--select-option-hover-bg, #f0f0f0);
|
|
2418
|
+
}
|
|
2419
|
+
|
|
2420
|
+
.option-container.selected {
|
|
2421
|
+
background-color: var(--select-option-selected-bg, #e3f2fd);
|
|
2422
|
+
color: var(--select-option-selected-color, #1976d2);
|
|
2423
|
+
}
|
|
2424
|
+
|
|
2425
|
+
.option-container.active {
|
|
2426
|
+
outline: 2px solid var(--select-option-active-outline, #1976d2);
|
|
2427
|
+
outline-offset: -2px;
|
|
2428
|
+
}
|
|
2429
|
+
|
|
2430
|
+
.option-container.disabled {
|
|
2431
|
+
opacity: 0.5;
|
|
2432
|
+
cursor: not-allowed;
|
|
2433
|
+
pointer-events: none;
|
|
2434
|
+
}
|
|
2435
|
+
|
|
2436
|
+
.option-content {
|
|
2437
|
+
flex: 1;
|
|
2438
|
+
overflow: hidden;
|
|
2439
|
+
text-overflow: ellipsis;
|
|
2440
|
+
white-space: nowrap;
|
|
2441
|
+
}
|
|
2442
|
+
|
|
2443
|
+
.remove-button {
|
|
2444
|
+
margin-left: 8px;
|
|
2445
|
+
padding: 2px 6px;
|
|
2446
|
+
border: none;
|
|
2447
|
+
background-color: var(--select-remove-btn-bg, transparent);
|
|
2448
|
+
color: var(--select-remove-btn-color, #666);
|
|
2449
|
+
cursor: pointer;
|
|
2450
|
+
border-radius: 3px;
|
|
2451
|
+
font-size: 16px;
|
|
2452
|
+
line-height: 1;
|
|
2453
|
+
transition: all 0.2s ease;
|
|
2454
|
+
}
|
|
2455
|
+
|
|
2456
|
+
.remove-button:hover {
|
|
2457
|
+
background-color: var(--select-remove-btn-hover-bg, #ffebee);
|
|
2458
|
+
color: var(--select-remove-btn-hover-color, #c62828);
|
|
2459
|
+
}
|
|
2460
|
+
|
|
2461
|
+
.remove-button:focus {
|
|
2462
|
+
outline: 2px solid var(--select-remove-btn-focus-outline, #1976d2);
|
|
2463
|
+
outline-offset: 2px;
|
|
2464
|
+
}
|
|
2465
|
+
`;
|
|
2466
|
+
this._shadow.appendChild(style);
|
|
2467
|
+
}
|
|
2468
|
+
_render() {
|
|
2469
|
+
const { item, index, selected, disabled, active, render, showRemoveButton } = this._config;
|
|
2470
|
+
// Clear container
|
|
2471
|
+
this._container.innerHTML = '';
|
|
2472
|
+
// Apply state classes
|
|
2473
|
+
this._container.classList.toggle('selected', selected);
|
|
2474
|
+
this._container.classList.toggle('disabled', disabled || false);
|
|
2475
|
+
this._container.classList.toggle('active', active || false);
|
|
2476
|
+
// Custom class name
|
|
2477
|
+
if (this._config.className) {
|
|
2478
|
+
this._container.className += ' ' + this._config.className;
|
|
2479
|
+
}
|
|
2480
|
+
// Apply custom styles
|
|
2481
|
+
if (this._config.style) {
|
|
2482
|
+
Object.assign(this._container.style, this._config.style);
|
|
2483
|
+
}
|
|
2484
|
+
// Render content
|
|
2485
|
+
const contentDiv = document.createElement('div');
|
|
2486
|
+
contentDiv.className = 'option-content';
|
|
2487
|
+
if (render) {
|
|
2488
|
+
const rendered = render(item, index);
|
|
2489
|
+
if (typeof rendered === 'string') {
|
|
2490
|
+
contentDiv.innerHTML = rendered;
|
|
2491
|
+
}
|
|
2492
|
+
else {
|
|
2493
|
+
contentDiv.appendChild(rendered);
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
else {
|
|
2497
|
+
const label = this._getLabel();
|
|
2498
|
+
contentDiv.textContent = label;
|
|
2499
|
+
}
|
|
2500
|
+
this._container.appendChild(contentDiv);
|
|
2501
|
+
// Add remove button if needed
|
|
2502
|
+
if (showRemoveButton && selected) {
|
|
2503
|
+
this._removeButton = document.createElement('button');
|
|
2504
|
+
this._removeButton.className = 'remove-button';
|
|
2505
|
+
this._removeButton.innerHTML = '×';
|
|
2506
|
+
this._removeButton.setAttribute('aria-label', 'Remove option');
|
|
2507
|
+
this._removeButton.setAttribute('type', 'button');
|
|
2508
|
+
this._container.appendChild(this._removeButton);
|
|
2509
|
+
}
|
|
2510
|
+
// Set ARIA attributes
|
|
2511
|
+
this.setAttribute('role', 'option');
|
|
2512
|
+
this.setAttribute('aria-selected', String(selected));
|
|
2513
|
+
if (disabled)
|
|
2514
|
+
this.setAttribute('aria-disabled', 'true');
|
|
2515
|
+
this.id = `select-option-${index}`;
|
|
2516
|
+
}
|
|
2517
|
+
_attachEventListeners() {
|
|
2518
|
+
// Click handler for selection
|
|
2519
|
+
this._container.addEventListener('click', (e) => {
|
|
2520
|
+
// Don't trigger selection if clicking remove button
|
|
2521
|
+
if (e.target === this._removeButton) {
|
|
2522
|
+
return;
|
|
2523
|
+
}
|
|
2524
|
+
if (!this._config.disabled) {
|
|
2525
|
+
this._handleSelect();
|
|
2526
|
+
}
|
|
2527
|
+
});
|
|
2528
|
+
// Remove button handler
|
|
2529
|
+
if (this._removeButton) {
|
|
2530
|
+
this._removeButton.addEventListener('click', (e) => {
|
|
2531
|
+
e.stopPropagation();
|
|
2532
|
+
this._handleRemove();
|
|
2533
|
+
});
|
|
2534
|
+
}
|
|
2535
|
+
// Keyboard handler
|
|
2536
|
+
this.addEventListener('keydown', (e) => {
|
|
2537
|
+
if (this._config.disabled)
|
|
2538
|
+
return;
|
|
2539
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
2540
|
+
e.preventDefault();
|
|
2541
|
+
this._handleSelect();
|
|
2542
|
+
}
|
|
2543
|
+
else if (e.key === 'Delete' || e.key === 'Backspace') {
|
|
2544
|
+
if (this._config.selected && this._config.showRemoveButton) {
|
|
2545
|
+
e.preventDefault();
|
|
2546
|
+
this._handleRemove();
|
|
2547
|
+
}
|
|
2548
|
+
}
|
|
2549
|
+
});
|
|
2550
|
+
}
|
|
2551
|
+
_handleSelect() {
|
|
2552
|
+
const detail = {
|
|
2553
|
+
item: this._config.item,
|
|
2554
|
+
index: this._config.index,
|
|
2555
|
+
value: this._getValue(),
|
|
2556
|
+
label: this._getLabel(),
|
|
2557
|
+
selected: !this._config.selected,
|
|
2558
|
+
};
|
|
2559
|
+
this.dispatchEvent(new CustomEvent('optionSelect', {
|
|
2560
|
+
detail,
|
|
2561
|
+
bubbles: true,
|
|
2562
|
+
composed: true,
|
|
2563
|
+
}));
|
|
2564
|
+
}
|
|
2565
|
+
_handleRemove() {
|
|
2566
|
+
const detail = {
|
|
2567
|
+
item: this._config.item,
|
|
2568
|
+
index: this._config.index,
|
|
2569
|
+
value: this._getValue(),
|
|
2570
|
+
label: this._getLabel(),
|
|
2571
|
+
selected: false,
|
|
2572
|
+
};
|
|
2573
|
+
this.dispatchEvent(new CustomEvent('optionRemove', {
|
|
2574
|
+
detail,
|
|
2575
|
+
bubbles: true,
|
|
2576
|
+
composed: true,
|
|
2577
|
+
}));
|
|
2578
|
+
}
|
|
2579
|
+
_getValue() {
|
|
2580
|
+
if (this._config.getValue) {
|
|
2581
|
+
return this._config.getValue(this._config.item);
|
|
2582
|
+
}
|
|
2583
|
+
return this._config.item?.value ?? this._config.item;
|
|
2584
|
+
}
|
|
2585
|
+
_getLabel() {
|
|
2586
|
+
if (this._config.getLabel) {
|
|
2587
|
+
return this._config.getLabel(this._config.item);
|
|
2588
|
+
}
|
|
2589
|
+
return this._config.item?.label ?? String(this._config.item);
|
|
2590
|
+
}
|
|
2591
|
+
/**
|
|
2592
|
+
* Update option configuration and re-render
|
|
2593
|
+
*/
|
|
2594
|
+
updateConfig(updates) {
|
|
2595
|
+
this._config = { ...this._config, ...updates };
|
|
2596
|
+
this._render();
|
|
2597
|
+
this._attachEventListeners();
|
|
2598
|
+
}
|
|
2599
|
+
/**
|
|
2600
|
+
* Get current configuration
|
|
2601
|
+
*/
|
|
2602
|
+
getConfig() {
|
|
2603
|
+
return this._config;
|
|
2604
|
+
}
|
|
2605
|
+
/**
|
|
2606
|
+
* Get option value
|
|
2607
|
+
*/
|
|
2608
|
+
getValue() {
|
|
2609
|
+
return this._getValue();
|
|
2610
|
+
}
|
|
2611
|
+
/**
|
|
2612
|
+
* Get option label
|
|
2613
|
+
*/
|
|
2614
|
+
getLabel() {
|
|
2615
|
+
return this._getLabel();
|
|
2616
|
+
}
|
|
2617
|
+
/**
|
|
2618
|
+
* Set selected state
|
|
2619
|
+
*/
|
|
2620
|
+
setSelected(selected) {
|
|
2621
|
+
this._config.selected = selected;
|
|
2622
|
+
this._render();
|
|
2623
|
+
}
|
|
2624
|
+
/**
|
|
2625
|
+
* Set active state
|
|
2626
|
+
*/
|
|
2627
|
+
setActive(active) {
|
|
2628
|
+
this._config.active = active;
|
|
2629
|
+
this._render();
|
|
2630
|
+
}
|
|
2631
|
+
/**
|
|
2632
|
+
* Set disabled state
|
|
2633
|
+
*/
|
|
2634
|
+
setDisabled(disabled) {
|
|
2635
|
+
this._config.disabled = disabled;
|
|
2636
|
+
this._render();
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
// Register custom element
|
|
2640
|
+
if (!customElements.get('select-option')) {
|
|
2641
|
+
customElements.define('select-option', SelectOption);
|
|
2642
|
+
}
|
|
2643
|
+
|
|
2644
|
+
/**
|
|
2645
|
+
* Web Worker protocol types for off-main-thread processing.
|
|
2646
|
+
* Supports heavy transforms, fuzzy search, and data processing.
|
|
2647
|
+
*/
|
|
2648
|
+
/**
|
|
2649
|
+
* Worker manager for handling off-main-thread processing.
|
|
2650
|
+
* Automatically falls back if workers not available or blocked by COOP/COEP.
|
|
2651
|
+
*/
|
|
2652
|
+
class WorkerManager {
|
|
2653
|
+
constructor() {
|
|
2654
|
+
this.worker = null;
|
|
2655
|
+
this.pending = new Map();
|
|
2656
|
+
this.supportsWorkers = false;
|
|
2657
|
+
this.supportsSharedArrayBuffer = false;
|
|
2658
|
+
this.nextId = 0;
|
|
2659
|
+
this.detectFeatures();
|
|
2660
|
+
this.initWorker();
|
|
2661
|
+
}
|
|
2662
|
+
/**
|
|
2663
|
+
* Detect browser capabilities
|
|
2664
|
+
*/
|
|
2665
|
+
detectFeatures() {
|
|
2666
|
+
this.supportsWorkers = typeof Worker !== 'undefined';
|
|
2667
|
+
this.supportsSharedArrayBuffer = typeof SharedArrayBuffer !== 'undefined';
|
|
2668
|
+
}
|
|
2669
|
+
/**
|
|
2670
|
+
* Initialize worker (inline blob URL for simplicity)
|
|
2671
|
+
*/
|
|
2672
|
+
initWorker() {
|
|
2673
|
+
if (!this.supportsWorkers)
|
|
2674
|
+
return;
|
|
2675
|
+
try {
|
|
2676
|
+
const workerCode = this.generateWorkerCode();
|
|
2677
|
+
const blob = new Blob([workerCode], { type: 'application/javascript' });
|
|
2678
|
+
const url = URL.createObjectURL(blob);
|
|
2679
|
+
this.worker = new Worker(url);
|
|
2680
|
+
this.worker.onmessage = this.handleMessage.bind(this);
|
|
2681
|
+
this.worker.onerror = this.handleError.bind(this);
|
|
2682
|
+
// Cleanup blob URL after worker loads
|
|
2683
|
+
URL.revokeObjectURL(url);
|
|
2684
|
+
}
|
|
2685
|
+
catch (err) {
|
|
2686
|
+
console.warn('Worker initialization failed, falling back to main thread', err);
|
|
2687
|
+
this.worker = null;
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
/**
|
|
2691
|
+
* Generate worker code as string
|
|
2692
|
+
*/
|
|
2693
|
+
generateWorkerCode() {
|
|
2694
|
+
return `
|
|
2695
|
+
// Worker code
|
|
2696
|
+
self.onmessage = function(e) {
|
|
2697
|
+
const start = performance.now();
|
|
2698
|
+
const { id, type, payload } = e.data;
|
|
2699
|
+
|
|
2700
|
+
try {
|
|
2701
|
+
let result;
|
|
2702
|
+
|
|
2703
|
+
switch (type) {
|
|
2704
|
+
case 'transform':
|
|
2705
|
+
result = handleTransform(payload);
|
|
2706
|
+
break;
|
|
2707
|
+
case 'search':
|
|
2708
|
+
result = handleSearch(payload);
|
|
2709
|
+
break;
|
|
2710
|
+
case 'filter':
|
|
2711
|
+
result = handleFilter(payload);
|
|
2712
|
+
break;
|
|
2713
|
+
case 'sort':
|
|
2714
|
+
result = handleSort(payload);
|
|
2715
|
+
break;
|
|
2716
|
+
default:
|
|
2717
|
+
throw new Error('Unknown operation: ' + type);
|
|
2718
|
+
}
|
|
2719
|
+
|
|
2720
|
+
const duration = performance.now() - start;
|
|
2721
|
+
self.postMessage({
|
|
2722
|
+
id,
|
|
2723
|
+
success: true,
|
|
2724
|
+
data: result,
|
|
2725
|
+
duration,
|
|
2726
|
+
});
|
|
2727
|
+
} catch (error) {
|
|
2728
|
+
self.postMessage({
|
|
2729
|
+
id,
|
|
2730
|
+
success: false,
|
|
2731
|
+
error: error.message,
|
|
2732
|
+
});
|
|
2733
|
+
}
|
|
2734
|
+
};
|
|
2735
|
+
|
|
2736
|
+
function handleTransform({ items, transformer }) {
|
|
2737
|
+
const fn = new Function('item', 'index', 'return (' + transformer + ')(item, index)');
|
|
2738
|
+
return items.map((item, i) => fn(item, i));
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
function handleSearch({ items, query, fuzzy, maxResults }) {
|
|
2742
|
+
const lowerQuery = query.toLowerCase();
|
|
2743
|
+
const results = [];
|
|
2744
|
+
|
|
2745
|
+
for (let i = 0; i < items.length; i++) {
|
|
2746
|
+
const item = items[i];
|
|
2747
|
+
const text = String(item).toLowerCase();
|
|
2748
|
+
|
|
2749
|
+
if (fuzzy) {
|
|
2750
|
+
if (fuzzyMatch(text, lowerQuery)) {
|
|
2751
|
+
results.push({ item, index: i, score: fuzzyScore(text, lowerQuery) });
|
|
2752
|
+
}
|
|
2753
|
+
} else {
|
|
2754
|
+
if (text.includes(lowerQuery)) {
|
|
2755
|
+
results.push({ item, index: i });
|
|
2756
|
+
}
|
|
2757
|
+
}
|
|
2758
|
+
|
|
2759
|
+
if (maxResults && results.length >= maxResults) break;
|
|
2760
|
+
}
|
|
2761
|
+
|
|
2762
|
+
if (fuzzy) {
|
|
2763
|
+
results.sort((a, b) => b.score - a.score);
|
|
2764
|
+
}
|
|
2765
|
+
|
|
2766
|
+
return results;
|
|
2767
|
+
}
|
|
2768
|
+
|
|
2769
|
+
function handleFilter({ items, predicate }) {
|
|
2770
|
+
const fn = new Function('item', 'index', 'return (' + predicate + ')(item, index)');
|
|
2771
|
+
return items.filter((item, i) => fn(item, i));
|
|
2772
|
+
}
|
|
2773
|
+
|
|
2774
|
+
function handleSort({ items, comparator }) {
|
|
2775
|
+
const fn = new Function('a', 'b', 'return (' + comparator + ')(a, b)');
|
|
2776
|
+
return [...items].sort(fn);
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
// Simple fuzzy matching (Levenshtein-inspired)
|
|
2780
|
+
function fuzzyMatch(text, query) {
|
|
2781
|
+
let qi = 0;
|
|
2782
|
+
for (let ti = 0; ti < text.length && qi < query.length; ti++) {
|
|
2783
|
+
if (text[ti] === query[qi]) qi++;
|
|
2784
|
+
}
|
|
2785
|
+
return qi === query.length;
|
|
2786
|
+
}
|
|
2787
|
+
|
|
2788
|
+
function fuzzyScore(text, query) {
|
|
2789
|
+
let score = 0;
|
|
2790
|
+
let qi = 0;
|
|
2791
|
+
for (let ti = 0; ti < text.length && qi < query.length; ti++) {
|
|
2792
|
+
if (text[ti] === query[qi]) {
|
|
2793
|
+
score += 100 - ti; // Earlier matches score higher
|
|
2794
|
+
qi++;
|
|
2795
|
+
}
|
|
2796
|
+
}
|
|
2797
|
+
return score;
|
|
2798
|
+
}
|
|
2799
|
+
`;
|
|
2800
|
+
}
|
|
2801
|
+
/**
|
|
2802
|
+
* Handle worker message
|
|
2803
|
+
*/
|
|
2804
|
+
handleMessage(e) {
|
|
2805
|
+
const { id, success, data, error } = e.data;
|
|
2806
|
+
const pending = this.pending.get(id);
|
|
2807
|
+
if (!pending)
|
|
2808
|
+
return;
|
|
2809
|
+
clearTimeout(pending.timeout);
|
|
2810
|
+
this.pending.delete(id);
|
|
2811
|
+
if (success) {
|
|
2812
|
+
pending.resolve(data);
|
|
2813
|
+
}
|
|
2814
|
+
else {
|
|
2815
|
+
pending.reject(new Error(error || 'Worker error'));
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
/**
|
|
2819
|
+
* Handle worker error
|
|
2820
|
+
*/
|
|
2821
|
+
handleError(err) {
|
|
2822
|
+
console.error('Worker error:', err);
|
|
2823
|
+
// Reject all pending requests
|
|
2824
|
+
for (const [id, pending] of this.pending) {
|
|
2825
|
+
clearTimeout(pending.timeout);
|
|
2826
|
+
pending.reject(new Error('Worker crashed'));
|
|
2827
|
+
}
|
|
2828
|
+
this.pending.clear();
|
|
2829
|
+
}
|
|
2830
|
+
/**
|
|
2831
|
+
* Execute operation (worker or fallback)
|
|
2832
|
+
*/
|
|
2833
|
+
async execute(type, payload, timeout = 5000) {
|
|
2834
|
+
// Fallback to main thread if no worker
|
|
2835
|
+
if (!this.worker) {
|
|
2836
|
+
return this.executeFallback(type, payload);
|
|
2837
|
+
}
|
|
2838
|
+
const id = `req_${this.nextId++}`;
|
|
2839
|
+
return new Promise((resolve, reject) => {
|
|
2840
|
+
const timeoutId = setTimeout(() => {
|
|
2841
|
+
this.pending.delete(id);
|
|
2842
|
+
reject(new Error('Worker timeout'));
|
|
2843
|
+
}, timeout);
|
|
2844
|
+
this.pending.set(id, { resolve, reject, timeout: timeoutId });
|
|
2845
|
+
this.worker.postMessage({ id, type, payload });
|
|
2846
|
+
});
|
|
2847
|
+
}
|
|
2848
|
+
/**
|
|
2849
|
+
* Fallback to main thread execution
|
|
2850
|
+
*/
|
|
2851
|
+
async executeFallback(type, payload) {
|
|
2852
|
+
// Simple synchronous fallback implementations
|
|
2853
|
+
switch (type) {
|
|
2854
|
+
case 'transform': {
|
|
2855
|
+
const { items, transformer } = payload;
|
|
2856
|
+
const fn = new Function('item', 'index', `return (${transformer})(item, index)`);
|
|
2857
|
+
return items.map((item, i) => fn(item, i));
|
|
2858
|
+
}
|
|
2859
|
+
case 'search': {
|
|
2860
|
+
const { items, query, fuzzy } = payload;
|
|
2861
|
+
const lowerQuery = query.toLowerCase();
|
|
2862
|
+
const results = [];
|
|
2863
|
+
items.forEach((item, index) => {
|
|
2864
|
+
const itemStr = String(item).toLowerCase();
|
|
2865
|
+
if (fuzzy) {
|
|
2866
|
+
// Simple fuzzy match - check if all query chars appear in order
|
|
2867
|
+
let queryIndex = 0;
|
|
2868
|
+
for (let i = 0; i < itemStr.length && queryIndex < lowerQuery.length; i++) {
|
|
2869
|
+
if (itemStr[i] === lowerQuery[queryIndex]) {
|
|
2870
|
+
queryIndex++;
|
|
2871
|
+
}
|
|
2872
|
+
}
|
|
2873
|
+
if (queryIndex === lowerQuery.length) {
|
|
2874
|
+
results.push({ item, index });
|
|
2875
|
+
}
|
|
2876
|
+
}
|
|
2877
|
+
else {
|
|
2878
|
+
// Exact substring match
|
|
2879
|
+
if (itemStr.includes(lowerQuery)) {
|
|
2880
|
+
results.push({ item, index });
|
|
2881
|
+
}
|
|
2882
|
+
}
|
|
2883
|
+
});
|
|
2884
|
+
return results;
|
|
2885
|
+
}
|
|
2886
|
+
case 'filter': {
|
|
2887
|
+
const { items, predicate } = payload;
|
|
2888
|
+
const fn = new Function('item', 'index', `return (${predicate})(item, index)`);
|
|
2889
|
+
return items.filter((item, i) => fn(item, i));
|
|
2890
|
+
}
|
|
2891
|
+
case 'sort': {
|
|
2892
|
+
const { items, comparator } = payload;
|
|
2893
|
+
const fn = new Function('a', 'b', `return (${comparator})(a, b)`);
|
|
2894
|
+
return [...items].sort(fn);
|
|
2895
|
+
}
|
|
2896
|
+
default:
|
|
2897
|
+
throw new Error(`Unknown operation: ${type}`);
|
|
2898
|
+
}
|
|
2899
|
+
}
|
|
2900
|
+
/**
|
|
2901
|
+
* Transform items with custom function
|
|
2902
|
+
*/
|
|
2903
|
+
async transform(items, transformer) {
|
|
2904
|
+
return this.execute('transform', {
|
|
2905
|
+
items,
|
|
2906
|
+
transformer: transformer.toString(),
|
|
2907
|
+
});
|
|
2908
|
+
}
|
|
2909
|
+
/**
|
|
2910
|
+
* Search items with optional fuzzy matching
|
|
2911
|
+
*/
|
|
2912
|
+
async search(items, query, fuzzy = false) {
|
|
2913
|
+
return this.execute('search', { items, query, fuzzy });
|
|
2914
|
+
}
|
|
2915
|
+
/**
|
|
2916
|
+
* Filter items with predicate
|
|
2917
|
+
*/
|
|
2918
|
+
async filter(items, predicate) {
|
|
2919
|
+
return this.execute('filter', {
|
|
2920
|
+
items,
|
|
2921
|
+
predicate: predicate.toString(),
|
|
2922
|
+
});
|
|
2923
|
+
}
|
|
2924
|
+
/**
|
|
2925
|
+
* Sort items with comparator
|
|
2926
|
+
*/
|
|
2927
|
+
async sort(items, comparator) {
|
|
2928
|
+
return this.execute('sort', {
|
|
2929
|
+
items,
|
|
2930
|
+
comparator: comparator.toString(),
|
|
2931
|
+
});
|
|
2932
|
+
}
|
|
2933
|
+
/**
|
|
2934
|
+
* Check if workers are supported
|
|
2935
|
+
*/
|
|
2936
|
+
get hasWorkerSupport() {
|
|
2937
|
+
return this.supportsWorkers && this.worker !== null;
|
|
2938
|
+
}
|
|
2939
|
+
/**
|
|
2940
|
+
* Check if SharedArrayBuffer is supported
|
|
2941
|
+
*/
|
|
2942
|
+
get hasSharedArrayBuffer() {
|
|
2943
|
+
return this.supportsSharedArrayBuffer;
|
|
2944
|
+
}
|
|
2945
|
+
/**
|
|
2946
|
+
* Terminate worker
|
|
2947
|
+
*/
|
|
2948
|
+
destroy() {
|
|
2949
|
+
if (this.worker) {
|
|
2950
|
+
// Reject all pending
|
|
2951
|
+
for (const [id, pending] of this.pending) {
|
|
2952
|
+
clearTimeout(pending.timeout);
|
|
2953
|
+
pending.reject(new Error('Worker terminated'));
|
|
2954
|
+
}
|
|
2955
|
+
this.pending.clear();
|
|
2956
|
+
this.worker.terminate();
|
|
2957
|
+
this.worker = null;
|
|
2958
|
+
}
|
|
2959
|
+
}
|
|
2960
|
+
}
|
|
2961
|
+
/**
|
|
2962
|
+
* Singleton instance
|
|
2963
|
+
*/
|
|
2964
|
+
let workerManager = null;
|
|
2965
|
+
function getWorkerManager() {
|
|
2966
|
+
if (!workerManager) {
|
|
2967
|
+
workerManager = new WorkerManager();
|
|
2968
|
+
}
|
|
2969
|
+
return workerManager;
|
|
2970
|
+
}
|
|
2971
|
+
|
|
2972
|
+
/**
|
|
2973
|
+
* Performance telemetry module for tracking frame timing and performance metrics.
|
|
2974
|
+
* Uses PerformanceObserver for precise measurements.
|
|
2975
|
+
*
|
|
2976
|
+
* Tracks:
|
|
2977
|
+
* - Frame time (should be < 16.67ms for 60fps)
|
|
2978
|
+
* - Main thread work per frame (target < 8ms, ideal 4-6ms)
|
|
2979
|
+
* - Long tasks (> 50ms blocks)
|
|
2980
|
+
* - Memory usage (if available)
|
|
2981
|
+
*/
|
|
2982
|
+
class PerformanceTelemetry {
|
|
2983
|
+
constructor() {
|
|
2984
|
+
this.frameTimes = [];
|
|
2985
|
+
this.longTasks = 0;
|
|
2986
|
+
this.lastFrameTime = 0;
|
|
2987
|
+
this.rafId = 0;
|
|
2988
|
+
this.measuring = false;
|
|
2989
|
+
this.observer = null;
|
|
2990
|
+
this.workTimes = [];
|
|
2991
|
+
/**
|
|
2992
|
+
* Measure frame timing via requestAnimationFrame
|
|
2993
|
+
*/
|
|
2994
|
+
this.measureFrame = () => {
|
|
2995
|
+
if (!this.measuring)
|
|
2996
|
+
return;
|
|
2997
|
+
const now = performance.now();
|
|
2998
|
+
const frameTime = now - this.lastFrameTime;
|
|
2999
|
+
this.frameTimes.push(frameTime);
|
|
3000
|
+
// Keep last 60 frames (1 second at 60fps)
|
|
3001
|
+
if (this.frameTimes.length > 60) {
|
|
3002
|
+
this.frameTimes.shift();
|
|
3003
|
+
}
|
|
3004
|
+
this.lastFrameTime = now;
|
|
3005
|
+
this.rafId = requestAnimationFrame(this.measureFrame);
|
|
3006
|
+
};
|
|
3007
|
+
this.setupObserver();
|
|
3008
|
+
}
|
|
3009
|
+
/**
|
|
3010
|
+
* Setup PerformanceObserver for long tasks
|
|
3011
|
+
*/
|
|
3012
|
+
setupObserver() {
|
|
3013
|
+
if (typeof PerformanceObserver === 'undefined')
|
|
3014
|
+
return;
|
|
3015
|
+
try {
|
|
3016
|
+
this.observer = new PerformanceObserver((list) => {
|
|
3017
|
+
for (const entry of list.getEntries()) {
|
|
3018
|
+
if (entry.entryType === 'longtask' && entry.duration > 50) {
|
|
3019
|
+
this.longTasks++;
|
|
3020
|
+
}
|
|
3021
|
+
if (entry.entryType === 'measure') {
|
|
3022
|
+
this.workTimes.push(entry.duration);
|
|
3023
|
+
}
|
|
3024
|
+
}
|
|
3025
|
+
});
|
|
3026
|
+
// Observe long tasks if supported
|
|
3027
|
+
try {
|
|
3028
|
+
this.observer.observe({ entryTypes: ['longtask', 'measure'] });
|
|
3029
|
+
}
|
|
3030
|
+
catch {
|
|
3031
|
+
// Fallback to just measure if longtask not supported
|
|
3032
|
+
this.observer.observe({ entryTypes: ['measure'] });
|
|
3033
|
+
}
|
|
3034
|
+
}
|
|
3035
|
+
catch (err) {
|
|
3036
|
+
console.warn('PerformanceObserver not available', err);
|
|
3037
|
+
}
|
|
3038
|
+
}
|
|
3039
|
+
/**
|
|
3040
|
+
* Start measuring frame performance
|
|
3041
|
+
*/
|
|
3042
|
+
start() {
|
|
3043
|
+
if (this.measuring)
|
|
3044
|
+
return;
|
|
3045
|
+
this.measuring = true;
|
|
3046
|
+
this.frameTimes = [];
|
|
3047
|
+
this.workTimes = [];
|
|
3048
|
+
this.longTasks = 0;
|
|
3049
|
+
this.lastFrameTime = performance.now();
|
|
3050
|
+
this.measureFrame();
|
|
3051
|
+
}
|
|
3052
|
+
/**
|
|
3053
|
+
* Stop measuring
|
|
3054
|
+
*/
|
|
3055
|
+
stop() {
|
|
3056
|
+
this.measuring = false;
|
|
3057
|
+
if (this.rafId) {
|
|
3058
|
+
cancelAnimationFrame(this.rafId);
|
|
3059
|
+
this.rafId = 0;
|
|
3060
|
+
}
|
|
3061
|
+
}
|
|
3062
|
+
/**
|
|
3063
|
+
* Mark start of work for custom measurement
|
|
3064
|
+
*/
|
|
3065
|
+
markStart(label) {
|
|
3066
|
+
performance.mark(`${label}-start`);
|
|
3067
|
+
}
|
|
3068
|
+
/**
|
|
3069
|
+
* Mark end of work and measure duration
|
|
3070
|
+
*/
|
|
3071
|
+
markEnd(label) {
|
|
3072
|
+
performance.mark(`${label}-end`);
|
|
3073
|
+
performance.measure(label, `${label}-start`, `${label}-end`);
|
|
3074
|
+
const measure = performance.getEntriesByName(label, 'measure')[0];
|
|
3075
|
+
return measure ? measure.duration : 0;
|
|
3076
|
+
}
|
|
3077
|
+
/**
|
|
3078
|
+
* Get current metrics snapshot
|
|
3079
|
+
*/
|
|
3080
|
+
getMetrics() {
|
|
3081
|
+
const avgFrameTime = this.frameTimes.length > 0
|
|
3082
|
+
? this.frameTimes.reduce((a, b) => a + b, 0) / this.frameTimes.length
|
|
3083
|
+
: 0;
|
|
3084
|
+
const fps = avgFrameTime > 0 ? 1000 / avgFrameTime : 0;
|
|
3085
|
+
const longFrames = this.frameTimes.filter(t => t > 16.67).length;
|
|
3086
|
+
const droppedFrames = this.frameTimes.filter(t => t > 33.33).length; // > 2 frames
|
|
3087
|
+
const avgMainThreadWork = this.workTimes.length > 0
|
|
3088
|
+
? this.workTimes.reduce((a, b) => a + b, 0) / this.workTimes.length
|
|
3089
|
+
: 0;
|
|
3090
|
+
const metrics = {
|
|
3091
|
+
frames: {
|
|
3092
|
+
frameTime: avgFrameTime,
|
|
3093
|
+
fps,
|
|
3094
|
+
longTasks: longFrames,
|
|
3095
|
+
droppedFrames,
|
|
3096
|
+
},
|
|
3097
|
+
mainThreadWork: avgMainThreadWork,
|
|
3098
|
+
longTaskCount: this.longTasks,
|
|
3099
|
+
};
|
|
3100
|
+
// Add memory info if available
|
|
3101
|
+
if ('memory' in performance && performance.memory) {
|
|
3102
|
+
const mem = performance.memory;
|
|
3103
|
+
metrics.memory = {
|
|
3104
|
+
usedJSHeapSize: mem.usedJSHeapSize,
|
|
3105
|
+
totalJSHeapSize: mem.totalJSHeapSize,
|
|
3106
|
+
jsHeapSizeLimit: mem.jsHeapSizeLimit,
|
|
3107
|
+
};
|
|
3108
|
+
}
|
|
3109
|
+
return metrics;
|
|
3110
|
+
}
|
|
3111
|
+
/**
|
|
3112
|
+
* Check if performance is acceptable
|
|
3113
|
+
*/
|
|
3114
|
+
isPerformanceGood() {
|
|
3115
|
+
const metrics = this.getMetrics();
|
|
3116
|
+
// Target: 60fps (16.67ms), < 8ms main thread work, minimal long tasks
|
|
3117
|
+
return (metrics.frames.fps >= 55 && // Allow some variance
|
|
3118
|
+
metrics.mainThreadWork < 8 &&
|
|
3119
|
+
metrics.frames.longTasks < 3 // Max 5% long frames
|
|
3120
|
+
);
|
|
3121
|
+
}
|
|
3122
|
+
/**
|
|
3123
|
+
* Get detailed report as string
|
|
3124
|
+
*/
|
|
3125
|
+
getReport() {
|
|
3126
|
+
const metrics = this.getMetrics();
|
|
3127
|
+
let report = '=== Performance Report ===\n';
|
|
3128
|
+
report += `FPS: ${metrics.frames.fps.toFixed(2)}\n`;
|
|
3129
|
+
report += `Avg Frame Time: ${metrics.frames.frameTime.toFixed(2)}ms\n`;
|
|
3130
|
+
report += `Long Frames (>16.67ms): ${metrics.frames.longTasks}\n`;
|
|
3131
|
+
report += `Dropped Frames (>33ms): ${metrics.frames.droppedFrames}\n`;
|
|
3132
|
+
report += `Avg Main Thread Work: ${metrics.mainThreadWork.toFixed(2)}ms\n`;
|
|
3133
|
+
report += `Long Tasks (>50ms): ${metrics.longTaskCount}\n`;
|
|
3134
|
+
if (metrics.memory) {
|
|
3135
|
+
const usedMB = (metrics.memory.usedJSHeapSize / 1024 / 1024).toFixed(2);
|
|
3136
|
+
const limitMB = (metrics.memory.jsHeapSizeLimit / 1024 / 1024).toFixed(2);
|
|
3137
|
+
report += `Memory: ${usedMB}MB / ${limitMB}MB\n`;
|
|
3138
|
+
}
|
|
3139
|
+
report += `Status: ${this.isPerformanceGood() ? '✓ GOOD' : '✗ POOR'}\n`;
|
|
3140
|
+
return report;
|
|
3141
|
+
}
|
|
3142
|
+
/**
|
|
3143
|
+
* Reset all metrics
|
|
3144
|
+
*/
|
|
3145
|
+
reset() {
|
|
3146
|
+
this.frameTimes = [];
|
|
3147
|
+
this.workTimes = [];
|
|
3148
|
+
this.longTasks = 0;
|
|
3149
|
+
}
|
|
3150
|
+
/**
|
|
3151
|
+
* Cleanup
|
|
3152
|
+
*/
|
|
3153
|
+
destroy() {
|
|
3154
|
+
this.stop();
|
|
3155
|
+
if (this.observer) {
|
|
3156
|
+
this.observer.disconnect();
|
|
3157
|
+
this.observer = null;
|
|
3158
|
+
}
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
/**
|
|
3162
|
+
* Singleton instance
|
|
3163
|
+
*/
|
|
3164
|
+
let telemetry = null;
|
|
3165
|
+
function getTelemetry() {
|
|
3166
|
+
if (!telemetry) {
|
|
3167
|
+
telemetry = new PerformanceTelemetry();
|
|
3168
|
+
}
|
|
3169
|
+
return telemetry;
|
|
3170
|
+
}
|
|
3171
|
+
/**
|
|
3172
|
+
* Helper to measure async function execution
|
|
3173
|
+
*/
|
|
3174
|
+
async function measureAsync(label, fn) {
|
|
3175
|
+
const start = performance.now();
|
|
3176
|
+
const result = await fn();
|
|
3177
|
+
const duration = performance.now() - start;
|
|
3178
|
+
console.log(`[Perf] ${label}: ${duration.toFixed(2)}ms`);
|
|
3179
|
+
return { result, duration };
|
|
3180
|
+
}
|
|
3181
|
+
/**
|
|
3182
|
+
* Helper to measure sync function execution
|
|
3183
|
+
*/
|
|
3184
|
+
function measureSync(label, fn) {
|
|
3185
|
+
const start = performance.now();
|
|
3186
|
+
const result = fn();
|
|
3187
|
+
const duration = performance.now() - start;
|
|
3188
|
+
console.log(`[Perf] ${label}: ${duration.toFixed(2)}ms`);
|
|
3189
|
+
return { result, duration };
|
|
3190
|
+
}
|
|
3191
|
+
|
|
3192
|
+
/**
|
|
3193
|
+
* Security utilities for CSP-compliant environments.
|
|
3194
|
+
* All utilities are opt-in and do not run by default.
|
|
3195
|
+
*/
|
|
3196
|
+
/**
|
|
3197
|
+
* No-op sanitizer - DOES NOT sanitize HTML.
|
|
3198
|
+
* Used by default to avoid forcing a dependency on sanitizer libraries.
|
|
3199
|
+
* Developers using untrusted data MUST provide a real sanitizer.
|
|
3200
|
+
*/
|
|
3201
|
+
class NoOpSanitizer {
|
|
3202
|
+
sanitize(html) {
|
|
3203
|
+
return html;
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
/**
|
|
3207
|
+
* Registry for custom sanitizer implementation.
|
|
3208
|
+
* Defaults to no-op - developers must explicitly set a sanitizer.
|
|
3209
|
+
*/
|
|
3210
|
+
let globalSanitizer = new NoOpSanitizer();
|
|
3211
|
+
/**
|
|
3212
|
+
* Set a custom HTML sanitizer (e.g., DOMPurify).
|
|
3213
|
+
*
|
|
3214
|
+
* @example
|
|
3215
|
+
* import DOMPurify from 'dompurify';
|
|
3216
|
+
* setHTMLSanitizer({
|
|
3217
|
+
* sanitize: (html) => DOMPurify.sanitize(html)
|
|
3218
|
+
* });
|
|
3219
|
+
*/
|
|
3220
|
+
function setHTMLSanitizer(sanitizer) {
|
|
3221
|
+
globalSanitizer = sanitizer;
|
|
3222
|
+
}
|
|
3223
|
+
/**
|
|
3224
|
+
* Get the current HTML sanitizer.
|
|
3225
|
+
*/
|
|
3226
|
+
function getHTMLSanitizer() {
|
|
3227
|
+
return globalSanitizer;
|
|
3228
|
+
}
|
|
3229
|
+
/**
|
|
3230
|
+
* Sanitize HTML using the registered sanitizer.
|
|
3231
|
+
* WARNING: Default is no-op! Set a real sanitizer for untrusted content.
|
|
3232
|
+
*
|
|
3233
|
+
* @param html - HTML string to sanitize
|
|
3234
|
+
* @returns Sanitized HTML string
|
|
3235
|
+
*/
|
|
3236
|
+
function sanitizeHTML(html) {
|
|
3237
|
+
return globalSanitizer.sanitize(html);
|
|
3238
|
+
}
|
|
3239
|
+
/**
|
|
3240
|
+
* Create a text node (CSP-safe alternative to innerHTML for text content).
|
|
3241
|
+
*
|
|
3242
|
+
* @param text - Text content
|
|
3243
|
+
* @returns Text node
|
|
3244
|
+
*/
|
|
3245
|
+
function createTextNode(text) {
|
|
3246
|
+
return document.createTextNode(text);
|
|
3247
|
+
}
|
|
3248
|
+
/**
|
|
3249
|
+
* Set text content safely (CSP-compliant).
|
|
3250
|
+
*
|
|
3251
|
+
* @param element - Target element
|
|
3252
|
+
* @param text - Text content
|
|
3253
|
+
*/
|
|
3254
|
+
function setTextContent(element, text) {
|
|
3255
|
+
element.textContent = text;
|
|
3256
|
+
}
|
|
3257
|
+
/**
|
|
3258
|
+
* Create an element with attributes (CSP-safe).
|
|
3259
|
+
* No inline styles or event handlers.
|
|
3260
|
+
*
|
|
3261
|
+
* @param tagName - HTML tag name
|
|
3262
|
+
* @param attributes - Element attributes (excluding style and on* handlers)
|
|
3263
|
+
* @param textContent - Optional text content
|
|
3264
|
+
* @returns Created element
|
|
3265
|
+
*/
|
|
3266
|
+
function createElement(tagName, attributes, textContent) {
|
|
3267
|
+
const el = document.createElement(tagName);
|
|
3268
|
+
if (attributes) {
|
|
3269
|
+
for (const [key, value] of Object.entries(attributes)) {
|
|
3270
|
+
// Block dangerous attributes
|
|
3271
|
+
if (key.startsWith('on') || key === 'style') {
|
|
3272
|
+
console.warn(`[NativeSelect Security] Blocked attribute: ${key}`);
|
|
3273
|
+
continue;
|
|
3274
|
+
}
|
|
3275
|
+
el.setAttribute(key, value);
|
|
3276
|
+
}
|
|
3277
|
+
}
|
|
3278
|
+
if (textContent !== undefined) {
|
|
3279
|
+
el.textContent = textContent;
|
|
3280
|
+
}
|
|
3281
|
+
return el;
|
|
3282
|
+
}
|
|
3283
|
+
/**
|
|
3284
|
+
* Set CSS custom properties (CSP-safe way to style elements).
|
|
3285
|
+
*
|
|
3286
|
+
* @param element - Target element
|
|
3287
|
+
* @param properties - CSS custom properties (e.g., { '--color': 'red' })
|
|
3288
|
+
*/
|
|
3289
|
+
function setCSSProperties(element, properties) {
|
|
3290
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
3291
|
+
if (!key.startsWith('--')) {
|
|
3292
|
+
console.warn(`[NativeSelect Security] CSS property must start with --: ${key}`);
|
|
3293
|
+
continue;
|
|
3294
|
+
}
|
|
3295
|
+
element.style.setProperty(key, value);
|
|
3296
|
+
}
|
|
3297
|
+
}
|
|
3298
|
+
/**
|
|
3299
|
+
* Feature detection utilities for CSP-restricted environments.
|
|
3300
|
+
*/
|
|
3301
|
+
const CSPFeatures = {
|
|
3302
|
+
/**
|
|
3303
|
+
* Check if inline scripts are allowed (CSP: script-src 'unsafe-inline').
|
|
3304
|
+
*/
|
|
3305
|
+
hasInlineScripts() {
|
|
3306
|
+
try {
|
|
3307
|
+
// Try to create a function (blocked by CSP: script-src without 'unsafe-eval')
|
|
3308
|
+
new Function('return true');
|
|
3309
|
+
return true;
|
|
3310
|
+
}
|
|
3311
|
+
catch {
|
|
3312
|
+
return false;
|
|
3313
|
+
}
|
|
3314
|
+
},
|
|
3315
|
+
/**
|
|
3316
|
+
* Check if eval is allowed (CSP: script-src 'unsafe-eval').
|
|
3317
|
+
*/
|
|
3318
|
+
hasEval() {
|
|
3319
|
+
try {
|
|
3320
|
+
eval('true');
|
|
3321
|
+
return true;
|
|
3322
|
+
}
|
|
3323
|
+
catch {
|
|
3324
|
+
return false;
|
|
3325
|
+
}
|
|
3326
|
+
},
|
|
3327
|
+
/**
|
|
3328
|
+
* Check if SharedArrayBuffer is available.
|
|
3329
|
+
* Required for some Worker features, blocked without COOP/COEP headers.
|
|
3330
|
+
*/
|
|
3331
|
+
hasSharedArrayBuffer() {
|
|
3332
|
+
return typeof SharedArrayBuffer !== 'undefined';
|
|
3333
|
+
},
|
|
3334
|
+
/**
|
|
3335
|
+
* Check if Web Workers are available and functional.
|
|
3336
|
+
*/
|
|
3337
|
+
hasWorkers() {
|
|
3338
|
+
try {
|
|
3339
|
+
return typeof Worker !== 'undefined';
|
|
3340
|
+
}
|
|
3341
|
+
catch {
|
|
3342
|
+
return false;
|
|
3343
|
+
}
|
|
3344
|
+
},
|
|
3345
|
+
/**
|
|
3346
|
+
* Check if we're in a sandboxed iframe.
|
|
3347
|
+
*/
|
|
3348
|
+
isSandboxed() {
|
|
3349
|
+
try {
|
|
3350
|
+
// Sandboxed iframes may not have access to parent
|
|
3351
|
+
return window !== window.parent && !window.parent;
|
|
3352
|
+
}
|
|
3353
|
+
catch {
|
|
3354
|
+
return true;
|
|
3355
|
+
}
|
|
3356
|
+
},
|
|
3357
|
+
/**
|
|
3358
|
+
* Check CSP policy via meta tag or SecurityPolicyViolationEvent.
|
|
3359
|
+
*/
|
|
3360
|
+
getCSPDirectives() {
|
|
3361
|
+
const metaTags = document.querySelectorAll('meta[http-equiv="Content-Security-Policy"]');
|
|
3362
|
+
const directives = [];
|
|
3363
|
+
metaTags.forEach((meta) => {
|
|
3364
|
+
const content = meta.getAttribute('content');
|
|
3365
|
+
if (content) {
|
|
3366
|
+
directives.push(content);
|
|
3367
|
+
}
|
|
3368
|
+
});
|
|
3369
|
+
return directives;
|
|
3370
|
+
}
|
|
3371
|
+
};
|
|
3372
|
+
/**
|
|
3373
|
+
* Detect environment capabilities once at initialization.
|
|
3374
|
+
*/
|
|
3375
|
+
function detectEnvironment() {
|
|
3376
|
+
return {
|
|
3377
|
+
canUseWorkers: CSPFeatures.hasWorkers(),
|
|
3378
|
+
canUseSharedArrayBuffer: CSPFeatures.hasSharedArrayBuffer(),
|
|
3379
|
+
canUseInlineScripts: CSPFeatures.hasInlineScripts(),
|
|
3380
|
+
canUseEval: CSPFeatures.hasEval(),
|
|
3381
|
+
isSandboxed: CSPFeatures.isSandboxed(),
|
|
3382
|
+
cspDirectives: CSPFeatures.getCSPDirectives()
|
|
3383
|
+
};
|
|
3384
|
+
}
|
|
3385
|
+
/**
|
|
3386
|
+
* Validation for untrusted HTML in templates.
|
|
3387
|
+
* Logs warning if no sanitizer is configured.
|
|
3388
|
+
*/
|
|
3389
|
+
function validateTemplate(html, source) {
|
|
3390
|
+
if (globalSanitizer instanceof NoOpSanitizer) {
|
|
3391
|
+
console.warn(`[NativeSelect Security] Template from "${source}" contains HTML but no sanitizer is configured. ` +
|
|
3392
|
+
`This may be unsafe if the content is untrusted. ` +
|
|
3393
|
+
`Call setHTMLSanitizer() with a sanitizer like DOMPurify.`);
|
|
3394
|
+
}
|
|
3395
|
+
return sanitizeHTML(html);
|
|
3396
|
+
}
|
|
3397
|
+
/**
|
|
3398
|
+
* Check if a string looks like it might contain executable code.
|
|
3399
|
+
*/
|
|
3400
|
+
function containsSuspiciousPatterns(str) {
|
|
3401
|
+
const patterns = [
|
|
3402
|
+
/<script/i,
|
|
3403
|
+
/javascript:/i,
|
|
3404
|
+
/on\w+\s*=/i, // event handlers like onclick=
|
|
3405
|
+
/eval\s*\(/i,
|
|
3406
|
+
/Function\s*\(/i,
|
|
3407
|
+
/setTimeout\s*\(/i,
|
|
3408
|
+
/setInterval\s*\(/i
|
|
3409
|
+
];
|
|
3410
|
+
return patterns.some(pattern => pattern.test(str));
|
|
3411
|
+
}
|
|
3412
|
+
/**
|
|
3413
|
+
* Escape HTML entities for text content.
|
|
3414
|
+
*/
|
|
3415
|
+
function escapeHTML(text) {
|
|
3416
|
+
const div = document.createElement('div');
|
|
3417
|
+
div.textContent = text;
|
|
3418
|
+
return div.innerHTML;
|
|
3419
|
+
}
|
|
3420
|
+
/**
|
|
3421
|
+
* Escape attribute values.
|
|
3422
|
+
*/
|
|
3423
|
+
function escapeAttribute(attr) {
|
|
3424
|
+
return attr
|
|
3425
|
+
.replace(/&/g, '&')
|
|
3426
|
+
.replace(/"/g, '"')
|
|
3427
|
+
.replace(/'/g, ''')
|
|
3428
|
+
.replace(/</g, '<')
|
|
3429
|
+
.replace(/>/g, '>');
|
|
3430
|
+
}
|
|
3431
|
+
|
|
3432
|
+
/**
|
|
3433
|
+
* CSP-compliant styling utilities.
|
|
3434
|
+
* No inline styles or runtime style tag injection.
|
|
3435
|
+
* Uses CSS custom properties and class names only.
|
|
3436
|
+
*/
|
|
3437
|
+
/**
|
|
3438
|
+
* Apply CSS classes to an element (CSP-safe).
|
|
3439
|
+
*/
|
|
3440
|
+
function applyClasses(element, classes) {
|
|
3441
|
+
element.className = classes.filter(Boolean).join(' ');
|
|
3442
|
+
}
|
|
3443
|
+
/**
|
|
3444
|
+
* Toggle a CSS class (CSP-safe).
|
|
3445
|
+
*/
|
|
3446
|
+
function toggleClass(element, className, force) {
|
|
3447
|
+
element.classList.toggle(className, force);
|
|
3448
|
+
}
|
|
3449
|
+
/**
|
|
3450
|
+
* Set CSS custom properties (CSP-safe dynamic styling).
|
|
3451
|
+
* All properties must start with -- (CSS variables).
|
|
3452
|
+
*
|
|
3453
|
+
* @example
|
|
3454
|
+
* setCustomProperties(element, {
|
|
3455
|
+
* '--item-height': '48px',
|
|
3456
|
+
* '--selected-bg': '#0066cc'
|
|
3457
|
+
* });
|
|
3458
|
+
*/
|
|
3459
|
+
function setCustomProperties(element, properties) {
|
|
3460
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
3461
|
+
if (!key.startsWith('--')) {
|
|
3462
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
3463
|
+
console.warn(`[NativeSelect CSP] Custom property must start with --: "${key}". Skipping.`);
|
|
3464
|
+
}
|
|
3465
|
+
continue;
|
|
3466
|
+
}
|
|
3467
|
+
element.style.setProperty(key, value);
|
|
3468
|
+
}
|
|
3469
|
+
}
|
|
3470
|
+
/**
|
|
3471
|
+
* Get a CSS custom property value.
|
|
3472
|
+
*/
|
|
3473
|
+
function getCustomProperty(element, propertyName) {
|
|
3474
|
+
return getComputedStyle(element).getPropertyValue(propertyName).trim();
|
|
3475
|
+
}
|
|
3476
|
+
/**
|
|
3477
|
+
* Remove a CSS custom property.
|
|
3478
|
+
*/
|
|
3479
|
+
function removeCustomProperty(element, propertyName) {
|
|
3480
|
+
element.style.removeProperty(propertyName);
|
|
3481
|
+
}
|
|
3482
|
+
/**
|
|
3483
|
+
* Predefined theme as CSS custom properties.
|
|
3484
|
+
* Can be overridden by consumers via CSS.
|
|
3485
|
+
*/
|
|
3486
|
+
const defaultTheme = {
|
|
3487
|
+
'--ns-item-height': '40px',
|
|
3488
|
+
'--ns-item-padding': '8px 12px',
|
|
3489
|
+
'--ns-item-bg': 'transparent',
|
|
3490
|
+
'--ns-item-hover-bg': 'rgba(0, 0, 0, 0.05)',
|
|
3491
|
+
'--ns-item-selected-bg': 'rgba(0, 102, 204, 0.1)',
|
|
3492
|
+
'--ns-item-active-bg': 'rgba(0, 102, 204, 0.2)',
|
|
3493
|
+
'--ns-item-color': 'inherit',
|
|
3494
|
+
'--ns-item-selected-color': '#0066cc',
|
|
3495
|
+
'--ns-border-color': '#ccc',
|
|
3496
|
+
'--ns-border-radius': '4px',
|
|
3497
|
+
'--ns-focus-outline': '2px solid #0066cc',
|
|
3498
|
+
'--ns-font-size': '14px',
|
|
3499
|
+
'--ns-font-family': 'system-ui, -apple-system, sans-serif'
|
|
3500
|
+
};
|
|
3501
|
+
/**
|
|
3502
|
+
* Apply the default theme to an element.
|
|
3503
|
+
*/
|
|
3504
|
+
function applyDefaultTheme(element) {
|
|
3505
|
+
setCustomProperties(element, defaultTheme);
|
|
3506
|
+
}
|
|
3507
|
+
/**
|
|
3508
|
+
* Static CSS for shadow DOM (no runtime injection).
|
|
3509
|
+
* This CSS is CSP-safe because it's defined at build time as a string constant,
|
|
3510
|
+
* not generated at runtime.
|
|
3511
|
+
*/
|
|
3512
|
+
const shadowDOMStyles = `
|
|
3513
|
+
:host {
|
|
3514
|
+
display: block;
|
|
3515
|
+
box-sizing: border-box;
|
|
3516
|
+
font-family: var(--ns-font-family, system-ui, -apple-system, sans-serif);
|
|
3517
|
+
font-size: var(--ns-font-size, 14px);
|
|
3518
|
+
}
|
|
3519
|
+
|
|
3520
|
+
* {
|
|
3521
|
+
box-sizing: border-box;
|
|
3522
|
+
}
|
|
3523
|
+
|
|
3524
|
+
.ns-list {
|
|
3525
|
+
position: relative;
|
|
3526
|
+
overflow: auto;
|
|
3527
|
+
max-height: var(--ns-max-height, 300px);
|
|
3528
|
+
border: 1px solid var(--ns-border-color, #ccc);
|
|
3529
|
+
border-radius: var(--ns-border-radius, 4px);
|
|
3530
|
+
background: var(--ns-bg, white);
|
|
3531
|
+
outline: none;
|
|
3532
|
+
}
|
|
3533
|
+
|
|
3534
|
+
.ns-list:focus {
|
|
3535
|
+
outline: var(--ns-focus-outline, 2px solid #0066cc);
|
|
3536
|
+
outline-offset: -2px;
|
|
3537
|
+
}
|
|
3538
|
+
|
|
3539
|
+
.ns-item {
|
|
3540
|
+
padding: var(--ns-item-padding, 8px 12px);
|
|
3541
|
+
min-height: var(--ns-item-height, 40px);
|
|
3542
|
+
color: var(--ns-item-color, inherit);
|
|
3543
|
+
background: var(--ns-item-bg, transparent);
|
|
3544
|
+
cursor: pointer;
|
|
3545
|
+
user-select: none;
|
|
3546
|
+
display: flex;
|
|
3547
|
+
align-items: center;
|
|
3548
|
+
transition: background-color 0.15s ease;
|
|
3549
|
+
}
|
|
3550
|
+
|
|
3551
|
+
.ns-item:hover {
|
|
3552
|
+
background: var(--ns-item-hover-bg, rgba(0, 0, 0, 0.05));
|
|
3553
|
+
}
|
|
3554
|
+
|
|
3555
|
+
.ns-item[aria-selected="true"] {
|
|
3556
|
+
background: var(--ns-item-selected-bg, rgba(0, 102, 204, 0.1));
|
|
3557
|
+
color: var(--ns-item-selected-color, #0066cc);
|
|
3558
|
+
}
|
|
3559
|
+
|
|
3560
|
+
.ns-item[data-active="true"] {
|
|
3561
|
+
background: var(--ns-item-active-bg, rgba(0, 102, 204, 0.2));
|
|
3562
|
+
}
|
|
3563
|
+
|
|
3564
|
+
.ns-item[aria-disabled="true"] {
|
|
3565
|
+
opacity: 0.5;
|
|
3566
|
+
cursor: not-allowed;
|
|
3567
|
+
}
|
|
3568
|
+
|
|
3569
|
+
.ns-viewport {
|
|
3570
|
+
position: relative;
|
|
3571
|
+
overflow: auto;
|
|
3572
|
+
-webkit-overflow-scrolling: touch;
|
|
3573
|
+
}
|
|
3574
|
+
|
|
3575
|
+
.ns-spacer {
|
|
3576
|
+
position: absolute;
|
|
3577
|
+
top: 0;
|
|
3578
|
+
left: 0;
|
|
3579
|
+
width: 1px;
|
|
3580
|
+
pointer-events: none;
|
|
3581
|
+
}
|
|
3582
|
+
|
|
3583
|
+
/* Screen reader only */
|
|
3584
|
+
.ns-sr-only {
|
|
3585
|
+
position: absolute;
|
|
3586
|
+
left: -9999px;
|
|
3587
|
+
width: 1px;
|
|
3588
|
+
height: 1px;
|
|
3589
|
+
overflow: hidden;
|
|
3590
|
+
clip: rect(0, 0, 0, 0);
|
|
3591
|
+
white-space: nowrap;
|
|
3592
|
+
}
|
|
3593
|
+
|
|
3594
|
+
/* Portal mode (teleported outside shadow DOM) */
|
|
3595
|
+
.ns-portal {
|
|
3596
|
+
position: fixed;
|
|
3597
|
+
z-index: var(--ns-portal-z-index, 9999);
|
|
3598
|
+
}
|
|
3599
|
+
|
|
3600
|
+
/* CSP warning banner (only shown in development) */
|
|
3601
|
+
.ns-csp-warning {
|
|
3602
|
+
padding: 8px;
|
|
3603
|
+
background: #fff3cd;
|
|
3604
|
+
border: 1px solid #ffc107;
|
|
3605
|
+
border-radius: 4px;
|
|
3606
|
+
color: #856404;
|
|
3607
|
+
font-size: 12px;
|
|
3608
|
+
margin-bottom: 8px;
|
|
3609
|
+
}
|
|
3610
|
+
`;
|
|
3611
|
+
/**
|
|
3612
|
+
* Inject static styles into shadow root (CSP-safe).
|
|
3613
|
+
* Only called once during component initialization.
|
|
3614
|
+
*/
|
|
3615
|
+
function injectShadowStyles(shadowRoot) {
|
|
3616
|
+
const style = document.createElement('style');
|
|
3617
|
+
style.textContent = shadowDOMStyles;
|
|
3618
|
+
shadowRoot.appendChild(style);
|
|
3619
|
+
}
|
|
3620
|
+
/**
|
|
3621
|
+
* Check if element has overflow:hidden ancestors (portal fallback detection).
|
|
3622
|
+
*/
|
|
3623
|
+
function hasOverflowHiddenAncestor(element) {
|
|
3624
|
+
let current = element.parentElement;
|
|
3625
|
+
while (current && current !== document.body) {
|
|
3626
|
+
const overflow = getComputedStyle(current).overflow;
|
|
3627
|
+
if (overflow === 'hidden' || overflow === 'clip') {
|
|
3628
|
+
return true;
|
|
3629
|
+
}
|
|
3630
|
+
current = current.parentElement;
|
|
3631
|
+
}
|
|
3632
|
+
return false;
|
|
3633
|
+
}
|
|
3634
|
+
/**
|
|
3635
|
+
* Runtime warning for CSP violations (development only).
|
|
3636
|
+
*/
|
|
3637
|
+
function warnCSPViolation(feature, fallback) {
|
|
3638
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
3639
|
+
console.warn(`[NativeSelect CSP] Feature "${feature}" blocked by Content Security Policy. ` +
|
|
3640
|
+
`Falling back to "${fallback}".`);
|
|
3641
|
+
}
|
|
3642
|
+
}
|
|
3643
|
+
|
|
3644
|
+
exports.CSPFeatures = CSPFeatures;
|
|
3645
|
+
exports.DOMPool = DOMPool;
|
|
3646
|
+
exports.EnhancedSelect = EnhancedSelect;
|
|
3647
|
+
exports.FenwickTree = FenwickTree;
|
|
3648
|
+
exports.NativeSelectElement = NativeSelectElement;
|
|
3649
|
+
exports.PerformanceTelemetry = PerformanceTelemetry;
|
|
3650
|
+
exports.SelectOption = SelectOption;
|
|
3651
|
+
exports.Virtualizer = Virtualizer;
|
|
3652
|
+
exports.WorkerManager = WorkerManager;
|
|
3653
|
+
exports.applyClasses = applyClasses;
|
|
3654
|
+
exports.applyDefaultTheme = applyDefaultTheme;
|
|
3655
|
+
exports.configureSelect = configureSelect;
|
|
3656
|
+
exports.containsSuspiciousPatterns = containsSuspiciousPatterns;
|
|
3657
|
+
exports.createElement = createElement;
|
|
3658
|
+
exports.createRendererHelpers = createRendererHelpers;
|
|
3659
|
+
exports.createTextNode = createTextNode;
|
|
3660
|
+
exports.defaultTheme = defaultTheme;
|
|
3661
|
+
exports.detectEnvironment = detectEnvironment;
|
|
3662
|
+
exports.escapeAttribute = escapeAttribute;
|
|
3663
|
+
exports.escapeHTML = escapeHTML;
|
|
3664
|
+
exports.getCustomProperty = getCustomProperty;
|
|
3665
|
+
exports.getHTMLSanitizer = getHTMLSanitizer;
|
|
3666
|
+
exports.getTelemetry = getTelemetry;
|
|
3667
|
+
exports.getWorkerManager = getWorkerManager;
|
|
3668
|
+
exports.hasOverflowHiddenAncestor = hasOverflowHiddenAncestor;
|
|
3669
|
+
exports.injectShadowStyles = injectShadowStyles;
|
|
3670
|
+
exports.measureAsync = measureAsync;
|
|
3671
|
+
exports.measureSync = measureSync;
|
|
3672
|
+
exports.removeCustomProperty = removeCustomProperty;
|
|
3673
|
+
exports.renderTemplate = renderTemplate;
|
|
3674
|
+
exports.resetSelectConfig = resetSelectConfig;
|
|
3675
|
+
exports.sanitizeHTML = sanitizeHTML;
|
|
3676
|
+
exports.selectConfig = selectConfig;
|
|
3677
|
+
exports.setCSSProperties = setCSSProperties;
|
|
3678
|
+
exports.setCustomProperties = setCustomProperties;
|
|
3679
|
+
exports.setHTMLSanitizer = setHTMLSanitizer;
|
|
3680
|
+
exports.setTextContent = setTextContent;
|
|
3681
|
+
exports.shadowDOMStyles = shadowDOMStyles;
|
|
3682
|
+
exports.toggleClass = toggleClass;
|
|
3683
|
+
exports.validateTemplate = validateTemplate;
|
|
3684
|
+
exports.warnCSPViolation = warnCSPViolation;
|
|
3685
|
+
//# sourceMappingURL=index.cjs.map
|