vue3-extended-multiselect 0.1.2 → 0.1.3

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 (40) hide show
  1. package/.eslintrc.js +29 -29
  2. package/.github/workflows/publish.yml +41 -0
  3. package/.nvmrc +0 -0
  4. package/CHANGELOG.md +47 -43
  5. package/LICENSE +21 -21
  6. package/README.md +116 -116
  7. package/index.js +2 -2
  8. package/jest.config.js +57 -57
  9. package/package.json +46 -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 +142 -142
  20. package/src/components/ExtendedMultiselectInput.vue +618 -618
  21. package/src/components/ExtendedMultiselectLoader.vue +176 -176
  22. package/src/components/ExtendedMultiselectMultiple.vue +291 -291
  23. package/src/components/ExtendedMultiselectOptions.vue +1087 -1087
  24. package/src/components/ExtendedMultiselectToggle.vue +254 -254
  25. package/src/components/Vue3ExtendedMultiselect.vue +1651 -1651
  26. package/src/composition/cancel.js +30 -30
  27. package/src/composition/labels.js +39 -39
  28. package/src/composition/preselected-options.js +66 -66
  29. package/src/composition/search-value.js +33 -33
  30. package/src/composition/sizes.js +42 -42
  31. package/src/composition/toggle.js +33 -33
  32. package/src/sets/sets.js +99 -99
  33. package/src/styles/animations.css +19 -19
  34. package/src/styles/main.css +433 -433
  35. package/src/styles/variables.css +41 -41
  36. package/tests/unit/events.spec.js +135 -135
  37. package/tests/unit/props.spec.js +1040 -1040
  38. package/tests/utils/mount.js +37 -37
  39. package/tests/utils/utils.js +20 -20
  40. package/typings/vue3-extended-multiselect.d.ts +33 -33
@@ -1,1088 +1,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
- <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
- });
504
-
505
- /**
506
- * Determines whether list of internal search results is empty
507
- * @function
508
- * @returns {boolean} emptiness
509
- */
510
- const emptySearchResult = computed(() => {
511
- return !availableOptions.value.length && !createOnTheGo.value && searchState.searchValue;
512
- });
513
-
514
- /**
515
- * Defines styles for "min-heigt", "max-height"
516
- * and "top" css-properties of options wrapper
517
- * @function
518
- * @returns {Object} styles
519
- */
520
- const heightFromProps = computed(() => {
521
- let heightRecord = {};
522
-
523
- if (chosenToggleAppearanceSide.value === "atop") {
524
- Object.defineProperty(heightRecord, "top", {
525
- enumerable: true,
526
- value: `-${toggleMaxHeight.value}px`,
527
- });
528
- }
529
-
530
- Object.defineProperty(heightRecord, "maxHeight", {
531
- enumerable: true,
532
- value: `${toggleMaxHeight.value}px`,
533
- });
534
-
535
- if (toggleMinHeight.value) {
536
- if (toggleMinHeight.value > toggleMaxHeight.value) {
537
- if (showInsertWarnings.value) {
538
- let errorMessage = "vue-extended-multiselect: «toggleMinHeight» property can not be greater than «toggleMaxHeight» property.";
539
- errorMessage += "«toggleMaxHeight» property was reset to default value of 400 pixels.";
540
- console.warn(errorMessage);
541
- }
542
-
543
- return Object.keys(heightRecord).length ? heightRecord : null;
544
- }
545
-
546
- Object.defineProperty(heightRecord, "minHeight", {
547
- enumerable: true,
548
- value: `${toggleMinHeight.value}px`,
549
- });
550
- }
551
-
552
- return Object.keys(heightRecord).length ? heightRecord : null;
553
- });
554
-
555
- /**
556
- * Determines whether maximal amount of options has been selected
557
- * @function
558
- * @returns {boolean} reaching
559
- */
560
- const maxOptionsWereSelected = computed(() => {
561
- return maxOptionsCount.value && multiple.value && selectedOptions.value.length === maxOptionsCount.value;
562
- });
563
-
564
- /**
565
- * Defines styles for "margin-top" css-property of marker element
566
- * @function
567
- * @returns {Object} styles
568
- */
569
- const markerShapeMargin = computed(() => {
570
- return { marginTop: `${Math.floor(anyOptionWrapperBlockHeight.value) / 30}px` };
571
- });
572
-
573
- /**
574
- * Determines whether minimal amount of options has not been selected
575
- * @function
576
- * @returns {boolean} reaching
577
- */
578
- const minOptionsWereNotSelected = computed(() => {
579
- return minOptionsCount.value && multiple.value && selectedOptions.value.length < minOptionsCount.value;
580
- });
581
-
582
- /**
583
- * Defines styles for "padding" and "height" css-property
584
- * of every option element
585
- * @function
586
- * @returns {Object} styles
587
- */
588
- const optionHeightByProps = computed(() => {
589
- let height = {
590
- height: `${anyOptionWrapperBlockHeight.value}px`,
591
- };
592
-
593
- if (optionsPadding.value.length) {
594
- const padding = {
595
- paddingTop: optionsPadding.value[0] ? `${optionsPadding.value[0]}px` : null,
596
- paddingLeft: optionsPadding.value[1] ? `${optionsPadding.value[1]}px` : null,
597
- paddingBottom: optionsPadding.value[2] ? `${optionsPadding.value[2]}px` : null,
598
- paddingRight: optionsPadding.value[3] ? `${optionsPadding.value[3]}px` : null,
599
- };
600
-
601
- return Object.assign(height, padding);
602
- }
603
-
604
- return height;
605
- });
606
-
607
- /**
608
- * Determines whether to show element with new custom option
609
- * @function
610
- * @returns {boolean} display
611
- */
612
- const showCreateNewOptionBlock = computed(() => {
613
- return createOnTheGo.value && !availableOptions.value.length && searchState.searchValue;
614
- });
615
-
616
- /**
617
- * Summarizes styles for options wrapper in combined collection
618
- * @function
619
- * @returns {Array} styles
620
- */
621
-
622
- const styles = computed(() => {
623
- return [
624
- heightFromProps.value,
625
- heightFromMounted.value,
626
- atopWithScroll.value,
627
- underWithScroll.value
628
- ];
629
- });
630
-
631
- /**
632
- * Restricts repeated selection of option
633
- * @function
634
- * @param {string} calculatedOptionLabel - displayed label of option
635
- */
636
- const compareWithSelected = (calculatedOptionLabel) => {
637
- return !selectedOptions.value.find((selectedOption) => {
638
- let calculatedSelectedOptionLabel;
639
-
640
- if (typeof selectedOption === "object") {
641
- if (Object.keys(selectedOption).length === 0) {
642
- calculatedSelectedOptionLabel = emptyObjectsPlaceholder.value;
643
- } else if (Array.isArray(selectedOption)) {
644
- calculatedSelectedOptionLabel = selectedOption.join(", ");
645
- } else {
646
- calculatedSelectedOptionLabel = typeof selectedOption[label.value] === "object"
647
- ? JSON.stringify(selectedOption[label.value])
648
- : selectedOption[label.value].toString();
649
- }
650
- } else {
651
- calculatedSelectedOptionLabel = selectedOption.toString();
652
- }
653
-
654
- return calculatedSelectedOptionLabel === calculatedOptionLabel;
655
- });
656
- };
657
-
658
- /**
659
- * Creates new option by users input
660
- * if "createOnTheGo" prop equals true
661
- * @function
662
- * @emits extended:create-option
663
- */
664
- const createNewOption = () => {
665
- let newOption;
666
-
667
- switch(createOptionType.value) {
668
- case "primitive":
669
- newOption = searchState.searchValue;
670
- break;
671
- case "array":
672
- newOption = [searchState.searchValue];
673
- break;
674
- case "object":
675
- newOption = createObjectOption();
676
- break;
677
- }
678
-
679
- options.value.unshift(newOption);
680
- emitter.emit("extended:create-option", newOption);
681
-
682
- if (autoSelectCreatedOption.value) {
683
- selectOption(newOption, { target: { id: "extended__multiselect" } });
684
- setSearchValue(null);
685
- setSearchPattern(null);
686
- }
687
- };
688
-
689
- /**
690
- * Creates option of type object
691
- * if "createOptionType" equals "object"
692
- * @function
693
- * @returns {UnionPropType} option
694
- */
695
- const createObjectOption = () => {
696
- if (!createOptionFields.value) {
697
- if (showInsertWarnings.value) {
698
- console.warn("vue-extended-multiselect: if option should be object — option fields should not be empty");
699
- }
700
-
701
- return searchState.searchValue;
702
- }
703
-
704
- const newOption = {};
705
-
706
- createOptionFields.value.forEach((field) => {
707
- Object.defineProperty(newOption, field, {
708
- enumerable: true,
709
- writable: true,
710
- configurable: true,
711
- value: `${field}-${searchState.searchValue}`,
712
- });
713
- });
714
-
715
- return newOption;
716
- };
717
-
718
- /**
719
- * Recognises if option is disabled
720
- * @function
721
- * @param {UnionPropType} option - option to recognize
722
- * @returns {boolean} prohibition
723
- */
724
- const defineDisabledOption = (option) => {
725
- if (typeof option === "object" && !Array.isArray(option)) return false;
726
-
727
- let convertedOption;
728
- if (Array.isArray(option) && !option.length) {
729
- convertedOption = emptyObjectsPlaceholder.value;
730
- } else {
731
- convertedOption = option;
732
- }
733
-
734
- if (disabledPrimitiveOptionsConverted.value.includes(convertedOption)) return true;
735
-
736
- return false;
737
- };
738
-
739
- /**
740
- * Filters options list by pattern of internal search for available options
741
- * @function
742
- * @param {Array} optionsList - pre-filtered list of oprions
743
- * @returns {boolean} filter
744
- */
745
- const filterBySearchPattern = (optionsList) => {
746
- return optionsList.filter((option) => {
747
- if (typeof option === "object") {
748
- if (Array.isArray(option)) {
749
- return searchState.searchPattern.test(option.length ? option.join(", ") : emptyObjectsPlaceholder.value);
750
- }
751
- return searchState.searchPattern.test(option[label.value].toString());
752
- } else {
753
- return searchState.searchPattern.test(option.toString());
754
- }
755
- });
756
- };
757
-
758
- /**
759
- * Blocks keys pressed in combination with "enter" key
760
- * for not to select options by them
761
- * @function
762
- * @param {KeyboardEvent} keyboardEvent - KeyboardEvent instance
763
- * @returns {boolean} block
764
- */
765
- const keyBlocker = (keyboardEvent) => {
766
- if (!keyboardEvent) return false;
767
-
768
- const altKeyBlock = specialKeysBlock.value.includes("alt");
769
- const ctrlKeyBlock = specialKeysBlock.value.includes("ctrl");
770
- const shiftKeyBlock = specialKeysBlock.value.includes("shift");
771
-
772
- if (altKeyBlock && keyboardEvent.altKey) return true;
773
- if (ctrlKeyBlock && keyboardEvent.ctrlKey) return true;
774
- if (shiftKeyBlock && keyboardEvent.shiftKey) return true;
775
-
776
- return false;
777
- }
778
-
779
- /**
780
- * Searchs for already selected options in particular
781
- * for options of type "object"
782
- * @function
783
- * @param {UnionPropType} option - option for search
784
- * @returns {boolean} selected
785
- */
786
- const lookForObjectOptions = (option) => {
787
- if (typeof option === "object") {
788
- let calculatedOptionLabel;
789
-
790
- if (Array.isArray(option) && !!option.length) {
791
- calculatedOptionLabel = option.join(", ");
792
- } else {
793
- const hasLabel = Object.getOwnPropertyNames(option).includes("label");
794
-
795
- if (hasLabel) {
796
- calculatedOptionLabel = typeof option[label.value] === "object"
797
- ? JSON.stringify(option[label.value])
798
- : option[label.value].toString();
799
- } else {
800
- calculatedOptionLabel = JSON.stringify(option);
801
- }
802
-
803
- if (Object.keys(option).length === 0) {
804
- calculatedOptionLabel = emptyObjectsPlaceholder.value;
805
- }
806
- }
807
-
808
- return compareWithSelected(calculatedOptionLabel);
809
- } else {
810
- return !selectedOptions.value.includes(option);
811
- }
812
- };
813
-
814
- /**
815
- * Searches for already selected options in particular
816
- * for options of primitive types or of type "function"
817
- * @function
818
- * @param {UnionPropType} option - option for search
819
- * @returns {boolean} selected
820
- */
821
- const lookForSimpleOptions = (option) => {
822
- if (typeof option === "object" && Object.keys(option).length === 0) {
823
- const emptyObject = selectedOptions.value.find((emptyObject) => {
824
- return typeof emptyObject === "object" && !Object.keys(emptyObject).length;
825
- });
826
-
827
- if (!emptyObject || selectedOptionsShown.value) return true;
828
-
829
- return false;
830
- }
831
-
832
- if (
833
- typeof option === "object"
834
- && !Array.isArray(option)
835
- && !Object.keys(option).includes(label.value)
836
- ) return false;
837
-
838
- return true;
839
- };
840
-
841
- /**
842
- * Defines a class for "marker" slot content
843
- * @function
844
- * @param {UnionPropType} option - option in options list
845
- * @returns {string} class
846
- */
847
- const markerShapeClass = (option) => {
848
- if (lookForSimpleOptions(option) && lookForObjectOptions(option)) {
849
- return "extended__multiselect-marker-shape--selected";
850
- }
851
-
852
- return "extended__multiselect-marker-shape";
853
- }
854
-
855
- /**
856
- * Creates role of every option element for accessible applications
857
- * if it needed
858
- * @function
859
- * @param {UnionPropType} option - each option from options list
860
- * @returns {string|null} role
861
- */
862
- const optionCreateRole = (option) => {
863
- if (optionIsDisabled(option)) return null;
864
- if (!option) return null;
865
-
866
- return "option";
867
- };
868
-
869
- /**
870
- * Determines whether option is disabled
871
- * @function
872
- * @param {UnionPropType} option - each option from options list
873
- * @returns {boolean} blocking
874
- */
875
- const optionIsDisabled = (option) => {
876
- if (option.disabled || defineDisabledOption(option)) return true;
877
-
878
- return false;
879
- }
880
-
881
- /**
882
- * Defines various classes for options dependent from props
883
- * @function
884
- * @param {UnionPropType} option - each option from options list
885
- * @returns {Array} classes
886
- */
887
- const optionHighlightClasses = (option) => {
888
- let theme = [];
889
-
890
- if (highlightOptions.value) {
891
- switch(themeType.value) {
892
- case "basic":
893
- theme.push("extended__multiselect-options_option");
894
- break;
895
- case "slate-grey":
896
- theme.push("extended__multiselect-options_option-slate-grey");
897
- break;
898
- case "slate-blue":
899
- theme.push("extended__multiselect-options_option-slate-blue");
900
- break;
901
- case "teal":
902
- theme.push("extended__multiselect-options_option-teal");
903
- break;
904
- case "strict":
905
- theme.push("extended__multiselect-options_option-strict");
906
- break;
907
- default:
908
- theme.push("extended__multiselect-options_option");
909
- }
910
- }
911
-
912
- if (showMarker) {
913
- theme.push("extended__multiselect-options_option--marker");
914
- }
915
-
916
- if (option && option.disabled) {
917
- theme.push("extended__multiselect-options_option--disabled");
918
- }
919
-
920
- if (disabledPrimitiveOptionsConverted.value && option && defineDisabledOption(option)) {
921
- theme.push("extended__multiselect-options_option--disabled");
922
- }
923
-
924
- return theme;
925
- };
926
-
927
- /**
928
- * Selects option
929
- * @function
930
- * @emits extended:deselect-option
931
- * @param {UnionPropType} option - option to select
932
- * @param {MouseEvent|KeyboardEvent} clickEvent - MouseEvent or KeyboardEvent instance
933
- */
934
- const selectOption = (option, clickEvent) => {
935
- emitter.emit("extended:trigger-selection", false);
936
-
937
- if (!clickEvent) return;
938
- if (keyBlocker(clickEvent)) return;
939
- if (maxOptionsWereSelected.value) return;
940
- if (option.disabled || defineDisabledOption(option)) return;
941
- if (
942
- clickEvent.target.id === "extended__multiselect-toggle"
943
- || clickEvent.target.id === "extended__multiselect-cancel"
944
- ) return;
945
-
946
- if (selectedOptionsShown.value) {
947
- const optionDeselected = lookForObjectOptions(option);
948
-
949
- if (!optionDeselected) {
950
- emitter.emit("extended:deselect-option");
951
- return;
952
- }
953
- }
954
-
955
- const isObject = typeof option === "object" && !Array.isArray(option);
956
-
957
- if (isObject && !Object.keys(option).includes(label.value)) {
958
- if (showInsertWarnings.value) {
959
- console.warn("vue-extended-multiselect: wrong label type inserted");
960
- }
961
- }
962
-
963
- selectOptionEmitter(option);
964
- };
965
-
966
- /**
967
- * Emits select events or clear event
968
- * if prop "clearBySelectWhenMultiple" equals true
969
- * @function
970
- * @emits extended:clear-field
971
- * @emits extended:select-option
972
- * @param {UnionPropType} - option to select
973
- */
974
- const selectOptionEmitter = (option) => {
975
- const isObjectOrArray = typeof option === "object";
976
- const label = createLabel(isObjectOrArray, option);
977
-
978
- if (multiple.value) {
979
- if (clearBySelectWhenMultiple.value) {
980
- emitter.emit("extended:clear-field");
981
- }
982
-
983
- emitter.emit("extended:select-option", {
984
- label,
985
- option,
986
- });
987
- } else {
988
- emitter.emit("extended:select-option", {
989
- label,
990
- option,
991
- });
992
- }
993
- };
994
-
995
- /**
996
- * Determines an option which will be selected
997
- * by pressing "enter" key on it
998
- * @function
999
- * @param {MouseEvent} event - MouseEvent instance
1000
- */
1001
- const setEnterIndex = (event) => {
1002
- enterIndex.value = event.target.getAttribute("enter-locator");
1003
- };
1004
-
1005
- /**
1006
- * Determines whether to show marker beside option by its selection
1007
- * @function
1008
- * @param {UnionPropType} option - each option from options list
1009
- * @returns {boolean} display
1010
- */
1011
- const showCurrentMarker = (option) => {
1012
- if (!lookForSimpleOptions(option) || !lookForObjectOptions(option)) {
1013
- return true;
1014
- }
1015
-
1016
- return false;
1017
- };
1018
-
1019
- /**
1020
- * Emits event which listener will add special class
1021
- * to search field before option selection
1022
- * @function
1023
- * @emits extended:trigger-selection
1024
- */
1025
- const triggerOptionBeforeSelection = () => {
1026
- const fieldWasShown = document.querySelector(".extended__multiselect-input");
1027
-
1028
- if (fieldWasShown) {
1029
- emitter.emit("extended:trigger-selection", true);
1030
- }
1031
- };
1032
-
1033
- /**
1034
- * "onBeforeMount" hook of ExtendedMultiselectOptions component
1035
- * @listens extended:select-enter
1036
- * @listens extended:reset-index
1037
- */
1038
- onBeforeMount(() => {
1039
- const objectFields = restrictedOptions.value.filter((option) => {
1040
- if (!option) return false;
1041
-
1042
- return typeof option === "object" && typeof option[label.value] === "object";
1043
- });
1044
-
1045
- if (objectFields.length && showInsertWarnings.value) {
1046
- console.warn("vue-extended-multiselect: «label» property can not be of type «object»");
1047
- }
1048
-
1049
- emitter.on("extended:select-enter", (keyboardEvent) => {
1050
- if (!enterIndex.value) return;
1051
-
1052
- const option = this.availableOptions[this.enterIndex];
1053
- this.selectOption(option, keyboardEvent);
1054
- });
1055
-
1056
- emitter.on("extended:reset-index", () => {
1057
- enterIndex.value = null;
1058
- });
1059
- });
1060
-
1061
- /**
1062
- * "onMounted" hook of ExtendedMultiselectOptions component
1063
- */
1064
- onMounted(() => {
1065
- const offsetHeight = optionsWrapper.value.offsetHeight;
1066
- const scrollHeight = optionsWrapper.value.scrollHeight;
1067
-
1068
- if (chosenToggleAppearanceSide.value === "atop") {
1069
- if (scrollHeight > offsetHeight) {
1070
- atopWithScroll.value = {
1071
- borderTopRightRadius: 0,
1072
- };
1073
- }
1074
-
1075
- if (toggleMaxHeight.value > offsetHeight) {
1076
- heightFromMounted.value = {
1077
- top: `-${offsetHeight}px`,
1078
- };
1079
- }
1080
- } else {
1081
- if (scrollHeight > offsetHeight) {
1082
- underWithScroll.value = {
1083
- borderBottomRightRadius: 0,
1084
- };
1085
- }
1086
- }
1087
- });
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
+ });
504
+
505
+ /**
506
+ * Determines whether list of internal search results is empty
507
+ * @function
508
+ * @returns {boolean} emptiness
509
+ */
510
+ const emptySearchResult = computed(() => {
511
+ return !availableOptions.value.length && !createOnTheGo.value && searchState.searchValue;
512
+ });
513
+
514
+ /**
515
+ * Defines styles for "min-heigt", "max-height"
516
+ * and "top" css-properties of options wrapper
517
+ * @function
518
+ * @returns {Object} styles
519
+ */
520
+ const heightFromProps = computed(() => {
521
+ let heightRecord = {};
522
+
523
+ if (chosenToggleAppearanceSide.value === "atop") {
524
+ Object.defineProperty(heightRecord, "top", {
525
+ enumerable: true,
526
+ value: `-${toggleMaxHeight.value}px`,
527
+ });
528
+ }
529
+
530
+ Object.defineProperty(heightRecord, "maxHeight", {
531
+ enumerable: true,
532
+ value: `${toggleMaxHeight.value}px`,
533
+ });
534
+
535
+ if (toggleMinHeight.value) {
536
+ if (toggleMinHeight.value > toggleMaxHeight.value) {
537
+ if (showInsertWarnings.value) {
538
+ let errorMessage = "vue-extended-multiselect: «toggleMinHeight» property can not be greater than «toggleMaxHeight» property.";
539
+ errorMessage += "«toggleMaxHeight» property was reset to default value of 400 pixels.";
540
+ console.warn(errorMessage);
541
+ }
542
+
543
+ return Object.keys(heightRecord).length ? heightRecord : null;
544
+ }
545
+
546
+ Object.defineProperty(heightRecord, "minHeight", {
547
+ enumerable: true,
548
+ value: `${toggleMinHeight.value}px`,
549
+ });
550
+ }
551
+
552
+ return Object.keys(heightRecord).length ? heightRecord : null;
553
+ });
554
+
555
+ /**
556
+ * Determines whether maximal amount of options has been selected
557
+ * @function
558
+ * @returns {boolean} reaching
559
+ */
560
+ const maxOptionsWereSelected = computed(() => {
561
+ return maxOptionsCount.value && multiple.value && selectedOptions.value.length === maxOptionsCount.value;
562
+ });
563
+
564
+ /**
565
+ * Defines styles for "margin-top" css-property of marker element
566
+ * @function
567
+ * @returns {Object} styles
568
+ */
569
+ const markerShapeMargin = computed(() => {
570
+ return { marginTop: `${Math.floor(anyOptionWrapperBlockHeight.value) / 30}px` };
571
+ });
572
+
573
+ /**
574
+ * Determines whether minimal amount of options has not been selected
575
+ * @function
576
+ * @returns {boolean} reaching
577
+ */
578
+ const minOptionsWereNotSelected = computed(() => {
579
+ return minOptionsCount.value && multiple.value && selectedOptions.value.length < minOptionsCount.value;
580
+ });
581
+
582
+ /**
583
+ * Defines styles for "padding" and "height" css-property
584
+ * of every option element
585
+ * @function
586
+ * @returns {Object} styles
587
+ */
588
+ const optionHeightByProps = computed(() => {
589
+ let height = {
590
+ height: `${anyOptionWrapperBlockHeight.value}px`,
591
+ };
592
+
593
+ if (optionsPadding.value.length) {
594
+ const padding = {
595
+ paddingTop: optionsPadding.value[0] ? `${optionsPadding.value[0]}px` : null,
596
+ paddingLeft: optionsPadding.value[1] ? `${optionsPadding.value[1]}px` : null,
597
+ paddingBottom: optionsPadding.value[2] ? `${optionsPadding.value[2]}px` : null,
598
+ paddingRight: optionsPadding.value[3] ? `${optionsPadding.value[3]}px` : null,
599
+ };
600
+
601
+ return Object.assign(height, padding);
602
+ }
603
+
604
+ return height;
605
+ });
606
+
607
+ /**
608
+ * Determines whether to show element with new custom option
609
+ * @function
610
+ * @returns {boolean} display
611
+ */
612
+ const showCreateNewOptionBlock = computed(() => {
613
+ return createOnTheGo.value && !availableOptions.value.length && searchState.searchValue;
614
+ });
615
+
616
+ /**
617
+ * Summarizes styles for options wrapper in combined collection
618
+ * @function
619
+ * @returns {Array} styles
620
+ */
621
+
622
+ const styles = computed(() => {
623
+ return [
624
+ heightFromProps.value,
625
+ heightFromMounted.value,
626
+ atopWithScroll.value,
627
+ underWithScroll.value
628
+ ];
629
+ });
630
+
631
+ /**
632
+ * Restricts repeated selection of option
633
+ * @function
634
+ * @param {string} calculatedOptionLabel - displayed label of option
635
+ */
636
+ const compareWithSelected = (calculatedOptionLabel) => {
637
+ return !selectedOptions.value.find((selectedOption) => {
638
+ let calculatedSelectedOptionLabel;
639
+
640
+ if (typeof selectedOption === "object") {
641
+ if (Object.keys(selectedOption).length === 0) {
642
+ calculatedSelectedOptionLabel = emptyObjectsPlaceholder.value;
643
+ } else if (Array.isArray(selectedOption)) {
644
+ calculatedSelectedOptionLabel = selectedOption.join(", ");
645
+ } else {
646
+ calculatedSelectedOptionLabel = typeof selectedOption[label.value] === "object"
647
+ ? JSON.stringify(selectedOption[label.value])
648
+ : selectedOption[label.value].toString();
649
+ }
650
+ } else {
651
+ calculatedSelectedOptionLabel = selectedOption.toString();
652
+ }
653
+
654
+ return calculatedSelectedOptionLabel === calculatedOptionLabel;
655
+ });
656
+ };
657
+
658
+ /**
659
+ * Creates new option by users input
660
+ * if "createOnTheGo" prop equals true
661
+ * @function
662
+ * @emits extended:create-option
663
+ */
664
+ const createNewOption = () => {
665
+ let newOption;
666
+
667
+ switch(createOptionType.value) {
668
+ case "primitive":
669
+ newOption = searchState.searchValue;
670
+ break;
671
+ case "array":
672
+ newOption = [searchState.searchValue];
673
+ break;
674
+ case "object":
675
+ newOption = createObjectOption();
676
+ break;
677
+ }
678
+
679
+ options.value.unshift(newOption);
680
+ emitter.emit("extended:create-option", newOption);
681
+
682
+ if (autoSelectCreatedOption.value) {
683
+ selectOption(newOption, { target: { id: "extended__multiselect" } });
684
+ setSearchValue(null);
685
+ setSearchPattern(null);
686
+ }
687
+ };
688
+
689
+ /**
690
+ * Creates option of type object
691
+ * if "createOptionType" equals "object"
692
+ * @function
693
+ * @returns {UnionPropType} option
694
+ */
695
+ const createObjectOption = () => {
696
+ if (!createOptionFields.value) {
697
+ if (showInsertWarnings.value) {
698
+ console.warn("vue-extended-multiselect: if option should be object — option fields should not be empty");
699
+ }
700
+
701
+ return searchState.searchValue;
702
+ }
703
+
704
+ const newOption = {};
705
+
706
+ createOptionFields.value.forEach((field) => {
707
+ Object.defineProperty(newOption, field, {
708
+ enumerable: true,
709
+ writable: true,
710
+ configurable: true,
711
+ value: `${field}-${searchState.searchValue}`,
712
+ });
713
+ });
714
+
715
+ return newOption;
716
+ };
717
+
718
+ /**
719
+ * Recognises if option is disabled
720
+ * @function
721
+ * @param {UnionPropType} option - option to recognize
722
+ * @returns {boolean} prohibition
723
+ */
724
+ const defineDisabledOption = (option) => {
725
+ if (typeof option === "object" && !Array.isArray(option)) return false;
726
+
727
+ let convertedOption;
728
+ if (Array.isArray(option) && !option.length) {
729
+ convertedOption = emptyObjectsPlaceholder.value;
730
+ } else {
731
+ convertedOption = option;
732
+ }
733
+
734
+ if (disabledPrimitiveOptionsConverted.value.includes(convertedOption)) return true;
735
+
736
+ return false;
737
+ };
738
+
739
+ /**
740
+ * Filters options list by pattern of internal search for available options
741
+ * @function
742
+ * @param {Array} optionsList - pre-filtered list of oprions
743
+ * @returns {boolean} filter
744
+ */
745
+ const filterBySearchPattern = (optionsList) => {
746
+ return optionsList.filter((option) => {
747
+ if (typeof option === "object") {
748
+ if (Array.isArray(option)) {
749
+ return searchState.searchPattern.test(option.length ? option.join(", ") : emptyObjectsPlaceholder.value);
750
+ }
751
+ return searchState.searchPattern.test(option[label.value].toString());
752
+ } else {
753
+ return searchState.searchPattern.test(option.toString());
754
+ }
755
+ });
756
+ };
757
+
758
+ /**
759
+ * Blocks keys pressed in combination with "enter" key
760
+ * for not to select options by them
761
+ * @function
762
+ * @param {KeyboardEvent} keyboardEvent - KeyboardEvent instance
763
+ * @returns {boolean} block
764
+ */
765
+ const keyBlocker = (keyboardEvent) => {
766
+ if (!keyboardEvent) return false;
767
+
768
+ const altKeyBlock = specialKeysBlock.value.includes("alt");
769
+ const ctrlKeyBlock = specialKeysBlock.value.includes("ctrl");
770
+ const shiftKeyBlock = specialKeysBlock.value.includes("shift");
771
+
772
+ if (altKeyBlock && keyboardEvent.altKey) return true;
773
+ if (ctrlKeyBlock && keyboardEvent.ctrlKey) return true;
774
+ if (shiftKeyBlock && keyboardEvent.shiftKey) return true;
775
+
776
+ return false;
777
+ }
778
+
779
+ /**
780
+ * Searchs for already selected options in particular
781
+ * for options of type "object"
782
+ * @function
783
+ * @param {UnionPropType} option - option for search
784
+ * @returns {boolean} selected
785
+ */
786
+ const lookForObjectOptions = (option) => {
787
+ if (typeof option === "object") {
788
+ let calculatedOptionLabel;
789
+
790
+ if (Array.isArray(option) && !!option.length) {
791
+ calculatedOptionLabel = option.join(", ");
792
+ } else {
793
+ const hasLabel = Object.getOwnPropertyNames(option).includes("label");
794
+
795
+ if (hasLabel) {
796
+ calculatedOptionLabel = typeof option[label.value] === "object"
797
+ ? JSON.stringify(option[label.value])
798
+ : option[label.value].toString();
799
+ } else {
800
+ calculatedOptionLabel = JSON.stringify(option);
801
+ }
802
+
803
+ if (Object.keys(option).length === 0) {
804
+ calculatedOptionLabel = emptyObjectsPlaceholder.value;
805
+ }
806
+ }
807
+
808
+ return compareWithSelected(calculatedOptionLabel);
809
+ } else {
810
+ return !selectedOptions.value.includes(option);
811
+ }
812
+ };
813
+
814
+ /**
815
+ * Searches for already selected options in particular
816
+ * for options of primitive types or of type "function"
817
+ * @function
818
+ * @param {UnionPropType} option - option for search
819
+ * @returns {boolean} selected
820
+ */
821
+ const lookForSimpleOptions = (option) => {
822
+ if (typeof option === "object" && Object.keys(option).length === 0) {
823
+ const emptyObject = selectedOptions.value.find((emptyObject) => {
824
+ return typeof emptyObject === "object" && !Object.keys(emptyObject).length;
825
+ });
826
+
827
+ if (!emptyObject || selectedOptionsShown.value) return true;
828
+
829
+ return false;
830
+ }
831
+
832
+ if (
833
+ typeof option === "object"
834
+ && !Array.isArray(option)
835
+ && !Object.keys(option).includes(label.value)
836
+ ) return false;
837
+
838
+ return true;
839
+ };
840
+
841
+ /**
842
+ * Defines a class for "marker" slot content
843
+ * @function
844
+ * @param {UnionPropType} option - option in options list
845
+ * @returns {string} class
846
+ */
847
+ const markerShapeClass = (option) => {
848
+ if (lookForSimpleOptions(option) && lookForObjectOptions(option)) {
849
+ return "extended__multiselect-marker-shape--selected";
850
+ }
851
+
852
+ return "extended__multiselect-marker-shape";
853
+ }
854
+
855
+ /**
856
+ * Creates role of every option element for accessible applications
857
+ * if it needed
858
+ * @function
859
+ * @param {UnionPropType} option - each option from options list
860
+ * @returns {string|null} role
861
+ */
862
+ const optionCreateRole = (option) => {
863
+ if (optionIsDisabled(option)) return null;
864
+ if (!option) return null;
865
+
866
+ return "option";
867
+ };
868
+
869
+ /**
870
+ * Determines whether option is disabled
871
+ * @function
872
+ * @param {UnionPropType} option - each option from options list
873
+ * @returns {boolean} blocking
874
+ */
875
+ const optionIsDisabled = (option) => {
876
+ if (option.disabled || defineDisabledOption(option)) return true;
877
+
878
+ return false;
879
+ }
880
+
881
+ /**
882
+ * Defines various classes for options dependent from props
883
+ * @function
884
+ * @param {UnionPropType} option - each option from options list
885
+ * @returns {Array} classes
886
+ */
887
+ const optionHighlightClasses = (option) => {
888
+ let theme = [];
889
+
890
+ if (highlightOptions.value) {
891
+ switch(themeType.value) {
892
+ case "basic":
893
+ theme.push("extended__multiselect-options_option");
894
+ break;
895
+ case "slate-grey":
896
+ theme.push("extended__multiselect-options_option-slate-grey");
897
+ break;
898
+ case "slate-blue":
899
+ theme.push("extended__multiselect-options_option-slate-blue");
900
+ break;
901
+ case "teal":
902
+ theme.push("extended__multiselect-options_option-teal");
903
+ break;
904
+ case "strict":
905
+ theme.push("extended__multiselect-options_option-strict");
906
+ break;
907
+ default:
908
+ theme.push("extended__multiselect-options_option");
909
+ }
910
+ }
911
+
912
+ if (showMarker) {
913
+ theme.push("extended__multiselect-options_option--marker");
914
+ }
915
+
916
+ if (option && option.disabled) {
917
+ theme.push("extended__multiselect-options_option--disabled");
918
+ }
919
+
920
+ if (disabledPrimitiveOptionsConverted.value && option && defineDisabledOption(option)) {
921
+ theme.push("extended__multiselect-options_option--disabled");
922
+ }
923
+
924
+ return theme;
925
+ };
926
+
927
+ /**
928
+ * Selects option
929
+ * @function
930
+ * @emits extended:deselect-option
931
+ * @param {UnionPropType} option - option to select
932
+ * @param {MouseEvent|KeyboardEvent} clickEvent - MouseEvent or KeyboardEvent instance
933
+ */
934
+ const selectOption = (option, clickEvent) => {
935
+ emitter.emit("extended:trigger-selection", false);
936
+
937
+ if (!clickEvent) return;
938
+ if (keyBlocker(clickEvent)) return;
939
+ if (maxOptionsWereSelected.value) return;
940
+ if (option.disabled || defineDisabledOption(option)) return;
941
+ if (
942
+ clickEvent.target.id === "extended__multiselect-toggle"
943
+ || clickEvent.target.id === "extended__multiselect-cancel"
944
+ ) return;
945
+
946
+ if (selectedOptionsShown.value) {
947
+ const optionDeselected = lookForObjectOptions(option);
948
+
949
+ if (!optionDeselected) {
950
+ emitter.emit("extended:deselect-option");
951
+ return;
952
+ }
953
+ }
954
+
955
+ const isObject = typeof option === "object" && !Array.isArray(option);
956
+
957
+ if (isObject && !Object.keys(option).includes(label.value)) {
958
+ if (showInsertWarnings.value) {
959
+ console.warn("vue-extended-multiselect: wrong label type inserted");
960
+ }
961
+ }
962
+
963
+ selectOptionEmitter(option);
964
+ };
965
+
966
+ /**
967
+ * Emits select events or clear event
968
+ * if prop "clearBySelectWhenMultiple" equals true
969
+ * @function
970
+ * @emits extended:clear-field
971
+ * @emits extended:select-option
972
+ * @param {UnionPropType} - option to select
973
+ */
974
+ const selectOptionEmitter = (option) => {
975
+ const isObjectOrArray = typeof option === "object";
976
+ const label = createLabel(isObjectOrArray, option);
977
+
978
+ if (multiple.value) {
979
+ if (clearBySelectWhenMultiple.value) {
980
+ emitter.emit("extended:clear-field");
981
+ }
982
+
983
+ emitter.emit("extended:select-option", {
984
+ label,
985
+ option,
986
+ });
987
+ } else {
988
+ emitter.emit("extended:select-option", {
989
+ label,
990
+ option,
991
+ });
992
+ }
993
+ };
994
+
995
+ /**
996
+ * Determines an option which will be selected
997
+ * by pressing "enter" key on it
998
+ * @function
999
+ * @param {MouseEvent} event - MouseEvent instance
1000
+ */
1001
+ const setEnterIndex = (event) => {
1002
+ enterIndex.value = event.target.getAttribute("enter-locator");
1003
+ };
1004
+
1005
+ /**
1006
+ * Determines whether to show marker beside option by its selection
1007
+ * @function
1008
+ * @param {UnionPropType} option - each option from options list
1009
+ * @returns {boolean} display
1010
+ */
1011
+ const showCurrentMarker = (option) => {
1012
+ if (!lookForSimpleOptions(option) || !lookForObjectOptions(option)) {
1013
+ return true;
1014
+ }
1015
+
1016
+ return false;
1017
+ };
1018
+
1019
+ /**
1020
+ * Emits event which listener will add special class
1021
+ * to search field before option selection
1022
+ * @function
1023
+ * @emits extended:trigger-selection
1024
+ */
1025
+ const triggerOptionBeforeSelection = () => {
1026
+ const fieldWasShown = document.querySelector(".extended__multiselect-input");
1027
+
1028
+ if (fieldWasShown) {
1029
+ emitter.emit("extended:trigger-selection", true);
1030
+ }
1031
+ };
1032
+
1033
+ /**
1034
+ * "onBeforeMount" hook of ExtendedMultiselectOptions component
1035
+ * @listens extended:select-enter
1036
+ * @listens extended:reset-index
1037
+ */
1038
+ onBeforeMount(() => {
1039
+ const objectFields = restrictedOptions.value.filter((option) => {
1040
+ if (!option) return false;
1041
+
1042
+ return typeof option === "object" && typeof option[label.value] === "object";
1043
+ });
1044
+
1045
+ if (objectFields.length && showInsertWarnings.value) {
1046
+ console.warn("vue-extended-multiselect: «label» property can not be of type «object»");
1047
+ }
1048
+
1049
+ emitter.on("extended:select-enter", (keyboardEvent) => {
1050
+ if (!enterIndex.value) return;
1051
+
1052
+ const option = this.availableOptions[this.enterIndex];
1053
+ this.selectOption(option, keyboardEvent);
1054
+ });
1055
+
1056
+ emitter.on("extended:reset-index", () => {
1057
+ enterIndex.value = null;
1058
+ });
1059
+ });
1060
+
1061
+ /**
1062
+ * "onMounted" hook of ExtendedMultiselectOptions component
1063
+ */
1064
+ onMounted(() => {
1065
+ const offsetHeight = optionsWrapper.value.offsetHeight;
1066
+ const scrollHeight = optionsWrapper.value.scrollHeight;
1067
+
1068
+ if (chosenToggleAppearanceSide.value === "atop") {
1069
+ if (scrollHeight > offsetHeight) {
1070
+ atopWithScroll.value = {
1071
+ borderTopRightRadius: 0,
1072
+ };
1073
+ }
1074
+
1075
+ if (toggleMaxHeight.value > offsetHeight) {
1076
+ heightFromMounted.value = {
1077
+ top: `-${offsetHeight}px`,
1078
+ };
1079
+ }
1080
+ } else {
1081
+ if (scrollHeight > offsetHeight) {
1082
+ underWithScroll.value = {
1083
+ borderBottomRightRadius: 0,
1084
+ };
1085
+ }
1086
+ }
1087
+ });
1088
1088
  </script>