3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*
\r
9 * Note that this control will most likely remain as an example, and not as a core Ext form
\r
10 * control. However, the API will be changing in a future release and so should not yet be
\r
11 * treated as a final, stable API at this time.
\r
14 <div id="cls-Ext.ux.form.ItemSelector"></div>/**
\r
15 * @class Ext.ux.form.ItemSelector
\r
16 * @extends Ext.form.Field
\r
17 * A control that allows selection of between two Ext.ux.form.MultiSelect controls.
\r
20 * 2008-06-19 bpm Original code contributed by Toby Stuart (with contributions from Robert Williams)
\r
23 * Create a new ItemSelector
\r
24 * @param {Object} config Configuration options
\r
25 * @xtype itemselector
\r
27 Ext.ux.form.ItemSelector = Ext.extend(Ext.form.Field, {
\r
31 iconDown:"down2.gif",
\r
32 iconLeft:"left2.gif",
\r
33 iconRight:"right2.gif",
\r
35 iconBottom:"bottom2.gif",
\r
45 defaultAutoCreate:{tag: "div"},
\r
46 <div id="cfg-Ext.ux.form.ItemSelector-multiselects"></div>/**
\r
47 * @cfg {Array} multiselects An array of {@link Ext.ux.form.MultiSelect} config objects, with at least all required parameters (e.g., store)
\r
51 initComponent: function(){
\r
52 Ext.ux.form.ItemSelector.superclass.initComponent.call(this);
\r
54 'rowdblclick' : true,
\r
59 onRender: function(ct, position){
\r
60 Ext.ux.form.ItemSelector.superclass.onRender.call(this, ct, position);
\r
62 // Internal default configuration for both multiselects
\r
64 legend: 'Available',
\r
77 this.fromMultiselect = new Ext.ux.form.MultiSelect(Ext.applyIf(this.multiselects[0], msConfig[0]));
\r
78 this.fromMultiselect.on('dblclick', this.onRowDblClick, this);
\r
80 this.toMultiselect = new Ext.ux.form.MultiSelect(Ext.applyIf(this.multiselects[1], msConfig[1]));
\r
81 this.toMultiselect.on('dblclick', this.onRowDblClick, this);
\r
83 var p = new Ext.Panel({
\r
84 bodyStyle:this.bodyStyle,
\r
87 layoutConfig:{columns:3}
\r
90 p.add(this.fromMultiselect);
\r
91 var icons = new Ext.Panel({header:false});
\r
93 p.add(this.toMultiselect);
\r
95 icons.el.down('.'+icons.bwrapCls).remove();
\r
98 if (this.imagePath!="" && this.imagePath.charAt(this.imagePath.length-1)!="/")
\r
99 this.imagePath+="/";
\r
100 this.iconUp = this.imagePath + (this.iconUp || 'up2.gif');
\r
101 this.iconDown = this.imagePath + (this.iconDown || 'down2.gif');
\r
102 this.iconLeft = this.imagePath + (this.iconLeft || 'left2.gif');
\r
103 this.iconRight = this.imagePath + (this.iconRight || 'right2.gif');
\r
104 this.iconTop = this.imagePath + (this.iconTop || 'top2.gif');
\r
105 this.iconBottom = this.imagePath + (this.iconBottom || 'bottom2.gif');
\r
106 var el=icons.getEl();
\r
107 this.toTopIcon = el.createChild({tag:'img', src:this.iconTop, style:{cursor:'pointer', margin:'2px'}});
\r
108 el.createChild({tag: 'br'});
\r
109 this.upIcon = el.createChild({tag:'img', src:this.iconUp, style:{cursor:'pointer', margin:'2px'}});
\r
110 el.createChild({tag: 'br'});
\r
111 this.addIcon = el.createChild({tag:'img', src:this.iconRight, style:{cursor:'pointer', margin:'2px'}});
\r
112 el.createChild({tag: 'br'});
\r
113 this.removeIcon = el.createChild({tag:'img', src:this.iconLeft, style:{cursor:'pointer', margin:'2px'}});
\r
114 el.createChild({tag: 'br'});
\r
115 this.downIcon = el.createChild({tag:'img', src:this.iconDown, style:{cursor:'pointer', margin:'2px'}});
\r
116 el.createChild({tag: 'br'});
\r
117 this.toBottomIcon = el.createChild({tag:'img', src:this.iconBottom, style:{cursor:'pointer', margin:'2px'}});
\r
118 this.toTopIcon.on('click', this.toTop, this);
\r
119 this.upIcon.on('click', this.up, this);
\r
120 this.downIcon.on('click', this.down, this);
\r
121 this.toBottomIcon.on('click', this.toBottom, this);
\r
122 this.addIcon.on('click', this.fromTo, this);
\r
123 this.removeIcon.on('click', this.toFrom, this);
\r
124 if (!this.drawUpIcon || this.hideNavIcons) { this.upIcon.dom.style.display='none'; }
\r
125 if (!this.drawDownIcon || this.hideNavIcons) { this.downIcon.dom.style.display='none'; }
\r
126 if (!this.drawLeftIcon || this.hideNavIcons) { this.addIcon.dom.style.display='none'; }
\r
127 if (!this.drawRightIcon || this.hideNavIcons) { this.removeIcon.dom.style.display='none'; }
\r
128 if (!this.drawTopIcon || this.hideNavIcons) { this.toTopIcon.dom.style.display='none'; }
\r
129 if (!this.drawBotIcon || this.hideNavIcons) { this.toBottomIcon.dom.style.display='none'; }
\r
131 var tb = p.body.first();
\r
132 this.el.setWidth(p.body.first().getWidth());
\r
133 p.body.removeClass();
\r
135 this.hiddenName = this.name;
\r
136 var hiddenTag = {tag: "input", type: "hidden", value: "", name: this.name};
\r
137 this.hiddenField = this.el.createChild(hiddenTag);
\r
140 doLayout: function(){
\r
142 this.fromMultiselect.fs.doLayout();
\r
143 this.toMultiselect.fs.doLayout();
\r
147 afterRender: function(){
\r
148 Ext.ux.form.ItemSelector.superclass.afterRender.call(this);
\r
150 this.toStore = this.toMultiselect.store;
\r
151 this.toStore.on('add', this.valueChanged, this);
\r
152 this.toStore.on('remove', this.valueChanged, this);
\r
153 this.toStore.on('load', this.valueChanged, this);
\r
154 this.valueChanged(this.toStore);
\r
157 toTop : function() {
\r
158 var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
\r
160 if (selectionsArray.length > 0) {
\r
161 selectionsArray.sort();
\r
162 for (var i=0; i<selectionsArray.length; i++) {
\r
163 record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
\r
164 records.push(record);
\r
166 selectionsArray = [];
\r
167 for (var i=records.length-1; i>-1; i--) {
\r
168 record = records[i];
\r
169 this.toMultiselect.view.store.remove(record);
\r
170 this.toMultiselect.view.store.insert(0, record);
\r
171 selectionsArray.push(((records.length - 1) - i));
\r
174 this.toMultiselect.view.refresh();
\r
175 this.toMultiselect.view.select(selectionsArray);
\r
178 toBottom : function() {
\r
179 var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
\r
181 if (selectionsArray.length > 0) {
\r
182 selectionsArray.sort();
\r
183 for (var i=0; i<selectionsArray.length; i++) {
\r
184 record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
\r
185 records.push(record);
\r
187 selectionsArray = [];
\r
188 for (var i=0; i<records.length; i++) {
\r
189 record = records[i];
\r
190 this.toMultiselect.view.store.remove(record);
\r
191 this.toMultiselect.view.store.add(record);
\r
192 selectionsArray.push((this.toMultiselect.view.store.getCount()) - (records.length - i));
\r
195 this.toMultiselect.view.refresh();
\r
196 this.toMultiselect.view.select(selectionsArray);
\r
201 var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
\r
202 selectionsArray.sort();
\r
203 var newSelectionsArray = [];
\r
204 if (selectionsArray.length > 0) {
\r
205 for (var i=0; i<selectionsArray.length; i++) {
\r
206 record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
\r
207 if ((selectionsArray[i] - 1) >= 0) {
\r
208 this.toMultiselect.view.store.remove(record);
\r
209 this.toMultiselect.view.store.insert(selectionsArray[i] - 1, record);
\r
210 newSelectionsArray.push(selectionsArray[i] - 1);
\r
213 this.toMultiselect.view.refresh();
\r
214 this.toMultiselect.view.select(newSelectionsArray);
\r
218 down : function() {
\r
220 var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
\r
221 selectionsArray.sort();
\r
222 selectionsArray.reverse();
\r
223 var newSelectionsArray = [];
\r
224 if (selectionsArray.length > 0) {
\r
225 for (var i=0; i<selectionsArray.length; i++) {
\r
226 record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
\r
227 if ((selectionsArray[i] + 1) < this.toMultiselect.view.store.getCount()) {
\r
228 this.toMultiselect.view.store.remove(record);
\r
229 this.toMultiselect.view.store.insert(selectionsArray[i] + 1, record);
\r
230 newSelectionsArray.push(selectionsArray[i] + 1);
\r
233 this.toMultiselect.view.refresh();
\r
234 this.toMultiselect.view.select(newSelectionsArray);
\r
238 fromTo : function() {
\r
239 var selectionsArray = this.fromMultiselect.view.getSelectedIndexes();
\r
241 if (selectionsArray.length > 0) {
\r
242 for (var i=0; i<selectionsArray.length; i++) {
\r
243 record = this.fromMultiselect.view.store.getAt(selectionsArray[i]);
\r
244 records.push(record);
\r
246 if(!this.allowDup)selectionsArray = [];
\r
247 for (var i=0; i<records.length; i++) {
\r
248 record = records[i];
\r
250 var x=new Ext.data.Record();
\r
253 this.toMultiselect.view.store.add(record);
\r
255 this.fromMultiselect.view.store.remove(record);
\r
256 this.toMultiselect.view.store.add(record);
\r
257 selectionsArray.push((this.toMultiselect.view.store.getCount() - 1));
\r
261 this.toMultiselect.view.refresh();
\r
262 this.fromMultiselect.view.refresh();
\r
263 var si = this.toMultiselect.store.sortInfo;
\r
265 this.toMultiselect.store.sort(si.field, si.direction);
\r
267 this.toMultiselect.view.select(selectionsArray);
\r
270 toFrom : function() {
\r
271 var selectionsArray = this.toMultiselect.view.getSelectedIndexes();
\r
273 if (selectionsArray.length > 0) {
\r
274 for (var i=0; i<selectionsArray.length; i++) {
\r
275 record = this.toMultiselect.view.store.getAt(selectionsArray[i]);
\r
276 records.push(record);
\r
278 selectionsArray = [];
\r
279 for (var i=0; i<records.length; i++) {
\r
280 record = records[i];
\r
281 this.toMultiselect.view.store.remove(record);
\r
282 if(!this.allowDup){
\r
283 this.fromMultiselect.view.store.add(record);
\r
284 selectionsArray.push((this.fromMultiselect.view.store.getCount() - 1));
\r
288 this.fromMultiselect.view.refresh();
\r
289 this.toMultiselect.view.refresh();
\r
290 var si = this.fromMultiselect.store.sortInfo;
\r
292 this.fromMultiselect.store.sort(si.field, si.direction);
\r
294 this.fromMultiselect.view.select(selectionsArray);
\r
297 valueChanged: function(store) {
\r
300 for (var i=0; i<store.getCount(); i++) {
\r
301 record = store.getAt(i);
\r
302 values.push(record.get(this.toMultiselect.valueField));
\r
304 this.hiddenField.dom.value = values.join(this.delimiter);
\r
305 this.fireEvent('change', this, this.getValue(), this.hiddenField.dom.value);
\r
308 getValue : function() {
\r
309 return this.hiddenField.dom.value;
\r
312 onRowDblClick : function(vw, index, node, e) {
\r
313 if (vw == this.toMultiselect.view){
\r
315 } else if (vw == this.fromMultiselect.view) {
\r
318 return this.fireEvent('rowdblclick', vw, index, node, e);
\r
322 range = this.toMultiselect.store.getRange();
\r
323 this.toMultiselect.store.removeAll();
\r
324 this.fromMultiselect.store.add(range);
\r
325 var si = this.fromMultiselect.store.sortInfo;
\r
327 this.fromMultiselect.store.sort(si.field, si.direction);
\r
329 this.valueChanged(this.toMultiselect.store);
\r
333 Ext.reg('itemselector', Ext.ux.form.ItemSelector);
\r
336 Ext.ux.ItemSelector = Ext.ux.form.ItemSelector;
\r