initial import
[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             }else{
133                 s.remove(r);
134             }
135         });
136     },
137
138     // private
139     onRemove : function(v, index, r){
140         this.selections.remove(r);
141     },
142
143     // private
144     onRowUpdated : function(v, index, r){
145         if(this.isSelected(r)){
146             v.onRowSelect(index);
147         }
148     },
149
150     /**
151      * Select records.
152      * @param {Array} records The records to select
153      * @param {Boolean} keepExisting (optional) True to keep existing selections
154      */
155     selectRecords : function(records, keepExisting){
156         if(!keepExisting){
157             this.clearSelections();
158         }
159         var ds = this.grid.dataSource;
160         for(var i = 0, len = records.length; i < len; i++){
161             this.selectRow(ds.indexOf(records[i]), true);
162         }
163     },
164
165     /**
166      * Gets the number of selected rows.
167      * @return {Number}
168      */
169     getCount : function(){
170         return this.selections.length;
171     },
172
173     /**
174      * Selects the first row in the grid.
175      */
176     selectFirstRow : function(){
177         this.selectRow(0);
178     },
179
180     /**
181      * Select the last row.
182      * @param {Boolean} keepExisting (optional) True to keep existing selections
183      */
184     selectLastRow : function(keepExisting){
185         this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
186     },
187
188     /**
189      * Selects the row immediately following the last selected row.
190      * @param {Boolean} keepExisting (optional) True to keep existing selections
191      */
192     selectNext : function(keepExisting){
193         if(this.last !== false && (this.last+1) < this.grid.dataSource.getCount()){
194             this.selectRow(this.last+1, keepExisting);
195             this.grid.getView().focusRow(this.last);
196         }
197     },
198
199     /**
200      * Selects the row that precedes the last selected row.
201      * @param {Boolean} keepExisting (optional) True to keep existing selections
202      */
203     selectPrevious : function(keepExisting){
204         if(this.last){
205             this.selectRow(this.last-1, keepExisting);
206             this.grid.getView().focusRow(this.last);
207         }
208     },
209
210     /**
211      * Returns the selected records
212      * @return {Array} Array of selected records
213      */
214     getSelections : function(){
215         return [].concat(this.selections.items);
216     },
217
218     /**
219      * Returns the first selected record.
220      * @return {Record}
221      */
222     getSelected : function(){
223         return this.selections.itemAt(0);
224     },
225
226
227     /**
228      * Clears all selections.
229      */
230     clearSelections : function(fast){
231         if(this.locked) return;
232         if(fast !== true){
233             var ds = this.grid.dataSource;
234             var s = this.selections;
235             s.each(function(r){
236                 this.deselectRow(ds.indexOfId(r.id));
237             }, this);
238             s.clear();
239         }else{
240             this.selections.clear();
241         }
242         this.last = false;
243     },
244
245
246     /**
247      * Selects all rows.
248      */
249     selectAll : function(){
250         if(this.locked) return;
251         this.selections.clear();
252         for(var i = 0, len = this.grid.dataSource.getCount(); i < len; i++){
253             this.selectRow(i, true);
254         }
255     },
256
257     /**
258      * Returns True if there is a selection.
259      * @return {Boolean}
260      */
261     hasSelection : function(){
262         return this.selections.length > 0;
263     },
264
265     /**
266      * Returns True if the specified row is selected.
267      * @param {Number/Record} record The record or index of the record to check
268      * @return {Boolean}
269      */
270     isSelected : function(index){
271         var r = typeof index == "number" ? this.grid.dataSource.getAt(index) : index;
272         return (r && this.selections.key(r.id) ? true : false);
273     },
274
275     /**
276      * Returns True if the specified record id is selected.
277      * @param {String} id The id of record to check
278      * @return {Boolean}
279      */
280     isIdSelected : function(id){
281         return (this.selections.key(id) ? true : false);
282     },
283
284     // private
285     handleMouseDown : function(e, t){
286         var view = this.grid.getView(), rowIndex;
287         if(this.isLocked() || (rowIndex = view.findRowIndex(t)) === false){
288             return;
289         };
290         if(e.shiftKey && this.last !== false){
291             var last = this.last;
292             this.selectRange(last, rowIndex, e.ctrlKey);
293             this.last = last; // reset the last
294             view.focusRow(rowIndex);
295         }else{
296             var isSelected = this.isSelected(rowIndex);
297             if(e.button !== 0 && isSelected){
298                 view.focusRow(rowIndex);
299             }else if(e.ctrlKey && isSelected){
300                 this.deselectRow(rowIndex);
301             }else if(!isSelected){
302                 this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
303                 view.focusRow(rowIndex);
304             }
305         }
306         this.fireEvent("afterselectionchange", this);
307     },
308     // private
309     handleDragableRowClick :  function(grid, rowIndex, e) 
310     {
311         if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
312             this.selectRow(rowIndex, false);
313             grid.view.focusRow(rowIndex);
314              this.fireEvent("afterselectionchange", this);
315         }
316     },
317     
318     /**
319      * Selects multiple rows.
320      * @param {Array} rows Array of the indexes of the row to select
321      * @param {Boolean} keepExisting (optional) True to keep existing selections
322      */
323     selectRows : function(rows, keepExisting){
324         if(!keepExisting){
325             this.clearSelections();
326         }
327         for(var i = 0, len = rows.length; i < len; i++){
328             this.selectRow(rows[i], true);
329         }
330     },
331
332     /**
333      * Selects a range of rows. All rows in between startRow and endRow are also selected.
334      * @param {Number} startRow The index of the first row in the range
335      * @param {Number} endRow The index of the last row in the range
336      * @param {Boolean} keepExisting (optional) True to retain existing selections
337      */
338     selectRange : function(startRow, endRow, keepExisting){
339         if(this.locked) return;
340         if(!keepExisting){
341             this.clearSelections();
342         }
343         if(startRow <= endRow){
344             for(var i = startRow; i <= endRow; i++){
345                 this.selectRow(i, true);
346             }
347         }else{
348             for(var i = startRow; i >= endRow; i--){
349                 this.selectRow(i, true);
350             }
351         }
352     },
353
354     /**
355      * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
356      * @param {Number} startRow The index of the first row in the range
357      * @param {Number} endRow The index of the last row in the range
358      */
359     deselectRange : function(startRow, endRow, preventViewNotify){
360         if(this.locked) return;
361         for(var i = startRow; i <= endRow; i++){
362             this.deselectRow(i, preventViewNotify);
363         }
364     },
365
366     /**
367      * Selects a row.
368      * @param {Number} row The index of the row to select
369      * @param {Boolean} keepExisting (optional) True to keep existing selections
370      */
371     selectRow : function(index, keepExisting, preventViewNotify){
372         if(this.locked || (index < 0 || index >= this.grid.dataSource.getCount())) return;
373         if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
374             if(!keepExisting || this.singleSelect){
375                 this.clearSelections();
376             }
377             var r = this.grid.dataSource.getAt(index);
378             this.selections.add(r);
379             this.last = this.lastActive = index;
380             if(!preventViewNotify){
381                 this.grid.getView().onRowSelect(index);
382             }
383             this.fireEvent("rowselect", this, index, r);
384             this.fireEvent("selectionchange", this);
385         }
386     },
387
388     /**
389      * Deselects a row.
390      * @param {Number} row The index of the row to deselect
391      */
392     deselectRow : function(index, preventViewNotify){
393         if(this.locked) return;
394         if(this.last == index){
395             this.last = false;
396         }
397         if(this.lastActive == index){
398             this.lastActive = false;
399         }
400         var r = this.grid.dataSource.getAt(index);
401         this.selections.remove(r);
402         if(!preventViewNotify){
403             this.grid.getView().onRowDeselect(index);
404         }
405         this.fireEvent("rowdeselect", this, index);
406         this.fireEvent("selectionchange", this);
407     },
408
409     // private
410     restoreLast : function(){
411         if(this._last){
412             this.last = this._last;
413         }
414     },
415
416     // private
417     acceptsNav : function(row, col, cm){
418         return !cm.isHidden(col) && cm.isCellEditable(col, row);
419     },
420
421     // private
422     onEditorKey : function(field, e){
423         var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
424         if(k == e.TAB){
425             e.stopEvent();
426             ed.completeEdit();
427             if(e.shiftKey){
428                 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
429             }else{
430                 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
431             }
432         }else if(k == e.ENTER && !e.ctrlKey){
433             e.stopEvent();
434             ed.completeEdit();
435             if(e.shiftKey){
436                 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
437             }else{
438                 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
439             }
440         }else if(k == e.ESC){
441             ed.cancelEdit();
442         }
443         if(newCell){
444             g.startEditing(newCell[0], newCell[1]);
445         }
446     }
447 });