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.initFieldAccessors(me.view.getGridColumns());
83 grid.relayEvents(me, ['beforeedit', 'edit', 'validateedit']);
84 // Marks the grid as editable, so that the SelectionModel
85 // can make appropriate decisions during navigation
86 grid.isEditable = true;
87 grid.editingPlugin = grid.view.editingPlugin = me;
90 <span id='Ext-grid-plugin-Editing-method-destroy'> /**
92 * AbstractComponent calls destroy on all its plugins at destroy time.
97 headerCt = grid.headerCt,
100 Ext.destroy(me.keyNav);
101 me.removeFieldAccessors(grid.getView().getGridColumns());
103 // Clear all listeners from all our events, clear all managed listeners we added to other Observables
106 delete me.grid.editingPlugin;
107 delete me.grid.view.editingPlugin;
115 getEditStyle: function() {
116 return this.editStyle;
120 initFieldAccessors: function(column) {
123 if (Ext.isArray(column)) {
124 Ext.Array.forEach(column, me.initFieldAccessors, me);
128 // Augment the Header class to have a getEditor and setEditor method
129 // Important: Only if the header does not have its own implementation.
130 Ext.applyIf(column, {
131 getEditor: function(record, defaultField) {
132 return me.getColumnField(this, defaultField);
135 setEditor: function(field) {
136 me.setColumnField(this, field);
142 removeFieldAccessors: function(column) {
145 if (Ext.isArray(column)) {
146 Ext.Array.forEach(column, me.removeFieldAccessors, me);
150 delete column.getEditor;
151 delete column.setEditor;
155 // remaps to the public API of Ext.grid.column.Column.getEditor
156 getColumnField: function(columnHeader, defaultField) {
157 var field = columnHeader.field;
159 if (!field && columnHeader.editor) {
160 field = columnHeader.editor;
161 delete columnHeader.editor;
164 if (!field && defaultField) {
165 field = defaultField;
169 if (Ext.isString(field)) {
170 field = { xtype: field };
172 if (Ext.isObject(field) && !field.isFormField) {
173 field = Ext.ComponentManager.create(field, this.defaultFieldXType);
174 columnHeader.field = field;
178 name: columnHeader.dataIndex
186 // remaps to the public API of Ext.grid.column.Column.setEditor
187 setColumnField: function(column, field) {
188 if (Ext.isObject(field) && !field.isFormField) {
189 field = Ext.ComponentManager.create(field, this.defaultFieldXType);
191 column.field = field;
195 initEvents: function() {
197 me.initEditTriggers();
198 me.initCancelTriggers();
202 initCancelTriggers: Ext.emptyFn,
205 initEditTriggers: function() {
208 clickEvent = me.clicksToEdit === 1 ? 'click' : 'dblclick';
211 me.mon(view, 'cell' + clickEvent, me.startEditByClick, me);
212 view.on('render', function() {
213 me.keyNav = Ext.create('Ext.util.KeyNav', view.el, {
214 enter: me.onEnterKey,
218 }, me, { single: true });
222 onEnterKey: function(e) {
225 selModel = grid.getSelectionModel(),
227 columnHeader = grid.headerCt.getHeaderAtIndex(0);
229 // Calculate editing start position from SelectionModel
230 // CellSelectionModel
231 if (selModel.getCurrentPosition) {
232 pos = selModel.getCurrentPosition();
233 record = grid.store.getAt(pos.row);
234 columnHeader = grid.headerCt.getHeaderAtIndex(pos.column);
238 record = selModel.getLastSelected();
240 me.startEdit(record, columnHeader);
244 onEscKey: function(e) {
249 startEditByClick: function(view, cell, colIdx, record, row, rowIdx, e) {
250 this.startEdit(record, view.getHeaderAtIndex(colIdx));
253 <span id='Ext-grid-plugin-Editing-property-beforeEdit'> /**
255 * @abstract. Template method called before editing begins.
256 * @param {Object} context The current editing context
257 * @return {Boolean} Return false to cancel the editing process
259 beforeEdit: Ext.emptyFn,
261 <span id='Ext-grid-plugin-Editing-method-startEdit'> /**
262 </span> * Start editing the specified record, using the specified Column definition to define which field is being edited.
263 * @param {Model} record The Store data record which backs the row to be edited.
264 * @param {Model} columnHeader The Column object defining the column to be edited.
266 startEdit: function(record, columnHeader) {
268 context = me.getEditingContext(record, columnHeader);
270 if (me.beforeEdit(context) === false || me.fireEvent('beforeedit', context) === false || context.cancel) {
274 me.context = context;
278 <span id='Ext-grid-plugin-Editing-method-getEditingContext'> /**
279 </span> * @private Collects all information necessary for any subclasses to perform their editing functions.
281 * @param columnHeader
282 * @returns {Object} The editing context based upon the passed record and column
284 getEditingContext: function(record, columnHeader) {
290 view = grid.getView(),
293 // If they'd passed numeric row, column indices, look them up.
294 if (Ext.isNumber(record)) {
296 record = store.getAt(rowIdx);
298 rowIdx = store.indexOf(record);
300 if (Ext.isNumber(columnHeader)) {
301 colIdx = columnHeader;
302 columnHeader = grid.headerCt.getHeaderAtIndex(colIdx);
304 colIdx = columnHeader.getIndex();
307 value = record.get(columnHeader.dataIndex);
311 field: columnHeader.dataIndex,
313 row: view.getNode(rowIdx),
314 column: columnHeader,
320 <span id='Ext-grid-plugin-Editing-method-cancelEdit'> /**
321 </span> * Cancel any active edit that is in progress.
323 cancelEdit: function() {
324 this.editing = false;
327 <span id='Ext-grid-plugin-Editing-method-completeEdit'> /**
328 </span> * Complete the edit if there is an active edit in progress.
330 completeEdit: function() {
333 if (me.editing && me.validateEdit()) {
334 me.fireEvent('edit', me.context);
342 validateEdit: function() {
344 context = me.context;
346 return me.fireEvent('validateedit', me, context) !== false && !context.cancel;