PromoCursed/node_modules/antd/lib/tree/utils/dictUtil.js
2024-08-20 23:25:37 +04:00

81 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.calcRangeKeys = calcRangeKeys;
exports.convertDirectoryKeysToNodes = convertDirectoryKeysToNodes;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _treeUtil = require("rc-tree/lib/utils/treeUtil");
const RECORD_NONE = 0;
const RECORD_START = 1;
const RECORD_END = 2;
function traverseNodesKey(treeData, callback, fieldNames) {
const {
key: fieldKey,
children: fieldChildren
} = fieldNames;
function processNode(dataNode) {
const key = dataNode[fieldKey];
const children = dataNode[fieldChildren];
if (callback(key, dataNode) !== false) {
traverseNodesKey(children || [], callback, fieldNames);
}
}
treeData.forEach(processNode);
}
/** 计算选中范围只考虑expanded情况以优化性能 */
function calcRangeKeys(_ref) {
let {
treeData,
expandedKeys,
startKey,
endKey,
fieldNames
} = _ref;
const keys = [];
let record = RECORD_NONE;
if (startKey && startKey === endKey) {
return [startKey];
}
if (!startKey || !endKey) {
return [];
}
function matchKey(key) {
return key === startKey || key === endKey;
}
traverseNodesKey(treeData, key => {
if (record === RECORD_END) {
return false;
}
if (matchKey(key)) {
// Match test
keys.push(key);
if (record === RECORD_NONE) {
record = RECORD_START;
} else if (record === RECORD_START) {
record = RECORD_END;
return false;
}
} else if (record === RECORD_START) {
// Append selection
keys.push(key);
}
return expandedKeys.includes(key);
}, (0, _treeUtil.fillFieldNames)(fieldNames));
return keys;
}
function convertDirectoryKeysToNodes(treeData, keys, fieldNames) {
const restKeys = (0, _toConsumableArray2.default)(keys);
const nodes = [];
traverseNodesKey(treeData, (key, node) => {
const index = restKeys.indexOf(key);
if (index !== -1) {
nodes.push(node);
restKeys.splice(index, 1);
}
return !!restKeys.length;
}, (0, _treeUtil.fillFieldNames)(fieldNames));
return nodes;
}