Blame view

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