allow string based values for comboboxarray
[roojs1] / Roo / grid / RowSelectionModel.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11 /**
12  * @extends Roo.grid.AbstractSelectionModel
13  * @class Roo.grid.RowSelectionModel
14  * The default SelectionModel used by {@link Roo.grid.Grid}.
15  * It supports multiple selections and keyboard selection/navigation. 
16  * @constructor
17  * @param {Object} config
18  */
19 Roo.grid.RowSelectionModel = function(config){
20     Roo.apply(this, config);
21     this.selections = new Roo.util.MixedCollection(false, function(o){
22         return o.id;
23     });
24
25     this.last = false;
26     this.lastActive = false;
27
28     this.addEvents({
29         /**
30              * @event selectionchange
31              * Fires when the selection changes
32              * @param {SelectionModel} this
33              */
34             "selectionchange" : true,
35         /**
36              * @event afterselectionchange
37              * Fires after the selection changes (eg. by key press or clicking)
38              * @param {SelectionModel} this
39              */
40             "afterselectionchange" : true,
41         /**
42              * @event beforerowselect
43              * Fires when a row is selected being selected, return false to cancel.
44              * @param {SelectionModel} this
45              * @param {Number} rowIndex The selected index
46              * @param {Boolean} keepExisting False if other selections will be cleared
47              */
48             "beforerowselect" : true,
49         /**
50              * @event rowselect
51              * Fires when a row is selected.
52              * @param {SelectionModel} this
53              * @param {Number} rowIndex The selected index
54              * @param {Roo.data.Record} r The record
55              */
56             "rowselect" : true,
57         /**
58              * @event rowdeselect
59              * Fires when a row is deselected.
60              * @param {SelectionModel} this
61              * @param {Number} rowIndex The selected index
62              */
63         "rowdeselect" : true
64     });
65     Roo.grid.RowSelectionModel.superclass.constructor.call(this);
66     this.locked = false;
67 };
68
69 Roo.extend(Roo.grid.RowSelectionModel, Roo.grid.AbstractSelectionModel,  {
70     /**
71      * @cfg {Boolean} singleSelect
72      * True to allow selection of only one row at a time (defaults to false)
73      */
74     singleSelect : false,
75
76     // private
77     initEvents : function(){
78
79         if(!this.grid.enableDragDrop && !this.grid.enableDrag){
80             this.grid.on("mousedown", this.handleMouseDown, this);
81         }else{ // allow click to work like normal
82             this.grid.on("rowclick", this.handleDragableRowClick, this);
83         }
84
85         this.rowNav = new Roo.KeyNav(this.grid.getGridEl(), {
86             "up" : function(e){
87                 if(!e.shiftKey){
88                     this.selectPrevious(e.shiftKey);
89                 }else if(this.last !== false && this.lastActive !== false){
90                     var last = this.last;
91                     this.selectRange(this.last,  this.lastActive-1);
92                     this.grid.getView().focusRow(this.lastActive);
93                     if(last !== false){
94                         this.last = last;
95                     }
96                 }else{
97                     this.selectFirstRow();
98                 }
99                 this.fireEvent("afterselectionchange", this);
100             },
101             "down" : function(e){
102                 if(!e.shiftKey){
103                     this.selectNext(e.shiftKey);
104                 }else if(this.last !== false && this.lastActive !== false){
105                     var last = this.last;
106                     this.selectRange(this.last,  this.lastActive+1);
107                     this.grid.getView().focusRow(this.lastActive);
108                     if(last !== false){
109                         this.last = last;
110                     }
111                 }else{
112                     this.selectFirstRow();
113                 }
114                 this.fireEvent("afterselectionchange", this);
115             },
116             scope: this
117         });
118
119         var view = this.grid.view;
120         view.on("refresh", this.onRefresh, this);
121         view.on("rowupdated", this.onRowUpdated, this);
122         view.on("rowremoved", this.onRemove, this);
123     },
124
125     // private
126     onRefresh : function(){
127         var ds = this.grid.dataSource, i, v = this.grid.view;
128         var s = this.selections;
129         s.each(function(r){
130             if((i = ds.indexOfId(r.id)) != -1){
131                 v.onRowSelect(i);
132                 s.add(ds.getAt(i)); // updating the selection relate data
133             }else{
134                 s.remove(r);
135             }
136         });
137     },
138
139     // private
140     onRemove : function(v, index, r){
141         this.selections.remove(r);
142     },
143
144     // private
145     onRowUpdated : function(v, index, r){
146         if(this.isSelected(r)){
147             v.onRowSelect(index);
148         }
149     },
150
151     /**
152      * Select records.
153      * @param {Array} records The records to select
154      * @param {Boolean} keepExisting (optional) True to keep existing selections
155      */
156     selectRecords : function(records, keepExisting){
157         if(!keepExisting){
158             this.clearSelections();
159         }
160         var ds = this.grid.dataSource;
161         for(var i = 0, len = records.length; i < len; i++){
162             this.selectRow(ds.indexOf(records[i]), true);
163         }
164     },
165
166     /**
167      * Gets the number of selected rows.
168      * @return {Number}
169      */
170     getCount : function(){
171         return this.selections.length;
172     },
173
174     /**
175      * Selects the first row in the grid.
176      */
177     selectFirstRow : function(){
178         this.selectRow(0);
179     },
180
181     /**
182      * Select the last row.
183      * @param {Boolean} keepExisting (optional) True to keep existing selections
184      */
185     selectLastRow : function(keepExisting){
186         this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
187     },
188
189     /**
190      * Selects the row immediately following the last selected row.
191      * @param {Boolean} keepExisting (optional) True to keep existing selections
192      */
193     selectNext : function(keepExisting){
194         if(this.last !== false && (this.last+1) < this.grid.dataSource.getCount()){
195             this.selectRow(this.last+1, keepExisting);
196             this.grid.getView().focusRow(this.last);
197         }
198     },
199
200     /**
201      * Selects the row that precedes the last selected row.
202      * @param {Boolean} keepExisting (optional) True to keep existing selections
203      */
204     selectPrevious : function(keepExisting){
205         if(this.last){
206             this.selectRow(this.last-1, keepExisting);
207             this.grid.getView().focusRow(this.last);
208         }
209     },
210
211     /**
212      * Returns the selected records
213      * @return {Array} Array of selected records
214      */
215     getSelections : function(){
216         return [].concat(this.selections.items);
217     },
218
219     /**
220      * Returns the first selected record.
221      * @return {Record}
222      */
223     getSelected : function(){
224         return this.selections.itemAt(0);
225     },
226
227
228     /**
229      * Clears all selections.
230      */
231     clearSelections : function(fast){
232         if(this.locked) {
233             return;
234         }
235         if(fast !== true){
236             var ds = this.grid.dataSource;
237             var s = this.selections;
238             s.each(function(r){
239                 this.deselectRow(ds.indexOfId(r.id));
240             }, this);
241             s.clear();
242         }else{
243             this.selections.clear();
244         }
245         this.last = false;
246     },
247
248
249     /**
250      * Selects all rows.
251      */
252     selectAll : function(){
253         if(this.locked) {
254             return;
255         }
256         this.selections.clear();
257         for(var i = 0, len = this.grid.dataSource.getCount(); i < len; i++){
258             this.selectRow(i, true);
259         }
260     },
261
262     /**
263      * Returns True if there is a selection.
264      * @return {Boolean}
265      */
266     hasSelection : function(){
267         return this.selections.length > 0;
268     },
269
270     /**
271      * Returns True if the specified row is selected.
272      * @param {Number/Record} record The record or index of the record to check
273      * @return {Boolean}
274      */
275     isSelected : function(index){
276         var r = typeof index == "number" ? this.grid.dataSource.getAt(index) : index;
277         return (r && this.selections.key(r.id) ? true : false);
278     },
279
280     /**
281      * Returns True if the specified record id is selected.
282      * @param {String} id The id of record to check
283      * @return {Boolean}
284      */
285     isIdSelected : function(id){
286         return (this.selections.key(id) ? true : false);
287     },
288
289     // private
290     handleMouseDown : function(e, t){
291         var view = this.grid.getView(), rowIndex;
292         if(this.isLocked() || (rowIndex = view.findRowIndex(t)) === false){
293             return;
294         };
295         if(e.shiftKey && this.last !== false){
296             var last = this.last;
297             this.selectRange(last, rowIndex, e.ctrlKey);
298             this.last = last; // reset the last
299             view.focusRow(rowIndex);
300         }else{
301             var isSelected = this.isSelected(rowIndex);
302             if(e.button !== 0 && isSelected){
303                 view.focusRow(rowIndex);
304             }else if(e.ctrlKey && isSelected){
305                 this.deselectRow(rowIndex);
306             }else if(!isSelected){
307                 this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
308                 view.focusRow(rowIndex);
309             }
310         }
311         this.fireEvent("afterselectionchange", this);
312     },
313     // private
314     handleDragableRowClick :  function(grid, rowIndex, e) 
315     {
316         if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
317             this.selectRow(rowIndex, false);
318             grid.view.focusRow(rowIndex);
319              this.fireEvent("afterselectionchange", this);
320         }
321     },
322     
323     /**
324      * Selects multiple rows.
325      * @param {Array} rows Array of the indexes of the row to select
326      * @param {Boolean} keepExisting (optional) True to keep existing selections
327      */
328     selectRows : function(rows, keepExisting){
329         if(!keepExisting){
330             this.clearSelections();
331         }
332         for(var i = 0, len = rows.length; i < len; i++){
333             this.selectRow(rows[i], true);
334         }
335     },
336
337     /**
338      * Selects a range of rows. All rows in between startRow and endRow are also selected.
339      * @param {Number} startRow The index of the first row in the range
340      * @param {Number} endRow The index of the last row in the range
341      * @param {Boolean} keepExisting (optional) True to retain existing selections
342      */
343     selectRange : function(startRow, endRow, keepExisting){
344         if(this.locked) {
345             return;
346         }
347         if(!keepExisting){
348             this.clearSelections();
349         }
350         if(startRow <= endRow){
351             for(var i = startRow; i <= endRow; i++){
352                 this.selectRow(i, true);
353             }
354         }else{
355             for(var i = startRow; i >= endRow; i--){
356                 this.selectRow(i, true);
357             }
358         }
359     },
360
361     /**
362      * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
363      * @param {Number} startRow The index of the first row in the range
364      * @param {Number} endRow The index of the last row in the range
365      */
366     deselectRange : function(startRow, endRow, preventViewNotify){
367         if(this.locked) {
368             return;
369         }
370         for(var i = startRow; i <= endRow; i++){
371             this.deselectRow(i, preventViewNotify);
372         }
373     },
374
375     /**
376      * Selects a row.
377      * @param {Number} row The index of the row to select
378      * @param {Boolean} keepExisting (optional) True to keep existing selections
379      */
380     selectRow : function(index, keepExisting, preventViewNotify){
381         if(this.locked || (index < 0 || index >= this.grid.dataSource.getCount())) {
382             return;
383         }
384         if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
385             if(!keepExisting || this.singleSelect){
386                 this.clearSelections();
387             }
388             var r = this.grid.dataSource.getAt(index);
389             this.selections.add(r);
390             this.last = this.lastActive = index;
391             if(!preventViewNotify){
392                 this.grid.getView().onRowSelect(index);
393             }
394             this.fireEvent("rowselect", this, index, r);
395             this.fireEvent("selectionchange", this);
396         }
397     },
398
399     /**
400      * Deselects a row.
401      * @param {Number} row The index of the row to deselect
402      */
403     deselectRow : function(index, preventViewNotify){
404         if(this.locked) {
405             return;
406         }
407         if(this.last == index){
408             this.last = false;
409         }
410         if(this.lastActive == index){
411             this.lastActive = false;
412         }
413         var r = this.grid.dataSource.getAt(index);
414         this.selections.remove(r);
415         if(!preventViewNotify){
416             this.grid.getView().onRowDeselect(index);
417         }
418         this.fireEvent("rowdeselect", this, index);
419         this.fireEvent("selectionchange", this);
420     },
421
422     // private
423     restoreLast : function(){
424         if(this._last){
425             this.last = this._last;
426         }
427     },
428
429     // private
430     acceptsNav : function(row, col, cm){
431         return !cm.isHidden(col) && cm.isCellEditable(col, row);
432     },
433
434     // private
435     onEditorKey : function(field, e){
436         var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
437         if(k == e.TAB){
438             e.stopEvent();
439             ed.completeEdit();
440             if(e.shiftKey){
441                 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
442             }else{
443                 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
444             }
445         }else if(k == e.ENTER && !e.ctrlKey){
446             e.stopEvent();
447             ed.completeEdit();
448             if(e.shiftKey){
449                 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
450             }else{
451                 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
452             }
453         }else if(k == e.ESC){
454             ed.cancelEdit();
455         }
456         if(newCell){
457             g.startEditing(newCell[0], newCell[1]);
458         }
459     }
460 });