4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../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">
19 <span id='Ext-grid-plugin-Editing'>/**
20 </span> * @class Ext.grid.plugin.Editing
22 This class provides an abstract grid editing plugin on selected {@link Ext.grid.column.Column columns}.
23 The editable columns are specified by providing an {@link Ext.grid.column.Column#editor editor}
24 in the {@link Ext.grid.column.Column column configuration}.
26 *Note:* This class should not be used directly. See {@link Ext.grid.plugin.CellEditing} and
27 {@link Ext.grid.plugin.RowEditing}.
31 Ext.define('Ext.grid.plugin.Editing', {
32 alias: 'editing.editing',
35 'Ext.grid.column.Column',
40 observable: 'Ext.util.Observable'
43 <span id='Ext-grid-plugin-Editing-cfg-clicksToEdit'> /**
44 </span> * @cfg {Number} clicksToEdit
45 * The number of clicks on a grid required to display the editor (defaults to 2).
50 defaultFieldXType: 'textfield',
55 constructor: function(config) {
57 Ext.apply(me, config);
60 // Doc'ed in separate editing plugins
63 // Doc'ed in separate editing plugins
66 // Doc'ed in separate editing plugins
69 me.mixins.observable.constructor.call(me);
70 // TODO: Deprecated, remove in 5.0
71 me.relayEvents(me, ['afteredit'], 'after');
75 init: function(grid) {
81 me.mon(grid, 'reconfigure', me.onReconfigure, me);
84 grid.relayEvents(me, ['beforeedit', 'edit', 'validateedit']);
85 // Marks the grid as editable, so that the SelectionModel
86 // can make appropriate decisions during navigation
87 grid.isEditable = true;
88 grid.editingPlugin = grid.view.editingPlugin = me;
91 <span id='Ext-grid-plugin-Editing-method-onReconfigure'> /**
92 </span> * Fires after the grid is reconfigured
95 onReconfigure: function(){
96 this.initFieldAccessors(this.view.getGridColumns());
99 <span id='Ext-grid-plugin-Editing-method-destroy'> /**
101 * AbstractComponent calls destroy on all its plugins at destroy time.
103 destroy: function() {
106 headerCt = grid.headerCt,
107 events = grid.events;
109 Ext.destroy(me.keyNav);
110 me.removeFieldAccessors(grid.getView().getGridColumns());
112 // Clear all listeners from all our events, clear all managed listeners we added to other Observables
115 delete me.grid.editingPlugin;
116 delete me.grid.view.editingPlugin;
124 getEditStyle: function() {
125 return this.editStyle;
129 initFieldAccessors: function(column) {
132 if (Ext.isArray(column)) {
133 Ext.Array.forEach(column, me.initFieldAccessors, me);
137 // Augment the Header class to have a getEditor and setEditor method
138 // Important: Only if the header does not have its own implementation.
139 Ext.applyIf(column, {
140 getEditor: function(record, defaultField) {
141 return me.getColumnField(this, defaultField);
144 setEditor: function(field) {
145 me.setColumnField(this, field);
151 removeFieldAccessors: function(column) {
154 if (Ext.isArray(column)) {
155 Ext.Array.forEach(column, me.removeFieldAccessors, me);
159 delete column.getEditor;
160 delete column.setEditor;
164 // remaps to the public API of Ext.grid.column.Column.getEditor
165 getColumnField: function(columnHeader, defaultField) {
166 var field = columnHeader.field;
168 if (!field && columnHeader.editor) {
169 field = columnHeader.editor;
170 delete columnHeader.editor;
173 if (!field && defaultField) {
174 field = defaultField;
178 if (Ext.isString(field)) {
179 field = { xtype: field };
181 if (Ext.isObject(field) && !field.isFormField) {
182 field = Ext.ComponentManager.create(field, this.defaultFieldXType);
183 columnHeader.field = field;
187 name: columnHeader.dataIndex
195 // remaps to the public API of Ext.grid.column.Column.setEditor
196 setColumnField: function(column, field) {
197 if (Ext.isObject(field) && !field.isFormField) {
198 field = Ext.ComponentManager.create(field, this.defaultFieldXType);
200 column.field = field;
204 initEvents: function() {
206 me.initEditTriggers();
207 me.initCancelTriggers();
211 initCancelTriggers: Ext.emptyFn,
214 initEditTriggers: function() {
217 clickEvent = me.clicksToEdit === 1 ? 'click' : 'dblclick';
220 me.mon(view, 'cell' + clickEvent, me.startEditByClick, me);
221 view.on('render', function() {
222 me.keyNav = Ext.create('Ext.util.KeyNav', view.el, {
223 enter: me.onEnterKey,
227 }, me, { single: true });
231 onEnterKey: function(e) {
234 selModel = grid.getSelectionModel(),
236 columnHeader = grid.headerCt.getHeaderAtIndex(0);
238 // Calculate editing start position from SelectionModel
239 // CellSelectionModel
240 if (selModel.getCurrentPosition) {
241 pos = selModel.getCurrentPosition();
242 record = grid.store.getAt(pos.row);
243 columnHeader = grid.headerCt.getHeaderAtIndex(pos.column);
247 record = selModel.getLastSelected();
249 me.startEdit(record, columnHeader);
253 onEscKey: function(e) {
258 startEditByClick: function(view, cell, colIdx, record, row, rowIdx, e) {
259 this.startEdit(record, view.getHeaderAtIndex(colIdx));
262 <span id='Ext-grid-plugin-Editing-property-beforeEdit'> /**
264 * @abstract. Template method called before editing begins.
265 * @param {Object} context The current editing context
266 * @return {Boolean} Return false to cancel the editing process
268 beforeEdit: Ext.emptyFn,
270 <span id='Ext-grid-plugin-Editing-method-startEdit'> /**
271 </span> * Start editing the specified record, using the specified Column definition to define which field is being edited.
272 * @param {Model} record The Store data record which backs the row to be edited.
273 * @param {Model} columnHeader The Column object defining the column to be edited.
275 startEdit: function(record, columnHeader) {
277 context = me.getEditingContext(record, columnHeader);
279 if (me.beforeEdit(context) === false || me.fireEvent('beforeedit', context) === false || context.cancel) {
283 me.context = context;
287 <span id='Ext-grid-plugin-Editing-method-getEditingContext'> /**
288 </span> * @private Collects all information necessary for any subclasses to perform their editing functions.
290 * @param columnHeader
291 * @returns {Object} The editing context based upon the passed record and column
293 getEditingContext: function(record, columnHeader) {
299 view = grid.getView(),
302 // If they'd passed numeric row, column indices, look them up.
303 if (Ext.isNumber(record)) {
305 record = store.getAt(rowIdx);
307 rowIdx = store.indexOf(record);
309 if (Ext.isNumber(columnHeader)) {
310 colIdx = columnHeader;
311 columnHeader = grid.headerCt.getHeaderAtIndex(colIdx);
313 colIdx = columnHeader.getIndex();
316 value = record.get(columnHeader.dataIndex);
320 field: columnHeader.dataIndex,
322 row: view.getNode(rowIdx),
323 column: columnHeader,
329 <span id='Ext-grid-plugin-Editing-method-cancelEdit'> /**
330 </span> * Cancel any active edit that is in progress.
332 cancelEdit: function() {
333 this.editing = false;
336 <span id='Ext-grid-plugin-Editing-method-completeEdit'> /**
337 </span> * Complete the edit if there is an active edit in progress.
339 completeEdit: function() {
342 if (me.editing && me.validateEdit()) {
343 me.fireEvent('edit', me.context);
351 validateEdit: function() {
353 context = me.context;
355 return me.fireEvent('validateedit', me, context) !== false && !context.cancel;