NodeList.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = exports.MotionEntity = exports.MOTION_KEY = void 0;
  7. exports.getMinimumRangeTransitionRange = getMinimumRangeTransitionRange;
  8. var _vue = require("vue");
  9. var _objectDestructuringEmpty2 = _interopRequireDefault(require("@babel/runtime/helpers/objectDestructuringEmpty"));
  10. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  11. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  12. var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
  13. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  14. var _vcVirtualList = _interopRequireDefault(require("../vc-virtual-list"));
  15. var _omit = _interopRequireDefault(require("../_util/omit"));
  16. var _contextTypes = require("./contextTypes");
  17. var _MotionTreeNode = _interopRequireDefault(require("./MotionTreeNode"));
  18. var _props = require("./props");
  19. var _diffUtil = require("./utils/diffUtil");
  20. var _treeUtil = require("./utils/treeUtil");
  21. var _excluded = ["prefixCls", "selectable", "checkable", "disabled", "motion", "height", "itemHeight", "virtual", "focusable", "activeItem", "focused", "tabindex", "onKeydown", "onFocus", "onBlur", "onListChangeStart", "onListChangeEnd"];
  22. var HIDDEN_STYLE = {
  23. width: 0,
  24. height: 0,
  25. display: 'flex',
  26. overflow: 'hidden',
  27. opacity: 0,
  28. border: 0,
  29. padding: 0,
  30. margin: 0
  31. };
  32. var noop = function noop() {};
  33. var MOTION_KEY = "RC_TREE_MOTION_".concat(Math.random());
  34. exports.MOTION_KEY = MOTION_KEY;
  35. var MotionNode = {
  36. key: MOTION_KEY
  37. };
  38. var MotionEntity = {
  39. key: MOTION_KEY,
  40. level: 0,
  41. index: 0,
  42. pos: '0',
  43. node: MotionNode,
  44. nodes: [MotionNode]
  45. };
  46. exports.MotionEntity = MotionEntity;
  47. var MotionFlattenData = {
  48. parent: null,
  49. children: [],
  50. pos: MotionEntity.pos,
  51. data: MotionNode,
  52. title: null,
  53. key: MOTION_KEY,
  54. /** Hold empty list here since we do not use it */
  55. isStart: [],
  56. isEnd: []
  57. };
  58. /**
  59. * We only need get visible content items to play the animation.
  60. */
  61. function getMinimumRangeTransitionRange(list, virtual, height, itemHeight) {
  62. if (virtual === false || !height) {
  63. return list;
  64. }
  65. return list.slice(0, Math.ceil(height / itemHeight) + 1);
  66. }
  67. function itemKey(item) {
  68. var key = item.key,
  69. pos = item.pos;
  70. return (0, _treeUtil.getKey)(key, pos);
  71. }
  72. function getAccessibilityPath(item) {
  73. var path = String(item.key);
  74. var current = item;
  75. while (current.parent) {
  76. current = current.parent;
  77. path = "".concat(current.key, " > ").concat(path);
  78. }
  79. return path;
  80. }
  81. var _default2 = (0, _vue.defineComponent)({
  82. compatConfig: {
  83. MODE: 3
  84. },
  85. name: 'NodeList',
  86. inheritAttrs: false,
  87. props: _props.nodeListProps,
  88. setup: function setup(props, _ref) {
  89. var expose = _ref.expose,
  90. attrs = _ref.attrs;
  91. // =============================== Ref ================================
  92. var listRef = (0, _vue.ref)();
  93. var indentMeasurerRef = (0, _vue.ref)();
  94. var _useInjectKeysState = (0, _contextTypes.useInjectKeysState)(),
  95. expandedKeys = _useInjectKeysState.expandedKeys,
  96. flattenNodes = _useInjectKeysState.flattenNodes;
  97. expose({
  98. scrollTo: function scrollTo(scroll) {
  99. listRef.value.scrollTo(scroll);
  100. },
  101. getIndentWidth: function getIndentWidth() {
  102. return indentMeasurerRef.value.offsetWidth;
  103. }
  104. });
  105. // ============================== Motion ==============================
  106. var transitionData = (0, _vue.shallowRef)(flattenNodes.value);
  107. var transitionRange = (0, _vue.shallowRef)([]);
  108. var motionType = (0, _vue.ref)(null);
  109. function onMotionEnd() {
  110. transitionData.value = flattenNodes.value;
  111. transitionRange.value = [];
  112. motionType.value = null;
  113. props.onListChangeEnd();
  114. }
  115. var context = (0, _contextTypes.useInjectTreeContext)();
  116. (0, _vue.watch)([function () {
  117. return expandedKeys.value.slice();
  118. }, flattenNodes], function (_ref2, _ref3) {
  119. var _ref4 = (0, _slicedToArray2.default)(_ref2, 2),
  120. expandedKeys = _ref4[0],
  121. data = _ref4[1];
  122. var _ref5 = (0, _slicedToArray2.default)(_ref3, 2),
  123. prevExpandedKeys = _ref5[0],
  124. prevData = _ref5[1];
  125. var diffExpanded = (0, _diffUtil.findExpandedKeys)(prevExpandedKeys, expandedKeys);
  126. if (diffExpanded.key !== null) {
  127. var virtual = props.virtual,
  128. height = props.height,
  129. itemHeight = props.itemHeight;
  130. if (diffExpanded.add) {
  131. var keyIndex = prevData.findIndex(function (_ref6) {
  132. var key = _ref6.key;
  133. return key === diffExpanded.key;
  134. });
  135. var rangeNodes = getMinimumRangeTransitionRange((0, _diffUtil.getExpandRange)(prevData, data, diffExpanded.key), virtual, height, itemHeight);
  136. var newTransitionData = prevData.slice();
  137. newTransitionData.splice(keyIndex + 1, 0, MotionFlattenData);
  138. transitionData.value = newTransitionData;
  139. transitionRange.value = rangeNodes;
  140. motionType.value = 'show';
  141. } else {
  142. var _keyIndex = data.findIndex(function (_ref7) {
  143. var key = _ref7.key;
  144. return key === diffExpanded.key;
  145. });
  146. var _rangeNodes = getMinimumRangeTransitionRange((0, _diffUtil.getExpandRange)(data, prevData, diffExpanded.key), virtual, height, itemHeight);
  147. var _newTransitionData = data.slice();
  148. _newTransitionData.splice(_keyIndex + 1, 0, MotionFlattenData);
  149. transitionData.value = _newTransitionData;
  150. transitionRange.value = _rangeNodes;
  151. motionType.value = 'hide';
  152. }
  153. } else if (prevData !== data) {
  154. transitionData.value = data;
  155. }
  156. });
  157. // We should clean up motion if is changed by dragging
  158. (0, _vue.watch)(function () {
  159. return context.value.dragging;
  160. }, function (dragging) {
  161. if (!dragging) {
  162. onMotionEnd();
  163. }
  164. });
  165. var mergedData = (0, _vue.computed)(function () {
  166. return props.motion === undefined ? transitionData.value : flattenNodes.value;
  167. });
  168. var onActiveChange = function onActiveChange() {
  169. props.onActiveChange(null);
  170. };
  171. return function () {
  172. var _props$attrs = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), attrs),
  173. prefixCls = _props$attrs.prefixCls,
  174. selectable = _props$attrs.selectable,
  175. checkable = _props$attrs.checkable,
  176. disabled = _props$attrs.disabled,
  177. motion = _props$attrs.motion,
  178. height = _props$attrs.height,
  179. itemHeight = _props$attrs.itemHeight,
  180. virtual = _props$attrs.virtual,
  181. focusable = _props$attrs.focusable,
  182. activeItem = _props$attrs.activeItem,
  183. focused = _props$attrs.focused,
  184. tabindex = _props$attrs.tabindex,
  185. onKeydown = _props$attrs.onKeydown,
  186. onFocus = _props$attrs.onFocus,
  187. onBlur = _props$attrs.onBlur,
  188. onListChangeStart = _props$attrs.onListChangeStart,
  189. onListChangeEnd = _props$attrs.onListChangeEnd,
  190. domProps = (0, _objectWithoutProperties2.default)(_props$attrs, _excluded);
  191. return (0, _vue.createVNode)(_vue.Fragment, null, [focused && activeItem && (0, _vue.createVNode)("span", {
  192. "style": HIDDEN_STYLE,
  193. "aria-live": "assertive"
  194. }, [getAccessibilityPath(activeItem)]), (0, _vue.createVNode)("div", null, [(0, _vue.createVNode)("input", {
  195. "style": HIDDEN_STYLE,
  196. "disabled": focusable === false || disabled,
  197. "tabindex": focusable !== false ? tabindex : null,
  198. "onKeydown": onKeydown,
  199. "onFocus": onFocus,
  200. "onBlur": onBlur,
  201. "value": "",
  202. "onChange": noop,
  203. "aria-label": "for screen reader"
  204. }, null)]), (0, _vue.createVNode)("div", {
  205. "class": "".concat(prefixCls, "-treenode"),
  206. "aria-hidden": true,
  207. "style": {
  208. position: 'absolute',
  209. pointerEvents: 'none',
  210. visibility: 'hidden',
  211. height: 0,
  212. overflow: 'hidden'
  213. }
  214. }, [(0, _vue.createVNode)("div", {
  215. "class": "".concat(prefixCls, "-indent")
  216. }, [(0, _vue.createVNode)("div", {
  217. "ref": indentMeasurerRef,
  218. "class": "".concat(prefixCls, "-indent-unit")
  219. }, null)])]), (0, _vue.createVNode)(_vcVirtualList.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, (0, _omit.default)(domProps, ['onActiveChange'])), {}, {
  220. "data": mergedData.value,
  221. "itemKey": itemKey,
  222. "height": height,
  223. "fullHeight": false,
  224. "virtual": virtual,
  225. "itemHeight": itemHeight,
  226. "prefixCls": "".concat(prefixCls, "-list"),
  227. "ref": listRef,
  228. "onVisibleChange": function onVisibleChange(originList, fullList) {
  229. var originSet = new Set(originList);
  230. var restList = fullList.filter(function (item) {
  231. return !originSet.has(item);
  232. });
  233. // Motion node is not render. Skip motion
  234. if (restList.some(function (item) {
  235. return itemKey(item) === MOTION_KEY;
  236. })) {
  237. onMotionEnd();
  238. }
  239. }
  240. }), {
  241. default: function _default(treeNode) {
  242. var pos = treeNode.pos,
  243. restProps = (0, _extends2.default)({}, ((0, _objectDestructuringEmpty2.default)(treeNode.data), treeNode.data)),
  244. title = treeNode.title,
  245. key = treeNode.key,
  246. isStart = treeNode.isStart,
  247. isEnd = treeNode.isEnd;
  248. var mergedKey = (0, _treeUtil.getKey)(key, pos);
  249. delete restProps.key;
  250. delete restProps.children;
  251. return (0, _vue.createVNode)(_MotionTreeNode.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, restProps), {}, {
  252. "eventKey": mergedKey,
  253. "title": title,
  254. "active": !!activeItem && key === activeItem.key,
  255. "data": treeNode.data,
  256. "isStart": isStart,
  257. "isEnd": isEnd,
  258. "motion": motion,
  259. "motionNodes": key === MOTION_KEY ? transitionRange.value : null,
  260. "motionType": motionType.value,
  261. "onMotionStart": onListChangeStart,
  262. "onMotionEnd": onMotionEnd,
  263. "onMousemove": onActiveChange
  264. }), null);
  265. }
  266. })]);
  267. };
  268. }
  269. });
  270. exports.default = _default2;