2022-04-01 02:36:22 +00:00
|
|
|
import { note } from '../../vc-util/warning';
|
2022-01-21 13:58:10 +00:00
|
|
|
import type { Key, DataEntity, DataNode, GetCheckDisabled, BasicDataNode } from '../interface';
|
2021-09-25 08:51:32 +00:00
|
|
|
|
|
|
|
interface ConductReturnType {
|
|
|
|
checkedKeys: Key[];
|
|
|
|
halfCheckedKeys: Key[];
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFromCheckedKeys(halfCheckedKeys: Set<Key>, checkedKeys: Set<Key>) {
|
|
|
|
const filteredKeys = new Set<Key>();
|
|
|
|
halfCheckedKeys.forEach(key => {
|
|
|
|
if (!checkedKeys.has(key)) {
|
|
|
|
filteredKeys.add(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return filteredKeys;
|
|
|
|
}
|
|
|
|
|
2022-01-21 13:58:10 +00:00
|
|
|
export function isCheckDisabled<TreeDataType>(node: TreeDataType) {
|
2021-09-25 08:51:32 +00:00
|
|
|
const { disabled, disableCheckbox, checkable } = (node || {}) as DataNode;
|
|
|
|
return !!(disabled || disableCheckbox) || checkable === false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill miss keys
|
2022-01-21 13:58:10 +00:00
|
|
|
function fillConductCheck<TreeDataType extends BasicDataNode = DataNode>(
|
2021-09-25 08:51:32 +00:00
|
|
|
keys: Set<Key>,
|
2022-01-21 13:58:10 +00:00
|
|
|
levelEntities: Map<number, Set<DataEntity<TreeDataType>>>,
|
2021-09-25 08:51:32 +00:00
|
|
|
maxLevel: number,
|
2022-01-21 13:58:10 +00:00
|
|
|
syntheticGetCheckDisabled: GetCheckDisabled<TreeDataType>,
|
2021-09-25 08:51:32 +00:00
|
|
|
): ConductReturnType {
|
|
|
|
const checkedKeys = new Set<Key>(keys);
|
|
|
|
const halfCheckedKeys = new Set<Key>();
|
|
|
|
|
|
|
|
// Add checked keys top to bottom
|
|
|
|
for (let level = 0; level <= maxLevel; level += 1) {
|
|
|
|
const entities = levelEntities.get(level) || new Set();
|
|
|
|
entities.forEach(entity => {
|
|
|
|
const { key, node, children = [] } = entity;
|
|
|
|
|
|
|
|
if (checkedKeys.has(key) && !syntheticGetCheckDisabled(node)) {
|
|
|
|
children
|
|
|
|
.filter(childEntity => !syntheticGetCheckDisabled(childEntity.node))
|
|
|
|
.forEach(childEntity => {
|
|
|
|
checkedKeys.add(childEntity.key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add checked keys from bottom to top
|
|
|
|
const visitedKeys = new Set<Key>();
|
|
|
|
for (let level = maxLevel; level >= 0; level -= 1) {
|
|
|
|
const entities = levelEntities.get(level) || new Set();
|
|
|
|
entities.forEach(entity => {
|
|
|
|
const { parent, node } = entity;
|
|
|
|
|
|
|
|
// Skip if no need to check
|
|
|
|
if (syntheticGetCheckDisabled(node) || !entity.parent || visitedKeys.has(entity.parent.key)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if parent is disabled
|
|
|
|
if (syntheticGetCheckDisabled(entity.parent.node)) {
|
|
|
|
visitedKeys.add(parent.key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let allChecked = true;
|
|
|
|
let partialChecked = false;
|
|
|
|
|
|
|
|
(parent.children || [])
|
|
|
|
.filter(childEntity => !syntheticGetCheckDisabled(childEntity.node))
|
|
|
|
.forEach(({ key }) => {
|
|
|
|
const checked = checkedKeys.has(key);
|
|
|
|
if (allChecked && !checked) {
|
|
|
|
allChecked = false;
|
|
|
|
}
|
|
|
|
if (!partialChecked && (checked || halfCheckedKeys.has(key))) {
|
|
|
|
partialChecked = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (allChecked) {
|
|
|
|
checkedKeys.add(parent.key);
|
|
|
|
}
|
|
|
|
if (partialChecked) {
|
|
|
|
halfCheckedKeys.add(parent.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
visitedKeys.add(parent.key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
checkedKeys: Array.from(checkedKeys),
|
|
|
|
halfCheckedKeys: Array.from(removeFromCheckedKeys(halfCheckedKeys, checkedKeys)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove useless key
|
2022-01-21 13:58:10 +00:00
|
|
|
function cleanConductCheck<TreeDataType extends BasicDataNode = DataNode>(
|
2021-09-25 08:51:32 +00:00
|
|
|
keys: Set<Key>,
|
|
|
|
halfKeys: Key[],
|
2022-01-21 13:58:10 +00:00
|
|
|
levelEntities: Map<number, Set<DataEntity<TreeDataType>>>,
|
2021-09-25 08:51:32 +00:00
|
|
|
maxLevel: number,
|
2022-01-21 13:58:10 +00:00
|
|
|
syntheticGetCheckDisabled: GetCheckDisabled<TreeDataType>,
|
2021-09-25 08:51:32 +00:00
|
|
|
): ConductReturnType {
|
|
|
|
const checkedKeys = new Set<Key>(keys);
|
|
|
|
let halfCheckedKeys = new Set<Key>(halfKeys);
|
|
|
|
|
|
|
|
// Remove checked keys from top to bottom
|
|
|
|
for (let level = 0; level <= maxLevel; level += 1) {
|
|
|
|
const entities = levelEntities.get(level) || new Set();
|
|
|
|
entities.forEach(entity => {
|
|
|
|
const { key, node, children = [] } = entity;
|
|
|
|
|
|
|
|
if (!checkedKeys.has(key) && !halfCheckedKeys.has(key) && !syntheticGetCheckDisabled(node)) {
|
|
|
|
children
|
|
|
|
.filter(childEntity => !syntheticGetCheckDisabled(childEntity.node))
|
|
|
|
.forEach(childEntity => {
|
|
|
|
checkedKeys.delete(childEntity.key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove checked keys form bottom to top
|
|
|
|
halfCheckedKeys = new Set<Key>();
|
|
|
|
const visitedKeys = new Set<Key>();
|
|
|
|
for (let level = maxLevel; level >= 0; level -= 1) {
|
|
|
|
const entities = levelEntities.get(level) || new Set();
|
|
|
|
|
|
|
|
entities.forEach(entity => {
|
|
|
|
const { parent, node } = entity;
|
|
|
|
|
|
|
|
// Skip if no need to check
|
|
|
|
if (syntheticGetCheckDisabled(node) || !entity.parent || visitedKeys.has(entity.parent.key)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if parent is disabled
|
|
|
|
if (syntheticGetCheckDisabled(entity.parent.node)) {
|
|
|
|
visitedKeys.add(parent.key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let allChecked = true;
|
|
|
|
let partialChecked = false;
|
|
|
|
|
|
|
|
(parent.children || [])
|
|
|
|
.filter(childEntity => !syntheticGetCheckDisabled(childEntity.node))
|
|
|
|
.forEach(({ key }) => {
|
|
|
|
const checked = checkedKeys.has(key);
|
|
|
|
if (allChecked && !checked) {
|
|
|
|
allChecked = false;
|
|
|
|
}
|
|
|
|
if (!partialChecked && (checked || halfCheckedKeys.has(key))) {
|
|
|
|
partialChecked = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!allChecked) {
|
|
|
|
checkedKeys.delete(parent.key);
|
|
|
|
}
|
|
|
|
if (partialChecked) {
|
|
|
|
halfCheckedKeys.add(parent.key);
|
|
|
|
}
|
|
|
|
|
|
|
|
visitedKeys.add(parent.key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
checkedKeys: Array.from(checkedKeys),
|
|
|
|
halfCheckedKeys: Array.from(removeFromCheckedKeys(halfCheckedKeys, checkedKeys)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Conduct with keys.
|
|
|
|
* @param keyList current key list
|
|
|
|
* @param keyEntities key - dataEntity map
|
|
|
|
* @param mode `fill` to fill missing key, `clean` to remove useless key
|
|
|
|
*/
|
2022-01-21 13:58:10 +00:00
|
|
|
export function conductCheck<TreeDataType extends BasicDataNode = DataNode>(
|
2021-09-25 08:51:32 +00:00
|
|
|
keyList: Key[],
|
|
|
|
checked: true | { checked: false; halfCheckedKeys: Key[] },
|
2022-01-21 13:58:10 +00:00
|
|
|
keyEntities: Record<Key, DataEntity<TreeDataType>>,
|
2022-03-20 08:42:00 +00:00
|
|
|
maxLevel: number,
|
|
|
|
levelEntities: Map<number, Set<DataEntity<TreeDataType>>>,
|
2022-01-21 13:58:10 +00:00
|
|
|
getCheckDisabled?: GetCheckDisabled<TreeDataType>,
|
2021-09-25 08:51:32 +00:00
|
|
|
): ConductReturnType {
|
|
|
|
const warningMissKeys: Key[] = [];
|
|
|
|
|
2022-01-21 13:58:10 +00:00
|
|
|
let syntheticGetCheckDisabled: GetCheckDisabled<TreeDataType>;
|
2021-09-25 08:51:32 +00:00
|
|
|
if (getCheckDisabled) {
|
|
|
|
syntheticGetCheckDisabled = getCheckDisabled;
|
|
|
|
} else {
|
|
|
|
syntheticGetCheckDisabled = isCheckDisabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only handle exist keys
|
|
|
|
const keys = new Set<Key>(
|
|
|
|
keyList.filter(key => {
|
|
|
|
const hasEntity = !!keyEntities[key];
|
|
|
|
if (!hasEntity) {
|
|
|
|
warningMissKeys.push(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasEntity;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2022-04-01 02:36:22 +00:00
|
|
|
note(
|
2021-09-25 08:51:32 +00:00
|
|
|
!warningMissKeys.length,
|
|
|
|
`Tree missing follow keys: ${warningMissKeys
|
|
|
|
.slice(0, 100)
|
|
|
|
.map(key => `'${key}'`)
|
|
|
|
.join(', ')}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
let result: ConductReturnType;
|
|
|
|
if (checked === true) {
|
2022-01-21 13:58:10 +00:00
|
|
|
result = fillConductCheck<TreeDataType>(
|
|
|
|
keys,
|
|
|
|
levelEntities,
|
|
|
|
maxLevel,
|
|
|
|
syntheticGetCheckDisabled,
|
|
|
|
);
|
2021-09-25 08:51:32 +00:00
|
|
|
} else {
|
|
|
|
result = cleanConductCheck(
|
|
|
|
keys,
|
|
|
|
checked.halfCheckedKeys,
|
|
|
|
levelEntities,
|
|
|
|
maxLevel,
|
|
|
|
syntheticGetCheckDisabled,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|