Blame view

src/components/transfer/transfer.vue 10 KB
16a87cde   jingsam   fix transfer
1
  <!-- <template>
77f7bb95   梁灏   add Transfer comp...
2
3
      <div :class="classes">
          <List
5b19b5f5   梁灏   support Transfer
4
              ref="left"
77f7bb95   梁灏   add Transfer comp...
5
6
7
              :prefix-cls="prefixCls + '-list'"
              :data="leftData"
              :render-format="renderFormat"
5b19b5f5   梁灏   support Transfer
8
9
10
              :checked-keys="leftCheckedKeys"
              @on-checked-keys-change="handleLeftCheckedKeysChange"
              :valid-keys-count="leftValidKeysCount"
77f7bb95   梁灏   add Transfer comp...
11
12
13
14
15
16
17
              :style="listStyle"
              :title="titles[0]"
              :filterable="filterable"
              :filter-placeholder="filterPlaceholder"
              :filter-method="filterMethod"
              :not-found-text="notFoundText">
              <slot></slot>
16a87cde   jingsam   fix transfer
18
19
          </List>
          <Operation
77f7bb95   梁灏   add Transfer comp...
20
21
22
              :prefix-cls="prefixCls"
              :operations="operations"
              :left-active="leftValidKeysCount > 0"
16a87cde   jingsam   fix transfer
23
24
25
              :right-active="rightValidKeysCount > 0">
          </Operation>
          <List
5b19b5f5   梁灏   support Transfer
26
              ref="right"
77f7bb95   梁灏   add Transfer comp...
27
28
29
              :prefix-cls="prefixCls + '-list'"
              :data="rightData"
              :render-format="renderFormat"
5b19b5f5   梁灏   support Transfer
30
31
32
              :checked-keys="rightCheckedKeys"
              @on-checked-keys-change="handleRightCheckedKeysChange"
              :valid-keys-count="rightValidKeysCount"
77f7bb95   梁灏   add Transfer comp...
33
34
35
36
37
38
              :style="listStyle"
              :title="titles[1]"
              :filterable="filterable"
              :filter-placeholder="filterPlaceholder"
              :filter-method="filterMethod"
              :not-found-text="notFoundText">
16a87cde   jingsam   fix transfer
39
              <slot></slot>
77f7bb95   梁灏   add Transfer comp...
40
41
          </List>
      </div>
16a87cde   jingsam   fix transfer
42
  </template> -->
77f7bb95   梁灏   add Transfer comp...
43
44
45
  <script>
      import List from './list.vue';
      import Operation from './operation.vue';
012cbf28   梁灏   update locale
46
      import { t } from '../../locale';
cd78c9c4   梁灏   some comps suppor...
47
      import Emitter from '../../mixins/emitter';
77f7bb95   梁灏   add Transfer comp...
48
49
50
51
  
      const prefixCls = 'ivu-transfer';
  
      export default {
cd78c9c4   梁灏   some comps suppor...
52
          mixins: [ Emitter ],
16a87cde   jingsam   fix transfer
53
54
55
          render (createElement) {
  
              function cloneVNode (vnode) {
92a0f30c   jingsam   fixup
56
                  const clonedChildren = vnode.children && vnode.children.map(vnode => cloneVNode(vnode));
16a87cde   jingsam   fix transfer
57
58
59
60
61
62
63
64
65
66
67
68
69
                  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...
70
71
              const vNodes = this.$slots.default === undefined ? [] : this.$slots.default;
              const clonedVNodes = this.$slots.default === undefined ? [] : vNodes.map(vnode => cloneVNode(vnode));
16a87cde   jingsam   fix transfer
72
73
74
75
76
77
78
79
80
81
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
  
              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,
                          style: this.listStyle,
                          title: this.titles[0],
                          filterable: this.filterable,
                          filterPlaceholder: this.filterPlaceholder,
                          filterMethod: this.filterMethod,
                          notFoundText: this.notFoundText
                      },
                      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,
                          style: this.listStyle,
                          title: this.titles[1],
                          filterable: this.filterable,
                          filterPlaceholder: this.filterPlaceholder,
                          filterMethod: this.filterMethod,
                          notFoundText: this.notFoundText
                      },
                      on: {
                          'on-checked-keys-change': this.handleRightCheckedKeysChange
                      }
                  }, clonedVNodes),
              ]);
          },
  
77f7bb95   梁灏   add Transfer comp...
127
128
129
130
131
          components: { List, Operation },
          props: {
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
132
                      return [];
77f7bb95   梁灏   add Transfer comp...
133
134
135
136
137
138
139
140
141
142
143
                  }
              },
              renderFormat: {
                  type: Function,
                  default (item) {
                      return item.label || item.key;
                  }
              },
              targetKeys: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
144
                      return [];
77f7bb95   梁灏   add Transfer comp...
145
146
147
148
149
                  }
              },
              selectedKeys: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
150
                      return [];
77f7bb95   梁灏   add Transfer comp...
151
152
153
154
155
                  }
              },
              listStyle: {
                  type: Object,
                  default () {
b0893113   jingsam   :art: add eslint
156
                      return {};
77f7bb95   梁灏   add Transfer comp...
157
158
159
160
161
                  }
              },
              titles: {
                  type: Array,
                  default () {
012cbf28   梁灏   update locale
162
                      return [t('i.transfer.titles.source'), t('i.transfer.titles.target')];
77f7bb95   梁灏   add Transfer comp...
163
164
165
166
167
                  }
              },
              operations: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
168
                      return [];
77f7bb95   梁灏   add Transfer comp...
169
170
171
172
173
174
175
176
                  }
              },
              filterable: {
                  type: Boolean,
                  default: false
              },
              filterPlaceholder: {
                  type: String,
012cbf28   梁灏   update locale
177
178
179
                  default () {
                      return t('i.transfer.filterPlaceholder');
                  }
77f7bb95   梁灏   add Transfer comp...
180
181
182
183
184
185
186
187
188
189
              },
              filterMethod: {
                  type: Function,
                  default (data, query) {
                      const type = ('label' in data) ? 'label' : 'key';
                      return data[type].indexOf(query) > -1;
                  }
              },
              notFoundText: {
                  type: String,
012cbf28   梁灏   update locale
190
191
192
                  default () {
                      return t('i.transfer.notFoundText');
                  }
77f7bb95   梁灏   add Transfer comp...
193
194
195
196
197
198
199
200
201
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  leftData: [],
                  rightData: [],
                  leftCheckedKeys: [],
                  rightCheckedKeys: []
b0893113   jingsam   :art: add eslint
202
              };
77f7bb95   梁灏   add Transfer comp...
203
204
205
206
207
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`
b0893113   jingsam   :art: add eslint
208
                  ];
77f7bb95   梁灏   add Transfer comp...
209
210
211
212
213
214
215
216
217
218
219
220
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
              },
              leftValidKeysCount () {
                  return this.getValidKeys('left').length;
              },
              rightValidKeysCount () {
                  return this.getValidKeys('right').length;
              }
          },
          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) => {
                          this.rightData.push(
                                  this.leftData.filter((data, index) => {
                                      if (data.key === targetKey) {
                                          this.leftData.splice(index, 1);
                                          return true;
                                      }
                                      return false;
                                  })[0]);
                      });
                  }
                  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...
261
262
263
264
265
                  this.dispatch('FormItem', 'on-form-change', {
                      tarketKeys: newTargetKeys,
                      direction: direction,
                      moveKeys: moveKeys
                  });
5b19b5f5   梁灏   support Transfer
266
267
268
269
270
271
              },
              handleLeftCheckedKeysChange (keys) {
                  this.leftCheckedKeys = keys;
              },
              handleRightCheckedKeysChange (keys) {
                  this.rightCheckedKeys = keys;
77f7bb95   梁灏   add Transfer comp...
272
273
274
275
276
277
278
279
280
281
              }
          },
          watch: {
              targetKeys () {
                  this.splitData(false);
              }
          },
          created () {
              this.splitData(true);
          }
b0893113   jingsam   :art: add eslint
282
283
      };
  </script>