Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize node ancestor checks #84

Merged
merged 1 commit into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions examples/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,17 @@ function loopData(data, callback) {
loop(data);
}

function isInclude(smallArray, bigArray) {
return smallArray.every((ii, i) => {
return ii === bigArray[i];
});
function isPositionPrefix(smallPos, bigPos) {
if (bigPos.length < smallPos.length) {
return false;
}
// attention: "0-0-1" "0-0-10"
if ((bigPos.length > smallPos.length) && (bigPos.charAt(smallPos.length) !== '-')) {
return false;
}
return bigPos.substr(0, smallPos.length) === smallPos;
}
// console.log(isInclude(['0', '1'], ['0', '10', '1']));
// console.log(isPositionPrefix("0-1", "0-10-1"));

export function getFilterValue(val, sVal, delVal) {
const allPos = [];
Expand All @@ -117,12 +122,8 @@ export function getFilterValue(val, sVal, delVal) {
});
const newPos = [];
delPos.forEach((item) => {
const nArr = item.split('-');
allPos.forEach((i) => {
const iArr = i.split('-');
if (item === i ||
nArr.length > iArr.length && isInclude(iArr, nArr) ||
nArr.length < iArr.length && isInclude(nArr, iArr)) {
if (isPositionPrefix(item, i) || isPositionPrefix(i, item)) {
// 过滤掉 父级节点 和 所有子节点。
// 因为 node节点 不选时,其 父级节点 和 所有子节点 都不选。
return;
Expand Down
8 changes: 2 additions & 6 deletions src/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
UNSELECTABLE_ATTRIBUTE, UNSELECTABLE_STYLE,
preventDefaultEvent,
getTreeNodesStates, flatToHierarchy, filterParentPosition,
isInclude, labelCompatible, loopAllChildren, filterAllCheckedData,
isPositionPrefix, labelCompatible, loopAllChildren, filterAllCheckedData,
processSimpleTreeData, saveRef,
} from './util';
import SelectTrigger from './SelectTrigger';
Expand Down Expand Up @@ -569,14 +569,10 @@ class Select extends Component {
unCheckPos = itemObj.pos;
}
});
const nArr = unCheckPos && unCheckPos.split('-');
const newVals = [];
const newCkTns = [];
checkedTreeNodes.forEach(itemObj => {
const iArr = itemObj.pos.split('-');
if (itemObj.pos === unCheckPos ||
nArr.length > iArr.length && isInclude(iArr, nArr) ||
nArr.length < iArr.length && isInclude(nArr, iArr)) {
if (isPositionPrefix(itemObj.pos, unCheckPos) || isPositionPrefix(unCheckPos, itemObj.pos)) {
// Filter ancestral and children nodes when uncheck a node.
return;
}
Expand Down
21 changes: 14 additions & 7 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ export function isInclude(smallArray, bigArray) {
});
}

export function isPositionPrefix(smallPos, bigPos) {
if (bigPos.length < smallPos.length) {
return false;
}
// attention: "0-0-1" "0-0-10"
if ((bigPos.length > smallPos.length) && (bigPos.charAt(smallPos.length) !== '-')) {
return false;
}
return bigPos.substr(0, smallPos.length) === smallPos;
}

/*
export function getCheckedKeys(node, checkedKeys, allCheckedNodesKeys) {
const nodeKey = node.props.eventKey;
Expand All @@ -87,13 +98,9 @@ export function getCheckedKeys(node, checkedKeys, allCheckedNodesKeys) {
}
});
if (unCheck) {
const nArr = nodePos.split('-');
newCks = [];
allCheckedNodesKeys.forEach(item => {
const iArr = item.pos.split('-');
if (item.pos === nodePos ||
nArr.length > iArr.length && isInclude(iArr, nArr) ||
nArr.length < iArr.length && isInclude(nArr, iArr)) {
if (isPositionPrefix(item.pos, nodePos) || isPositionPrefix(nodePos, item.pos)) {
return;
}
newCks.push(item.key);
Expand Down Expand Up @@ -179,7 +186,7 @@ export function flatToHierarchy(arr) {
levelObj[pre].forEach((item) => {
let haveParent = false;
levelObj[cur].forEach((ii) => {
if (isInclude(ii.pos.split('-'), item.pos.split('-'))) {
if (isPositionPrefix(ii.pos, item.pos)) {
haveParent = true;
if (!ii.children) {
ii.children = [];
Expand Down Expand Up @@ -215,7 +222,7 @@ export function filterParentPosition(arr) {
levelObj[levelArr[i]].forEach(ii => {
for (let j = i + 1; j < levelArr.length; j++) {
levelObj[levelArr[j]].forEach((_i, index) => {
if (isInclude(ii.split('-'), _i.split('-'))) {
if (isPositionPrefix(ii, _i)) {
levelObj[levelArr[j]][index] = null;
}
});
Expand Down