Blame view

src/components/tree/tree.vue 4.58 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"
2e84df44   梁灏   fixed Tree key bu...
5
              :key="item.nodeKey"
cb84e64a   梁灏   update Tree
6
7
8
9
10
              :data="item"
              visible
              :multiple="multiple"
              :show-checkbox="showCheckbox">
          </Tree-node>
e5337c81   梁灏   fixed some compon...
11
          <div :class="[prefixCls + '-empty']" v-if="!data.length">{{ localeEmptyText }}</div>
cb84e64a   梁灏   update Tree
12
      </div>
89f2ba8b   梁灏   init Tree component
13
14
  </template>
  <script>
cb84e64a   梁灏   update Tree
15
16
      import TreeNode from './node.vue';
      import { findComponentsDownward } from '../../utils/assist';
e2c6ff2b   梁灏   update Rate
17
      import Emitter from '../../mixins/emitter';
e5337c81   梁灏   fixed some compon...
18
      import Locale from '../../mixins/locale';
e81207a2   梁灏   update Tree
19
20
21
  
      const prefixCls = 'ivu-tree';
  
2e84df44   梁灏   fixed Tree key bu...
22
23
      let key = 1;
  
89f2ba8b   梁灏   init Tree component
24
      export default {
34ee7b4a   梁灏   support Tree & ad...
25
          name: 'Tree',
e5337c81   梁灏   fixed some compon...
26
          mixins: [ Emitter, Locale ],
cb84e64a   梁灏   update Tree
27
          components: { TreeNode },
e81207a2   梁灏   update Tree
28
          props: {
9d79a51f   梁灏   update Tree
29
              data: {
e81207a2   梁灏   update Tree
30
31
32
33
34
                  type: Array,
                  default () {
                      return [];
                  }
              },
e81207a2   梁灏   update Tree
35
36
37
38
39
40
41
42
              multiple: {
                  type: Boolean,
                  default: false
              },
              showCheckbox: {
                  type: Boolean,
                  default: false
              },
e81207a2   梁灏   update Tree
43
              emptyText: {
e5337c81   梁灏   fixed some compon...
44
                  type: String
e81207a2   梁灏   update Tree
45
46
              }
          },
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
          },
e5337c81   梁灏   fixed some compon...
52
53
54
55
56
57
58
59
60
          computed: {
              localeEmptyText () {
                  if (this.emptyText === undefined) {
                      return this.t('i.tree.emptyText');
                  } else {
                      return this.emptyText;
                  }
              }
          },
e81207a2   梁灏   update Tree
61
          methods: {
cb84e64a   梁灏   update Tree
62
63
64
65
              getSelectedNodes () {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  return nodes.filter(node => node.data.selected).map(node => node.data);
              },
53754a31   梁灏   fixed #468
66
67
68
69
              getCheckedNodes () {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  return nodes.filter(node => node.data.checked).map(node => node.data);
              },
3c145e6f   梁灏   update Tree
70
              updateData (isInit = true) {
cb84e64a   梁灏   update Tree
71
72
                  // init checked status
                  function reverseChecked(data) {
2e84df44   梁灏   fixed Tree key bu...
73
                      if (!data.nodeKey) data.nodeKey = key++;
173f1c00   Rijn   skip checking chi...
74
                      if (data.children && data.children.length) {
cb84e64a   梁灏   update Tree
75
76
77
78
                          let checkedLength = 0;
                          data.children.forEach(node => {
                              if (node.children) node = reverseChecked(node);
                              if (node.checked) checkedLength++;
34ee7b4a   梁灏   support Tree & ad...
79
                          });
3c145e6f   梁灏   update Tree
80
81
82
83
84
                          if (isInit) {
                              if (checkedLength >= data.children.length) data.checked = true;
                          } else {
                              data.checked = checkedLength >= data.children.length;
                          }
cb84e64a   梁灏   update Tree
85
                          return data;
e81207a2   梁灏   update Tree
86
                      } else {
cb84e64a   梁灏   update Tree
87
                          return data;
e81207a2   梁灏   update Tree
88
89
                      }
                  }
cb84e64a   梁灏   update Tree
90
91
92
93
94
95
                  
                  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
96
                          });
cb84e64a   梁灏   update Tree
97
                          return data;
75c32d5f   梁灏   rebuild Tree
98
                      } else {
cb84e64a   梁灏   update Tree
99
                          return data;
75c32d5f   梁灏   rebuild Tree
100
                      }
e81207a2   梁灏   update Tree
101
                  }
3c145e6f   梁灏   update Tree
102
                  this.data.map(node => reverseChecked(node)).map(node => forwardChecked(node));
cb84e64a   梁灏   update Tree
103
                  this.broadcast('TreeNode', 'indeterminate');
e81207a2   梁灏   update Tree
104
105
              }
          },
34ee7b4a   梁灏   support Tree & ad...
106
          mounted () {
cb84e64a   梁灏   update Tree
107
              this.updateData();
cb84e64a   梁灏   update Tree
108
109
110
111
              this.$on('selected', ori => {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  nodes.forEach(node => {
                      this.$set(node.data, 'selected', false);
ce03ac52   梁灏   update Tree
112
                  });
cb84e64a   梁灏   update Tree
113
                  this.$set(ori, 'selected', true);
e81207a2   梁灏   update Tree
114
              });
cb84e64a   梁灏   update Tree
115
116
              this.$on('on-selected', () => {
                  this.$emit('on-select-change', this.getSelectedNodes());
e81207a2   梁灏   update Tree
117
              });
cb84e64a   梁灏   update Tree
118
              this.$on('checked', () => {
3c145e6f   梁灏   update Tree
119
                  this.updateData(false);
e81207a2   梁灏   update Tree
120
              });
53754a31   梁灏   fixed #468
121
122
123
              this.$on('on-checked', () => {
                  this.$emit('on-check-change', this.getCheckedNodes());
              });
75dad873   Lawrence Lee   add listener on T...
124
125
126
              this.$on('toggle-expand', (payload) => {
                  this.$emit('on-toggle-expand', payload);
              });
69a10b78   梁灏   fixed #787
127
128
129
130
131
132
          },
          watch: {
              data () {
                  this.$nextTick(() => {
                      this.updateData();
                      this.broadcast('TreeNode', 'indeterminate');
545aa32b   梁灏   update
133
                  });
69a10b78   梁灏   fixed #787
134
              }
e81207a2   梁灏   update Tree
135
          }
89f2ba8b   梁灏   init Tree component
136
137
      };
  </script>