Blame view

src/components/transfer/transfer.vue 9.38 KB
77f7bb95   梁灏   add Transfer comp...
1
2
3
  <script>
      import List from './list.vue';
      import Operation from './operation.vue';
e5337c81   梁灏   fixed some compon...
4
      import Locale from '../../mixins/locale';
cd78c9c4   梁灏   some comps suppor...
5
      import Emitter from '../../mixins/emitter';
77f7bb95   梁灏   add Transfer comp...
6
7
8
9
  
      const prefixCls = 'ivu-transfer';
  
      export default {
7bab7c15   david.shi   fix components/tr...
10
          name: 'Transfer',
e5337c81   梁灏   fixed some compon...
11
          mixins: [ Emitter, Locale ],
7479854b   梁灏   update Transfer
12
          render (h) {
16a87cde   jingsam   fix transfer
13
14
  
              function cloneVNode (vnode) {
92a0f30c   jingsam   fixup
15
                  const clonedChildren = vnode.children && vnode.children.map(vnode => cloneVNode(vnode));
7479854b   梁灏   update Transfer
16
                  const cloned = h(vnode.tag, vnode.data, clonedChildren);
16a87cde   jingsam   fix transfer
17
18
19
20
21
22
23
24
25
26
27
28
                  cloned.text = vnode.text;
                  cloned.isComment = vnode.isComment;
                  cloned.componentOptions = vnode.componentOptions;
                  cloned.elm = vnode.elm;
                  cloned.context = vnode.context;
                  cloned.ns = vnode.ns;
                  cloned.isStatic = vnode.isStatic;
                  cloned.key = vnode.key;
  
                  return cloned;
              }
  
84a8a413   梁灏   fixed bug of Tran...
29
30
              const vNodes = this.$slots.default === undefined ? [] : this.$slots.default;
              const clonedVNodes = this.$slots.default === undefined ? [] : vNodes.map(vnode => cloneVNode(vnode));
16a87cde   jingsam   fix transfer
31
  
7479854b   梁灏   update Transfer
32
              return h('div', {
16a87cde   jingsam   fix transfer
33
34
                  'class': this.classes
              }, [
7479854b   梁灏   update Transfer
35
                  h(List, {
16a87cde   jingsam   fix transfer
36
37
38
39
40
41
42
                      ref: 'left',
                      props: {
                          prefixCls: this.prefixCls + '-list',
                          data: this.leftData,
                          renderFormat: this.renderFormat,
                          checkedKeys: this.leftCheckedKeys,
                          validKeysCount: this.leftValidKeysCount,
f031f465   Rui Ma   `style` cannot be...
43
                          listStyle: this.listStyle,
e5337c81   梁灏   fixed some compon...
44
                          title: this.localeTitles[0],
16a87cde   jingsam   fix transfer
45
                          filterable: this.filterable,
e5337c81   梁灏   fixed some compon...
46
                          filterPlaceholder: this.localeFilterPlaceholder,
16a87cde   jingsam   fix transfer
47
                          filterMethod: this.filterMethod,
e5337c81   梁灏   fixed some compon...
48
                          notFoundText: this.localeNotFoundText
16a87cde   jingsam   fix transfer
49
50
51
52
53
54
                      },
                      on: {
                          'on-checked-keys-change': this.handleLeftCheckedKeysChange
                      }
                  }, vNodes),
  
7479854b   梁灏   update Transfer
55
                  h(Operation, {
16a87cde   jingsam   fix transfer
56
57
58
59
60
61
62
63
                      props: {
                          prefixCls: this.prefixCls,
                          operations: this.operations,
                          leftActive: this.leftValidKeysCount > 0,
                          rightActive: this.rightValidKeysCount > 0
                      }
                  }),
  
7479854b   梁灏   update Transfer
64
                  h(List, {
16a87cde   jingsam   fix transfer
65
66
67
68
69
70
71
                      ref: 'right',
                      props: {
                          prefixCls: this.prefixCls + '-list',
                          data: this.rightData,
                          renderFormat: this.renderFormat,
                          checkedKeys: this.rightCheckedKeys,
                          validKeysCount: this.rightValidKeysCount,
f031f465   Rui Ma   `style` cannot be...
72
                          listStyle: this.listStyle,
e5337c81   梁灏   fixed some compon...
73
                          title: this.localeTitles[1],
16a87cde   jingsam   fix transfer
74
                          filterable: this.filterable,
e5337c81   梁灏   fixed some compon...
75
                          filterPlaceholder: this.localeFilterPlaceholder,
16a87cde   jingsam   fix transfer
76
                          filterMethod: this.filterMethod,
e5337c81   梁灏   fixed some compon...
77
                          notFoundText: this.localeNotFoundText
16a87cde   jingsam   fix transfer
78
79
80
81
                      },
                      on: {
                          'on-checked-keys-change': this.handleRightCheckedKeysChange
                      }
aa9fc758   梁灏   update Transfer
82
                  }, clonedVNodes)
16a87cde   jingsam   fix transfer
83
84
              ]);
          },
77f7bb95   梁灏   add Transfer comp...
85
86
87
88
          props: {
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
89
                      return [];
77f7bb95   梁灏   add Transfer comp...
90
91
92
93
94
95
96
97
98
99
100
                  }
              },
              renderFormat: {
                  type: Function,
                  default (item) {
                      return item.label || item.key;
                  }
              },
              targetKeys: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
101
                      return [];
77f7bb95   梁灏   add Transfer comp...
102
103
104
105
106
                  }
              },
              selectedKeys: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
107
                      return [];
77f7bb95   梁灏   add Transfer comp...
108
109
110
111
112
                  }
              },
              listStyle: {
                  type: Object,
                  default () {
b0893113   jingsam   :art: add eslint
113
                      return {};
77f7bb95   梁灏   add Transfer comp...
114
115
116
                  }
              },
              titles: {
e5337c81   梁灏   fixed some compon...
117
                  type: Array
77f7bb95   梁灏   add Transfer comp...
118
119
120
121
              },
              operations: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
122
                      return [];
77f7bb95   梁灏   add Transfer comp...
123
124
125
126
127
128
129
                  }
              },
              filterable: {
                  type: Boolean,
                  default: false
              },
              filterPlaceholder: {
e5337c81   梁灏   fixed some compon...
130
                  type: String
77f7bb95   梁灏   add Transfer comp...
131
132
133
134
135
136
137
138
139
              },
              filterMethod: {
                  type: Function,
                  default (data, query) {
                      const type = ('label' in data) ? 'label' : 'key';
                      return data[type].indexOf(query) > -1;
                  }
              },
              notFoundText: {
e5337c81   梁灏   fixed some compon...
140
                  type: String
77f7bb95   梁灏   add Transfer comp...
141
142
143
144
145
146
147
148
149
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  leftData: [],
                  rightData: [],
                  leftCheckedKeys: [],
                  rightCheckedKeys: []
b0893113   jingsam   :art: add eslint
150
              };
77f7bb95   梁灏   add Transfer comp...
151
152
153
154
155
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`
b0893113   jingsam   :art: add eslint
156
                  ];
77f7bb95   梁灏   add Transfer comp...
157
158
159
160
161
162
              },
              leftValidKeysCount () {
                  return this.getValidKeys('left').length;
              },
              rightValidKeysCount () {
                  return this.getValidKeys('right').length;
e5337c81   梁灏   fixed some compon...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
              },
              localeFilterPlaceholder () {
                  if (this.filterPlaceholder === undefined) {
                      return this.t('i.transfer.filterPlaceholder');
                  } else {
                      return this.filterPlaceholder;
                  }
              },
              localeNotFoundText () {
                  if (this.notFoundText === undefined) {
                      return this.t('i.transfer.notFoundText');
                  } else {
                      return this.notFoundText;
                  }
              },
              localeTitles () {
                  if (this.titles === undefined) {
                      return [this.t('i.transfer.titles.source'), this.t('i.transfer.titles.target')];
                  } else {
                      return this.titles;
                  }
77f7bb95   梁灏   add Transfer comp...
184
185
186
187
188
189
190
191
192
193
194
              }
          },
          methods: {
              getValidKeys (direction) {
                  return this[`${direction}Data`].filter(data => !data.disabled && this[`${direction}CheckedKeys`].indexOf(data.key) > -1).map(data => data.key);
              },
              splitData (init = false) {
                  this.leftData = [...this.data];
                  this.rightData = [];
                  if (this.targetKeys.length > 0) {
                      this.targetKeys.forEach((targetKey) => {
778e9214   Lawrence Lee   bugfix on transfer
195
196
197
198
199
200
                          const filteredData = this.leftData.filter((data, index) => {
                              if (data.key === targetKey) {
                                  this.leftData.splice(index, 1);
                                  return true;
                              }
                              return false;
11748ba5   Lawrence Lee   lint
201
202
                          });
                          if (filteredData && filteredData.length > 0) this.rightData.push(filteredData[0]);
77f7bb95   梁灏   add Transfer comp...
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
                      });
                  }
                  if (init) {
                      this.splitSelectedKey();
                  }
              },
              splitSelectedKey () {
                  const selectedKeys = this.selectedKeys;
                  if (selectedKeys.length > 0) {
                      this.leftCheckedKeys = this.leftData
                              .filter(data => selectedKeys.indexOf(data.key) > -1)
                              .map(data => data.key);
                      this.rightCheckedKeys = this.rightData
                              .filter(data => selectedKeys.indexOf(data.key) > -1)
                              .map(data => data.key);
                  }
              },
              moveTo (direction) {
                  const targetKeys = this.targetKeys;
                  const opposite = direction === 'left' ? 'right' : 'left';
                  const moveKeys = this.getValidKeys(opposite);
                  const newTargetKeys = direction === 'right' ?
                          moveKeys.concat(targetKeys) :
                          targetKeys.filter(targetKey => !moveKeys.some(checkedKey => targetKey === checkedKey));
  
                  this.$refs[opposite].toggleSelectAll(false);
                  this.$emit('on-change', newTargetKeys, direction, moveKeys);
cd78c9c4   梁灏   some comps suppor...
230
231
232
233
234
                  this.dispatch('FormItem', 'on-form-change', {
                      tarketKeys: newTargetKeys,
                      direction: direction,
                      moveKeys: moveKeys
                  });
5b19b5f5   梁灏   support Transfer
235
236
237
238
239
240
              },
              handleLeftCheckedKeysChange (keys) {
                  this.leftCheckedKeys = keys;
              },
              handleRightCheckedKeysChange (keys) {
                  this.rightCheckedKeys = keys;
9f5a643a   梁灏   fixed #2019
241
242
243
244
245
              },
              handleCheckedKeys () {
                  const sourceSelectedKeys = this.getValidKeys('left');
                  const targetSelectedKeys = this.getValidKeys('right');
                  this.$emit('on-selected-change', sourceSelectedKeys, targetSelectedKeys);
77f7bb95   梁灏   add Transfer comp...
246
247
248
249
250
              }
          },
          watch: {
              targetKeys () {
                  this.splitData(false);
8fbc8c71   梁灏   fixed #555
251
252
253
              },
              data () {
                  this.splitData(false);
77f7bb95   梁灏   add Transfer comp...
254
255
              }
          },
7cc81ecb   梁灏   update Transfer t...
256
          mounted () {
77f7bb95   梁灏   add Transfer comp...
257
258
              this.splitData(true);
          }
b0893113   jingsam   :art: add eslint
259
260
      };
  </script>