Blame view

src/components/tree/tree.vue 3.56 KB
89f2ba8b   梁灏   init Tree component
1
  <template>
cb84e64a   梁灏   update Tree
2
3
4
5
6
7
8
9
10
11
      <div :class="prefixCls">
          <Tree-node
              v-for="item in currentData"
              :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 {
cb84e64a   梁灏   update Tree
49
50
                  prefixCls: prefixCls,
                  currentData: this.data
e81207a2   梁灏   update Tree
51
              };
89f2ba8b   梁灏   init Tree component
52
          },
e81207a2   梁灏   update Tree
53
          watch: {
cb84e64a   梁灏   update Tree
54
55
              data (val) {
                  
e81207a2   梁灏   update Tree
56
57
58
              }
          },
          methods: {
cb84e64a   梁灏   update Tree
59
60
61
62
63
64
65
66
67
68
69
70
              getSelectedNodes () {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  return nodes.filter(node => node.data.selected).map(node => node.data);
              },
              updateData () {
                  // 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...
71
                          });
cb84e64a   梁灏   update Tree
72
73
74
  //                        data.checked = checkedLength >= data.children.length;
                          if (checkedLength >= data.children.length) data.checked = true;
                          return data;
e81207a2   梁灏   update Tree
75
                      } else {
cb84e64a   梁灏   update Tree
76
                          return data;
e81207a2   梁灏   update Tree
77
78
                      }
                  }
cb84e64a   梁灏   update Tree
79
80
81
82
83
84
                  
                  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
85
                          });
cb84e64a   梁灏   update Tree
86
                          return data;
75c32d5f   梁灏   rebuild Tree
87
                      } else {
cb84e64a   梁灏   update Tree
88
                          return data;
75c32d5f   梁灏   rebuild Tree
89
                      }
e81207a2   梁灏   update Tree
90
                  }
cb84e64a   梁灏   update Tree
91
92
                  this.currentData = this.data.map(node => reverseChecked(node)).map(node => forwardChecked(node));
                  this.broadcast('TreeNode', 'indeterminate');
e81207a2   梁灏   update Tree
93
94
              }
          },
34ee7b4a   梁灏   support Tree & ad...
95
          mounted () {
cb84e64a   梁灏   update Tree
96
              this.updateData();
e81207a2   梁灏   update Tree
97
  
cb84e64a   梁灏   update Tree
98
99
100
101
              this.$on('selected', ori => {
                  const nodes = findComponentsDownward(this, 'TreeNode');
                  nodes.forEach(node => {
                      this.$set(node.data, 'selected', false);
ce03ac52   梁灏   update Tree
102
                  });
cb84e64a   梁灏   update Tree
103
                  this.$set(ori, 'selected', true);
e81207a2   梁灏   update Tree
104
              });
cb84e64a   梁灏   update Tree
105
106
              this.$on('on-selected', () => {
                  this.$emit('on-select-change', this.getSelectedNodes());
e81207a2   梁灏   update Tree
107
              });
cb84e64a   梁灏   update Tree
108
109
              this.$on('checked', () => {
                  this.updateData();
e81207a2   梁灏   update Tree
110
111
              });
          }
89f2ba8b   梁灏   init Tree component
112
113
      };
  </script>