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