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 Roo.grid.CellSelectionModel.superclass.constructor.call(this);
63 Roo.extend(Roo.grid.CellSelectionModel, Roo.grid.AbstractSelectionModel, {
68 initEvents : function(){
69 this.grid.on("mousedown", this.handleMouseDown, this);
70 this.grid.getGridEl().on(Roo.isIE ? "keydown" : "keypress", this.handleKeyDown, this);
71 var view = this.grid.view;
72 view.on("refresh", this.onViewChange, this);
73 view.on("rowupdated", this.onRowUpdated, this);
74 view.on("beforerowremoved", this.clearSelections, this);
75 view.on("beforerowsinserted", this.clearSelections, this);
76 if(this.grid.isEditor){
77 this.grid.on("beforeedit", this.beforeEdit, this);
82 beforeEdit : function(e){
83 this.select(e.row, e.column, false, true, e.record);
87 onRowUpdated : function(v, index, r){
88 if(this.selection && this.selection.record == r){
89 v.onCellSelect(index, this.selection.cell[1]);
94 onViewChange : function(){
95 this.clearSelections(true);
99 * Returns the currently selected cell,.
100 * @return {Array} The selected cell (row, column) or null if none selected.
102 getSelectedCell : function(){
103 return this.selection ? this.selection.cell : null;
107 * Clears all selections.
108 * @param {Boolean} true to prevent the gridview from being notified about the change.
110 clearSelections : function(preventNotify){
111 var s = this.selection;
113 if(preventNotify !== true){
114 this.grid.view.onCellDeselect(s.cell[0], s.cell[1]);
116 this.selection = null;
117 this.fireEvent("selectionchange", this, null);
122 * Returns true if there is a selection.
125 hasSelection : function(){
126 return this.selection ? true : false;
130 handleMouseDown : function(e, t){
131 var v = this.grid.getView();
135 var row = v.findRowIndex(t);
136 var cell = v.findCellIndex(t);
137 if(row !== false && cell !== false){
138 this.select(row, cell);
144 * @param {Number} rowIndex
145 * @param {Number} collIndex
147 select : function(rowIndex, colIndex, preventViewNotify, preventFocus, /*internal*/ r){
148 if(this.fireEvent("beforecellselect", this, rowIndex, colIndex) !== false){
149 this.clearSelections();
150 r = r || this.grid.dataSource.getAt(rowIndex);
153 cell : [rowIndex, colIndex]
155 if(!preventViewNotify){
156 var v = this.grid.getView();
157 v.onCellSelect(rowIndex, colIndex);
158 if(preventFocus !== true){
159 v.focusCell(rowIndex, colIndex);
162 this.fireEvent("cellselect", this, rowIndex, colIndex);
163 this.fireEvent("selectionchange", this, this.selection);
168 isSelectable : function(rowIndex, colIndex, cm){
169 return !cm.isHidden(colIndex);
173 handleKeyDown : function(e){
174 //Roo.log('Cell Sel Model handleKeyDown');
175 if(!e.isNavKeyPress()){
178 var g = this.grid, s = this.selection;
181 var cell = g.walkCells(0, 0, 1, this.isSelectable, this);
183 this.select(cell[0], cell[1]);
188 var walk = function(row, col, step){
189 return g.walkCells(row, col, step, sm.isSelectable, sm);
191 var k = e.getKey(), r = s.cell[0], c = s.cell[1];
192 var newCell, forward=false;
198 // handled by onEditorKey
199 if (g.isEditor && g.editing) {
203 newCell = walk(r, c-1, -1);
205 newCell = walk(r, c+1, 1);
211 newCell = walk(r+1, c, 1);
216 newCell = walk(r-1, c, -1);
220 newCell = walk(r, c+1, 1);
225 newCell = walk(r, c-1, -1);
230 if(g.isEditor && !g.editing){
231 g.startEditing(r, c);
240 this.select(newCell[0], newCell[1]);
246 acceptsNav : function(row, col, cm){
247 return !cm.isHidden(col) && cm.isCellEditable(col, row);
251 * @param {Number} field (not used) - as it's normally used as a listener
252 * @param {Number} e - event - fake it by using
254 * var e = Roo.EventObjectImpl.prototype;
259 onEditorKey : function(field, e){
266 ///Roo.log('onEditorKey' + k);
269 if (this.enter_is_tab && k == e.ENTER) {
275 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
277 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
283 }else if(k == e.ENTER && !e.ctrlKey){
286 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
287 }else if(k == e.ESC){
293 //Roo.log('next cell after edit');
294 g.startEditing.defer(100, g, [newCell[0], newCell[1]]);
295 } else if (forward) {
297 this.fireEvent('tabend',this);