util.js 9.2 KB

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