Blame view

src/components/tree/tree.vue 3.94 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>
a9e59197   梁灏   update Tree
11
          <div :class="[prefixCls + '-empty']" v-if="!data.length">{{ emptyText }}</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';
cb84e64a   梁灏   update Tree
18
      import { t } from '../../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',
e2c6ff2b   梁灏   update Rate
24
          mixins: [ Emitter ],
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
42
43
44
45
46
47
              emptyText: {
                  type: String,
                  default () {
                      return t('i.tree.emptyText');
                  }
              }
          },
89f2ba8b   梁灏   init Tree component
48
          data () {
e81207a2   梁灏   update Tree
49
              return {
3c145e6f   梁灏   update Tree
50
                  prefixCls: prefixCls
e81207a2   梁灏   update Tree
51
              };
89f2ba8b   梁灏   init Tree component
52
          },
e81207a2   梁灏   update Tree
53
          methods: {
cb84e64a   梁灏   update Tree
54
55
56
57
              getSelectedNodes () {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  return nodes.filter(node => node.data.selected).map(node => node.data);
              },
53754a31   梁灏   fixed #468
58
59
60
61
              getCheckedNodes () {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  return nodes.filter(node => node.data.checked).map(node => node.data);
              },
3c145e6f   梁灏   update Tree
62
              updateData (isInit = true) {
cb84e64a   梁灏   update Tree
63
64
65
66
67
68
69
                  // 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...
70
                          });
3c145e6f   梁灏   update Tree
71
72
73
74
75
                          if (isInit) {
                              if (checkedLength >= data.children.length) data.checked = true;
                          } else {
                              data.checked = checkedLength >= data.children.length;
                          }
cb84e64a   梁灏   update Tree
76
                          return data;
e81207a2   梁灏   update Tree
77
                      } else {
cb84e64a   梁灏   update Tree
78
                          return data;
e81207a2   梁灏   update Tree
79
80
                      }
                  }
cb84e64a   梁灏   update Tree
81
82
83
84
85
86
                  
                  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
87
                          });
cb84e64a   梁灏   update Tree
88
                          return data;
75c32d5f   梁灏   rebuild Tree
89
                      } else {
cb84e64a   梁灏   update Tree
90
                          return data;
75c32d5f   梁灏   rebuild Tree
91
                      }
e81207a2   梁灏   update Tree
92
                  }
3c145e6f   梁灏   update Tree
93
                  this.data.map(node => reverseChecked(node)).map(node => forwardChecked(node));
cb84e64a   梁灏   update Tree
94
                  this.broadcast('TreeNode', 'indeterminate');
e81207a2   梁灏   update Tree
95
96
              }
          },
34ee7b4a   梁灏   support Tree & ad...
97
          mounted () {
cb84e64a   梁灏   update Tree
98
              this.updateData();
e81207a2   梁灏   update Tree
99
  
cb84e64a   梁灏   update Tree
100
101
102
103
              this.$on('selected', ori => {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  nodes.forEach(node => {
                      this.$set(node.data, 'selected', false);
ce03ac52   梁灏   update Tree
104
                  });
cb84e64a   梁灏   update Tree
105
                  this.$set(ori, 'selected', true);
e81207a2   梁灏   update Tree
106
              });
cb84e64a   梁灏   update Tree
107
108
              this.$on('on-selected', () => {
                  this.$emit('on-select-change', this.getSelectedNodes());
e81207a2   梁灏   update Tree
109
              });
cb84e64a   梁灏   update Tree
110
              this.$on('checked', () => {
3c145e6f   梁灏   update Tree
111
                  this.updateData(false);
e81207a2   梁灏   update Tree
112
              });
53754a31   梁灏   fixed #468
113
114
115
              this.$on('on-checked', () => {
                  this.$emit('on-check-change', this.getCheckedNodes());
              });
e81207a2   梁灏   update Tree
116
          }
89f2ba8b   梁灏   init Tree component
117
118
      };
  </script>