slim-select 2.5.1 → 2.7.0

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