util.js 9.6 KB

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