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);
84 // bootstrap does not have a view..
85 var view = this.grid.view ? this.grid.view : this.grid;
86 this.rowNav = new Roo.KeyNav(this.grid.getGridEl(), {
89 this.selectPrevious(e.shiftKey);
90 }else if(this.last !== false && this.lastActive !== false){
92 this.selectRange(this.last, this.lastActive-1);
93 view.focusRow(this.lastActive);
98 this.selectFirstRow();
100 this.fireEvent("afterselectionchange", this);
102 "down" : function(e){
104 this.selectNext(e.shiftKey);
105 }else if(this.last !== false && this.lastActive !== false){
106 var last = this.last;
107 this.selectRange(this.last, this.lastActive+1);
108 view.focusRow(this.lastActive);
113 this.selectFirstRow();
115 this.fireEvent("afterselectionchange", this);
121 view.on("refresh", this.onRefresh, this);
122 view.on("rowupdated", this.onRowUpdated, this);
123 view.on("rowremoved", this.onRemove, this);
127 onRefresh : function(){
128 var ds = this.grid.ds, i, v = this.grid.view;
129 var s = this.selections;
131 if((i = ds.indexOfId(r.id)) != -1){
133 s.add(ds.getAt(i)); // updating the selection relate data
141 onRemove : function(v, index, r){
142 this.selections.remove(r);
146 onRowUpdated : function(v, index, r){
147 if(this.isSelected(r)){
148 v.onRowSelect(index);
154 * @param {Array} records The records to select
155 * @param {Boolean} keepExisting (optional) True to keep existing selections
157 selectRecords : function(records, keepExisting){
159 this.clearSelections();
161 var ds = this.grid.ds;
162 for(var i = 0, len = records.length; i < len; i++){
163 this.selectRow(ds.indexOf(records[i]), true);
168 * Gets the number of selected rows.
171 getCount : function(){
172 return this.selections.length;
176 * Selects the first row in the grid.
178 selectFirstRow : function(){
183 * Select the last row.
184 * @param {Boolean} keepExisting (optional) True to keep existing selections
186 selectLastRow : function(keepExisting){
187 this.selectRow(this.grid.ds.getCount() - 1, keepExisting);
191 * Selects the row immediately following the last selected row.
192 * @param {Boolean} keepExisting (optional) True to keep existing selections
194 selectNext : function(keepExisting){
195 if(this.last !== false && (this.last+1) < this.grid.ds.getCount()){
196 this.selectRow(this.last+1, keepExisting);
197 var view = this.grid.view ? this.grid.view : this.grid;
198 view.focusRow(this.last);
203 * Selects the row that precedes the last selected row.
204 * @param {Boolean} keepExisting (optional) True to keep existing selections
206 selectPrevious : function(keepExisting){
208 this.selectRow(this.last-1, keepExisting);
209 var view = this.grid.view ? this.grid.view : this.grid;
210 view.focusRow(this.last);
215 * Returns the selected records
216 * @return {Array} Array of selected records
218 getSelections : function(){
219 return [].concat(this.selections.items);
223 * Returns the first selected record.
226 getSelected : function(){
227 return this.selections.itemAt(0);
232 * Clears all selections.
234 clearSelections : function(fast){
239 var ds = this.grid.ds;
240 var s = this.selections;
242 this.deselectRow(ds.indexOfId(r.id));
246 this.selections.clear();
255 selectAll : function(){
259 this.selections.clear();
260 for(var i = 0, len = this.grid.ds.getCount(); i < len; i++){
261 this.selectRow(i, true);
266 * Returns True if there is a selection.
269 hasSelection : function(){
270 return this.selections.length > 0;
274 * Returns True if the specified row is selected.
275 * @param {Number/Record} record The record or index of the record to check
278 isSelected : function(index){
279 var r = typeof index == "number" ? this.grid.ds.getAt(index) : index;
280 return (r && this.selections.key(r.id) ? true : false);
284 * Returns True if the specified record id is selected.
285 * @param {String} id The id of record to check
288 isIdSelected : function(id){
289 return (this.selections.key(id) ? true : false);
293 handleMouseDown : function(e, t)
295 var view = this.grid.view ? this.grid.view : this.grid;
297 if(this.isLocked() || (rowIndex = view.findRowIndex(t)) === false){
300 if(e.shiftKey && this.last !== false){
301 var last = this.last;
302 this.selectRange(last, rowIndex, e.ctrlKey);
303 this.last = last; // reset the last
304 view.focusRow(rowIndex);
306 var isSelected = this.isSelected(rowIndex);
307 if(e.button !== 0 && isSelected){
308 view.focusRow(rowIndex);
309 }else if(e.ctrlKey && isSelected){
310 this.deselectRow(rowIndex);
311 }else if(!isSelected){
312 this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
313 view.focusRow(rowIndex);
316 this.fireEvent("afterselectionchange", this);
319 handleDragableRowClick : function(grid, rowIndex, e)
321 if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
322 this.selectRow(rowIndex, false);
323 var view = this.grid.view ? this.grid.view : this.grid;
324 view.focusRow(rowIndex);
325 this.fireEvent("afterselectionchange", this);
330 * Selects multiple rows.
331 * @param {Array} rows Array of the indexes of the row to select
332 * @param {Boolean} keepExisting (optional) True to keep existing selections
334 selectRows : function(rows, keepExisting){
336 this.clearSelections();
338 for(var i = 0, len = rows.length; i < len; i++){
339 this.selectRow(rows[i], true);
344 * Selects a range of rows. All rows in between startRow and endRow are also selected.
345 * @param {Number} startRow The index of the first row in the range
346 * @param {Number} endRow The index of the last row in the range
347 * @param {Boolean} keepExisting (optional) True to retain existing selections
349 selectRange : function(startRow, endRow, keepExisting){
354 this.clearSelections();
356 if(startRow <= endRow){
357 for(var i = startRow; i <= endRow; i++){
358 this.selectRow(i, true);
361 for(var i = startRow; i >= endRow; i--){
362 this.selectRow(i, true);
368 * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
369 * @param {Number} startRow The index of the first row in the range
370 * @param {Number} endRow The index of the last row in the range
372 deselectRange : function(startRow, endRow, preventViewNotify){
376 for(var i = startRow; i <= endRow; i++){
377 this.deselectRow(i, preventViewNotify);
383 * @param {Number} row The index of the row to select
384 * @param {Boolean} keepExisting (optional) True to keep existing selections
386 selectRow : function(index, keepExisting, preventViewNotify){
387 if(this.locked || (index < 0 || index >= this.grid.ds.getCount())) {
390 if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
391 if(!keepExisting || this.singleSelect){
392 this.clearSelections();
394 var r = this.grid.ds.getAt(index);
395 this.selections.add(r);
396 this.last = this.lastActive = index;
397 if(!preventViewNotify){
398 var view = this.grid.view ? this.grid.view : this.grid;
399 view.onRowSelect(index);
401 this.fireEvent("rowselect", this, index, r);
402 this.fireEvent("selectionchange", this);
408 * @param {Number} row The index of the row to deselect
410 deselectRow : function(index, preventViewNotify){
414 if(this.last == index){
417 if(this.lastActive == index){
418 this.lastActive = false;
420 var r = this.grid.ds.getAt(index);
421 this.selections.remove(r);
422 if(!preventViewNotify){
423 var view = this.grid.view ? this.grid.view : this.grid;
424 view.onRowDeselect(index);
426 this.fireEvent("rowdeselect", this, index);
427 this.fireEvent("selectionchange", this);
431 restoreLast : function(){
433 this.last = this._last;
438 acceptsNav : function(row, col, cm){
439 return !cm.isHidden(col) && cm.isCellEditable(col, row);
443 onEditorKey : function(field, e){
444 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
449 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
451 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
453 }else if(k == e.ENTER && !e.ctrlKey){
457 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
459 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
461 }else if(k == e.ESC){
465 g.startEditing(newCell[0], newCell[1]);