conductUtil.js
6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isCheckDisabled = isCheckDisabled;
exports.conductCheck = conductCheck;
var _warning = _interopRequireDefault(require("rc-util/lib/warning"));
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 = !!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;
}