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
53 Roo.grid.CellSelectionModel.superclass.constructor.call(this);
56 Roo.extend(Roo.grid.CellSelectionModel, Roo.grid.AbstractSelectionModel, {
61 initEvents : function(){
62 this.grid.on("mousedown", this.handleMouseDown, this);
63 this.grid.getGridEl().on(Roo.isIE ? "keydown" : "keypress", this.handleKeyDown, this);
64 var view = this.grid.view;
65 view.on("refresh", this.onViewChange, this);
66 view.on("rowupdated", this.onRowUpdated, this);
67 view.on("beforerowremoved", this.clearSelections, this);
68 view.on("beforerowsinserted", this.clearSelections, this);
69 if(this.grid.isEditor){
70 this.grid.on("beforeedit", this.beforeEdit, this);
75 beforeEdit : function(e){
76 this.select(e.row, e.column, false, true, e.record);
80 onRowUpdated : function(v, index, r){
81 if(this.selection && this.selection.record == r){
82 v.onCellSelect(index, this.selection.cell[1]);
87 onViewChange : function(){
88 this.clearSelections(true);
92 * Returns the currently selected cell,.
93 * @return {Array} The selected cell (row, column) or null if none selected.
95 getSelectedCell : function(){
96 return this.selection ? this.selection.cell : null;
100 * Clears all selections.
101 * @param {Boolean} true to prevent the gridview from being notified about the change.
103 clearSelections : function(preventNotify){
104 var s = this.selection;
106 if(preventNotify !== true){
107 this.grid.view.onCellDeselect(s.cell[0], s.cell[1]);
109 this.selection = null;
110 this.fireEvent("selectionchange", this, null);
115 * Returns true if there is a selection.
118 hasSelection : function(){
119 return this.selection ? true : false;
123 handleMouseDown : function(e, t){
124 var v = this.grid.getView();
128 var row = v.findRowIndex(t);
129 var cell = v.findCellIndex(t);
130 if(row !== false && cell !== false){
131 this.select(row, cell);
137 * @param {Number} rowIndex
138 * @param {Number} collIndex
140 select : function(rowIndex, colIndex, preventViewNotify, preventFocus, /*internal*/ r){
141 if(this.fireEvent("beforecellselect", this, rowIndex, colIndex) !== false){
142 this.clearSelections();
143 r = r || this.grid.dataSource.getAt(rowIndex);
146 cell : [rowIndex, colIndex]
148 if(!preventViewNotify){
149 var v = this.grid.getView();
150 v.onCellSelect(rowIndex, colIndex);
151 if(preventFocus !== true){
152 v.focusCell(rowIndex, colIndex);
155 this.fireEvent("cellselect", this, rowIndex, colIndex);
156 this.fireEvent("selectionchange", this, this.selection);
161 isSelectable : function(rowIndex, colIndex, cm){
162 return !cm.isHidden(colIndex);
166 handleKeyDown : function(e){
167 //Roo.log('Cell Sel Model handleKeyDown');
168 if(!e.isNavKeyPress()){
171 var g = this.grid, s = this.selection;
174 var cell = g.walkCells(0, 0, 1, this.isSelectable, this);
176 this.select(cell[0], cell[1]);
181 var walk = function(row, col, step){
182 return g.walkCells(row, col, step, sm.isSelectable, sm);
184 var k = e.getKey(), r = s.cell[0], c = s.cell[1];
185 var newCell, forward=false;
187 if (this.enter_is_tab && k == e.ENTER) {
193 // handled by onEditorKey
194 if (g.isEditor && g.editing) {
198 newCell = walk(r, c-1, -1);
200 newCell = walk(r, c+1, 1);
206 newCell = walk(r+1, c, 1);
211 newCell = walk(r-1, c, -1);
215 newCell = walk(r, c+1, 1);
220 newCell = walk(r, c-1, -1);
225 if(g.isEditor && !g.editing){
226 g.startEditing(r, c);
235 this.select(newCell[0], newCell[1]);
238 } else if (forward) {
240 g.fireEvent('tabend');
245 acceptsNav : function(row, col, cm){
246 return !cm.isHidden(col) && cm.isCellEditable(col, row);
250 * @param {Number} field (not used) - as it's normally used as a listener
251 * @param {Number} e - event - fake it by using
253 * var e = Roo.EventObjectImpl.prototype;
258 onEditorKey : function(field, e){
260 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
261 ///Roo.log('onEditorKey' + k);
269 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
271 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
276 }else if(k == e.ENTER && !e.ctrlKey){
279 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
280 }else if(k == e.ESC){
286 //Roo.log('next cell after edit');
287 g.startEditing.defer(100, g, [newCell[0], newCell[1]]);