You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
12 KiB

3 months ago
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var index = require('../../collapse-transition/index.js');
  5. var index$1 = require('../../checkbox/index.js');
  6. var index$2 = require('../../icon/index.js');
  7. var iconsVue = require('@element-plus/icons-vue');
  8. var treeNodeContent = require('./tree-node-content.js');
  9. var util = require('./model/util.js');
  10. var useNodeExpandEventBroadcast = require('./model/useNodeExpandEventBroadcast.js');
  11. var useDragNode = require('./model/useDragNode.js');
  12. var node = require('./model/node.js');
  13. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  14. var index$3 = require('../../../hooks/use-namespace/index.js');
  15. var error = require('../../../utils/error.js');
  16. var shared = require('@vue/shared');
  17. const _sfc_main = vue.defineComponent({
  18. name: "ElTreeNode",
  19. components: {
  20. ElCollapseTransition: index.ElCollapseTransition,
  21. ElCheckbox: index$1.ElCheckbox,
  22. NodeContent: treeNodeContent["default"],
  23. ElIcon: index$2.ElIcon,
  24. Loading: iconsVue.Loading
  25. },
  26. props: {
  27. node: {
  28. type: node["default"],
  29. default: () => ({})
  30. },
  31. props: {
  32. type: Object,
  33. default: () => ({})
  34. },
  35. accordion: Boolean,
  36. renderContent: Function,
  37. renderAfterExpand: Boolean,
  38. showCheckbox: {
  39. type: Boolean,
  40. default: false
  41. }
  42. },
  43. emits: ["node-expand"],
  44. setup(props, ctx) {
  45. const ns = index$3.useNamespace("tree");
  46. const { broadcastExpanded } = useNodeExpandEventBroadcast.useNodeExpandEventBroadcast(props);
  47. const tree = vue.inject("RootTree");
  48. const expanded = vue.ref(false);
  49. const childNodeRendered = vue.ref(false);
  50. const oldChecked = vue.ref();
  51. const oldIndeterminate = vue.ref();
  52. const node$ = vue.ref();
  53. const dragEvents = vue.inject(useDragNode.dragEventsKey);
  54. const instance = vue.getCurrentInstance();
  55. vue.provide("NodeInstance", instance);
  56. if (!tree) {
  57. error.debugWarn("Tree", "Can not find node's tree.");
  58. }
  59. if (props.node.expanded) {
  60. expanded.value = true;
  61. childNodeRendered.value = true;
  62. }
  63. const childrenKey = tree.props.props["children"] || "children";
  64. vue.watch(() => {
  65. var _a;
  66. const children = (_a = props.node.data) == null ? void 0 : _a[childrenKey];
  67. return children && [...children];
  68. }, () => {
  69. props.node.updateChildren();
  70. });
  71. vue.watch(() => props.node.indeterminate, (val) => {
  72. handleSelectChange(props.node.checked, val);
  73. });
  74. vue.watch(() => props.node.checked, (val) => {
  75. handleSelectChange(val, props.node.indeterminate);
  76. });
  77. vue.watch(() => props.node.childNodes.length, () => props.node.reInitChecked());
  78. vue.watch(() => props.node.expanded, (val) => {
  79. vue.nextTick(() => expanded.value = val);
  80. if (val) {
  81. childNodeRendered.value = true;
  82. }
  83. });
  84. const getNodeKey = (node) => {
  85. return util.getNodeKey(tree.props.nodeKey, node.data);
  86. };
  87. const getNodeClass = (node) => {
  88. const nodeClassFunc = props.props.class;
  89. if (!nodeClassFunc) {
  90. return {};
  91. }
  92. let className;
  93. if (shared.isFunction(nodeClassFunc)) {
  94. const { data } = node;
  95. className = nodeClassFunc(data, node);
  96. } else {
  97. className = nodeClassFunc;
  98. }
  99. if (shared.isString(className)) {
  100. return { [className]: true };
  101. } else {
  102. return className;
  103. }
  104. };
  105. const handleSelectChange = (checked, indeterminate) => {
  106. if (oldChecked.value !== checked || oldIndeterminate.value !== indeterminate) {
  107. tree.ctx.emit("check-change", props.node.data, checked, indeterminate);
  108. }
  109. oldChecked.value = checked;
  110. oldIndeterminate.value = indeterminate;
  111. };
  112. const handleClick = (e) => {
  113. util.handleCurrentChange(tree.store, tree.ctx.emit, () => {
  114. var _a;
  115. const nodeKeyProp = (_a = tree == null ? void 0 : tree.props) == null ? void 0 : _a.nodeKey;
  116. if (nodeKeyProp) {
  117. const curNodeKey = getNodeKey(props.node);
  118. tree.store.value.setCurrentNodeKey(curNodeKey);
  119. } else {
  120. tree.store.value.setCurrentNode(props.node);
  121. }
  122. });
  123. tree.currentNode.value = props.node;
  124. if (tree.props.expandOnClickNode) {
  125. handleExpandIconClick();
  126. }
  127. if ((tree.props.checkOnClickNode || props.node.isLeaf && tree.props.checkOnClickLeaf) && !props.node.disabled) {
  128. handleCheckChange(!props.node.checked);
  129. }
  130. tree.ctx.emit("node-click", props.node.data, props.node, instance, e);
  131. };
  132. const handleContextMenu = (event) => {
  133. var _a;
  134. if ((_a = tree.instance.vnode.props) == null ? void 0 : _a["onNodeContextmenu"]) {
  135. event.stopPropagation();
  136. event.preventDefault();
  137. }
  138. tree.ctx.emit("node-contextmenu", event, props.node.data, props.node, instance);
  139. };
  140. const handleExpandIconClick = () => {
  141. if (props.node.isLeaf)
  142. return;
  143. if (expanded.value) {
  144. tree.ctx.emit("node-collapse", props.node.data, props.node, instance);
  145. props.node.collapse();
  146. } else {
  147. props.node.expand(() => {
  148. ctx.emit("node-expand", props.node.data, props.node, instance);
  149. });
  150. }
  151. };
  152. const handleCheckChange = (value) => {
  153. props.node.setChecked(value, !(tree == null ? void 0 : tree.props.checkStrictly));
  154. vue.nextTick(() => {
  155. const store = tree.store.value;
  156. tree.ctx.emit("check", props.node.data, {
  157. checkedNodes: store.getCheckedNodes(),
  158. checkedKeys: store.getCheckedKeys(),
  159. halfCheckedNodes: store.getHalfCheckedNodes(),
  160. halfCheckedKeys: store.getHalfCheckedKeys()
  161. });
  162. });
  163. };
  164. const handleChildNodeExpand = (nodeData, node, instance2) => {
  165. broadcastExpanded(node);
  166. tree.ctx.emit("node-expand", nodeData, node, instance2);
  167. };
  168. const handleDragStart = (event) => {
  169. if (!tree.props.draggable)
  170. return;
  171. dragEvents.treeNodeDragStart({ event, treeNode: props });
  172. };
  173. const handleDragOver = (event) => {
  174. event.preventDefault();
  175. if (!tree.props.draggable)
  176. return;
  177. dragEvents.treeNodeDragOver({
  178. event,
  179. treeNode: { $el: node$.value, node: props.node }
  180. });
  181. };
  182. const handleDrop = (event) => {
  183. event.preventDefault();
  184. };
  185. const handleDragEnd = (event) => {
  186. if (!tree.props.draggable)
  187. return;
  188. dragEvents.treeNodeDragEnd(event);
  189. };
  190. return {
  191. ns,
  192. node$,
  193. tree,
  194. expanded,
  195. childNodeRendered,
  196. oldChecked,
  197. oldIndeterminate,
  198. getNodeKey,
  199. getNodeClass,
  200. handleSelectChange,
  201. handleClick,
  202. handleContextMenu,
  203. handleExpandIconClick,
  204. handleCheckChange,
  205. handleChildNodeExpand,
  206. handleDragStart,
  207. handleDragOver,
  208. handleDrop,
  209. handleDragEnd,
  210. CaretRight: iconsVue.CaretRight
  211. };
  212. }
  213. });
  214. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  215. const _component_el_icon = vue.resolveComponent("el-icon");
  216. const _component_el_checkbox = vue.resolveComponent("el-checkbox");
  217. const _component_loading = vue.resolveComponent("loading");
  218. const _component_node_content = vue.resolveComponent("node-content");
  219. const _component_el_tree_node = vue.resolveComponent("el-tree-node");
  220. const _component_el_collapse_transition = vue.resolveComponent("el-collapse-transition");
  221. return vue.withDirectives((vue.openBlock(), vue.createElementBlock("div", {
  222. ref: "node$",
  223. class: vue.normalizeClass([
  224. _ctx.ns.b("node"),
  225. _ctx.ns.is("expanded", _ctx.expanded),
  226. _ctx.ns.is("current", _ctx.node.isCurrent),
  227. _ctx.ns.is("hidden", !_ctx.node.visible),
  228. _ctx.ns.is("focusable", !_ctx.node.disabled),
  229. _ctx.ns.is("checked", !_ctx.node.disabled && _ctx.node.checked),
  230. _ctx.getNodeClass(_ctx.node)
  231. ]),
  232. role: "treeitem",
  233. tabindex: "-1",
  234. "aria-expanded": _ctx.expanded,
  235. "aria-disabled": _ctx.node.disabled,
  236. "aria-checked": _ctx.node.checked,
  237. draggable: _ctx.tree.props.draggable,
  238. "data-key": _ctx.getNodeKey(_ctx.node),
  239. onClick: vue.withModifiers(_ctx.handleClick, ["stop"]),
  240. onContextmenu: _ctx.handleContextMenu,
  241. onDragstart: vue.withModifiers(_ctx.handleDragStart, ["stop"]),
  242. onDragover: vue.withModifiers(_ctx.handleDragOver, ["stop"]),
  243. onDragend: vue.withModifiers(_ctx.handleDragEnd, ["stop"]),
  244. onDrop: vue.withModifiers(_ctx.handleDrop, ["stop"])
  245. }, [
  246. vue.createElementVNode("div", {
  247. class: vue.normalizeClass(_ctx.ns.be("node", "content")),
  248. style: vue.normalizeStyle({ paddingLeft: (_ctx.node.level - 1) * _ctx.tree.props.indent + "px" })
  249. }, [
  250. _ctx.tree.props.icon || _ctx.CaretRight ? (vue.openBlock(), vue.createBlock(_component_el_icon, {
  251. key: 0,
  252. class: vue.normalizeClass([
  253. _ctx.ns.be("node", "expand-icon"),
  254. _ctx.ns.is("leaf", _ctx.node.isLeaf),
  255. {
  256. expanded: !_ctx.node.isLeaf && _ctx.expanded
  257. }
  258. ]),
  259. onClick: vue.withModifiers(_ctx.handleExpandIconClick, ["stop"])
  260. }, {
  261. default: vue.withCtx(() => [
  262. (vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(_ctx.tree.props.icon || _ctx.CaretRight)))
  263. ]),
  264. _: 1
  265. }, 8, ["class", "onClick"])) : vue.createCommentVNode("v-if", true),
  266. _ctx.showCheckbox ? (vue.openBlock(), vue.createBlock(_component_el_checkbox, {
  267. key: 1,
  268. "model-value": _ctx.node.checked,
  269. indeterminate: _ctx.node.indeterminate,
  270. disabled: !!_ctx.node.disabled,
  271. onClick: vue.withModifiers(() => {
  272. }, ["stop"]),
  273. onChange: _ctx.handleCheckChange
  274. }, null, 8, ["model-value", "indeterminate", "disabled", "onClick", "onChange"])) : vue.createCommentVNode("v-if", true),
  275. _ctx.node.loading ? (vue.openBlock(), vue.createBlock(_component_el_icon, {
  276. key: 2,
  277. class: vue.normalizeClass([_ctx.ns.be("node", "loading-icon"), _ctx.ns.is("loading")])
  278. }, {
  279. default: vue.withCtx(() => [
  280. vue.createVNode(_component_loading)
  281. ]),
  282. _: 1
  283. }, 8, ["class"])) : vue.createCommentVNode("v-if", true),
  284. vue.createVNode(_component_node_content, {
  285. node: _ctx.node,
  286. "render-content": _ctx.renderContent
  287. }, null, 8, ["node", "render-content"])
  288. ], 6),
  289. vue.createVNode(_component_el_collapse_transition, null, {
  290. default: vue.withCtx(() => [
  291. !_ctx.renderAfterExpand || _ctx.childNodeRendered ? vue.withDirectives((vue.openBlock(), vue.createElementBlock("div", {
  292. key: 0,
  293. class: vue.normalizeClass(_ctx.ns.be("node", "children")),
  294. role: "group",
  295. "aria-expanded": _ctx.expanded
  296. }, [
  297. (vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(_ctx.node.childNodes, (child) => {
  298. return vue.openBlock(), vue.createBlock(_component_el_tree_node, {
  299. key: _ctx.getNodeKey(child),
  300. "render-content": _ctx.renderContent,
  301. "render-after-expand": _ctx.renderAfterExpand,
  302. "show-checkbox": _ctx.showCheckbox,
  303. node: child,
  304. accordion: _ctx.accordion,
  305. props: _ctx.props,
  306. onNodeExpand: _ctx.handleChildNodeExpand
  307. }, null, 8, ["render-content", "render-after-expand", "show-checkbox", "node", "accordion", "props", "onNodeExpand"]);
  308. }), 128))
  309. ], 10, ["aria-expanded"])), [
  310. [vue.vShow, _ctx.expanded]
  311. ]) : vue.createCommentVNode("v-if", true)
  312. ]),
  313. _: 1
  314. })
  315. ], 42, ["aria-expanded", "aria-disabled", "aria-checked", "draggable", "data-key", "onClick", "onContextmenu", "onDragstart", "onDragover", "onDragend", "onDrop"])), [
  316. [vue.vShow, _ctx.node.visible]
  317. ]);
  318. }
  319. var ElTreeNode = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["render", _sfc_render], ["__file", "tree-node.vue"]]);
  320. exports["default"] = ElTreeNode;
  321. //# sourceMappingURL=tree-node.js.map