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.
18 Roo.grid.CellSelectionModel = function(config){
19 Roo.apply(this, config);
21 this.selection = null;
25 * @event beforerowselect
26 * Fires before a cell is selected.
27 * @param {SelectionModel} this
28 * @param {Number} rowIndex The selected row index
29 * @param {Number} colIndex The selected cell index
31 "beforecellselect" : true,
34 * Fires when a cell is selected.
35 * @param {SelectionModel} this
36 * @param {Number} rowIndex The selected row index
37 * @param {Number} colIndex The selected cell index
41 * @event selectionchange
42 * Fires when the active selection changes.
43 * @param {SelectionModel} this
44 * @param {Object} selection null for no selection or an object (o) with two properties
46 <li>o.record: the record object for the row the selection is in</li>
47 <li>o.cell: An array of [rowIndex, columnIndex]</li>
50 "selectionchange" : true
52 Roo.grid.CellSelectionModel.superclass.constructor.call(this);
55 Roo.extend(Roo.grid.CellSelectionModel, Roo.grid.AbstractSelectionModel, {
58 initEvents : function(){
59 this.grid.on("mousedown", this.handleMouseDown, this);
60 this.grid.getGridEl().on(Roo.isIE ? "keydown" : "keypress", this.handleKeyDown, this);
61 var view = this.grid.view;
62 view.on("refresh", this.onViewChange, this);
63 view.on("rowupdated", this.onRowUpdated, this);
64 view.on("beforerowremoved", this.clearSelections, this);
65 view.on("beforerowsinserted", this.clearSelections, this);
66 if(this.grid.isEditor){
67 this.grid.on("beforeedit", this.beforeEdit, this);
72 beforeEdit : function(e){
73 this.select(e.row, e.column, false, true, e.record);
77 onRowUpdated : function(v, index, r){
78 if(this.selection && this.selection.record == r){
79 v.onCellSelect(index, this.selection.cell[1]);
84 onViewChange : function(){
85 this.clearSelections(true);
89 * Returns the currently selected cell,.
90 * @return {Array} The selected cell (row, column) or null if none selected.
92 getSelectedCell : function(){
93 return this.selection ? this.selection.cell : null;
97 * Clears all selections.
98 * @param {Boolean} true to prevent the gridview from being notified about the change.
100 clearSelections : function(preventNotify){
101 var s = this.selection;
103 if(preventNotify !== true){
104 this.grid.view.onCellDeselect(s.cell[0], s.cell[1]);
106 this.selection = null;
107 this.fireEvent("selectionchange", this, null);
112 * Returns true if there is a selection.
115 hasSelection : function(){
116 return this.selection ? true : false;
120 handleMouseDown : function(e, t){
121 var v = this.grid.getView();
125 var row = v.findRowIndex(t);
126 var cell = v.findCellIndex(t);
127 if(row !== false && cell !== false){
128 this.select(row, cell);
134 * @param {Number} rowIndex
135 * @param {Number} collIndex
137 select : function(rowIndex, colIndex, preventViewNotify, preventFocus, /*internal*/ r){
138 if(this.fireEvent("beforecellselect", this, rowIndex, colIndex) !== false){
139 this.clearSelections();
140 r = r || this.grid.dataSource.getAt(rowIndex);
143 cell : [rowIndex, colIndex]
145 if(!preventViewNotify){
146 var v = this.grid.getView();
147 v.onCellSelect(rowIndex, colIndex);
148 if(preventFocus !== true){
149 v.focusCell(rowIndex, colIndex);
152 this.fireEvent("cellselect", this, rowIndex, colIndex);
153 this.fireEvent("selectionchange", this, this.selection);
158 isSelectable : function(rowIndex, colIndex, cm){
159 return !cm.isHidden(colIndex);
163 handleKeyDown : function(e){
164 //Roo.log('Cell Sel Model handleKeyDown');
165 if(!e.isNavKeyPress()){
168 var g = this.grid, s = this.selection;
171 var cell = g.walkCells(0, 0, 1, this.isSelectable, this);
173 this.select(cell[0], cell[1]);
178 var walk = function(row, col, step){
179 return g.walkCells(row, col, step, sm.isSelectable, sm);
181 var k = e.getKey(), r = s.cell[0], c = s.cell[1];
182 var newCell, forward=false;
184 if (this.enter_is_tab && k == e.ENTER) {
190 // handled by onEditorKey
191 if (g.isEditor && g.editing) {
195 newCell = walk(r, c-1, -1);
197 newCell = walk(r, c+1, 1);
203 newCell = walk(r+1, c, 1);
208 newCell = walk(r-1, c, -1);
212 newCell = walk(r, c+1, 1);
217 newCell = walk(r, c-1, -1);
222 if(g.isEditor && !g.editing){
223 g.startEditing(r, c);
232 this.select(newCell[0], newCell[1]);
235 } else if (forward) {
237 g.fireEvent('tabend');
242 acceptsNav : function(row, col, cm){
243 return !cm.isHidden(col) && cm.isCellEditable(col, row);
247 * @param {Number} field (not used) - as it's normally used as a listener
248 * @param {Number} e - event - fake it by using
250 * var e = Roo.EventObjectImpl.prototype;
255 onEditorKey : function(field, e){
257 var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
258 ///Roo.log('onEditorKey' + k);
266 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
268 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
273 }else if(k == e.ENTER && !e.ctrlKey){
276 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
277 }else if(k == e.ESC){
283 //Roo.log('next cell after edit');
284 g.startEditing.defer(100, g, [newCell[0], newCell[1]]);