slim-select 2.13.0 → 3.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.
Files changed (55) hide show
  1. package/README.md +184 -43
  2. package/dist/index.d.ts +304 -52
  3. package/dist/react/index.d.ts +304 -0
  4. package/dist/react/index.js +1549 -0
  5. package/dist/react/index.umd.js +22 -0
  6. package/dist/slimselect.cjs.js +1 -2030
  7. package/dist/slimselect.css +1 -1
  8. package/dist/slimselect.css.map +1 -1
  9. package/dist/slimselect.es.js +1224 -2012
  10. package/dist/slimselect.umd.js +1 -2036
  11. package/dist/vue/index.d.ts +304 -0
  12. package/dist/vue/index.js +1341 -0
  13. package/dist/vue/index.umd.js +1 -0
  14. package/index.html +1 -0
  15. package/package.json +59 -37
  16. package/release.js +0 -7
  17. package/src/slim-select/accessibility.test.ts +782 -0
  18. package/src/slim-select/classes.test.ts +7 -6
  19. package/src/slim-select/classes.ts +8 -6
  20. package/src/slim-select/helpers.test.ts +10 -8
  21. package/src/slim-select/index.test.ts +509 -14
  22. package/src/slim-select/index.ts +58 -31
  23. package/src/slim-select/react/index.tsx +9 -0
  24. package/src/slim-select/react/react.tsx +112 -0
  25. package/src/slim-select/render.test.ts +479 -119
  26. package/src/slim-select/render.ts +213 -91
  27. package/src/slim-select/select.test.ts +28 -10
  28. package/src/slim-select/select.ts +35 -12
  29. package/src/slim-select/settings.test.ts +19 -8
  30. package/src/slim-select/settings.ts +2 -4
  31. package/src/slim-select/slimselect.scss +30 -15
  32. package/src/slim-select/store.test.ts +13 -16
  33. package/src/slim-select/store.ts +97 -86
  34. package/src/slim-select/vue/index.ts +9 -0
  35. package/src/slim-select/vue/vue.test.ts +832 -0
  36. package/src/slim-select/{vue.vue → vue/vue.vue} +45 -23
  37. package/tsconfig.json +6 -4
  38. package/{vite.config.mts → vite.config.docs.mts} +10 -0
  39. package/vite.config.lib.mts +42 -0
  40. package/vite.config.react.mts +40 -0
  41. package/vite.config.vue.mts +39 -0
  42. package/vitest.config.ts +29 -0
  43. package/dist/classes.d.ts +0 -43
  44. package/dist/helpers.d.ts +0 -5
  45. package/dist/render.d.ts +0 -81
  46. package/dist/select.d.ts +0 -30
  47. package/dist/settings.d.ts +0 -35
  48. package/dist/slimselect.global.js +0 -2033
  49. package/dist/slimselect.js +0 -2036
  50. package/dist/slimselect.min.js +0 -1
  51. package/dist/slimselect.umd.min.js +0 -1
  52. package/dist/store.d.ts +0 -84
  53. package/jest.config.js +0 -14
  54. package/src/slim-select/rollup.config.mjs +0 -127
  55. package/src/slim-select/tsconfig.json +0 -14
@@ -1,2030 +1 @@
1
- 'use strict';
2
-
3
- class CssClasses {
4
- constructor(classes) {
5
- if (!classes) {
6
- classes = {};
7
- }
8
- this.main = classes.main || 'ss-main';
9
- this.placeholder = classes.placeholder || 'ss-placeholder';
10
- this.values = classes.values || 'ss-values';
11
- this.single = classes.single || 'ss-single';
12
- this.max = classes.max || 'ss-max';
13
- this.value = classes.value || 'ss-value';
14
- this.valueText = classes.valueText || 'ss-value-text';
15
- this.valueDelete = classes.valueDelete || 'ss-value-delete';
16
- this.valueOut = classes.valueOut || 'ss-value-out';
17
- this.deselect = classes.deselect || 'ss-deselect';
18
- this.deselectPath = classes.deselectPath || 'M10,10 L90,90 M10,90 L90,10';
19
- this.arrow = classes.arrow || 'ss-arrow';
20
- this.arrowClose = classes.arrowClose || 'M10,30 L50,70 L90,30';
21
- this.arrowOpen = classes.arrowOpen || 'M10,70 L50,30 L90,70';
22
- this.content = classes.content || 'ss-content';
23
- this.openAbove = classes.openAbove || 'ss-open-above';
24
- this.openBelow = classes.openBelow || 'ss-open-below';
25
- this.search = classes.search || 'ss-search';
26
- this.searchHighlighter = classes.searchHighlighter || 'ss-search-highlight';
27
- this.searching = classes.searching || 'ss-searching';
28
- this.addable = classes.addable || 'ss-addable';
29
- this.addablePath = classes.addablePath || 'M50,10 L50,90 M10,50 L90,50';
30
- this.list = classes.list || 'ss-list';
31
- this.optgroup = classes.optgroup || 'ss-optgroup';
32
- this.optgroupLabel = classes.optgroupLabel || 'ss-optgroup-label';
33
- this.optgroupLabelText = classes.optgroupLabelText || 'ss-optgroup-label-text';
34
- this.optgroupActions = classes.optgroupActions || 'ss-optgroup-actions';
35
- this.optgroupSelectAll = classes.optgroupSelectAll || 'ss-selectall';
36
- this.optgroupSelectAllBox = classes.optgroupSelectAllBox || 'M60,10 L10,10 L10,90 L90,90 L90,50';
37
- this.optgroupSelectAllCheck = classes.optgroupSelectAllCheck || 'M30,45 L50,70 L90,10';
38
- this.optgroupClosable = classes.optgroupClosable || 'ss-closable';
39
- this.option = classes.option || 'ss-option';
40
- this.optionDelete = classes.optionDelete || 'M10,10 L90,90 M10,90 L90,10';
41
- this.highlighted = classes.highlighted || 'ss-highlighted';
42
- this.open = classes.open || 'ss-open';
43
- this.close = classes.close || 'ss-close';
44
- this.selected = classes.selected || 'ss-selected';
45
- this.error = classes.error || 'ss-error';
46
- this.disabled = classes.disabled || 'ss-disabled';
47
- this.hide = classes.hide || 'ss-hide';
48
- }
49
- }
50
-
51
- function generateID() {
52
- return Math.random().toString(36).substring(2, 10);
53
- }
54
- function hasClassInTree(element, className) {
55
- function hasClass(e, c) {
56
- if (c && e && e.classList && e.classList.contains(c)) {
57
- return e;
58
- }
59
- if (c && e && e.dataset && e.dataset.id && e.dataset.id === className) {
60
- return e;
61
- }
62
- return null;
63
- }
64
- function parentByClass(e, c) {
65
- if (!e || e === document) {
66
- return null;
67
- }
68
- else if (hasClass(e, c)) {
69
- return e;
70
- }
71
- else {
72
- return parentByClass(e.parentNode, c);
73
- }
74
- }
75
- return hasClass(element, className) || parentByClass(element, className);
76
- }
77
- function debounce(func, wait = 50, immediate = false) {
78
- let timeout;
79
- return function (...args) {
80
- const context = self;
81
- const later = () => {
82
- timeout = null;
83
- if (!immediate) {
84
- func.apply(context, args);
85
- }
86
- };
87
- const callNow = immediate && !timeout;
88
- clearTimeout(timeout);
89
- timeout = setTimeout(later, wait);
90
- if (callNow) {
91
- func.apply(context, args);
92
- }
93
- };
94
- }
95
- function isEqual(a, b) {
96
- return JSON.stringify(a) === JSON.stringify(b);
97
- }
98
- function kebabCase(str) {
99
- const result = str.replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g, (match) => '-' + match.toLowerCase());
100
- return str[0] === str[0].toUpperCase() ? result.substring(1) : result;
101
- }
102
-
103
- class Optgroup {
104
- constructor(optgroup) {
105
- this.id = !optgroup.id || optgroup.id === '' ? generateID() : optgroup.id;
106
- this.label = optgroup.label || '';
107
- this.selectAll = optgroup.selectAll === undefined ? false : optgroup.selectAll;
108
- this.selectAllText = optgroup.selectAllText || 'Select All';
109
- this.closable = optgroup.closable || 'off';
110
- this.options = [];
111
- if (optgroup.options) {
112
- for (const o of optgroup.options) {
113
- this.options.push(new Option(o));
114
- }
115
- }
116
- }
117
- }
118
- class Option {
119
- constructor(option) {
120
- this.id = !option.id || option.id === '' ? generateID() : option.id;
121
- this.value = option.value === undefined ? option.text : option.value;
122
- this.text = option.text || '';
123
- this.html = option.html || '';
124
- this.defaultSelected = option.defaultSelected !== undefined ? option.defaultSelected : false;
125
- this.selected = option.selected !== undefined ? option.selected : false;
126
- this.display = option.display !== undefined ? option.display : true;
127
- this.disabled = option.disabled !== undefined ? option.disabled : false;
128
- this.mandatory = option.mandatory !== undefined ? option.mandatory : false;
129
- this.placeholder = option.placeholder !== undefined ? option.placeholder : false;
130
- this.class = option.class || '';
131
- this.style = option.style || '';
132
- this.data = option.data || {};
133
- }
134
- }
135
- class Store {
136
- constructor(type, data) {
137
- this.selectType = 'single';
138
- this.data = [];
139
- this.selectedOrder = [];
140
- this.selectType = type;
141
- this.setData(data);
142
- }
143
- validateDataArray(data) {
144
- if (!Array.isArray(data)) {
145
- return new Error('Data must be an array');
146
- }
147
- for (let dataObj of data) {
148
- if (dataObj instanceof Optgroup || 'label' in dataObj) {
149
- if (!('label' in dataObj)) {
150
- return new Error('Optgroup must have a label');
151
- }
152
- if ('options' in dataObj && dataObj.options) {
153
- for (let option of dataObj.options) {
154
- const validationError = this.validateOption(option);
155
- if (validationError) {
156
- return validationError;
157
- }
158
- }
159
- }
160
- }
161
- else if (dataObj instanceof Option || 'text' in dataObj) {
162
- const validationError = this.validateOption(dataObj);
163
- if (validationError) {
164
- return validationError;
165
- }
166
- }
167
- else {
168
- return new Error('Data object must be a valid optgroup or option');
169
- }
170
- }
171
- return null;
172
- }
173
- validateOption(option) {
174
- if (!('text' in option)) {
175
- return new Error('Option must have a text');
176
- }
177
- return null;
178
- }
179
- partialToFullData(data) {
180
- let dataFinal = [];
181
- data.forEach((dataObj) => {
182
- if (dataObj instanceof Optgroup || 'label' in dataObj) {
183
- let optOptions = [];
184
- if ('options' in dataObj && dataObj.options) {
185
- dataObj.options.forEach((option) => {
186
- optOptions.push(new Option(option));
187
- });
188
- }
189
- if (optOptions.length > 0) {
190
- dataFinal.push(new Optgroup(dataObj));
191
- }
192
- }
193
- if (dataObj instanceof Option || 'text' in dataObj) {
194
- dataFinal.push(new Option(dataObj));
195
- }
196
- });
197
- return dataFinal;
198
- }
199
- setData(data) {
200
- this.data = this.partialToFullData(data);
201
- if (this.selectType === 'single') {
202
- this.setSelectedBy('id', this.getSelected());
203
- }
204
- }
205
- getData() {
206
- return this.filter(null, true);
207
- }
208
- getDataOptions() {
209
- return this.filter(null, false);
210
- }
211
- addOption(option, addToStart = false) {
212
- if (addToStart) {
213
- let data = [new Option(option)];
214
- this.setData(data.concat(this.getData()));
215
- }
216
- else {
217
- this.setData(this.getData().concat(new Option(option)));
218
- }
219
- }
220
- setSelectedBy(selectedType, selectedValues) {
221
- let firstOption = null;
222
- let hasSelected = false;
223
- const selectedObjects = [];
224
- for (let dataObj of this.data) {
225
- if (dataObj instanceof Optgroup) {
226
- for (let option of dataObj.options) {
227
- if (!firstOption) {
228
- firstOption = option;
229
- }
230
- option.selected = hasSelected ? false : selectedValues.includes(option[selectedType]);
231
- if (option.selected) {
232
- selectedObjects.push(option);
233
- if (this.selectType === 'single') {
234
- hasSelected = true;
235
- }
236
- }
237
- }
238
- }
239
- if (dataObj instanceof Option) {
240
- if (!firstOption) {
241
- firstOption = dataObj;
242
- }
243
- dataObj.selected = hasSelected ? false : selectedValues.includes(dataObj[selectedType]);
244
- if (dataObj.selected) {
245
- selectedObjects.push(dataObj);
246
- if (this.selectType === 'single') {
247
- hasSelected = true;
248
- }
249
- }
250
- }
251
- }
252
- if (this.selectType === 'single' && firstOption && !hasSelected) {
253
- firstOption.selected = true;
254
- selectedObjects.push(firstOption);
255
- }
256
- const selectedIds = selectedValues.map((value) => {
257
- var _a;
258
- return ((_a = selectedObjects.find((option) => option[selectedType] === value)) === null || _a === void 0 ? void 0 : _a.id) || '';
259
- });
260
- this.selectedOrder = selectedIds;
261
- }
262
- getSelected() {
263
- return this.getSelectedOptions().map((option) => option.id);
264
- }
265
- getSelectedValues() {
266
- return this.getSelectedOptions().map((option) => option.value);
267
- }
268
- getSelectedOptions() {
269
- return this.filter((opt) => {
270
- return opt.selected;
271
- }, false);
272
- }
273
- getOptgroupByID(id) {
274
- for (let dataObj of this.data) {
275
- if (dataObj instanceof Optgroup && dataObj.id === id) {
276
- return dataObj;
277
- }
278
- }
279
- return null;
280
- }
281
- getOptionByID(id) {
282
- let options = this.filter((opt) => {
283
- return opt.id === id;
284
- }, false);
285
- return options.length ? options[0] : null;
286
- }
287
- getSelectType() {
288
- return this.selectType;
289
- }
290
- getFirstOption() {
291
- let option = null;
292
- for (let dataObj of this.data) {
293
- if (dataObj instanceof Optgroup) {
294
- option = dataObj.options[0];
295
- }
296
- else if (dataObj instanceof Option) {
297
- option = dataObj;
298
- }
299
- if (option) {
300
- break;
301
- }
302
- }
303
- return option;
304
- }
305
- search(search, searchFilter) {
306
- search = search.trim();
307
- if (search === '') {
308
- return this.getData();
309
- }
310
- return this.filter((opt) => {
311
- return searchFilter(opt, search);
312
- }, true);
313
- }
314
- filter(filter, includeOptgroup) {
315
- const dataSearch = [];
316
- this.data.forEach((dataObj) => {
317
- if (dataObj instanceof Optgroup) {
318
- let optOptions = [];
319
- dataObj.options.forEach((option) => {
320
- if (!filter || filter(option)) {
321
- if (!includeOptgroup) {
322
- dataSearch.push(new Option(option));
323
- }
324
- else {
325
- optOptions.push(new Option(option));
326
- }
327
- }
328
- });
329
- if (optOptions.length > 0) {
330
- let optgroup = new Optgroup(dataObj);
331
- optgroup.options = optOptions;
332
- dataSearch.push(optgroup);
333
- }
334
- }
335
- if (dataObj instanceof Option) {
336
- if (!filter || filter(dataObj)) {
337
- dataSearch.push(new Option(dataObj));
338
- }
339
- }
340
- });
341
- return dataSearch;
342
- }
343
- selectedOrderOptions(options) {
344
- const newOrder = [];
345
- this.selectedOrder.forEach((id) => {
346
- const option = options.find((opt) => opt.id === id);
347
- if (option) {
348
- newOrder.push(option);
349
- }
350
- });
351
- options.forEach((option) => {
352
- let isIn = false;
353
- newOrder.forEach((selectedOption) => {
354
- if (option.id === selectedOption.id) {
355
- isIn = true;
356
- return;
357
- }
358
- });
359
- if (!isIn) {
360
- newOrder.push(option);
361
- }
362
- });
363
- return newOrder;
364
- }
365
- }
366
-
367
- class Render {
368
- constructor(settings, classes, store, callbacks) {
369
- this.store = store;
370
- this.settings = settings;
371
- this.classes = classes;
372
- this.callbacks = callbacks;
373
- this.lastSelectedOption = null;
374
- this.main = this.mainDiv();
375
- this.content = this.contentDiv();
376
- this.updateClassStyles();
377
- this.updateAriaAttributes();
378
- if (this.settings.contentLocation) {
379
- this.settings.contentLocation.appendChild(this.content.main);
380
- }
381
- }
382
- enable() {
383
- this.main.main.classList.remove(this.classes.disabled);
384
- this.content.search.input.disabled = false;
385
- }
386
- disable() {
387
- this.main.main.classList.add(this.classes.disabled);
388
- this.content.search.input.disabled = true;
389
- }
390
- open() {
391
- this.main.arrow.path.setAttribute('d', this.classes.arrowOpen);
392
- this.main.main.classList.add(this.settings.openPosition === 'up' ? this.classes.openAbove : this.classes.openBelow);
393
- this.main.main.setAttribute('aria-expanded', 'true');
394
- this.moveContent();
395
- const selectedOptions = this.store.getSelectedOptions();
396
- if (selectedOptions.length) {
397
- const selectedId = selectedOptions[selectedOptions.length - 1].id;
398
- const selectedOption = this.content.list.querySelector('[data-id="' + selectedId + '"]');
399
- if (selectedOption) {
400
- this.ensureElementInView(this.content.list, selectedOption);
401
- }
402
- }
403
- }
404
- close() {
405
- this.main.main.classList.remove(this.classes.openAbove);
406
- this.main.main.classList.remove(this.classes.openBelow);
407
- this.main.main.setAttribute('aria-expanded', 'false');
408
- this.content.main.classList.remove(this.classes.openAbove);
409
- this.content.main.classList.remove(this.classes.openBelow);
410
- this.main.arrow.path.setAttribute('d', this.classes.arrowClose);
411
- }
412
- updateClassStyles() {
413
- this.main.main.className = '';
414
- this.main.main.removeAttribute('style');
415
- this.content.main.className = '';
416
- this.content.main.removeAttribute('style');
417
- this.main.main.classList.add(this.classes.main);
418
- this.content.main.classList.add(this.classes.content);
419
- if (this.settings.style !== '') {
420
- this.main.main.style.cssText = this.settings.style;
421
- this.content.main.style.cssText = this.settings.style;
422
- }
423
- if (this.settings.class.length) {
424
- for (const c of this.settings.class) {
425
- if (c.trim() !== '') {
426
- this.main.main.classList.add(c.trim());
427
- this.content.main.classList.add(c.trim());
428
- }
429
- }
430
- }
431
- if (this.settings.contentPosition === 'relative' || this.settings.contentPosition === 'fixed') {
432
- this.content.main.classList.add('ss-' + this.settings.contentPosition);
433
- }
434
- }
435
- updateAriaAttributes() {
436
- var _a;
437
- this.main.main.role = 'combobox';
438
- this.main.main.setAttribute('aria-haspopup', 'listbox');
439
- this.main.main.setAttribute('aria-controls', (_a = this.content.main.dataset.id) !== null && _a !== void 0 ? _a : '');
440
- this.main.main.setAttribute('aria-expanded', 'false');
441
- this.content.main.setAttribute('role', 'listbox');
442
- }
443
- mainDiv() {
444
- var _a;
445
- const main = document.createElement('div');
446
- main.dataset.id = this.settings.id;
447
- main.setAttribute('aria-label', this.settings.ariaLabel);
448
- main.tabIndex = 0;
449
- main.onkeydown = (e) => {
450
- switch (e.key) {
451
- case 'ArrowUp':
452
- case 'ArrowDown':
453
- this.callbacks.open();
454
- e.key === 'ArrowDown' ? this.highlight('down') : this.highlight('up');
455
- return false;
456
- case 'Tab':
457
- this.callbacks.close();
458
- return true;
459
- case 'Enter':
460
- case ' ':
461
- this.callbacks.open();
462
- const highlighted = this.content.list.querySelector('.' + this.classes.highlighted);
463
- if (highlighted) {
464
- highlighted.click();
465
- }
466
- return false;
467
- case 'Escape':
468
- this.callbacks.close();
469
- return false;
470
- }
471
- if (e.key.length === 1) {
472
- this.callbacks.open();
473
- }
474
- return true;
475
- };
476
- main.onclick = (e) => {
477
- if (this.settings.disabled) {
478
- return;
479
- }
480
- this.settings.isOpen ? this.callbacks.close() : this.callbacks.open();
481
- };
482
- const values = document.createElement('div');
483
- values.classList.add(this.classes.values);
484
- main.appendChild(values);
485
- const deselect = document.createElement('div');
486
- deselect.classList.add(this.classes.deselect);
487
- const selectedOptions = (_a = this.store) === null || _a === void 0 ? void 0 : _a.getSelectedOptions();
488
- if (!this.settings.allowDeselect || (this.settings.isMultiple && selectedOptions && selectedOptions.length <= 0)) {
489
- deselect.classList.add(this.classes.hide);
490
- }
491
- else {
492
- deselect.classList.remove(this.classes.hide);
493
- }
494
- deselect.onclick = (e) => {
495
- e.stopPropagation();
496
- if (this.settings.disabled) {
497
- return;
498
- }
499
- let shouldDelete = true;
500
- const before = this.store.getSelectedOptions();
501
- const after = [];
502
- if (this.callbacks.beforeChange) {
503
- shouldDelete = this.callbacks.beforeChange(after, before) === true;
504
- }
505
- if (shouldDelete) {
506
- if (this.settings.isMultiple) {
507
- this.callbacks.setSelected([], false);
508
- this.updateDeselectAll();
509
- }
510
- else {
511
- const firstOption = this.store.getFirstOption();
512
- const id = firstOption ? firstOption.id : '';
513
- this.callbacks.setSelected(id, false);
514
- }
515
- if (this.settings.closeOnSelect) {
516
- this.callbacks.close();
517
- }
518
- if (this.callbacks.afterChange) {
519
- this.callbacks.afterChange(this.store.getSelectedOptions());
520
- }
521
- }
522
- };
523
- const deselectSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
524
- deselectSvg.setAttribute('viewBox', '0 0 100 100');
525
- const deselectPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
526
- deselectPath.setAttribute('d', this.classes.deselectPath);
527
- deselectSvg.appendChild(deselectPath);
528
- deselect.appendChild(deselectSvg);
529
- main.appendChild(deselect);
530
- const arrow = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
531
- arrow.classList.add(this.classes.arrow);
532
- arrow.setAttribute('viewBox', '0 0 100 100');
533
- const arrowPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
534
- arrowPath.setAttribute('d', this.classes.arrowClose);
535
- if (this.settings.alwaysOpen) {
536
- arrow.classList.add(this.classes.hide);
537
- }
538
- arrow.appendChild(arrowPath);
539
- main.appendChild(arrow);
540
- return {
541
- main: main,
542
- values: values,
543
- deselect: {
544
- main: deselect,
545
- svg: deselectSvg,
546
- path: deselectPath
547
- },
548
- arrow: {
549
- main: arrow,
550
- path: arrowPath
551
- }
552
- };
553
- }
554
- mainFocus(eventType) {
555
- if (eventType !== 'click') {
556
- this.main.main.focus({ preventScroll: true });
557
- }
558
- }
559
- placeholder() {
560
- const placeholderOption = this.store.filter((o) => o.placeholder, false);
561
- let placeholderText = this.settings.placeholderText;
562
- if (placeholderOption.length) {
563
- if (placeholderOption[0].html !== '') {
564
- placeholderText = placeholderOption[0].html;
565
- }
566
- else if (placeholderOption[0].text !== '') {
567
- placeholderText = placeholderOption[0].text;
568
- }
569
- }
570
- const placeholder = document.createElement('div');
571
- placeholder.classList.add(this.classes.placeholder);
572
- placeholder.innerHTML = placeholderText;
573
- return placeholder;
574
- }
575
- renderValues() {
576
- if (!this.settings.isMultiple) {
577
- this.renderSingleValue();
578
- return;
579
- }
580
- this.renderMultipleValues();
581
- this.updateDeselectAll();
582
- }
583
- renderSingleValue() {
584
- const selected = this.store.filter((o) => {
585
- return o.selected && !o.placeholder;
586
- }, false);
587
- const selectedSingle = selected.length > 0 ? selected[0] : null;
588
- if (!selectedSingle) {
589
- this.main.values.innerHTML = this.placeholder().outerHTML;
590
- }
591
- else {
592
- const singleValue = document.createElement('div');
593
- singleValue.classList.add(this.classes.single);
594
- if (selectedSingle.html) {
595
- singleValue.innerHTML = selectedSingle.html;
596
- }
597
- else {
598
- singleValue.innerText = selectedSingle.text;
599
- }
600
- this.main.values.innerHTML = singleValue.outerHTML;
601
- }
602
- if (!this.settings.allowDeselect || !selected.length) {
603
- this.main.deselect.main.classList.add(this.classes.hide);
604
- }
605
- else {
606
- this.main.deselect.main.classList.remove(this.classes.hide);
607
- }
608
- }
609
- renderMultipleValues() {
610
- let currentNodes = this.main.values.childNodes;
611
- let selectedOptions = this.store.filter((opt) => {
612
- return opt.selected && opt.display;
613
- }, false);
614
- if (selectedOptions.length === 0) {
615
- this.main.values.innerHTML = this.placeholder().outerHTML;
616
- return;
617
- }
618
- else {
619
- const placeholder = this.main.values.querySelector('.' + this.classes.placeholder);
620
- if (placeholder) {
621
- placeholder.remove();
622
- }
623
- }
624
- if (selectedOptions.length > this.settings.maxValuesShown) {
625
- const singleValue = document.createElement('div');
626
- singleValue.classList.add(this.classes.max);
627
- singleValue.textContent = this.settings.maxValuesMessage.replace('{number}', selectedOptions.length.toString());
628
- this.main.values.innerHTML = singleValue.outerHTML;
629
- return;
630
- }
631
- else {
632
- const maxValuesMessage = this.main.values.querySelector('.' + this.classes.max);
633
- if (maxValuesMessage) {
634
- maxValuesMessage.remove();
635
- }
636
- }
637
- if (this.settings.keepOrder) {
638
- selectedOptions = this.store.selectedOrderOptions(selectedOptions);
639
- }
640
- let removeNodes = [];
641
- for (let i = 0; i < currentNodes.length; i++) {
642
- const node = currentNodes[i];
643
- const id = node.getAttribute('data-id');
644
- if (id) {
645
- const found = selectedOptions.filter((opt) => {
646
- return opt.id === id;
647
- }, false);
648
- if (!found.length) {
649
- removeNodes.push(node);
650
- }
651
- }
652
- }
653
- for (const n of removeNodes) {
654
- n.classList.add(this.classes.valueOut);
655
- setTimeout(() => {
656
- if (this.main.values.hasChildNodes() && this.main.values.contains(n)) {
657
- this.main.values.removeChild(n);
658
- }
659
- }, 100);
660
- }
661
- currentNodes = this.main.values.childNodes;
662
- for (let d = 0; d < selectedOptions.length; d++) {
663
- let shouldAdd = true;
664
- for (let i = 0; i < currentNodes.length; i++) {
665
- if (selectedOptions[d].id === String(currentNodes[i].dataset.id)) {
666
- shouldAdd = false;
667
- }
668
- }
669
- if (shouldAdd) {
670
- if (this.settings.keepOrder) {
671
- this.main.values.appendChild(this.multipleValue(selectedOptions[d]));
672
- }
673
- else {
674
- if (currentNodes.length === 0) {
675
- this.main.values.appendChild(this.multipleValue(selectedOptions[d]));
676
- }
677
- else if (d === 0) {
678
- this.main.values.insertBefore(this.multipleValue(selectedOptions[d]), currentNodes[d]);
679
- }
680
- else {
681
- currentNodes[d - 1].insertAdjacentElement('afterend', this.multipleValue(selectedOptions[d]));
682
- }
683
- }
684
- }
685
- }
686
- }
687
- multipleValue(option) {
688
- const value = document.createElement('div');
689
- value.classList.add(this.classes.value);
690
- value.dataset.id = option.id;
691
- const text = document.createElement('div');
692
- text.classList.add(this.classes.valueText);
693
- text.textContent = option.text;
694
- value.appendChild(text);
695
- if (!option.mandatory) {
696
- const deleteDiv = document.createElement('div');
697
- deleteDiv.classList.add(this.classes.valueDelete);
698
- deleteDiv.setAttribute('tabindex', '0');
699
- deleteDiv.onclick = (e) => {
700
- e.preventDefault();
701
- e.stopPropagation();
702
- if (this.settings.disabled) {
703
- return;
704
- }
705
- let shouldDelete = true;
706
- const before = this.store.getSelectedOptions();
707
- const after = before.filter((o) => {
708
- return o.selected && o.id !== option.id;
709
- }, true);
710
- if (this.settings.minSelected && after.length < this.settings.minSelected) {
711
- return;
712
- }
713
- if (this.callbacks.beforeChange) {
714
- shouldDelete = this.callbacks.beforeChange(after, before) === true;
715
- }
716
- if (shouldDelete) {
717
- let selectedIds = [];
718
- for (const o of after) {
719
- if (o instanceof Optgroup) {
720
- for (const c of o.options) {
721
- selectedIds.push(c.id);
722
- }
723
- }
724
- if (o instanceof Option) {
725
- selectedIds.push(o.id);
726
- }
727
- }
728
- this.callbacks.setSelected(selectedIds, false);
729
- if (this.settings.closeOnSelect) {
730
- this.callbacks.close();
731
- }
732
- if (this.callbacks.afterChange) {
733
- this.callbacks.afterChange(after);
734
- }
735
- this.updateDeselectAll();
736
- }
737
- };
738
- const deleteSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
739
- deleteSvg.setAttribute('viewBox', '0 0 100 100');
740
- const deletePath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
741
- deletePath.setAttribute('d', this.classes.optionDelete);
742
- deleteSvg.appendChild(deletePath);
743
- deleteDiv.appendChild(deleteSvg);
744
- value.appendChild(deleteDiv);
745
- deleteDiv.onkeydown = (e) => {
746
- if (e.key === 'Enter') {
747
- deleteDiv.click();
748
- }
749
- };
750
- }
751
- return value;
752
- }
753
- contentDiv() {
754
- const main = document.createElement('div');
755
- main.dataset.id = this.settings.id;
756
- const search = this.searchDiv();
757
- main.appendChild(search.main);
758
- const list = this.listDiv();
759
- main.appendChild(list);
760
- return {
761
- main: main,
762
- search: search,
763
- list: list
764
- };
765
- }
766
- moveContent() {
767
- if (this.settings.contentPosition === 'relative') {
768
- this.moveContentBelow();
769
- return;
770
- }
771
- if (this.settings.openPosition === 'down') {
772
- this.moveContentBelow();
773
- return;
774
- }
775
- else if (this.settings.openPosition === 'up') {
776
- this.moveContentAbove();
777
- return;
778
- }
779
- if (this.putContent() === 'up') {
780
- this.moveContentAbove();
781
- }
782
- else {
783
- this.moveContentBelow();
784
- }
785
- }
786
- searchDiv() {
787
- const main = document.createElement('div');
788
- const input = document.createElement('input');
789
- const addable = document.createElement('div');
790
- main.classList.add(this.classes.search);
791
- const searchReturn = {
792
- main,
793
- input
794
- };
795
- if (!this.settings.showSearch) {
796
- main.classList.add(this.classes.hide);
797
- input.readOnly = true;
798
- }
799
- input.type = 'search';
800
- input.placeholder = this.settings.searchPlaceholder;
801
- input.tabIndex = -1;
802
- input.setAttribute('aria-label', this.settings.searchPlaceholder);
803
- input.setAttribute('autocapitalize', 'off');
804
- input.setAttribute('autocomplete', 'off');
805
- input.setAttribute('autocorrect', 'off');
806
- input.oninput = debounce((e) => {
807
- this.callbacks.search(e.target.value);
808
- }, 100);
809
- input.onkeydown = (e) => {
810
- switch (e.key) {
811
- case 'ArrowUp':
812
- case 'ArrowDown':
813
- e.key === 'ArrowDown' ? this.highlight('down') : this.highlight('up');
814
- return false;
815
- case 'Tab':
816
- this.callbacks.close();
817
- return true;
818
- case 'Escape':
819
- this.callbacks.close();
820
- return false;
821
- case ' ':
822
- const highlighted = this.content.list.querySelector('.' + this.classes.highlighted);
823
- if (highlighted) {
824
- highlighted.click();
825
- return false;
826
- }
827
- return true;
828
- case 'Enter':
829
- if (this.callbacks.addable) {
830
- addable.click();
831
- return false;
832
- }
833
- else {
834
- const highlighted = this.content.list.querySelector('.' + this.classes.highlighted);
835
- if (highlighted) {
836
- highlighted.click();
837
- return false;
838
- }
839
- }
840
- return true;
841
- }
842
- return true;
843
- };
844
- main.appendChild(input);
845
- if (this.callbacks.addable) {
846
- addable.classList.add(this.classes.addable);
847
- const plus = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
848
- plus.setAttribute('viewBox', '0 0 100 100');
849
- const plusPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
850
- plusPath.setAttribute('d', this.classes.addablePath);
851
- plus.appendChild(plusPath);
852
- addable.appendChild(plus);
853
- addable.onclick = (e) => {
854
- e.preventDefault();
855
- e.stopPropagation();
856
- if (!this.callbacks.addable) {
857
- return;
858
- }
859
- const inputValue = this.content.search.input.value.trim();
860
- if (inputValue === '') {
861
- this.content.search.input.focus();
862
- return;
863
- }
864
- const runFinish = (oo) => {
865
- let newOption = new Option(oo);
866
- this.callbacks.addOption(newOption);
867
- if (this.settings.isMultiple) {
868
- let ids = this.store.getSelected();
869
- ids.push(newOption.id);
870
- this.callbacks.setSelected(ids, true);
871
- }
872
- else {
873
- this.callbacks.setSelected([newOption.id], true);
874
- }
875
- this.callbacks.search('');
876
- if (this.settings.closeOnSelect) {
877
- setTimeout(() => {
878
- this.callbacks.close();
879
- }, 100);
880
- }
881
- };
882
- const addableValue = this.callbacks.addable(inputValue);
883
- if (addableValue === false || addableValue === undefined || addableValue === null) {
884
- return;
885
- }
886
- if (addableValue instanceof Promise) {
887
- addableValue.then((value) => {
888
- if (typeof value === 'string') {
889
- runFinish({
890
- text: value,
891
- value: value
892
- });
893
- }
894
- else if (addableValue instanceof Error) {
895
- this.renderError(addableValue.message);
896
- }
897
- else {
898
- runFinish(value);
899
- }
900
- });
901
- }
902
- else if (typeof addableValue === 'string') {
903
- runFinish({
904
- text: addableValue,
905
- value: addableValue
906
- });
907
- }
908
- else if (addableValue instanceof Error) {
909
- this.renderError(addableValue.message);
910
- }
911
- else {
912
- runFinish(addableValue);
913
- }
914
- return;
915
- };
916
- main.appendChild(addable);
917
- searchReturn.addable = {
918
- main: addable,
919
- svg: plus,
920
- path: plusPath
921
- };
922
- }
923
- return searchReturn;
924
- }
925
- searchFocus() {
926
- this.content.search.input.focus();
927
- }
928
- getOptions(notPlaceholder = false, notDisabled = false, notHidden = false) {
929
- let query = '.' + this.classes.option;
930
- if (notPlaceholder) {
931
- query += ':not(.' + this.classes.placeholder + ')';
932
- }
933
- if (notDisabled) {
934
- query += ':not(.' + this.classes.disabled + ')';
935
- }
936
- if (notHidden) {
937
- query += ':not(.' + this.classes.hide + ')';
938
- }
939
- return Array.from(this.content.list.querySelectorAll(query));
940
- }
941
- highlight(dir) {
942
- const options = this.getOptions(true, true, true);
943
- if (options.length === 0) {
944
- return;
945
- }
946
- if (options.length === 1) {
947
- if (!options[0].classList.contains(this.classes.highlighted)) {
948
- options[0].classList.add(this.classes.highlighted);
949
- return;
950
- }
951
- }
952
- let highlighted = false;
953
- for (const o of options) {
954
- if (o.classList.contains(this.classes.highlighted)) {
955
- highlighted = true;
956
- }
957
- }
958
- if (!highlighted) {
959
- for (const o of options) {
960
- if (o.classList.contains(this.classes.selected)) {
961
- o.classList.add(this.classes.highlighted);
962
- break;
963
- }
964
- }
965
- }
966
- for (let i = 0; i < options.length; i++) {
967
- if (options[i].classList.contains(this.classes.highlighted)) {
968
- const prevOption = options[i];
969
- prevOption.classList.remove(this.classes.highlighted);
970
- const prevParent = prevOption.parentElement;
971
- if (prevParent && prevParent.classList.contains(this.classes.open)) {
972
- const optgroupLabel = prevParent.querySelector('.' + this.classes.optgroupLabel);
973
- if (optgroupLabel) {
974
- optgroupLabel.click();
975
- }
976
- }
977
- let selectOption = options[dir === 'down' ? (i + 1 < options.length ? i + 1 : 0) : i - 1 >= 0 ? i - 1 : options.length - 1];
978
- selectOption.classList.add(this.classes.highlighted);
979
- this.ensureElementInView(this.content.list, selectOption);
980
- const selectParent = selectOption.parentElement;
981
- if (selectParent && selectParent.classList.contains(this.classes.close)) {
982
- const optgroupLabel = selectParent.querySelector('.' + this.classes.optgroupLabel);
983
- if (optgroupLabel) {
984
- optgroupLabel.click();
985
- }
986
- }
987
- return;
988
- }
989
- }
990
- options[dir === 'down' ? 0 : options.length - 1].classList.add(this.classes.highlighted);
991
- this.ensureElementInView(this.content.list, options[dir === 'down' ? 0 : options.length - 1]);
992
- }
993
- listDiv() {
994
- const options = document.createElement('div');
995
- options.classList.add(this.classes.list);
996
- return options;
997
- }
998
- renderError(error) {
999
- this.content.list.innerHTML = '';
1000
- const errorDiv = document.createElement('div');
1001
- errorDiv.classList.add(this.classes.error);
1002
- errorDiv.textContent = error;
1003
- this.content.list.appendChild(errorDiv);
1004
- }
1005
- renderSearching() {
1006
- this.content.list.innerHTML = '';
1007
- const searchingDiv = document.createElement('div');
1008
- searchingDiv.classList.add(this.classes.searching);
1009
- searchingDiv.textContent = this.settings.searchingText;
1010
- this.content.list.appendChild(searchingDiv);
1011
- }
1012
- renderOptions(data) {
1013
- this.content.list.innerHTML = '';
1014
- if (data.length === 0) {
1015
- const noResults = document.createElement('div');
1016
- noResults.classList.add(this.classes.search);
1017
- if (this.callbacks.addable) {
1018
- noResults.innerHTML = this.settings.addableText.replace('{value}', this.content.search.input.value);
1019
- }
1020
- else {
1021
- noResults.innerHTML = this.settings.searchText;
1022
- }
1023
- this.content.list.appendChild(noResults);
1024
- return;
1025
- }
1026
- if (this.settings.allowDeselect && !this.settings.isMultiple) {
1027
- const placeholderOption = this.store.filter((o) => o.placeholder, false);
1028
- if (!placeholderOption.length) {
1029
- this.store.addOption(new Option({
1030
- text: '',
1031
- value: '',
1032
- selected: false,
1033
- placeholder: true
1034
- }), true);
1035
- }
1036
- }
1037
- const fragment = document.createDocumentFragment();
1038
- for (const d of data) {
1039
- if (d instanceof Optgroup) {
1040
- const optgroupEl = document.createElement('div');
1041
- optgroupEl.classList.add(this.classes.optgroup);
1042
- const optgroupLabel = document.createElement('div');
1043
- optgroupLabel.classList.add(this.classes.optgroupLabel);
1044
- optgroupEl.appendChild(optgroupLabel);
1045
- const optgroupLabelText = document.createElement('div');
1046
- optgroupLabelText.classList.add(this.classes.optgroupLabelText);
1047
- optgroupLabelText.textContent = d.label;
1048
- optgroupLabel.appendChild(optgroupLabelText);
1049
- const optgroupActions = document.createElement('div');
1050
- optgroupActions.classList.add(this.classes.optgroupActions);
1051
- optgroupLabel.appendChild(optgroupActions);
1052
- if (this.settings.isMultiple && d.selectAll) {
1053
- const selectAll = document.createElement('div');
1054
- selectAll.classList.add(this.classes.optgroupSelectAll);
1055
- let allSelected = true;
1056
- for (const o of d.options) {
1057
- if (!o.selected) {
1058
- allSelected = false;
1059
- break;
1060
- }
1061
- }
1062
- if (allSelected) {
1063
- selectAll.classList.add(this.classes.selected);
1064
- }
1065
- const selectAllText = document.createElement('span');
1066
- selectAllText.textContent = d.selectAllText;
1067
- selectAll.appendChild(selectAllText);
1068
- const selectAllSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
1069
- selectAllSvg.setAttribute('viewBox', '0 0 100 100');
1070
- selectAll.appendChild(selectAllSvg);
1071
- const selectAllBox = document.createElementNS('http://www.w3.org/2000/svg', 'path');
1072
- selectAllBox.setAttribute('d', this.classes.optgroupSelectAllBox);
1073
- selectAllSvg.appendChild(selectAllBox);
1074
- const selectAllCheck = document.createElementNS('http://www.w3.org/2000/svg', 'path');
1075
- selectAllCheck.setAttribute('d', this.classes.optgroupSelectAllCheck);
1076
- selectAllSvg.appendChild(selectAllCheck);
1077
- selectAll.addEventListener('click', (e) => {
1078
- e.preventDefault();
1079
- e.stopPropagation();
1080
- const currentSelected = this.store.getSelected();
1081
- if (allSelected) {
1082
- const newSelected = currentSelected.filter((s) => {
1083
- for (const o of d.options) {
1084
- if (s === o.id) {
1085
- return false;
1086
- }
1087
- }
1088
- return true;
1089
- });
1090
- this.callbacks.setSelected(newSelected, true);
1091
- return;
1092
- }
1093
- else {
1094
- const newSelected = currentSelected.concat(d.options.map((o) => o.id));
1095
- for (const o of d.options) {
1096
- if (!this.store.getOptionByID(o.id)) {
1097
- this.callbacks.addOption(o);
1098
- }
1099
- }
1100
- this.callbacks.setSelected(newSelected, true);
1101
- return;
1102
- }
1103
- });
1104
- optgroupActions.appendChild(selectAll);
1105
- }
1106
- if (d.closable !== 'off') {
1107
- const optgroupClosable = document.createElement('div');
1108
- optgroupClosable.classList.add(this.classes.optgroupClosable);
1109
- const optgroupClosableSvg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
1110
- optgroupClosableSvg.setAttribute('viewBox', '0 0 100 100');
1111
- optgroupClosableSvg.classList.add(this.classes.arrow);
1112
- optgroupClosable.appendChild(optgroupClosableSvg);
1113
- const optgroupClosableArrow = document.createElementNS('http://www.w3.org/2000/svg', 'path');
1114
- optgroupClosableSvg.appendChild(optgroupClosableArrow);
1115
- if (d.options.some((o) => o.selected) || this.content.search.input.value.trim() !== '') {
1116
- optgroupClosable.classList.add(this.classes.open);
1117
- optgroupClosableArrow.setAttribute('d', this.classes.arrowOpen);
1118
- }
1119
- else if (d.closable === 'open') {
1120
- optgroupEl.classList.add(this.classes.open);
1121
- optgroupClosableArrow.setAttribute('d', this.classes.arrowOpen);
1122
- }
1123
- else if (d.closable === 'close') {
1124
- optgroupEl.classList.add(this.classes.close);
1125
- optgroupClosableArrow.setAttribute('d', this.classes.arrowClose);
1126
- }
1127
- optgroupLabel.addEventListener('click', (e) => {
1128
- e.preventDefault();
1129
- e.stopPropagation();
1130
- if (optgroupEl.classList.contains(this.classes.close)) {
1131
- optgroupEl.classList.remove(this.classes.close);
1132
- optgroupEl.classList.add(this.classes.open);
1133
- optgroupClosableArrow.setAttribute('d', this.classes.arrowOpen);
1134
- }
1135
- else {
1136
- optgroupEl.classList.remove(this.classes.open);
1137
- optgroupEl.classList.add(this.classes.close);
1138
- optgroupClosableArrow.setAttribute('d', this.classes.arrowClose);
1139
- }
1140
- });
1141
- optgroupActions.appendChild(optgroupClosable);
1142
- }
1143
- optgroupEl.appendChild(optgroupLabel);
1144
- for (const o of d.options) {
1145
- optgroupEl.appendChild(this.option(o));
1146
- fragment.appendChild(optgroupEl);
1147
- }
1148
- }
1149
- if (d instanceof Option) {
1150
- fragment.appendChild(this.option(d));
1151
- }
1152
- }
1153
- this.content.list.appendChild(fragment);
1154
- }
1155
- option(option) {
1156
- if (option.placeholder) {
1157
- const placeholder = document.createElement('div');
1158
- placeholder.classList.add(this.classes.option);
1159
- placeholder.classList.add(this.classes.hide);
1160
- return placeholder;
1161
- }
1162
- const optionEl = document.createElement('div');
1163
- optionEl.dataset.id = option.id;
1164
- optionEl.classList.add(this.classes.option);
1165
- optionEl.setAttribute('role', 'option');
1166
- if (option.class) {
1167
- option.class.split(' ').forEach((dataClass) => {
1168
- optionEl.classList.add(dataClass);
1169
- });
1170
- }
1171
- if (option.style) {
1172
- optionEl.style.cssText = option.style;
1173
- }
1174
- if (this.settings.searchHighlight && this.content.search.input.value.trim() !== '') {
1175
- optionEl.innerHTML = this.highlightText(option.html !== '' ? option.html : option.text, this.content.search.input.value, this.classes.searchHighlighter);
1176
- }
1177
- else if (option.html !== '') {
1178
- optionEl.innerHTML = option.html;
1179
- }
1180
- else {
1181
- optionEl.textContent = option.text;
1182
- }
1183
- if (this.settings.showOptionTooltips && optionEl.textContent) {
1184
- optionEl.setAttribute('title', optionEl.textContent);
1185
- }
1186
- if (!option.display) {
1187
- optionEl.classList.add(this.classes.hide);
1188
- }
1189
- if (option.disabled) {
1190
- optionEl.classList.add(this.classes.disabled);
1191
- }
1192
- if (option.selected && this.settings.hideSelected) {
1193
- optionEl.classList.add(this.classes.hide);
1194
- }
1195
- if (option.selected) {
1196
- optionEl.classList.add(this.classes.selected);
1197
- optionEl.setAttribute('aria-selected', 'true');
1198
- this.main.main.setAttribute('aria-activedescendant', optionEl.id);
1199
- }
1200
- else {
1201
- optionEl.classList.remove(this.classes.selected);
1202
- optionEl.setAttribute('aria-selected', 'false');
1203
- }
1204
- optionEl.addEventListener('click', (e) => {
1205
- e.preventDefault();
1206
- e.stopPropagation();
1207
- const selectedOptions = this.store.getSelected();
1208
- const element = e.currentTarget;
1209
- const elementID = String(element.dataset.id);
1210
- if (option.disabled || (option.selected && !this.settings.allowDeselect)) {
1211
- return;
1212
- }
1213
- if ((this.settings.isMultiple && this.settings.maxSelected <= selectedOptions.length && !option.selected) ||
1214
- (this.settings.isMultiple && this.settings.minSelected >= selectedOptions.length && option.selected)) {
1215
- return;
1216
- }
1217
- let shouldUpdate = false;
1218
- const before = this.store.getSelectedOptions();
1219
- let after = [];
1220
- if (this.settings.isMultiple) {
1221
- if (option.selected) {
1222
- after = before.filter((o) => o.id !== elementID);
1223
- }
1224
- else {
1225
- after = before.concat(option);
1226
- if (!this.settings.closeOnSelect) {
1227
- if (e.shiftKey && this.lastSelectedOption) {
1228
- const options = this.store.getDataOptions();
1229
- let lastClickedOptionIndex = options.findIndex((o) => o.id === this.lastSelectedOption.id);
1230
- let currentOptionIndex = options.findIndex((o) => o.id === option.id);
1231
- if (lastClickedOptionIndex >= 0 && currentOptionIndex >= 0) {
1232
- const startIndex = Math.min(lastClickedOptionIndex, currentOptionIndex);
1233
- const endIndex = Math.max(lastClickedOptionIndex, currentOptionIndex);
1234
- const afterRange = options.slice(startIndex, endIndex + 1);
1235
- if (afterRange.length > 0 && afterRange.length < this.settings.maxSelected) {
1236
- after = before.concat(afterRange.filter((a) => !before.find((b) => b.id === a.id)));
1237
- }
1238
- }
1239
- }
1240
- else if (!option.selected) {
1241
- this.lastSelectedOption = option;
1242
- }
1243
- }
1244
- }
1245
- }
1246
- if (!this.settings.isMultiple) {
1247
- if (option.selected) {
1248
- after = [];
1249
- }
1250
- else {
1251
- after = [option];
1252
- }
1253
- }
1254
- if (!this.callbacks.beforeChange) {
1255
- shouldUpdate = true;
1256
- }
1257
- if (this.callbacks.beforeChange) {
1258
- if (this.callbacks.beforeChange(after, before) === false) {
1259
- shouldUpdate = false;
1260
- }
1261
- else {
1262
- shouldUpdate = true;
1263
- }
1264
- }
1265
- if (shouldUpdate) {
1266
- if (!this.store.getOptionByID(elementID)) {
1267
- this.callbacks.addOption(option);
1268
- }
1269
- this.callbacks.setSelected(after.map((o) => o.id), false);
1270
- if (this.settings.closeOnSelect) {
1271
- this.callbacks.close();
1272
- }
1273
- if (this.callbacks.afterChange) {
1274
- this.callbacks.afterChange(after);
1275
- }
1276
- }
1277
- });
1278
- return optionEl;
1279
- }
1280
- destroy() {
1281
- this.main.main.remove();
1282
- this.content.main.remove();
1283
- }
1284
- highlightText(str, search, className) {
1285
- let completedString = str;
1286
- const regex = new RegExp('(?![^<]*>)(' + search.trim() + ')(?![^<]*>[^<>]*</)', 'i');
1287
- if (!str.match(regex)) {
1288
- return str;
1289
- }
1290
- const matchStartPosition = str.match(regex).index;
1291
- const matchEndPosition = matchStartPosition + str.match(regex)[0].toString().length;
1292
- const originalTextFoundByRegex = str.substring(matchStartPosition, matchEndPosition);
1293
- completedString = completedString.replace(regex, `<mark class="${className}">${originalTextFoundByRegex}</mark>`);
1294
- return completedString;
1295
- }
1296
- moveContentAbove() {
1297
- const mainHeight = this.main.main.offsetHeight;
1298
- const contentHeight = this.content.main.offsetHeight;
1299
- this.main.main.classList.remove(this.classes.openBelow);
1300
- this.main.main.classList.add(this.classes.openAbove);
1301
- this.content.main.classList.remove(this.classes.openBelow);
1302
- this.content.main.classList.add(this.classes.openAbove);
1303
- const containerRect = this.main.main.getBoundingClientRect();
1304
- this.content.main.style.margin = '-' + (mainHeight + contentHeight - 1) + 'px 0px 0px 0px';
1305
- this.content.main.style.top =
1306
- containerRect.top + containerRect.height + (this.settings.contentPosition === 'fixed' ? 0 : window.scrollY) + 'px';
1307
- this.content.main.style.left =
1308
- containerRect.left + (this.settings.contentPosition === 'fixed' ? 0 : window.scrollX) + 'px';
1309
- this.content.main.style.width = containerRect.width + 'px';
1310
- }
1311
- moveContentBelow() {
1312
- this.main.main.classList.remove(this.classes.openAbove);
1313
- this.main.main.classList.add(this.classes.openBelow);
1314
- this.content.main.classList.remove(this.classes.openAbove);
1315
- this.content.main.classList.add(this.classes.openBelow);
1316
- const containerRect = this.main.main.getBoundingClientRect();
1317
- this.content.main.style.margin = '-1px 0px 0px 0px';
1318
- if (this.settings.contentPosition !== 'relative') {
1319
- this.content.main.style.top =
1320
- containerRect.top +
1321
- containerRect.height +
1322
- (this.settings.contentPosition === 'fixed' ? 0 : window.scrollY) +
1323
- 'px';
1324
- this.content.main.style.left =
1325
- containerRect.left + (this.settings.contentPosition === 'fixed' ? 0 : window.scrollX) + 'px';
1326
- this.content.main.style.width = containerRect.width + 'px';
1327
- }
1328
- }
1329
- ensureElementInView(container, element) {
1330
- const cTop = container.scrollTop + container.offsetTop;
1331
- const cBottom = cTop + container.clientHeight;
1332
- const eTop = element.offsetTop;
1333
- const eBottom = eTop + element.clientHeight;
1334
- if (eTop < cTop) {
1335
- container.scrollTop -= cTop - eTop;
1336
- }
1337
- else if (eBottom > cBottom) {
1338
- container.scrollTop += eBottom - cBottom;
1339
- }
1340
- }
1341
- putContent() {
1342
- const mainHeight = this.main.main.offsetHeight;
1343
- const mainRect = this.main.main.getBoundingClientRect();
1344
- const contentHeight = this.content.main.offsetHeight;
1345
- const spaceBelow = window.innerHeight - (mainRect.top + mainHeight);
1346
- if (spaceBelow <= contentHeight) {
1347
- if (mainRect.top > contentHeight) {
1348
- return 'up';
1349
- }
1350
- else {
1351
- return 'down';
1352
- }
1353
- }
1354
- return 'down';
1355
- }
1356
- updateDeselectAll() {
1357
- if (!this.store || !this.settings) {
1358
- return;
1359
- }
1360
- const selected = this.store.getSelectedOptions();
1361
- const hasSelectedItems = selected && selected.length > 0;
1362
- const isMultiple = this.settings.isMultiple;
1363
- const allowDeselect = this.settings.allowDeselect;
1364
- const deselectButton = this.main.deselect.main;
1365
- const hideClass = this.classes.hide;
1366
- if (allowDeselect && !(isMultiple && !hasSelectedItems)) {
1367
- deselectButton.classList.remove(hideClass);
1368
- }
1369
- else {
1370
- deselectButton.classList.add(hideClass);
1371
- }
1372
- }
1373
- }
1374
-
1375
- class Select {
1376
- constructor(select) {
1377
- this.listen = false;
1378
- this.observer = null;
1379
- this.select = select;
1380
- this.valueChange = this.valueChange.bind(this);
1381
- this.select.addEventListener('change', this.valueChange, {
1382
- passive: true
1383
- });
1384
- this.observer = new MutationObserver(this.observeCall.bind(this));
1385
- this.changeListen(true);
1386
- }
1387
- enable() {
1388
- this.select.disabled = false;
1389
- }
1390
- disable() {
1391
- this.select.disabled = true;
1392
- }
1393
- hideUI() {
1394
- this.select.tabIndex = -1;
1395
- this.select.style.display = 'none';
1396
- this.select.setAttribute('aria-hidden', 'true');
1397
- }
1398
- showUI() {
1399
- this.select.removeAttribute('tabindex');
1400
- this.select.style.display = '';
1401
- this.select.removeAttribute('aria-hidden');
1402
- }
1403
- changeListen(listen) {
1404
- this.listen = listen;
1405
- if (listen) {
1406
- if (this.observer) {
1407
- this.observer.observe(this.select, {
1408
- subtree: true,
1409
- childList: true,
1410
- attributes: true
1411
- });
1412
- }
1413
- }
1414
- if (!listen) {
1415
- if (this.observer) {
1416
- this.observer.disconnect();
1417
- }
1418
- }
1419
- }
1420
- valueChange(ev) {
1421
- if (this.listen && this.onValueChange) {
1422
- this.onValueChange(this.getSelectedOptions());
1423
- }
1424
- return true;
1425
- }
1426
- observeCall(mutations) {
1427
- if (!this.listen) {
1428
- return;
1429
- }
1430
- let classChanged = false;
1431
- let disabledChanged = false;
1432
- let optgroupOptionChanged = false;
1433
- for (const m of mutations) {
1434
- if (m.target === this.select) {
1435
- if (m.attributeName === 'disabled') {
1436
- disabledChanged = true;
1437
- }
1438
- if (m.attributeName === 'class') {
1439
- classChanged = true;
1440
- }
1441
- if (m.type === 'childList') {
1442
- for (const n of m.addedNodes) {
1443
- if (n.nodeName === 'OPTION' && n.value === this.select.value) {
1444
- this.select.dispatchEvent(new Event('change'));
1445
- break;
1446
- }
1447
- }
1448
- optgroupOptionChanged = true;
1449
- }
1450
- }
1451
- if (m.target.nodeName === 'OPTGROUP' || m.target.nodeName === 'OPTION') {
1452
- optgroupOptionChanged = true;
1453
- }
1454
- }
1455
- if (classChanged && this.onClassChange) {
1456
- this.onClassChange(this.select.className.split(' '));
1457
- }
1458
- if (disabledChanged && this.onDisabledChange) {
1459
- this.changeListen(false);
1460
- this.onDisabledChange(this.select.disabled);
1461
- this.changeListen(true);
1462
- }
1463
- if (optgroupOptionChanged && this.onOptionsChange) {
1464
- this.changeListen(false);
1465
- this.onOptionsChange(this.getData());
1466
- this.changeListen(true);
1467
- }
1468
- }
1469
- getData() {
1470
- let data = [];
1471
- const nodes = this.select.childNodes;
1472
- for (const n of nodes) {
1473
- if (n.nodeName === 'OPTGROUP') {
1474
- data.push(this.getDataFromOptgroup(n));
1475
- }
1476
- if (n.nodeName === 'OPTION') {
1477
- data.push(this.getDataFromOption(n));
1478
- }
1479
- }
1480
- return data;
1481
- }
1482
- getDataFromOptgroup(optgroup) {
1483
- let data = {
1484
- id: optgroup.id,
1485
- label: optgroup.label,
1486
- selectAll: optgroup.dataset ? optgroup.dataset.selectall === 'true' : false,
1487
- selectAllText: optgroup.dataset ? optgroup.dataset.selectalltext : 'Select all',
1488
- closable: optgroup.dataset ? optgroup.dataset.closable : 'off',
1489
- options: []
1490
- };
1491
- const options = optgroup.childNodes;
1492
- for (const o of options) {
1493
- if (o.nodeName === 'OPTION') {
1494
- data.options.push(this.getDataFromOption(o));
1495
- }
1496
- }
1497
- return data;
1498
- }
1499
- getDataFromOption(option) {
1500
- return {
1501
- id: option.id,
1502
- value: option.value,
1503
- text: option.text,
1504
- html: option.dataset && option.dataset.html ? option.dataset.html : '',
1505
- defaultSelected: option.defaultSelected,
1506
- selected: option.selected,
1507
- display: option.style.display !== 'none',
1508
- disabled: option.disabled,
1509
- mandatory: option.dataset ? option.dataset.mandatory === 'true' : false,
1510
- placeholder: option.dataset.placeholder === 'true',
1511
- class: option.className,
1512
- style: option.style.cssText,
1513
- data: option.dataset
1514
- };
1515
- }
1516
- getSelectedOptions() {
1517
- let options = [];
1518
- const opts = this.select.childNodes;
1519
- for (const o of opts) {
1520
- if (o.nodeName === 'OPTGROUP') {
1521
- const optgroupOptions = o.childNodes;
1522
- for (const oo of optgroupOptions) {
1523
- if (oo.nodeName === 'OPTION') {
1524
- const option = oo;
1525
- if (option.selected) {
1526
- options.push(this.getDataFromOption(option));
1527
- }
1528
- }
1529
- }
1530
- }
1531
- if (o.nodeName === 'OPTION') {
1532
- const option = o;
1533
- if (option.selected) {
1534
- options.push(this.getDataFromOption(option));
1535
- }
1536
- }
1537
- }
1538
- return options;
1539
- }
1540
- getSelectedValues() {
1541
- return this.getSelectedOptions().map((option) => option.value);
1542
- }
1543
- setSelected(ids) {
1544
- this.changeListen(false);
1545
- const options = this.select.childNodes;
1546
- for (const o of options) {
1547
- if (o.nodeName === 'OPTGROUP') {
1548
- const optgroup = o;
1549
- const optgroupOptions = optgroup.childNodes;
1550
- for (const oo of optgroupOptions) {
1551
- if (oo.nodeName === 'OPTION') {
1552
- const option = oo;
1553
- option.selected = ids.includes(option.id);
1554
- }
1555
- }
1556
- }
1557
- if (o.nodeName === 'OPTION') {
1558
- const option = o;
1559
- option.selected = ids.includes(option.id);
1560
- }
1561
- }
1562
- this.changeListen(true);
1563
- }
1564
- setSelectedByValue(values) {
1565
- this.changeListen(false);
1566
- const options = this.select.childNodes;
1567
- for (const o of options) {
1568
- if (o.nodeName === 'OPTGROUP') {
1569
- const optgroup = o;
1570
- const optgroupOptions = optgroup.childNodes;
1571
- for (const oo of optgroupOptions) {
1572
- if (oo.nodeName === 'OPTION') {
1573
- const option = oo;
1574
- option.selected = values.includes(option.value);
1575
- }
1576
- }
1577
- }
1578
- if (o.nodeName === 'OPTION') {
1579
- const option = o;
1580
- option.selected = values.includes(option.value);
1581
- }
1582
- }
1583
- this.changeListen(true);
1584
- }
1585
- updateSelect(id, style, classes) {
1586
- this.changeListen(false);
1587
- if (id) {
1588
- this.select.dataset.id = id;
1589
- }
1590
- if (style) {
1591
- this.select.style.cssText = style;
1592
- }
1593
- if (classes) {
1594
- this.select.className = '';
1595
- classes.forEach((c) => {
1596
- if (c.trim() !== '') {
1597
- this.select.classList.add(c.trim());
1598
- }
1599
- });
1600
- }
1601
- this.changeListen(true);
1602
- }
1603
- updateOptions(data) {
1604
- this.changeListen(false);
1605
- this.select.innerHTML = '';
1606
- for (const d of data) {
1607
- if (d instanceof Optgroup) {
1608
- this.select.appendChild(this.createOptgroup(d));
1609
- }
1610
- if (d instanceof Option) {
1611
- this.select.appendChild(this.createOption(d));
1612
- }
1613
- }
1614
- this.select.dispatchEvent(new Event('change', { bubbles: true }));
1615
- this.changeListen(true);
1616
- }
1617
- createOptgroup(optgroup) {
1618
- const optgroupEl = document.createElement('optgroup');
1619
- optgroupEl.id = optgroup.id;
1620
- optgroupEl.label = optgroup.label;
1621
- if (optgroup.selectAll) {
1622
- optgroupEl.dataset.selectAll = 'true';
1623
- }
1624
- if (optgroup.closable !== 'off') {
1625
- optgroupEl.dataset.closable = optgroup.closable;
1626
- }
1627
- if (optgroup.options) {
1628
- for (const o of optgroup.options) {
1629
- optgroupEl.appendChild(this.createOption(o));
1630
- }
1631
- }
1632
- return optgroupEl;
1633
- }
1634
- createOption(info) {
1635
- const optionEl = document.createElement('option');
1636
- optionEl.id = info.id;
1637
- optionEl.value = info.value;
1638
- optionEl.textContent = info.text;
1639
- if (info.html !== '') {
1640
- optionEl.setAttribute('data-html', info.html);
1641
- }
1642
- optionEl.defaultSelected = info.defaultSelected;
1643
- optionEl.selected = info.selected;
1644
- if (info.disabled) {
1645
- optionEl.disabled = true;
1646
- }
1647
- if (!info.display) {
1648
- optionEl.style.display = 'none';
1649
- }
1650
- if (info.placeholder) {
1651
- optionEl.setAttribute('data-placeholder', 'true');
1652
- }
1653
- if (info.mandatory) {
1654
- optionEl.setAttribute('data-mandatory', 'true');
1655
- }
1656
- if (info.class) {
1657
- info.class.split(' ').forEach((optionClass) => {
1658
- optionEl.classList.add(optionClass);
1659
- });
1660
- }
1661
- if (info.data && typeof info.data === 'object') {
1662
- Object.keys(info.data).forEach((key) => {
1663
- optionEl.setAttribute('data-' + kebabCase(key), info.data[key]);
1664
- });
1665
- }
1666
- return optionEl;
1667
- }
1668
- destroy() {
1669
- this.changeListen(false);
1670
- this.select.removeEventListener('change', this.valueChange);
1671
- if (this.observer) {
1672
- this.observer.disconnect();
1673
- this.observer = null;
1674
- }
1675
- delete this.select.dataset.id;
1676
- this.showUI();
1677
- }
1678
- }
1679
-
1680
- class Settings {
1681
- constructor(settings) {
1682
- this.id = '';
1683
- this.style = '';
1684
- this.class = [];
1685
- this.isMultiple = false;
1686
- this.isOpen = false;
1687
- this.isFullOpen = false;
1688
- this.intervalMove = null;
1689
- if (!settings) {
1690
- settings = {};
1691
- }
1692
- this.id = 'ss-' + generateID();
1693
- this.style = settings.style || '';
1694
- this.class = settings.class || [];
1695
- this.disabled = settings.disabled !== undefined ? settings.disabled : false;
1696
- this.alwaysOpen = settings.alwaysOpen !== undefined ? settings.alwaysOpen : false;
1697
- this.showSearch = settings.showSearch !== undefined ? settings.showSearch : true;
1698
- this.focusSearch = settings.focusSearch !== undefined ? settings.focusSearch : true;
1699
- this.ariaLabel = settings.ariaLabel || 'Combobox';
1700
- this.searchPlaceholder = settings.searchPlaceholder || 'Search';
1701
- this.searchText = settings.searchText || 'No Results';
1702
- this.searchingText = settings.searchingText || 'Searching...';
1703
- this.searchHighlight = settings.searchHighlight !== undefined ? settings.searchHighlight : false;
1704
- this.closeOnSelect = settings.closeOnSelect !== undefined ? settings.closeOnSelect : true;
1705
- this.contentLocation = settings.contentLocation || document.body;
1706
- this.contentPosition = settings.contentPosition || 'absolute';
1707
- this.openPosition = settings.openPosition || 'auto';
1708
- this.placeholderText = settings.placeholderText !== undefined ? settings.placeholderText : 'Select Value';
1709
- this.allowDeselect = settings.allowDeselect !== undefined ? settings.allowDeselect : false;
1710
- this.hideSelected = settings.hideSelected !== undefined ? settings.hideSelected : false;
1711
- this.keepOrder = settings.keepOrder !== undefined ? settings.keepOrder : false;
1712
- this.showOptionTooltips = settings.showOptionTooltips !== undefined ? settings.showOptionTooltips : false;
1713
- this.minSelected = settings.minSelected || 0;
1714
- this.maxSelected = settings.maxSelected || 1000;
1715
- this.timeoutDelay = settings.timeoutDelay || 200;
1716
- this.maxValuesShown = settings.maxValuesShown || 20;
1717
- this.maxValuesMessage = settings.maxValuesMessage || '{number} selected';
1718
- this.addableText = settings.addableText || 'Press "Enter" to add {value}';
1719
- }
1720
- }
1721
-
1722
- class SlimSelect {
1723
- constructor(config) {
1724
- var _a;
1725
- this.events = {
1726
- search: undefined,
1727
- searchFilter: (opt, search) => {
1728
- return opt.text.toLowerCase().indexOf(search.toLowerCase()) !== -1;
1729
- },
1730
- addable: undefined,
1731
- beforeChange: undefined,
1732
- afterChange: undefined,
1733
- beforeOpen: undefined,
1734
- afterOpen: undefined,
1735
- beforeClose: undefined,
1736
- afterClose: undefined
1737
- };
1738
- this.windowResize = debounce(() => {
1739
- if (!this.settings.isOpen && !this.settings.isFullOpen) {
1740
- return;
1741
- }
1742
- this.render.moveContent();
1743
- });
1744
- this.windowScroll = debounce(() => {
1745
- if (!this.settings.isOpen && !this.settings.isFullOpen) {
1746
- return;
1747
- }
1748
- this.render.moveContent();
1749
- });
1750
- this.documentClick = (e) => {
1751
- if (!this.settings.isOpen) {
1752
- return;
1753
- }
1754
- if (e.target && !hasClassInTree(e.target, this.settings.id)) {
1755
- this.close(e.type);
1756
- }
1757
- };
1758
- this.windowVisibilityChange = () => {
1759
- if (document.hidden) {
1760
- this.close();
1761
- }
1762
- };
1763
- this.selectEl = (typeof config.select === 'string' ? document.querySelector(config.select) : config.select);
1764
- if (!this.selectEl) {
1765
- if (config.events && config.events.error) {
1766
- config.events.error(new Error('Could not find select element'));
1767
- }
1768
- return;
1769
- }
1770
- if (this.selectEl.tagName !== 'SELECT') {
1771
- if (config.events && config.events.error) {
1772
- config.events.error(new Error('Element isnt of type select'));
1773
- }
1774
- return;
1775
- }
1776
- if (this.selectEl.dataset.ssid) {
1777
- this.destroy();
1778
- }
1779
- this.settings = new Settings(config.settings);
1780
- this.cssClasses = new CssClasses(config.cssClasses);
1781
- const debounceEvents = ['afterChange', 'beforeOpen', 'afterOpen', 'beforeClose', 'afterClose'];
1782
- for (const key in config.events) {
1783
- if (!config.events.hasOwnProperty(key)) {
1784
- continue;
1785
- }
1786
- if (debounceEvents.indexOf(key) !== -1) {
1787
- this.events[key] = debounce(config.events[key], 100);
1788
- }
1789
- else {
1790
- this.events[key] = config.events[key];
1791
- }
1792
- }
1793
- this.settings.disabled = ((_a = config.settings) === null || _a === void 0 ? void 0 : _a.disabled) ? config.settings.disabled : this.selectEl.disabled;
1794
- this.settings.isMultiple = this.selectEl.multiple;
1795
- this.settings.style = this.selectEl.style.cssText;
1796
- this.settings.class = this.selectEl.className.split(' ');
1797
- this.select = new Select(this.selectEl);
1798
- this.select.updateSelect(this.settings.id, this.settings.style, this.settings.class);
1799
- this.select.hideUI();
1800
- this.select.onValueChange = (options) => {
1801
- this.setSelected(options.map((option) => option.id));
1802
- };
1803
- this.select.onClassChange = (classes) => {
1804
- this.settings.class = classes;
1805
- this.render.updateClassStyles();
1806
- };
1807
- this.select.onDisabledChange = (disabled) => {
1808
- if (disabled) {
1809
- this.disable();
1810
- }
1811
- else {
1812
- this.enable();
1813
- }
1814
- };
1815
- this.select.onOptionsChange = (data) => {
1816
- this.setData(data);
1817
- };
1818
- this.store = new Store(this.settings.isMultiple ? 'multiple' : 'single', config.data ? config.data : this.select.getData());
1819
- if (config.data) {
1820
- this.select.updateOptions(this.store.getData());
1821
- }
1822
- const renderCallbacks = {
1823
- open: this.open.bind(this),
1824
- close: this.close.bind(this),
1825
- addable: this.events.addable ? this.events.addable : undefined,
1826
- setSelected: this.setSelected.bind(this),
1827
- addOption: this.addOption.bind(this),
1828
- search: this.search.bind(this),
1829
- beforeChange: this.events.beforeChange,
1830
- afterChange: this.events.afterChange
1831
- };
1832
- this.render = new Render(this.settings, this.cssClasses, this.store, renderCallbacks);
1833
- this.render.renderValues();
1834
- this.render.renderOptions(this.store.getData());
1835
- const selectAriaLabel = this.selectEl.getAttribute('aria-label');
1836
- const selectAriaLabelledBy = this.selectEl.getAttribute('aria-labelledby');
1837
- if (selectAriaLabel) {
1838
- this.render.main.main.setAttribute('aria-label', selectAriaLabel);
1839
- }
1840
- else if (selectAriaLabelledBy) {
1841
- this.render.main.main.setAttribute('aria-labelledby', selectAriaLabelledBy);
1842
- }
1843
- if (this.selectEl.parentNode) {
1844
- this.selectEl.parentNode.insertBefore(this.render.main.main, this.selectEl.nextSibling);
1845
- }
1846
- window.addEventListener('resize', this.windowResize, false);
1847
- if (this.settings.openPosition === 'auto') {
1848
- window.addEventListener('scroll', this.windowScroll, false);
1849
- }
1850
- document.addEventListener('visibilitychange', this.windowVisibilityChange);
1851
- if (this.settings.disabled) {
1852
- this.disable();
1853
- }
1854
- if (this.settings.alwaysOpen) {
1855
- this.open();
1856
- }
1857
- this.selectEl.slim = this;
1858
- }
1859
- enable() {
1860
- this.settings.disabled = false;
1861
- this.select.enable();
1862
- this.render.enable();
1863
- }
1864
- disable() {
1865
- this.settings.disabled = true;
1866
- this.select.disable();
1867
- this.render.disable();
1868
- }
1869
- getData() {
1870
- return this.store.getData();
1871
- }
1872
- setData(data) {
1873
- const selected = this.store.getSelected();
1874
- const err = this.store.validateDataArray(data);
1875
- if (err) {
1876
- if (this.events.error) {
1877
- this.events.error(err);
1878
- }
1879
- return;
1880
- }
1881
- this.store.setData(data);
1882
- const dataClean = this.store.getData();
1883
- this.select.updateOptions(dataClean);
1884
- this.render.renderValues();
1885
- this.render.renderOptions(dataClean);
1886
- if (this.events.afterChange && !isEqual(selected, this.store.getSelected())) {
1887
- this.events.afterChange(this.store.getSelectedOptions());
1888
- }
1889
- }
1890
- getSelected() {
1891
- let options = this.store.getSelectedOptions();
1892
- if (this.settings.keepOrder) {
1893
- options = this.store.selectedOrderOptions(options);
1894
- }
1895
- return options.map((option) => option.value);
1896
- }
1897
- setSelected(values, runAfterChange = true) {
1898
- const selected = this.store.getSelected();
1899
- const options = this.store.getDataOptions();
1900
- values = Array.isArray(values) ? values : [values];
1901
- const ids = [];
1902
- for (const value of values) {
1903
- if (options.find((option) => option.id == value)) {
1904
- ids.push(value);
1905
- continue;
1906
- }
1907
- for (const option of options.filter((option) => option.value == value)) {
1908
- ids.push(option.id);
1909
- }
1910
- }
1911
- this.store.setSelectedBy('id', ids);
1912
- const data = this.store.getData();
1913
- this.select.updateOptions(data);
1914
- this.render.renderValues();
1915
- if (this.render.content.search.input.value !== '') {
1916
- this.search(this.render.content.search.input.value);
1917
- }
1918
- else {
1919
- this.render.renderOptions(data);
1920
- }
1921
- if (runAfterChange && this.events.afterChange && !isEqual(selected, this.store.getSelected())) {
1922
- this.events.afterChange(this.store.getSelectedOptions());
1923
- }
1924
- }
1925
- addOption(option) {
1926
- const selected = this.store.getSelected();
1927
- if (!this.store.getDataOptions().some((o) => { var _a; return o.value === ((_a = option.value) !== null && _a !== void 0 ? _a : option.text); })) {
1928
- this.store.addOption(option);
1929
- }
1930
- const data = this.store.getData();
1931
- this.select.updateOptions(data);
1932
- this.render.renderValues();
1933
- this.render.renderOptions(data);
1934
- if (this.events.afterChange && !isEqual(selected, this.store.getSelected())) {
1935
- this.events.afterChange(this.store.getSelectedOptions());
1936
- }
1937
- }
1938
- open() {
1939
- if (this.settings.disabled || this.settings.isOpen) {
1940
- return;
1941
- }
1942
- if (this.events.beforeOpen) {
1943
- this.events.beforeOpen();
1944
- }
1945
- this.render.open();
1946
- if (this.settings.showSearch && this.settings.focusSearch) {
1947
- this.render.searchFocus();
1948
- }
1949
- this.settings.isOpen = true;
1950
- setTimeout(() => {
1951
- if (this.events.afterOpen) {
1952
- this.events.afterOpen();
1953
- }
1954
- if (this.settings.isOpen) {
1955
- this.settings.isFullOpen = true;
1956
- }
1957
- document.addEventListener('click', this.documentClick);
1958
- }, this.settings.timeoutDelay);
1959
- if (this.settings.contentPosition === 'absolute') {
1960
- if (this.settings.intervalMove) {
1961
- clearInterval(this.settings.intervalMove);
1962
- }
1963
- this.settings.intervalMove = setInterval(this.render.moveContent.bind(this.render), 500);
1964
- }
1965
- }
1966
- close(eventType = null) {
1967
- if (!this.settings.isOpen || this.settings.alwaysOpen) {
1968
- return;
1969
- }
1970
- if (this.events.beforeClose) {
1971
- this.events.beforeClose();
1972
- }
1973
- this.render.close();
1974
- if (this.render.content.search.input.value !== '') {
1975
- this.search('');
1976
- }
1977
- this.render.mainFocus(eventType);
1978
- this.settings.isOpen = false;
1979
- this.settings.isFullOpen = false;
1980
- setTimeout(() => {
1981
- if (this.events.afterClose) {
1982
- this.events.afterClose();
1983
- }
1984
- document.removeEventListener('click', this.documentClick);
1985
- }, this.settings.timeoutDelay);
1986
- if (this.settings.intervalMove) {
1987
- clearInterval(this.settings.intervalMove);
1988
- }
1989
- }
1990
- search(value) {
1991
- if (this.render.content.search.input.value !== value) {
1992
- this.render.content.search.input.value = value;
1993
- }
1994
- if (!this.events.search) {
1995
- this.render.renderOptions(value === '' ? this.store.getData() : this.store.search(value, this.events.searchFilter));
1996
- return;
1997
- }
1998
- this.render.renderSearching();
1999
- const searchResp = this.events.search(value, this.store.getSelectedOptions());
2000
- if (searchResp instanceof Promise) {
2001
- searchResp
2002
- .then((data) => {
2003
- this.render.renderOptions(this.store.partialToFullData(data));
2004
- })
2005
- .catch((err) => {
2006
- this.render.renderError(typeof err === 'string' ? err : err.message);
2007
- });
2008
- return;
2009
- }
2010
- else if (Array.isArray(searchResp)) {
2011
- this.render.renderOptions(this.store.partialToFullData(searchResp));
2012
- }
2013
- else {
2014
- this.render.renderError('Search event must return a promise or an array of data');
2015
- }
2016
- }
2017
- destroy() {
2018
- document.removeEventListener('click', this.documentClick);
2019
- window.removeEventListener('resize', this.windowResize, false);
2020
- if (this.settings.openPosition === 'auto') {
2021
- window.removeEventListener('scroll', this.windowScroll, false);
2022
- }
2023
- document.removeEventListener('visibilitychange', this.windowVisibilityChange);
2024
- this.store.setData([]);
2025
- this.render.destroy();
2026
- this.select.destroy();
2027
- }
2028
- }
2029
-
2030
- module.exports = SlimSelect;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class N{main;placeholder;values;single;max;value;valueText;valueDelete;valueOut;deselect;deselectPath;arrow;arrowClose;arrowOpen;content;contentOpen;dirAbove;dirBelow;search;searchHighlighter;searching;addable;addablePath;list;optgroup;optgroupLabel;optgroupLabelText;optgroupActions;optgroupSelectAll;optgroupSelectAllBox;optgroupSelectAllCheck;optgroupClosable;option;optionDelete;highlighted;mainOpen;close;selected;error;disabled;hide;constructor(e){e||(e={}),this.main=e.main||"ss-main",this.placeholder=e.placeholder||"ss-placeholder",this.values=e.values||"ss-values",this.single=e.single||"ss-single",this.max=e.max||"ss-max",this.value=e.value||"ss-value",this.valueText=e.valueText||"ss-value-text",this.valueDelete=e.valueDelete||"ss-value-delete",this.valueOut=e.valueOut||"ss-value-out",this.deselect=e.deselect||"ss-deselect",this.deselectPath=e.deselectPath||"M10,10 L90,90 M10,90 L90,10",this.arrow=e.arrow||"ss-arrow",this.arrowClose=e.arrowClose||"M10,30 L50,70 L90,30",this.arrowOpen=e.arrowOpen||"M10,70 L50,30 L90,70",this.content=e.content||"ss-content",this.contentOpen=e.contentOpen||"ss-open",this.dirAbove=e.dirAbove||"ss-dir-above",this.dirBelow=e.dirBelow||"ss-dir-below",this.search=e.search||"ss-search",this.searchHighlighter=e.searchHighlighter||"ss-search-highlight",this.searching=e.searching||"ss-searching",this.addable=e.addable||"ss-addable",this.addablePath=e.addablePath||"M50,10 L50,90 M10,50 L90,50",this.list=e.list||"ss-list",this.optgroup=e.optgroup||"ss-optgroup",this.optgroupLabel=e.optgroupLabel||"ss-optgroup-label",this.optgroupLabelText=e.optgroupLabelText||"ss-optgroup-label-text",this.optgroupActions=e.optgroupActions||"ss-optgroup-actions",this.optgroupSelectAll=e.optgroupSelectAll||"ss-selectall",this.optgroupSelectAllBox=e.optgroupSelectAllBox||"M60,10 L10,10 L10,90 L90,90 L90,50",this.optgroupSelectAllCheck=e.optgroupSelectAllCheck||"M30,45 L50,70 L90,10",this.optgroupClosable=e.optgroupClosable||"ss-closable",this.option=e.option||"ss-option",this.optionDelete=e.optionDelete||"M10,10 L90,90 M10,90 L90,10",this.highlighted=e.highlighted||"ss-highlighted",this.mainOpen=e.mainOpen||"ss-open",this.close=e.close||"ss-close",this.selected=e.selected||"ss-selected",this.error=e.error||"ss-error",this.disabled=e.disabled||"ss-disabled",this.hide=e.hide||"ss-hide"}}function L(){return Math.random().toString(36).substring(2,10)}function M(p,e){function t(i,l){return l&&i&&i.classList&&i.classList.contains(l)||l&&i&&i.dataset&&i.dataset.id&&i.dataset.id===e?i:null}function s(i,l){return!i||i===document?null:t(i,l)?i:s(i.parentNode,l)}return t(p,e)||s(p,e)}function S(p,e=50,t=!1){let s;return function(...i){const l=self,a=()=>{s=null,t||p.apply(l,i)},n=t&&!s;clearTimeout(s),s=setTimeout(a,e),n&&p.apply(l,i)}}function C(p,e){return JSON.stringify(p)===JSON.stringify(e)}function P(p){const e=p.replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g,t=>"-"+t.toLowerCase());return p[0]===p[0].toUpperCase()?e.substring(1):e}class u{id;value;text;html;defaultSelected;selected;display;disabled;placeholder;class;style;data;mandatory;constructor(e){this.id=!e.id||e.id===""?L():e.id,this.value=e.value===void 0?e.text||"":e.value||"",this.text=e.text||"",this.html=e.html||"",this.defaultSelected=e.defaultSelected!==void 0?e.defaultSelected:!1,this.selected=e.selected!==void 0?e.selected:!1,this.display=e.display!==void 0?e.display:!0,this.disabled=e.disabled!==void 0?e.disabled:!1,this.mandatory=e.mandatory!==void 0?e.mandatory:!1,this.placeholder=e.placeholder!==void 0?e.placeholder:!1,this.class=e.class||"",this.style=e.style||"",this.data=e.data||{}}}class g{id;label;selectAll;selectAllText;closable;options;constructor(e){if(this.id=!e.id||e.id===""?L():e.id,this.label=e.label||"",this.selectAll=e.selectAll===void 0?!1:e.selectAll,this.selectAllText=e.selectAllText||"Select All",this.closable=e.closable||"off",this.options=[],e.options)for(const t of e.options)this.options.push(new u(t))}}class x{selectType="single";data=[];selectedOrder=[];constructor(e,t){this.selectType=e,this.setData(t)}validateDataArray(e){if(!Array.isArray(e))return new Error("Data must be an array");for(let t of e)if(t)if(t instanceof g||"label"in t){if(!("label"in t))return new Error("Optgroup must have a label");if("options"in t&&t.options)for(let s of t.options){const i=this.validateOption(s);if(i)return i}}else if(t instanceof u||"text"in t){const s=this.validateOption(t);if(s)return s}else return new Error("Data object must be a valid optgroup or option");return null}validateOption(e){return"text"in e?null:new Error("Option must have a text")}partialToFullData(e){let t=[];return e.forEach(s=>{if(s){if(s instanceof g||"label"in s){let i=[];"options"in s&&s.options&&s.options.forEach(l=>{i.push(new u(l))}),i.length>0&&t.push(new g(s))}(s instanceof u||"text"in s)&&t.push(new u(s))}}),t}setData(e,t=!1){const s=this.partialToFullData(e);if(t){const i=this.getSelectedOptions(),l=[];i.forEach(a=>{let n=!1;for(const o of s){if(o instanceof u&&o.id===a.id){n=!0;break}if(o instanceof g){for(const c of o.options)if(c.id===a.id){n=!0;break}}}n||l.push(a)}),this.data=[...l,...s]}else this.data=s;this.selectType==="single"&&this.setSelectedBy("id",this.getSelected())}getData(){return this.filter(null,!0)}getDataOptions(){return this.filter(null,!1)}addOption(e,t=!1){if(t){let s=[new u(e)];this.setData(s.concat(this.getData()))}else this.setData(this.getData().concat(new u(e)))}setSelectedBy(e,t){let s=null,i=!1;const l=[];for(let n of this.data){if(n instanceof g)for(let o of n.options){s||(s=o);let c=o[e]||"";o.selected=i?!1:t.includes(c),o.selected&&(l.push(o),this.selectType==="single"&&(i=!0))}n instanceof u&&(s||(s=n),n.selected=i?!1:t.includes(n[e]),n.selected&&(l.push(n),this.selectType==="single"&&(i=!0)))}this.selectType==="single"&&s&&!i&&(s.selected=!0,l.push(s));const a=t.map(n=>l.find(o=>o[e]===n)?.id||"");this.selectedOrder=a}getSelected(){return this.getSelectedOptions().map(e=>e.id)}getSelectedValues(){return this.getSelectedOptions().map(e=>e.value)}getSelectedOptions(){return this.filter(e=>e.selected,!1)}getOptgroupByID(e){for(let t of this.data)if(t instanceof g&&t.id===e)return t;return null}getOptionByID(e){let t=this.filter(s=>s.id===e,!1);return t.length?t[0]:null}getSelectType(){return this.selectType}getFirstOption(){let e=null;for(let t of this.data)if(t instanceof g?e=t.options[0]:t instanceof u&&(e=t),e)break;return e}search(e,t){return e=e.trim(),e===""?this.getData():this.filter(s=>t(s,e),!0)}filter(e,t){const s=[];return this.data.forEach(i=>{if(i instanceof g){let l=[];if(i.options.forEach(n=>{(!e||e(n))&&(t?l.push(new u(n)):s.push(new u(n)))}),l.length>0){let n=new g(i);n.options=l,s.push(n)}}i instanceof u&&(!e||e(i))&&s.push(new u(i))}),s}selectedOrderOptions(e){const t=[];return this.selectedOrder.forEach(s=>{const i=e.find(l=>l.id===s);i&&t.push(i)}),e.forEach(s=>{let i=!1;t.forEach(l=>{if(s.id===l.id){i=!0;return}}),i||t.push(s)}),t}}class A{settings;store;callbacks;lastSelectedOption;main;content;classes;constructor(e,t,s,i){this.store=s,this.settings=e,this.classes=t,this.callbacks=i,this.lastSelectedOption=null,this.main=this.mainDiv(),this.content=this.contentDiv(),this.updateClassStyles(),this.updateAriaAttributes(),this.settings.contentLocation&&this.settings.contentLocation.appendChild(this.content.main)}enable(){this.main.main.classList.remove(this.classes.disabled),this.main.main.setAttribute("aria-disabled","false"),this.content.search.input.disabled=!1}disable(){this.main.main.classList.add(this.classes.disabled),this.main.main.setAttribute("aria-disabled","true"),this.content.search.input.disabled=!0}open(){this.main.arrow.path.setAttribute("d",this.classes.arrowOpen),this.main.main.setAttribute("aria-expanded","true");const t=this.settings.openPosition==="up"?this.classes.dirAbove:this.classes.dirBelow;this.main.main.classList.add(t),this.content.main.classList.add(t),this.content.main.classList.add(this.classes.contentOpen),this.content.search.input.removeAttribute("aria-hidden"),this.moveContent();const s=this.store.getSelectedOptions();if(s.length){const i=s[s.length-1].id,l=this.content.list.querySelector('[data-id="'+i+'"]');l&&this.ensureElementInView(this.content.list,l)}}close(){this.main.main.setAttribute("aria-expanded","false"),this.main.arrow.path.setAttribute("d",this.classes.arrowClose),this.content.main.classList.remove(this.classes.contentOpen),this.content.search.input.setAttribute("aria-hidden","true"),this.main.main.removeAttribute("aria-activedescendant")}updateClassStyles(){if(this.main.main.className="",this.main.main.removeAttribute("style"),this.content.main.className="",this.content.main.removeAttribute("style"),this.main.main.classList.add(this.classes.main),this.content.main.classList.add(this.classes.content),this.settings.style!==""&&(this.main.main.style.cssText=this.settings.style,this.content.main.style.cssText=this.settings.style),this.settings.class.length)for(const e of this.settings.class)e.trim()!==""&&(this.main.main.classList.add(e.trim()),this.content.main.classList.add(e.trim()));(this.settings.contentPosition==="relative"||this.settings.contentPosition==="fixed")&&this.content.main.classList.add("ss-"+this.settings.contentPosition)}updateAriaAttributes(){const e=this.content.list.id;this.main.main.role="combobox",this.main.main.setAttribute("aria-haspopup","listbox"),this.main.main.setAttribute("aria-controls",e),this.main.main.setAttribute("aria-expanded","false"),this.content.list.setAttribute("role","listbox"),this.content.list.setAttribute("aria-label",this.settings.ariaLabel+" listbox"),this.settings.isMultiple&&this.content.list.setAttribute("aria-multiselectable","true"),this.content.search.input.setAttribute("aria-controls",e)}mainDiv(){const e=document.createElement("div");e.dataset.id=this.settings.id,e.setAttribute("aria-label",this.settings.ariaLabel),e.tabIndex=0,e.onkeydown=c=>{switch(c.key){case"ArrowUp":case"ArrowDown":return this.callbacks.open(),c.key==="ArrowDown"?this.highlight("down"):this.highlight("up"),!1;case"Tab":return this.callbacks.close(),!0;case"Enter":case" ":this.callbacks.open();const r=this.content.list.querySelector("."+this.classes.highlighted);return r&&r.click(),!1;case"Escape":return this.callbacks.close(),!1}return c.key.length===1&&this.callbacks.open(),!0},e.onclick=c=>{this.settings.disabled||(this.settings.isOpen?this.callbacks.close():this.callbacks.open())};const t=document.createElement("div");t.classList.add(this.classes.values),e.appendChild(t);const s=document.createElement("div");s.classList.add(this.classes.deselect);const i=this.store?.getSelectedOptions();!this.settings.allowDeselect||this.settings.isMultiple&&i&&i.length<=0?s.classList.add(this.classes.hide):s.classList.remove(this.classes.hide),s.onclick=c=>{if(c.stopPropagation(),this.settings.disabled)return;let r=!0;const h=this.store.getSelectedOptions(),f=[];if(this.callbacks.beforeChange&&(r=this.callbacks.beforeChange(f,h)===!0),r){if(this.settings.isMultiple)this.callbacks.setSelected([],!1),this.updateDeselectAll();else{const d=this.store.getFirstOption(),m=d?d.id:"";this.callbacks.setSelected(m,!1)}this.settings.closeOnSelect&&this.callbacks.close(),this.callbacks.afterChange&&this.callbacks.afterChange(this.store.getSelectedOptions())}};const l=document.createElementNS("http://www.w3.org/2000/svg","svg");l.setAttribute("viewBox","0 0 100 100");const a=document.createElementNS("http://www.w3.org/2000/svg","path");a.setAttribute("d",this.classes.deselectPath),l.appendChild(a),s.appendChild(l),e.appendChild(s);const n=document.createElementNS("http://www.w3.org/2000/svg","svg");n.classList.add(this.classes.arrow),n.setAttribute("viewBox","0 0 100 100");const o=document.createElementNS("http://www.w3.org/2000/svg","path");return o.setAttribute("d",this.classes.arrowClose),this.settings.alwaysOpen&&n.classList.add(this.classes.hide),n.appendChild(o),e.appendChild(n),{main:e,values:t,deselect:{main:s,svg:l,path:a},arrow:{main:n,path:o}}}mainFocus(e){e!=="click"&&this.main.main.focus({preventScroll:!0})}placeholder(){const e=this.store.filter(i=>i.placeholder,!1);let t=this.settings.placeholderText;e.length&&(e[0].html!==""?t=e[0].html:e[0].text!==""&&(t=e[0].text));const s=document.createElement("div");return s.classList.add(this.classes.placeholder),s.innerHTML=t,s}renderValues(){if(!this.settings.isMultiple){this.renderSingleValue();return}this.renderMultipleValues(),this.updateDeselectAll()}renderSingleValue(){const e=this.store.filter(s=>s.selected&&!s.placeholder,!1),t=e.length>0?e[0]:null;if(!t)this.main.values.innerHTML=this.placeholder().outerHTML;else{const s=document.createElement("div");s.classList.add(this.classes.single),t.html?s.innerHTML=t.html:s.innerText=t.text,this.main.values.innerHTML=s.outerHTML}!this.settings.allowDeselect||!e.length?this.main.deselect.main.classList.add(this.classes.hide):this.main.deselect.main.classList.remove(this.classes.hide)}renderMultipleValues(){let e=this.main.values.childNodes,t=this.store.filter(i=>i.selected&&i.display,!1);if(t.length===0){this.main.values.innerHTML=this.placeholder().outerHTML;return}else{const i=this.main.values.querySelector("."+this.classes.placeholder);i&&i.remove()}if(t.length>this.settings.maxValuesShown){const i=document.createElement("div");i.classList.add(this.classes.max),i.textContent=this.settings.maxValuesMessage.replace("{number}",t.length.toString()),this.main.values.innerHTML=i.outerHTML;return}else{const i=this.main.values.querySelector("."+this.classes.max);i&&i.remove()}this.settings.keepOrder&&(t=this.store.selectedOrderOptions(t));let s=[];for(let i=0;i<e.length;i++){const l=e[i],a=l.getAttribute("data-id");a&&(t.filter(o=>o.id===a,!1).length||s.push(l))}for(const i of s)i.classList.add(this.classes.valueOut),setTimeout(()=>{this.main.values.hasChildNodes()&&this.main.values.contains(i)&&this.main.values.removeChild(i)},100);e=this.main.values.childNodes;for(let i=0;i<t.length;i++){let l=!0;for(let a=0;a<e.length;a++)t[i].id===String(e[a].dataset.id)&&(l=!1);l&&(this.settings.keepOrder?this.main.values.appendChild(this.multipleValue(t[i])):e.length===0?this.main.values.appendChild(this.multipleValue(t[i])):i===0?this.main.values.insertBefore(this.multipleValue(t[i]),e[i]):e[i-1].insertAdjacentElement("afterend",this.multipleValue(t[i])))}}multipleValue(e){const t=document.createElement("div");t.classList.add(this.classes.value),t.dataset.id=e.id;const s=document.createElement("div");if(s.classList.add(this.classes.valueText),s.textContent=e.text,t.appendChild(s),!e.mandatory){const i=document.createElement("div");i.classList.add(this.classes.valueDelete),i.setAttribute("tabindex","0"),i.onclick=n=>{if(n.preventDefault(),n.stopPropagation(),this.settings.disabled)return;let o=!0;const c=this.store.getSelectedOptions(),r=c.filter(h=>h.selected&&h.id!==e.id,!0);if(!(this.settings.minSelected&&r.length<this.settings.minSelected)&&(this.callbacks.beforeChange&&(o=this.callbacks.beforeChange(r,c)===!0),o)){let h=[];for(const f of r){if(f instanceof g)for(const d of f.options)d.id&&h.push(d.id);f instanceof u&&h.push(f.id)}this.callbacks.setSelected(h,!1),this.settings.closeOnSelect&&this.callbacks.close(),this.callbacks.afterChange&&this.callbacks.afterChange(r),this.updateDeselectAll()}};const l=document.createElementNS("http://www.w3.org/2000/svg","svg");l.setAttribute("viewBox","0 0 100 100");const a=document.createElementNS("http://www.w3.org/2000/svg","path");a.setAttribute("d",this.classes.optionDelete),l.appendChild(a),i.appendChild(l),t.appendChild(i),i.onkeydown=n=>{n.key==="Enter"&&i.click()}}return t}contentDiv(){const e=document.createElement("div");e.dataset.id=this.settings.id;const t=this.searchDiv();e.appendChild(t.main);const s=this.listDiv();return e.appendChild(s),{main:e,search:t,list:s}}moveContent(){if(this.settings.contentPosition==="relative"){this.moveContentBelow();return}if(this.settings.openPosition==="down"){this.moveContentBelow();return}else if(this.settings.openPosition==="up"){this.moveContentAbove();return}this.putContent()==="up"?this.moveContentAbove():this.moveContentBelow()}searchDiv(){const e=document.createElement("div"),t=document.createElement("input"),s=document.createElement("div");e.classList.add(this.classes.search);const i={main:e,input:t};if(this.settings.showSearch||(e.classList.add(this.classes.hide),t.readOnly=!0),t.type="search",t.placeholder=this.settings.searchPlaceholder,t.tabIndex=-1,t.setAttribute("aria-label",this.settings.searchPlaceholder),t.setAttribute("aria-autocomplete","list"),t.setAttribute("autocapitalize","off"),t.setAttribute("autocomplete","off"),t.setAttribute("autocorrect","off"),t.setAttribute("aria-hidden","true"),t.oninput=S(l=>{this.callbacks.search(l.target.value)},100),t.onkeydown=l=>{switch(l.key){case"ArrowUp":case"ArrowDown":return l.key==="ArrowDown"?this.highlight("down"):this.highlight("up"),!1;case"Tab":return this.callbacks.close(),!0;case"Escape":return this.callbacks.close(),!1;case" ":const a=this.content.list.querySelector("."+this.classes.highlighted);return a?(a.click(),!1):!0;case"Enter":const n=this.content.list.querySelector("."+this.classes.highlighted);return n?(n.click(),!1):this.callbacks.addable?(s.click(),!1):!0}return!0},e.appendChild(t),this.callbacks.addable){s.classList.add(this.classes.addable);const l=document.createElementNS("http://www.w3.org/2000/svg","svg");l.setAttribute("viewBox","0 0 100 100");const a=document.createElementNS("http://www.w3.org/2000/svg","path");a.setAttribute("d",this.classes.addablePath),l.appendChild(a),s.appendChild(l),s.onclick=n=>{if(n.preventDefault(),n.stopPropagation(),!this.callbacks.addable)return;const o=this.content.search.input.value.trim();if(o===""){this.content.search.input.focus();return}const c=h=>{let f=new u(h);if(this.callbacks.addOption(f),this.settings.isMultiple){let d=this.store.getSelected();d.push(f.id),this.callbacks.setSelected(d,!0)}else this.callbacks.setSelected([f.id],!0);this.callbacks.search(""),this.settings.closeOnSelect&&setTimeout(()=>{this.callbacks.close()},100)},r=this.callbacks.addable(o);r===!1||r===void 0||r===null||(r instanceof Promise?r.then(h=>{typeof h=="string"?c({text:h,value:h}):r instanceof Error?this.renderError(r.message):c(h)}):typeof r=="string"?c({text:r,value:r}):r instanceof Error?this.renderError(r.message):c(r))},e.appendChild(s),i.addable={main:s,svg:l,path:a}}return i}searchFocus(){this.content.search.input.focus()}clearSearch(){this.content.search.input.value=""}getOptions(e=!1,t=!1,s=!1){let i="."+this.classes.option;return e&&(i+=":not(."+this.classes.placeholder+")"),t&&(i+=":not(."+this.classes.disabled+")"),s&&(i+=":not(."+this.classes.hide+")"),Array.from(this.content.list.querySelectorAll(i))}highlight(e){const t=this.getOptions(!0,!0,!0);if(t.length===0)return;if(t.length===1&&!t[0].classList.contains(this.classes.highlighted)){t[0].classList.add(this.classes.highlighted);return}let s=!1;for(const l of t)l.classList.contains(this.classes.highlighted)&&(s=!0);if(!s){for(const l of t)if(l.classList.contains(this.classes.selected)){l.classList.add(this.classes.highlighted);break}}for(let l=0;l<t.length;l++)if(t[l].classList.contains(this.classes.highlighted)){const a=t[l];a.classList.remove(this.classes.highlighted);const n=a.parentElement;if(n&&n.classList.contains(this.classes.mainOpen)){const r=n.querySelector("."+this.classes.optgroupLabel);r&&r.click()}let o=t[e==="down"?l+1<t.length?l+1:0:l-1>=0?l-1:t.length-1];o.classList.add(this.classes.highlighted),this.ensureElementInView(this.content.list,o),o.id&&this.main.main.setAttribute("aria-activedescendant",o.id);const c=o.parentElement;if(c&&c.classList.contains(this.classes.close)){const r=c.querySelector("."+this.classes.optgroupLabel);r&&r.click()}return}const i=t[e==="down"?0:t.length-1];i.classList.add(this.classes.highlighted),i.id&&this.main.main.setAttribute("aria-activedescendant",i.id),this.ensureElementInView(this.content.list,i)}listDiv(){const e=document.createElement("div");e.classList.add(this.classes.list);const t=this.settings.id+"-list";return e.id=t,e.dataset.id=t,e}renderError(e){this.content.list.innerHTML="";const t=document.createElement("div");t.classList.add(this.classes.error),t.textContent=e,this.content.list.appendChild(t)}renderSearching(){this.content.list.innerHTML="";const e=document.createElement("div");e.classList.add(this.classes.searching),e.textContent=this.settings.searchingText,this.content.list.appendChild(e)}renderOptions(e){if(this.content.list.innerHTML="",e.length===0){const s=document.createElement("div");s.classList.add(this.classes.search),this.callbacks.addable?s.innerHTML=this.settings.addableText.replace("{value}",this.content.search.input.value):s.innerHTML=this.settings.searchText,this.content.list.appendChild(s);return}this.settings.allowDeselect&&!this.settings.isMultiple&&(this.store.filter(i=>i.placeholder,!1).length||this.store.addOption(new u({text:"",value:"",selected:!1,placeholder:!0}),!0));const t=document.createDocumentFragment();for(const s of e){if(s instanceof g){const i=document.createElement("div");i.classList.add(this.classes.optgroup);const l=document.createElement("div");l.classList.add(this.classes.optgroupLabel),i.appendChild(l);const a=document.createElement("div");a.classList.add(this.classes.optgroupLabelText),a.textContent=s.label,l.appendChild(a);const n=document.createElement("div");if(n.classList.add(this.classes.optgroupActions),l.appendChild(n),this.settings.isMultiple&&s.selectAll){const o=document.createElement("div");o.classList.add(this.classes.optgroupSelectAll);let c=!0;for(const m of s.options)if(!m.selected){c=!1;break}c&&o.classList.add(this.classes.selected);const r=document.createElement("span");r.textContent=s.selectAllText,o.appendChild(r);const h=document.createElementNS("http://www.w3.org/2000/svg","svg");h.setAttribute("viewBox","0 0 100 100"),o.appendChild(h);const f=document.createElementNS("http://www.w3.org/2000/svg","path");f.setAttribute("d",this.classes.optgroupSelectAllBox),h.appendChild(f);const d=document.createElementNS("http://www.w3.org/2000/svg","path");d.setAttribute("d",this.classes.optgroupSelectAllCheck),h.appendChild(d),o.addEventListener("click",m=>{m.preventDefault(),m.stopPropagation();const b=this.store.getSelected();if(c){const w=b.filter(O=>{for(const v of s.options)if(O===v.id)return!1;return!0});this.callbacks.setSelected(w,!0);return}else{let w=s.options.map(v=>v.id).filter(v=>v!==void 0);const O=b.concat(w);for(const v of s.options)v.id&&!this.store.getOptionByID(v.id)&&this.callbacks.addOption(new u(v));this.callbacks.setSelected(O,!0);return}}),n.appendChild(o)}if(s.closable!=="off"){const o=document.createElement("div");o.classList.add(this.classes.optgroupClosable);const c=document.createElementNS("http://www.w3.org/2000/svg","svg");c.setAttribute("viewBox","0 0 100 100"),c.classList.add(this.classes.arrow),o.appendChild(c);const r=document.createElementNS("http://www.w3.org/2000/svg","path");c.appendChild(r),s.options.some(h=>h.selected)||this.content.search.input.value.trim()!==""?(o.classList.add(this.classes.mainOpen),r.setAttribute("d",this.classes.arrowOpen)):s.closable==="open"?(i.classList.add(this.classes.mainOpen),r.setAttribute("d",this.classes.arrowOpen)):s.closable==="close"&&(i.classList.add(this.classes.close),r.setAttribute("d",this.classes.arrowClose)),l.addEventListener("click",h=>{h.preventDefault(),h.stopPropagation(),i.classList.contains(this.classes.close)?(i.classList.remove(this.classes.close),i.classList.add(this.classes.mainOpen),r.setAttribute("d",this.classes.arrowOpen)):(i.classList.remove(this.classes.mainOpen),i.classList.add(this.classes.close),r.setAttribute("d",this.classes.arrowClose))}),n.appendChild(o)}i.appendChild(l);for(const o of s.options)i.appendChild(this.option(new u(o))),t.appendChild(i)}s instanceof u&&t.appendChild(this.option(s))}this.content.list.appendChild(t)}option(e){if(e.placeholder){const s=document.createElement("div");return s.classList.add(this.classes.option),s.classList.add(this.classes.hide),s}const t=document.createElement("div");return t.dataset.id=e.id,t.id=this.settings.id+"-"+e.id,t.classList.add(this.classes.option),t.setAttribute("role","option"),e.class&&e.class.split(" ").forEach(s=>{t.classList.add(s)}),e.style&&(t.style.cssText=e.style),this.settings.searchHighlight&&this.content.search.input.value.trim()!==""?t.innerHTML=this.highlightText(e.html!==""?e.html:e.text,this.content.search.input.value,this.classes.searchHighlighter):e.html!==""?t.innerHTML=e.html:t.textContent=e.text,this.settings.showOptionTooltips&&t.textContent&&t.setAttribute("title",t.textContent),e.display||t.classList.add(this.classes.hide),e.disabled&&t.classList.add(this.classes.disabled),e.selected&&this.settings.hideSelected&&t.classList.add(this.classes.hide),e.selected?(t.classList.add(this.classes.selected),t.setAttribute("aria-selected","true"),this.main.main.setAttribute("aria-activedescendant",t.id)):(t.classList.remove(this.classes.selected),t.setAttribute("aria-selected","false")),t.addEventListener("click",s=>{s.preventDefault(),s.stopPropagation();const i=this.store.getSelected(),l=s.currentTarget,a=String(l.dataset.id),n=s.ctrlKey||s.metaKey;if(e.disabled||!this.settings.isMultiple&&e.selected&&!this.settings.allowDeselect||this.settings.isMultiple&&this.settings.maxSelected<=i.length&&!e.selected||this.settings.isMultiple&&this.settings.minSelected>=i.length&&e.selected&&!n)return;let o=!1;const c=this.store.getSelectedOptions();let r=[];if(this.settings.isMultiple){const h=c.some(d=>d.id===a);if(s.shiftKey&&this.lastSelectedOption){const d=this.store.getDataOptions(),m=d.findIndex(w=>w.id===this.lastSelectedOption.id),b=d.findIndex(w=>w.id===e.id);if(m>=0&&b>=0){const w=Math.min(m,b),O=Math.max(m,b),y=d.slice(w,O+1).filter(D=>!c.find(k=>k.id===D.id));c.length+y.length<=this.settings.maxSelected?r=c.concat(y):r=c}else r=c}else n?(h?r=c.filter(d=>d.id!==a):r=c.concat(e),this.lastSelectedOption=e):(h?r=c.filter(d=>d.id!==a):r=c.concat(e),this.lastSelectedOption=e)}if(this.settings.isMultiple||(e.selected?r=[]:r=[e]),this.callbacks.beforeChange||(o=!0),this.callbacks.beforeChange&&(this.callbacks.beforeChange(r,c)===!1?o=!1:o=!0),o){this.store.getOptionByID(a)||this.callbacks.addOption(e),this.callbacks.setSelected(r.map(d=>d.id),!1);const h=s.ctrlKey||s.metaKey||s.shiftKey;this.settings.closeOnSelect&&!(this.settings.isMultiple&&h)&&this.callbacks.close(),this.callbacks.afterChange&&this.callbacks.afterChange(r)}}),t}destroy(){this.main.main.remove(),this.content.main.remove()}highlightText(e,t,s){const i=t.trim();if(i==="")return e;const l=i.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),a=document.createElement("div");a.innerHTML=e;const n=o=>{if(o.nodeType===Node.TEXT_NODE){const c=o.textContent||"",r=new RegExp("("+l+")","i");if(r.test(c)){const h=document.createElement("span");c.split(r).forEach((d,m)=>{if(d&&r.test(d)){const b=document.createElement("mark");b.className=s,b.textContent=d,h.appendChild(b)}else d&&h.appendChild(document.createTextNode(d))}),o.parentNode?.replaceChild(h,o)}}else o.nodeType===Node.ELEMENT_NODE&&Array.from(o.childNodes).forEach(c=>n(c))};return Array.from(a.childNodes).forEach(o=>n(o)),a.innerHTML}moveContentAbove(){const e=this.main.main.offsetHeight,t=this.content.main.offsetHeight;this.main.main.classList.remove(this.classes.dirBelow),this.main.main.classList.add(this.classes.dirAbove),this.content.main.classList.remove(this.classes.dirBelow),this.content.main.classList.add(this.classes.dirAbove);const s=this.main.main.getBoundingClientRect();this.content.main.style.margin="-"+(e+t-1)+"px 0px 0px 0px",this.content.main.style.top=s.top+s.height+(this.settings.contentPosition==="fixed"?0:window.scrollY)+"px",this.content.main.style.left=s.left+(this.settings.contentPosition==="fixed"?0:window.scrollX)+"px",this.content.main.style.width=s.width+"px"}moveContentBelow(){this.main.main.classList.remove(this.classes.dirAbove),this.main.main.classList.add(this.classes.dirBelow),this.content.main.classList.remove(this.classes.dirAbove),this.content.main.classList.add(this.classes.dirBelow);const e=this.main.main.getBoundingClientRect();this.content.main.style.margin="-1px 0px 0px 0px",this.settings.contentPosition!=="relative"&&(this.content.main.style.top=e.top+e.height+(this.settings.contentPosition==="fixed"?0:window.scrollY)+"px",this.content.main.style.left=e.left+(this.settings.contentPosition==="fixed"?0:window.scrollX)+"px",this.content.main.style.width=e.width+"px")}ensureElementInView(e,t){const s=e.scrollTop+e.offsetTop,i=s+e.clientHeight,l=t.offsetTop,a=l+t.clientHeight;l<s?e.scrollTop-=s-l:a>i&&(e.scrollTop+=a-i)}putContent(){const e=this.main.main.offsetHeight,t=this.main.main.getBoundingClientRect(),s=this.content.main.offsetHeight;return window.innerHeight-(t.top+e)<=s&&t.top>s?"up":"down"}updateDeselectAll(){if(!this.store||!this.settings)return;const e=this.store.getSelectedOptions(),t=e&&e.length>0,s=this.settings.isMultiple,i=this.settings.allowDeselect,l=this.main.deselect.main,a=this.classes.hide;i&&!(s&&!t)?l.classList.remove(a):l.classList.add(a)}}class E{select;onValueChange;onClassChange;onDisabledChange;onOptionsChange;listen=!1;observer=null;constructor(e){this.select=e,this.valueChange=this.valueChange.bind(this),this.select.addEventListener("change",this.valueChange,{passive:!0}),this.observer=new MutationObserver(this.observeCall.bind(this)),this.changeListen(!0)}enable(){this.select.disabled=!1}disable(){this.select.disabled=!0}hideUI(){this.select.tabIndex=-1,this.select.style.position="absolute",this.select.style.width="1px",this.select.style.height="1px",this.select.style.opacity="0",this.select.style.overflow="hidden",this.select.style.pointerEvents="none",this.select.style.margin="0",this.select.style.padding="0",this.select.style.borderWidth="0",this.select.style.clip="rect(0 0 0 0)",this.select.setAttribute("aria-hidden","true")}showUI(){this.select.removeAttribute("tabindex"),this.select.style.position="",this.select.style.width="",this.select.style.height="",this.select.style.opacity="",this.select.style.overflow="",this.select.style.pointerEvents="",this.select.style.margin="",this.select.style.padding="",this.select.style.borderWidth="",this.select.style.clip="",this.select.removeAttribute("aria-hidden")}changeListen(e){this.listen=e,e&&this.observer&&this.observer.observe(this.select,{subtree:!0,childList:!0,attributes:!0}),e||this.observer&&this.observer.disconnect()}valueChange(e){return this.listen&&this.onValueChange&&this.onValueChange(this.getSelectedOptions()),!0}observeCall(e){if(!this.listen)return;let t=!1,s=!1,i=!1;for(const l of e){if(l.target===this.select&&(l.attributeName==="disabled"&&(s=!0),l.attributeName==="class"&&(t=!0),l.type==="childList")){for(const a of Array.from(l.addedNodes))if(a.nodeName==="OPTION"&&a.value===this.select.value){this.select.dispatchEvent(new Event("change"));break}i=!0}(l.target.nodeName==="OPTGROUP"||l.target.nodeName==="OPTION")&&(i=!0)}t&&this.onClassChange&&this.onClassChange(this.select.className.split(" ")),s&&this.onDisabledChange&&(this.changeListen(!1),this.onDisabledChange(this.select.disabled),this.changeListen(!0)),i&&this.onOptionsChange&&(this.changeListen(!1),this.onOptionsChange(this.getData()),this.changeListen(!0))}getData(){let e=[];const t=this.select.childNodes;for(const s of t)s.nodeName==="OPTGROUP"&&e.push(this.getDataFromOptgroup(s)),s.nodeName==="OPTION"&&e.push(this.getDataFromOption(s));return e}getDataFromOptgroup(e){let t={id:e.id,label:e.label,selectAll:e.dataset?e.dataset.selectall==="true":!1,selectAllText:e.dataset?e.dataset.selectalltext:"Select all",closable:e.dataset?e.dataset.closable:"off",options:[]};const s=e.childNodes;for(const i of s)i.nodeName==="OPTION"&&t.options.push(this.getDataFromOption(i));return t}getDataFromOption(e){return{id:e.id,value:e.value,text:e.text,html:e.dataset&&e.dataset.html?e.dataset.html:"",defaultSelected:e.defaultSelected,selected:e.selected,display:e.style.display!=="none",disabled:e.disabled,mandatory:e.dataset?e.dataset.mandatory==="true":!1,placeholder:e.dataset.placeholder==="true",class:e.className,style:e.style.cssText,data:e.dataset}}getSelectedOptions(){let e=[];const t=this.select.childNodes;for(const s of t){if(s.nodeName==="OPTGROUP"){const i=s.childNodes;for(const l of i)if(l.nodeName==="OPTION"){const a=l;a.selected&&e.push(this.getDataFromOption(a))}}if(s.nodeName==="OPTION"){const i=s;i.selected&&e.push(this.getDataFromOption(i))}}return e}getSelectedValues(){return this.getSelectedOptions().map(e=>e.value)}setSelected(e){this.changeListen(!1);const t=this.select.childNodes;for(const s of t){if(s.nodeName==="OPTGROUP"){const l=s.childNodes;for(const a of l)if(a.nodeName==="OPTION"){const n=a;n.selected=e.includes(n.id)}}if(s.nodeName==="OPTION"){const i=s;i.selected=e.includes(i.id)}}this.changeListen(!0)}setSelectedByValue(e){this.changeListen(!1);const t=this.select.childNodes;for(const s of t){if(s.nodeName==="OPTGROUP"){const l=s.childNodes;for(const a of l)if(a.nodeName==="OPTION"){const n=a;n.selected=e.includes(n.value)}}if(s.nodeName==="OPTION"){const i=s;i.selected=e.includes(i.value)}}this.changeListen(!0)}updateSelect(e,t,s){this.changeListen(!1),e&&(this.select.dataset.id=e),t&&(this.select.style.cssText=t),s&&(this.select.className="",s.forEach(i=>{i.trim()!==""&&this.select.classList.add(i.trim())})),this.changeListen(!0)}updateOptions(e){this.changeListen(!1),this.select.innerHTML="";for(const t of e)t instanceof g&&this.select.appendChild(this.createOptgroup(t)),t instanceof u&&this.select.appendChild(this.createOption(t));this.select.dispatchEvent(new Event("change",{bubbles:!0})),this.changeListen(!0)}createOptgroup(e){const t=document.createElement("optgroup");if(t.id=e.id,t.label=e.label,e.selectAll&&(t.dataset.selectAll="true"),e.closable!=="off"&&(t.dataset.closable=e.closable),e.options)for(const s of e.options)t.appendChild(this.createOption(s));return t}createOption(e){const t=document.createElement("option");return t.id=e.id,t.value=e.value,t.textContent=e.text,e.html!==""&&t.setAttribute("data-html",e.html),t.defaultSelected=e.defaultSelected,t.selected=e.selected,e.disabled&&(t.disabled=!0),e.display||(t.style.display="none"),e.placeholder&&t.setAttribute("data-placeholder","true"),e.mandatory&&t.setAttribute("data-mandatory","true"),e.class&&e.class.split(" ").forEach(s=>{t.classList.add(s)}),e.data&&typeof e.data=="object"&&Object.keys(e.data).forEach(s=>{t.setAttribute("data-"+P(s),e.data[s])}),t}destroy(){this.changeListen(!1),this.select.removeEventListener("change",this.valueChange),this.observer&&(this.observer.disconnect(),this.observer=null),delete this.select.dataset.id,this.showUI()}}class T{id="";style="";class=[];isMultiple=!1;isOpen=!1;isFullOpen=!1;intervalMove=null;disabled;alwaysOpen;showSearch;focusSearch;ariaLabel;searchPlaceholder;searchText;searchingText;searchHighlight;closeOnSelect;contentLocation;contentPosition;openPosition;placeholderText;allowDeselect;hideSelected;keepOrder;showOptionTooltips;minSelected;maxSelected;timeoutDelay;maxValuesShown;maxValuesMessage;addableText;constructor(e){e||(e={}),this.id="ss-"+L(),this.style=e.style||"",this.class=e.class||[],this.disabled=e.disabled!==void 0?e.disabled:!1,this.alwaysOpen=e.alwaysOpen!==void 0?e.alwaysOpen:!1,this.showSearch=e.showSearch!==void 0?e.showSearch:!0,this.focusSearch=e.focusSearch!==void 0?e.focusSearch:!0,this.ariaLabel=e.ariaLabel||"Combobox",this.searchPlaceholder=e.searchPlaceholder||"Search",this.searchText=e.searchText||"No Results",this.searchingText=e.searchingText||"Searching...",this.searchHighlight=e.searchHighlight!==void 0?e.searchHighlight:!1,this.closeOnSelect=e.closeOnSelect!==void 0?e.closeOnSelect:!0,this.contentLocation=e.contentLocation||document.body,this.contentPosition=e.contentPosition||"absolute",this.openPosition=e.openPosition||"auto",this.placeholderText=e.placeholderText!==void 0?e.placeholderText:"Select Value",this.allowDeselect=e.allowDeselect!==void 0?e.allowDeselect:!1,this.hideSelected=e.hideSelected!==void 0?e.hideSelected:!1,this.keepOrder=e.keepOrder!==void 0?e.keepOrder:!1,this.showOptionTooltips=e.showOptionTooltips!==void 0?e.showOptionTooltips:!1,this.minSelected=e.minSelected||0,this.maxSelected=e.maxSelected||1e3,this.timeoutDelay=e.timeoutDelay||200,this.maxValuesShown=e.maxValuesShown||20,this.maxValuesMessage=e.maxValuesMessage||"{number} selected",this.addableText=e.addableText||'Press "Enter" to add {value}'}}class I{selectEl;settings;cssClasses;select;store;render;openTimeout=null;closeTimeout=null;events={search:void 0,searchFilter:(e,t)=>e.text.toLowerCase().indexOf(t.toLowerCase())!==-1,addable:void 0,beforeChange:void 0,afterChange:void 0,beforeOpen:void 0,afterOpen:void 0,beforeClose:void 0,afterClose:void 0};constructor(e){if(this.selectEl=typeof e.select=="string"?document.querySelector(e.select):e.select,!this.selectEl){e.events&&e.events.error&&e.events.error(new Error("Could not find select element"));return}if(this.selectEl.tagName!=="SELECT"){e.events&&e.events.error&&e.events.error(new Error("Element isnt of type select"));return}this.selectEl.dataset.ssid&&this.destroy(),this.settings=new T(e.settings),this.cssClasses=new N(e.cssClasses);const t=["beforeOpen","afterOpen","beforeClose","afterClose"];for(const n in e.events)e.events.hasOwnProperty(n)&&(t.indexOf(n)!==-1?this.events[n]=S(e.events[n],100):this.events[n]=e.events[n]);this.settings.disabled=e.settings?.disabled?e.settings.disabled:this.selectEl.disabled,this.settings.isMultiple=this.selectEl.multiple,this.settings.style=this.selectEl.style.cssText,this.settings.class=this.selectEl.className.split(" "),this.select=new E(this.selectEl),this.select.updateSelect(this.settings.id,this.settings.style,this.settings.class),this.select.hideUI(),this.select.onValueChange=n=>{this.setSelected(n.map(o=>o.id))},this.select.onClassChange=n=>{this.settings.class=n,this.render.updateClassStyles()},this.select.onDisabledChange=n=>{n?this.disable():this.enable()},this.select.onOptionsChange=n=>{this.setData(n)};const s=e.data?e.data:this.select.getData();this.store=new x(this.settings.isMultiple?"multiple":"single",s),e.data&&this.select.updateOptions(this.store.getData());const i={open:this.open.bind(this),close:this.close.bind(this),addable:this.events.addable?this.events.addable:void 0,setSelected:this.setSelected.bind(this),addOption:this.addOption.bind(this),search:this.search.bind(this),beforeChange:this.events.beforeChange,afterChange:this.events.afterChange};this.render=new A(this.settings,this.cssClasses,this.store,i),this.render.renderValues(),this.render.renderOptions(this.store.getData());const l=this.selectEl.getAttribute("aria-label"),a=this.selectEl.getAttribute("aria-labelledby");l?this.render.main.main.setAttribute("aria-label",l):a&&this.render.main.main.setAttribute("aria-labelledby",a),this.selectEl.parentNode&&this.selectEl.parentNode.insertBefore(this.render.main.main,this.selectEl.nextSibling),window.addEventListener("resize",this.windowResize,!1),this.settings.openPosition==="auto"&&window.addEventListener("scroll",this.windowScroll,!1),document.addEventListener("visibilitychange",this.windowVisibilityChange),this.settings.disabled&&this.disable(),this.settings.alwaysOpen&&this.open(),this.selectEl.slim=this}enable(){this.settings.disabled=!1,this.select.enable(),this.render.enable()}disable(){this.settings.disabled=!0,this.select.disable(),this.render.disable()}getData(){return this.store.getData()}setData(e){const t=this.store.getSelected(),s=this.store.validateDataArray(e);if(s){this.events.error&&this.events.error(s);return}this.store.setData(e);const i=this.store.getData();this.select.updateOptions(i),this.render.renderValues(),this.render.renderOptions(i),this.events.afterChange&&!C(t,this.store.getSelected())&&this.events.afterChange(this.store.getSelectedOptions())}getSelected(){let e=this.store.getSelectedOptions();return this.settings.keepOrder&&(e=this.store.selectedOrderOptions(e)),e.map(t=>t.value)}setSelected(e,t=!0){const s=this.store.getSelected(),i=this.store.getDataOptions();e=Array.isArray(e)?e:[e];const l=[];for(const n of e){if(i.find(o=>o.id==n)){l.push(n);continue}for(const o of i.filter(c=>c.value==n))l.push(o.id)}this.store.setSelectedBy("id",l);const a=this.store.getData();this.select.updateOptions(a),this.render.renderValues(),this.render.content.search.input.value!==""?this.search(this.render.content.search.input.value):this.render.renderOptions(a),t&&this.events.afterChange&&!C(s,this.store.getSelected())&&this.events.afterChange(this.store.getSelectedOptions())}addOption(e){const t=this.store.getSelected();this.store.getDataOptions().some(i=>i.value===(e.value??e.text))||this.store.addOption(e);const s=this.store.getData();this.select.updateOptions(s),this.render.renderValues(),this.render.renderOptions(s),this.events.afterChange&&!C(t,this.store.getSelected())&&this.events.afterChange(this.store.getSelectedOptions())}open(){this.settings.disabled||this.settings.isOpen||(this.events.beforeOpen&&this.events.beforeOpen(),this.render.open(),this.settings.showSearch&&this.settings.focusSearch&&this.render.searchFocus(),this.settings.isOpen=!0,this.openTimeout=setTimeout(()=>{this.events.afterOpen&&this.events.afterOpen(),this.settings.isOpen&&(this.settings.isFullOpen=!0),document.addEventListener("click",this.documentClick)},this.settings.timeoutDelay),this.settings.contentPosition==="absolute"&&(this.settings.intervalMove&&clearInterval(this.settings.intervalMove),this.settings.intervalMove=setInterval(this.render.moveContent.bind(this.render),500)))}close(e=null){!this.settings.isOpen||this.settings.alwaysOpen||(this.events.beforeClose&&this.events.beforeClose(),this.render.close(),this.render.clearSearch(),this.render.mainFocus(e),this.settings.isOpen=!1,this.settings.isFullOpen=!1,this.closeTimeout=setTimeout(()=>{this.events.afterClose&&this.events.afterClose(),document.removeEventListener("click",this.documentClick)},this.settings.timeoutDelay),this.settings.intervalMove&&clearInterval(this.settings.intervalMove))}search(e){if(this.render.content.search.input.value!==e&&(this.render.content.search.input.value=e),!this.events.search){const s=e===""?this.store.getData():this.store.search(e,this.events.searchFilter);this.render.renderOptions(s);return}this.render.renderSearching();const t=this.events.search(e,this.store.getSelectedOptions());if(t instanceof Promise){t.then(s=>{this.store.setData(s,!0),this.select.updateOptions(this.store.getData()),this.render.renderOptions(this.store.getData())}).catch(s=>{this.render.renderError(typeof s=="string"?s:s.message)});return}else Array.isArray(t)?(this.store.setData(t,!0),this.select.updateOptions(this.store.getData()),this.render.renderOptions(this.store.getData())):this.render.renderError("Search event must return a promise or an array of data")}destroy(){this.openTimeout&&(clearTimeout(this.openTimeout),this.openTimeout=null),this.closeTimeout&&(clearTimeout(this.closeTimeout),this.closeTimeout=null),this.settings.intervalMove&&(clearInterval(this.settings.intervalMove),this.settings.intervalMove=null),document.removeEventListener("click",this.documentClick),window.removeEventListener("resize",this.windowResize,!1),this.settings.openPosition==="auto"&&window.removeEventListener("scroll",this.windowScroll,!1),document.removeEventListener("visibilitychange",this.windowVisibilityChange),this.store.setData([]),this.render.destroy(),this.select.destroy()}windowResize=S(()=>{!this.settings.isOpen&&!this.settings.isFullOpen||this.render.moveContent()});windowScroll=S(()=>{!this.settings.isOpen&&!this.settings.isFullOpen||this.render.moveContent()});documentClick=e=>{this.settings.isOpen&&e.target&&!M(e.target,this.settings.id)&&this.close(e.type)};windowVisibilityChange=()=>{document.hidden&&this.close()}}exports.Optgroup=g;exports.Option=u;exports.Render=A;exports.Select=E;exports.Settings=T;exports.Store=x;exports.default=I;