4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-grid-plugin-Editing'>/**
19 </span> * This class provides an abstract grid editing plugin on selected {@link Ext.grid.column.Column columns}.
20 * The editable columns are specified by providing an {@link Ext.grid.column.Column#editor editor}
21 * in the {@link Ext.grid.column.Column column configuration}.
23 * **Note:** This class should not be used directly. See {@link Ext.grid.plugin.CellEditing} and
24 * {@link Ext.grid.plugin.RowEditing}.
26 Ext.define('Ext.grid.plugin.Editing', {
27 alias: 'editing.editing',
30 'Ext.grid.column.Column',
35 observable: 'Ext.util.Observable'
38 <span id='Ext-grid-plugin-Editing-cfg-clicksToEdit'> /**
39 </span> * @cfg {Number} clicksToEdit
40 * The number of clicks on a grid required to display the editor.
45 defaultFieldXType: 'textfield',
50 constructor: function(config) {
52 Ext.apply(me, config);
55 // Doc'ed in separate editing plugins
58 // Doc'ed in separate editing plugins
61 // Doc'ed in separate editing plugins
64 me.mixins.observable.constructor.call(me);
65 // TODO: Deprecated, remove in 5.0
66 me.relayEvents(me, ['afteredit'], 'after');
70 init: function(grid) {
76 me.mon(grid, 'reconfigure', me.onReconfigure, me);
79 grid.relayEvents(me, ['beforeedit', 'edit', 'validateedit']);
80 // Marks the grid as editable, so that the SelectionModel
81 // can make appropriate decisions during navigation
82 grid.isEditable = true;
83 grid.editingPlugin = grid.view.editingPlugin = me;
86 <span id='Ext-grid-plugin-Editing-method-onReconfigure'> /**
87 </span> * Fires after the grid is reconfigured
90 onReconfigure: function(){
91 this.initFieldAccessors(this.view.getGridColumns());
94 <span id='Ext-grid-plugin-Editing-method-destroy'> /**
96 * AbstractComponent calls destroy on all its plugins at destroy time.
101 headerCt = grid.headerCt,
102 events = grid.events;
104 Ext.destroy(me.keyNav);
105 me.removeFieldAccessors(grid.getView().getGridColumns());
107 // Clear all listeners from all our events, clear all managed listeners we added to other Observables
110 delete me.grid.editingPlugin;
111 delete me.grid.view.editingPlugin;
119 getEditStyle: function() {
120 return this.editStyle;
124 initFieldAccessors: function(column) {
127 if (Ext.isArray(column)) {
128 Ext.Array.forEach(column, me.initFieldAccessors, me);
132 // Augment the Header class to have a getEditor and setEditor method
133 // Important: Only if the header does not have its own implementation.
134 Ext.applyIf(column, {
135 getEditor: function(record, defaultField) {
136 return me.getColumnField(this, defaultField);
139 setEditor: function(field) {
140 me.setColumnField(this, field);
146 removeFieldAccessors: function(column) {
149 if (Ext.isArray(column)) {
150 Ext.Array.forEach(column, me.removeFieldAccessors, me);
154 delete column.getEditor;
155 delete column.setEditor;
159 // remaps to the public API of Ext.grid.column.Column.getEditor
160 getColumnField: function(columnHeader, defaultField) {
161 var field = columnHeader.field;
163 if (!field && columnHeader.editor) {
164 field = columnHeader.editor;
165 delete columnHeader.editor;
168 if (!field && defaultField) {
169 field = defaultField;
173 if (Ext.isString(field)) {
174 field = { xtype: field };
176 if (Ext.isObject(field) && !field.isFormField) {
177 field = Ext.ComponentManager.create(field, this.defaultFieldXType);
178 columnHeader.field = field;
182 name: columnHeader.dataIndex
190 // remaps to the public API of Ext.grid.column.Column.setEditor
191 setColumnField: function(column, field) {
192 if (Ext.isObject(field) && !field.isFormField) {
193 field = Ext.ComponentManager.create(field, this.defaultFieldXType);
195 column.field = field;
199 initEvents: function() {
201 me.initEditTriggers();
202 me.initCancelTriggers();
206 initCancelTriggers: Ext.emptyFn,
209 initEditTriggers: function() {
212 clickEvent = me.clicksToEdit === 1 ? 'click' : 'dblclick';
215 me.mon(view, 'cell' + clickEvent, me.startEditByClick, me);
216 view.on('render', function() {
217 me.keyNav = Ext.create('Ext.util.KeyNav', view.el, {
218 enter: me.onEnterKey,
222 }, me, { single: true });
226 onEnterKey: function(e) {
229 selModel = grid.getSelectionModel(),
231 columnHeader = grid.headerCt.getHeaderAtIndex(0);
233 // Calculate editing start position from SelectionModel
234 // CellSelectionModel
235 if (selModel.getCurrentPosition) {
236 pos = selModel.getCurrentPosition();
237 record = grid.store.getAt(pos.row);
238 columnHeader = grid.headerCt.getHeaderAtIndex(pos.column);
242 record = selModel.getLastSelected();
244 me.startEdit(record, columnHeader);
248 onEscKey: function(e) {
253 startEditByClick: function(view, cell, colIdx, record, row, rowIdx, e) {
254 this.startEdit(record, view.getHeaderAtIndex(colIdx));
257 <span id='Ext-grid-plugin-Editing-property-beforeEdit'> /**
260 * Template method called before editing begins.
261 * @param {Object} context The current editing context
262 * @return {Boolean} Return false to cancel the editing process
264 beforeEdit: Ext.emptyFn,
266 <span id='Ext-grid-plugin-Editing-method-startEdit'> /**
267 </span> * Starts editing the specified record, using the specified Column definition to define which field is being edited.
268 * @param {Ext.data.Model/Number} record The Store data record which backs the row to be edited, or index of the record in Store.
269 * @param {Ext.grid.column.Column/Number} columnHeader The Column object defining the column to be edited, or index of the column.
271 startEdit: function(record, columnHeader) {
273 context = me.getEditingContext(record, columnHeader);
275 if (me.beforeEdit(context) === false || me.fireEvent('beforeedit', context) === false || context.cancel) {
279 me.context = context;
283 <span id='Ext-grid-plugin-Editing-method-getEditingContext'> /**
285 * Collects all information necessary for any subclasses to perform their editing functions.
287 * @param columnHeader
288 * @returns {Object} The editing context based upon the passed record and column
290 getEditingContext: function(record, columnHeader) {
296 view = grid.getView(),
299 // If they'd passed numeric row, column indices, look them up.
300 if (Ext.isNumber(record)) {
302 record = store.getAt(rowIdx);
304 rowIdx = store.indexOf(record);
306 if (Ext.isNumber(columnHeader)) {
307 colIdx = columnHeader;
308 columnHeader = grid.headerCt.getHeaderAtIndex(colIdx);
310 colIdx = columnHeader.getIndex();
313 value = record.get(columnHeader.dataIndex);
317 field: columnHeader.dataIndex,
319 row: view.getNode(rowIdx),
320 column: columnHeader,
326 <span id='Ext-grid-plugin-Editing-method-cancelEdit'> /**
327 </span> * Cancels any active edit that is in progress.
329 cancelEdit: function() {
330 this.editing = false;
333 <span id='Ext-grid-plugin-Editing-method-completeEdit'> /**
334 </span> * Completes the edit if there is an active edit in progress.
336 completeEdit: function() {
339 if (me.editing && me.validateEdit()) {
340 me.fireEvent('edit', me.context);
348 validateEdit: function() {
350 context = me.context;
352 return me.fireEvent('validateedit', me, context) !== false && !context.cancel;