-
Notifications
You must be signed in to change notification settings - Fork 141
/
useTreeDataStrategy.ts
429 lines (379 loc) Β· 13.9 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
* @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 { VKey } from '@idux/cdk/utils'
import type { TransferDataStrategyProp } from '@idux/components/transfer'
import type { GetKeyFn } from '@idux/components/utils'
import { type ComputedRef, type Ref, ref, watch } from 'vue'
import { type CascaderStrategy } from '@idux/components/cascader'
import { combineTrees, filterTree, flattenTree, genFlattenedTreeKeys, traverseTree } from '../utils'
import { type TreeDataStrategyContext, useTreeDataStrategyContext } from './useTreeDataStrategyContext'
export function useTreeDataStrategies<C extends VKey>(
props: ProTransferProps,
childrenKey: ComputedRef<C>,
cascadeStrategy: ComputedRef<CascaderStrategy>,
): {
context: TreeDataStrategyContext<C>
mergedDataStrategy: Ref<TransferDataStrategyProp<TreeTransferData<C>>>
} {
const context = useTreeDataStrategyContext(props, childrenKey)
const getMergedDataStrategy = () => ({
...context.baseDataStrategy,
...createStrategy(context, childrenKey, cascadeStrategy.value, props.flatTargetData),
})
const mergedDataStrategy = ref<TransferDataStrategyProp<TreeTransferData<C>>>(getMergedDataStrategy())
watch([cascadeStrategy, childrenKey], () => {
mergedDataStrategy.value = getMergedDataStrategy()
})
return {
context,
mergedDataStrategy,
}
}
const commonRemoveFn = (keys: VKey[], selectedKeySet: VKey[], parentKeyMap: Map<VKey, VKey | undefined>) => {
const newKeys = new Set(selectedKeySet)
keys.forEach(key => {
let currentKey: VKey | undefined = key
while (currentKey) {
newKeys.delete(currentKey)
currentKey = parentKeyMap.get(currentKey!)
}
})
return Array.from(newKeys)
}
const getAllSelectedKeys = <C extends VKey>(
selected: boolean,
data: TreeTransferData<C>[],
selectedKeySet: Set<VKey>,
disabledkeySet: Set<VKey>,
getKey: GetKeyFn,
childrenKey: C,
) => {
let tempKeys: Set<VKey>
if (!selected) {
tempKeys = new Set()
disabledkeySet.forEach(key => {
selectedKeySet.has(key) && tempKeys.add(key)
})
} else {
tempKeys = new Set(genFlattenedTreeKeys(data, childrenKey, getKey))
disabledkeySet.forEach(key => {
!selectedKeySet.has(key) && tempKeys.delete(key)
})
}
return Array.from(tempKeys)
}
const genDisabledKeys = <C extends VKey>(
data: TreeTransferData<C>[],
getKey: (item: TreeTransferData<C>) => VKey,
childrenKey: C,
cascade: boolean,
): Set<VKey> => {
const keys = new Set<VKey>()
traverseTree(
data,
childrenKey,
(item, parents) => {
const key = getKey(item)
const cihldren = item[childrenKey]
if (
item.disabled ||
(cascade &&
(parents.some(parent => parent.disabled) ||
(cihldren?.length && cihldren.every(child => keys.has(getKey(child))))))
) {
keys.add(key)
}
},
'post',
)
return keys
}
function createSeparateDataSourceFn<C extends VKey>(
childrenKey: ComputedRef<C>,
cachedTargetData: Ref<TreeTransferData<C>[]>,
cascaderStrategy: CascaderStrategy,
targetDataCount: Ref<number>,
): Exclude<TransferDataStrategyProp<TreeTransferData<C>>['separateDataSource'], undefined> {
const targetDataKeySet = new Set<VKey>()
const getFilterFn = (
selectedKeySet: Set<VKey>,
getKey: GetKeyFn,
): ((data: TreeTransferData<C>[], isSource: boolean) => TreeTransferData<C>[]) => {
// 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: TreeTransferData<C>, parent: TreeTransferData<C>[], 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.value]?.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.value,
(item, parent) => {
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) {
targetDataKeySet.add(getKey(item))
}
return filterRes
},
'or',
)
}
const res: TreeTransferData<C>[] = []
traverseTree(data, childrenKey.value, (item, parent) => {
const key = getKey(item)
if (filterFn(item, parent, isSource) && !targetDataKeySet.has(key)) {
targetDataKeySet.add(key)
res.push({ ...item, [childrenKey.value]: undefined })
}
})
return res
}
}
return (data, _, selectedKeySet, getKey) => {
const filterData = getFilterFn(selectedKeySet, getKey)
const newTargetData = filterData(data, false)
const previousTargetData = filterData(cachedTargetData.value, false)
targetDataCount.value = targetDataKeySet.size
targetDataKeySet.clear()
// combine new data with previous data
// beacause we intend to cache selected data after dataSource changes
const targetData = combineTrees(newTargetData, previousTargetData, childrenKey.value, getKey)
cachedTargetData.value = targetData
return {
sourceData: filterData(data, true),
targetData,
}
}
}
function createSeparateDataSourceFnWithFlatten<C extends VKey>(
childrenKey: ComputedRef<C>,
cachedTargetData: Ref<TreeTransferData<C>[]>,
cascaderStrategy: CascaderStrategy,
targetDataCount: Ref<number>,
flatTargetData: boolean | 'all' = false,
): Exclude<TransferDataStrategyProp<TreeTransferData<C>>['separateDataSource'], undefined> {
const fn = createSeparateDataSourceFn(childrenKey, cachedTargetData, cascaderStrategy, targetDataCount)
return (...args) => {
const { sourceData, targetData } = fn(...args)
return {
sourceData,
targetData:
cascaderStrategy === 'off' ? targetData : flattenTargetTree(targetData, childrenKey.value, flatTargetData),
}
}
}
function flattenTargetTree<C extends VKey>(
data: TreeTransferData<C>[],
childrenKey: C,
flatTargetData: boolean | 'all',
) {
if (!flatTargetData) {
return data
}
return flattenTree(data, childrenKey, item => ({ ...item, children: undefined }), flatTargetData !== 'all')
}
function createStrategy<C extends VKey>(
context: TreeDataStrategyContext<C>,
childrenKey: ComputedRef<C>,
cascaderStrategy: CascaderStrategy,
flatTargetData: boolean | 'all',
) {
switch (cascaderStrategy) {
case 'parent':
return createParentStrategy(context, childrenKey, flatTargetData)
case 'child':
return createChildStrategy(context, childrenKey, flatTargetData)
case 'off':
return createOffStrategy(context, childrenKey, flatTargetData)
case 'all':
default:
return createAllStrategy(context, childrenKey, flatTargetData)
}
}
function createAllStrategy<C extends VKey>(
context: TreeDataStrategyContext<C>,
childrenKey: ComputedRef<C>,
flatTargetData: boolean | 'all',
): TransferDataStrategyProp<TreeTransferData<C>> {
const { cachedTargetData, parentKeyMap, targetDataCount } = context
return {
genDisabledKeys: (data, getKey) => genDisabledKeys(data, getKey, childrenKey.value, true),
separateDataSource: createSeparateDataSourceFnWithFlatten(
childrenKey,
cachedTargetData,
'all',
targetDataCount,
flatTargetData,
),
getAllSelectedKeys: (selected, data, selectedKeySet, disabledKeySet, getKey) =>
getAllSelectedKeys(selected, data, selectedKeySet, disabledKeySet, getKey, childrenKey.value),
remove: (keys, selectedKeySet) => commonRemoveFn(keys, selectedKeySet, parentKeyMap),
}
}
function createChildStrategy<C extends VKey>(
context: TreeDataStrategyContext<C>,
childrenKey: ComputedRef<C>,
flatTargetData: boolean | 'all',
): TransferDataStrategyProp<TreeTransferData<C>> {
const { cachedTargetData, parentKeyMap, targetDataCount } = context
return {
genDisabledKeys: (data, getKey) => genDisabledKeys(data, getKey, childrenKey.value, true),
getAllSelectedKeys(selected, data, selectedKeySet, disabledKeySet, getKey) {
if (!selected) {
return Array.from(selectedKeySet).filter(key => disabledKeySet.has(key))
}
const keys: VKey[] = []
flattenTree(data, childrenKey.value, undefined, true).forEach(node => {
const key = getKey(node)
if (selectedKeySet.has(key)) {
keys.push(key)
} else if (!disabledKeySet.has(key)) {
keys.push(key)
}
})
return keys
},
separateDataSource: createSeparateDataSourceFnWithFlatten(
childrenKey,
cachedTargetData,
'child',
targetDataCount,
flatTargetData,
),
remove: (keys, selectedKeySet) => commonRemoveFn(keys, selectedKeySet, parentKeyMap),
}
}
function createParentStrategy<C extends VKey>(
context: TreeDataStrategyContext<C>,
childrenKey: ComputedRef<C>,
flatTargetData: boolean | 'all',
): TransferDataStrategyProp<TreeTransferData<C>> {
const { cachedTargetData, dataKeyMap, targetDataCount } = context
const separateDataSource = createSeparateDataSourceFn(childrenKey, cachedTargetData, 'parent', targetDataCount)
return {
genDisabledKeys: (data, getKey) => genDisabledKeys(data, getKey, childrenKey.value, true),
getAllSelectedKeys: (selected, data, selectedKeySet, disabledKeySet, getKey) => {
const keys: VKey[] = []
if (selected) {
traverseTree(data, childrenKey.value, (item, parents) => {
const key = getKey(item)
if (disabledKeySet.has(key)) {
;[...parents, item].some(node => selectedKeySet.has(getKey(node))) && keys.push(key)
return
}
if (!item[childrenKey.value]?.length) {
keys.push(key)
} else if (item[childrenKey.value]!.every(child => !disabledKeySet.has(getKey(child)))) {
keys.push(key)
}
})
} else {
traverseTree(data, childrenKey.value, (item, parents) => {
const key = getKey(item)
if (!disabledKeySet.has(key)) {
return
}
if ([...parents, item].some(node => selectedKeySet.has(getKey(node)))) {
keys.push(key)
return
}
})
}
return keys
},
separateDataSource(data, _, selectedKeySet, getKey) {
const { sourceData, targetData } = separateDataSource(data, _, selectedKeySet, getKey)
return {
sourceData,
targetData: flattenTargetTree(targetData, childrenKey.value, flatTargetData),
}
},
append(keys, selectedKey, getKey) {
const newKeySet = new Set([...selectedKey, ...keys])
keys.forEach(key => {
const children = dataKeyMap.get(key)?.[childrenKey.value]
if (children) {
traverseTree(children, childrenKey.value, item => {
newKeySet.delete(getKey(item))
})
}
})
return Array.from(newKeySet)
},
remove(keys: VKey[], selectedKey: VKey[], getKey: GetKeyFn) {
const keySet = new Set(keys)
const newKeySet = new Set(selectedKey)
const deletedKeys = new Set()
// store already deleted keys
const deleteKey = (key: VKey) => {
deletedKeys.add(key)
newKeySet.delete(key)
}
// it is not possible to get the correct selected keys from bottom to top
traverseTree(cachedTargetData.value, childrenKey.value, (item, parents) => {
if (keySet.has(getKey(item))) {
const keysInChain = [item, ...parents].map(getKey)
const selectedKeyIdx = keysInChain.findIndex(key => newKeySet.has(key))
// if one of the ancestors was selected
// replace parent node with child nodes
if (selectedKeyIdx > -1) {
// only travers chain from selected node to the bottom
parents.slice(0, selectedKeyIdx).forEach(parent => {
parent[childrenKey.value]?.forEach(child => {
const childKey = getKey(child)
// add only if the child node hasn't been deleted before
if (!deletedKeys.has(childKey)) {
newKeySet.add(childKey)
}
})
})
// not one of the ancestors should be selected
keysInChain.slice(0, selectedKeyIdx + 1).forEach(key => deleteKey(key))
}
} else if (parents.some(parent => keySet.has(getKey(parent)))) {
// if some of the ancestors was removed
// remove the node itself
deleteKey(getKey(item))
}
})
return Array.from(newKeySet)
},
}
}
function createOffStrategy<C extends VKey>(
context: TreeDataStrategyContext<C>,
childrenKey: ComputedRef<C>,
flatTargetData: boolean | 'all',
): TransferDataStrategyProp<TreeTransferData<C>> {
const { cachedTargetData, targetDataCount } = context
return {
genDisabledKeys: (data, getKey) => genDisabledKeys(data, getKey, childrenKey.value, false),
separateDataSource: createSeparateDataSourceFnWithFlatten(
childrenKey,
cachedTargetData,
'off',
targetDataCount,
flatTargetData,
),
getAllSelectedKeys: (selected, data, selectedKeySet, disabledKeySet, getKey) =>
getAllSelectedKeys(selected, data, selectedKeySet, disabledKeySet, getKey, childrenKey.value),
}
}