vue3-extended-multiselect 0.2.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.eslintrc.js +35 -29
  2. package/.github/workflows/publish.yml +41 -41
  3. package/CHANGELOG.md +91 -65
  4. package/LICENSE +21 -21
  5. package/README.md +261 -262
  6. package/babel.config.js +3 -2
  7. package/index.js +2 -2
  8. package/jest.config.js +57 -57
  9. package/package.json +47 -46
  10. package/src/assets/base-arrow.svg +70 -70
  11. package/src/assets/cancel.svg +34 -34
  12. package/src/assets/circle-arrow.svg +7 -7
  13. package/src/assets/double-arrow.svg +39 -39
  14. package/src/assets/inner-arrow.svg +8 -8
  15. package/src/assets/loader.svg +5 -5
  16. package/src/assets/triangle-arrow.svg +72 -72
  17. package/src/assets/triangle-circle-arrow.svg +38 -38
  18. package/src/assets/wide-arrow.svg +72 -72
  19. package/src/components/ExtendedMultiselectCancel.vue +148 -145
  20. package/src/components/ExtendedMultiselectInput.vue +617 -617
  21. package/src/components/ExtendedMultiselectLoader.vue +182 -176
  22. package/src/components/ExtendedMultiselectMultiple.vue +295 -291
  23. package/src/components/ExtendedMultiselectOptions.vue +1091 -1088
  24. package/src/components/ExtendedMultiselectToggle.vue +261 -261
  25. package/src/components/Vue3ExtendedMultiselect.vue +1661 -1661
  26. package/src/composition/cancel.js +33 -28
  27. package/src/composition/image-path.js +17 -0
  28. package/src/composition/labels.js +39 -39
  29. package/src/composition/preselected-options.js +66 -66
  30. package/src/composition/search-value.js +33 -33
  31. package/src/composition/sizes.js +42 -42
  32. package/src/composition/toggle.js +33 -31
  33. package/src/events/emitter.js +3 -0
  34. package/src/sets/sets.js +99 -99
  35. package/src/styles/animations.css +18 -18
  36. package/src/styles/main.css +432 -432
  37. package/src/styles/variables.css +47 -48
  38. package/tests/unit/events.spec.js +135 -135
  39. package/tests/unit/props.spec.js +1040 -1040
  40. package/tests/utils/mount.js +37 -37
  41. package/tests/utils/utils.js +20 -20
  42. package/typings/vue3-extended-multiselect.d.ts +42 -42
  43. package/.eslintignore +0 -2
@@ -1,1089 +1,1092 @@
1
- <template>
2
- <div
3
- ref="optionsWrapper"
4
- tabindex="-1"
5
- :class="classes"
6
- :style="styles"
7
- >
8
- <slot name="listHeader"></slot>
9
- <div
10
- v-if="showCreateNewOptionBlock"
11
- :class="optionHighlightClasses(null)"
12
- @mousedown="createNewOption"
13
- >
14
- {{ searchState.searchValue }}
15
- </div>
16
- <slot
17
- name="moreThanLimit"
18
- v-if="maxOptionsWereSelected"
19
- >
20
- <span>Maximum limit of selected options was achieved</span>
21
- </slot>
22
- <slot
23
- name="lessThanLimit"
24
- v-if="minOptionsWereNotSelected"
25
- >
26
- <span>Minimum amount of selected options was not achieved</span>
27
- </slot>
28
- <div class="extended__multiselect-options_container">
29
- <div
30
- role="listbox"
31
- v-for="(option, index) in availableOptions"
32
- :key="index"
33
- >
34
- <div
35
- v-if="option"
36
- :aria-setsize="availableOptions.length"
37
- :aria-posinset="index"
38
- :aria-labelledby="`option-label-${index}`"
39
- :aria-disabled="optionIsDisabled(option)"
40
- :class="optionHighlightClasses(option)"
41
- :style="optionHeightByProps"
42
- :enter-locator="index"
43
- :role="optionCreateRole(option)"
44
- @click.stop="selectOption(option, $event)"
45
- @keypress.stop="selectOption(option, $event)"
46
- @mousedown="triggerOptionBeforeSelection"
47
- @mouseenter.stop="setEnterIndex"
48
- >
49
- <slot
50
- name="option"
51
- :option="option"
52
- :create-custom-option-label="createCustomOptionLabel"
53
- >
54
- <span :id="`option-label-${index}`">
55
- {{ optionCreateLabel(option) }}
56
- </span>
57
- </slot>
58
- <div
59
- class="extended__multiselect-marker"
60
- v-if="showMarker && showCurrentMarker(option)"
61
- >
62
- <slot
63
- name="marker"
64
- :option="option"
65
- >
66
- <div
67
- :class="markerShapeClass(option)"
68
- :style="markerShapeMargin"
69
- >
70
- </div>
71
- </slot>
72
- </div>
73
- </div>
74
- </div>
75
- </div>
76
- <slot
77
- name="noResults"
78
- v-if="emptySearchResult"
79
- >
80
- <span>No results were found by search</span>
81
- </slot>
82
- <slot
83
- name="noOptions"
84
- v-if="emptyOptionsList"
85
- >
86
- <span>Options list is empty</span>
87
- </slot>
88
- <slot name="listFooter"></slot>
89
- </div>
90
- </template>
91
-
92
- <script setup>
93
- import {
94
- computed,
95
- defineProps,
96
- inject,
97
- onBeforeMount,
98
- onMounted,
99
- ref,
100
- toRefs,
101
- } from "vue";
102
-
103
- import emitter from 'tiny-emitter/instance';
104
-
105
- import usePreselectedOptions from "../composition/preselected-options";
106
- import useLabels from "../composition/labels";
107
-
108
- import { UnionPropType } from "../sets/sets";
109
-
110
- const props = defineProps({
111
- /**
112
- * Determines whether to immediately select just now created option
113
- * @property {boolean} autoSelectCreatedOption
114
- */
115
- autoSelectCreatedOption: {
116
- type: Boolean,
117
- required: true,
118
- },
119
-
120
- /**
121
- * Determines whether to show noResults slot
122
- * and block with new option's text
123
- * @property {boolean} createOnTheGo
124
- */
125
- createOnTheGo: {
126
- type: Boolean,
127
- required: true,
128
- },
129
-
130
- /**
131
- * Determines whether to emit clear event
132
- * by option selection
133
- * @property {boolean} clearBySelectWhenMultiple
134
- */
135
- clearBySelectWhenMultiple: {
136
- type: Boolean,
137
- required: true,
138
- },
139
-
140
- /**
141
- * Determines whether to define thematic class for
142
- * every option
143
- * @property {boolean} highlightOptions
144
- */
145
- highlightOptions: {
146
- type: Boolean,
147
- required: true,
148
- },
149
-
150
- /**
151
- * Determines whether to use functionality of
152
- * multiple select
153
- * @property {boolean} multiple
154
- */
155
- multiple: {
156
- type: Boolean,
157
- required: true,
158
- },
159
-
160
- /**
161
- * Determines whether to show selected options
162
- * in options list
163
- * @property {boolean} selectedOptionsShown
164
- */
165
- selectedOptionsShown: {
166
- type: Boolean,
167
- required: true,
168
- },
169
-
170
- /**
171
- * Determines whether to log warnings about incorrect props
172
- * @property {boolean} showInsertWarnings
173
- */
174
- showInsertWarnings: {
175
- type: Boolean,
176
- required: true,
177
- },
178
-
179
- /**
180
- * Determines whether to show marker slot
181
- * @property {boolean} showMarker
182
- */
183
- showMarker: {
184
- type: Boolean,
185
- required: true,
186
- },
187
-
188
- /**
189
- * Defines class of options list placement after expanse
190
- * @property {string} chosenToggleAppearanceSide
191
- */
192
- chosenToggleAppearanceSide: {
193
- type: String,
194
- required: true,
195
- },
196
-
197
- /**
198
- * Determines type of option that will be created
199
- * @property {string} createOptionType
200
- */
201
- createOptionType: {
202
- type: String,
203
- required: true,
204
- },
205
-
206
- /**
207
- * Sets labels for options of type "object"
208
- * or Array instances which length/keys length equals 0
209
- * @property {string} emptyObjectsPlaceholder
210
- */
211
- emptyObjectsPlaceholder: {
212
- type: String,
213
- required: true,
214
- },
215
-
216
- /**
217
- * Determines field of option which will be displayed as label
218
- * @property {string} label
219
- */
220
- label: {
221
- type: String,
222
- required: true,
223
- },
224
-
225
- /**
226
- * Current theme of extended-multiselect
227
- * used in class definition
228
- * @property {string} themeType
229
- */
230
- themeType: {
231
- type: String,
232
- required: true,
233
- },
234
-
235
- /**
236
- * Defines "height" css-property of every option element
237
- * @property {number} anyOptionWrapperBlockHeight
238
- */
239
- anyOptionWrapperBlockHeight: {
240
- type: Number,
241
- required: true,
242
- },
243
-
244
- /**
245
- * Determines maximal limit of selected options
246
- * @property {number|null} maxOptionsCount
247
- */
248
- maxOptionsCount: {
249
- type: Number,
250
- default: null,
251
- },
252
-
253
- /**
254
- * Determines minimal limit of selected options
255
- * @property {number|null} minOptionsCount
256
- */
257
- minOptionsCount: {
258
- type: Number,
259
- default: null,
260
- },
261
-
262
- /**
263
- * Defines value of "max-height" css-property of options list
264
- * @property {number} toggleMaxHeight
265
- */
266
- toggleMaxHeight: {
267
- type: Number,
268
- required: true,
269
- },
270
-
271
- /**
272
- * Defines value of "min-height" css-property of options list
273
- * @property {number} toggleMinHeight
274
- */
275
- toggleMinHeight: {
276
- type: Number,
277
- default: null,
278
- },
279
-
280
- /**
281
- * List of fields for new options of type "object" created by user
282
- * if "createOnTheGo" prop equals true
283
- * Each field will have basic value of "searchValue" prop
284
- * @property {Array} createOptionFields
285
- */
286
- createOptionFields: {
287
- type: Array,
288
- required: true,
289
- },
290
-
291
- /**
292
- * Searches for options with listed primitive values
293
- * and disables it
294
- * @property {Array} disabledPrimitiveOptionsConverted
295
- */
296
- disabledPrimitiveOptionsConverted: {
297
- type: Array,
298
- required: true,
299
- },
300
-
301
- /**
302
- * Unfiltered options list
303
- * @property {Array} options
304
- */
305
- options: {
306
- type: Array,
307
- required: true,
308
- },
309
-
310
- /**
311
- * Defines value of "padding" css-property of every option
312
- * @property {Array} optionsPadding
313
- */
314
- optionsPadding: {
315
- type: Array,
316
- required: true,
317
- },
318
-
319
- /**
320
- * List of options which should be selected by default
321
- * if "multiple" prop equals true
322
- * @property {Array} preselectedOptions
323
- */
324
- preselectedOptions: {
325
- type: Array,
326
- required: true,
327
- },
328
-
329
- /**
330
- * List of options filtered by maximal available options amount
331
- * @property {Array} restrictedOptions
332
- */
333
- restrictedOptions: {
334
- type: Array,
335
- required: true,
336
- },
337
-
338
- /**
339
- * List of selected options or list with single selected option
340
- * if "multiple" prop equals false
341
- * @property {Array} selectedOptions
342
- */
343
- selectedOptions: {
344
- type: Array,
345
- required: true,
346
- },
347
-
348
- /**
349
- * List of key names that in combination with "enter" key
350
- * should cancel option selection
351
- * @property {Array} specialKeysBlock
352
- */
353
- specialKeysBlock: {
354
- type: Array,
355
- required: true,
356
- },
357
-
358
- /**
359
- * Creates custom label for every option in options list
360
- * @property {Function} createCustomOptionLabel
361
- */
362
- createCustomOptionLabel: {
363
- type: Function,
364
- required: true,
365
- },
366
-
367
- /**
368
- * Option which should be selected by default
369
- * if "multiple" prop equals false
370
- * @property {UnionPropType|null} preselectedOption
371
- */
372
- preselectedOption: {
373
- type: UnionPropType,
374
- default: null,
375
- },
376
- });
377
-
378
- const searchState = inject("searchState");
379
- const setSearchValue = inject("setSearchValue");
380
- const setSearchPattern = inject("setSearchPattern");
381
-
382
- const atopWithScroll = ref(null);
383
- const enterIndex = ref(null);
384
- const heightFromMounted = ref(null);
385
- const underWithScroll = ref(null);
386
-
387
- /**
388
- * Element references
389
- */
390
- const optionsWrapper = ref(null);
391
-
392
- const {
393
- anyOptionWrapperBlockHeight,
394
- autoSelectCreatedOption,
395
- chosenToggleAppearanceSide,
396
- clearBySelectWhenMultiple,
397
- createOnTheGo,
398
- createCustomOptionLabel,
399
- createOptionFields,
400
- createOptionType,
401
- disabledPrimitiveOptionsConverted,
402
- emptyObjectsPlaceholder,
403
- highlightOptions,
404
- label,
405
- maxOptionsCount,
406
- minOptionsCount,
407
- multiple,
408
- options,
409
- optionsPadding,
410
- restrictedOptions,
411
- selectedOptions,
412
- selectedOptionsShown,
413
- showInsertWarnings,
414
- showMarker,
415
- specialKeysBlock,
416
- themeType,
417
- toggleMaxHeight,
418
- toggleMinHeight,
419
- } = toRefs(props);
420
-
421
- const { createLabel, optionTypeRestrictor } = usePreselectedOptions(
422
- label,
423
- emptyObjectsPlaceholder,
424
- showInsertWarnings,
425
- );
426
- const { optionCreateLabel } = useLabels(createCustomOptionLabel, emptyObjectsPlaceholder);
427
-
428
- /**
429
- * List of options filtered by "disabled" and "selected" signs
430
- * also by equality to null and undefined primitive values
431
- * or instance declarations
432
- * @function
433
- * @returns {Array} options
434
- */
435
- const availableOptions = computed(() => {
436
- let filteredOptions = restrictedOptions.value.filter((option) => {
437
- const availableOptionType = optionTypeRestrictor(option);
438
-
439
- if (!option || !availableOptionType) return false;
440
-
441
- if (!lookForSimpleOptions(option)) return false;
442
-
443
- if (selectedOptionsShown.value) return true;
444
-
445
- return lookForObjectOptions(option);
446
- });
447
-
448
- let finalOptionsList = filteredOptions;
449
-
450
- if (searchState.searchPattern) {
451
- finalOptionsList = filterBySearchPattern(filteredOptions);
452
- }
453
-
454
- return finalOptionsList;
455
- });
456
-
457
- /**
458
- * Defines a list of options list classes
459
- * @function
460
- * @returns {Array} classes
461
- */
462
- const classes = computed(() => {
463
- let theme = [];
464
-
465
- switch(themeType.value) {
466
- case "basic":
467
- theme.push("extended__multiselect-options");
468
- break;
469
- case "slate-grey":
470
- theme.push("extended__multiselect-options-slate-grey");
471
- break;
472
- case "slate-blue":
473
- theme.push("extended__multiselect-options-slate-blue");
474
- break;
475
- case "teal":
476
- theme.push("extended__multiselect-options-teal");
477
- break;
478
- case "strict":
479
- theme.push("extended__multiselect-options-strict");
480
- break;
481
- default:
482
- theme.push("extended__multiselect-options");
483
- }
484
-
485
- if (showMarker.value) {
486
- theme.push("extended__multiselect-options--marker");
487
- }
488
-
489
- if (chosenToggleAppearanceSide.value === "atop") {
490
- theme.push("extended__multiselect-options--atop");
491
- }
492
-
493
- return theme;
494
- });
495
-
496
- /**
497
- * Determines whether given options list is empty
498
- * @function
499
- * @returns {boolean} emptiness
500
- */
501
- const emptyOptionsList = computed(() => {
502
- return (!options.value.length || !options.value.filter((option) => !!option).length
503
- || !availableOptions.value.length) && !(createOnTheGo.value && searchState.searchValue);
504
- });
505
-
506
- /**
507
- * Determines whether list of internal search results is empty
508
- * @function
509
- * @returns {boolean} emptiness
510
- */
511
- const emptySearchResult = computed(() => {
512
- return !availableOptions.value.length && !createOnTheGo.value && searchState.searchValue;
513
- });
514
-
515
- /**
516
- * Defines styles for "min-heigt", "max-height"
517
- * and "top" css-properties of options wrapper
518
- * @function
519
- * @returns {Object} styles
520
- */
521
- const heightFromProps = computed(() => {
522
- let heightRecord = {};
523
-
524
- if (chosenToggleAppearanceSide.value === "atop") {
525
- Object.defineProperty(heightRecord, "top", {
526
- enumerable: true,
527
- value: `-${toggleMaxHeight.value}px`,
528
- });
529
- }
530
-
531
- Object.defineProperty(heightRecord, "maxHeight", {
532
- enumerable: true,
533
- value: `${toggleMaxHeight.value}px`,
534
- });
535
-
536
- if (toggleMinHeight.value) {
537
- if (toggleMinHeight.value > toggleMaxHeight.value) {
538
- if (showInsertWarnings.value) {
539
- let errorMessage = "vue-extended-multiselect: «toggleMinHeight» property can not be greater than «toggleMaxHeight» property.";
540
- errorMessage += "«toggleMaxHeight» property was reset to default value of 400 pixels.";
541
- console.warn(errorMessage);
542
- }
543
-
544
- return Object.keys(heightRecord).length ? heightRecord : null;
545
- }
546
-
547
- Object.defineProperty(heightRecord, "minHeight", {
548
- enumerable: true,
549
- value: `${toggleMinHeight.value}px`,
550
- });
551
- }
552
-
553
- return Object.keys(heightRecord).length ? heightRecord : null;
554
- });
555
-
556
- /**
557
- * Determines whether maximal amount of options has been selected
558
- * @function
559
- * @returns {boolean} reaching
560
- */
561
- const maxOptionsWereSelected = computed(() => {
562
- return maxOptionsCount.value && multiple.value && selectedOptions.value.length === maxOptionsCount.value;
563
- });
564
-
565
- /**
566
- * Defines styles for "margin-top" css-property of marker element
567
- * @function
568
- * @returns {Object} styles
569
- */
570
- const markerShapeMargin = computed(() => {
571
- return { marginTop: `${Math.floor(anyOptionWrapperBlockHeight.value) / 30}px` };
572
- });
573
-
574
- /**
575
- * Determines whether minimal amount of options has not been selected
576
- * @function
577
- * @returns {boolean} reaching
578
- */
579
- const minOptionsWereNotSelected = computed(() => {
580
- return minOptionsCount.value && multiple.value && selectedOptions.value.length < minOptionsCount.value;
581
- });
582
-
583
- /**
584
- * Defines styles for "padding" and "height" css-property
585
- * of every option element
586
- * @function
587
- * @returns {Object} styles
588
- */
589
- const optionHeightByProps = computed(() => {
590
- let height = {
591
- height: `${anyOptionWrapperBlockHeight.value}px`,
592
- };
593
-
594
- if (optionsPadding.value.length) {
595
- const padding = {
596
- paddingTop: optionsPadding.value[0] ? `${optionsPadding.value[0]}px` : null,
597
- paddingLeft: optionsPadding.value[1] ? `${optionsPadding.value[1]}px` : null,
598
- paddingBottom: optionsPadding.value[2] ? `${optionsPadding.value[2]}px` : null,
599
- paddingRight: optionsPadding.value[3] ? `${optionsPadding.value[3]}px` : null,
600
- };
601
-
602
- return Object.assign(height, padding);
603
- }
604
-
605
- return height;
606
- });
607
-
608
- /**
609
- * Determines whether to show element with new custom option
610
- * @function
611
- * @returns {boolean} display
612
- */
613
- const showCreateNewOptionBlock = computed(() => {
614
- return createOnTheGo.value && !availableOptions.value.length && searchState.searchValue;
615
- });
616
-
617
- /**
618
- * Summarizes styles for options wrapper in combined collection
619
- * @function
620
- * @returns {Array} styles
621
- */
622
-
623
- const styles = computed(() => {
624
- return [
625
- heightFromProps.value,
626
- heightFromMounted.value,
627
- atopWithScroll.value,
628
- underWithScroll.value
629
- ];
630
- });
631
-
632
- /**
633
- * Restricts repeated selection of option
634
- * @function
635
- * @param {string} calculatedOptionLabel - displayed label of option
636
- */
637
- const compareWithSelected = (calculatedOptionLabel) => {
638
- return !selectedOptions.value.find((selectedOption) => {
639
- let calculatedSelectedOptionLabel;
640
-
641
- if (typeof selectedOption === "object") {
642
- if (Object.keys(selectedOption).length === 0) {
643
- calculatedSelectedOptionLabel = emptyObjectsPlaceholder.value;
644
- } else if (Array.isArray(selectedOption)) {
645
- calculatedSelectedOptionLabel = selectedOption.join(", ");
646
- } else {
647
- calculatedSelectedOptionLabel = typeof selectedOption[label.value] === "object"
648
- ? JSON.stringify(selectedOption[label.value])
649
- : selectedOption[label.value].toString();
650
- }
651
- } else {
652
- calculatedSelectedOptionLabel = selectedOption.toString();
653
- }
654
-
655
- return calculatedSelectedOptionLabel === calculatedOptionLabel;
656
- });
657
- };
658
-
659
- /**
660
- * Creates new option by users input
661
- * if "createOnTheGo" prop equals true
662
- * @function
663
- * @emits extended:create-option
664
- */
665
- const createNewOption = () => {
666
- let newOption;
667
-
668
- switch(createOptionType.value) {
669
- case "primitive":
670
- newOption = searchState.searchValue;
671
- break;
672
- case "array":
673
- newOption = [searchState.searchValue];
674
- break;
675
- case "object":
676
- newOption = createObjectOption();
677
- break;
678
- }
679
-
680
- options.value.unshift(newOption);
681
- emitter.emit("extended:create-option", newOption);
682
-
683
- if (autoSelectCreatedOption.value) {
684
- selectOption(newOption, { target: { id: "extended__multiselect" } });
685
- setSearchValue(null);
686
- setSearchPattern(null);
687
- }
688
- };
689
-
690
- /**
691
- * Creates option of type object
692
- * if "createOptionType" equals "object"
693
- * @function
694
- * @returns {UnionPropType} option
695
- */
696
- const createObjectOption = () => {
697
- if (!createOptionFields.value) {
698
- if (showInsertWarnings.value) {
699
- console.warn("vue-extended-multiselect: if option should be object — option fields should not be empty");
700
- }
701
-
702
- return searchState.searchValue;
703
- }
704
-
705
- const newOption = {};
706
-
707
- createOptionFields.value.forEach((field) => {
708
- Object.defineProperty(newOption, field, {
709
- enumerable: true,
710
- writable: true,
711
- configurable: true,
712
- value: `${field}-${searchState.searchValue}`,
713
- });
714
- });
715
-
716
- return newOption;
717
- };
718
-
719
- /**
720
- * Recognises if option is disabled
721
- * @function
722
- * @param {UnionPropType} option - option to recognize
723
- * @returns {boolean} prohibition
724
- */
725
- const defineDisabledOption = (option) => {
726
- if (typeof option === "object" && !Array.isArray(option)) return false;
727
-
728
- let convertedOption;
729
- if (Array.isArray(option) && !option.length) {
730
- convertedOption = emptyObjectsPlaceholder.value;
731
- } else {
732
- convertedOption = option;
733
- }
734
-
735
- if (disabledPrimitiveOptionsConverted.value.includes(convertedOption)) return true;
736
-
737
- return false;
738
- };
739
-
740
- /**
741
- * Filters options list by pattern of internal search for available options
742
- * @function
743
- * @param {Array} optionsList - pre-filtered list of oprions
744
- * @returns {boolean} filter
745
- */
746
- const filterBySearchPattern = (optionsList) => {
747
- return optionsList.filter((option) => {
748
- if (typeof option === "object") {
749
- if (Array.isArray(option)) {
750
- return searchState.searchPattern.test(option.length ? option.join(", ") : emptyObjectsPlaceholder.value);
751
- }
752
- return searchState.searchPattern.test(option[label.value].toString());
753
- } else {
754
- return searchState.searchPattern.test(option.toString());
755
- }
756
- });
757
- };
758
-
759
- /**
760
- * Blocks keys pressed in combination with "enter" key
761
- * for not to select options by them
762
- * @function
763
- * @param {KeyboardEvent} keyboardEvent - KeyboardEvent instance
764
- * @returns {boolean} block
765
- */
766
- const keyBlocker = (keyboardEvent) => {
767
- if (!keyboardEvent) return false;
768
-
769
- const altKeyBlock = specialKeysBlock.value.includes("alt");
770
- const ctrlKeyBlock = specialKeysBlock.value.includes("ctrl");
771
- const shiftKeyBlock = specialKeysBlock.value.includes("shift");
772
-
773
- if (altKeyBlock && keyboardEvent.altKey) return true;
774
- if (ctrlKeyBlock && keyboardEvent.ctrlKey) return true;
775
- if (shiftKeyBlock && keyboardEvent.shiftKey) return true;
776
-
777
- return false;
778
- }
779
-
780
- /**
781
- * Searchs for already selected options in particular
782
- * for options of type "object"
783
- * @function
784
- * @param {UnionPropType} option - option for search
785
- * @returns {boolean} selected
786
- */
787
- const lookForObjectOptions = (option) => {
788
- if (typeof option === "object") {
789
- let calculatedOptionLabel;
790
-
791
- if (Array.isArray(option) && !!option.length) {
792
- calculatedOptionLabel = option.join(", ");
793
- } else {
794
- const hasLabel = Object.getOwnPropertyNames(option).includes("label");
795
-
796
- if (hasLabel) {
797
- calculatedOptionLabel = typeof option[label.value] === "object"
798
- ? JSON.stringify(option[label.value])
799
- : option[label.value].toString();
800
- } else {
801
- calculatedOptionLabel = JSON.stringify(option);
802
- }
803
-
804
- if (Object.keys(option).length === 0) {
805
- calculatedOptionLabel = emptyObjectsPlaceholder.value;
806
- }
807
- }
808
-
809
- return compareWithSelected(calculatedOptionLabel);
810
- } else {
811
- return !selectedOptions.value.includes(option);
812
- }
813
- };
814
-
815
- /**
816
- * Searches for already selected options in particular
817
- * for options of primitive types or of type "function"
818
- * @function
819
- * @param {UnionPropType} option - option for search
820
- * @returns {boolean} selected
821
- */
822
- const lookForSimpleOptions = (option) => {
823
- if (typeof option === "object" && Object.keys(option).length === 0) {
824
- const emptyObject = selectedOptions.value.find((emptyObject) => {
825
- return typeof emptyObject === "object" && !Object.keys(emptyObject).length;
826
- });
827
-
828
- if (!emptyObject || selectedOptionsShown.value) return true;
829
-
830
- return false;
831
- }
832
-
833
- if (
834
- typeof option === "object"
835
- && !Array.isArray(option)
836
- && !Object.keys(option).includes(label.value)
837
- ) return false;
838
-
839
- return true;
840
- };
841
-
842
- /**
843
- * Defines a class for "marker" slot content
844
- * @function
845
- * @param {UnionPropType} option - option in options list
846
- * @returns {string} class
847
- */
848
- const markerShapeClass = (option) => {
849
- if (lookForSimpleOptions(option) && lookForObjectOptions(option)) {
850
- return "extended__multiselect-marker-shape--selected";
851
- }
852
-
853
- return "extended__multiselect-marker-shape";
854
- }
855
-
856
- /**
857
- * Creates role of every option element for accessible applications
858
- * if it needed
859
- * @function
860
- * @param {UnionPropType} option - each option from options list
861
- * @returns {string|null} role
862
- */
863
- const optionCreateRole = (option) => {
864
- if (optionIsDisabled(option)) return null;
865
- if (!option) return null;
866
-
867
- return "option";
868
- };
869
-
870
- /**
871
- * Determines whether option is disabled
872
- * @function
873
- * @param {UnionPropType} option - each option from options list
874
- * @returns {boolean} blocking
875
- */
876
- const optionIsDisabled = (option) => {
877
- if (option.disabled || defineDisabledOption(option)) return true;
878
-
879
- return false;
880
- }
881
-
882
- /**
883
- * Defines various classes for options dependent from props
884
- * @function
885
- * @param {UnionPropType} option - each option from options list
886
- * @returns {Array} classes
887
- */
888
- const optionHighlightClasses = (option) => {
889
- let theme = [];
890
-
891
- if (highlightOptions.value) {
892
- switch(themeType.value) {
893
- case "basic":
894
- theme.push("extended__multiselect-options_option");
895
- break;
896
- case "slate-grey":
897
- theme.push("extended__multiselect-options_option-slate-grey");
898
- break;
899
- case "slate-blue":
900
- theme.push("extended__multiselect-options_option-slate-blue");
901
- break;
902
- case "teal":
903
- theme.push("extended__multiselect-options_option-teal");
904
- break;
905
- case "strict":
906
- theme.push("extended__multiselect-options_option-strict");
907
- break;
908
- default:
909
- theme.push("extended__multiselect-options_option");
910
- }
911
- }
912
-
913
- if (showMarker) {
914
- theme.push("extended__multiselect-options_option--marker");
915
- }
916
-
917
- if (option && option.disabled) {
918
- theme.push("extended__multiselect-options_option--disabled");
919
- }
920
-
921
- if (disabledPrimitiveOptionsConverted.value && option && defineDisabledOption(option)) {
922
- theme.push("extended__multiselect-options_option--disabled");
923
- }
924
-
925
- return theme;
926
- };
927
-
928
- /**
929
- * Selects option
930
- * @function
931
- * @emits extended:deselect-option
932
- * @param {UnionPropType} option - option to select
933
- * @param {MouseEvent|KeyboardEvent} clickEvent - MouseEvent or KeyboardEvent instance
934
- */
935
- const selectOption = (option, clickEvent) => {
936
- emitter.emit("extended:trigger-selection", false);
937
-
938
- if (!clickEvent) return;
939
- if (keyBlocker(clickEvent)) return;
940
- if (maxOptionsWereSelected.value) return;
941
- if (option.disabled || defineDisabledOption(option)) return;
942
- if (
943
- clickEvent.target.id === "extended__multiselect-toggle"
944
- || clickEvent.target.id === "extended__multiselect-cancel"
945
- ) return;
946
-
947
- if (selectedOptionsShown.value) {
948
- const optionDeselected = lookForObjectOptions(option);
949
-
950
- if (!optionDeselected) {
951
- emitter.emit("extended:deselect-option");
952
- return;
953
- }
954
- }
955
-
956
- const isObject = typeof option === "object" && !Array.isArray(option);
957
-
958
- if (isObject && !Object.keys(option).includes(label.value)) {
959
- if (showInsertWarnings.value) {
960
- console.warn("vue-extended-multiselect: wrong label type inserted");
961
- }
962
- }
963
-
964
- selectOptionEmitter(option);
965
- };
966
-
967
- /**
968
- * Emits select events or clear event
969
- * if prop "clearBySelectWhenMultiple" equals true
970
- * @function
971
- * @emits extended:clear-field
972
- * @emits extended:select-option
973
- * @param {UnionPropType} - option to select
974
- */
975
- const selectOptionEmitter = (option) => {
976
- const isObjectOrArray = typeof option === "object";
977
- const label = createLabel(isObjectOrArray, option);
978
-
979
- if (multiple.value) {
980
- if (clearBySelectWhenMultiple.value) {
981
- emitter.emit("extended:clear-field");
982
- }
983
-
984
- emitter.emit("extended:select-option", {
985
- label,
986
- option,
987
- });
988
- } else {
989
- emitter.emit("extended:select-option", {
990
- label,
991
- option,
992
- });
993
- }
994
- };
995
-
996
- /**
997
- * Determines an option which will be selected
998
- * by pressing "enter" key on it
999
- * @function
1000
- * @param {MouseEvent} event - MouseEvent instance
1001
- */
1002
- const setEnterIndex = (event) => {
1003
- enterIndex.value = event.target.getAttribute("enter-locator");
1004
- };
1005
-
1006
- /**
1007
- * Determines whether to show marker beside option by its selection
1008
- * @function
1009
- * @param {UnionPropType} option - each option from options list
1010
- * @returns {boolean} display
1011
- */
1012
- const showCurrentMarker = (option) => {
1013
- if (!lookForSimpleOptions(option) || !lookForObjectOptions(option)) {
1014
- return true;
1015
- }
1016
-
1017
- return false;
1018
- };
1019
-
1020
- /**
1021
- * Emits event which listener will add special class
1022
- * to search field before option selection
1023
- * @function
1024
- * @emits extended:trigger-selection
1025
- */
1026
- const triggerOptionBeforeSelection = () => {
1027
- const fieldWasShown = document.querySelector(".extended__multiselect-input");
1028
-
1029
- if (fieldWasShown) {
1030
- emitter.emit("extended:trigger-selection", true);
1031
- }
1032
- };
1033
-
1034
- /**
1035
- * "onBeforeMount" hook of ExtendedMultiselectOptions component
1036
- * @listens extended:select-enter
1037
- * @listens extended:reset-index
1038
- */
1039
- onBeforeMount(() => {
1040
- const objectFields = restrictedOptions.value.filter((option) => {
1041
- if (!option) return false;
1042
-
1043
- return typeof option === "object" && typeof option[label.value] === "object";
1044
- });
1045
-
1046
- if (objectFields.length && showInsertWarnings.value) {
1047
- console.warn("vue-extended-multiselect: «label» property can not be of type «object»");
1048
- }
1049
-
1050
- emitter.on("extended:select-enter", (keyboardEvent) => {
1051
- if (!enterIndex.value) return;
1052
-
1053
- const option = this.availableOptions[this.enterIndex];
1054
- this.selectOption(option, keyboardEvent);
1055
- });
1056
-
1057
- emitter.on("extended:reset-index", () => {
1058
- enterIndex.value = null;
1059
- });
1060
- });
1061
-
1062
- /**
1063
- * "onMounted" hook of ExtendedMultiselectOptions component
1064
- */
1065
- onMounted(() => {
1066
- const offsetHeight = optionsWrapper.value.offsetHeight;
1067
- const scrollHeight = optionsWrapper.value.scrollHeight;
1068
-
1069
- if (chosenToggleAppearanceSide.value === "atop") {
1070
- if (scrollHeight > offsetHeight) {
1071
- atopWithScroll.value = {
1072
- borderTopRightRadius: 0,
1073
- };
1074
- }
1075
-
1076
- if (toggleMaxHeight.value > offsetHeight) {
1077
- heightFromMounted.value = {
1078
- top: `-${offsetHeight}px`,
1079
- };
1080
- }
1081
- } else {
1082
- if (scrollHeight > offsetHeight) {
1083
- underWithScroll.value = {
1084
- borderBottomRightRadius: 0,
1085
- };
1086
- }
1087
- }
1088
- });
1
+ <template>
2
+ <div
3
+ ref="optionsWrapper"
4
+ tabindex="-1"
5
+ :class="classes"
6
+ :style="styles"
7
+ >
8
+ <slot name="listHeader"></slot>
9
+ <div
10
+ v-if="showCreateNewOptionBlock"
11
+ :class="optionHighlightClasses(null)"
12
+ @mousedown="createNewOption"
13
+ >
14
+ {{ searchState.searchValue }}
15
+ </div>
16
+ <slot
17
+ name="moreThanLimit"
18
+ v-if="maxOptionsWereSelected"
19
+ >
20
+ <span>Maximum limit of selected options was achieved</span>
21
+ </slot>
22
+ <slot
23
+ name="lessThanLimit"
24
+ v-if="minOptionsWereNotSelected"
25
+ >
26
+ <span>Minimum amount of selected options was not achieved</span>
27
+ </slot>
28
+ <div class="extended__multiselect-options_container">
29
+ <div
30
+ role="listbox"
31
+ v-for="(option, index) in availableOptions"
32
+ :key="index"
33
+ >
34
+ <div
35
+ v-if="option"
36
+ :aria-setsize="availableOptions.length"
37
+ :aria-posinset="index"
38
+ :aria-labelledby="`option-label-${index}`"
39
+ :aria-disabled="optionIsDisabled(option)"
40
+ :class="optionHighlightClasses(option)"
41
+ :style="optionHeightByProps"
42
+ :enter-locator="index"
43
+ :role="optionCreateRole(option)"
44
+ @click.stop="selectOption(option, $event)"
45
+ @keypress.stop="selectOption(option, $event)"
46
+ @mousedown="triggerOptionBeforeSelection"
47
+ @mouseenter.stop="setEnterIndex"
48
+ >
49
+ <slot
50
+ name="option"
51
+ :option="option"
52
+ :create-custom-option-label="createCustomOptionLabel"
53
+ >
54
+ <div class="extended__multiselect-marker">
55
+ <slot
56
+ name="marker"
57
+ :option="option"
58
+ >
59
+ <div
60
+ v-if="showMarker && showCurrentMarker(option)"
61
+ :class="markerShapeClass(option)"
62
+ :style="markerShapeMargin"
63
+ >
64
+ </div>
65
+ <div
66
+ v-else-if="showMarker"
67
+ class="extended__multiselect-marker-shape-only"
68
+ >
69
+ </div>
70
+ </slot>
71
+ </div>
72
+ <span :id="`option-label-${index}`">
73
+ {{ optionCreateLabel(option) }}
74
+ </span>
75
+ </slot>
76
+ </div>
77
+ </div>
78
+ </div>
79
+ <slot
80
+ name="noResults"
81
+ v-if="emptySearchResult"
82
+ >
83
+ <span>No results were found by search</span>
84
+ </slot>
85
+ <slot
86
+ name="noOptions"
87
+ v-if="emptyOptionsList"
88
+ >
89
+ <span>Options list is empty</span>
90
+ </slot>
91
+ <slot name="listFooter"></slot>
92
+ </div>
93
+ </template>
94
+
95
+ <script setup>
96
+ import {
97
+ computed,
98
+ defineProps,
99
+ inject,
100
+ onBeforeMount,
101
+ onMounted,
102
+ ref,
103
+ toRefs,
104
+ } from "vue";
105
+
106
+ import emitter from '../events/emitter';
107
+
108
+ import usePreselectedOptions from "../composition/preselected-options";
109
+ import useLabels from "../composition/labels";
110
+
111
+ import { UnionPropType } from "../sets/sets";
112
+
113
+ const props = defineProps({
114
+ /**
115
+ * Determines whether to immediately select just now created option
116
+ * @property {boolean} autoSelectCreatedOption
117
+ */
118
+ autoSelectCreatedOption: {
119
+ type: Boolean,
120
+ required: true,
121
+ },
122
+
123
+ /**
124
+ * Determines whether to show noResults slot
125
+ * and block with new option's text
126
+ * @property {boolean} createOnTheGo
127
+ */
128
+ createOnTheGo: {
129
+ type: Boolean,
130
+ required: true,
131
+ },
132
+
133
+ /**
134
+ * Determines whether to emit clear event
135
+ * by option selection
136
+ * @property {boolean} clearBySelectWhenMultiple
137
+ */
138
+ clearBySelectWhenMultiple: {
139
+ type: Boolean,
140
+ required: true,
141
+ },
142
+
143
+ /**
144
+ * Determines whether to define thematic class for
145
+ * every option
146
+ * @property {boolean} highlightOptions
147
+ */
148
+ highlightOptions: {
149
+ type: Boolean,
150
+ required: true,
151
+ },
152
+
153
+ /**
154
+ * Determines whether to use functionality of
155
+ * multiple select
156
+ * @property {boolean} multiple
157
+ */
158
+ multiple: {
159
+ type: Boolean,
160
+ required: true,
161
+ },
162
+
163
+ /**
164
+ * Determines whether to show selected options
165
+ * in options list
166
+ * @property {boolean} selectedOptionsShown
167
+ */
168
+ selectedOptionsShown: {
169
+ type: Boolean,
170
+ required: true,
171
+ },
172
+
173
+ /**
174
+ * Determines whether to log warnings about incorrect props
175
+ * @property {boolean} showInsertWarnings
176
+ */
177
+ showInsertWarnings: {
178
+ type: Boolean,
179
+ required: true,
180
+ },
181
+
182
+ /**
183
+ * Determines whether to show marker slot
184
+ * @property {boolean} showMarker
185
+ */
186
+ showMarker: {
187
+ type: Boolean,
188
+ required: true,
189
+ },
190
+
191
+ /**
192
+ * Defines class of options list placement after expanse
193
+ * @property {string} chosenToggleAppearanceSide
194
+ */
195
+ chosenToggleAppearanceSide: {
196
+ type: String,
197
+ required: true,
198
+ },
199
+
200
+ /**
201
+ * Determines type of option that will be created
202
+ * @property {string} createOptionType
203
+ */
204
+ createOptionType: {
205
+ type: String,
206
+ required: true,
207
+ },
208
+
209
+ /**
210
+ * Sets labels for options of type "object"
211
+ * or Array instances which length/keys length equals 0
212
+ * @property {string} emptyObjectsPlaceholder
213
+ */
214
+ emptyObjectsPlaceholder: {
215
+ type: String,
216
+ required: true,
217
+ },
218
+
219
+ /**
220
+ * Determines field of option which will be displayed as label
221
+ * @property {string} label
222
+ */
223
+ label: {
224
+ type: String,
225
+ required: true,
226
+ },
227
+
228
+ /**
229
+ * Current theme of extended-multiselect
230
+ * used in class definition
231
+ * @property {string} themeType
232
+ */
233
+ themeType: {
234
+ type: String,
235
+ required: true,
236
+ },
237
+
238
+ /**
239
+ * Defines "height" css-property of every option element
240
+ * @property {number} anyOptionWrapperBlockHeight
241
+ */
242
+ anyOptionWrapperBlockHeight: {
243
+ type: Number,
244
+ required: true,
245
+ },
246
+
247
+ /**
248
+ * Determines maximal limit of selected options
249
+ * @property {number|null} maxOptionsCount
250
+ */
251
+ maxOptionsCount: {
252
+ type: Number,
253
+ default: null,
254
+ },
255
+
256
+ /**
257
+ * Determines minimal limit of selected options
258
+ * @property {number|null} minOptionsCount
259
+ */
260
+ minOptionsCount: {
261
+ type: Number,
262
+ default: null,
263
+ },
264
+
265
+ /**
266
+ * Defines value of "max-height" css-property of options list
267
+ * @property {number} toggleMaxHeight
268
+ */
269
+ toggleMaxHeight: {
270
+ type: Number,
271
+ required: true,
272
+ },
273
+
274
+ /**
275
+ * Defines value of "min-height" css-property of options list
276
+ * @property {number} toggleMinHeight
277
+ */
278
+ toggleMinHeight: {
279
+ type: Number,
280
+ default: null,
281
+ },
282
+
283
+ /**
284
+ * List of fields for new options of type "object" created by user
285
+ * if "createOnTheGo" prop equals true
286
+ * Each field will have basic value of "searchValue" prop
287
+ * @property {Array} createOptionFields
288
+ */
289
+ createOptionFields: {
290
+ type: Array,
291
+ required: true,
292
+ },
293
+
294
+ /**
295
+ * Searches for options with listed primitive values
296
+ * and disables it
297
+ * @property {Array} disabledPrimitiveOptionsConverted
298
+ */
299
+ disabledPrimitiveOptionsConverted: {
300
+ type: Array,
301
+ required: true,
302
+ },
303
+
304
+ /**
305
+ * Unfiltered options list
306
+ * @property {Array} options
307
+ */
308
+ options: {
309
+ type: Array,
310
+ required: true,
311
+ },
312
+
313
+ /**
314
+ * Defines value of "padding" css-property of every option
315
+ * @property {Array} optionsPadding
316
+ */
317
+ optionsPadding: {
318
+ type: Array,
319
+ required: true,
320
+ },
321
+
322
+ /**
323
+ * List of options which should be selected by default
324
+ * if "multiple" prop equals true
325
+ * @property {Array} preselectedOptions
326
+ */
327
+ preselectedOptions: {
328
+ type: Array,
329
+ required: true,
330
+ },
331
+
332
+ /**
333
+ * List of options filtered by maximal available options amount
334
+ * @property {Array} restrictedOptions
335
+ */
336
+ restrictedOptions: {
337
+ type: Array,
338
+ required: true,
339
+ },
340
+
341
+ /**
342
+ * List of selected options or list with single selected option
343
+ * if "multiple" prop equals false
344
+ * @property {Array} selectedOptions
345
+ */
346
+ selectedOptions: {
347
+ type: Array,
348
+ required: true,
349
+ },
350
+
351
+ /**
352
+ * List of key names that in combination with "enter" key
353
+ * should cancel option selection
354
+ * @property {Array} specialKeysBlock
355
+ */
356
+ specialKeysBlock: {
357
+ type: Array,
358
+ required: true,
359
+ },
360
+
361
+ /**
362
+ * Creates custom label for every option in options list
363
+ * @property {Function} createCustomOptionLabel
364
+ */
365
+ createCustomOptionLabel: {
366
+ type: Function,
367
+ required: true,
368
+ },
369
+
370
+ /**
371
+ * Option which should be selected by default
372
+ * if "multiple" prop equals false
373
+ * @property {UnionPropType|null} preselectedOption
374
+ */
375
+ preselectedOption: {
376
+ type: UnionPropType,
377
+ default: null,
378
+ },
379
+ });
380
+
381
+ const searchState = inject("searchState");
382
+ const setSearchValue = inject("setSearchValue");
383
+ const setSearchPattern = inject("setSearchPattern");
384
+
385
+ const atopWithScroll = ref(null);
386
+ const enterIndex = ref(null);
387
+ const heightFromMounted = ref(null);
388
+ const underWithScroll = ref(null);
389
+
390
+ /**
391
+ * Element references
392
+ */
393
+ const optionsWrapper = ref(null);
394
+
395
+ const {
396
+ anyOptionWrapperBlockHeight,
397
+ autoSelectCreatedOption,
398
+ chosenToggleAppearanceSide,
399
+ clearBySelectWhenMultiple,
400
+ createOnTheGo,
401
+ createCustomOptionLabel,
402
+ createOptionFields,
403
+ createOptionType,
404
+ disabledPrimitiveOptionsConverted,
405
+ emptyObjectsPlaceholder,
406
+ highlightOptions,
407
+ label,
408
+ maxOptionsCount,
409
+ minOptionsCount,
410
+ multiple,
411
+ options,
412
+ optionsPadding,
413
+ restrictedOptions,
414
+ selectedOptions,
415
+ selectedOptionsShown,
416
+ showInsertWarnings,
417
+ showMarker,
418
+ specialKeysBlock,
419
+ themeType,
420
+ toggleMaxHeight,
421
+ toggleMinHeight,
422
+ } = toRefs(props);
423
+
424
+ const { createLabel, optionTypeRestrictor } = usePreselectedOptions(
425
+ label,
426
+ emptyObjectsPlaceholder,
427
+ showInsertWarnings,
428
+ );
429
+ const { optionCreateLabel } = useLabels(createCustomOptionLabel, emptyObjectsPlaceholder);
430
+
431
+ /**
432
+ * List of options filtered by "disabled" and "selected" signs
433
+ * also by equality to null and undefined primitive values
434
+ * or instance declarations
435
+ * @function
436
+ * @returns {Array} options
437
+ */
438
+ const availableOptions = computed(() => {
439
+ let filteredOptions = restrictedOptions.value.filter((option) => {
440
+ const availableOptionType = optionTypeRestrictor(option);
441
+
442
+ if (!option || !availableOptionType) return false;
443
+
444
+ if (!lookForSimpleOptions(option)) return false;
445
+
446
+ if (selectedOptionsShown.value) return true;
447
+
448
+ return lookForObjectOptions(option);
449
+ });
450
+
451
+ let finalOptionsList = filteredOptions;
452
+
453
+ if (searchState.searchPattern) {
454
+ finalOptionsList = filterBySearchPattern(filteredOptions);
455
+ }
456
+
457
+ return finalOptionsList;
458
+ });
459
+
460
+ /**
461
+ * Defines a list of options list classes
462
+ * @function
463
+ * @returns {Array} classes
464
+ */
465
+ const classes = computed(() => {
466
+ let theme = [];
467
+
468
+ switch(themeType.value) {
469
+ case "basic":
470
+ theme.push("extended__multiselect-options");
471
+ break;
472
+ case "slate-grey":
473
+ theme.push("extended__multiselect-options-slate-grey");
474
+ break;
475
+ case "slate-blue":
476
+ theme.push("extended__multiselect-options-slate-blue");
477
+ break;
478
+ case "teal":
479
+ theme.push("extended__multiselect-options-teal");
480
+ break;
481
+ case "strict":
482
+ theme.push("extended__multiselect-options-strict");
483
+ break;
484
+ default:
485
+ theme.push("extended__multiselect-options");
486
+ }
487
+
488
+ if (showMarker.value) {
489
+ theme.push("extended__multiselect-options--marker");
490
+ }
491
+
492
+ if (chosenToggleAppearanceSide.value === "atop") {
493
+ theme.push("extended__multiselect-options--atop");
494
+ }
495
+
496
+ return theme;
497
+ });
498
+
499
+ /**
500
+ * Determines whether given options list is empty
501
+ * @function
502
+ * @returns {boolean} emptiness
503
+ */
504
+ const emptyOptionsList = computed(() => {
505
+ return (!options.value.length || !options.value.filter((option) => !!option).length
506
+ || !availableOptions.value.length) && !(createOnTheGo.value && searchState.searchValue);
507
+ });
508
+
509
+ /**
510
+ * Determines whether list of internal search results is empty
511
+ * @function
512
+ * @returns {boolean} emptiness
513
+ */
514
+ const emptySearchResult = computed(() => {
515
+ return !availableOptions.value.length && !createOnTheGo.value && searchState.searchValue;
516
+ });
517
+
518
+ /**
519
+ * Defines styles for "min-heigt", "max-height"
520
+ * and "top" css-properties of options wrapper
521
+ * @function
522
+ * @returns {Object} styles
523
+ */
524
+ const heightFromProps = computed(() => {
525
+ let heightRecord = {};
526
+
527
+ if (chosenToggleAppearanceSide.value === "atop") {
528
+ Object.defineProperty(heightRecord, "top", {
529
+ enumerable: true,
530
+ value: `-${toggleMaxHeight.value}px`,
531
+ });
532
+ }
533
+
534
+ Object.defineProperty(heightRecord, "maxHeight", {
535
+ enumerable: true,
536
+ value: `${toggleMaxHeight.value}px`,
537
+ });
538
+
539
+ if (toggleMinHeight.value) {
540
+ if (toggleMinHeight.value > toggleMaxHeight.value) {
541
+ if (showInsertWarnings.value) {
542
+ let errorMessage = "vue-extended-multiselect: «toggleMinHeight» property can not be greater than «toggleMaxHeight» property.";
543
+ errorMessage += "«toggleMaxHeight» property was reset to default value of 400 pixels.";
544
+ console.warn(errorMessage);
545
+ }
546
+
547
+ return Object.keys(heightRecord).length ? heightRecord : null;
548
+ }
549
+
550
+ Object.defineProperty(heightRecord, "minHeight", {
551
+ enumerable: true,
552
+ value: `${toggleMinHeight.value}px`,
553
+ });
554
+ }
555
+
556
+ return Object.keys(heightRecord).length ? heightRecord : null;
557
+ });
558
+
559
+ /**
560
+ * Determines whether maximal amount of options has been selected
561
+ * @function
562
+ * @returns {boolean} reaching
563
+ */
564
+ const maxOptionsWereSelected = computed(() => {
565
+ return maxOptionsCount.value && multiple.value && selectedOptions.value.length === maxOptionsCount.value;
566
+ });
567
+
568
+ /**
569
+ * Defines styles for "margin-top" css-property of marker element
570
+ * @function
571
+ * @returns {Object} styles
572
+ */
573
+ const markerShapeMargin = computed(() => {
574
+ return { marginTop: "2px" };
575
+ });
576
+
577
+ /**
578
+ * Determines whether minimal amount of options has not been selected
579
+ * @function
580
+ * @returns {boolean} reaching
581
+ */
582
+ const minOptionsWereNotSelected = computed(() => {
583
+ return minOptionsCount.value && multiple.value && selectedOptions.value.length < minOptionsCount.value;
584
+ });
585
+
586
+ /**
587
+ * Defines styles for "padding" and "height" css-property
588
+ * of every option element
589
+ * @function
590
+ * @returns {Object} styles
591
+ */
592
+ const optionHeightByProps = computed(() => {
593
+ let height = {
594
+ height: `${anyOptionWrapperBlockHeight.value}px`,
595
+ };
596
+
597
+ if (optionsPadding.value.length) {
598
+ const padding = {
599
+ paddingTop: optionsPadding.value[0] ? `${optionsPadding.value[0]}px` : null,
600
+ paddingLeft: optionsPadding.value[1] ? `${optionsPadding.value[1]}px` : null,
601
+ paddingBottom: optionsPadding.value[2] ? `${optionsPadding.value[2]}px` : null,
602
+ paddingRight: optionsPadding.value[3] ? `${optionsPadding.value[3]}px` : null,
603
+ };
604
+
605
+ return Object.assign(height, padding);
606
+ }
607
+
608
+ return height;
609
+ });
610
+
611
+ /**
612
+ * Determines whether to show element with new custom option
613
+ * @function
614
+ * @returns {boolean} display
615
+ */
616
+ const showCreateNewOptionBlock = computed(() => {
617
+ return createOnTheGo.value && !availableOptions.value.length && searchState.searchValue;
618
+ });
619
+
620
+ /**
621
+ * Summarizes styles for options wrapper in combined collection
622
+ * @function
623
+ * @returns {Array} styles
624
+ */
625
+
626
+ const styles = computed(() => {
627
+ return [
628
+ heightFromProps.value,
629
+ heightFromMounted.value,
630
+ atopWithScroll.value,
631
+ underWithScroll.value
632
+ ];
633
+ });
634
+
635
+ /**
636
+ * Restricts repeated selection of option
637
+ * @function
638
+ * @param {string} calculatedOptionLabel - displayed label of option
639
+ */
640
+ const compareWithSelected = (calculatedOptionLabel) => {
641
+ return !selectedOptions.value.find((selectedOption) => {
642
+ let calculatedSelectedOptionLabel;
643
+
644
+ if (typeof selectedOption === "object") {
645
+ if (Object.keys(selectedOption).length === 0) {
646
+ calculatedSelectedOptionLabel = emptyObjectsPlaceholder.value;
647
+ } else if (Array.isArray(selectedOption)) {
648
+ calculatedSelectedOptionLabel = selectedOption.join(", ");
649
+ } else {
650
+ calculatedSelectedOptionLabel = typeof selectedOption[label.value] === "object"
651
+ ? JSON.stringify(selectedOption[label.value])
652
+ : selectedOption[label.value].toString();
653
+ }
654
+ } else {
655
+ calculatedSelectedOptionLabel = selectedOption.toString();
656
+ }
657
+
658
+ return calculatedSelectedOptionLabel === calculatedOptionLabel;
659
+ });
660
+ };
661
+
662
+ /**
663
+ * Creates new option by users input
664
+ * if "createOnTheGo" prop equals true
665
+ * @function
666
+ * @emits extended:create-option
667
+ */
668
+ const createNewOption = () => {
669
+ let newOption;
670
+
671
+ switch(createOptionType.value) {
672
+ case "primitive":
673
+ newOption = searchState.searchValue;
674
+ break;
675
+ case "array":
676
+ newOption = [searchState.searchValue];
677
+ break;
678
+ case "object":
679
+ newOption = createObjectOption();
680
+ break;
681
+ }
682
+
683
+ options.value.unshift(newOption);
684
+ emitter.emit("extended:create-option", newOption);
685
+
686
+ if (autoSelectCreatedOption.value) {
687
+ selectOption(newOption, { target: { id: "extended__multiselect" } });
688
+ setSearchValue(null);
689
+ setSearchPattern(null);
690
+ }
691
+ };
692
+
693
+ /**
694
+ * Creates option of type object
695
+ * if "createOptionType" equals "object"
696
+ * @function
697
+ * @returns {UnionPropType} option
698
+ */
699
+ const createObjectOption = () => {
700
+ if (!createOptionFields.value) {
701
+ if (showInsertWarnings.value) {
702
+ console.warn("vue-extended-multiselect: if option should be object — option fields should not be empty");
703
+ }
704
+
705
+ return searchState.searchValue;
706
+ }
707
+
708
+ const newOption = {};
709
+
710
+ createOptionFields.value.forEach((field) => {
711
+ Object.defineProperty(newOption, field, {
712
+ enumerable: true,
713
+ writable: true,
714
+ configurable: true,
715
+ value: `${field}-${searchState.searchValue}`,
716
+ });
717
+ });
718
+
719
+ return newOption;
720
+ };
721
+
722
+ /**
723
+ * Recognises if option is disabled
724
+ * @function
725
+ * @param {UnionPropType} option - option to recognize
726
+ * @returns {boolean} prohibition
727
+ */
728
+ const defineDisabledOption = (option) => {
729
+ if (typeof option === "object" && !Array.isArray(option)) return false;
730
+
731
+ let convertedOption;
732
+ if (Array.isArray(option) && !option.length) {
733
+ convertedOption = emptyObjectsPlaceholder.value;
734
+ } else {
735
+ convertedOption = option;
736
+ }
737
+
738
+ if (disabledPrimitiveOptionsConverted.value.includes(convertedOption)) return true;
739
+
740
+ return false;
741
+ };
742
+
743
+ /**
744
+ * Filters options list by pattern of internal search for available options
745
+ * @function
746
+ * @param {Array} optionsList - pre-filtered list of oprions
747
+ * @returns {boolean} filter
748
+ */
749
+ const filterBySearchPattern = (optionsList) => {
750
+ return optionsList.filter((option) => {
751
+ if (typeof option === "object") {
752
+ if (Array.isArray(option)) {
753
+ return searchState.searchPattern.test(option.length ? option.join(", ") : emptyObjectsPlaceholder.value);
754
+ }
755
+ return searchState.searchPattern.test(option[label.value].toString());
756
+ } else {
757
+ return searchState.searchPattern.test(option.toString());
758
+ }
759
+ });
760
+ };
761
+
762
+ /**
763
+ * Blocks keys pressed in combination with "enter" key
764
+ * for not to select options by them
765
+ * @function
766
+ * @param {KeyboardEvent} keyboardEvent - KeyboardEvent instance
767
+ * @returns {boolean} block
768
+ */
769
+ const keyBlocker = (keyboardEvent) => {
770
+ if (!keyboardEvent) return false;
771
+
772
+ const altKeyBlock = specialKeysBlock.value.includes("alt");
773
+ const ctrlKeyBlock = specialKeysBlock.value.includes("ctrl");
774
+ const shiftKeyBlock = specialKeysBlock.value.includes("shift");
775
+
776
+ if (altKeyBlock && keyboardEvent.altKey) return true;
777
+ if (ctrlKeyBlock && keyboardEvent.ctrlKey) return true;
778
+ if (shiftKeyBlock && keyboardEvent.shiftKey) return true;
779
+
780
+ return false;
781
+ }
782
+
783
+ /**
784
+ * Searchs for already selected options in particular
785
+ * for options of type "object"
786
+ * @function
787
+ * @param {UnionPropType} option - option for search
788
+ * @returns {boolean} selected
789
+ */
790
+ const lookForObjectOptions = (option) => {
791
+ if (typeof option === "object") {
792
+ let calculatedOptionLabel;
793
+
794
+ if (Array.isArray(option) && !!option.length) {
795
+ calculatedOptionLabel = option.join(", ");
796
+ } else {
797
+ const hasLabel = Object.getOwnPropertyNames(option).includes("label");
798
+
799
+ if (hasLabel) {
800
+ calculatedOptionLabel = typeof option[label.value] === "object"
801
+ ? JSON.stringify(option[label.value])
802
+ : option[label.value].toString();
803
+ } else {
804
+ calculatedOptionLabel = JSON.stringify(option);
805
+ }
806
+
807
+ if (Object.keys(option).length === 0) {
808
+ calculatedOptionLabel = emptyObjectsPlaceholder.value;
809
+ }
810
+ }
811
+
812
+ return compareWithSelected(calculatedOptionLabel);
813
+ } else {
814
+ return !selectedOptions.value.includes(option);
815
+ }
816
+ };
817
+
818
+ /**
819
+ * Searches for already selected options in particular
820
+ * for options of primitive types or of type "function"
821
+ * @function
822
+ * @param {UnionPropType} option - option for search
823
+ * @returns {boolean} selected
824
+ */
825
+ const lookForSimpleOptions = (option) => {
826
+ if (typeof option === "object" && Object.keys(option).length === 0) {
827
+ const emptyObject = selectedOptions.value.find((emptyObject) => {
828
+ return typeof emptyObject === "object" && !Object.keys(emptyObject).length;
829
+ });
830
+
831
+ if (!emptyObject || selectedOptionsShown.value) return true;
832
+
833
+ return false;
834
+ }
835
+
836
+ if (
837
+ typeof option === "object"
838
+ && !Array.isArray(option)
839
+ && !Object.keys(option).includes(label.value)
840
+ ) return false;
841
+
842
+ return true;
843
+ };
844
+
845
+ /**
846
+ * Defines a class for "marker" slot content
847
+ * @function
848
+ * @param {UnionPropType} option - option in options list
849
+ * @returns {string} class
850
+ */
851
+ const markerShapeClass = (option) => {
852
+ if (lookForSimpleOptions(option) && lookForObjectOptions(option)) {
853
+ return "extended__multiselect-marker-shape--selected";
854
+ }
855
+
856
+ return "extended__multiselect-marker-shape";
857
+ }
858
+
859
+ /**
860
+ * Creates role of every option element for accessible applications
861
+ * if it needed
862
+ * @function
863
+ * @param {UnionPropType} option - each option from options list
864
+ * @returns {string|null} role
865
+ */
866
+ const optionCreateRole = (option) => {
867
+ if (optionIsDisabled(option)) return null;
868
+ if (!option) return null;
869
+
870
+ return "option";
871
+ };
872
+
873
+ /**
874
+ * Determines whether option is disabled
875
+ * @function
876
+ * @param {UnionPropType} option - each option from options list
877
+ * @returns {boolean} blocking
878
+ */
879
+ const optionIsDisabled = (option) => {
880
+ if (option.disabled || defineDisabledOption(option)) return true;
881
+
882
+ return false;
883
+ }
884
+
885
+ /**
886
+ * Defines various classes for options dependent from props
887
+ * @function
888
+ * @param {UnionPropType} option - each option from options list
889
+ * @returns {Array} classes
890
+ */
891
+ const optionHighlightClasses = (option) => {
892
+ let theme = [];
893
+
894
+ if (highlightOptions.value) {
895
+ switch(themeType.value) {
896
+ case "basic":
897
+ theme.push("extended__multiselect-options_option");
898
+ break;
899
+ case "slate-grey":
900
+ theme.push("extended__multiselect-options_option-slate-grey");
901
+ break;
902
+ case "slate-blue":
903
+ theme.push("extended__multiselect-options_option-slate-blue");
904
+ break;
905
+ case "teal":
906
+ theme.push("extended__multiselect-options_option-teal");
907
+ break;
908
+ case "strict":
909
+ theme.push("extended__multiselect-options_option-strict");
910
+ break;
911
+ default:
912
+ theme.push("extended__multiselect-options_option");
913
+ }
914
+ }
915
+
916
+ if (showMarker) {
917
+ theme.push("extended__multiselect-options_option--marker");
918
+ }
919
+
920
+ if (option && option.disabled) {
921
+ theme.push("extended__multiselect-options_option--disabled");
922
+ }
923
+
924
+ if (disabledPrimitiveOptionsConverted.value && option && defineDisabledOption(option)) {
925
+ theme.push("extended__multiselect-options_option--disabled");
926
+ }
927
+
928
+ return theme;
929
+ };
930
+
931
+ /**
932
+ * Selects option
933
+ * @function
934
+ * @emits extended:deselect-option
935
+ * @param {UnionPropType} option - option to select
936
+ * @param {MouseEvent|KeyboardEvent} clickEvent - MouseEvent or KeyboardEvent instance
937
+ */
938
+ const selectOption = (option, clickEvent) => {
939
+ emitter.emit("extended:trigger-selection", false);
940
+
941
+ if (!clickEvent) return;
942
+ if (keyBlocker(clickEvent)) return;
943
+ if (maxOptionsWereSelected.value) return;
944
+ if (option.disabled || defineDisabledOption(option)) return;
945
+ if (
946
+ clickEvent.target.id === "extended__multiselect-toggle"
947
+ || clickEvent.target.id === "extended__multiselect-cancel"
948
+ ) return;
949
+
950
+ if (selectedOptionsShown.value) {
951
+ const optionDeselected = lookForObjectOptions(option);
952
+
953
+ if (!optionDeselected) {
954
+ emitter.emit("extended:deselect-option");
955
+ return;
956
+ }
957
+ }
958
+
959
+ const isObject = typeof option === "object" && !Array.isArray(option);
960
+
961
+ if (isObject && !Object.keys(option).includes(label.value)) {
962
+ if (showInsertWarnings.value) {
963
+ console.warn("vue-extended-multiselect: wrong label type inserted");
964
+ }
965
+ }
966
+
967
+ selectOptionEmitter(option);
968
+ };
969
+
970
+ /**
971
+ * Emits select events or clear event
972
+ * if prop "clearBySelectWhenMultiple" equals true
973
+ * @function
974
+ * @emits extended:clear-field
975
+ * @emits extended:select-option
976
+ * @param {UnionPropType} - option to select
977
+ */
978
+ const selectOptionEmitter = (option) => {
979
+ const isObjectOrArray = typeof option === "object";
980
+ const label = createLabel(isObjectOrArray, option);
981
+
982
+ if (multiple.value) {
983
+ if (clearBySelectWhenMultiple.value) {
984
+ emitter.emit("extended:clear-field");
985
+ }
986
+
987
+ emitter.emit("extended:select-option", {
988
+ label,
989
+ option,
990
+ });
991
+ } else {
992
+ emitter.emit("extended:select-option", {
993
+ label,
994
+ option,
995
+ });
996
+ }
997
+ };
998
+
999
+ /**
1000
+ * Determines an option which will be selected
1001
+ * by pressing "enter" key on it
1002
+ * @function
1003
+ * @param {MouseEvent} event - MouseEvent instance
1004
+ */
1005
+ const setEnterIndex = (event) => {
1006
+ enterIndex.value = event.target.getAttribute("enter-locator");
1007
+ };
1008
+
1009
+ /**
1010
+ * Determines whether to show marker beside option by its selection
1011
+ * @function
1012
+ * @param {UnionPropType} option - each option from options list
1013
+ * @returns {boolean} display
1014
+ */
1015
+ const showCurrentMarker = (option) => {
1016
+ if (!lookForSimpleOptions(option) || !lookForObjectOptions(option)) {
1017
+ return true;
1018
+ }
1019
+
1020
+ return false;
1021
+ };
1022
+
1023
+ /**
1024
+ * Emits event which listener will add special class
1025
+ * to search field before option selection
1026
+ * @function
1027
+ * @emits extended:trigger-selection
1028
+ */
1029
+ const triggerOptionBeforeSelection = () => {
1030
+ const fieldWasShown = document.querySelector(".extended__multiselect-input");
1031
+
1032
+ if (fieldWasShown) {
1033
+ emitter.emit("extended:trigger-selection", true);
1034
+ }
1035
+ };
1036
+
1037
+ /**
1038
+ * "onBeforeMount" hook of ExtendedMultiselectOptions component
1039
+ * @listens extended:select-enter
1040
+ * @listens extended:reset-index
1041
+ */
1042
+ onBeforeMount(() => {
1043
+ const objectFields = restrictedOptions.value.filter((option) => {
1044
+ if (!option) return false;
1045
+
1046
+ return typeof option === "object" && typeof option[label.value] === "object";
1047
+ });
1048
+
1049
+ if (objectFields.length && showInsertWarnings.value) {
1050
+ console.warn("vue-extended-multiselect: «label» property can not be of type «object»");
1051
+ }
1052
+
1053
+ emitter.on("extended:select-enter", (keyboardEvent) => {
1054
+ if (!enterIndex.value) return;
1055
+
1056
+ const option = this.availableOptions[this.enterIndex];
1057
+ this.selectOption(option, keyboardEvent);
1058
+ });
1059
+
1060
+ emitter.on("extended:reset-index", () => {
1061
+ enterIndex.value = null;
1062
+ });
1063
+ });
1064
+
1065
+ /**
1066
+ * "onMounted" hook of ExtendedMultiselectOptions component
1067
+ */
1068
+ onMounted(() => {
1069
+ const offsetHeight = optionsWrapper.value.offsetHeight;
1070
+ const scrollHeight = optionsWrapper.value.scrollHeight;
1071
+
1072
+ if (chosenToggleAppearanceSide.value === "atop") {
1073
+ if (scrollHeight > offsetHeight) {
1074
+ atopWithScroll.value = {
1075
+ borderTopRightRadius: 0,
1076
+ };
1077
+ }
1078
+
1079
+ if (toggleMaxHeight.value > offsetHeight) {
1080
+ heightFromMounted.value = {
1081
+ top: `-${offsetHeight}px`,
1082
+ };
1083
+ }
1084
+ } else {
1085
+ if (scrollHeight > offsetHeight) {
1086
+ underWithScroll.value = {
1087
+ borderBottomRightRadius: 0,
1088
+ };
1089
+ }
1090
+ }
1091
+ });
1089
1092
  </script>