-
Notifications
You must be signed in to change notification settings - Fork 138
/
useTreeDataStrategy.ts
211 lines (184 loc) Β· 7.03 KB
/
useTreeDataStrategy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @license
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE
*/
import type { ProTransferProps, TreeTransferData } from '../types'
import type { TransferDataStrategyProp } from '@idux/components/transfer'
import type { GetKeyFn, TreeCheckStateResolver } from '@idux/components/utils'
import { type ComputedRef, type Ref, ref, watch } from 'vue'
import { type VKey, filterTree, flattenTree, mergeTree, traverseTree } from '@idux/cdk/utils'
import { type CascaderStrategy } from '@idux/components/cascader'
import { type TreeDataStrategyContext, useTreeDataStrategyContext } from './useTreeDataStrategyContext'
export function useTreeDataStrategies<V extends TreeTransferData<V, C>, C extends string>(
props: ProTransferProps,
childrenKey: ComputedRef<C>,
getKey: ComputedRef<GetKeyFn>,
cascadeStrategy: ComputedRef<CascaderStrategy>,
): {
context: TreeDataStrategyContext<V, C>
mergedDataStrategy: Ref<TransferDataStrategyProp<V>>
} {
const context = useTreeDataStrategyContext<V, C>(props, childrenKey, getKey, cascadeStrategy)
const getMergedDataStrategy = (): TransferDataStrategyProp<V> => ({
...context.baseDataStrategy,
...createStrategy(context, cascadeStrategy.value, props.flatTargetData),
})
const mergedDataStrategy = ref<TransferDataStrategyProp<V>>(getMergedDataStrategy())
watch([cascadeStrategy, childrenKey], () => {
mergedDataStrategy.value = getMergedDataStrategy()
})
return {
context,
mergedDataStrategy,
}
}
const getAllSelectedKeys = <V extends TreeTransferData<V, C>, C extends string>(
selected: boolean,
checkStateResolver: TreeCheckStateResolver<V, C>,
data: V[],
selectedKeySet: Set<VKey>,
disabledkeySet: Set<VKey>,
) => {
const defaultKeys: VKey[] = []
disabledkeySet.forEach(key => {
if (selected) {
!selectedKeySet.has(key) && defaultKeys.push(key)
} else {
selectedKeySet.has(key) && defaultKeys.push(key)
}
})
return checkStateResolver[selected ? 'getAllCheckedKeys' : 'getAllUncheckedKeys'](data, defaultKeys)
}
const genDisabledKeys = <V extends TreeTransferData<V, C>, C extends string>(
data: V[],
getKey: (item: V) => VKey,
childrenKey: C,
cascade: boolean,
): Set<VKey> => {
const keys = new Set<VKey>()
traverseTree(
data,
childrenKey,
(item, parents) => {
const key = getKey(item)
const children = item[childrenKey]
if (
item.disabled ||
(cascade &&
(parents.some(parent => parent.disabled) ||
(children?.length && children.every(child => keys.has(getKey(child))))))
) {
keys.add(key)
}
},
'post',
)
return keys
}
function createSeparateDataSourceFn<V extends TreeTransferData<V, C>, C extends string>(
childrenKey: C,
cachedTargetData: { value: V[] },
cascaderStrategy: CascaderStrategy,
targetDataCount: Ref<number>,
dataMap: Map<VKey, V>,
flatTargetData: boolean | 'all' = false,
): Exclude<TransferDataStrategyProp<V>['separateDataSource'], undefined> {
const sourceDataKeySet = new Set<VKey>()
const getFilterFn = (selectedKeySet: Set<VKey>, getKey: GetKeyFn): ((data: V[], isSource: boolean) => V[]) => {
// under cascaderStrategy `parent`, selected child nodes are not in selectedKeys
// so we consider the item is selected when its parent exists in selectedKeys
const filterFn: (item: V, parent: V[], isSource: boolean) => boolean = (() => {
switch (cascaderStrategy) {
case 'all':
case 'off':
return (item, _, isSource) => selectedKeySet.has(getKey(item)) !== isSource
case 'child':
return (item, _, isSource) => selectedKeySet.has(getKey(item)) !== isSource && !item[childrenKey]?.length
case 'parent':
return (item, parent, isSource) =>
[item, ...parent].map(getKey).some(key => selectedKeySet.has(key)) !== isSource
}
})()
return (data, isSource) => {
if (isSource || cascaderStrategy !== 'off') {
return filterTree(
data,
childrenKey,
(item, parent, filteredChildren) => {
const filterRes = filterFn(item, parent, isSource)
// set targetDataKeySet to collect targetData count later
// we collect this during filter process to avoid unecessary traveral
if (isSource && (filterRes || filteredChildren?.length)) {
sourceDataKeySet.add(getKey(item))
}
return filterRes
},
'or',
)
}
const res: V[] = []
traverseTree(data, childrenKey, (item, parent) => {
if (filterFn(item, parent, isSource)) {
res.push({ ...item, [childrenKey]: undefined })
}
})
return res
}
}
return (data, _, selectedKeySet, getKey) => {
const filterData = getFilterFn(selectedKeySet, getKey)
sourceDataKeySet.clear()
const sourceData = filterData(data, true)
const newTargetData = filterData(data, false)
const previousTargetData = filterData(cachedTargetData.value, false)
// merge new data with previous data
// beacause we intend to cache selected data after dataSource changes
const targetData = mergeTree(previousTargetData, newTargetData, childrenKey, getKey)
cachedTargetData.value = targetData
targetDataCount.value = cascaderStrategy === 'off' ? targetData.length : dataMap.size - sourceDataKeySet.size
return {
sourceData,
targetData: cascaderStrategy === 'off' ? targetData : flattenTargetTree(targetData, childrenKey, flatTargetData),
}
}
}
function flattenTargetTree<V extends TreeTransferData<V, C>, C extends string>(
data: V[],
childrenKey: C,
flatTargetData: boolean | 'all',
) {
if (!flatTargetData) {
return data
}
return flattenTree(data, childrenKey, item => ({ ...item, children: undefined }), flatTargetData !== 'all')
}
function createStrategy<V extends TreeTransferData<V, C>, C extends string>(
context: TreeDataStrategyContext<V, C>,
cascaderStrategy: CascaderStrategy,
flatTargetData: boolean | 'all',
): TransferDataStrategyProp<V> {
const { cachedTargetData, checkStateResolver, childrenKey, dataMap, targetDataCount } = context
return {
genDisabledKeys: (data, getKey) => genDisabledKeys(data, getKey, childrenKey.value, cascaderStrategy !== 'off'),
getAllSelectedKeys: (selected, data, selectedKeySet, disabledKeySet) =>
getAllSelectedKeys(selected, checkStateResolver, data, selectedKeySet, disabledKeySet),
separateDataSource: createSeparateDataSourceFn(
childrenKey.value,
cachedTargetData,
cascaderStrategy,
targetDataCount,
dataMap,
flatTargetData,
),
append: (keys, selectedKey) => checkStateResolver.appendKeys(selectedKey, keys),
remove: (keys, selectedkey) =>
checkStateResolver.removeKeys(
selectedkey,
cascaderStrategy === 'all' || cascaderStrategy === 'child'
? keys.filter(key => !dataMap.get(key)?.[childrenKey.value]?.length)
: keys,
),
}
}