3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.2.0
11 * Copyright(c) 2006-2010 Ext JS, Inc.
13 * http://www.extjs.com/license
15 <div id="cls-Ext.grid.CellSelectionModel"></div>/**
16 * @class Ext.grid.CellSelectionModel
17 * @extends Ext.grid.AbstractSelectionModel
18 * This class provides the basic implementation for <i>single</i> <b>cell</b> selection in a grid.
19 * The object stored as the selection contains the following properties:
20 * <div class="mdetail-params"><ul>
21 * <li><b>cell</b> : see {@link #getSelectedCell}
22 * <li><b>record</b> : Ext.data.record The {@link Ext.data.Record Record}
23 * which provides the data for the row containing the selection</li>
26 * @param {Object} config The object containing the configuration of this model.
28 Ext.grid.CellSelectionModel = Ext.extend(Ext.grid.AbstractSelectionModel, {
30 constructor : function(config){
31 Ext.apply(this, config);
33 this.selection = null;
36 <div id="event-Ext.grid.CellSelectionModel-beforecellselect"></div>/**
37 * @event beforecellselect
38 * Fires before a cell is selected, return false to cancel the selection.
39 * @param {SelectionModel} this
40 * @param {Number} rowIndex The selected row index
41 * @param {Number} colIndex The selected cell index
44 <div id="event-Ext.grid.CellSelectionModel-cellselect"></div>/**
46 * Fires when a cell is selected.
47 * @param {SelectionModel} this
48 * @param {Number} rowIndex The selected row index
49 * @param {Number} colIndex The selected cell index
52 <div id="event-Ext.grid.CellSelectionModel-selectionchange"></div>/**
53 * @event selectionchange
54 * Fires when the active selection changes.
55 * @param {SelectionModel} this
56 * @param {Object} selection null for no selection or an object with two properties
57 * <div class="mdetail-params"><ul>
58 * <li><b>cell</b> : see {@link #getSelectedCell}
59 * <li><b>record</b> : Ext.data.record<p class="sub-desc">The {@link Ext.data.Record Record}
60 * which provides the data for the row containing the selection</p></li>
66 Ext.grid.CellSelectionModel.superclass.constructor.call(this);
70 initEvents : function(){
71 this.grid.on('cellmousedown', this.handleMouseDown, this);
72 this.grid.on(Ext.EventManager.useKeydown ? 'keydown' : 'keypress', this.handleKeyDown, this);
73 this.grid.getView().on({
75 refresh: this.onViewChange,
76 rowupdated: this.onRowUpdated,
77 beforerowremoved: this.clearSelections,
78 beforerowsinserted: this.clearSelections
80 if(this.grid.isEditor){
81 this.grid.on('beforeedit', this.beforeEdit, this);
86 beforeEdit : function(e){
87 this.select(e.row, e.column, false, true, e.record);
91 onRowUpdated : function(v, index, r){
92 if(this.selection && this.selection.record == r){
93 v.onCellSelect(index, this.selection.cell[1]);
98 onViewChange : function(){
99 this.clearSelections(true);
102 <div id="method-Ext.grid.CellSelectionModel-getSelectedCell"></div>/**
103 * Returns an array containing the row and column indexes of the currently selected cell
104 * (e.g., [0, 0]), or null if none selected. The array has elements:
105 * <div class="mdetail-params"><ul>
106 * <li><b>rowIndex</b> : Number<p class="sub-desc">The index of the selected row</p></li>
107 * <li><b>cellIndex</b> : Number<p class="sub-desc">The index of the selected cell.
108 * Due to possible column reordering, the cellIndex should <b>not</b> be used as an
109 * index into the Record's data. Instead, use the cellIndex to determine the <i>name</i>
110 * of the selected cell and use the field name to retrieve the data value from the record:<pre><code>
112 var fieldName = grid.getColumnModel().getDataIndex(cellIndex);
113 // get data value based on name
114 var data = record.get(fieldName);
115 * </code></pre></p></li>
117 * @return {Array} An array containing the row and column indexes of the selected cell, or null if none selected.
119 getSelectedCell : function(){
120 return this.selection ? this.selection.cell : null;
123 <div id="method-Ext.grid.CellSelectionModel-clearSelections"></div>/**
124 * If anything is selected, clears all selections and fires the selectionchange event.
125 * @param {Boolean} preventNotify <tt>true</tt> to prevent the gridview from
126 * being notified about the change.
128 clearSelections : function(preventNotify){
129 var s = this.selection;
131 if(preventNotify !== true){
132 this.grid.view.onCellDeselect(s.cell[0], s.cell[1]);
134 this.selection = null;
135 this.fireEvent("selectionchange", this, null);
139 <div id="method-Ext.grid.CellSelectionModel-hasSelection"></div>/**
140 * Returns <tt>true</tt> if there is a selection.
143 hasSelection : function(){
144 return this.selection ? true : false;
148 handleMouseDown : function(g, row, cell, e){
149 if(e.button !== 0 || this.isLocked()){
152 this.select(row, cell);
155 <div id="method-Ext.grid.CellSelectionModel-select"></div>/**
156 * Selects a cell. Before selecting a cell, fires the
157 * {@link #beforecellselect} event. If this check is satisfied the cell
158 * will be selected and followed up by firing the {@link #cellselect} and
159 * {@link #selectionchange} events.
160 * @param {Number} rowIndex The index of the row to select
161 * @param {Number} colIndex The index of the column to select
162 * @param {Boolean} preventViewNotify (optional) Specify <tt>true</tt> to
163 * prevent notifying the view (disables updating the selected appearance)
164 * @param {Boolean} preventFocus (optional) Whether to prevent the cell at
165 * the specified rowIndex / colIndex from being focused.
166 * @param {Ext.data.Record} r (optional) The record to select
168 select : function(rowIndex, colIndex, preventViewNotify, preventFocus, /*internal*/ r){
169 if(this.fireEvent("beforecellselect", this, rowIndex, colIndex) !== false){
170 this.clearSelections();
171 r = r || this.grid.store.getAt(rowIndex);
174 cell : [rowIndex, colIndex]
176 if(!preventViewNotify){
177 var v = this.grid.getView();
178 v.onCellSelect(rowIndex, colIndex);
179 if(preventFocus !== true){
180 v.focusCell(rowIndex, colIndex);
183 this.fireEvent("cellselect", this, rowIndex, colIndex);
184 this.fireEvent("selectionchange", this, this.selection);
189 isSelectable : function(rowIndex, colIndex, cm){
190 return !cm.isHidden(colIndex);
194 onEditorKey: function(field, e){
195 if(e.getKey() == e.TAB){
196 this.handleKeyDown(e);
201 handleKeyDown : function(e){
202 if(!e.isNavKeyPress()){
210 walk = function(row, col, step){
215 g.isEditor && g.editing ? sm.acceptsNav : sm.isSelectable, // *** handle tabbing while editorgrid is in edit mode
219 cell, newCell, r, c, ae;
228 // *** call e.stopEvent() only for non ESC, PAGE UP/DOWN KEYS
234 cell = walk(0, 0, 1); // *** use private walk() function defined above
236 this.select(cell[0], cell[1]);
241 cell = s.cell; // currently selected cell
242 r = cell[0]; // current row
243 c = cell[1]; // current column
248 newCell = walk(r, c - 1, -1);
250 newCell = walk(r, c + 1, 1);
254 newCell = walk(r + 1, c, 1);
257 newCell = walk(r - 1, c, -1);
260 newCell = walk(r, c + 1, 1);
263 newCell = walk(r, c - 1, -1);
266 if (g.isEditor && !g.editing) {
267 g.startEditing(r, c);
274 // *** reassign r & c variables to newly-selected cell's row and column
278 this.select(r, c); // *** highlight newly-selected cell and update selection
280 if(g.isEditor && g.editing){ // *** handle tabbing while editorgrid is in edit mode
282 if(ae && ae.field.triggerBlur){
283 // *** if activeEditor is a TriggerField, explicitly call its triggerBlur() method
284 ae.field.triggerBlur();
286 g.startEditing(r, c);
291 acceptsNav : function(row, col, cm){
292 return !cm.isHidden(col) && cm.isCellEditable(col, row);