util.js 8.6 KB

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