4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
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.
17 * @param {Object} config
19 Roo.grid.RowSelectionModel = function(config){
20 Roo.apply(this, config);
21 this.selections = new Roo.util.MixedCollection(false, function(o){
26 this.lastActive = false;
30 * @event selectionchange
31 * Fires when the selection changes
32 * @param {SelectionModel} this
34 "selectionchange" : true,
36 * @event afterselectionchange
37 * Fires after the selection changes (eg. by key press or clicking)
38 * @param {SelectionModel} this
40 "afterselectionchange" : true,
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
48 "beforerowselect" : true,
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
59 * Fires when a row is deselected.
60 * @param {SelectionModel} this
61 * @param {Number} rowIndex The selected index
65 Roo.grid.RowSelectionModel.superclass.constructor.call(this);
69 Roo.extend(Roo.grid.RowSelectionModel, Roo.grid.AbstractSelectionModel, {
71 * @cfg {Boolean} singleSelect
72 * True to allow selection of only one row at a time (defaults to false)
77 initEvents : function(){
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);
85 this.rowNav = new Roo.KeyNav(this.grid.getGridEl(), {
88 this.selectPrevious(e.shiftKey);
89 }else if(this.last !== false && this.lastActive !== false){
91 this.selectRange(this.last, this.lastActive-1);
92 this.grid.getView().focusRow(this.lastActive);
97 this.selectFirstRow();
99 this.fireEvent("afterselectionchange", this);
101 "down" : function(e){
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);
112 this.selectFirstRow();
114 this.fireEvent("afterselectionchange", this);
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);
126 onRefresh : function(){
127 var ds = this.grid.dataSource, i, v = this.grid.view;
128 var s = this.selections;
130 if((i = ds.indexOfId(r.id)) != -1){
139 onRemove : function(v, index, r){
140 this.selections.remove(r);
144 onRowUpdated : function(v, index, r){
145 if(this.isSelected(r)){
146 v.onRowSelect(index);
152 * @param {Array} records The records to select
153 * @param {Boolean} keepExisting (optional) True to keep existing selections
155 selectRecords : function(records, keepExisting){
157 this.clearSelections();
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);
166 * Gets the number of selected rows.
169 getCount : function(){
170 return this.selections.length;
174 * Selects the first row in the grid.
176 selectFirstRow : function(){
181 * Select the last row.
182 * @param {Boolean} keepExisting (optional) True to keep existing selections
184 selectLastRow : function(keepExisting){
185 this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
189 * Selects the row immediately following the last selected row.
190 * @param {Boolean} keepExisting (optional) True to keep existing selections
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);
200 * Selects the row that precedes the last selected row.
201 * @param {Boolean} keepExisting (optional) True to keep existing selections
203 selectPrevious : function(keepExisting){
205 this.selectRow(this.last-1, keepExisting);
206 this.grid.getView().focusRow(this.last);
211 * Returns the selected records
212 * @return {Array} Array of selected records
214 getSelections : function(){
215 return [].concat(this.selections.items);
219 * Returns the first selected record.
222 getSelected : function(){
223 return this.selections.itemAt(0);
228 * Clears all selections.
230 clearSelections : function(fast){
231 if(this.locked) return;
233 var ds = this.grid.dataSource;
234 var s = this.selections;
236 this.deselectRow(ds.indexOfId(r.id));
240 this.selections.clear();
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);
258 * Returns True if there is a selection.
261 hasSelection : function(){
262 return this.selections.length > 0;
266 * Returns True if the specified row is selected.
267 * @param {Number/Record} record The record or index of the record to check
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);
276 * Returns True if the specified record id is selected.
277 * @param {String} id The id of record to check
280 isIdSelected : function(id){
281 return (this.selections.key(id) ? true : false);
285 handleMouseDown : function(e, t){
286 var view = this.grid.getView(), rowIndex;
287 if(this.isLocked() || (rowIndex = view.findRowIndex(t)) === false){
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);
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);
306 this.fireEvent("afterselectionchange", this);
309 handleDragableRowClick : function(grid, rowIndex, e)
311 if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
312 this.selectRow(rowIndex, false);
313 grid.view.focusRow(rowIndex);
314 this.fireEvent("afterselectionchange", this);
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
323 selectRows : function(rows, keepExisting){
325 this.clearSelections();
327 for(var i = 0, len = rows.length; i < len; i++){
328 this.selectRow(rows[i], true);
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
338 selectRange : function(startRow, endRow, keepExisting){
339 if(this.locked) return;
341 this.clearSelections();
343 if(startRow <= endRow){
344 for(var i = startRow; i <= endRow; i++){
345 this.selectRow(i, true);
348 for(var i = startRow; i >= endRow; i--){
349 this.selectRow(i, true);
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
359 deselectRange : function(startRow, endRow, preventViewNotify){
360 if(this.locked) return;
361 for(var i = startRow; i <= endRow; i++){
362 this.deselectRow(i, preventViewNotify);
368 * @param {Number} row The index of the row to select
369 * @param {Boolean} keepExisting (optional) True to keep existing selections
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();
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);
383 this.fireEvent("rowselect", this, index, r);
384 this.fireEvent("selectionchange", this);
390 * @param {Number} row The index of the row to deselect
392 deselectRow : function(index, preventViewNotify){
393 if(this.locked) return;
394 if(this.last == index){
397 if(this.lastActive == index){
398 this.lastActive = false;
400 var r = this.grid.dataSource.getAt(index);
401 this.selections.remove(r);
402 if(!preventViewNotify){
403 this.grid.getView().onRowDeselect(index);
405 this.fireEvent("rowdeselect", this, index);
406 this.fireEvent("selectionchange", this);
410 restoreLast : function(){
412 this.last = this._last;
417 acceptsNav : function(row, col, cm){
418 return !cm.isHidden(col) && cm.isCellEditable(col, row);
422 onEditorKey : function(field, e){
423 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
428 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
430 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
432 }else if(k == e.ENTER && !e.ctrlKey){
436 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
438 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
440 }else if(k == e.ESC){
444 g.startEditing(newCell[0], newCell[1]);