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){
132 s.add(ds.getAt(i)); // updating the selection relate data
140 onRemove : function(v, index, r){
141 this.selections.remove(r);
145 onRowUpdated : function(v, index, r){
146 if(this.isSelected(r)){
147 v.onRowSelect(index);
153 * @param {Array} records The records to select
154 * @param {Boolean} keepExisting (optional) True to keep existing selections
156 selectRecords : function(records, keepExisting){
158 this.clearSelections();
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);
167 * Gets the number of selected rows.
170 getCount : function(){
171 return this.selections.length;
175 * Selects the first row in the grid.
177 selectFirstRow : function(){
182 * Select the last row.
183 * @param {Boolean} keepExisting (optional) True to keep existing selections
185 selectLastRow : function(keepExisting){
186 this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
190 * Selects the row immediately following the last selected row.
191 * @param {Boolean} keepExisting (optional) True to keep existing selections
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);
201 * Selects the row that precedes the last selected row.
202 * @param {Boolean} keepExisting (optional) True to keep existing selections
204 selectPrevious : function(keepExisting){
206 this.selectRow(this.last-1, keepExisting);
207 this.grid.getView().focusRow(this.last);
212 * Returns the selected records
213 * @return {Array} Array of selected records
215 getSelections : function(){
216 return [].concat(this.selections.items);
220 * Returns the first selected record.
223 getSelected : function(){
224 return this.selections.itemAt(0);
229 * Clears all selections.
231 clearSelections : function(fast){
236 var ds = this.grid.dataSource;
237 var s = this.selections;
239 this.deselectRow(ds.indexOfId(r.id));
243 this.selections.clear();
252 selectAll : function(){
256 this.selections.clear();
257 for(var i = 0, len = this.grid.dataSource.getCount(); i < len; i++){
258 this.selectRow(i, true);
263 * Returns True if there is a selection.
266 hasSelection : function(){
267 return this.selections.length > 0;
271 * Returns True if the specified row is selected.
272 * @param {Number/Record} record The record or index of the record to check
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);
281 * Returns True if the specified record id is selected.
282 * @param {String} id The id of record to check
285 isIdSelected : function(id){
286 return (this.selections.key(id) ? true : false);
290 handleMouseDown : function(e, t){
291 var view = this.grid.getView(), rowIndex;
292 if(this.isLocked() || (rowIndex = view.findRowIndex(t)) === false){
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);
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);
311 this.fireEvent("afterselectionchange", this);
314 handleDragableRowClick : function(grid, rowIndex, e)
316 if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
317 this.selectRow(rowIndex, false);
318 grid.view.focusRow(rowIndex);
319 this.fireEvent("afterselectionchange", this);
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
328 selectRows : function(rows, keepExisting){
330 this.clearSelections();
332 for(var i = 0, len = rows.length; i < len; i++){
333 this.selectRow(rows[i], true);
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
343 selectRange : function(startRow, endRow, keepExisting){
348 this.clearSelections();
350 if(startRow <= endRow){
351 for(var i = startRow; i <= endRow; i++){
352 this.selectRow(i, true);
355 for(var i = startRow; i >= endRow; i--){
356 this.selectRow(i, true);
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
366 deselectRange : function(startRow, endRow, preventViewNotify){
370 for(var i = startRow; i <= endRow; i++){
371 this.deselectRow(i, preventViewNotify);
377 * @param {Number} row The index of the row to select
378 * @param {Boolean} keepExisting (optional) True to keep existing selections
380 selectRow : function(index, keepExisting, preventViewNotify){
381 if(this.locked || (index < 0 || index >= this.grid.dataSource.getCount())) {
384 if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
385 if(!keepExisting || this.singleSelect){
386 this.clearSelections();
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);
394 this.fireEvent("rowselect", this, index, r);
395 this.fireEvent("selectionchange", this);
401 * @param {Number} row The index of the row to deselect
403 deselectRow : function(index, preventViewNotify){
407 if(this.last == index){
410 if(this.lastActive == index){
411 this.lastActive = false;
413 var r = this.grid.dataSource.getAt(index);
414 this.selections.remove(r);
415 if(!preventViewNotify){
416 this.grid.getView().onRowDeselect(index);
418 this.fireEvent("rowdeselect", this, index);
419 this.fireEvent("selectionchange", this);
423 restoreLast : function(){
425 this.last = this._last;
430 acceptsNav : function(row, col, cm){
431 return !cm.isHidden(col) && cm.isCellEditable(col, row);
435 onEditorKey : function(field, e){
436 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
441 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
443 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
445 }else if(k == e.ENTER && !e.ctrlKey){
449 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
451 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
453 }else if(k == e.ESC){
457 g.startEditing(newCell[0], newCell[1]);