Blame view

src/components/tree/tree.vue 3.53 KB
89f2ba8b   梁灏   init Tree component
1
  <template>
cb84e64a   梁灏   update Tree
2
3
      <div :class="prefixCls">
          <Tree-node
3c145e6f   梁灏   update Tree
4
              v-for="item in data"
cb84e64a   梁灏   update Tree
5
6
7
8
9
10
11
              :key="item"
              :data="item"
              visible
              :multiple="multiple"
              :show-checkbox="showCheckbox">
          </Tree-node>
      </div>
89f2ba8b   梁灏   init Tree component
12
13
  </template>
  <script>
cb84e64a   梁灏   update Tree
14
15
      import TreeNode from './node.vue';
      import { findComponentsDownward } from '../../utils/assist';
e2c6ff2b   梁灏   update Rate
16
      import Emitter from '../../mixins/emitter';
cb84e64a   梁灏   update Tree
17
      import { t } from '../../locale';
e81207a2   梁灏   update Tree
18
19
20
  
      const prefixCls = 'ivu-tree';
  
89f2ba8b   梁灏   init Tree component
21
      export default {
34ee7b4a   梁灏   support Tree & ad...
22
          name: 'Tree',
e2c6ff2b   梁灏   update Rate
23
          mixins: [ Emitter ],
cb84e64a   梁灏   update Tree
24
          components: { TreeNode },
e81207a2   梁灏   update Tree
25
          props: {
9d79a51f   梁灏   update Tree
26
              data: {
e81207a2   梁灏   update Tree
27
28
29
30
31
                  type: Array,
                  default () {
                      return [];
                  }
              },
e81207a2   梁灏   update Tree
32
33
34
35
36
37
38
39
              multiple: {
                  type: Boolean,
                  default: false
              },
              showCheckbox: {
                  type: Boolean,
                  default: false
              },
e81207a2   梁灏   update Tree
40
41
42
43
44
45
46
              emptyText: {
                  type: String,
                  default () {
                      return t('i.tree.emptyText');
                  }
              }
          },
89f2ba8b   梁灏   init Tree component
47
          data () {
e81207a2   梁灏   update Tree
48
              return {
3c145e6f   梁灏   update Tree
49
                  prefixCls: prefixCls
e81207a2   梁灏   update Tree
50
              };
89f2ba8b   梁灏   init Tree component
51
          },
e81207a2   梁灏   update Tree
52
          methods: {
cb84e64a   梁灏   update Tree
53
54
55
56
              getSelectedNodes () {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  return nodes.filter(node => node.data.selected).map(node => node.data);
              },
3c145e6f   梁灏   update Tree
57
              updateData (isInit = true) {
cb84e64a   梁灏   update Tree
58
59
60
61
62
63
64
                  // init checked status
                  function reverseChecked(data) {
                      if (data.children) {
                          let checkedLength = 0;
                          data.children.forEach(node => {
                              if (node.children) node = reverseChecked(node);
                              if (node.checked) checkedLength++;
34ee7b4a   梁灏   support Tree & ad...
65
                          });
3c145e6f   梁灏   update Tree
66
67
68
69
70
                          if (isInit) {
                              if (checkedLength >= data.children.length) data.checked = true;
                          } else {
                              data.checked = checkedLength >= data.children.length;
                          }
cb84e64a   梁灏   update Tree
71
                          return data;
e81207a2   梁灏   update Tree
72
                      } else {
cb84e64a   梁灏   update Tree
73
                          return data;
e81207a2   梁灏   update Tree
74
75
                      }
                  }
cb84e64a   梁灏   update Tree
76
77
78
79
80
81
                  
                  function forwardChecked(data) {
                      if (data.children) {
                          data.children.forEach(node => {
                              if (data.checked) node.checked = true;
                              if (node.children) node = forwardChecked(node);
75c32d5f   梁灏   rebuild Tree
82
                          });
cb84e64a   梁灏   update Tree
83
                          return data;
75c32d5f   梁灏   rebuild Tree
84
                      } else {
cb84e64a   梁灏   update Tree
85
                          return data;
75c32d5f   梁灏   rebuild Tree
86
                      }
e81207a2   梁灏   update Tree
87
                  }
3c145e6f   梁灏   update Tree
88
                  this.data.map(node => reverseChecked(node)).map(node => forwardChecked(node));
cb84e64a   梁灏   update Tree
89
                  this.broadcast('TreeNode', 'indeterminate');
e81207a2   梁灏   update Tree
90
91
              }
          },
34ee7b4a   梁灏   support Tree & ad...
92
          mounted () {
cb84e64a   梁灏   update Tree
93
              this.updateData();
e81207a2   梁灏   update Tree
94
  
cb84e64a   梁灏   update Tree
95
96
97
98
              this.$on('selected', ori => {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  nodes.forEach(node => {
                      this.$set(node.data, 'selected', false);
ce03ac52   梁灏   update Tree
99
                  });
cb84e64a   梁灏   update Tree
100
                  this.$set(ori, 'selected', true);
e81207a2   梁灏   update Tree
101
              });
cb84e64a   梁灏   update Tree
102
103
              this.$on('on-selected', () => {
                  this.$emit('on-select-change', this.getSelectedNodes());
e81207a2   梁灏   update Tree
104
              });
cb84e64a   梁灏   update Tree
105
              this.$on('checked', () => {
3c145e6f   梁灏   update Tree
106
                  this.updateData(false);
e81207a2   梁灏   update Tree
107
108
              });
          }
89f2ba8b   梁灏   init Tree component
109
110
      };
  </script>