util.js 10 KB

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