c7557aee54a51c34fcada99be1e9e9e87ffe3bc0ad05897564b5c40022b15c9db352d9f0d466b89c7f65e20c3ce2a8d2f4875259be3b7d6d32af3515984779 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <div
  3. class="el-tree-node"
  4. @click.stop="handleClick"
  5. @contextmenu="($event) => this.handleContextMenu($event)"
  6. v-show="node.visible"
  7. :class="{
  8. 'is-expanded': expanded,
  9. 'is-current': node.isCurrent,
  10. 'is-hidden': !node.visible,
  11. 'is-focusable': !node.disabled,
  12. 'is-checked': !node.disabled && node.checked
  13. }"
  14. role="treeitem"
  15. tabindex="-1"
  16. :aria-expanded="expanded"
  17. :aria-disabled="node.disabled"
  18. :aria-checked="node.checked"
  19. :draggable="tree.draggable"
  20. @dragstart.stop="handleDragStart"
  21. @dragover.stop="handleDragOver"
  22. @dragend.stop="handleDragEnd"
  23. @drop.stop="handleDrop"
  24. ref="node"
  25. >
  26. <div class="el-tree-node__content"
  27. :style="{ 'padding-left': (node.level - 1) * tree.indent + 'px' }">
  28. <span
  29. @click.stop="handleExpandIconClick"
  30. :class="[
  31. { 'is-leaf': node.isLeaf, expanded: !node.isLeaf && expanded },
  32. 'el-tree-node__expand-icon',
  33. tree.iconClass ? tree.iconClass : 'el-icon-caret-right'
  34. ]"
  35. >
  36. </span>
  37. <el-checkbox
  38. v-if="showCheckbox"
  39. v-model="node.checked"
  40. :indeterminate="node.indeterminate"
  41. :disabled="!!node.disabled"
  42. @click.native.stop
  43. @change="handleCheckChange"
  44. >
  45. </el-checkbox>
  46. <span
  47. v-if="node.loading"
  48. class="el-tree-node__loading-icon el-icon-loading">
  49. </span>
  50. <node-content :node="node"></node-content>
  51. </div>
  52. <el-collapse-transition>
  53. <div
  54. class="el-tree-node__children"
  55. v-if="!renderAfterExpand || childNodeRendered"
  56. v-show="expanded"
  57. role="group"
  58. :aria-expanded="expanded"
  59. >
  60. <el-tree-node
  61. :render-content="renderContent"
  62. v-for="child in node.childNodes"
  63. :render-after-expand="renderAfterExpand"
  64. :show-checkbox="showCheckbox"
  65. :key="getNodeKey(child)"
  66. :node="child"
  67. @node-expand="handleChildNodeExpand">
  68. </el-tree-node>
  69. </div>
  70. </el-collapse-transition>
  71. </div>
  72. </template>
  73. <script type="text/jsx">
  74. import ElCollapseTransition from 'element-ui/src/transitions/collapse-transition';
  75. import ElCheckbox from 'element-ui/packages/checkbox';
  76. import emitter from 'element-ui/src/mixins/emitter';
  77. import { getNodeKey } from './model/util';
  78. export default {
  79. name: 'ElTreeNode',
  80. componentName: 'ElTreeNode',
  81. mixins: [emitter],
  82. props: {
  83. node: {
  84. default() {
  85. return {};
  86. }
  87. },
  88. props: {},
  89. renderContent: Function,
  90. renderAfterExpand: {
  91. type: Boolean,
  92. default: true
  93. },
  94. showCheckbox: {
  95. type: Boolean,
  96. default: false
  97. }
  98. },
  99. components: {
  100. ElCollapseTransition,
  101. ElCheckbox,
  102. NodeContent: {
  103. props: {
  104. node: {
  105. required: true
  106. }
  107. },
  108. render(h) {
  109. const parent = this.$parent;
  110. const tree = parent.tree;
  111. const node = this.node;
  112. const { data, store } = node;
  113. return (
  114. parent.renderContent
  115. ? parent.renderContent.call(parent._renderProxy, h, { _self: tree.$vnode.context, node, data, store })
  116. : tree.$scopedSlots.default
  117. ? tree.$scopedSlots.default({ node, data })
  118. : <span class="el-tree-node__label">{ node.label }</span>
  119. );
  120. }
  121. }
  122. },
  123. data() {
  124. return {
  125. tree: null,
  126. expanded: false,
  127. childNodeRendered: false,
  128. oldChecked: null,
  129. oldIndeterminate: null
  130. };
  131. },
  132. watch: {
  133. 'node.indeterminate'(val) {
  134. this.handleSelectChange(this.node.checked, val);
  135. },
  136. 'node.checked'(val) {
  137. this.handleSelectChange(val, this.node.indeterminate);
  138. },
  139. 'node.expanded'(val) {
  140. this.$nextTick(() => this.expanded = val);
  141. if (val) {
  142. this.childNodeRendered = true;
  143. }
  144. }
  145. },
  146. methods: {
  147. getNodeKey(node) {
  148. return getNodeKey(this.tree.nodeKey, node.data);
  149. },
  150. handleSelectChange(checked, indeterminate) {
  151. if (this.oldChecked !== checked && this.oldIndeterminate !== indeterminate) {
  152. this.tree.$emit('check-change', this.node.data, checked, indeterminate);
  153. }
  154. this.oldChecked = checked;
  155. this.indeterminate = indeterminate;
  156. },
  157. handleClick() {
  158. const store = this.tree.store;
  159. store.setCurrentNode(this.node);
  160. this.tree.$emit('current-change', store.currentNode ? store.currentNode.data : null, store.currentNode);
  161. this.tree.currentNode = this;
  162. if (this.tree.expandOnClickNode) {
  163. this.handleExpandIconClick();
  164. }
  165. if (this.tree.checkOnClickNode && !this.node.disabled) {
  166. this.handleCheckChange(null, {
  167. target: { checked: !this.node.checked }
  168. });
  169. }
  170. this.tree.$emit('node-click', this.node.data, this.node, this);
  171. },
  172. handleContextMenu(event) {
  173. if (this.tree._events['node-contextmenu'] && this.tree._events['node-contextmenu'].length > 0) {
  174. event.stopPropagation();
  175. event.preventDefault();
  176. }
  177. this.tree.$emit('node-contextmenu', event, this.node.data, this.node, this);
  178. },
  179. handleExpandIconClick() {
  180. if (this.node.isLeaf) return;
  181. if (this.expanded) {
  182. this.tree.$emit('node-collapse', this.node.data, this.node, this);
  183. this.node.collapse();
  184. } else {
  185. this.node.expand();
  186. this.$emit('node-expand', this.node.data, this.node, this);
  187. }
  188. },
  189. handleCheckChange(value, ev) {
  190. this.node.setChecked(ev.target.checked, !this.tree.checkStrictly);
  191. this.$nextTick(() => {
  192. const store = this.tree.store;
  193. this.tree.$emit('check', this.node.data, {
  194. checkedNodes: store.getCheckedNodes(),
  195. checkedKeys: store.getCheckedKeys(),
  196. halfCheckedNodes: store.getHalfCheckedNodes(),
  197. halfCheckedKeys: store.getHalfCheckedKeys(),
  198. });
  199. });
  200. },
  201. handleChildNodeExpand(nodeData, node, instance) {
  202. this.broadcast('ElTreeNode', 'tree-node-expand', node);
  203. this.tree.$emit('node-expand', nodeData, node, instance);
  204. },
  205. handleDragStart(event) {
  206. if (!this.tree.draggable) return;
  207. this.tree.$emit('tree-node-drag-start', event, this);
  208. },
  209. handleDragOver(event) {
  210. if (!this.tree.draggable) return;
  211. this.tree.$emit('tree-node-drag-over', event, this);
  212. event.preventDefault();
  213. },
  214. handleDrop(event) {
  215. event.preventDefault();
  216. },
  217. handleDragEnd(event) {
  218. if (!this.tree.draggable) return;
  219. this.tree.$emit('tree-node-drag-end', event, this);
  220. }
  221. },
  222. created() {
  223. const parent = this.$parent;
  224. if (parent.isTree) {
  225. this.tree = parent;
  226. } else {
  227. this.tree = parent.tree;
  228. }
  229. const tree = this.tree;
  230. if (!tree) {
  231. console.warn('Can not find node\'s tree.');
  232. }
  233. const props = tree.props || {};
  234. const childrenKey = props['children'] || 'children';
  235. this.$watch(`node.data.${childrenKey}`, () => {
  236. this.node.updateChildren();
  237. });
  238. if (this.node.expanded) {
  239. this.expanded = true;
  240. this.childNodeRendered = true;
  241. }
  242. if(this.tree.accordion) {
  243. this.$on('tree-node-expand', node => {
  244. if(this.node !== node) {
  245. this.node.collapse();
  246. }
  247. });
  248. }
  249. }
  250. };
  251. </script>