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

178 lines
4.6 KiB
JavaScript
Raw Normal View History

2018-04-11 13:25:16 +00:00
/* eslint no-loop-func: 0*/
/* eslint no-console:0 */
2019-01-12 03:33:27 +00:00
export function generateData(x = 3, y = 2, z = 1, gData = []) {
2018-04-11 13:25:16 +00:00
// x每一级下的节点总数。y每级节点里有y个节点、存在子节点。z树的level层级数0表示一级
2019-01-12 03:33:27 +00:00
function _loop(_level, _preKey, _tns) {
const preKey = _preKey || '0';
const tns = _tns || gData;
2018-04-11 13:25:16 +00:00
2019-01-12 03:33:27 +00:00
const children = [];
2018-04-11 13:25:16 +00:00
for (let i = 0; i < x; i++) {
2019-01-12 03:33:27 +00:00
const key = `${preKey}-${i}`;
tns.push({ title: `${key}-label`, key: `${key}-key` });
2018-04-11 13:25:16 +00:00
if (i < y) {
2019-01-12 03:33:27 +00:00
children.push(key);
2018-04-11 13:25:16 +00:00
}
}
if (_level < 0) {
2019-01-12 03:33:27 +00:00
return tns;
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
const __level = _level - 1;
2018-04-11 13:25:16 +00:00
children.forEach((key, index) => {
2019-01-12 03:33:27 +00:00
tns[index].children = [];
return _loop(__level, key, tns[index].children);
});
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
_loop(z);
return gData;
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
export function calcTotal(x = 3, y = 2, z = 1) {
2018-04-11 13:25:16 +00:00
/* eslint no-param-reassign:0*/
2019-01-12 03:33:27 +00:00
const rec = n => (n >= 0 ? x * Math.pow(y, n--) + rec(n) : 0);
return rec(z + 1);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
console.log('总节点数单个tree', calcTotal());
2018-04-11 13:25:16 +00:00
// 性能测试:总节点数超过 2000z要小明显感觉慢。z 变大时,递归多,会卡死。
2019-01-12 03:33:27 +00:00
export const gData = generateData();
2018-04-11 13:25:16 +00:00
2019-01-12 03:33:27 +00:00
function isPositionPrefix(smallPos, bigPos) {
2018-04-11 13:25:16 +00:00
if (bigPos.length < smallPos.length) {
2019-01-12 03:33:27 +00:00
return false;
2018-04-11 13:25:16 +00:00
}
// attention: "0-0-1" "0-0-10"
2019-01-12 03:33:27 +00:00
if (bigPos.length > smallPos.length && bigPos.charAt(smallPos.length) !== '-') {
return false;
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
return bigPos.substr(0, smallPos.length) === smallPos;
2018-04-11 13:25:16 +00:00
}
// console.log(isPositionPrefix("0-1", "0-10-1"));
// arr.length === 628, use time: ~20ms
2019-01-12 03:33:27 +00:00
export function filterParentPosition(arr) {
const levelObj = {};
arr.forEach(item => {
const posLen = item.split('-').length;
2018-04-11 13:25:16 +00:00
if (!levelObj[posLen]) {
2019-01-12 03:33:27 +00:00
levelObj[posLen] = [];
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
levelObj[posLen].push(item);
});
const levelArr = Object.keys(levelObj).sort();
2018-04-11 13:25:16 +00:00
for (let i = 0; i < levelArr.length; i++) {
if (levelArr[i + 1]) {
levelObj[levelArr[i]].forEach(ii => {
for (let j = i + 1; j < levelArr.length; j++) {
levelObj[levelArr[j]].forEach((_i, index) => {
if (isPositionPrefix(ii, _i)) {
2019-01-12 03:33:27 +00:00
levelObj[levelArr[j]][index] = null;
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
});
levelObj[levelArr[j]] = levelObj[levelArr[j]].filter(p => p);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
}
}
2019-01-12 03:33:27 +00:00
let nArr = [];
2018-04-11 13:25:16 +00:00
levelArr.forEach(i => {
2019-01-12 03:33:27 +00:00
nArr = nArr.concat(levelObj[i]);
});
return nArr;
2018-04-11 13:25:16 +00:00
}
// console.log(filterParentPosition(
// ['0-2', '0-3-3', '0-10', '0-10-0', '0-0-1', '0-0', '0-1-1', '0-1']
// ));
2019-01-12 03:33:27 +00:00
function loopData(data, callback) {
2018-04-11 13:25:16 +00:00
const loop = (d, level = 0) => {
d.forEach((item, index) => {
2019-01-12 03:33:27 +00:00
const pos = `${level}-${index}`;
2018-04-11 13:25:16 +00:00
if (item.children) {
2019-01-12 03:33:27 +00:00
loop(item.children, pos);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
callback(item, index, pos);
});
};
loop(data);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
function spl(str) {
return str.split('-');
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
function splitLen(str) {
return str.split('-').length;
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
export function getFilterExpandedKeys(data, expandedKeys) {
const expandedPosArr = [];
2018-04-11 13:25:16 +00:00
loopData(data, (item, index, pos) => {
if (expandedKeys.indexOf(item.key) > -1) {
2019-01-12 03:33:27 +00:00
expandedPosArr.push(pos);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
});
const filterExpandedKeys = [];
2018-04-11 13:25:16 +00:00
loopData(data, (item, index, pos) => {
expandedPosArr.forEach(p => {
2019-01-12 03:33:27 +00:00
if (
((splitLen(pos) < splitLen(p) && p.indexOf(pos) === 0) || pos === p) &&
filterExpandedKeys.indexOf(item.key) === -1
) {
filterExpandedKeys.push(item.key);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
});
});
return filterExpandedKeys;
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
function isSibling(pos, pos1) {
pos.pop();
pos1.pop();
return pos.join(',') === pos1.join(',');
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
export function getRadioSelectKeys(data, selectedKeys, key) {
const res = [];
const pkObjArr = [];
const selPkObjArr = [];
2018-04-11 13:25:16 +00:00
loopData(data, (item, index, pos) => {
if (selectedKeys.indexOf(item.key) > -1) {
2019-01-12 03:33:27 +00:00
pkObjArr.push([pos, item.key]);
2018-04-11 13:25:16 +00:00
}
if (key && key === item.key) {
2019-01-12 03:33:27 +00:00
selPkObjArr.push(pos, item.key);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
});
const lenObj = {};
2018-04-11 13:25:16 +00:00
const getPosKey = (pos, k) => {
2019-01-12 03:33:27 +00:00
const posLen = splitLen(pos);
2018-04-11 13:25:16 +00:00
if (!lenObj[posLen]) {
2019-01-12 03:33:27 +00:00
lenObj[posLen] = [[pos, k]];
2018-04-11 13:25:16 +00:00
} else {
lenObj[posLen].forEach((pkArr, i) => {
if (isSibling(spl(pkArr[0]), spl(pos))) {
// 后来覆盖前者
2019-01-12 03:33:27 +00:00
lenObj[posLen][i] = [pos, k];
2018-04-11 13:25:16 +00:00
} else if (spl(pkArr[0]) !== spl(pos)) {
2019-01-12 03:33:27 +00:00
lenObj[posLen].push([pos, k]);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
};
pkObjArr.forEach(pk => {
getPosKey(pk[0], pk[1]);
});
2018-04-11 13:25:16 +00:00
if (key) {
2019-01-12 03:33:27 +00:00
getPosKey(selPkObjArr[0], selPkObjArr[1]);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
Object.keys(lenObj).forEach(item => {
lenObj[item].forEach(i => {
2018-04-11 13:25:16 +00:00
if (res.indexOf(i[1]) === -1) {
2019-01-12 03:33:27 +00:00
res.push(i[1]);
2018-04-11 13:25:16 +00:00
}
2019-01-12 03:33:27 +00:00
});
});
return res;
2018-04-11 13:25:16 +00:00
}