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