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