@reviewpush/rp-treeselect 0.0.0 → 0.0.2

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.
@@ -1,15 +1,824 @@
1
1
  <script>
2
- import treeselectMixin from '../mixins/treeselectMixin'
2
+ import fuzzysearch from 'fuzzysearch'
3
+ import {
4
+ warning,
5
+ onLeftClick, scrollIntoView,
6
+ isNaN, isPromise, once,
7
+ identity, constant, createMap,
8
+ quickDiff, last as getLast, includes, find, removeFromArray,
9
+ } from '../utils'
10
+ import {
11
+ NO_PARENT_NODE,
12
+ UNCHECKED, INDETERMINATE, CHECKED,
13
+ LOAD_ROOT_OPTIONS, LOAD_CHILDREN_OPTIONS, ASYNC_SEARCH,
14
+ ALL, BRANCH_PRIORITY, LEAF_PRIORITY, ALL_WITH_INDETERMINATE,
15
+ ALL_CHILDREN, ALL_DESCENDANTS, LEAF_CHILDREN, LEAF_DESCENDANTS,
16
+ ORDER_SELECTED, LEVEL, INDEX,
17
+ } from '../constants'
3
18
  import HiddenFields from './HiddenFields'
4
19
  import Control from './Control'
5
20
  import Menu from './Menu'
6
21
  import MenuPortal from './MenuPortal'
7
22
 
23
+ function sortValueByIndex(a, b) {
24
+ let i = 0
25
+ do {
26
+ if (a.level < i) return -1
27
+ if (b.level < i) return 1
28
+ if (a.index[i] !== b.index[i]) return a.index[i] - b.index[i]
29
+ i++
30
+ } while (true)
31
+ }
32
+
33
+ function sortValueByLevel(a, b) {
34
+ return a.level === b.level
35
+ ? sortValueByIndex(a, b)
36
+ : a.level - b.level
37
+ }
38
+
39
+ function createAsyncOptionsStates() {
40
+ return {
41
+ isLoaded: false,
42
+ isLoading: false,
43
+ loadingError: '',
44
+ }
45
+ }
46
+
47
+ function stringifyOptionPropValue(value) {
48
+ if (typeof value === 'string') return value
49
+ if (typeof value === 'number' && !isNaN(value)) return value + ''
50
+ // istanbul ignore next
51
+ return ''
52
+ }
53
+
54
+ function match(enableFuzzyMatch, needle, haystack) {
55
+ return enableFuzzyMatch
56
+ ? fuzzysearch(needle, haystack)
57
+ : includes(haystack, needle)
58
+ }
59
+
60
+ function getErrorMessage(err) {
61
+ return err.message || /* istanbul ignore next */String(err)
62
+ }
63
+
64
+ let instanceId = 0
65
+
8
66
  export default {
9
67
  name: 'rp-treeselect',
10
- mixins: [ treeselectMixin ],
68
+
69
+ provide() {
70
+ return {
71
+ // Enable access to the instance of root component of rp-treeselect
72
+ // across hierarchy.
73
+ instance: this,
74
+ }
75
+ },
76
+
77
+ props: {
78
+ /**
79
+ * Whether to allow resetting value even if there are disabled selected nodes.
80
+ */
81
+ allowClearingDisabled: {
82
+ type: Boolean,
83
+ default: false,
84
+ },
85
+
86
+ /**
87
+ * When an ancestor node is selected/deselected, whether its disabled descendants should be selected/deselected.
88
+ * You may want to use this in conjunction with `allowClearingDisabled` prop.
89
+ */
90
+ allowSelectingDisabledDescendants: {
91
+ type: Boolean,
92
+ default: false,
93
+ },
94
+
95
+ /**
96
+ * Whether the menu should be always open.
97
+ */
98
+ alwaysOpen: {
99
+ type: Boolean,
100
+ default: false,
101
+ },
102
+
103
+ /**
104
+ * Append the menu to <body />?
105
+ */
106
+ appendToBody: {
107
+ type: Boolean,
108
+ default: false,
109
+ },
110
+
111
+ /**
112
+ * Whether to enable async search mode.
113
+ */
114
+ async: {
115
+ type: Boolean,
116
+ default: false,
117
+ },
118
+
119
+ /**
120
+ * Automatically focus the component on mount?
121
+ */
122
+ autoFocus: {
123
+ type: Boolean,
124
+ default: false,
125
+ },
126
+
127
+ /**
128
+ * Automatically load root options on mount. When set to `false`, root options will be loaded when the menu is opened.
129
+ */
130
+ autoLoadRootOptions: {
131
+ type: Boolean,
132
+ default: true,
133
+ },
134
+
135
+ /**
136
+ * When user deselects a node, automatically deselect its ancestors. Applies to flat mode only.
137
+ */
138
+ autoDeselectAncestors: {
139
+ type: Boolean,
140
+ default: false,
141
+ },
142
+
143
+ /**
144
+ * When user deselects a node, automatically deselect its descendants. Applies to flat mode only.
145
+ */
146
+ autoDeselectDescendants: {
147
+ type: Boolean,
148
+ default: false,
149
+ },
150
+
151
+ /**
152
+ * When user selects a node, automatically select its ancestors. Applies to flat mode only.
153
+ */
154
+ autoSelectAncestors: {
155
+ type: Boolean,
156
+ default: false,
157
+ },
158
+
159
+ /**
160
+ * When user selects a node, automatically select its descendants. Applies to flat mode only.
161
+ */
162
+ autoSelectDescendants: {
163
+ type: Boolean,
164
+ default: false,
165
+ },
166
+
167
+ /**
168
+ * Whether pressing backspace key removes the last item if there is no text input.
169
+ */
170
+ backspaceRemoves: {
171
+ type: Boolean,
172
+ default: true,
173
+ },
174
+
175
+ /**
176
+ * Function that processes before clearing all input fields.
177
+ * Return `false` to prevent value from being cleared.
178
+ * @type {function(): (boolean|Promise<boolean>)}
179
+ */
180
+ beforeClearAll: {
181
+ type: Function,
182
+ default: constant(true),
183
+ },
184
+
185
+ /**
186
+ * Show branch nodes before leaf nodes?
187
+ */
188
+ branchNodesFirst: {
189
+ type: Boolean,
190
+ default: false,
191
+ },
192
+
193
+ /**
194
+ * Should cache results of every search request?
195
+ */
196
+ cacheOptions: {
197
+ type: Boolean,
198
+ default: true,
199
+ },
200
+
201
+ /**
202
+ * Show an "×" button that resets value?
203
+ */
204
+ clearable: {
205
+ type: Boolean,
206
+ default: true,
207
+ },
208
+
209
+ /**
210
+ * Title for the "×" button when `multiple: true`.
211
+ */
212
+ clearAllText: {
213
+ type: String,
214
+ default: 'Clear all',
215
+ },
216
+
217
+ /**
218
+ * Whether to clear the search input after selecting.
219
+ * Use only when `multiple` is `true`.
220
+ * For single-select mode, it **always** clears the input after selecting an option regardless of the prop value.
221
+ */
222
+ clearOnSelect: {
223
+ type: Boolean,
224
+ default: false,
225
+ },
226
+
227
+ /**
228
+ * Title for the "×" button.
229
+ */
230
+ clearValueText: {
231
+ type: String,
232
+ default: 'Clear value',
233
+ },
234
+
235
+ /**
236
+ * Whether to close the menu after selecting an option?
237
+ * Use only when `multiple` is `true`.
238
+ */
239
+ closeOnSelect: {
240
+ type: Boolean,
241
+ default: true,
242
+ },
243
+
244
+ /**
245
+ * How many levels of branch nodes should be automatically expanded when loaded.
246
+ * Set `Infinity` to make all branch nodes expanded by default.
247
+ */
248
+ defaultExpandLevel: {
249
+ type: Number,
250
+ default: 0,
251
+ },
252
+
253
+ /**
254
+ * The default set of options to show before the user starts searching. Used for async search mode.
255
+ * When set to `true`, the results for search query as a empty string will be autoloaded.
256
+ * @type {boolean|node[]}
257
+ */
258
+ // eslint-disable-next-line vue/require-prop-types
259
+ defaultOptions: {
260
+ default: false,
261
+ },
262
+
263
+ /**
264
+ * Whether pressing delete key removes the last item if there is no text input.
265
+ */
266
+ deleteRemoves: {
267
+ type: Boolean,
268
+ default: true,
269
+ },
270
+
271
+ /**
272
+ * Delimiter to use to join multiple values for the hidden field value.
273
+ */
274
+ delimiter: {
275
+ type: String,
276
+ default: ',',
277
+ },
278
+
279
+ /**
280
+ * Only show the nodes that match the search value directly, excluding its ancestors.
281
+ *
282
+ * @type {Object}
283
+ */
284
+ flattenSearchResults: {
285
+ type: Boolean,
286
+ default: false,
287
+ },
288
+
289
+ /**
290
+ * Prevent branch nodes from being selected?
291
+ */
292
+ disableBranchNodes: {
293
+ type: Boolean,
294
+ default: false,
295
+ },
296
+
297
+ /**
298
+ * Disable the control?
299
+ */
300
+ disabled: {
301
+ type: Boolean,
302
+ default: false,
303
+ },
304
+
305
+ /**
306
+ * Disable the fuzzy matching functionality?
307
+ */
308
+ disableFuzzyMatching: {
309
+ type: Boolean,
310
+ default: false,
311
+ },
312
+
313
+ /**
314
+ * Whether to enable flat mode or not. Non-flat mode (default) means:
315
+ * - Whenever a branch node gets checked, all its children will be checked too
316
+ * - Whenever a branch node has all children checked, the branch node itself will be checked too
317
+ * Set `true` to disable this mechanism
318
+ */
319
+ flat: {
320
+ type: Boolean,
321
+ default: false,
322
+ },
323
+
324
+ /**
325
+ * Will be passed with all events as the last param.
326
+ * Useful for identifying events origin.
327
+ */
328
+ instanceId: {
329
+ // Add two trailing "$" to distinguish from explictly specified ids.
330
+ default: () => `${instanceId++}$$`,
331
+ type: [ String, Number ],
332
+ },
333
+
334
+ /**
335
+ * Joins multiple values into a single form field with the `delimiter` (legacy mode).
336
+ */
337
+ joinValues: {
338
+ type: Boolean,
339
+ default: false,
340
+ },
341
+
342
+ /**
343
+ * Limit the display of selected options.
344
+ * The rest will be hidden within the limitText string.
345
+ */
346
+ limit: {
347
+ type: Number,
348
+ default: Infinity,
349
+ },
350
+
351
+ /**
352
+ * Function that processes the message shown when selected elements pass the defined limit.
353
+ * @type {function(number): string}
354
+ */
355
+ limitText: {
356
+ type: Function,
357
+ default: function limitTextDefault(count) { // eslint-disable-line func-name-matching
358
+ return `and ${count} more`
359
+ },
360
+ },
361
+
362
+ /**
363
+ * Text displayed when loading options.
364
+ */
365
+ loadingText: {
366
+ type: String,
367
+ default: 'Loading...',
368
+ },
369
+
370
+ /**
371
+ * Used for dynamically loading options.
372
+ * @type {function({action: string, callback: (function((Error|string)=): void), parentNode: node=, instanceId}): void}
373
+ */
374
+ // eslint-disable-next-line vue/require-default-prop
375
+ loadOptions: {
376
+ type: Function,
377
+ },
378
+
379
+ /**
380
+ * Which node properties to filter on.
381
+ */
382
+ matchKeys: {
383
+ type: Array,
384
+ default: constant([ 'label' ]),
385
+ },
386
+
387
+ /**
388
+ * Sets `maxHeight` style value of the menu.
389
+ */
390
+ maxHeight: {
391
+ type: Number,
392
+ default: 300,
393
+ },
394
+
395
+ /**
396
+ * Set `true` to allow selecting multiple options (a.k.a., multi-select mode).
397
+ */
398
+ multiple: {
399
+ type: Boolean,
400
+ default: false,
401
+ },
402
+
403
+ /**
404
+ * Generates a hidden <input /> tag with this field name for html forms.
405
+ */
406
+ // eslint-disable-next-line vue/require-default-prop
407
+ name: {
408
+ type: String,
409
+ },
410
+
411
+ /**
412
+ * Text displayed when a branch node has no children.
413
+ */
414
+ noChildrenText: {
415
+ type: String,
416
+ default: 'No sub-options.',
417
+ },
418
+
419
+ /**
420
+ * Text displayed when there are no available options.
421
+ */
422
+ noOptionsText: {
423
+ type: String,
424
+ default: 'No options available.',
425
+ },
426
+
427
+ /**
428
+ * Text displayed when there are no matching search results.
429
+ */
430
+ noResultsText: {
431
+ type: String,
432
+ default: 'No results found...',
433
+ },
434
+
435
+ /**
436
+ * Used for normalizing source data.
437
+ * @type {function(node, instanceId): node}
438
+ */
439
+ normalizer: {
440
+ type: Function,
441
+ default: identity,
442
+ },
443
+
444
+ /**
445
+ * By default (`auto`), the menu will open below the control. If there is not
446
+ * enough space, rp-treeselect will automatically flip the menu.
447
+ * You can use one of other four options to force the menu to be always opened
448
+ * to specified direction.
449
+ * Acceptable values:
450
+ * - `"auto"`
451
+ * - `"below"`
452
+ * - `"bottom"`
453
+ * - `"above"`
454
+ * - `"top"`
455
+ */
456
+ openDirection: {
457
+ type: String,
458
+ default: 'auto',
459
+ validator(value) {
460
+ const acceptableValues = [ 'auto', 'top', 'bottom', 'above', 'below' ]
461
+ return includes(acceptableValues, value)
462
+ },
463
+ },
464
+
465
+ /**
466
+ * Whether to automatically open the menu when the control is clicked.
467
+ */
468
+ openOnClick: {
469
+ type: Boolean,
470
+ default: true,
471
+ },
472
+
473
+ /**
474
+ * Whether to automatically open the menu when the control is focused.
475
+ */
476
+ openOnFocus: {
477
+ type: Boolean,
478
+ default: false,
479
+ },
480
+
481
+ /**
482
+ * Array of available options.
483
+ * @type {node[]}
484
+ */
485
+ // eslint-disable-next-line vue/require-default-prop
486
+ options: {
487
+ type: Array,
488
+ },
489
+
490
+ /**
491
+ * Field placeholder, displayed when there's no value.
492
+ */
493
+ placeholder: {
494
+ type: String,
495
+ default: 'Select...',
496
+ },
497
+
498
+ /**
499
+ * Applies HTML5 required attribute when needed.
500
+ */
501
+ required: {
502
+ type: Boolean,
503
+ default: false,
504
+ },
505
+
506
+ /**
507
+ * Text displayed asking user whether to retry loading children options.
508
+ */
509
+ retryText: {
510
+ type: String,
511
+ default: 'Retry?',
512
+ },
513
+
514
+ /**
515
+ * Title for the retry button.
516
+ */
517
+ retryTitle: {
518
+ type: String,
519
+ default: 'Click to retry',
520
+ },
521
+
522
+ /**
523
+ * Enable searching feature?
524
+ */
525
+ searchable: {
526
+ type: Boolean,
527
+ default: true,
528
+ },
529
+
530
+ /**
531
+ * Search in ancestor nodes too.
532
+ */
533
+ searchNested: {
534
+ type: Boolean,
535
+ default: false,
536
+ },
537
+
538
+ /**
539
+ * Text tip to prompt for async search.
540
+ */
541
+ searchPromptText: {
542
+ type: String,
543
+ default: 'Type to search...',
544
+ },
545
+
546
+ /**
547
+ * Whether to show a children count next to the label of each branch node.
548
+ */
549
+ showCount: {
550
+ type: Boolean,
551
+ default: false,
552
+ },
553
+
554
+ /**
555
+ * Used in conjunction with `showCount` to specify which type of count number should be displayed.
556
+ * Acceptable values:
557
+ * - "ALL_CHILDREN"
558
+ * - "ALL_DESCENDANTS"
559
+ * - "LEAF_CHILDREN"
560
+ * - "LEAF_DESCENDANTS"
561
+ */
562
+ showCountOf: {
563
+ type: String,
564
+ default: ALL_CHILDREN,
565
+ validator(value) {
566
+ const acceptableValues = [ ALL_CHILDREN, ALL_DESCENDANTS, LEAF_CHILDREN, LEAF_DESCENDANTS ]
567
+ return includes(acceptableValues, value)
568
+ },
569
+ },
570
+
571
+ /**
572
+ * Whether to show children count when searching.
573
+ * Fallbacks to the value of `showCount` when not specified.
574
+ * @type {boolean}
575
+ */
576
+ // eslint-disable-next-line vue/require-default-prop
577
+ showCountOnSearch: null,
578
+
579
+ /**
580
+ * In which order the selected options should be displayed in trigger & sorted in `value` array.
581
+ * Used for multi-select mode only.
582
+ * Acceptable values:
583
+ * - "ORDER_SELECTED"
584
+ * - "LEVEL"
585
+ * - "INDEX"
586
+ */
587
+ sortValueBy: {
588
+ type: String,
589
+ default: ORDER_SELECTED,
590
+ validator(value) {
591
+ const acceptableValues = [ ORDER_SELECTED, LEVEL, INDEX ]
592
+ return includes(acceptableValues, value)
593
+ },
594
+ },
595
+
596
+ /**
597
+ * Tab index of the control.
598
+ */
599
+ tabIndex: {
600
+ type: Number,
601
+ default: 0,
602
+ },
603
+
604
+ /**
605
+ * The value of the control.
606
+ * Should be `id` or `node` object for single-select mode, or an array of `id` or `node` object for multi-select mode.
607
+ * Its format depends on the `valueFormat` prop.
608
+ * For most cases, just use `v-model` instead.
609
+ * @type {?Array}
610
+ */
611
+ // eslint-disable-next-line vue/require-default-prop
612
+ value: null,
613
+
614
+ /**
615
+ * Which kind of nodes should be included in the `value` array in multi-select mode.
616
+ * Acceptable values:
617
+ * - "ALL" - Any node that is checked will be included in the `value` array
618
+ * - "BRANCH_PRIORITY" (default) - If a branch node is checked, all its descendants will be excluded in the `value` array
619
+ * - "LEAF_PRIORITY" - If a branch node is checked, this node itself and its branch descendants will be excluded from the `value` array but its leaf descendants will be included
620
+ * - "ALL_WITH_INDETERMINATE" - Any node that is checked will be included in the `value` array, plus indeterminate nodes
621
+ */
622
+ valueConsistsOf: {
623
+ type: String,
624
+ default: BRANCH_PRIORITY,
625
+ validator(value) {
626
+ const acceptableValues = [ ALL, BRANCH_PRIORITY, LEAF_PRIORITY, ALL_WITH_INDETERMINATE ]
627
+ return includes(acceptableValues, value)
628
+ },
629
+ },
630
+
631
+ /**
632
+ * Format of `value` prop.
633
+ * Note that, when set to `"object"`, only `id` & `label` properties are required in each `node` object in `value` prop.
634
+ * Acceptable values:
635
+ * - "id"
636
+ * - "object"
637
+ */
638
+ valueFormat: {
639
+ type: String,
640
+ default: 'id',
641
+ },
642
+
643
+ /**
644
+ * z-index of the menu.
645
+ */
646
+ zIndex: {
647
+ type: [ Number, String ],
648
+ default: 999,
649
+ },
650
+ },
651
+
652
+ data() {
653
+ return {
654
+ trigger: {
655
+ // Is the control focused?
656
+ isFocused: false,
657
+ // User entered search query - value of the input.
658
+ searchQuery: '',
659
+ },
660
+
661
+ menu: {
662
+ // Is the menu opened?
663
+ isOpen: false,
664
+ // Id of current highlighted option.
665
+ current: null,
666
+ // The scroll position before last menu closing.
667
+ lastScrollPosition: 0,
668
+ // Which direction to open the menu.
669
+ placement: 'bottom',
670
+ },
671
+
672
+ forest: {
673
+ // Normalized options.
674
+ normalizedOptions: [],
675
+ // <id, node> map for quick look-up.
676
+ nodeMap: createMap(),
677
+ // <id, checkedState> map, used for multi-select mode.
678
+ checkedStateMap: createMap(),
679
+ // Id list of all selected options.
680
+ selectedNodeIds: this.extractCheckedNodeIdsFromValue(),
681
+ // <id, true> map for fast checking:
682
+ // if (forest.selectedNodeIds.indexOf(id) !== -1) forest.selectedNodeMap[id] === true
683
+ selectedNodeMap: createMap(),
684
+ },
685
+
686
+ // States of root options.
687
+ rootOptionsStates: createAsyncOptionsStates(),
688
+
689
+ localSearch: {
690
+ // Has user entered any query to search local options?
691
+ active: false,
692
+ // Has any options matched the search query?
693
+ noResults: true,
694
+ // <id, countObject> map for counting matched children/descendants.
695
+ countMap: createMap(),
696
+ },
697
+
698
+ // <searchQuery, remoteSearchEntry> map.
699
+ remoteSearch: createMap(),
700
+ }
701
+ },
11
702
 
12
703
  computed: {
704
+ /* eslint-disable valid-jsdoc */
705
+ /**
706
+ * Normalized nodes that have been selected.
707
+ * @type {node[]}
708
+ */
709
+ selectedNodes() {
710
+ return this.forest.selectedNodeIds.map(this.getNode)
711
+ },
712
+ /**
713
+ * Id list of selected nodes with `sortValueBy` prop applied.
714
+ * @type {nodeId[]}
715
+ */
716
+ internalValue() {
717
+ let internalValue
718
+
719
+ // istanbul ignore else
720
+ if (this.single || this.flat || this.disableBranchNodes || this.valueConsistsOf === ALL) {
721
+ internalValue = this.forest.selectedNodeIds.slice()
722
+ } else if (this.valueConsistsOf === BRANCH_PRIORITY) {
723
+ internalValue = this.forest.selectedNodeIds.filter(id => {
724
+ const node = this.getNode(id)
725
+ if (node.isRootNode) return true
726
+ return !this.isSelected(node.parentNode)
727
+ })
728
+ } else if (this.valueConsistsOf === LEAF_PRIORITY) {
729
+ internalValue = this.forest.selectedNodeIds.filter(id => {
730
+ const node = this.getNode(id)
731
+ if (node.isLeaf) return true
732
+ return node.children.length === 0
733
+ })
734
+ } else if (this.valueConsistsOf === ALL_WITH_INDETERMINATE) {
735
+ const indeterminateNodeIds = []
736
+ internalValue = this.forest.selectedNodeIds.slice()
737
+ this.selectedNodes.forEach(selectedNode => {
738
+ selectedNode.ancestors.forEach(ancestor => {
739
+ if (includes(indeterminateNodeIds, ancestor.id)) return
740
+ if (includes(internalValue, ancestor.id)) return
741
+ indeterminateNodeIds.push(ancestor.id)
742
+ })
743
+ })
744
+ internalValue.push(...indeterminateNodeIds)
745
+ }
746
+
747
+ if (this.sortValueBy === LEVEL) {
748
+ internalValue.sort((a, b) => sortValueByLevel(this.getNode(a), this.getNode(b)))
749
+ } else if (this.sortValueBy === INDEX) {
750
+ internalValue.sort((a, b) => sortValueByIndex(this.getNode(a), this.getNode(b)))
751
+ }
752
+
753
+ return internalValue
754
+ },
755
+ /**
756
+ * Has any option been selected?
757
+ * @type {boolean}
758
+ */
759
+ hasValue() {
760
+ return this.internalValue.length > 0
761
+ },
762
+ /**
763
+ * Single-select mode?
764
+ * @type {boolean}
765
+ */
766
+ single() {
767
+ return !this.multiple
768
+ },
769
+ /**
770
+ * Id list of nodes displayed in the menu. Nodes that are considered NOT visible:
771
+ * - descendants of a collapsed branch node
772
+ * - in local search mode, nodes that are not matched, unless
773
+ * - it's a branch node and has matched descendants
774
+ * - it's a leaf node and its parent node is explicitly set to show all children
775
+ * @type {id[]}
776
+ */
777
+ visibleOptionIds() {
778
+ const visibleOptionIds = []
779
+
780
+ this.traverseAllNodesByIndex(node => {
781
+ if (!this.localSearch.active || this.shouldOptionBeIncludedInSearchResult(node)) {
782
+ visibleOptionIds.push(node.id)
783
+ }
784
+ // Skip the traversal of descendants of a branch node if it's not expanded.
785
+ if (node.isBranch && !this.shouldExpand(node)) {
786
+ return false
787
+ }
788
+ })
789
+
790
+ return visibleOptionIds
791
+ },
792
+ /**
793
+ * Has any option should be displayed in the menu?
794
+ * @type {boolean}
795
+ */
796
+ hasVisibleOptions() {
797
+ return this.visibleOptionIds.length !== 0
798
+ },
799
+ /**
800
+ * Should show children count when searching?
801
+ * @type {boolean}
802
+ */
803
+ showCountOnSearchComputed() {
804
+ // Vue doesn't allow setting default prop value based on another prop value.
805
+ // So use computed property as a workaround.
806
+ // https://github.com/vuejs/vue/issues/6358
807
+ return typeof this.showCountOnSearch === 'boolean'
808
+ ? this.showCountOnSearch
809
+ : this.showCount
810
+ },
811
+ /**
812
+ * Is there any branch node?
813
+ * @type {boolean}
814
+ */
815
+ hasBranchNodes() {
816
+ return this.forest.normalizedOptions.some(rootNode => rootNode.isBranch)
817
+ },
818
+ shouldFlattenOptions() {
819
+ return this.localSearch.active && this.flattenSearchResults
820
+ },
821
+ /* eslint-enable valid-jsdoc */
13
822
  wrapperClass() {
14
823
  return {
15
824
  'rp-treeselect': true,
@@ -29,6 +838,1143 @@
29
838
  },
30
839
  },
31
840
 
841
+ watch: {
842
+ alwaysOpen(newValue) {
843
+ if (newValue) this.openMenu()
844
+ else this.closeMenu()
845
+ },
846
+
847
+ branchNodesFirst() {
848
+ this.initialize()
849
+ },
850
+
851
+ disabled(newValue) {
852
+ // force close the menu after disabling the control
853
+ if (newValue && this.menu.isOpen) this.closeMenu()
854
+ else if (!newValue && !this.menu.isOpen && this.alwaysOpen) this.openMenu()
855
+ },
856
+
857
+ flat() {
858
+ this.initialize()
859
+ },
860
+
861
+ internalValue(newValue, oldValue) {
862
+ const hasChanged = quickDiff(newValue, oldValue)
863
+ // #122
864
+ // Vue would trigger this watcher when `newValue` and `oldValue` are shallow-equal.
865
+ // We emit the `input` event only when the value actually changes.
866
+ if (hasChanged) this.$emit('input', this.getValue(), this.getInstanceId())
867
+ },
868
+
869
+ matchKeys() {
870
+ this.initialize()
871
+ },
872
+
873
+ multiple(newValue) {
874
+ // We need to rebuild the state when switching from single-select mode
875
+ // to multi-select mode.
876
+ // istanbul ignore else
877
+ if (newValue) this.buildForestState()
878
+ },
879
+
880
+ options: {
881
+ handler() {
882
+ if (this.async) return
883
+ // Re-initialize options when the `options` prop has changed.
884
+ this.initialize()
885
+ this.rootOptionsStates.isLoaded = Array.isArray(this.options)
886
+ },
887
+ deep: true,
888
+ immediate: true,
889
+ },
890
+
891
+ 'trigger.searchQuery'() {
892
+ if (this.async) {
893
+ this.handleRemoteSearch()
894
+ } else {
895
+ this.handleLocalSearch()
896
+ }
897
+
898
+ this.$emit('search-change', this.trigger.searchQuery, this.getInstanceId())
899
+ },
900
+
901
+ value() {
902
+ const nodeIdsFromValue = this.extractCheckedNodeIdsFromValue()
903
+ const hasChanged = quickDiff(nodeIdsFromValue, this.internalValue)
904
+ if (hasChanged) this.fixSelectedNodeIds(nodeIdsFromValue)
905
+ },
906
+ },
907
+
908
+ created() {
909
+ this.verifyProps()
910
+ this.resetFlags()
911
+ },
912
+
913
+ mounted() {
914
+ if (this.autoFocus) this.focusInput()
915
+ if (!this.options && !this.async && this.autoLoadRootOptions) this.loadRootOptions()
916
+ if (this.alwaysOpen) this.openMenu()
917
+ if (this.async && this.defaultOptions) this.handleRemoteSearch()
918
+ },
919
+
920
+ destroyed() {
921
+ // istanbul ignore next
922
+ this.toggleClickOutsideEvent(false)
923
+ },
924
+
925
+ methods: {
926
+ verifyProps() {
927
+ warning(
928
+ () => this.async ? this.searchable : true,
929
+ () => 'For async search mode, the value of "searchable" prop must be true.',
930
+ )
931
+
932
+ if (this.options == null && !this.loadOptions) {
933
+ warning(
934
+ () => false,
935
+ () => 'Are you meant to dynamically load options? You need to use "loadOptions" prop.',
936
+ )
937
+ }
938
+
939
+ if (this.flat) {
940
+ warning(
941
+ () => this.multiple,
942
+ () => 'You are using flat mode. But you forgot to add "multiple=true"?',
943
+ )
944
+ }
945
+
946
+ if (!this.flat) {
947
+ const propNames = [
948
+ 'autoSelectAncestors',
949
+ 'autoSelectDescendants',
950
+ 'autoDeselectAncestors',
951
+ 'autoDeselectDescendants',
952
+ ]
953
+
954
+ propNames.forEach(propName => {
955
+ warning(
956
+ () => !this[propName],
957
+ () => `"${propName}" only applies to flat mode.`,
958
+ )
959
+ })
960
+ }
961
+ },
962
+
963
+ resetFlags() {
964
+ this._blurOnSelect = false
965
+ },
966
+
967
+ initialize() {
968
+ const options = this.async
969
+ ? this.getRemoteSearchEntry().options
970
+ : this.options
971
+
972
+ if (Array.isArray(options)) {
973
+ // In case we are re-initializing options, keep the old state tree temporarily.
974
+ const prevNodeMap = this.forest.nodeMap
975
+ this.forest.nodeMap = createMap()
976
+ this.keepDataOfSelectedNodes(prevNodeMap)
977
+ this.forest.normalizedOptions = this.normalize(NO_PARENT_NODE, options, prevNodeMap)
978
+ // Cases that need fixing `selectedNodeIds`:
979
+ // 1) Children options of a checked node have been delayed loaded,
980
+ // we should also mark these children as checked. (multi-select mode)
981
+ // 2) Root options have been delayed loaded, we need to initialize states
982
+ // of these nodes. (multi-select mode)
983
+ // 3) Async search mode.
984
+ this.fixSelectedNodeIds(this.internalValue)
985
+ } else {
986
+ this.forest.normalizedOptions = []
987
+ }
988
+ },
989
+
990
+ getInstanceId() {
991
+ return this.instanceId == null ? this.id : this.instanceId
992
+ },
993
+
994
+ getValue() {
995
+ if (this.valueFormat === 'id') {
996
+ return this.multiple
997
+ ? this.internalValue.slice()
998
+ : this.internalValue[0]
999
+ }
1000
+
1001
+ const rawNodes = this.internalValue.map(id => this.getNode(id).raw)
1002
+ return this.multiple ? rawNodes : rawNodes[0]
1003
+ },
1004
+
1005
+ getNode(nodeId) {
1006
+ warning(
1007
+ () => nodeId != null,
1008
+ () => `Invalid node id: ${nodeId}`,
1009
+ )
1010
+
1011
+ if (nodeId == null) return null
1012
+
1013
+ return nodeId in this.forest.nodeMap
1014
+ ? this.forest.nodeMap[nodeId]
1015
+ : this.createFallbackNode(nodeId)
1016
+ },
1017
+
1018
+ createFallbackNode(id) {
1019
+ // In case there is a default selected node that is not loaded into the tree yet,
1020
+ // we create a fallback node to keep the component working.
1021
+ // When the real data is loaded, we'll override this fake node.
1022
+
1023
+ const raw = this.extractNodeFromValue(id)
1024
+ const label = this.enhancedNormalizer(raw).label || `${id} (unknown)`
1025
+ const fallbackNode = {
1026
+ id,
1027
+ label,
1028
+ ancestors: [],
1029
+ parentNode: NO_PARENT_NODE,
1030
+ isFallbackNode: true,
1031
+ isRootNode: true,
1032
+ isLeaf: true,
1033
+ isBranch: false,
1034
+ isDisabled: false,
1035
+ isNew: false,
1036
+ index: [ -1 ],
1037
+ level: 0,
1038
+ raw,
1039
+ }
1040
+
1041
+ return this.$set(this.forest.nodeMap, id, fallbackNode)
1042
+ },
1043
+
1044
+ extractCheckedNodeIdsFromValue() {
1045
+ if (this.value == null) return []
1046
+
1047
+ if (this.valueFormat === 'id') {
1048
+ return this.multiple
1049
+ ? this.value.slice()
1050
+ : [ this.value ]
1051
+ }
1052
+
1053
+ return (this.multiple ? this.value : [ this.value ])
1054
+ .map(node => this.enhancedNormalizer(node))
1055
+ .map(node => node.id)
1056
+ },
1057
+
1058
+ extractNodeFromValue(id) {
1059
+ const defaultNode = { id }
1060
+
1061
+ if (this.valueFormat === 'id') {
1062
+ return defaultNode
1063
+ }
1064
+
1065
+ const valueArray = this.multiple
1066
+ ? Array.isArray(this.value) ? this.value : []
1067
+ : this.value ? [ this.value ] : []
1068
+ const matched = find(
1069
+ valueArray,
1070
+ node => node && this.enhancedNormalizer(node).id === id,
1071
+ )
1072
+
1073
+ return matched || defaultNode
1074
+ },
1075
+
1076
+ fixSelectedNodeIds(nodeIdListOfPrevValue) {
1077
+ let nextSelectedNodeIds = []
1078
+
1079
+ // istanbul ignore else
1080
+ if (this.single || this.flat || this.disableBranchNodes || this.valueConsistsOf === ALL) {
1081
+ nextSelectedNodeIds = nodeIdListOfPrevValue
1082
+ } else if (this.valueConsistsOf === BRANCH_PRIORITY) {
1083
+ nodeIdListOfPrevValue.forEach(nodeId => {
1084
+ nextSelectedNodeIds.push(nodeId)
1085
+ const node = this.getNode(nodeId)
1086
+ if (node.isBranch) this.traverseDescendantsBFS(node, descendant => {
1087
+ nextSelectedNodeIds.push(descendant.id)
1088
+ })
1089
+ })
1090
+ } else if (this.valueConsistsOf === LEAF_PRIORITY) {
1091
+ const map = createMap()
1092
+ const queue = nodeIdListOfPrevValue.slice()
1093
+ while (queue.length) {
1094
+ const nodeId = queue.shift()
1095
+ const node = this.getNode(nodeId)
1096
+ nextSelectedNodeIds.push(nodeId)
1097
+ if (node.isRootNode) continue
1098
+ if (!(node.parentNode.id in map)) map[node.parentNode.id] = node.parentNode.children.length
1099
+ if (--map[node.parentNode.id] === 0) queue.push(node.parentNode.id)
1100
+ }
1101
+ } else if (this.valueConsistsOf === ALL_WITH_INDETERMINATE) {
1102
+ const map = createMap()
1103
+ const queue = nodeIdListOfPrevValue.filter(nodeId => {
1104
+ const node = this.getNode(nodeId)
1105
+ return node.isLeaf || node.children.length === 0
1106
+ })
1107
+ while (queue.length) {
1108
+ const nodeId = queue.shift()
1109
+ const node = this.getNode(nodeId)
1110
+ nextSelectedNodeIds.push(nodeId)
1111
+ if (node.isRootNode) continue
1112
+ if (!(node.parentNode.id in map)) map[node.parentNode.id] = node.parentNode.children.length
1113
+ if (--map[node.parentNode.id] === 0) queue.push(node.parentNode.id)
1114
+ }
1115
+ }
1116
+
1117
+ const hasChanged = quickDiff(this.forest.selectedNodeIds, nextSelectedNodeIds)
1118
+ // If `nextSelectedNodeIds` doesn't actually differ from old `selectedNodeIds`,
1119
+ // we don't make the assignment to avoid triggering its watchers which may cause
1120
+ // unnecessary calculations.
1121
+ if (hasChanged) this.forest.selectedNodeIds = nextSelectedNodeIds
1122
+
1123
+ this.buildForestState()
1124
+ },
1125
+
1126
+ keepDataOfSelectedNodes(prevNodeMap) {
1127
+ // In case there is any selected node that is not present in the new `options` array.
1128
+ // It could be useful for async search mode.
1129
+ this.forest.selectedNodeIds.forEach(id => {
1130
+ if (!prevNodeMap[id]) return
1131
+ const node = {
1132
+ ...prevNodeMap[id],
1133
+ isFallbackNode: true,
1134
+ }
1135
+ this.$set(this.forest.nodeMap, id, node)
1136
+ })
1137
+ },
1138
+
1139
+ isSelected(node) {
1140
+ // whether a node is selected (single-select mode) or fully-checked (multi-select mode)
1141
+ return this.forest.selectedNodeMap[node.id] === true
1142
+ },
1143
+
1144
+ traverseDescendantsBFS(parentNode, callback) {
1145
+ // istanbul ignore if
1146
+ if (!parentNode.isBranch) return
1147
+ const queue = parentNode.children.slice()
1148
+ while (queue.length) {
1149
+ const currNode = queue[0]
1150
+ if (currNode.isBranch) queue.push(...currNode.children)
1151
+ callback(currNode)
1152
+ queue.shift()
1153
+ }
1154
+ },
1155
+
1156
+ traverseDescendantsDFS(parentNode, callback) {
1157
+ if (!parentNode.isBranch) return
1158
+ parentNode.children.forEach(child => {
1159
+ // deep-level node first
1160
+ this.traverseDescendantsDFS(child, callback)
1161
+ callback(child)
1162
+ })
1163
+ },
1164
+
1165
+ traverseAllNodesDFS(callback) {
1166
+ this.forest.normalizedOptions.forEach(rootNode => {
1167
+ // deep-level node first
1168
+ this.traverseDescendantsDFS(rootNode, callback)
1169
+ callback(rootNode)
1170
+ })
1171
+ },
1172
+
1173
+ traverseAllNodesByIndex(callback) {
1174
+ const walk = parentNode => {
1175
+ parentNode.children.forEach(child => {
1176
+ if (callback(child) !== false && child.isBranch) {
1177
+ walk(child)
1178
+ }
1179
+ })
1180
+ }
1181
+
1182
+ // To simplify the code logic of traversal,
1183
+ // we create a fake root node that holds all the root options.
1184
+ walk({ children: this.forest.normalizedOptions })
1185
+ },
1186
+
1187
+ toggleClickOutsideEvent(enabled) {
1188
+ if (enabled) {
1189
+ document.addEventListener('mousedown', this.handleClickOutside, false)
1190
+ } else {
1191
+ document.removeEventListener('mousedown', this.handleClickOutside, false)
1192
+ }
1193
+ },
1194
+
1195
+ getValueContainer() {
1196
+ return this.$refs.control.$refs['value-container']
1197
+ },
1198
+
1199
+ getInput() {
1200
+ return this.getValueContainer().$refs.input
1201
+ },
1202
+
1203
+ focusInput() {
1204
+ this.getInput().focus()
1205
+ },
1206
+
1207
+ blurInput() {
1208
+ this.getInput().blur()
1209
+ },
1210
+
1211
+ handleMouseDown: onLeftClick(function handleMouseDown(evt) {
1212
+ evt.preventDefault()
1213
+ evt.stopPropagation()
1214
+
1215
+ if (this.disabled) return
1216
+
1217
+ const isClickedOnValueContainer = this.getValueContainer().$el.contains(evt.target)
1218
+ if (isClickedOnValueContainer && !this.menu.isOpen && (this.openOnClick || this.trigger.isFocused)) {
1219
+ this.openMenu()
1220
+ }
1221
+
1222
+ if (this._blurOnSelect) {
1223
+ this.blurInput()
1224
+ } else {
1225
+ // Focus the input or prevent blurring.
1226
+ this.focusInput()
1227
+ }
1228
+
1229
+ this.resetFlags()
1230
+ }),
1231
+
1232
+ handleClickOutside(evt) {
1233
+ // istanbul ignore else
1234
+ if (this.$refs.wrapper && !this.$refs.wrapper.contains(evt.target)) {
1235
+ this.blurInput()
1236
+ this.closeMenu()
1237
+ }
1238
+ },
1239
+
1240
+ handleLocalSearch() {
1241
+ const { searchQuery } = this.trigger
1242
+ const done = () => this.resetHighlightedOptionWhenNecessary(true)
1243
+
1244
+ if (!searchQuery) {
1245
+ // Exit sync search mode.
1246
+ this.localSearch.active = false
1247
+ return done()
1248
+ }
1249
+
1250
+ // Enter sync search mode.
1251
+ this.localSearch.active = true
1252
+
1253
+ // Reset states.
1254
+ this.localSearch.noResults = true
1255
+ this.traverseAllNodesDFS(node => {
1256
+ if (node.isBranch) {
1257
+ node.isExpandedOnSearch = false
1258
+ node.showAllChildrenOnSearch = false
1259
+ node.isMatched = false
1260
+ node.hasMatchedDescendants = false
1261
+ this.$set(this.localSearch.countMap, node.id, {
1262
+ [ALL_CHILDREN]: 0,
1263
+ [ALL_DESCENDANTS]: 0,
1264
+ [LEAF_CHILDREN]: 0,
1265
+ [LEAF_DESCENDANTS]: 0,
1266
+ })
1267
+ }
1268
+ })
1269
+
1270
+ const lowerCasedSearchQuery = searchQuery.trim().toLocaleLowerCase()
1271
+ const splitSearchQuery = lowerCasedSearchQuery.replace(/\s+/g, ' ').split(' ')
1272
+ this.traverseAllNodesDFS(node => {
1273
+ if (this.searchNested && splitSearchQuery.length > 1) {
1274
+ node.isMatched = splitSearchQuery.every(filterValue =>
1275
+ match(false, filterValue, node.nestedSearchLabel),
1276
+ )
1277
+ } else {
1278
+ node.isMatched = this.matchKeys.some(matchKey =>
1279
+ match(!this.disableFuzzyMatching, lowerCasedSearchQuery, node.lowerCased[matchKey]),
1280
+ )
1281
+ }
1282
+
1283
+ if (node.isMatched) {
1284
+ this.localSearch.noResults = false
1285
+ node.ancestors.forEach(ancestor => this.localSearch.countMap[ancestor.id][ALL_DESCENDANTS]++)
1286
+ if (node.isLeaf) node.ancestors.forEach(ancestor => this.localSearch.countMap[ancestor.id][LEAF_DESCENDANTS]++)
1287
+ if (node.parentNode !== NO_PARENT_NODE) {
1288
+ this.localSearch.countMap[node.parentNode.id][ALL_CHILDREN] += 1
1289
+ // istanbul ignore else
1290
+ if (node.isLeaf) this.localSearch.countMap[node.parentNode.id][LEAF_CHILDREN] += 1
1291
+ }
1292
+ }
1293
+
1294
+ if (
1295
+ (node.isMatched || (node.isBranch && node.isExpandedOnSearch)) &&
1296
+ node.parentNode !== NO_PARENT_NODE
1297
+ ) {
1298
+ node.parentNode.isExpandedOnSearch = true
1299
+ node.parentNode.hasMatchedDescendants = true
1300
+ }
1301
+ })
1302
+
1303
+ done()
1304
+ },
1305
+
1306
+ handleRemoteSearch() {
1307
+ const { searchQuery } = this.trigger
1308
+ const entry = this.getRemoteSearchEntry()
1309
+ const done = () => {
1310
+ this.initialize()
1311
+ this.resetHighlightedOptionWhenNecessary(true)
1312
+ }
1313
+
1314
+ if ((searchQuery === '' || this.cacheOptions) && entry.isLoaded) {
1315
+ return done()
1316
+ }
1317
+
1318
+ this.callLoadOptionsProp({
1319
+ action: ASYNC_SEARCH,
1320
+ args: { searchQuery },
1321
+ isPending() {
1322
+ return entry.isLoading
1323
+ },
1324
+ start: () => {
1325
+ entry.isLoading = true
1326
+ entry.isLoaded = false
1327
+ entry.loadingError = ''
1328
+ },
1329
+ succeed: options => {
1330
+ entry.isLoaded = true
1331
+ entry.options = options
1332
+ // When the request completes, the search query may have changed.
1333
+ // We only show these options if they are for the current search query.
1334
+ if (this.trigger.searchQuery === searchQuery) done()
1335
+ },
1336
+ fail: err => {
1337
+ entry.loadingError = getErrorMessage(err)
1338
+ },
1339
+ end: () => {
1340
+ entry.isLoading = false
1341
+ },
1342
+ })
1343
+ },
1344
+
1345
+ getRemoteSearchEntry() {
1346
+ const { searchQuery } = this.trigger
1347
+ const entry = this.remoteSearch[searchQuery] || {
1348
+ ...createAsyncOptionsStates(),
1349
+ options: [],
1350
+ }
1351
+
1352
+ // Vue doesn't support directly watching on objects.
1353
+ this.$watch(
1354
+ () => entry.options,
1355
+ () => {
1356
+ // TODO: potential redundant re-initialization.
1357
+ if (this.trigger.searchQuery === searchQuery) this.initialize()
1358
+ },
1359
+ { deep: true },
1360
+ )
1361
+
1362
+ if (searchQuery === '') {
1363
+ if (Array.isArray(this.defaultOptions)) {
1364
+ entry.options = this.defaultOptions
1365
+ entry.isLoaded = true
1366
+ return entry
1367
+ } else if (this.defaultOptions !== true) {
1368
+ entry.isLoaded = true
1369
+ return entry
1370
+ }
1371
+ }
1372
+
1373
+ if (!this.remoteSearch[searchQuery]) {
1374
+ this.$set(this.remoteSearch, searchQuery, entry)
1375
+ }
1376
+
1377
+ return entry
1378
+ },
1379
+
1380
+ shouldExpand(node) {
1381
+ return this.localSearch.active ? node.isExpandedOnSearch : node.isExpanded
1382
+ },
1383
+
1384
+ shouldOptionBeIncludedInSearchResult(node) {
1385
+ // 1) This option is matched.
1386
+ if (node.isMatched) return true
1387
+ // 2) This option is not matched, but has matched descendant(s).
1388
+ if (node.isBranch && node.hasMatchedDescendants && !this.flattenSearchResults) return true
1389
+ // 3) This option's parent has no matched descendants,
1390
+ // but after being expanded, all its children should be shown.
1391
+ if (!node.isRootNode && node.parentNode.showAllChildrenOnSearch) return true
1392
+ // 4) The default case.
1393
+ return false
1394
+ },
1395
+
1396
+ shouldShowOptionInMenu(node) {
1397
+ if (this.localSearch.active && !this.shouldOptionBeIncludedInSearchResult(node)) {
1398
+ return false
1399
+ }
1400
+ return true
1401
+ },
1402
+
1403
+ getControl() {
1404
+ return this.$refs.control.$el
1405
+ },
1406
+
1407
+ getMenu() {
1408
+ const ref = this.appendToBody ? this.$refs.portal.portalTarget : this
1409
+ const $menu = ref.$refs.menu.$refs.menu
1410
+ return $menu && $menu.nodeName !== '#comment' ? $menu : null
1411
+ },
1412
+
1413
+ setCurrentHighlightedOption(node, scroll = true) {
1414
+ const prev = this.menu.current
1415
+ if (prev != null && prev in this.forest.nodeMap) {
1416
+ this.forest.nodeMap[prev].isHighlighted = false
1417
+ }
1418
+
1419
+ this.menu.current = node.id
1420
+ node.isHighlighted = true
1421
+
1422
+ if (this.menu.isOpen && scroll) {
1423
+ const scrollToOption = () => {
1424
+ const $menu = this.getMenu()
1425
+ const $option = $menu.querySelector(`.rp-treeselect__option[data-id="${node.id}"]`)
1426
+ if ($option) scrollIntoView($menu, $option)
1427
+ }
1428
+
1429
+ // In case `openMenu()` is just called and the menu is not rendered yet.
1430
+ if (this.getMenu()) {
1431
+ scrollToOption()
1432
+ } else {
1433
+ // istanbul ignore next
1434
+ this.$nextTick(scrollToOption)
1435
+ }
1436
+ }
1437
+ },
1438
+
1439
+ resetHighlightedOptionWhenNecessary(forceReset = false) {
1440
+ const { current } = this.menu
1441
+
1442
+ if (
1443
+ forceReset || current == null ||
1444
+ !(current in this.forest.nodeMap) ||
1445
+ !this.shouldShowOptionInMenu(this.getNode(current))
1446
+ ) {
1447
+ this.highlightFirstOption()
1448
+ }
1449
+ },
1450
+
1451
+ highlightFirstOption() {
1452
+ if (!this.hasVisibleOptions) return
1453
+
1454
+ const first = this.visibleOptionIds[0]
1455
+ this.setCurrentHighlightedOption(this.getNode(first))
1456
+ },
1457
+
1458
+ highlightPrevOption() {
1459
+ if (!this.hasVisibleOptions) return
1460
+
1461
+ const prev = this.visibleOptionIds.indexOf(this.menu.current) - 1
1462
+ if (prev === -1) return this.highlightLastOption()
1463
+ this.setCurrentHighlightedOption(this.getNode(this.visibleOptionIds[prev]))
1464
+ },
1465
+
1466
+ highlightNextOption() {
1467
+ if (!this.hasVisibleOptions) return
1468
+
1469
+ const next = this.visibleOptionIds.indexOf(this.menu.current) + 1
1470
+ if (next === this.visibleOptionIds.length) return this.highlightFirstOption()
1471
+ this.setCurrentHighlightedOption(this.getNode(this.visibleOptionIds[next]))
1472
+ },
1473
+
1474
+ highlightLastOption() {
1475
+ if (!this.hasVisibleOptions) return
1476
+
1477
+ const last = getLast(this.visibleOptionIds)
1478
+ this.setCurrentHighlightedOption(this.getNode(last))
1479
+ },
1480
+
1481
+ resetSearchQuery() {
1482
+ this.trigger.searchQuery = ''
1483
+ },
1484
+
1485
+ closeMenu() {
1486
+ if (!this.menu.isOpen || (!this.disabled && this.alwaysOpen)) return
1487
+ this.saveMenuScrollPosition()
1488
+ this.menu.isOpen = false
1489
+ this.toggleClickOutsideEvent(false)
1490
+ this.resetSearchQuery()
1491
+ this.$emit('close', this.getValue(), this.getInstanceId())
1492
+ },
1493
+
1494
+ openMenu() {
1495
+ if (this.disabled || this.menu.isOpen) return
1496
+ this.menu.isOpen = true
1497
+ this.$nextTick(this.resetHighlightedOptionWhenNecessary)
1498
+ this.$nextTick(this.restoreMenuScrollPosition)
1499
+ if (!this.options && !this.async) this.loadRootOptions()
1500
+ this.toggleClickOutsideEvent(true)
1501
+ this.$emit('open', this.getInstanceId())
1502
+ },
1503
+
1504
+ toggleMenu() {
1505
+ if (this.menu.isOpen) {
1506
+ this.closeMenu()
1507
+ } else {
1508
+ this.openMenu()
1509
+ }
1510
+ },
1511
+
1512
+ toggleExpanded(node) {
1513
+ let nextState
1514
+
1515
+ if (this.localSearch.active) {
1516
+ nextState = node.isExpandedOnSearch = !node.isExpandedOnSearch
1517
+ if (nextState) node.showAllChildrenOnSearch = true
1518
+ } else {
1519
+ nextState = node.isExpanded = !node.isExpanded
1520
+ }
1521
+
1522
+ if (nextState && !node.childrenStates.isLoaded) {
1523
+ this.loadChildrenOptions(node)
1524
+ }
1525
+ },
1526
+
1527
+ buildForestState() {
1528
+ const selectedNodeMap = createMap()
1529
+ this.forest.selectedNodeIds.forEach(selectedNodeId => {
1530
+ selectedNodeMap[selectedNodeId] = true
1531
+ })
1532
+ this.forest.selectedNodeMap = selectedNodeMap
1533
+
1534
+ const checkedStateMap = createMap()
1535
+ if (this.multiple) {
1536
+ this.traverseAllNodesByIndex(node => {
1537
+ checkedStateMap[node.id] = UNCHECKED
1538
+ })
1539
+
1540
+ this.selectedNodes.forEach(selectedNode => {
1541
+ checkedStateMap[selectedNode.id] = CHECKED
1542
+
1543
+ if (!this.flat && !this.disableBranchNodes) {
1544
+ selectedNode.ancestors.forEach(ancestorNode => {
1545
+ if (!this.isSelected(ancestorNode)) {
1546
+ checkedStateMap[ancestorNode.id] = INDETERMINATE
1547
+ }
1548
+ })
1549
+ }
1550
+ })
1551
+ }
1552
+ this.forest.checkedStateMap = checkedStateMap
1553
+ },
1554
+
1555
+ enhancedNormalizer(raw) {
1556
+ return {
1557
+ ...raw,
1558
+ ...this.normalizer(raw, this.getInstanceId()),
1559
+ }
1560
+ },
1561
+
1562
+ normalize(parentNode, nodes, prevNodeMap) {
1563
+ let normalizedOptions = nodes
1564
+ .map(node => [ this.enhancedNormalizer(node), node ])
1565
+ .map(([ node, raw ], index) => {
1566
+ this.checkDuplication(node)
1567
+ this.verifyNodeShape(node)
1568
+
1569
+ const { id, label, children, isDefaultExpanded } = node
1570
+ const isRootNode = parentNode === NO_PARENT_NODE
1571
+ const level = isRootNode ? 0 : parentNode.level + 1
1572
+ const isBranch = Array.isArray(children) || children === null
1573
+ const isLeaf = !isBranch
1574
+ const isDisabled = !!node.isDisabled || (!this.flat && !isRootNode && parentNode.isDisabled)
1575
+ const isNew = !!node.isNew
1576
+ const lowerCased = this.matchKeys.reduce((prev, key) => ({
1577
+ ...prev,
1578
+ [key]: stringifyOptionPropValue(node[key]).toLocaleLowerCase(),
1579
+ }), {})
1580
+ const nestedSearchLabel = isRootNode
1581
+ ? lowerCased.label
1582
+ : parentNode.nestedSearchLabel + ' ' + lowerCased.label
1583
+
1584
+ const normalized = this.$set(this.forest.nodeMap, id, createMap())
1585
+ this.$set(normalized, 'id', id)
1586
+ this.$set(normalized, 'label', label)
1587
+ this.$set(normalized, 'level', level)
1588
+ this.$set(normalized, 'ancestors', isRootNode ? [] : [ parentNode ].concat(parentNode.ancestors))
1589
+ this.$set(normalized, 'index', (isRootNode ? [] : parentNode.index).concat(index))
1590
+ this.$set(normalized, 'parentNode', parentNode)
1591
+ this.$set(normalized, 'lowerCased', lowerCased)
1592
+ this.$set(normalized, 'nestedSearchLabel', nestedSearchLabel)
1593
+ this.$set(normalized, 'isDisabled', isDisabled)
1594
+ this.$set(normalized, 'isNew', isNew)
1595
+ this.$set(normalized, 'isMatched', false)
1596
+ this.$set(normalized, 'isHighlighted', false)
1597
+ this.$set(normalized, 'isBranch', isBranch)
1598
+ this.$set(normalized, 'isLeaf', isLeaf)
1599
+ this.$set(normalized, 'isRootNode', isRootNode)
1600
+ this.$set(normalized, 'raw', raw)
1601
+
1602
+ if (isBranch) {
1603
+ const isLoaded = Array.isArray(children)
1604
+
1605
+ this.$set(normalized, 'childrenStates', {
1606
+ ...createAsyncOptionsStates(),
1607
+ isLoaded,
1608
+ })
1609
+ this.$set(normalized, 'isExpanded', typeof isDefaultExpanded === 'boolean'
1610
+ ? isDefaultExpanded
1611
+ : level < this.defaultExpandLevel)
1612
+ this.$set(normalized, 'hasMatchedDescendants', false)
1613
+ this.$set(normalized, 'hasDisabledDescendants', false)
1614
+ this.$set(normalized, 'isExpandedOnSearch', false)
1615
+ this.$set(normalized, 'showAllChildrenOnSearch', false)
1616
+ this.$set(normalized, 'count', {
1617
+ [ALL_CHILDREN]: 0,
1618
+ [ALL_DESCENDANTS]: 0,
1619
+ [LEAF_CHILDREN]: 0,
1620
+ [LEAF_DESCENDANTS]: 0,
1621
+ })
1622
+ this.$set(normalized, 'children', isLoaded
1623
+ ? this.normalize(normalized, children, prevNodeMap)
1624
+ : [])
1625
+
1626
+ if (isDefaultExpanded === true) normalized.ancestors.forEach(ancestor => {
1627
+ ancestor.isExpanded = true
1628
+ })
1629
+
1630
+ if (!isLoaded && typeof this.loadOptions !== 'function') {
1631
+ warning(
1632
+ () => false,
1633
+ () => 'Unloaded branch node detected. "loadOptions" prop is required to load its children.',
1634
+ )
1635
+ } else if (!isLoaded && normalized.isExpanded) {
1636
+ this.loadChildrenOptions(normalized)
1637
+ }
1638
+ }
1639
+
1640
+ normalized.ancestors.forEach(ancestor => ancestor.count[ALL_DESCENDANTS]++)
1641
+ if (isLeaf) normalized.ancestors.forEach(ancestor => ancestor.count[LEAF_DESCENDANTS]++)
1642
+ if (!isRootNode) {
1643
+ parentNode.count[ALL_CHILDREN] += 1
1644
+ if (isLeaf) parentNode.count[LEAF_CHILDREN] += 1
1645
+ if (isDisabled) parentNode.hasDisabledDescendants = true
1646
+ }
1647
+
1648
+ // Preserve previous states.
1649
+ if (prevNodeMap && prevNodeMap[id]) {
1650
+ const prev = prevNodeMap[id]
1651
+
1652
+ normalized.isMatched = prev.isMatched
1653
+ normalized.showAllChildrenOnSearch = prev.showAllChildrenOnSearch
1654
+ normalized.isHighlighted = prev.isHighlighted
1655
+
1656
+ if (prev.isBranch && normalized.isBranch) {
1657
+ normalized.isExpanded = prev.isExpanded
1658
+ normalized.isExpandedOnSearch = prev.isExpandedOnSearch
1659
+ // #97
1660
+ // If `isLoaded` was true, but IS NOT now, we consider this branch node
1661
+ // to be reset to unloaded state by the user of this component.
1662
+ if (prev.childrenStates.isLoaded && !normalized.childrenStates.isLoaded) {
1663
+ // Make sure the node is collapsed, then the user can load its
1664
+ // children again (by expanding).
1665
+ normalized.isExpanded = false
1666
+ // We have reset `childrenStates` and don't want to preserve states here.
1667
+ } else {
1668
+ normalized.childrenStates = { ...prev.childrenStates }
1669
+ }
1670
+ }
1671
+ }
1672
+
1673
+ return normalized
1674
+ })
1675
+
1676
+ if (this.branchNodesFirst) {
1677
+ const branchNodes = normalizedOptions.filter(option => option.isBranch)
1678
+ const leafNodes = normalizedOptions.filter(option => option.isLeaf)
1679
+ normalizedOptions = branchNodes.concat(leafNodes)
1680
+ }
1681
+
1682
+ return normalizedOptions
1683
+ },
1684
+
1685
+ loadRootOptions() {
1686
+ this.callLoadOptionsProp({
1687
+ action: LOAD_ROOT_OPTIONS,
1688
+ isPending: () => {
1689
+ return this.rootOptionsStates.isLoading
1690
+ },
1691
+ start: () => {
1692
+ this.rootOptionsStates.isLoading = true
1693
+ this.rootOptionsStates.loadingError = ''
1694
+ },
1695
+ succeed: () => {
1696
+ this.rootOptionsStates.isLoaded = true
1697
+ // Wait for `options` being re-initialized.
1698
+ this.$nextTick(() => {
1699
+ this.resetHighlightedOptionWhenNecessary(true)
1700
+ })
1701
+ },
1702
+ fail: err => {
1703
+ this.rootOptionsStates.loadingError = getErrorMessage(err)
1704
+ },
1705
+ end: () => {
1706
+ this.rootOptionsStates.isLoading = false
1707
+ },
1708
+ })
1709
+ },
1710
+
1711
+ loadChildrenOptions(parentNode) {
1712
+ // The options may be re-initialized anytime during the loading process.
1713
+ // So `parentNode` can be stale and we use `getNode()` to avoid that.
1714
+
1715
+ const { id, raw } = parentNode
1716
+
1717
+ this.callLoadOptionsProp({
1718
+ action: LOAD_CHILDREN_OPTIONS,
1719
+ args: {
1720
+ // We always pass the raw node instead of the normalized node to any
1721
+ // callback provided by the user of this component.
1722
+ // Because the shape of the raw node is more likely to be closing to
1723
+ // what the back-end API service of the application needs.
1724
+ parentNode: raw,
1725
+ },
1726
+ isPending: () => {
1727
+ return this.getNode(id).childrenStates.isLoading
1728
+ },
1729
+ start: () => {
1730
+ this.getNode(id).childrenStates.isLoading = true
1731
+ this.getNode(id).childrenStates.loadingError = ''
1732
+ },
1733
+ succeed: () => {
1734
+ this.getNode(id).childrenStates.isLoaded = true
1735
+ },
1736
+ fail: err => {
1737
+ this.getNode(id).childrenStates.loadingError = getErrorMessage(err)
1738
+ },
1739
+ end: () => {
1740
+ this.getNode(id).childrenStates.isLoading = false
1741
+ },
1742
+ })
1743
+ },
1744
+
1745
+ callLoadOptionsProp({ action, args, isPending, start, succeed, fail, end }) {
1746
+ if (!this.loadOptions || isPending()) {
1747
+ return
1748
+ }
1749
+
1750
+ start()
1751
+
1752
+ const callback = once((err, result) => {
1753
+ if (err) {
1754
+ fail(err)
1755
+ } else {
1756
+ succeed(result)
1757
+ }
1758
+
1759
+ end()
1760
+ })
1761
+ const result = this.loadOptions({
1762
+ id: this.getInstanceId(),
1763
+ instanceId: this.getInstanceId(),
1764
+ action,
1765
+ ...args,
1766
+ callback,
1767
+ })
1768
+
1769
+ if (isPromise(result)) {
1770
+ result.then(() => {
1771
+ callback()
1772
+ }, err => {
1773
+ callback(err)
1774
+ }).catch(err => {
1775
+ // istanbul ignore next
1776
+ console.error(err)
1777
+ })
1778
+ }
1779
+ },
1780
+
1781
+ checkDuplication(node) {
1782
+ warning(
1783
+ () => !((node.id in this.forest.nodeMap) && !this.forest.nodeMap[node.id].isFallbackNode),
1784
+ () => `Detected duplicate presence of node id ${JSON.stringify(node.id)}. ` +
1785
+ `Their labels are "${this.forest.nodeMap[node.id].label}" and "${node.label}" respectively.`,
1786
+ )
1787
+ },
1788
+
1789
+ verifyNodeShape(node) {
1790
+ warning(
1791
+ () => !(node.children === undefined && node.isBranch === true),
1792
+ () => 'Are you meant to declare an unloaded branch node? ' +
1793
+ '`isBranch: true` is no longer supported, please use `children: null` instead.',
1794
+ )
1795
+ },
1796
+
1797
+ select(node) {
1798
+ if (this.disabled || node.isDisabled) {
1799
+ return
1800
+ }
1801
+
1802
+ if (this.single) {
1803
+ this.clear()
1804
+ }
1805
+
1806
+ const nextState = this.multiple && !this.flat
1807
+ ? this.forest.checkedStateMap[node.id] === UNCHECKED
1808
+ : !this.isSelected(node)
1809
+
1810
+ if (nextState) {
1811
+ this._selectNode(node)
1812
+ } else {
1813
+ this._deselectNode(node)
1814
+ }
1815
+
1816
+ this.buildForestState()
1817
+
1818
+ if (nextState) {
1819
+ this.$emit('select', node.raw, this.getInstanceId())
1820
+ } else {
1821
+ this.$emit('deselect', node.raw, this.getInstanceId())
1822
+ }
1823
+
1824
+ if (this.localSearch.active && nextState && (this.single || this.clearOnSelect)) {
1825
+ this.resetSearchQuery()
1826
+ }
1827
+
1828
+ if (this.single && this.closeOnSelect) {
1829
+ this.closeMenu()
1830
+
1831
+ // istanbul ignore else
1832
+ if (this.searchable) {
1833
+ this._blurOnSelect = true
1834
+ }
1835
+ }
1836
+ },
1837
+
1838
+ clear() {
1839
+ if (this.hasValue) {
1840
+ if (this.single || this.allowClearingDisabled) {
1841
+ this.forest.selectedNodeIds = []
1842
+ } else /* if (this.multiple && !this.allowClearingDisabled) */ {
1843
+ this.forest.selectedNodeIds = this.forest.selectedNodeIds.filter(nodeId =>
1844
+ this.getNode(nodeId).isDisabled,
1845
+ )
1846
+ }
1847
+
1848
+ this.buildForestState()
1849
+ }
1850
+ },
1851
+
1852
+ // This is meant to be called only by `select()`.
1853
+ _selectNode(node) {
1854
+ if (this.single || this.disableBranchNodes) {
1855
+ return this.addValue(node)
1856
+ }
1857
+
1858
+ if (this.flat) {
1859
+ this.addValue(node)
1860
+
1861
+ if (this.autoSelectAncestors) {
1862
+ node.ancestors.forEach(ancestor => {
1863
+ if (!this.isSelected(ancestor) && !ancestor.isDisabled) this.addValue(ancestor)
1864
+ })
1865
+ } else if (this.autoSelectDescendants) {
1866
+ this.traverseDescendantsBFS(node, descendant => {
1867
+ if (!this.isSelected(descendant) && !descendant.isDisabled) this.addValue(descendant)
1868
+ })
1869
+ }
1870
+
1871
+ return
1872
+ }
1873
+
1874
+ const isFullyChecked = (
1875
+ node.isLeaf ||
1876
+ (/* node.isBranch && */!node.hasDisabledDescendants) ||
1877
+ (/* node.isBranch && */this.allowSelectingDisabledDescendants)
1878
+ )
1879
+ if (isFullyChecked) {
1880
+ this.addValue(node)
1881
+ }
1882
+
1883
+ if (node.isBranch) {
1884
+ this.traverseDescendantsBFS(node, descendant => {
1885
+ if (!descendant.isDisabled || this.allowSelectingDisabledDescendants) {
1886
+ this.addValue(descendant)
1887
+ }
1888
+ })
1889
+ }
1890
+
1891
+ if (isFullyChecked) {
1892
+ let curr = node
1893
+ while ((curr = curr.parentNode) !== NO_PARENT_NODE) {
1894
+ if (curr.children.every(this.isSelected)) this.addValue(curr)
1895
+ else break
1896
+ }
1897
+ }
1898
+ },
1899
+
1900
+ // This is meant to be called only by `select()`.
1901
+ _deselectNode(node) {
1902
+ if (this.disableBranchNodes) {
1903
+ return this.removeValue(node)
1904
+ }
1905
+
1906
+ if (this.flat) {
1907
+ this.removeValue(node)
1908
+
1909
+ if (this.autoDeselectAncestors) {
1910
+ node.ancestors.forEach(ancestor => {
1911
+ if (this.isSelected(ancestor) && !ancestor.isDisabled) this.removeValue(ancestor)
1912
+ })
1913
+ } else if (this.autoDeselectDescendants) {
1914
+ this.traverseDescendantsBFS(node, descendant => {
1915
+ if (this.isSelected(descendant) && !descendant.isDisabled) this.removeValue(descendant)
1916
+ })
1917
+ }
1918
+
1919
+ return
1920
+ }
1921
+
1922
+ let hasUncheckedSomeDescendants = false
1923
+ if (node.isBranch) {
1924
+ this.traverseDescendantsDFS(node, descendant => {
1925
+ if (!descendant.isDisabled || this.allowSelectingDisabledDescendants) {
1926
+ this.removeValue(descendant)
1927
+ hasUncheckedSomeDescendants = true
1928
+ }
1929
+ })
1930
+ }
1931
+
1932
+ if (
1933
+ node.isLeaf ||
1934
+ /* node.isBranch && */hasUncheckedSomeDescendants ||
1935
+ /* node.isBranch && */node.children.length === 0
1936
+ ) {
1937
+ this.removeValue(node)
1938
+
1939
+ let curr = node
1940
+ while ((curr = curr.parentNode) !== NO_PARENT_NODE) {
1941
+ if (this.isSelected(curr)) this.removeValue(curr)
1942
+ else break
1943
+ }
1944
+ }
1945
+ },
1946
+
1947
+ addValue(node) {
1948
+ this.forest.selectedNodeIds.push(node.id)
1949
+ this.forest.selectedNodeMap[node.id] = true
1950
+ },
1951
+
1952
+ removeValue(node) {
1953
+ removeFromArray(this.forest.selectedNodeIds, node.id)
1954
+ delete this.forest.selectedNodeMap[node.id]
1955
+ },
1956
+
1957
+ removeLastValue() {
1958
+ if (!this.hasValue) return
1959
+ if (this.single) return this.clear()
1960
+ const lastValue = getLast(this.internalValue)
1961
+ const lastSelectedNode = this.getNode(lastValue)
1962
+ this.select(lastSelectedNode) // deselect
1963
+ },
1964
+
1965
+ saveMenuScrollPosition() {
1966
+ const $menu = this.getMenu()
1967
+ // istanbul ignore else
1968
+ if ($menu) this.menu.lastScrollPosition = $menu.scrollTop
1969
+ },
1970
+
1971
+ restoreMenuScrollPosition() {
1972
+ const $menu = this.getMenu()
1973
+ // istanbul ignore else
1974
+ if ($menu) $menu.scrollTop = this.menu.lastScrollPosition
1975
+ },
1976
+ },
1977
+
32
1978
  render() {
33
1979
  return (
34
1980
  <div ref="wrapper" class={this.wrapperClass}>