ant-design-vue/components/tree/util.js

75 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-01-12 03:33:27 +00:00
import { getNodeChildren, convertTreeToEntities } from '../vc-tree/src/util';
import { getSlots } from '../_util/props-util';
2018-09-26 14:57:01 +00:00
const Record = {
None: 'node',
Start: 'start',
End: 'end',
2019-01-12 03:33:27 +00:00
};
2018-09-26 14:57:01 +00:00
// TODO: Move this logic into `rc-tree`
2019-01-12 03:33:27 +00:00
function traverseNodesKey(rootChildren, callback) {
const nodeList = getNodeChildren(rootChildren) || [];
2018-09-26 14:57:01 +00:00
2019-01-12 03:33:27 +00:00
function processNode(node) {
const { key } = node;
const children = getSlots(node).default;
2018-09-26 14:57:01 +00:00
if (callback(key) !== false) {
2019-08-07 13:56:30 +00:00
traverseNodesKey(typeof children === 'function' ? children() : children, callback);
2018-09-26 14:57:01 +00:00
}
}
2019-01-12 03:33:27 +00:00
nodeList.forEach(processNode);
2018-09-26 14:57:01 +00:00
}
2019-01-12 03:33:27 +00:00
export function getFullKeyList(children) {
const { keyEntities } = convertTreeToEntities(children);
return [...keyEntities.keys()];
2018-09-26 14:57:01 +00:00
}
/** 计算选中范围只考虑expanded情况以优化性能 */
2019-01-12 03:33:27 +00:00
export function calcRangeKeys(rootChildren, expandedKeys, startKey, endKey) {
const keys = [];
let record = Record.None;
2018-09-26 14:57:01 +00:00
if (startKey && startKey === endKey) {
2019-01-12 03:33:27 +00:00
return [startKey];
2018-09-26 14:57:01 +00:00
}
if (!startKey || !endKey) {
2019-01-12 03:33:27 +00:00
return [];
2018-09-26 14:57:01 +00:00
}
2019-01-12 03:33:27 +00:00
function matchKey(key) {
return key === startKey || key === endKey;
2018-09-26 14:57:01 +00:00
}
2019-01-12 03:33:27 +00:00
traverseNodesKey(rootChildren, key => {
2018-09-26 14:57:01 +00:00
if (record === Record.End) {
2019-01-12 03:33:27 +00:00
return false;
2018-09-26 14:57:01 +00:00
}
if (matchKey(key)) {
// Match test
2019-01-12 03:33:27 +00:00
keys.push(key);
2018-09-26 14:57:01 +00:00
if (record === Record.None) {
2019-01-12 03:33:27 +00:00
record = Record.Start;
2018-09-26 14:57:01 +00:00
} else if (record === Record.Start) {
2019-01-12 03:33:27 +00:00
record = Record.End;
return false;
2018-09-26 14:57:01 +00:00
}
} else if (record === Record.Start) {
// Append selection
2019-01-12 03:33:27 +00:00
keys.push(key);
2018-09-26 14:57:01 +00:00
}
if (expandedKeys.indexOf(key) === -1) {
2019-01-12 03:33:27 +00:00
return false;
2018-09-26 14:57:01 +00:00
}
2019-01-12 03:33:27 +00:00
return true;
});
2018-09-26 14:57:01 +00:00
2019-01-12 03:33:27 +00:00
return keys;
2018-09-26 14:57:01 +00:00
}