1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | Object.defineProperty(exports, "__esModule", {
|
5 | value: true
|
6 | });
|
7 | exports.arrAdd = arrAdd;
|
8 | exports.arrDel = arrDel;
|
9 | exports.calcDropPosition = calcDropPosition;
|
10 | exports.calcSelectedKeys = calcSelectedKeys;
|
11 | exports.conductExpandParent = conductExpandParent;
|
12 | exports.convertDataToTree = convertDataToTree;
|
13 | exports.getDragChildrenKeys = getDragChildrenKeys;
|
14 | exports.getPosition = getPosition;
|
15 | exports.isFirstChild = isFirstChild;
|
16 | exports.isLastChild = isLastChild;
|
17 | exports.isTreeNode = isTreeNode;
|
18 | exports.parseCheckedKeys = parseCheckedKeys;
|
19 | exports.posToArr = posToArr;
|
20 | var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
21 | var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
22 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
23 | var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
24 | var _react = _interopRequireDefault(require("react"));
|
25 | var _warning = _interopRequireDefault(require("rc-util/lib/warning"));
|
26 | var _TreeNode = _interopRequireDefault(require("./TreeNode"));
|
27 | var _excluded = ["children"];
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function arrDel(list, value) {
|
33 | if (!list) return [];
|
34 | var clone = list.slice();
|
35 | var index = clone.indexOf(value);
|
36 | if (index >= 0) {
|
37 | clone.splice(index, 1);
|
38 | }
|
39 | return clone;
|
40 | }
|
41 | function arrAdd(list, value) {
|
42 | var clone = (list || []).slice();
|
43 | if (clone.indexOf(value) === -1) {
|
44 | clone.push(value);
|
45 | }
|
46 | return clone;
|
47 | }
|
48 | function posToArr(pos) {
|
49 | return pos.split('-');
|
50 | }
|
51 | function getPosition(level, index) {
|
52 | return "".concat(level, "-").concat(index);
|
53 | }
|
54 | function isTreeNode(node) {
|
55 | return node && node.type && node.type.isTreeNode;
|
56 | }
|
57 | function getDragChildrenKeys(dragNodeKey, keyEntities) {
|
58 |
|
59 |
|
60 | var dragChildrenKeys = [];
|
61 | var entity = keyEntities[dragNodeKey];
|
62 | function dig() {
|
63 | var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
64 | list.forEach(function (_ref) {
|
65 | var key = _ref.key,
|
66 | children = _ref.children;
|
67 | dragChildrenKeys.push(key);
|
68 | dig(children);
|
69 | });
|
70 | }
|
71 | dig(entity.children);
|
72 | return dragChildrenKeys;
|
73 | }
|
74 | function isLastChild(treeNodeEntity) {
|
75 | if (treeNodeEntity.parent) {
|
76 | var posArr = posToArr(treeNodeEntity.pos);
|
77 | return Number(posArr[posArr.length - 1]) === treeNodeEntity.parent.children.length - 1;
|
78 | }
|
79 | return false;
|
80 | }
|
81 | function isFirstChild(treeNodeEntity) {
|
82 | var posArr = posToArr(treeNodeEntity.pos);
|
83 | return Number(posArr[posArr.length - 1]) === 0;
|
84 | }
|
85 |
|
86 | function calcDropPosition(event, dragNode, targetNode, indent, startMousePosition, allowDrop, flattenedNodes, keyEntities, expandKeys, direction) {
|
87 | var _abstractDropNodeEnti;
|
88 | var clientX = event.clientX,
|
89 | clientY = event.clientY;
|
90 | var _event$target$getBoun = event.target.getBoundingClientRect(),
|
91 | top = _event$target$getBoun.top,
|
92 | height = _event$target$getBoun.height;
|
93 |
|
94 | var horizontalMouseOffset = (direction === 'rtl' ? -1 : 1) * (((startMousePosition === null || startMousePosition === void 0 ? void 0 : startMousePosition.x) || 0) - clientX);
|
95 | var rawDropLevelOffset = (horizontalMouseOffset - 12) / indent;
|
96 |
|
97 | var abstractDropNodeEntity = keyEntities[targetNode.props.eventKey];
|
98 | if (clientY < top + height / 2) {
|
99 |
|
100 | var nodeIndex = flattenedNodes.findIndex(function (flattenedNode) {
|
101 | return flattenedNode.key === abstractDropNodeEntity.key;
|
102 | });
|
103 | var prevNodeIndex = nodeIndex <= 0 ? 0 : nodeIndex - 1;
|
104 | var prevNodeKey = flattenedNodes[prevNodeIndex].key;
|
105 | abstractDropNodeEntity = keyEntities[prevNodeKey];
|
106 | }
|
107 | var initialAbstractDropNodeKey = abstractDropNodeEntity.key;
|
108 | var abstractDragOverEntity = abstractDropNodeEntity;
|
109 | var dragOverNodeKey = abstractDropNodeEntity.key;
|
110 | var dropPosition = 0;
|
111 | var dropLevelOffset = 0;
|
112 |
|
113 | if (!expandKeys.includes(initialAbstractDropNodeKey)) {
|
114 | for (var i = 0; i < rawDropLevelOffset; i += 1) {
|
115 | if (isLastChild(abstractDropNodeEntity)) {
|
116 | abstractDropNodeEntity = abstractDropNodeEntity.parent;
|
117 | dropLevelOffset += 1;
|
118 | } else {
|
119 | break;
|
120 | }
|
121 | }
|
122 | }
|
123 | var abstractDragDataNode = dragNode.props.data;
|
124 | var abstractDropDataNode = abstractDropNodeEntity.node;
|
125 | var dropAllowed = true;
|
126 | if (isFirstChild(abstractDropNodeEntity) && abstractDropNodeEntity.level === 0 && clientY < top + height / 2 && allowDrop({
|
127 | dragNode: abstractDragDataNode,
|
128 | dropNode: abstractDropDataNode,
|
129 | dropPosition: -1
|
130 | }) && abstractDropNodeEntity.key === targetNode.props.eventKey) {
|
131 |
|
132 | dropPosition = -1;
|
133 | } else if ((abstractDragOverEntity.children || []).length && expandKeys.includes(dragOverNodeKey)) {
|
134 |
|
135 |
|
136 | if (allowDrop({
|
137 | dragNode: abstractDragDataNode,
|
138 | dropNode: abstractDropDataNode,
|
139 | dropPosition: 0
|
140 | })) {
|
141 | dropPosition = 0;
|
142 | } else {
|
143 | dropAllowed = false;
|
144 | }
|
145 | } else if (dropLevelOffset === 0) {
|
146 | if (rawDropLevelOffset > -1.5) {
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | if (allowDrop({
|
152 | dragNode: abstractDragDataNode,
|
153 | dropNode: abstractDropDataNode,
|
154 | dropPosition: 1
|
155 | })) {
|
156 | dropPosition = 1;
|
157 | } else {
|
158 | dropAllowed = false;
|
159 | }
|
160 | } else {
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | if (allowDrop({
|
169 | dragNode: abstractDragDataNode,
|
170 | dropNode: abstractDropDataNode,
|
171 | dropPosition: 0
|
172 | })) {
|
173 | dropPosition = 0;
|
174 | } else if (allowDrop({
|
175 | dragNode: abstractDragDataNode,
|
176 | dropNode: abstractDropDataNode,
|
177 | dropPosition: 1
|
178 | })) {
|
179 | dropPosition = 1;
|
180 | } else {
|
181 | dropAllowed = false;
|
182 | }
|
183 | }
|
184 | } else {
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | if (allowDrop({
|
191 | dragNode: abstractDragDataNode,
|
192 | dropNode: abstractDropDataNode,
|
193 | dropPosition: 1
|
194 | })) {
|
195 | dropPosition = 1;
|
196 | } else {
|
197 | dropAllowed = false;
|
198 | }
|
199 | }
|
200 | return {
|
201 | dropPosition: dropPosition,
|
202 | dropLevelOffset: dropLevelOffset,
|
203 | dropTargetKey: abstractDropNodeEntity.key,
|
204 | dropTargetPos: abstractDropNodeEntity.pos,
|
205 | dragOverNodeKey: dragOverNodeKey,
|
206 | dropContainerKey: dropPosition === 0 ? null : ((_abstractDropNodeEnti = abstractDropNodeEntity.parent) === null || _abstractDropNodeEnti === void 0 ? void 0 : _abstractDropNodeEnti.key) || null,
|
207 | dropAllowed: dropAllowed
|
208 | };
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | function calcSelectedKeys(selectedKeys, props) {
|
217 | if (!selectedKeys) return undefined;
|
218 | var multiple = props.multiple;
|
219 | if (multiple) {
|
220 | return selectedKeys.slice();
|
221 | }
|
222 | if (selectedKeys.length) {
|
223 | return [selectedKeys[0]];
|
224 | }
|
225 | return selectedKeys;
|
226 | }
|
227 | var internalProcessProps = function internalProcessProps(props) {
|
228 | return props;
|
229 | };
|
230 | function convertDataToTree(treeData, processor) {
|
231 | if (!treeData) return [];
|
232 | var _ref2 = processor || {},
|
233 | _ref2$processProps = _ref2.processProps,
|
234 | processProps = _ref2$processProps === void 0 ? internalProcessProps : _ref2$processProps;
|
235 | var list = Array.isArray(treeData) ? treeData : [treeData];
|
236 | return list.map(function (_ref3) {
|
237 | var children = _ref3.children,
|
238 | props = (0, _objectWithoutProperties2.default)(_ref3, _excluded);
|
239 | var childrenNodes = convertDataToTree(children, processor);
|
240 | return _react.default.createElement(_TreeNode.default, (0, _extends2.default)({
|
241 | key: props.key
|
242 | }, processProps(props)), childrenNodes);
|
243 | });
|
244 | }
|
245 |
|
246 |
|
247 |
|
248 | function parseCheckedKeys(keys) {
|
249 | if (!keys) {
|
250 | return null;
|
251 | }
|
252 |
|
253 | var keyProps;
|
254 | if (Array.isArray(keys)) {
|
255 |
|
256 | keyProps = {
|
257 | checkedKeys: keys,
|
258 | halfCheckedKeys: undefined
|
259 | };
|
260 | } else if ((0, _typeof2.default)(keys) === 'object') {
|
261 | keyProps = {
|
262 | checkedKeys: keys.checked || undefined,
|
263 | halfCheckedKeys: keys.halfChecked || undefined
|
264 | };
|
265 | } else {
|
266 | (0, _warning.default)(false, '`checkedKeys` is not an array or an object');
|
267 | return null;
|
268 | }
|
269 | return keyProps;
|
270 | }
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | function conductExpandParent(keyList, keyEntities) {
|
277 | var expandedKeys = new Set();
|
278 | function conductUp(key) {
|
279 | if (expandedKeys.has(key)) return;
|
280 | var entity = keyEntities[key];
|
281 | if (!entity) return;
|
282 | expandedKeys.add(key);
|
283 | var parent = entity.parent,
|
284 | node = entity.node;
|
285 | if (node.disabled) return;
|
286 | if (parent) {
|
287 | conductUp(parent.key);
|
288 | }
|
289 | }
|
290 | (keyList || []).forEach(function (key) {
|
291 | conductUp(key);
|
292 | });
|
293 | return (0, _toConsumableArray2.default)(expandedKeys);
|
294 | } |
\ | No newline at end of file |