util.js 9.2 KB

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