[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | /** |
---|
| 8 | * @class Ext.grid.EditorGridPanel |
---|
| 9 | * @extends Ext.grid.GridPanel |
---|
| 10 | * <p>This class extends the {@link Ext.grid.GridPanel GridPanel Class} to provide cell editing |
---|
| 11 | * on selected {@link Ext.grid.Column columns}. The editable columns are specified by providing |
---|
| 12 | * an {@link Ext.grid.ColumnModel#editor editor} in the {@link Ext.grid.Column column configuration}.</p> |
---|
| 13 | * <p>Editability of columns may be controlled programatically by inserting an implementation |
---|
| 14 | * of {@link Ext.grid.ColumnModel#isCellEditable isCellEditable} into the |
---|
| 15 | * {@link Ext.grid.ColumnModel ColumnModel}.</p> |
---|
| 16 | * <p>Editing is performed on the value of the <i>field</i> specified by the column's |
---|
| 17 | * <tt>{@link Ext.grid.ColumnModel#dataIndex dataIndex}</tt> in the backing {@link Ext.data.Store Store} |
---|
| 18 | * (so if you are using a {@link Ext.grid.ColumnModel#setRenderer renderer} in order to display |
---|
| 19 | * transformed data, this must be accounted for).</p> |
---|
| 20 | * <p>If a value-to-description mapping is used to render a column, then a {@link Ext.form.Field#ComboBox ComboBox} |
---|
| 21 | * which uses the same {@link Ext.form.Field#valueField value}-to-{@link Ext.form.Field#displayFieldField description} |
---|
| 22 | * mapping would be an appropriate editor.</p> |
---|
| 23 | * If there is a more complex mismatch between the visible data in the grid, and the editable data in |
---|
| 24 | * the {@link Edt.data.Store Store}, then code to transform the data both before and after editing can be |
---|
| 25 | * injected using the {@link #beforeedit} and {@link #afteredit} events. |
---|
| 26 | * @constructor |
---|
| 27 | * @param {Object} config The config object |
---|
| 28 | * @xtype editorgrid |
---|
| 29 | */ |
---|
| 30 | Ext.grid.EditorGridPanel = Ext.extend(Ext.grid.GridPanel, { |
---|
| 31 | /** |
---|
| 32 | * @cfg {Number} clicksToEdit |
---|
| 33 | * <p>The number of clicks on a cell required to display the cell's editor (defaults to 2).</p> |
---|
| 34 | * <p>Setting this option to 'auto' means that mousedown <i>on the selected cell</i> starts |
---|
| 35 | * editing that cell.</p> |
---|
| 36 | */ |
---|
| 37 | clicksToEdit: 2, |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * @cfg {Boolean} forceValidation |
---|
| 41 | * True to force validation even if the value is unmodified (defaults to false) |
---|
| 42 | */ |
---|
| 43 | forceValidation: false, |
---|
| 44 | |
---|
| 45 | // private |
---|
| 46 | isEditor : true, |
---|
| 47 | // private |
---|
| 48 | detectEdit: false, |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * @cfg {Boolean} autoEncode |
---|
| 52 | * True to automatically HTML encode and decode values pre and post edit (defaults to false) |
---|
| 53 | */ |
---|
| 54 | autoEncode : false, |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * @cfg {Boolean} trackMouseOver @hide |
---|
| 58 | */ |
---|
| 59 | // private |
---|
| 60 | trackMouseOver: false, // causes very odd FF errors |
---|
| 61 | |
---|
| 62 | // private |
---|
| 63 | initComponent : function(){ |
---|
| 64 | Ext.grid.EditorGridPanel.superclass.initComponent.call(this); |
---|
| 65 | |
---|
| 66 | if(!this.selModel){ |
---|
| 67 | /** |
---|
| 68 | * @cfg {Object} selModel Any subclass of AbstractSelectionModel that will provide the selection model for |
---|
| 69 | * the grid (defaults to {@link Ext.grid.CellSelectionModel} if not specified). |
---|
| 70 | */ |
---|
| 71 | this.selModel = new Ext.grid.CellSelectionModel(); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | this.activeEditor = null; |
---|
| 75 | |
---|
| 76 | this.addEvents( |
---|
| 77 | /** |
---|
| 78 | * @event beforeedit |
---|
| 79 | * Fires before cell editing is triggered. The edit event object has the following properties <br /> |
---|
| 80 | * <ul style="padding:5px;padding-left:16px;"> |
---|
| 81 | * <li>grid - This grid</li> |
---|
| 82 | * <li>record - The record being edited</li> |
---|
| 83 | * <li>field - The field name being edited</li> |
---|
| 84 | * <li>value - The value for the field being edited.</li> |
---|
| 85 | * <li>row - The grid row index</li> |
---|
| 86 | * <li>column - The grid column index</li> |
---|
| 87 | * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li> |
---|
| 88 | * </ul> |
---|
| 89 | * @param {Object} e An edit event (see above for description) |
---|
| 90 | */ |
---|
| 91 | "beforeedit", |
---|
| 92 | /** |
---|
| 93 | * @event afteredit |
---|
| 94 | * Fires after a cell is edited. The edit event object has the following properties <br /> |
---|
| 95 | * <ul style="padding:5px;padding-left:16px;"> |
---|
| 96 | * <li>grid - This grid</li> |
---|
| 97 | * <li>record - The record being edited</li> |
---|
| 98 | * <li>field - The field name being edited</li> |
---|
| 99 | * <li>value - The value being set</li> |
---|
| 100 | * <li>originalValue - The original value for the field, before the edit.</li> |
---|
| 101 | * <li>row - The grid row index</li> |
---|
| 102 | * <li>column - The grid column index</li> |
---|
| 103 | * </ul> |
---|
| 104 | * |
---|
| 105 | * <pre><code> |
---|
| 106 | grid.on('afteredit', afterEdit, this ); |
---|
| 107 | |
---|
| 108 | function afterEdit(e) { |
---|
| 109 | // execute an XHR to send/commit data to the server, in callback do (if successful): |
---|
| 110 | e.record.commit(); |
---|
| 111 | }; |
---|
| 112 | * </code></pre> |
---|
| 113 | * @param {Object} e An edit event (see above for description) |
---|
| 114 | */ |
---|
| 115 | "afteredit", |
---|
| 116 | /** |
---|
| 117 | * @event validateedit |
---|
| 118 | * Fires after a cell is edited, but before the value is set in the record. Return false |
---|
| 119 | * to cancel the change. The edit event object has the following properties <br /> |
---|
| 120 | * <ul style="padding:5px;padding-left:16px;"> |
---|
| 121 | * <li>grid - This grid</li> |
---|
| 122 | * <li>record - The record being edited</li> |
---|
| 123 | * <li>field - The field name being edited</li> |
---|
| 124 | * <li>value - The value being set</li> |
---|
| 125 | * <li>originalValue - The original value for the field, before the edit.</li> |
---|
| 126 | * <li>row - The grid row index</li> |
---|
| 127 | * <li>column - The grid column index</li> |
---|
| 128 | * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li> |
---|
| 129 | * </ul> |
---|
| 130 | * Usage example showing how to remove the red triangle (dirty record indicator) from some |
---|
| 131 | * records (not all). By observing the grid's validateedit event, it can be cancelled if |
---|
| 132 | * the edit occurs on a targeted row (for example) and then setting the field's new value |
---|
| 133 | * in the Record directly: |
---|
| 134 | * <pre><code> |
---|
| 135 | grid.on('validateedit', function(e) { |
---|
| 136 | var myTargetRow = 6; |
---|
| 137 | |
---|
| 138 | if (e.row == myTargetRow) { |
---|
| 139 | e.cancel = true; |
---|
| 140 | e.record.data[e.field] = e.value; |
---|
| 141 | } |
---|
| 142 | }); |
---|
| 143 | * </code></pre> |
---|
| 144 | * @param {Object} e An edit event (see above for description) |
---|
| 145 | */ |
---|
| 146 | "validateedit" |
---|
| 147 | ); |
---|
| 148 | }, |
---|
| 149 | |
---|
| 150 | // private |
---|
| 151 | initEvents : function(){ |
---|
| 152 | Ext.grid.EditorGridPanel.superclass.initEvents.call(this); |
---|
| 153 | |
---|
| 154 | this.getGridEl().on('mousewheel', this.stopEditing.createDelegate(this, [true]), this); |
---|
| 155 | this.on('columnresize', this.stopEditing, this, [true]); |
---|
| 156 | |
---|
| 157 | if(this.clicksToEdit == 1){ |
---|
| 158 | this.on("cellclick", this.onCellDblClick, this); |
---|
| 159 | }else { |
---|
| 160 | var view = this.getView(); |
---|
| 161 | if(this.clicksToEdit == 'auto' && view.mainBody){ |
---|
| 162 | view.mainBody.on('mousedown', this.onAutoEditClick, this); |
---|
| 163 | } |
---|
| 164 | this.on('celldblclick', this.onCellDblClick, this); |
---|
| 165 | } |
---|
| 166 | }, |
---|
| 167 | |
---|
| 168 | onResize : function(){ |
---|
| 169 | Ext.grid.EditorGridPanel.superclass.onResize.apply(this, arguments); |
---|
| 170 | var ae = this.activeEditor; |
---|
| 171 | if(this.editing && ae){ |
---|
| 172 | ae.realign(true); |
---|
| 173 | } |
---|
| 174 | }, |
---|
| 175 | |
---|
| 176 | // private |
---|
| 177 | onCellDblClick : function(g, row, col){ |
---|
| 178 | this.startEditing(row, col); |
---|
| 179 | }, |
---|
| 180 | |
---|
| 181 | // private |
---|
| 182 | onAutoEditClick : function(e, t){ |
---|
| 183 | if(e.button !== 0){ |
---|
| 184 | return; |
---|
| 185 | } |
---|
| 186 | var row = this.view.findRowIndex(t), |
---|
| 187 | col = this.view.findCellIndex(t); |
---|
| 188 | if(row !== false && col !== false){ |
---|
| 189 | this.stopEditing(); |
---|
| 190 | if(this.selModel.getSelectedCell){ // cell sm |
---|
| 191 | var sc = this.selModel.getSelectedCell(); |
---|
| 192 | if(sc && sc[0] === row && sc[1] === col){ |
---|
| 193 | this.startEditing(row, col); |
---|
| 194 | } |
---|
| 195 | }else{ |
---|
| 196 | if(this.selModel.isSelected(row)){ |
---|
| 197 | this.startEditing(row, col); |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | }, |
---|
| 202 | |
---|
| 203 | // private |
---|
| 204 | onEditComplete : function(ed, value, startValue){ |
---|
| 205 | this.editing = false; |
---|
| 206 | this.lastActiveEditor = this.activeEditor; |
---|
| 207 | this.activeEditor = null; |
---|
| 208 | |
---|
| 209 | var r = ed.record, |
---|
| 210 | field = this.colModel.getDataIndex(ed.col); |
---|
| 211 | value = this.postEditValue(value, startValue, r, field); |
---|
| 212 | if(this.forceValidation === true || String(value) !== String(startValue)){ |
---|
| 213 | var e = { |
---|
| 214 | grid: this, |
---|
| 215 | record: r, |
---|
| 216 | field: field, |
---|
| 217 | originalValue: startValue, |
---|
| 218 | value: value, |
---|
| 219 | row: ed.row, |
---|
| 220 | column: ed.col, |
---|
| 221 | cancel:false |
---|
| 222 | }; |
---|
| 223 | if(this.fireEvent("validateedit", e) !== false && !e.cancel && String(value) !== String(startValue)){ |
---|
| 224 | r.set(field, e.value); |
---|
| 225 | delete e.cancel; |
---|
| 226 | this.fireEvent("afteredit", e); |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | this.view.focusCell(ed.row, ed.col); |
---|
| 230 | }, |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | * Starts editing the specified for the specified row/column |
---|
| 234 | * @param {Number} rowIndex |
---|
| 235 | * @param {Number} colIndex |
---|
| 236 | */ |
---|
| 237 | startEditing : function(row, col){ |
---|
| 238 | this.stopEditing(); |
---|
| 239 | if(this.colModel.isCellEditable(col, row)){ |
---|
| 240 | this.view.ensureVisible(row, col, true); |
---|
| 241 | var r = this.store.getAt(row), |
---|
| 242 | field = this.colModel.getDataIndex(col), |
---|
| 243 | e = { |
---|
| 244 | grid: this, |
---|
| 245 | record: r, |
---|
| 246 | field: field, |
---|
| 247 | value: r.data[field], |
---|
| 248 | row: row, |
---|
| 249 | column: col, |
---|
| 250 | cancel:false |
---|
| 251 | }; |
---|
| 252 | if(this.fireEvent("beforeedit", e) !== false && !e.cancel){ |
---|
| 253 | this.editing = true; |
---|
| 254 | var ed = this.colModel.getCellEditor(col, row); |
---|
| 255 | if(!ed){ |
---|
| 256 | return; |
---|
| 257 | } |
---|
| 258 | if(!ed.rendered){ |
---|
| 259 | ed.parentEl = this.view.getEditorParent(ed); |
---|
| 260 | ed.on({ |
---|
| 261 | scope: this, |
---|
| 262 | render: { |
---|
| 263 | fn: function(c){ |
---|
| 264 | c.field.focus(false, true); |
---|
| 265 | }, |
---|
| 266 | single: true, |
---|
| 267 | scope: this |
---|
| 268 | }, |
---|
| 269 | specialkey: function(field, e){ |
---|
| 270 | this.getSelectionModel().onEditorKey(field, e); |
---|
| 271 | }, |
---|
| 272 | complete: this.onEditComplete, |
---|
| 273 | canceledit: this.stopEditing.createDelegate(this, [true]) |
---|
| 274 | }); |
---|
| 275 | } |
---|
| 276 | Ext.apply(ed, { |
---|
| 277 | row : row, |
---|
| 278 | col : col, |
---|
| 279 | record : r |
---|
| 280 | }); |
---|
| 281 | this.lastEdit = { |
---|
| 282 | row: row, |
---|
| 283 | col: col |
---|
| 284 | }; |
---|
| 285 | this.activeEditor = ed; |
---|
| 286 | // Set the selectSameEditor flag if we are reusing the same editor again and |
---|
| 287 | // need to prevent the editor from firing onBlur on itself. |
---|
| 288 | ed.selectSameEditor = (this.activeEditor == this.lastActiveEditor); |
---|
| 289 | var v = this.preEditValue(r, field); |
---|
| 290 | ed.startEdit(this.view.getCell(row, col).firstChild, Ext.isDefined(v) ? v : ''); |
---|
| 291 | |
---|
| 292 | // Clear the selectSameEditor flag |
---|
| 293 | (function(){ |
---|
| 294 | delete ed.selectSameEditor; |
---|
| 295 | }).defer(50); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | }, |
---|
| 299 | |
---|
| 300 | // private |
---|
| 301 | preEditValue : function(r, field){ |
---|
| 302 | var value = r.data[field]; |
---|
| 303 | return this.autoEncode && Ext.isString(value) ? Ext.util.Format.htmlDecode(value) : value; |
---|
| 304 | }, |
---|
| 305 | |
---|
| 306 | // private |
---|
| 307 | postEditValue : function(value, originalValue, r, field){ |
---|
| 308 | return this.autoEncode && Ext.isString(value) ? Ext.util.Format.htmlEncode(value) : value; |
---|
| 309 | }, |
---|
| 310 | |
---|
| 311 | /** |
---|
| 312 | * Stops any active editing |
---|
| 313 | * @param {Boolean} cancel (optional) True to cancel any changes |
---|
| 314 | */ |
---|
| 315 | stopEditing : function(cancel){ |
---|
| 316 | if(this.editing){ |
---|
| 317 | // Store the lastActiveEditor to check if it is changing |
---|
| 318 | var ae = this.lastActiveEditor = this.activeEditor; |
---|
| 319 | if(ae){ |
---|
| 320 | ae[cancel === true ? 'cancelEdit' : 'completeEdit'](); |
---|
| 321 | this.view.focusCell(ae.row, ae.col); |
---|
| 322 | } |
---|
| 323 | this.activeEditor = null; |
---|
| 324 | } |
---|
| 325 | this.editing = false; |
---|
| 326 | } |
---|
| 327 | }); |
---|
| 328 | Ext.reg('editorgrid', Ext.grid.EditorGridPanel); |
---|