Blame view

src/components/tree/tree.vue 4.13 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
              :key="item"
              :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';
  
89f2ba8b   梁灏   init Tree component
22
      export default {
34ee7b4a   梁灏   support Tree & ad...
23
          name: 'Tree',
e5337c81   梁灏   fixed some compon...
24
          mixins: [ Emitter, Locale ],
cb84e64a   梁灏   update Tree
25
          components: { TreeNode },
e81207a2   梁灏   update Tree
26
          props: {
9d79a51f   梁灏   update Tree
27
              data: {
e81207a2   梁灏   update Tree
28
29
30
31
32
                  type: Array,
                  default () {
                      return [];
                  }
              },
e81207a2   梁灏   update Tree
33
34
35
36
37
38
39
40
              multiple: {
                  type: Boolean,
                  default: false
              },
              showCheckbox: {
                  type: Boolean,
                  default: false
              },
e81207a2   梁灏   update Tree
41
              emptyText: {
e5337c81   梁灏   fixed some compon...
42
                  type: String
e81207a2   梁灏   update Tree
43
44
              }
          },
89f2ba8b   梁灏   init Tree component
45
          data () {
e81207a2   梁灏   update Tree
46
              return {
3c145e6f   梁灏   update Tree
47
                  prefixCls: prefixCls
e81207a2   梁灏   update Tree
48
              };
89f2ba8b   梁灏   init Tree component
49
          },
e5337c81   梁灏   fixed some compon...
50
51
52
53
54
55
56
57
58
          computed: {
              localeEmptyText () {
                  if (this.emptyText === undefined) {
                      return this.t('i.tree.emptyText');
                  } else {
                      return this.emptyText;
                  }
              }
          },
e81207a2   梁灏   update Tree
59
          methods: {
cb84e64a   梁灏   update Tree
60
61
62
63
              getSelectedNodes () {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  return nodes.filter(node => node.data.selected).map(node => node.data);
              },
53754a31   梁灏   fixed #468
64
65
66
67
              getCheckedNodes () {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  return nodes.filter(node => node.data.checked).map(node => node.data);
              },
3c145e6f   梁灏   update Tree
68
              updateData (isInit = true) {
cb84e64a   梁灏   update Tree
69
70
71
72
73
74
75
                  // 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...
76
                          });
3c145e6f   梁灏   update Tree
77
78
79
80
81
                          if (isInit) {
                              if (checkedLength >= data.children.length) data.checked = true;
                          } else {
                              data.checked = checkedLength >= data.children.length;
                          }
cb84e64a   梁灏   update Tree
82
                          return data;
e81207a2   梁灏   update Tree
83
                      } else {
cb84e64a   梁灏   update Tree
84
                          return data;
e81207a2   梁灏   update Tree
85
86
                      }
                  }
cb84e64a   梁灏   update Tree
87
88
89
90
91
92
                  
                  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
93
                          });
cb84e64a   梁灏   update Tree
94
                          return data;
75c32d5f   梁灏   rebuild Tree
95
                      } else {
cb84e64a   梁灏   update Tree
96
                          return data;
75c32d5f   梁灏   rebuild Tree
97
                      }
e81207a2   梁灏   update Tree
98
                  }
3c145e6f   梁灏   update Tree
99
                  this.data.map(node => reverseChecked(node)).map(node => forwardChecked(node));
cb84e64a   梁灏   update Tree
100
                  this.broadcast('TreeNode', 'indeterminate');
e81207a2   梁灏   update Tree
101
102
              }
          },
34ee7b4a   梁灏   support Tree & ad...
103
          mounted () {
cb84e64a   梁灏   update Tree
104
              this.updateData();
e81207a2   梁灏   update Tree
105
  
cb84e64a   梁灏   update Tree
106
107
108
109
              this.$on('selected', ori => {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  nodes.forEach(node => {
                      this.$set(node.data, 'selected', false);
ce03ac52   梁灏   update Tree
110
                  });
cb84e64a   梁灏   update Tree
111
                  this.$set(ori, 'selected', true);
e81207a2   梁灏   update Tree
112
              });
cb84e64a   梁灏   update Tree
113
114
              this.$on('on-selected', () => {
                  this.$emit('on-select-change', this.getSelectedNodes());
e81207a2   梁灏   update Tree
115
              });
cb84e64a   梁灏   update Tree
116
              this.$on('checked', () => {
3c145e6f   梁灏   update Tree
117
                  this.updateData(false);
e81207a2   梁灏   update Tree
118
              });
53754a31   梁灏   fixed #468
119
120
121
              this.$on('on-checked', () => {
                  this.$emit('on-check-change', this.getCheckedNodes());
              });
e81207a2   梁灏   update Tree
122
          }
89f2ba8b   梁灏   init Tree component
123
124
      };
  </script>