ItemSelector.js
11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Note that this control will most likely remain as an example, and not as a core Ext form
* control. However, the API will be changing in a future release and so should not yet be
* treated as a final, stable API at this time.
*/
/**
* @class Ext.ux.form.ItemSelector
* @extends Ext.form.field.Base
* A control that allows selection of between two Ext.ux.form.MultiSelect controls.
*
* @history
* 2008-06-19 bpm Original code contributed by Toby Stuart (with contributions from Robert Williams)
*
* @constructor
* Create a new ItemSelector
* @param {Object} config Configuration options
* @xtype itemselector
*/
Ext.define('Ext.ux.form.ItemSelector', {
extend: 'Ext.ux.form.MultiSelect',
alias: ['widget.itemselectorfield', 'widget.itemselector'],
alternateClassName: ['Ext.ux.ItemSelector'],
requires: ['Ext.ux.layout.component.form.ItemSelector', 'Ext.button.Button'],
hideNavIcons:false,
/**
* @cfg {Array} buttons Defines the set of buttons that should be displayed in between the ItemSelector
* fields. Defaults to <tt>['top', 'up', 'add', 'remove', 'down', 'bottom']</tt>. These names are used
* to build the button CSS class names, and to look up the button text labels in {@link #buttonsText}.
* This can be overridden with a custom Array to change which buttons are displayed or their order.
*/
buttons: ['top', 'up', 'add', 'remove', 'down', 'bottom'],
buttonsText: {
top: "Move to Top",
up: "Move Up",
add: "Add to Selected",
remove: "Remove from Selected",
down: "Move Down",
bottom: "Move to Bottom"
},
/**
* @cfg {Array} multiselects An optional array of {@link Ext.ux.form.MultiSelect} config objects, containing
* additional configuration to be applied to the internal MultiSelect fields.
*/
multiselects: [],
componentLayout: 'itemselectorfield',
fieldBodyCls: Ext.baseCSSPrefix + 'form-itemselector-body',
bindStore: function(store, initial) {
var me = this,
toField = me.toField,
fromField = me.fromField,
models;
me.callParent(arguments);
if (toField) {
// Clear both field stores
toField.store.removeAll();
fromField.store.removeAll();
// Clone the contents of the main store into the fromField
models = [];
me.store.each(function(model) {
models.push(model.copy(model.getId()));
});
fromField.store.add(models);
}
},
onRender: function(ct, position) {
var me = this,
baseCSSPrefix = Ext.baseCSSPrefix,
ddGroup = 'ItemSelectorDD-' + Ext.id(),
commonConfig = {
displayField: me.displayField,
valueField: me.valueField,
dragGroup: ddGroup,
dropGroup: ddGroup,
flex: 1,
hideLabel: true
},
fromConfig = Ext.apply({
listTitle: 'Available',
store: Ext.create('Ext.data.Store', {model: me.store.model}), //blank store to begin
listeners: {
boundList: {
itemdblclick: me.onItemDblClick,
scope: me
}
}
}, me.multiselects[0], commonConfig),
toConfig = Ext.apply({
listTitle: 'Selected',
store: Ext.create('Ext.data.Store', {model: me.store.model}), //blank store to begin
listeners: {
boundList: {
itemdblclick: me.onItemDblClick,
scope: me
},
change: me.onToFieldChange,
scope: me
}
}, me.multiselects[1], commonConfig),
fromField = Ext.widget('multiselect', fromConfig),
toField = Ext.widget('multiselect', toConfig),
innerCt,
buttons = [];
// Skip MultiSelect's onRender as we don't want its content
Ext.ux.form.MultiSelect.superclass.onRender.call(me, ct, position);
me.fromField = fromField;
me.toField = toField;
if (!me.hideNavIcons) {
Ext.Array.forEach(me.buttons, function(name) {
buttons.push({
xtype: 'button',
tooltip: me.buttonsText[name],
handler: me['on' + Ext.String.capitalize(name) + 'BtnClick'],
cls: baseCSSPrefix + 'form-itemselector-btn',
iconCls: baseCSSPrefix + 'form-itemselector-' + name,
scope: me
});
//div separator to force vertical stacking
buttons.push({xtype: 'component', height: 3, width: 1, style: 'font-size:0;line-height:0'});
});
}
innerCt = me.innerCt = Ext.widget('container', {
renderTo: me.bodyEl,
layout: {
type: 'hbox',
align: 'middle'
},
items: [
me.fromField,
{
xtype: 'container',
margins: '0 4',
items: buttons
},
me.toField
]
});
// Must set upward link after first render
innerCt.ownerCt = me;
// Rebind the store so it gets cloned to the fromField
me.bindStore(me.store);
// Set the initial value
me.setRawValue(me.rawValue);
},
onToFieldChange: function() {
this.checkChange();
},
getSelections: function(list){
var store = list.getStore(),
selections = list.getSelectionModel().getSelection(),
i = 0,
len = selections.length;
return Ext.Array.sort(selections, function(a, b){
a = store.indexOf(a);
b = store.indexOf(b);
if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
return 0;
});
},
onTopBtnClick : function() {
var list = this.toField.boundList,
store = list.getStore(),
selected = this.getSelections(list),
i = selected.length - 1,
selection;
store.suspendEvents();
for (; i > -1; --i) {
selection = selected[i];
store.remove(selected);
store.insert(0, selected);
}
store.resumeEvents();
list.refresh();
},
onBottomBtnClick : function() {
var list = this.toField.boundList,
store = list.getStore(),
selected = this.getSelections(list),
i = 0,
len = selected.length,
selection;
store.suspendEvents();
for (; i < len; ++i) {
selection = selected[i];
store.remove(selection);
store.add(selection);
}
store.resumeEvents();
list.refresh();
},
onUpBtnClick : function() {
var list = this.toField.boundList,
store = list.getStore(),
selected = this.getSelections(list),
i = 0,
len = selected.length,
selection,
index;
store.suspendEvents();
for (; i < len; ++i) {
selection = selected[i];
index = Math.max(0, store.indexOf(selection) - 1);
store.remove(selection);
store.insert(index, selection);
}
store.resumeEvents();
list.refresh();
},
onDownBtnClick : function() {
var list = this.toField.boundList,
store = list.getStore(),
selected = this.getSelections(list),
i = 0,
len = selected.length,
max = store.getCount(),
selection,
index;
store.suspendEvents();
for (; i < len; ++i) {
selection = selected[i];
index = Math.min(max, store.indexOf(selection) + 1);
store.remove(selection);
store.insert(index, selection);
}
store.resumeEvents();
list.refresh();
},
onAddBtnClick : function() {
var me = this,
fromList = me.fromField.boundList,
selected = this.getSelections(fromList);
fromList.getStore().remove(selected);
this.toField.boundList.getStore().add(selected);
},
onRemoveBtnClick : function() {
var me = this,
toList = me.toField.boundList,
selected = this.getSelections(toList);
toList.getStore().remove(selected);
this.fromField.boundList.getStore().add(selected);
},
onItemDblClick : function(view) {
var me = this;
if (view == me.toField.boundList){
me.onRemoveBtnClick();
}
else if (view == me.fromField.boundList) {
me.onAddBtnClick();
}
},
setRawValue: function(value) {
var me = this,
Array = Ext.Array,
toStore, fromStore, models;
value = Array.from(value);
me.rawValue = value;
if (me.toField) {
toStore = me.toField.boundList.getStore();
fromStore = me.fromField.boundList.getStore();
// Move any selected values back to the fromField
fromStore.add(toStore.getRange());
toStore.removeAll();
// Move the new values over to the toField
models = [];
Ext.Array.forEach(value, function(val) {
var undef,
model = fromStore.findRecord(me.valueField, val, undef, undef, true, true);
if (model) {
models.push(model);
}
});
fromStore.remove(models);
toStore.add(models);
}
return value;
},
getRawValue: function() {
var me = this,
toField = me.toField,
rawValue = me.rawValue;
if (toField) {
rawValue = Ext.Array.map(toField.boundList.getStore().getRange(), function(model) {
return model.get(me.valueField);
});
}
me.rawValue = rawValue;
return rawValue;
},
/**
* @private Cascade readOnly/disabled state to the sub-fields and buttons
*/
updateReadOnly: function() {
var me = this,
readOnly = me.readOnly || me.disabled;
if (me.rendered) {
me.toField.setReadOnly(readOnly);
me.fromField.setReadOnly(readOnly);
Ext.Array.forEach(me.innerCt.query('button'), function(button) {
button.setDisabled(readOnly);
});
}
},
onDestroy: function() {
Ext.destroyMembers(this, 'innerCt');
this.callParent();
}
});