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