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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
var _typeof2 = require('babel-runtime/helpers/typeof');
var _typeof3 = _interopRequireDefault(_typeof2);
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
exports.warnOnlyTreeNode = warnOnlyTreeNode;
exports.arrDel = arrDel;
exports.arrAdd = arrAdd;
exports.posToArr = posToArr;
exports.getPosition = getPosition;
exports.isTreeNode = isTreeNode;
exports.getNodeChildren = getNodeChildren;
exports.isCheckDisabled = isCheckDisabled;
exports.traverseTreeNodes = traverseTreeNodes;
exports.mapChildren = mapChildren;
exports.getDragNodesKeys = getDragNodesKeys;
exports.calcDropPosition = calcDropPosition;
exports.calcSelectedKeys = calcSelectedKeys;
exports.convertDataToTree = convertDataToTree;
exports.convertTreeToEntities = convertTreeToEntities;
exports.parseCheckedKeys = parseCheckedKeys;
exports.conductCheck = conductCheck;
exports.conductExpandParent = conductExpandParent;
exports.getDataAndAria = getDataAndAria;
var _warning = require('warning');
var _warning2 = _interopRequireDefault(_warning);
var _omit = require('omit.js');
var _omit2 = _interopRequireDefault(_omit);
var _TreeNode = require('./TreeNode');
var _TreeNode2 = _interopRequireDefault(_TreeNode);
var _propsUtil = require('../../_util/props-util');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
/* eslint no-loop-func: 0*/
var DRAG_SIDE_RANGE = 0.25;
var DRAG_MIN_GAP = 2;
var onlyTreeNodeWarned = false;
function warnOnlyTreeNode() {
if (onlyTreeNodeWarned) return;
onlyTreeNodeWarned = true;
(0, _warning2['default'])(false, 'Tree only accept TreeNode as children.');
}
function arrDel(list, value) {
var clone = list.slice();
var index = clone.indexOf(value);
if (index >= 0) {
clone.splice(index, 1);
}
return clone;
}
function arrAdd(list, value) {
var clone = list.slice();
if (clone.indexOf(value) === -1) {
clone.push(value);
}
return clone;
}
function posToArr(pos) {
return pos.split('-');
}
function getPosition(level, index) {
return level + '-' + index;
}
function isTreeNode(node) {
return (0, _propsUtil.getSlotOptions)(node).isTreeNode;
}
function getNodeChildren() {
var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
return children.filter(isTreeNode);
}
function isCheckDisabled(node) {
var _ref = (0, _propsUtil.getOptionProps)(node) || {},
disabled = _ref.disabled,
disableCheckbox = _ref.disableCheckbox;
return !!(disabled || disableCheckbox);
}
function traverseTreeNodes(treeNodes, callback) {
function processNode(node, index, parent) {
var children = node ? node.componentOptions.children : treeNodes;
var pos = node ? getPosition(parent.pos, index) : 0;
// Filter children
var childList = getNodeChildren(children);
// Process node if is not root
if (node) {
var key = node.key;
if (!key && (key === undefined || key === null)) {
key = pos;
}
var data = {
node: node,
index: index,
pos: pos,
key: key,
parentPos: parent.node ? parent.pos : null
};
callback(data);
}
// Process children node
childList.forEach(function (subNode, subIndex) {
processNode(subNode, subIndex, { node: node, pos: pos });
});
}
processNode(null);
}
/**
* Use `rc-util` `toArray` to get the children list which keeps the key.
* And return single node if children is only one(This can avoid `key` missing check).
*/
function mapChildren() {
var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
var func = arguments[1];
var list = children.map(func);
if (list.length === 1) {
return list[0];
}
return list;
}
function getDragNodesKeys(treeNodes, node) {
var _getOptionProps = (0, _propsUtil.getOptionProps)(node),
eventKey = _getOptionProps.eventKey,
pos = _getOptionProps.pos;
var dragNodesKeys = [];
traverseTreeNodes(treeNodes, function (_ref2) {
var key = _ref2.key;
dragNodesKeys.push(key);
});
dragNodesKeys.push(eventKey || pos);
return dragNodesKeys;
}
function calcDropPosition(event, treeNode) {
var clientY = event.clientY;
var _treeNode$$refs$selec = treeNode.$refs.selectHandle.getBoundingClientRect(),
top = _treeNode$$refs$selec.top,
bottom = _treeNode$$refs$selec.bottom,
height = _treeNode$$refs$selec.height;
var des = Math.max(height * DRAG_SIDE_RANGE, DRAG_MIN_GAP);
if (clientY <= top + des) {
return -1;
} else if (clientY >= bottom - des) {
return 1;
}
return 0;
}
/**
* Return selectedKeys according with multiple prop
* @param selectedKeys
* @param props
* @returns [string]
*/
function calcSelectedKeys(selectedKeys, props) {
if (!selectedKeys) {
return undefined;
}
var multiple = props.multiple;
if (multiple) {
return selectedKeys.slice();
}
if (selectedKeys.length) {
return [selectedKeys[0]];
}
return selectedKeys;
}
/**
* Since React internal will convert key to string,
* we need do this to avoid `checkStrictly` use number match
*/
// function keyListToString (keyList) {
// if (!keyList) return keyList
// return keyList.map(key => String(key))
// }
var internalProcessProps = function internalProcessProps() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return {
props: (0, _omit2['default'])(props, ['on', 'key', 'class', 'className', 'style']),
on: props.on || {},
'class': props['class'] || props.className,
style: props.style,
key: props.key
};
};
function convertDataToTree(h, treeData, processer) {
if (!treeData) return [];
var _ref3 = processer || {},
_ref3$processProps = _ref3.processProps,
processProps = _ref3$processProps === undefined ? internalProcessProps : _ref3$processProps;
var list = Array.isArray(treeData) ? treeData : [treeData];
return list.map(function (_ref4) {
var children = _ref4.children,
props = (0, _objectWithoutProperties3['default'])(_ref4, ['children']);
var childrenNodes = convertDataToTree(h, children, processer);
return h(
_TreeNode2['default'],
processProps(props),
[childrenNodes]
);
});
}
// TODO: ========================= NEW LOGIC =========================
/**
* Calculate treeNodes entities. `processTreeEntity` is used for `rc-tree-select`
* @param treeNodes
* @param processTreeEntity User can customize the entity
*/
function convertTreeToEntities(treeNodes) {
var _ref5 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
initWrapper = _ref5.initWrapper,
processEntity = _ref5.processEntity,
onProcessFinished = _ref5.onProcessFinished;
var posEntities = new Map();
var keyEntities = new Map();
var wrapper = {
posEntities: posEntities,
keyEntities: keyEntities
};
if (initWrapper) {
wrapper = initWrapper(wrapper) || wrapper;
}
traverseTreeNodes(treeNodes, function (item) {
var node = item.node,
index = item.index,
pos = item.pos,
key = item.key,
parentPos = item.parentPos;
var entity = { node: node, index: index, key: key, pos: pos };
posEntities.set(pos, entity);
keyEntities.set(key, entity);
// Fill children
entity.parent = posEntities.get(parentPos);
if (entity.parent) {
entity.parent.children = entity.parent.children || [];
entity.parent.children.push(entity);
}
if (processEntity) {
processEntity(entity, wrapper);
}
});
if (onProcessFinished) {
onProcessFinished(wrapper);
}
return wrapper;
}
/**
* Parse `checkedKeys` to { checkedKeys, halfCheckedKeys } style
*/
function parseCheckedKeys(keys) {
if (!keys) {
return null;
}
// Convert keys to object format
var keyProps = void 0;
if (Array.isArray(keys)) {
// [Legacy] Follow the api doc
keyProps = {
checkedKeys: keys,
halfCheckedKeys: undefined
};
} else if ((typeof keys === 'undefined' ? 'undefined' : (0, _typeof3['default'])(keys)) === 'object') {
keyProps = {
checkedKeys: keys.checked || undefined,
halfCheckedKeys: keys.halfChecked || undefined
};
} else {
(0, _warning2['default'])(false, '`checkedKeys` is not an array or an object');
return null;
}
// keyProps.checkedKeys = keyListToString(keyProps.checkedKeys)
// keyProps.halfCheckedKeys = keyListToString(keyProps.halfCheckedKeys)
return keyProps;
}
/**
* Conduct check state by the keyList. It will conduct up & from the provided key.
* If the conduct path reach the disabled or already checked / unchecked node will stop conduct.
* @param keyList list of keys
* @param isCheck is check the node or not
* @param keyEntities parsed by `convertTreeToEntities` function in Tree
* @param checkStatus Can pass current checked status for process (usually for uncheck operation)
* @returns {{checkedKeys: [], halfCheckedKeys: []}}
*/
function conductCheck(keyList, isCheck, keyEntities) {
var checkStatus = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var checkedKeys = new Map();
var halfCheckedKeys = new Map(); // Record the key has some child checked (include child half checked)
(checkStatus.checkedKeys || []).forEach(function (key) {
checkedKeys.set(key, true);
});
(checkStatus.halfCheckedKeys || []).forEach(function (key) {
halfCheckedKeys.set(key, true);
});
// Conduct up
function conductUp(key) {
if (checkedKeys.get(key) === isCheck) return;
var entity = keyEntities.get(key);
if (!entity) return;
var children = entity.children,
parent = entity.parent,
node = entity.node;
if (isCheckDisabled(node)) return;
// Check child node checked status
var everyChildChecked = true;
var someChildChecked = false; // Child checked or half checked
(children || []).filter(function (child) {
return !isCheckDisabled(child.node);
}).forEach(function (_ref6) {
var childKey = _ref6.key;
var childChecked = checkedKeys.get(childKey);
var childHalfChecked = halfCheckedKeys.get(childKey);
if (childChecked || childHalfChecked) someChildChecked = true;
if (!childChecked) everyChildChecked = false;
});
// Update checked status
if (isCheck) {
checkedKeys.set(key, everyChildChecked);
} else {
checkedKeys.set(key, false);
}
halfCheckedKeys.set(key, someChildChecked);
if (parent) {
conductUp(parent.key);
}
}
// Conduct down
function conductDown(key) {
if (checkedKeys.get(key) === isCheck) return;
var entity = keyEntities.get(key);
if (!entity) return;
var children = entity.children,
node = entity.node;
if (isCheckDisabled(node)) return;
checkedKeys.set(key, isCheck);
(children || []).forEach(function (child) {
conductDown(child.key);
});
}
function conduct(key) {
var entity = keyEntities.get(key);
if (!entity) {
(0, _warning2['default'])(false, '\'' + key + '\' does not exist in the tree.');
return;
}
var children = entity.children,
parent = entity.parent,
node = entity.node;
checkedKeys.set(key, isCheck);
if (isCheckDisabled(node)) return;
// Conduct down
(children || []).filter(function (child) {
return !isCheckDisabled(child.node);
}).forEach(function (child) {
conductDown(child.key);
});
// Conduct up
if (parent) {
conductUp(parent.key);
}
}
(keyList || []).forEach(function (key) {
conduct(key);
});
var checkedKeyList = [];
var halfCheckedKeyList = [];
// Fill checked list
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = checkedKeys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _ref7 = _step.value;
var _ref8 = (0, _slicedToArray3['default'])(_ref7, 2);
var key = _ref8[0];
var value = _ref8[1];
if (value) {
checkedKeyList.push(key);
}
}
// Fill half checked list
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = halfCheckedKeys[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var _ref9 = _step2.value;
var _ref10 = (0, _slicedToArray3['default'])(_ref9, 2);
var _key = _ref10[0];
var _value = _ref10[1];
if (!checkedKeys.get(_key) && _value) {
halfCheckedKeyList.push(_key);
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2['return']) {
_iterator2['return']();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
return {
checkedKeys: checkedKeyList,
halfCheckedKeys: halfCheckedKeyList
};
}
/**
* If user use `autoExpandParent` we should get the list of parent node
* @param keyList
* @param keyEntities
*/
function conductExpandParent(keyList, keyEntities) {
var expandedKeys = new Map();
function conductUp(key) {
if (expandedKeys.get(key)) return;
var entity = keyEntities.get(key);
if (!entity) return;
expandedKeys.set(key, true);
var parent = entity.parent,
node = entity.node;
if (isCheckDisabled(node)) return;
if (parent) {
conductUp(parent.key);
}
}
(keyList || []).forEach(function (key) {
conductUp(key);
});
return [].concat((0, _toConsumableArray3['default'])(expandedKeys.keys()));
}
/**
* Returns only the data- and aria- key/value pairs
* @param {object} props
*/
function getDataAndAria(props) {
return Object.keys(props).reduce(function (prev, key) {
if (key.substr(0, 5) === 'data-' || key.substr(0, 5) === 'aria-') {
prev[key] = props[key];
}
return prev;
}, {});
}