UNPKG

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