Blame view

src/components/tree/tree.vue 13.2 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';
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: {
9d79a51f   梁灏   update Tree
42
              data: {
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 {
9d79a51f   梁灏   update Tree
69
                  prefixCls: prefixCls
e81207a2   梁灏   update Tree
70
              };
89f2ba8b   梁灏   init Tree component
71
          },
e81207a2   梁灏   update Tree
72
73
          computed: {
              classes () {
34ee7b4a   梁灏   support Tree & ad...
74
                  if (this.name === '0') {
e81207a2   梁灏   update Tree
75
76
77
78
79
80
81
                      return this.prefixCls;
                  } else {
                      return `${this.prefixCls}-child-tree`;
                  }
              }
          },
          watch: {
34ee7b4a   梁灏   support Tree & ad...
82
83
              data () {
                  if (this.name === '0') {
e81207a2   梁灏   update Tree
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
                      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
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++) {
34ee7b4a   梁灏   support Tree & ad...
124
                      this.data[i].name = `${this.name}.${i}`;
e81207a2   梁灏   update Tree
125
126
                  }
              },
b923c818   梁灏   update Tree
127
              preHandle () {
34ee7b4a   梁灏   support Tree & ad...
128
                  for (let [i, item] of this.data.entries()) {
ce03ac52   梁灏   update Tree
129
                      if (!item.children || !item.children.length) {
34ee7b4a   梁灏   support Tree & ad...
130
131
132
133
  //                        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
134
135
136
                          continue;
                      }
                      if (item.checked && !item.childrenCheckedStatus) {
34ee7b4a   梁灏   support Tree & ad...
137
138
139
140
141
142
143
  //                        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
144
                      } else {
ce03ac52   梁灏   update Tree
145
                          let status = this.getChildrenCheckedStatus(item.children);
34ee7b4a   梁灏   support Tree & ad...
146
147
148
149
  //                        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
150
151
152
                      }
                  }
              },
b923c818   梁灏   update Tree
153
              setExpand (disabled, index) {
e81207a2   梁灏   update Tree
154
                  if (!disabled) {
34ee7b4a   梁灏   support Tree & ad...
155
156
  //                    this.$set(`data[${index}].expand`, !this.data[index].expand);
                      this.$set(this.data[index], 'expand', !this.data[index].expand);
e81207a2   梁灏   update Tree
157
158
                  }
              },
b923c818   梁灏   update Tree
159
              setSelect (disabled, index) {
e81207a2   梁灏   update Tree
160
161
162
                  if (!disabled) {
                      const selected = !this.data[index].selected;
                      if (this.multiple || !selected) {
34ee7b4a   梁灏   support Tree & ad...
163
164
  //                        this.$set(`data[${index}].selected`, selected);
                          this.$set(this.data[index], 'selected', selected);
e81207a2   梁灏   update Tree
165
166
167
                      } else {
                          for (let i = 0; i < this.data.length; i++) {
                              if (i == index) {
34ee7b4a   梁灏   support Tree & ad...
168
169
  //                                this.$set(`data[${i}].selected`, true);
                                  this.$set(this.data[i], 'selected', true);
e81207a2   梁灏   update Tree
170
                              } else {
34ee7b4a   梁灏   support Tree & ad...
171
172
  //                                this.$set(`data[${i}].selected`, false);
                                  this.$set(this.data[i], 'selected', false);
e81207a2   梁灏   update Tree
173
174
175
                              }
                          }
                      }
34ee7b4a   梁灏   support Tree & ad...
176
177
178
179
  //                    this.$dispatch('nodeSelected', this, selected);
                      this.dispatch('Tree', 'nodeSelected', {
                          ori: this,
                          selected: selected
e6c0b158   梁灏   update
180
                      });
e81207a2   梁灏   update Tree
181
182
                  }
              },
b923c818   梁灏   update Tree
183
              setCheck (disabled, index) {
e81207a2   梁灏   update Tree
184
185
                  if (disabled) return;
                  const checked = !this.data[index].checked;
34ee7b4a   梁灏   support Tree & ad...
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  //                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
200
              },
b923c818   梁灏   update Tree
201
              getNodes (data, opt) {
e81207a2   梁灏   update Tree
202
203
204
205
206
207
208
209
210
211
212
213
214
                  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
215
216
                      if (node.children && node.children.length) {
                          res = res.concat(this.getNodes(node.children, opt));
e81207a2   梁灏   update Tree
217
218
219
220
                      }
                  }
                  return res;
              },
b923c818   梁灏   update Tree
221
              getSelectedNodes () {
e81207a2   梁灏   update Tree
222
223
                  return this.getNodes(this.data, {selected: true});
              },
b923c818   梁灏   update Tree
224
              getCheckedNodes () {
e81207a2   梁灏   update Tree
225
226
                  return this.getNodes(this.data, {checked: true, childrenCheckedStatus: 2});
              },
b923c818   梁灏   update Tree
227
              getChildrenCheckedStatus (children) {
e81207a2   梁灏   update Tree
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
                  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...
248
          mounted () {
e81207a2   梁灏   update Tree
249
250
251
              this.setKey();
              this.preHandle();
  
34ee7b4a   梁灏   support Tree & ad...
252
253
254
255
256
257
  //            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
258
259
260
                  if (!this.multiple && selected) {
                      if (this !== ori) {
                          for (let i = 0; i < this.data.length; i++) {
34ee7b4a   梁灏   support Tree & ad...
261
262
  //                            this.$set(`data[${i}].selected`, false);
                              this.$set(this.data[i], 'selected', false);
e81207a2   梁灏   update Tree
263
264
                          }
                      }
34ee7b4a   梁灏   support Tree & ad...
265
266
  //                    this.$broadcast('cancelSelected', ori);
                      this.broadcast('Tree', 'cancelSelected', ori);
e81207a2   梁灏   update Tree
267
                  }
ce03ac52   梁灏   update Tree
268
269
270
                  this.$nextTick(() => {
                      this.$emit('on-select-change', this.getSelectedNodes());
                  });
e81207a2   梁灏   update Tree
271
272
              });
              this.$on('cancelSelected', ori => {
34ee7b4a   梁灏   support Tree & ad...
273
274
  //                this.$broadcast('cancelSelected', ori);
                  this.broadcast('Tree', 'cancelSelected', ori);
e81207a2   梁灏   update Tree
275
276
                  if (this !== ori) {
                      for (let i = 0; i < this.data.length; i++) {
34ee7b4a   梁灏   support Tree & ad...
277
278
  //                        this.$set(`data[${i}].selected`, false);
                          this.$set(this.data[i], 'selected', false);
e81207a2   梁灏   update Tree
279
280
281
                      }
                  }
              });
34ee7b4a   梁灏   support Tree & ad...
282
283
284
285
286
287
  //            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
288
                      for (let i = 0; i < this.data.length; i++) {
34ee7b4a   梁灏   support Tree & ad...
289
290
291
292
  //                        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
293
                      }
34ee7b4a   梁灏   support Tree & ad...
294
295
296
297
298
  //                    this.$broadcast('parentChecked', status, name);
                      this.broadcast('Tree', 'parentChecked', {
                          status: status,
                          name: name
                      });
e81207a2   梁灏   update Tree
299
300
                  }
              });
34ee7b4a   梁灏   support Tree & ad...
301
302
303
304
305
306
  //            this.$on('childChecked', (ori, name) => {
              this.$on('childChecked', (params) => {
                  const ori = params.ori;
                  const name = params.name;
  
                  if (this.name === '0') {
e81207a2   梁灏   update Tree
307
                      this.$nextTick(() => {
ce03ac52   梁灏   update Tree
308
                          this.$emit('on-check-change', this.getCheckedNodes());
e81207a2   梁灏   update Tree
309
310
311
312
                      });
                  }
                  if (this === ori) return;
                  for (let [i,item] of this.data.entries()) {
34ee7b4a   梁灏   support Tree & ad...
313
                      if (this.name + '.' + i == name) {
ce03ac52   梁灏   update Tree
314
                          let temp = this.getChildrenCheckedStatus(item.children);
e81207a2   梁灏   update Tree
315
                          if (temp != item.childrenCheckedStatus) {
34ee7b4a   梁灏   support Tree & ad...
316
317
318
319
320
321
322
323
324
  //                            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
325
326
327
328
329
                          }
                      }
                  }
              });
          }
89f2ba8b   梁灏   init Tree component
330
331
      };
  </script>