UNPKG

8.87 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 { getPosition, isTreeNode } from './utils/treeUtil';
14export 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}
23export function arrAdd(list, value) {
24 var clone = (list || []).slice();
25 if (clone.indexOf(value) === -1) {
26 clone.push(value);
27 }
28 return clone;
29}
30export function posToArr(pos) {
31 return pos.split('-');
32}
33export function getDragChildrenKeys(dragNodeKey, keyEntities) {
34 // not contains self
35 // self for left or right drag
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}
50export 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}
57export function isFirstChild(treeNodeEntity) {
58 var posArr = posToArr(treeNodeEntity.pos);
59 return Number(posArr[posArr.length - 1]) === 0;
60}
61// Only used when drag, not affect SSR.
62export 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 // optional chain for testing
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 // find abstract drop node by horizontal offset
73 var abstractDropNodeEntity = keyEntities[targetNode.props.eventKey];
74 if (clientY < top + height / 2) {
75 // first half, set abstract drop node to previous node
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 // Only allow cross level drop when dragging on a non-expanded node
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 // first half of first node in first level
108 dropPosition = -1;
109 } else if ((abstractDragOverEntity.children || []).length && expandKeys.includes(dragOverNodeKey)) {
110 // drop on expanded node
111 // only allow drop inside
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 // | Node | <- abstractDropNode
124 // | -^-===== | <- mousePosition
125 // 1. try drop after
126 // 2. do not allow drop
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 // | Node | <- abstractDropNode
138 // | ---==^== | <- mousePosition
139 // whether it has children or doesn't has children
140 // always
141 // 1. try drop inside
142 // 2. try drop after
143 // 3. do not allow drop
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 // | Node1 | <- abstractDropNode
162 // | Node2 |
163 // --^--|----=====| <- mousePosition
164 // 1. try insert after Node1
165 // 2. do not allow drop
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 * Return selectedKeys according with multiple prop
188 * @param selectedKeys
189 * @param props
190 * @returns [string]
191 */
192export 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}
203var internalProcessProps = function internalProcessProps(props) {
204 return props;
205};
206export 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 /*#__PURE__*/React.createElement(TreeNode, _extends({
217 key: props.key
218 }, processProps(props)), childrenNodes);
219 });
220}
221/**
222 * Parse `checkedKeys` to { checkedKeys, halfCheckedKeys } style
223 */
224export function parseCheckedKeys(keys) {
225 if (!keys) {
226 return null;
227 }
228 // Convert keys to object format
229 var keyProps;
230 if (Array.isArray(keys)) {
231 // [Legacy] Follow the api doc
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 * If user use `autoExpandParent` we should get the list of parent node
249 * @param keyList
250 * @param keyEntities
251 */
252export 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