UNPKG

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