Blame view

src/components/transfer/transfer.vue 9.22 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 ],
16a87cde   jingsam   fix transfer
12
13
14
          render (createElement) {
  
              function cloneVNode (vnode) {
92a0f30c   jingsam   fixup
15
                  const clonedChildren = vnode.children && vnode.children.map(vnode => cloneVNode(vnode));
16a87cde   jingsam   fix transfer
16
17
18
19
20
21
22
23
24
25
26
27
28
                  const cloned = createElement(vnode.tag, vnode.data, clonedChildren);
                  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
32
33
34
35
36
37
38
39
40
41
42
  
              return createElement('div', {
                  'class': this.classes
              }, [
                  createElement('List', {
                      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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
                      },
                      on: {
                          'on-checked-keys-change': this.handleLeftCheckedKeysChange
                      }
                  }, vNodes),
  
                  createElement('Operation', {
                      props: {
                          prefixCls: this.prefixCls,
                          operations: this.operations,
                          leftActive: this.leftValidKeysCount > 0,
                          rightActive: this.rightValidKeysCount > 0
                      }
                  }),
  
                  createElement('List', {
                      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
85
              ]);
          },
  
77f7bb95   梁灏   add Transfer comp...
86
87
88
89
90
          components: { List, Operation },
          props: {
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
91
                      return [];
77f7bb95   梁灏   add Transfer comp...
92
93
94
95
96
97
98
99
100
101
102
                  }
              },
              renderFormat: {
                  type: Function,
                  default (item) {
                      return item.label || item.key;
                  }
              },
              targetKeys: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
103
                      return [];
77f7bb95   梁灏   add Transfer comp...
104
105
106
107
108
                  }
              },
              selectedKeys: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
109
                      return [];
77f7bb95   梁灏   add Transfer comp...
110
111
112
113
114
                  }
              },
              listStyle: {
                  type: Object,
                  default () {
b0893113   jingsam   :art: add eslint
115
                      return {};
77f7bb95   梁灏   add Transfer comp...
116
117
118
                  }
              },
              titles: {
e5337c81   梁灏   fixed some compon...
119
                  type: Array
77f7bb95   梁灏   add Transfer comp...
120
121
122
123
              },
              operations: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
124
                      return [];
77f7bb95   梁灏   add Transfer comp...
125
126
127
128
129
130
131
                  }
              },
              filterable: {
                  type: Boolean,
                  default: false
              },
              filterPlaceholder: {
e5337c81   梁灏   fixed some compon...
132
                  type: String
77f7bb95   梁灏   add Transfer comp...
133
134
135
136
137
138
139
140
141
              },
              filterMethod: {
                  type: Function,
                  default (data, query) {
                      const type = ('label' in data) ? 'label' : 'key';
                      return data[type].indexOf(query) > -1;
                  }
              },
              notFoundText: {
e5337c81   梁灏   fixed some compon...
142
                  type: String
77f7bb95   梁灏   add Transfer comp...
143
144
145
146
147
148
149
150
151
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  leftData: [],
                  rightData: [],
                  leftCheckedKeys: [],
                  rightCheckedKeys: []
b0893113   jingsam   :art: add eslint
152
              };
77f7bb95   梁灏   add Transfer comp...
153
154
155
156
157
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`
b0893113   jingsam   :art: add eslint
158
                  ];
77f7bb95   梁灏   add Transfer comp...
159
160
161
162
163
164
              },
              leftValidKeysCount () {
                  return this.getValidKeys('left').length;
              },
              rightValidKeysCount () {
                  return this.getValidKeys('right').length;
e5337c81   梁灏   fixed some compon...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
              },
              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...
186
187
188
189
190
191
192
193
194
195
196
              }
          },
          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
197
198
199
200
201
202
                          const filteredData = this.leftData.filter((data, index) => {
                              if (data.key === targetKey) {
                                  this.leftData.splice(index, 1);
                                  return true;
                              }
                              return false;
11748ba5   Lawrence Lee   lint
203
204
                          });
                          if (filteredData && filteredData.length > 0) this.rightData.push(filteredData[0]);
77f7bb95   梁灏   add Transfer comp...
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
230
231
                      });
                  }
                  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...
232
233
234
235
236
                  this.dispatch('FormItem', 'on-form-change', {
                      tarketKeys: newTargetKeys,
                      direction: direction,
                      moveKeys: moveKeys
                  });
5b19b5f5   梁灏   support Transfer
237
238
239
240
241
242
              },
              handleLeftCheckedKeysChange (keys) {
                  this.leftCheckedKeys = keys;
              },
              handleRightCheckedKeysChange (keys) {
                  this.rightCheckedKeys = keys;
77f7bb95   梁灏   add Transfer comp...
243
244
245
246
247
              }
          },
          watch: {
              targetKeys () {
                  this.splitData(false);
8fbc8c71   梁灏   fixed #555
248
249
250
              },
              data () {
                  this.splitData(false);
77f7bb95   梁灏   add Transfer comp...
251
252
253
254
255
              }
          },
          created () {
              this.splitData(true);
          }
b0893113   jingsam   :art: add eslint
256
257
      };
  </script>