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 or fire the tabend
63 * if you set cell to false
64 * @param {Object} eventdata object : { cell : [ row, col ] }
66 "beforeeditnext" : true
68 Roo.grid.CellSelectionModel.superclass.constructor.call(this);
71 Roo.extend(Roo.grid.CellSelectionModel, Roo.grid.AbstractSelectionModel, {
76 initEvents : function(){
77 this.grid.on("mousedown", this.handleMouseDown, this);
78 this.grid.getGridEl().on(Roo.isIE ? "keydown" : "keypress", this.handleKeyDown, this);
79 var view = this.grid.view;
80 view.on("refresh", this.onViewChange, this);
81 view.on("rowupdated", this.onRowUpdated, this);
82 view.on("beforerowremoved", this.clearSelections, this);
83 view.on("beforerowsinserted", this.clearSelections, this);
84 if(this.grid.isEditor){
85 this.grid.on("beforeedit", this.beforeEdit, this);
90 beforeEdit : function(e){
91 this.select(e.row, e.column, false, true, e.record);
95 onRowUpdated : function(v, index, r){
96 if(this.selection && this.selection.record == r){
97 v.onCellSelect(index, this.selection.cell[1]);
102 onViewChange : function(){
103 this.clearSelections(true);
107 * Returns the currently selected cell,.
108 * @return {Array} The selected cell (row, column) or null if none selected.
110 getSelectedCell : function(){
111 return this.selection ? this.selection.cell : null;
115 * Clears all selections.
116 * @param {Boolean} true to prevent the gridview from being notified about the change.
118 clearSelections : function(preventNotify){
119 var s = this.selection;
121 if(preventNotify !== true){
122 this.grid.view.onCellDeselect(s.cell[0], s.cell[1]);
124 this.selection = null;
125 this.fireEvent("selectionchange", this, null);
130 * Returns true if there is a selection.
133 hasSelection : function(){
134 return this.selection ? true : false;
138 handleMouseDown : function(e, t){
139 var v = this.grid.getView();
143 var row = v.findRowIndex(t);
144 var cell = v.findCellIndex(t);
145 if(row !== false && cell !== false){
146 this.select(row, cell);
152 * @param {Number} rowIndex
153 * @param {Number} collIndex
155 select : function(rowIndex, colIndex, preventViewNotify, preventFocus, /*internal*/ r){
156 if(this.fireEvent("beforecellselect", this, rowIndex, colIndex) !== false){
157 this.clearSelections();
158 r = r || this.grid.dataSource.getAt(rowIndex);
161 cell : [rowIndex, colIndex]
163 if(!preventViewNotify){
164 var v = this.grid.getView();
165 v.onCellSelect(rowIndex, colIndex);
166 if(preventFocus !== true){
167 v.focusCell(rowIndex, colIndex);
170 this.fireEvent("cellselect", this, rowIndex, colIndex);
171 this.fireEvent("selectionchange", this, this.selection);
176 isSelectable : function(rowIndex, colIndex, cm){
177 return !cm.isHidden(colIndex);
181 handleKeyDown : function(e){
182 //Roo.log('Cell Sel Model handleKeyDown');
183 if(!e.isNavKeyPress()){
186 var g = this.grid, s = this.selection;
189 var cell = g.walkCells(0, 0, 1, this.isSelectable, this);
191 this.select(cell[0], cell[1]);
196 var walk = function(row, col, step){
197 return g.walkCells(row, col, step, sm.isSelectable, sm);
199 var k = e.getKey(), r = s.cell[0], c = s.cell[1];
206 // handled by onEditorKey
207 if (g.isEditor && g.editing) {
211 newCell = walk(r, c-1, -1);
213 newCell = walk(r, c+1, 1);
218 newCell = walk(r+1, c, 1);
222 newCell = walk(r-1, c, -1);
226 newCell = walk(r, c+1, 1);
230 newCell = walk(r, c-1, -1);
235 if(g.isEditor && !g.editing){
236 g.startEditing(r, c);
245 this.select(newCell[0], newCell[1]);
251 acceptsNav : function(row, col, cm){
252 return !cm.isHidden(col) && cm.isCellEditable(col, row);
256 * @param {Number} field (not used) - as it's normally used as a listener
257 * @param {Number} e - event - fake it by using
259 * var e = Roo.EventObjectImpl.prototype;
264 onEditorKey : function(field, e){
271 ///Roo.log('onEditorKey' + k);
274 if (this.enter_is_tab && k == e.ENTER) {
280 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
282 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
288 } else if(k == e.ENTER && !e.ctrlKey){
291 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
293 } else if(k == e.ESC){
298 var ecall = { cell : newCell, forward : forward };
299 this.fireEvent('beforeeditnext', ecall );
300 newCell = ecall.cell;
301 forward = ecall.forward;
305 //Roo.log('next cell after edit');
306 g.startEditing.defer(100, g, [newCell[0], newCell[1]]);
307 } else if (forward) {
309 this.fireEvent.defer(100, this, ['tabend',this]);