Blame view

src/components/tree/tree.vue 10.2 KB
89f2ba8b   梁灏   init Tree component
1
  <template>
e81207a2   梁灏   update Tree
2
3
      <ul :class="classes">
          <li v-for="item in data" :class="itemCls(item)">
b923c818   梁灏   update Tree
4
5
              <span :class="arrowCls(item)" @click="setExpand(item.disabled, $index)">
                  <Icon type="arrow-right-b"></Icon>
e81207a2   梁灏   update Tree
6
              </span>
b923c818   梁灏   update Tree
7
              <Checkbox
b566d106   梁灏   update Tree
8
                  v-if="showCheckbox"
b923c818   梁灏   update Tree
9
10
                  :checked="item.checked && item.childrenCheckedStatus == 2"
                  :disabled="item.disabled || item.disableCheckbox"
07e243ff   梁灏   update Checkbox i...
11
                  :indeterminate="item.checked && item.childrenCheckedStatus == 1"
b923c818   梁灏   update Tree
12
                  @click.prevent="setCheck(item.disabled||item.disableCheckbox,$index)"></Checkbox>
e81207a2   梁灏   update Tree
13
14
15
16
17
18
19
              <a :class="titleCls(item)" @click="setSelect(item.disabled, $index)">
                  <span :class="[prefixCls + '-title']" v-html="item.title"></span>
              </a>
              <tree
                  v-if="!item.isLeaf"
                  v-show="item.expand"
                  :class="expandCls(item)"
ce03ac52   梁灏   update Tree
20
                  :data.sync="item.children"
e81207a2   梁灏   update Tree
21
22
23
24
25
26
                  :key="this.key+'.'+$index"
                  :multiple="multiple"
                  :show-checkbox="showCheckbox"
                  transition="slide-up"></tree>
          </li>
      </ul>
89f2ba8b   梁灏   init Tree component
27
28
  </template>
  <script>
b923c818   梁灏   update Tree
29
30
      import Icon from '../icon/icon.vue';
      import Checkbox from '../checkbox/checkbox.vue';
e81207a2   梁灏   update Tree
31
32
33
34
      import { t } from '../../locale';
  
      const prefixCls = 'ivu-tree';
  
89f2ba8b   梁灏   init Tree component
35
      export default {
e81207a2   梁灏   update Tree
36
          name: 'tree',
b923c818   梁灏   update Tree
37
          components: { Icon, Checkbox },
e81207a2   梁灏   update Tree
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
          props: {
              data: {
                  type: Array,
                  default () {
                      return [];
                  }
              },
              key: {
                  type: String,
                  default: '0'
              },
              multiple: {
                  type: Boolean,
                  default: false
              },
              showCheckbox: {
                  type: Boolean,
                  default: false
              },
e81207a2   梁灏   update Tree
57
58
59
60
61
62
63
              emptyText: {
                  type: String,
                  default () {
                      return t('i.tree.emptyText');
                  }
              }
          },
89f2ba8b   梁灏   init Tree component
64
          data () {
e81207a2   梁灏   update Tree
65
66
67
              return {
                  prefixCls: prefixCls
              };
89f2ba8b   梁灏   init Tree component
68
          },
e81207a2   梁灏   update Tree
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
          computed: {
              classes () {
                  if (this.key === '0') {
                      return this.prefixCls;
                  } else {
                      return `${this.prefixCls}-child-tree`;
                  }
              }
          },
          watch: {
              data(){
                  if (this.key === '0') {
                      this.setKey();
                      this.preHandle();
                  }
              }
          },
          methods: {
              itemCls (item) {
                  return [
                      {
                          [`${prefixCls}-item-disabled`]: item.disabled
                      }
                  ];
              },
              arrowCls (item) {
                  return [
                      `${this.prefixCls}-switcher`,
                      {
                          [`${this.prefixCls}-switcher-disabled`]: item.disabled,
                          [`${this.prefixCls}-noline_close`]: !item.expand && !item.isLeaf,
                          [`${this.prefixCls}-noline_open`]: item.expand && !item.isLeaf,
                          [`${this.prefixCls}-switcher-noop`]: item.isLeaf
                      }
                  ];
              },
e81207a2   梁灏   update Tree
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
              titleCls (item) {
                  return [
                      {
                          [`${this.prefixCls}-node-selected`]: item.selected
                      }
                  ];
              },
              expandCls (item) {
                  return [
                      {
                          [`${this.prefixCls}-child-tree-open`]: item.expand
                      }
                  ];
              },
              setKey () {
                  for (let i = 0; i < this.data.length; i++) {
                      this.data[i].key = `${this.key}.${i}`;
                  }
              },
b923c818   梁灏   update Tree
124
              preHandle () {
e81207a2   梁灏   update Tree
125
                  for (let [i,item] of this.data.entries()) {
ce03ac52   梁灏   update Tree
126
                      if (!item.children || !item.children.length) {
e81207a2   梁灏   update Tree
127
128
129
130
131
132
133
134
                          this.$set(`data[${i}].isLeaf`, true);
                          this.$set(`data[${i}].childrenCheckedStatus`, 2);
                          continue;
                      }
                      if (item.checked && !item.childrenCheckedStatus) {
                          this.$set(`data[${i}].childrenCheckedStatus`, 2);
                          this.$broadcast('parentChecked', true, `${this.key}.${i}`);
                      } else {
ce03ac52   梁灏   update Tree
135
                          let status = this.getChildrenCheckedStatus(item.children);
e81207a2   梁灏   update Tree
136
137
138
139
140
                          this.$set(`data[${i}].childrenCheckedStatus`, status);
                          if (status !== 0) this.$set(`data[${i}].checked`, true);
                      }
                  }
              },
b923c818   梁灏   update Tree
141
              setExpand (disabled, index) {
e81207a2   梁灏   update Tree
142
143
144
145
                  if (!disabled) {
                      this.$set(`data[${index}].expand`, !this.data[index].expand);
                  }
              },
b923c818   梁灏   update Tree
146
              setSelect (disabled, index) {
e81207a2   梁灏   update Tree
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
                  if (!disabled) {
                      const selected = !this.data[index].selected;
                      if (this.multiple || !selected) {
                          this.$set(`data[${index}].selected`, selected);
                      } else {
                          for (let i = 0; i < this.data.length; i++) {
                              if (i == index) {
                                  this.$set(`data[${i}].selected`, true);
                              } else {
                                  this.$set(`data[${i}].selected`, false);
                              }
                          }
                      }
                      this.$dispatch('nodeSelected', this, selected);
                  }
              },
b923c818   梁灏   update Tree
163
              setCheck (disabled, index) {
e81207a2   梁灏   update Tree
164
165
166
167
168
169
170
                  if (disabled) return;
                  const checked = !this.data[index].checked;
                  this.$set(`data[${index}].checked`, checked);
                  this.$set(`data[${index}].childrenCheckedStatus`, checked ? 2 : 0);
                  this.$dispatch('childChecked', this, this.key);
                  this.$broadcast('parentChecked', checked, `${this.key}.${index}`);
              },
b923c818   梁灏   update Tree
171
              getNodes (data, opt) {
e81207a2   梁灏   update Tree
172
173
174
175
176
177
178
179
180
181
182
183
184
                  data = data || this.data;
                  let res = [];
                  for (let node of data) {
                      let tmp = true;
                      for (let [key, value] of Object.entries(opt)) {
                          if (node[key] != value) {
                              tmp = false;
                              break;
                          }
                      }
                      if (tmp) {
                          res.push(node);
                      }
ce03ac52   梁灏   update Tree
185
186
                      if (node.children && node.children.length) {
                          res = res.concat(this.getNodes(node.children, opt));
e81207a2   梁灏   update Tree
187
188
189
190
                      }
                  }
                  return res;
              },
b923c818   梁灏   update Tree
191
              getSelectedNodes () {
e81207a2   梁灏   update Tree
192
193
                  return this.getNodes(this.data, {selected: true});
              },
b923c818   梁灏   update Tree
194
              getCheckedNodes () {
e81207a2   梁灏   update Tree
195
196
                  return this.getNodes(this.data, {checked: true, childrenCheckedStatus: 2});
              },
b923c818   梁灏   update Tree
197
              getChildrenCheckedStatus (children) {
e81207a2   梁灏   update Tree
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
                  let checkNum = 0, child_childrenAllChecked = true;
                  for (let child of children) {
                      if (child.checked) {
                          checkNum++;
                      }
                      if (child.childrenCheckedStatus !== 2) {
                          child_childrenAllChecked = false;
                      }
                  }
                  // select all
                  if (checkNum == children.length) {
                      return child_childrenAllChecked ? 2 : 1;
                      // select some
                  } else if (checkNum > 0) {
                      return 1;
                  } else {
                      return 0;
                  }
              }
          },
          ready(){
              this.setKey();
              this.preHandle();
  
              this.$on('nodeSelected', (ori, selected) => {
                  if (this.key !== '0') return true;
                  if (!this.multiple && selected) {
                      if (this !== ori) {
                          for (let i = 0; i < this.data.length; i++) {
                              this.$set(`data[${i}].selected`, false);
                          }
                      }
                      this.$broadcast('cancelSelected', ori);
                  }
ce03ac52   梁灏   update Tree
232
233
234
                  this.$nextTick(() => {
                      this.$emit('on-select-change', this.getSelectedNodes());
                  });
e81207a2   梁灏   update Tree
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
              });
              this.$on('cancelSelected', ori => {
                  this.$broadcast('cancelSelected', ori);
                  if (this !== ori) {
                      for (let i = 0; i < this.data.length; i++) {
                          this.$set(`data[${i}].selected`, false);
                      }
                  }
              });
              this.$on('parentChecked', (status, key) => {
                  if (this.key == key || this.key.startsWith(key + '.')) {
                      for (let i = 0; i < this.data.length; i++) {
                          this.$set(`data[${i}].checked`, status);
                          this.$set(`data[${i}].childrenCheckedStatus`, status ? 2 : 0);
                      }
                      this.$broadcast('parentChecked', status, key);
                  }
              });
              this.$on('childChecked', (ori, key) => {
ce03ac52   梁灏   update Tree
254
                  if (this.key === '0') {
e81207a2   梁灏   update Tree
255
                      this.$nextTick(() => {
ce03ac52   梁灏   update Tree
256
                          this.$emit('on-check-change', this.getCheckedNodes());
e81207a2   梁灏   update Tree
257
258
259
260
261
                      });
                  }
                  if (this === ori) return;
                  for (let [i,item] of this.data.entries()) {
                      if (this.key + '.' + i == key) {
ce03ac52   梁灏   update Tree
262
                          let temp = this.getChildrenCheckedStatus(item.children);
e81207a2   梁灏   update Tree
263
264
265
266
267
268
269
270
271
                          if (temp != item.childrenCheckedStatus) {
                              this.$set(`data[${i}].checked`, !!temp);
                              this.$set(`data[${i}].childrenCheckedStatus`, temp);
                              if (this.key !== '0') this.$dispatch('childChecked', this, this.key);
                          }
                      }
                  }
              });
          }
89f2ba8b   梁灏   init Tree component
272
273
      };
  </script>