Blame view

src/components/transfer/transfer.vue 9.81 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';
77f7bb95   梁灏   add Transfer comp...
47
48
49
50
  
      const prefixCls = 'ivu-transfer';
  
      export default {
16a87cde   jingsam   fix transfer
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
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
          render (createElement) {
  
              function cloneVNode (vnode) {
                  const clonedChildren = vnode.children && vnode.children.map(vnode => cloneVNode(vnode)) || vnode.text;
                  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;
              }
  
              const vNodes = this.$slots.default;
              const clonedVNodes = vNodes.map(vnode => cloneVNode(vnode));
  
              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...
125
126
127
128
129
          components: { List, Operation },
          props: {
              data: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
130
                      return [];
77f7bb95   梁灏   add Transfer comp...
131
132
133
134
135
136
137
138
139
140
141
                  }
              },
              renderFormat: {
                  type: Function,
                  default (item) {
                      return item.label || item.key;
                  }
              },
              targetKeys: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
142
                      return [];
77f7bb95   梁灏   add Transfer comp...
143
144
145
146
147
                  }
              },
              selectedKeys: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
148
                      return [];
77f7bb95   梁灏   add Transfer comp...
149
150
151
152
153
                  }
              },
              listStyle: {
                  type: Object,
                  default () {
b0893113   jingsam   :art: add eslint
154
                      return {};
77f7bb95   梁灏   add Transfer comp...
155
156
157
158
159
                  }
              },
              titles: {
                  type: Array,
                  default () {
012cbf28   梁灏   update locale
160
                      return [t('i.transfer.titles.source'), t('i.transfer.titles.target')];
77f7bb95   梁灏   add Transfer comp...
161
162
163
164
165
                  }
              },
              operations: {
                  type: Array,
                  default () {
b0893113   jingsam   :art: add eslint
166
                      return [];
77f7bb95   梁灏   add Transfer comp...
167
168
169
170
171
172
173
174
                  }
              },
              filterable: {
                  type: Boolean,
                  default: false
              },
              filterPlaceholder: {
                  type: String,
012cbf28   梁灏   update locale
175
176
177
                  default () {
                      return t('i.transfer.filterPlaceholder');
                  }
77f7bb95   梁灏   add Transfer comp...
178
179
180
181
182
183
184
185
186
187
              },
              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
188
189
190
                  default () {
                      return t('i.transfer.notFoundText');
                  }
77f7bb95   梁灏   add Transfer comp...
191
192
193
194
195
196
197
198
199
              }
          },
          data () {
              return {
                  prefixCls: prefixCls,
                  leftData: [],
                  rightData: [],
                  leftCheckedKeys: [],
                  rightCheckedKeys: []
b0893113   jingsam   :art: add eslint
200
              };
77f7bb95   梁灏   add Transfer comp...
201
202
203
204
205
          },
          computed: {
              classes () {
                  return [
                      `${prefixCls}`
b0893113   jingsam   :art: add eslint
206
                  ];
77f7bb95   梁灏   add Transfer comp...
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
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
              },
              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);
5b19b5f5   梁灏   support Transfer
259
260
261
262
263
264
265
266
                  // todo 事件
  //                this.$dispatch('on-form-change', newTargetKeys, direction, moveKeys);
              },
              handleLeftCheckedKeysChange (keys) {
                  this.leftCheckedKeys = keys;
              },
              handleRightCheckedKeysChange (keys) {
                  this.rightCheckedKeys = keys;
77f7bb95   梁灏   add Transfer comp...
267
268
269
270
271
272
273
274
275
276
              }
          },
          watch: {
              targetKeys () {
                  this.splitData(false);
              }
          },
          created () {
              this.splitData(true);
          }
b0893113   jingsam   :art: add eslint
277
278
      };
  </script>