slim-select 2.6.0 → 2.7.1

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