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