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

148 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-07-11 09:51:20 +00:00
/* eslint no-loop-func: 0, no-console: 0 */
2019-01-12 03:33:27 +00:00
export function generateData(x = 3, y = 2, z = 1, gData = []) {
2018-07-11 09:51:20 +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-07-11 09:51:20 +00:00
2019-01-12 03:33:27 +00:00
const children = [];
2018-07-11 09:51:20 +00:00
for (let i = 0; i < x; i++) {
2019-01-12 03:33:27 +00:00
const key = `${preKey}-${i}`;
2018-07-11 09:51:20 +00:00
tns.push({
label: `${key}-label`,
value: `${key}-value`,
key,
disabled: key === '0-0-0-1' || false,
2019-01-12 03:33:27 +00:00
});
2018-07-11 09:51:20 +00:00
if (i < y) {
2019-01-12 03:33:27 +00:00
children.push(key);
2018-07-11 09:51:20 +00:00
}
}
if (_level < 0) {
2019-01-12 03:33:27 +00:00
return tns;
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
const __level = _level - 1;
2018-07-11 09:51:20 +00:00
children.forEach((key, index) => {
2019-01-12 03:33:27 +00:00
tns[index].children = [];
return _loop(__level, key, tns[index].children);
});
2019-01-12 03:33:27 +00:00
return null;
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
_loop(z);
return gData;
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
export function calcTotal(x = 3, y = 2, z = 1) {
/* eslint no-param-reassign:0 */
2019-01-12 03:33:27 +00:00
const rec = n => (n >= 0 ? x * y ** n-- + rec(n) : 0);
return rec(z + 1);
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
console.log('总节点数单个tree', calcTotal());
export const gData = generateData();
2018-07-11 09:51:20 +00:00
2019-01-12 03:33:27 +00:00
export function generateTreeNodes(treeNode) {
const arr = [];
const key = treeNode.eventKey;
2018-07-11 09:51:20 +00:00
for (let i = 0; i < 3; i++) {
2019-01-12 03:33:27 +00:00
arr.push({ label: `${key}-${i}-label`, value: `${key}-${i}-value`, key: `${key}-${i}` });
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
return arr;
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
function setLeaf(treeData, curKey, level) {
2018-07-11 09:51:20 +00:00
const loopLeaf = (data, lev) => {
2019-01-12 03:33:27 +00:00
const l = lev - 1;
data.forEach(item => {
if (
item.key.length > curKey.length
? item.key.indexOf(curKey) !== 0
: curKey.indexOf(item.key) !== 0
) {
return;
2018-07-11 09:51:20 +00:00
}
if (item.children) {
2019-01-12 03:33:27 +00:00
loopLeaf(item.children, l);
2018-07-11 09:51:20 +00:00
} else if (l < 1) {
2019-01-12 03:33:27 +00:00
item.isLeaf = true;
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
});
};
loopLeaf(treeData, level + 1);
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
export function getNewTreeData(treeData, curKey, child, level) {
const loop = data => {
if (level < 1 || curKey.length - 3 > level * 2) return;
data.forEach(item => {
2018-07-11 09:51:20 +00:00
if (curKey.indexOf(item.key) === 0) {
if (item.children) {
2019-01-12 03:33:27 +00:00
loop(item.children);
2018-07-11 09:51:20 +00:00
} else {
2019-01-12 03:33:27 +00:00
item.children = child;
2018-07-11 09:51:20 +00:00
}
}
2019-01-12 03:33:27 +00:00
});
};
loop(treeData);
setLeaf(treeData, curKey, level);
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
function loopData(data, callback) {
2018-07-11 09:51:20 +00:00
const loop = (d, level = 0) => {
d.forEach((item, index) => {
2019-01-12 03:33:27 +00:00
const pos = `${level}-${index}`;
2018-07-11 09:51:20 +00:00
if (item.children) {
2019-01-12 03:33:27 +00:00
loop(item.children, pos);
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
callback(item, index, pos);
});
};
loop(data);
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
function isPositionPrefix(smallPos, bigPos) {
2018-07-11 09:51:20 +00:00
if (bigPos.length < smallPos.length) {
2019-01-12 03:33:27 +00:00
return false;
2018-07-11 09:51:20 +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-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
return bigPos.substr(0, smallPos.length) === smallPos;
2018-07-11 09:51:20 +00:00
}
// console.log(isPositionPrefix("0-1", "0-10-1"));
2019-01-12 03:33:27 +00:00
export function getFilterValue(val, sVal, delVal) {
const allPos = [];
const delPos = [];
2018-07-11 09:51:20 +00:00
loopData(gData, (item, index, pos) => {
if (sVal.indexOf(item.value) > -1) {
2019-01-12 03:33:27 +00:00
allPos.push(pos);
2018-07-11 09:51:20 +00:00
}
if (delVal.indexOf(item.value) > -1) {
2019-01-12 03:33:27 +00:00
delPos.push(pos);
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
});
const newPos = [];
delPos.forEach(item => {
allPos.forEach(i => {
2018-07-11 09:51:20 +00:00
if (isPositionPrefix(item, i) || isPositionPrefix(i, item)) {
// 过滤掉 父级节点 和 所有子节点。
// 因为 node节点 不选时,其 父级节点 和 所有子节点 都不选。
2019-01-12 03:33:27 +00:00
return;
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
newPos.push(i);
});
});
const newVal = [];
2018-07-11 09:51:20 +00:00
if (newPos.length) {
loopData(gData, (item, index, pos) => {
if (newPos.indexOf(pos) > -1) {
2019-01-12 03:33:27 +00:00
newVal.push(item.value);
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
});
2018-07-11 09:51:20 +00:00
}
2019-01-12 03:33:27 +00:00
return newVal;
2018-07-11 09:51:20 +00:00
}