tree.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <div
  3. class="el-tree"
  4. :class="{ 'el-tree--highlight-current': highlightCurrent }"
  5. role="tree"
  6. >
  7. <el-tree-node
  8. v-for="child in root.childNodes"
  9. :node="child"
  10. :props="props"
  11. :render-after-expand="renderAfterExpand"
  12. :key="getNodeKey(child)"
  13. :render-content="renderContent"
  14. @node-expand="handleNodeExpand">
  15. </el-tree-node>
  16. <div class="el-tree__empty-block" v-if="!root.childNodes || root.childNodes.length === 0">
  17. <span class="el-tree__empty-text">{{ emptyText }}</span>
  18. </div>
  19. </div>
  20. </template>
  21. <script>
  22. import TreeStore from './model/tree-store';
  23. import { getNodeKey } from './model/util';
  24. import ElTreeNode from './tree-node.vue';
  25. import {t} from 'element-ui/src/locale';
  26. import emitter from 'element-ui/src/mixins/emitter';
  27. export default {
  28. name: 'ElTree',
  29. mixins: [emitter],
  30. components: {
  31. ElTreeNode
  32. },
  33. data() {
  34. return {
  35. store: null,
  36. root: null,
  37. currentNode: null,
  38. treeItems: null,
  39. checkboxItems: []
  40. };
  41. },
  42. props: {
  43. data: {
  44. type: Array
  45. },
  46. emptyText: {
  47. type: String,
  48. default() {
  49. return t('el.tree.emptyText');
  50. }
  51. },
  52. renderAfterExpand: {
  53. type: Boolean,
  54. default: true
  55. },
  56. nodeKey: String,
  57. checkStrictly: Boolean,
  58. defaultExpandAll: Boolean,
  59. expandOnClickNode: {
  60. type: Boolean,
  61. default: true
  62. },
  63. checkDescendants: {
  64. type: Boolean,
  65. default: false
  66. },
  67. autoExpandParent: {
  68. type: Boolean,
  69. default: true
  70. },
  71. defaultCheckedKeys: Array,
  72. defaultExpandedKeys: Array,
  73. renderContent: Function,
  74. showCheckbox: {
  75. type: Boolean,
  76. default: false
  77. },
  78. props: {
  79. default() {
  80. return {
  81. children: 'children',
  82. label: 'label',
  83. icon: 'icon',
  84. disabled: 'disabled'
  85. };
  86. }
  87. },
  88. lazy: {
  89. type: Boolean,
  90. default: false
  91. },
  92. highlightCurrent: Boolean,
  93. load: Function,
  94. filterNodeMethod: Function,
  95. accordion: Boolean,
  96. indent: {
  97. type: Number,
  98. default: 18
  99. }
  100. },
  101. computed: {
  102. children: {
  103. set(value) {
  104. this.data = value;
  105. },
  106. get() {
  107. return this.data;
  108. }
  109. },
  110. treeItemArray() {
  111. return Array.prototype.slice.call(this.treeItems);
  112. }
  113. },
  114. watch: {
  115. defaultCheckedKeys(newVal) {
  116. this.store.defaultCheckedKeys = newVal;
  117. this.store.setDefaultCheckedKey(newVal);
  118. },
  119. defaultExpandedKeys(newVal) {
  120. this.store.defaultExpandedKeys = newVal;
  121. this.store.setDefaultExpandedKeys(newVal);
  122. },
  123. data(newVal) {
  124. this.store.setData(newVal);
  125. },
  126. checkboxItems(val) {
  127. Array.prototype.forEach.call(val, (checkbox) => {
  128. checkbox.setAttribute('tabindex', -1);
  129. });
  130. }
  131. },
  132. methods: {
  133. filter(value) {
  134. if (!this.filterNodeMethod) throw new Error('[Tree] filterNodeMethod is required when filter');
  135. this.store.filter(value);
  136. },
  137. getNodeKey(node) {
  138. return getNodeKey(this.nodeKey, node.data);
  139. },
  140. getNodePath(data) {
  141. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in getNodePath');
  142. const node = this.store.getNode(data);
  143. if (!node) return [];
  144. const path = [node.data];
  145. let parent = node.parent;
  146. while (parent && parent !== this.root) {
  147. path.push(parent.data);
  148. parent = parent.parent;
  149. }
  150. return path.reverse();
  151. },
  152. getCheckedNodes(leafOnly) {
  153. return this.store.getCheckedNodes(leafOnly);
  154. },
  155. getCheckedKeys(leafOnly) {
  156. return this.store.getCheckedKeys(leafOnly);
  157. },
  158. getCurrentNode() {
  159. const currentNode = this.store.getCurrentNode();
  160. return currentNode ? currentNode.data : null;
  161. },
  162. getCurrentKey() {
  163. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in getCurrentKey');
  164. const currentNode = this.getCurrentNode();
  165. return currentNode ? currentNode[this.nodeKey] : null;
  166. },
  167. setCheckedNodes(nodes, leafOnly) {
  168. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedNodes');
  169. this.store.setCheckedNodes(nodes, leafOnly);
  170. },
  171. setCheckedKeys(keys, leafOnly) {
  172. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCheckedKeys');
  173. this.store.setCheckedKeys(keys, leafOnly);
  174. },
  175. setChecked(data, checked, deep) {
  176. this.store.setChecked(data, checked, deep);
  177. },
  178. getHalfCheckedNodes() {
  179. return this.store.getHalfCheckedNodes();
  180. },
  181. getHalfCheckedKeys() {
  182. return this.store.getHalfCheckedKeys();
  183. },
  184. setCurrentNode(node) {
  185. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentNode');
  186. this.store.setUserCurrentNode(node);
  187. },
  188. setCurrentKey(key) {
  189. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in setCurrentKey');
  190. this.store.setCurrentNodeKey(key);
  191. },
  192. getNode(data) {
  193. return this.store.getNode(data);
  194. },
  195. remove(data) {
  196. this.store.remove(data);
  197. },
  198. append(data, parentNode) {
  199. this.store.append(data, parentNode);
  200. },
  201. insertBefore(data, refNode) {
  202. this.store.insertBefore(data, refNode);
  203. },
  204. insertAfter(data, refNode) {
  205. this.store.insertAfter(data, refNode);
  206. },
  207. handleNodeExpand(nodeData, node, instance) {
  208. this.broadcast('ElTreeNode', 'tree-node-expand', node);
  209. this.$emit('node-expand', nodeData, node, instance);
  210. },
  211. updateKeyChildren(key, data) {
  212. if (!this.nodeKey) throw new Error('[Tree] nodeKey is required in updateKeyChild');
  213. this.store.updateChildren(key, data);
  214. },
  215. initTabindex() {
  216. this.treeItems = this.$el.querySelectorAll('.is-focusable[role=treeitem]');
  217. this.checkboxItems = this.$el.querySelectorAll('input[type=checkbox]');
  218. const checkedItem = this.$el.querySelectorAll('.is-checked[role=treeitem]');
  219. if (checkedItem.length) {
  220. checkedItem[0].setAttribute('tabindex', 0);
  221. return;
  222. }
  223. this.treeItems[0] && this.treeItems[0].setAttribute('tabindex', 0);
  224. },
  225. handelKeydown(ev) {
  226. const currentItem = ev.target;
  227. if (currentItem.className.indexOf('el-tree-node') === -1) return;
  228. ev.preventDefault();
  229. const keyCode = ev.keyCode;
  230. this.treeItems = this.$el.querySelectorAll('.is-focusable[role=treeitem]');
  231. const currentIndex = this.treeItemArray.indexOf(currentItem);
  232. let nextIndex;
  233. if ([38, 40].indexOf(keyCode) > -1) { // up、down
  234. if (keyCode === 38) { // up
  235. nextIndex = currentIndex !== 0 ? currentIndex - 1 : 0;
  236. } else {
  237. nextIndex = (currentIndex < this.treeItemArray.length - 1) ? currentIndex + 1 : 0;
  238. }
  239. this.treeItemArray[nextIndex].focus(); // 选中
  240. }
  241. const hasInput = currentItem.querySelector('[type="checkbox"]');
  242. if ([37, 39].indexOf(keyCode) > -1) { // left、right 展开
  243. currentItem.click(); // 选中
  244. }
  245. if ([13, 32].indexOf(keyCode) > -1) { // space enter选中checkbox
  246. if (hasInput) {
  247. hasInput.click();
  248. }
  249. }
  250. }
  251. },
  252. created() {
  253. this.isTree = true;
  254. this.store = new TreeStore({
  255. key: this.nodeKey,
  256. data: this.data,
  257. lazy: this.lazy,
  258. props: this.props,
  259. load: this.load,
  260. currentNodeKey: this.currentNodeKey,
  261. checkStrictly: this.checkStrictly,
  262. checkDescendants: this.checkDescendants,
  263. defaultCheckedKeys: this.defaultCheckedKeys,
  264. defaultExpandedKeys: this.defaultExpandedKeys,
  265. autoExpandParent: this.autoExpandParent,
  266. defaultExpandAll: this.defaultExpandAll,
  267. filterNodeMethod: this.filterNodeMethod
  268. });
  269. this.root = this.store.root;
  270. },
  271. mounted() {
  272. this.initTabindex();
  273. this.$el.addEventListener('keydown', this.handelKeydown);
  274. },
  275. updated() {
  276. this.treeItems = this.$el.querySelectorAll('[role=treeitem]');
  277. this.checkboxItems = this.$el.querySelectorAll('input[type=checkbox]');
  278. }
  279. };
  280. </script>