UNPKG

8.99 kBJavaScriptView Raw
1import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2import _typeof from "@babel/runtime/helpers/esm/typeof";
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
5var _excluded = ["children"];
6/* eslint-disable no-lonely-if */
7/**
8 * Legacy code. Should avoid to use if you are new to import these code.
9 */
10import React from 'react';
11import warning from "rc-util/es/warning";
12import TreeNode from './TreeNode';
13export 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}
22export function arrAdd(list, value) {
23 var clone = (list || []).slice();
24 if (clone.indexOf(value) === -1) {
25 clone.push(value);
26 }
27 return clone;
28}
29export function posToArr(pos) {
30 return pos.split('-');
31}
32export function getPosition(level, index) {
33 return "".concat(level, "-").concat(index);
34}
35export function isTreeNode(node) {
36 return node && node.type && node.type.isTreeNode;
37}
38export function getDragChildrenKeys(dragNodeKey, keyEntities) {
39 // not contains self
40 // self for left or right drag
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}
55export 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}
62export function isFirstChild(treeNodeEntity) {
63 var posArr = posToArr(treeNodeEntity.pos);
64 return Number(posArr[posArr.length - 1]) === 0;
65}
66// Only used when drag, not affect SSR.
67export 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 // optional chain for testing
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 // find abstract drop node by horizontal offset
78 var abstractDropNodeEntity = keyEntities[targetNode.props.eventKey];
79 if (clientY < top + height / 2) {
80 // first half, set abstract drop node to previous node
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 // Only allow cross level drop when dragging on a non-expanded node
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 // first half of first node in first level
113 dropPosition = -1;
114 } else if ((abstractDragOverEntity.children || []).length && expandKeys.includes(dragOverNodeKey)) {
115 // drop on expanded node
116 // only allow drop inside
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 // | Node | <- abstractDropNode
129 // | -^-===== | <- mousePosition
130 // 1. try drop after
131 // 2. do not allow drop
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 // | Node | <- abstractDropNode
143 // | ---==^== | <- mousePosition
144 // whether it has children or doesn't has children
145 // always
146 // 1. try drop inside
147 // 2. try drop after
148 // 3. do not allow drop
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 // | Node1 | <- abstractDropNode
167 // | Node2 |
168 // --^--|----=====| <- mousePosition
169 // 1. try insert after Node1
170 // 2. do not allow drop
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 * Return selectedKeys according with multiple prop
193 * @param selectedKeys
194 * @param props
195 * @returns [string]
196 */
197export 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}
208var internalProcessProps = function internalProcessProps(props) {
209 return props;
210};
211export 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 /*#__PURE__*/React.createElement(TreeNode, _extends({
222 key: props.key
223 }, processProps(props)), childrenNodes);
224 });
225}
226/**
227 * Parse `checkedKeys` to { checkedKeys, halfCheckedKeys } style
228 */
229export function parseCheckedKeys(keys) {
230 if (!keys) {
231 return null;
232 }
233 // Convert keys to object format
234 var keyProps;
235 if (Array.isArray(keys)) {
236 // [Legacy] Follow the api doc
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 * If user use `autoExpandParent` we should get the list of parent node
254 * @param keyList
255 * @param keyEntities
256 */
257export 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