j1-template 2023.4.1 → 2023.4.2

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