Blame view

src/components/tree/tree.vue 11.1 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
8
9
10
11
12
13
              <!--<span v-if="showCheckbox" :class="checkboxCls(item)" @click="setCheck(item.disabled||item.disableCheckbox,$index)">-->
                  <!--<span :class="[prefixCls + '-checkbox-inner']"></span>-->
              <!--</span>-->
              <Checkbox
                  :checked="item.checked && item.childrenCheckedStatus == 2"
                  :disabled="item.disabled || item.disableCheckbox"
                  @click.prevent="setCheck(item.disabled||item.disableCheckbox,$index)"></Checkbox>
e81207a2   梁灏   update Tree
14
15
16
17
18
19
20
21
22
23
24
25
26
27
              <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)"
                  :data.sync="item.node"
                  :key="this.key+'.'+$index"
                  :multiple="multiple"
                  :show-checkbox="showCheckbox"
                  transition="slide-up"></tree>
          </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
33
34
35
      import { t } from '../../locale';
  
      const prefixCls = 'ivu-tree';
  
89f2ba8b   梁灏   init Tree component
36
      export default {
e81207a2   梁灏   update Tree
37
          name: 'tree',
b923c818   梁灏   update Tree
38
          components: { Icon, Checkbox },
e81207a2   梁灏   update Tree
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
          props: {
              data: {
                  type: Array,
                  default () {
                      return [];
                  }
              },
              key: {
                  type: String,
                  default: '0'
              },
              multiple: {
                  type: Boolean,
                  default: false
              },
              showCheckbox: {
                  type: Boolean,
                  default: false
              },
              onSelect: {
                  type: Function,
                  default () {
                      return {};
                  }
              },
              onCheck: {
                  type: Function,
                  default () {
                      return {};
                  }
              },
              emptyText: {
                  type: String,
                  default () {
                      return t('i.tree.emptyText');
                  }
              }
          },
89f2ba8b   梁灏   init Tree component
77
          data () {
e81207a2   梁灏   update Tree
78
79
80
              return {
                  prefixCls: prefixCls
              };
89f2ba8b   梁灏   init Tree component
81
          },
e81207a2   梁灏   update Tree
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
          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
                      }
                  ];
              },
              checkboxCls (item) {
                  return [
                      `${this.prefixCls}-checkbox`,
                      {
                          [`${this.prefixCls}-checkbox-disabled`]: item.disabled || item.disableCheckbox,
                          [`${this.prefixCls}-checkbox-checked`]: item.checked && item.childrenCheckedStatus == 2,
                          [`${this.prefixCls}-checkbox-indeterminate`]: item.checked && item.childrenCheckedStatus == 1
                      }
                  ];
              },
              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
147
              preHandle () {
e81207a2   梁灏   update Tree
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
                  for (let [i,item] of this.data.entries()) {
                      if (!item.node || !item.node.length) {
                          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 {
                          let status = this.getChildrenCheckedStatus(item.node);
                          this.$set(`data[${i}].childrenCheckedStatus`, status);
                          if (status !== 0) this.$set(`data[${i}].checked`, true);
                      }
                  }
              },
b923c818   梁灏   update Tree
164
              setExpand (disabled, index) {
e81207a2   梁灏   update Tree
165
166
167
168
                  if (!disabled) {
                      this.$set(`data[${index}].expand`, !this.data[index].expand);
                  }
              },
b923c818   梁灏   update Tree
169
              setSelect (disabled, index) {
e81207a2   梁灏   update Tree
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
                  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
186
              setCheck (disabled, index) {
e81207a2   梁灏   update Tree
187
188
189
190
191
192
193
                  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
194
              getNodes (data, opt) {
e81207a2   梁灏   update Tree
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
                  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);
                      }
                      if (node.node && node.node.length) {
                          res = res.concat(this.getNodes(node.node, opt));
                      }
                  }
                  return res;
              },
b923c818   梁灏   update Tree
214
              getSelectedNodes () {
e81207a2   梁灏   update Tree
215
216
                  return this.getNodes(this.data, {selected: true});
              },
b923c818   梁灏   update Tree
217
              getCheckedNodes () {
e81207a2   梁灏   update Tree
218
219
                  return this.getNodes(this.data, {checked: true, childrenCheckedStatus: 2});
              },
b923c818   梁灏   update Tree
220
              getChildrenCheckedStatus (children) {
e81207a2   梁灏   update Tree
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
                  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);
                  }
                  if (this.onSelect) {
                      this.$nextTick(() => {
                          this.onSelect(this.getSelectedNodes());
                      });
                  }
              });
              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) => {
                  if (this.key === '0' && this.onCheck) {
                      this.$nextTick(() => {
                          this.onCheck(this.getCheckedNodes());
                      });
                  }
                  if (this === ori) return;
                  for (let [i,item] of this.data.entries()) {
                      if (this.key + '.' + i == key) {
                          let temp = this.getChildrenCheckedStatus(item.node);
                          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
297
298
      };
  </script>