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 * @class Roo.grid.CellSelectionModel
13 * @extends Roo.grid.AbstractSelectionModel
14 * This class provides the basic implementation for cell selection in a grid.
16 * @param {Object} config The object containing the configuration of this model.
17 * @cfg {Boolean} enter_is_tab Enter behaves the same as tab. (eg. goes to next cell) default: false
19 Roo.grid.CellSelectionModel = function(config){
20 Roo.apply(this, config);
22 this.selection = null;
26 * @event beforerowselect
27 * Fires before a cell is selected.
28 * @param {SelectionModel} this
29 * @param {Number} rowIndex The selected row index
30 * @param {Number} colIndex The selected cell index
32 "beforecellselect" : true,
35 * Fires when a cell is selected.
36 * @param {SelectionModel} this
37 * @param {Number} rowIndex The selected row index
38 * @param {Number} colIndex The selected cell index
42 * @event selectionchange
43 * Fires when the active selection changes.
44 * @param {SelectionModel} this
45 * @param {Object} selection null for no selection or an object (o) with two properties
47 <li>o.record: the record object for the row the selection is in</li>
48 <li>o.cell: An array of [rowIndex, columnIndex]</li>
51 "selectionchange" : true,
54 * Fires when the tab (or enter) was pressed on the last editable cell
55 * You can use this to trigger add new row.
56 * @param {SelectionModel} this
60 * @event beforeeditnext
61 * Fires before the next editable sell is made active
62 * You can use this to skip to another cell
63 * @param {Array} newCell the new [ row, col ] to be selected
65 "beforeeditnext" : true
67 Roo.grid.CellSelectionModel.superclass.constructor.call(this);
70 Roo.extend(Roo.grid.CellSelectionModel, Roo.grid.AbstractSelectionModel, {
75 initEvents : function(){
76 this.grid.on("mousedown", this.handleMouseDown, this);
77 this.grid.getGridEl().on(Roo.isIE ? "keydown" : "keypress", this.handleKeyDown, this);
78 var view = this.grid.view;
79 view.on("refresh", this.onViewChange, this);
80 view.on("rowupdated", this.onRowUpdated, this);
81 view.on("beforerowremoved", this.clearSelections, this);
82 view.on("beforerowsinserted", this.clearSelections, this);
83 if(this.grid.isEditor){
84 this.grid.on("beforeedit", this.beforeEdit, this);
89 beforeEdit : function(e){
90 this.select(e.row, e.column, false, true, e.record);
94 onRowUpdated : function(v, index, r){
95 if(this.selection && this.selection.record == r){
96 v.onCellSelect(index, this.selection.cell[1]);
101 onViewChange : function(){
102 this.clearSelections(true);
106 * Returns the currently selected cell,.
107 * @return {Array} The selected cell (row, column) or null if none selected.
109 getSelectedCell : function(){
110 return this.selection ? this.selection.cell : null;
114 * Clears all selections.
115 * @param {Boolean} true to prevent the gridview from being notified about the change.
117 clearSelections : function(preventNotify){
118 var s = this.selection;
120 if(preventNotify !== true){
121 this.grid.view.onCellDeselect(s.cell[0], s.cell[1]);
123 this.selection = null;
124 this.fireEvent("selectionchange", this, null);
129 * Returns true if there is a selection.
132 hasSelection : function(){
133 return this.selection ? true : false;
137 handleMouseDown : function(e, t){
138 var v = this.grid.getView();
142 var row = v.findRowIndex(t);
143 var cell = v.findCellIndex(t);
144 if(row !== false && cell !== false){
145 this.select(row, cell);
151 * @param {Number} rowIndex
152 * @param {Number} collIndex
154 select : function(rowIndex, colIndex, preventViewNotify, preventFocus, /*internal*/ r){
155 if(this.fireEvent("beforecellselect", this, rowIndex, colIndex) !== false){
156 this.clearSelections();
157 r = r || this.grid.dataSource.getAt(rowIndex);
160 cell : [rowIndex, colIndex]
162 if(!preventViewNotify){
163 var v = this.grid.getView();
164 v.onCellSelect(rowIndex, colIndex);
165 if(preventFocus !== true){
166 v.focusCell(rowIndex, colIndex);
169 this.fireEvent("cellselect", this, rowIndex, colIndex);
170 this.fireEvent("selectionchange", this, this.selection);
175 isSelectable : function(rowIndex, colIndex, cm){
176 return !cm.isHidden(colIndex);
180 handleKeyDown : function(e){
181 //Roo.log('Cell Sel Model handleKeyDown');
182 if(!e.isNavKeyPress()){
185 var g = this.grid, s = this.selection;
188 var cell = g.walkCells(0, 0, 1, this.isSelectable, this);
190 this.select(cell[0], cell[1]);
195 var walk = function(row, col, step){
196 return g.walkCells(row, col, step, sm.isSelectable, sm);
198 var k = e.getKey(), r = s.cell[0], c = s.cell[1];
205 // handled by onEditorKey
206 if (g.isEditor && g.editing) {
210 newCell = walk(r, c-1, -1);
212 newCell = walk(r, c+1, 1);
217 newCell = walk(r+1, c, 1);
221 newCell = walk(r-1, c, -1);
225 newCell = walk(r, c+1, 1);
229 newCell = walk(r, c-1, -1);
234 if(g.isEditor && !g.editing){
235 g.startEditing(r, c);
244 this.select(newCell[0], newCell[1]);
250 acceptsNav : function(row, col, cm){
251 return !cm.isHidden(col) && cm.isCellEditable(col, row);
255 * @param {Number} field (not used) - as it's normally used as a listener
256 * @param {Number} e - event - fake it by using
258 * var e = Roo.EventObjectImpl.prototype;
263 onEditorKey : function(field, e){
270 ///Roo.log('onEditorKey' + k);
273 if (this.enter_is_tab && k == e.ENTER) {
279 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
281 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
287 }else if(k == e.ENTER && !e.ctrlKey){
290 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
291 }else if(k == e.ESC){
295 var ecall = { cell : newCell }
296 this.fireEvent('beforeeditnext', ecall );
297 newCell = ecall.newCell;
300 // can modify new Cell
303 //Roo.log('next cell after edit');
304 g.startEditing.defer(100, g, [newCell[0], newCell[1]]);
305 } else if (forward) {
307 this.fireEvent.defer(100, this, ['tabend',this]);