Blame view

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