Roo/grid/RowSelectionModel.js
[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) return;
233         if(fast !== true){
234             var ds = this.grid.dataSource;
235             var s = this.selections;
236             s.each(function(r){
237                 this.deselectRow(ds.indexOfId(r.id));
238             }, this);
239             s.clear();
240         }else{
241             this.selections.clear();
242         }
243         this.last = false;
244     },
245
246
247     /**
248      * Selects all rows.
249      */
250     selectAll : function(){
251         if(this.locked) return;
252         this.selections.clear();
253         for(var i = 0, len = this.grid.dataSource.getCount(); i < len; i++){
254             this.selectRow(i, true);
255         }
256     },
257
258     /**
259      * Returns True if there is a selection.
260      * @return {Boolean}
261      */
262     hasSelection : function(){
263         return this.selections.length > 0;
264     },
265
266     /**
267      * Returns True if the specified row is selected.
268      * @param {Number/Record} record The record or index of the record to check
269      * @return {Boolean}
270      */
271     isSelected : function(index){
272         var r = typeof index == "number" ? this.grid.dataSource.getAt(index) : index;
273         return (r && this.selections.key(r.id) ? true : false);
274     },
275
276     /**
277      * Returns True if the specified record id is selected.
278      * @param {String} id The id of record to check
279      * @return {Boolean}
280      */
281     isIdSelected : function(id){
282         return (this.selections.key(id) ? true : false);
283     },
284
285     // private
286     handleMouseDown : function(e, t){
287         var view = this.grid.getView(), rowIndex;
288         if(this.isLocked() || (rowIndex = view.findRowIndex(t)) === false){
289             return;
290         };
291         if(e.shiftKey && this.last !== false){
292             var last = this.last;
293             this.selectRange(last, rowIndex, e.ctrlKey);
294             this.last = last; // reset the last
295             view.focusRow(rowIndex);
296         }else{
297             var isSelected = this.isSelected(rowIndex);
298             if(e.button !== 0 && isSelected){
299                 view.focusRow(rowIndex);
300             }else if(e.ctrlKey && isSelected){
301                 this.deselectRow(rowIndex);
302             }else if(!isSelected){
303                 this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
304                 view.focusRow(rowIndex);
305             }
306         }
307         this.fireEvent("afterselectionchange", this);
308     },
309     // private
310     handleDragableRowClick :  function(grid, rowIndex, e) 
311     {
312         if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
313             this.selectRow(rowIndex, false);
314             grid.view.focusRow(rowIndex);
315              this.fireEvent("afterselectionchange", this);
316         }
317     },
318     
319     /**
320      * Selects multiple rows.
321      * @param {Array} rows Array of the indexes of the row to select
322      * @param {Boolean} keepExisting (optional) True to keep existing selections
323      */
324     selectRows : function(rows, keepExisting){
325         if(!keepExisting){
326             this.clearSelections();
327         }
328         for(var i = 0, len = rows.length; i < len; i++){
329             this.selectRow(rows[i], true);
330         }
331     },
332
333     /**
334      * Selects a range of rows. All rows in between startRow and endRow are also selected.
335      * @param {Number} startRow The index of the first row in the range
336      * @param {Number} endRow The index of the last row in the range
337      * @param {Boolean} keepExisting (optional) True to retain existing selections
338      */
339     selectRange : function(startRow, endRow, keepExisting){
340         if(this.locked) return;
341         if(!keepExisting){
342             this.clearSelections();
343         }
344         if(startRow <= endRow){
345             for(var i = startRow; i <= endRow; i++){
346                 this.selectRow(i, true);
347             }
348         }else{
349             for(var i = startRow; i >= endRow; i--){
350                 this.selectRow(i, true);
351             }
352         }
353     },
354
355     /**
356      * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
357      * @param {Number} startRow The index of the first row in the range
358      * @param {Number} endRow The index of the last row in the range
359      */
360     deselectRange : function(startRow, endRow, preventViewNotify){
361         if(this.locked) return;
362         for(var i = startRow; i <= endRow; i++){
363             this.deselectRow(i, preventViewNotify);
364         }
365     },
366
367     /**
368      * Selects a row.
369      * @param {Number} row The index of the row to select
370      * @param {Boolean} keepExisting (optional) True to keep existing selections
371      */
372     selectRow : function(index, keepExisting, preventViewNotify){
373         if(this.locked || (index < 0 || index >= this.grid.dataSource.getCount())) return;
374         if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
375             if(!keepExisting || this.singleSelect){
376                 this.clearSelections();
377             }
378             var r = this.grid.dataSource.getAt(index);
379             this.selections.add(r);
380             this.last = this.lastActive = index;
381             if(!preventViewNotify){
382                 this.grid.getView().onRowSelect(index);
383             }
384             this.fireEvent("rowselect", this, index, r);
385             this.fireEvent("selectionchange", this);
386         }
387     },
388
389     /**
390      * Deselects a row.
391      * @param {Number} row The index of the row to deselect
392      */
393     deselectRow : function(index, preventViewNotify){
394         if(this.locked) return;
395         if(this.last == index){
396             this.last = false;
397         }
398         if(this.lastActive == index){
399             this.lastActive = false;
400         }
401         var r = this.grid.dataSource.getAt(index);
402         this.selections.remove(r);
403         if(!preventViewNotify){
404             this.grid.getView().onRowDeselect(index);
405         }
406         this.fireEvent("rowdeselect", this, index);
407         this.fireEvent("selectionchange", this);
408     },
409
410     // private
411     restoreLast : function(){
412         if(this._last){
413             this.last = this._last;
414         }
415     },
416
417     // private
418     acceptsNav : function(row, col, cm){
419         return !cm.isHidden(col) && cm.isCellEditable(col, row);
420     },
421
422     // private
423     onEditorKey : function(field, e){
424         var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
425         if(k == e.TAB){
426             e.stopEvent();
427             ed.completeEdit();
428             if(e.shiftKey){
429                 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
430             }else{
431                 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
432             }
433         }else if(k == e.ENTER && !e.ctrlKey){
434             e.stopEvent();
435             ed.completeEdit();
436             if(e.shiftKey){
437                 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
438             }else{
439                 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
440             }
441         }else if(k == e.ESC){
442             ed.cancelEdit();
443         }
444         if(newCell){
445             g.startEditing(newCell[0], newCell[1]);
446         }
447     }
448 });