slim-select 2.6.0 → 2.7.1

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