amis-rpc-design/node_modules/rc-tree/lib/utils/conductUtil.js
2023-10-07 19:42:30 +08:00

204 lines
6.9 KiB
JavaScript

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.conductCheck = conductCheck;
exports.isCheckDisabled = isCheckDisabled;
var _warning = _interopRequireDefault(require("rc-util/lib/warning"));
var _keyUtil = _interopRequireDefault(require("./keyUtil"));
function removeFromCheckedKeys(halfCheckedKeys, checkedKeys) {
var filteredKeys = new Set();
halfCheckedKeys.forEach(function (key) {
if (!checkedKeys.has(key)) {
filteredKeys.add(key);
}
});
return filteredKeys;
}
function isCheckDisabled(node) {
var _ref = node || {},
disabled = _ref.disabled,
disableCheckbox = _ref.disableCheckbox,
checkable = _ref.checkable;
return !!(disabled || disableCheckbox) || checkable === false;
}
// Fill miss keys
function fillConductCheck(keys, levelEntities, maxLevel, syntheticGetCheckDisabled) {
var checkedKeys = new Set(keys);
var halfCheckedKeys = new Set();
// Add checked keys top to bottom
for (var level = 0; level <= maxLevel; level += 1) {
var entities = levelEntities.get(level) || new Set();
entities.forEach(function (entity) {
var key = entity.key,
node = entity.node,
_entity$children = entity.children,
children = _entity$children === void 0 ? [] : _entity$children;
if (checkedKeys.has(key) && !syntheticGetCheckDisabled(node)) {
children.filter(function (childEntity) {
return !syntheticGetCheckDisabled(childEntity.node);
}).forEach(function (childEntity) {
checkedKeys.add(childEntity.key);
});
}
});
}
// Add checked keys from bottom to top
var visitedKeys = new Set();
for (var _level = maxLevel; _level >= 0; _level -= 1) {
var _entities = levelEntities.get(_level) || new Set();
_entities.forEach(function (entity) {
var parent = entity.parent,
node = entity.node;
// 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;
}
var allChecked = true;
var partialChecked = false;
(parent.children || []).filter(function (childEntity) {
return !syntheticGetCheckDisabled(childEntity.node);
}).forEach(function (_ref2) {
var key = _ref2.key;
var 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
function cleanConductCheck(keys, halfKeys, levelEntities, maxLevel, syntheticGetCheckDisabled) {
var checkedKeys = new Set(keys);
var halfCheckedKeys = new Set(halfKeys);
// Remove checked keys from top to bottom
for (var level = 0; level <= maxLevel; level += 1) {
var entities = levelEntities.get(level) || new Set();
entities.forEach(function (entity) {
var key = entity.key,
node = entity.node,
_entity$children2 = entity.children,
children = _entity$children2 === void 0 ? [] : _entity$children2;
if (!checkedKeys.has(key) && !halfCheckedKeys.has(key) && !syntheticGetCheckDisabled(node)) {
children.filter(function (childEntity) {
return !syntheticGetCheckDisabled(childEntity.node);
}).forEach(function (childEntity) {
checkedKeys.delete(childEntity.key);
});
}
});
}
// Remove checked keys form bottom to top
halfCheckedKeys = new Set();
var visitedKeys = new Set();
for (var _level2 = maxLevel; _level2 >= 0; _level2 -= 1) {
var _entities2 = levelEntities.get(_level2) || new Set();
_entities2.forEach(function (entity) {
var parent = entity.parent,
node = entity.node;
// 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;
}
var allChecked = true;
var partialChecked = false;
(parent.children || []).filter(function (childEntity) {
return !syntheticGetCheckDisabled(childEntity.node);
}).forEach(function (_ref3) {
var key = _ref3.key;
var 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
*/
function conductCheck(keyList, checked, keyEntities, getCheckDisabled) {
var warningMissKeys = [];
var syntheticGetCheckDisabled;
if (getCheckDisabled) {
syntheticGetCheckDisabled = getCheckDisabled;
} else {
syntheticGetCheckDisabled = isCheckDisabled;
}
// We only handle exist keys
var keys = new Set(keyList.filter(function (key) {
var hasEntity = !!(0, _keyUtil.default)(keyEntities, key);
if (!hasEntity) {
warningMissKeys.push(key);
}
return hasEntity;
}));
var levelEntities = new Map();
var maxLevel = 0;
// Convert entities by level for calculation
Object.keys(keyEntities).forEach(function (key) {
var entity = keyEntities[key];
var level = entity.level;
var levelSet = levelEntities.get(level);
if (!levelSet) {
levelSet = new Set();
levelEntities.set(level, levelSet);
}
levelSet.add(entity);
maxLevel = Math.max(maxLevel, level);
});
(0, _warning.default)(!warningMissKeys.length, "Tree missing follow keys: ".concat(warningMissKeys.slice(0, 100).map(function (key) {
return "'".concat(key, "'");
}).join(', ')));
var result;
if (checked === true) {
result = fillConductCheck(keys, levelEntities, maxLevel, syntheticGetCheckDisabled);
} else {
result = cleanConductCheck(keys, checked.halfCheckedKeys, levelEntities, maxLevel, syntheticGetCheckDisabled);
}
return result;
}