Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / grid / plugin / RowEditing.js
1 /**
2  * @class Ext.grid.plugin.RowEditing
3  * @extends Ext.grid.plugin.Editing
4  * 
5  * The Ext.grid.plugin.RowEditing plugin injects editing at a row level for a Grid. When editing begins,
6  * a small floating dialog will be shown for the appropriate row. Each editable column will show a field
7  * for editing. There is a button to save or cancel all changes for the edit.
8  * 
9  * The field that will be used for the editor is defined at the
10  * {@link Ext.grid.column.Column#field field}. The editor can be a field instance or a field configuration.
11  * If an editor is not specified for a particular column then that column won't be editable and the value of
12  * the column will be displayed.
13  *
14  * The editor may be shared for each column in the grid, or a different one may be specified for each column.
15  * An appropriate field type should be chosen to match the data structure that it will be editing. For example,
16  * to edit a date, it would be useful to specify {@link Ext.form.field.Date} as the editor.
17  * 
18  * {@img Ext.grid.plugin.RowEditing/Ext.grid.plugin.RowEditing.png Ext.grid.plugin.RowEditing plugin}
19  *
20  * ## Example Usage
21  *
22  *     Ext.create('Ext.data.Store', {
23  *         storeId:'simpsonsStore',
24  *         fields:['name', 'email', 'phone'],
25  *         data:{'items':[
26  *             {"name":"Lisa", "email":"lisa@simpsons.com", "phone":"555-111-1224"},
27  *             {"name":"Bart", "email":"bart@simpsons.com", "phone":"555--222-1234"},
28  *             {"name":"Homer", "email":"home@simpsons.com", "phone":"555-222-1244"},                        
29  *             {"name":"Marge", "email":"marge@simpsons.com", "phone":"555-222-1254"}            
30  *         ]},
31  *         proxy: {
32  *             type: 'memory',
33  *             reader: {
34  *                 type: 'json',
35  *                 root: 'items'
36  *             }
37  *         }
38  *     });
39  *     
40  *     Ext.create('Ext.grid.Panel', {
41  *         title: 'Simpsons',
42  *         store: Ext.data.StoreManager.lookup('simpsonsStore'),
43  *         columns: [
44  *             {header: 'Name',  dataIndex: 'name', field: 'textfield'},
45  *             {header: 'Email', dataIndex: 'email', flex:1, 
46  *                 editor: {
47  *                     xtype:'textfield',
48  *                     allowBlank:false
49  *                 }
50  *             },
51  *             {header: 'Phone', dataIndex: 'phone'}
52  *         ],
53  *         selType: 'rowmodel',
54  *         plugins: [
55  *             Ext.create('Ext.grid.plugin.RowEditing', {
56  *                 clicksToEdit: 1
57  *             })
58  *         ],
59  *         height: 200,
60  *         width: 400,
61  *         renderTo: Ext.getBody()
62  *     });
63  */
64 Ext.define('Ext.grid.plugin.RowEditing', {
65     extend: 'Ext.grid.plugin.Editing',
66     alias: 'plugin.rowediting',
67
68     requires: [
69         'Ext.grid.RowEditor'
70     ],
71
72     editStyle: 'row',
73
74     /**
75      * @cfg {Boolean} autoCancel
76      * `true` to automatically cancel any pending changes when the row editor begins editing a new row.
77      * `false` to force the user to explicitly cancel the pending changes. Defaults to `true`.
78      * @markdown
79      */
80     autoCancel: true,
81
82     /**
83      * @cfg {Number} clicksToMoveEditor
84      * The number of clicks to move the row editor to a new row while it is visible and actively editing another row.
85      * This will default to the same value as {@link Ext.grid.plugin.Editing#clicksToEdit clicksToEdit}.
86      * @markdown
87      */
88
89     /**
90      * @cfg {Boolean} errorSummary
91      * `true` to show a {@link Ext.tip.ToolTip tooltip} that summarizes all validation errors present
92      * in the row editor. Set to `false` to prevent the tooltip from showing. Defaults to `true`.
93      * @markdown
94      */
95     errorSummary: true,
96
97     /**
98      * @event beforeedit
99      * Fires before row editing is triggered. The edit event object has the following properties <br />
100      * <ul style="padding:5px;padding-left:16px;">
101      * <li>grid - The grid this editor is on</li>
102      * <li>view - The grid view</li>
103      * <li>store - The grid store</li>
104      * <li>record - The record being edited</li>
105      * <li>row - The grid table row</li>
106      * <li>column - The grid {@link Ext.grid.column.Column Column} defining the column that initiated the edit</li>
107      * <li>rowIdx - The row index that is being edited</li>
108      * <li>colIdx - The column index that initiated the edit</li>
109      * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
110      * </ul>
111      * @param {Ext.grid.plugin.Editing} editor
112      * @param {Object} e An edit event (see above for description)
113      */
114     /**
115      * @event edit
116      * Fires after a row is edited. The edit event object has the following properties <br />
117      * <ul style="padding:5px;padding-left:16px;">
118      * <li>grid - The grid this editor is on</li>
119      * <li>view - The grid view</li>
120      * <li>store - The grid store</li>
121      * <li>record - The record being edited</li>
122      * <li>row - The grid table row</li>
123      * <li>column - The grid {@link Ext.grid.column.Column Column} defining the column that initiated the edit</li>
124      * <li>rowIdx - The row index that is being edited</li>
125      * <li>colIdx - The column index that initiated the edit</li>
126      * </ul>
127      *
128      * <pre><code>
129 grid.on('edit', onEdit, this);
130
131 function onEdit(e) {
132     // execute an XHR to send/commit data to the server, in callback do (if successful):
133     e.record.commit();
134 };
135      * </code></pre>
136      * @param {Ext.grid.plugin.Editing} editor
137      * @param {Object} e An edit event (see above for description)
138      */
139     /**
140      * @event validateedit
141      * Fires after a cell is edited, but before the value is set in the record. Return false
142      * to cancel the change. The edit event object has the following properties <br />
143      * <ul style="padding:5px;padding-left:16px;">
144      * <li>grid - The grid this editor is on</li>
145      * <li>view - The grid view</li>
146      * <li>store - The grid store</li>
147      * <li>record - The record being edited</li>
148      * <li>row - The grid table row</li>
149      * <li>column - The grid {@link Ext.grid.column.Column Column} defining the column that initiated the edit</li>
150      * <li>rowIdx - The row index that is being edited</li>
151      * <li>colIdx - The column index that initiated the edit</li>
152      * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
153      * </ul>
154      * Usage example showing how to remove the red triangle (dirty record indicator) from some
155      * records (not all).  By observing the grid's validateedit event, it can be cancelled if
156      * the edit occurs on a targeted row (for example) and then setting the field's new value
157      * in the Record directly:
158      * <pre><code>
159 grid.on('validateedit', function(e) {
160   var myTargetRow = 6;
161
162   if (e.rowIdx == myTargetRow) {
163     e.cancel = true;
164     e.record.data[e.field] = e.value;
165   }
166 });
167      * </code></pre>
168      * @param {Ext.grid.plugin.Editing} editor
169      * @param {Object} e An edit event (see above for description)
170      */
171
172     constructor: function() {
173         var me = this;
174         me.callParent(arguments);
175
176         if (!me.clicksToMoveEditor) {
177             me.clicksToMoveEditor = me.clicksToEdit;
178         }
179
180         me.autoCancel = !!me.autoCancel;
181     },
182
183     /**
184      * @private
185      * AbstractComponent calls destroy on all its plugins at destroy time.
186      */
187     destroy: function() {
188         var me = this;
189         Ext.destroy(me.editor);
190         me.callParent(arguments);
191     },
192
193     /**
194      * Start editing the specified record, using the specified Column definition to define which field is being edited.
195      * @param {Model} record The Store data record which backs the row to be edited.
196      * @param {Model} columnHeader The Column object defining the column to be edited.
197      * @override
198      */
199     startEdit: function(record, columnHeader) {
200         var me = this,
201             editor = me.getEditor();
202
203         if (me.callParent(arguments) === false) {
204             return false;
205         }
206
207         // Fire off our editor
208         if (editor.beforeEdit() !== false) {
209             editor.startEdit(me.context.record, me.context.column);
210         }
211     },
212
213     // private
214     cancelEdit: function() {
215         var me = this;
216
217         if (me.editing) {
218             me.getEditor().cancelEdit();
219             me.callParent(arguments);
220         }
221     },
222
223     // private
224     completeEdit: function() {
225         var me = this;
226
227         if (me.editing && me.validateEdit()) {
228             me.editing = false;
229             me.fireEvent('edit', me.context);
230         }
231     },
232
233     // private
234     validateEdit: function() {
235         var me = this;
236         return me.callParent(arguments) && me.getEditor().completeEdit();
237     },
238
239     // private
240     getEditor: function() {
241         var me = this;
242
243         if (!me.editor) {
244             me.editor = me.initEditor();
245         }
246         return me.editor;
247     },
248
249     // private
250     initEditor: function() {
251         var me = this,
252             grid = me.grid,
253             view = me.view,
254             headerCt = grid.headerCt;
255
256         return Ext.create('Ext.grid.RowEditor', {
257             autoCancel: me.autoCancel,
258             errorSummary: me.errorSummary,
259             fields: headerCt.getGridColumns(),
260             hidden: true,
261
262             // keep a reference..
263             editingPlugin: me,
264             renderTo: view.el
265         });
266     },
267
268     // private
269     initEditTriggers: function() {
270         var me = this,
271             grid = me.grid,
272             view = me.view,
273             headerCt = grid.headerCt,
274             moveEditorEvent = me.clicksToMoveEditor === 1 ? 'click' : 'dblclick';
275
276         me.callParent(arguments);
277
278         if (me.clicksToMoveEditor !== me.clicksToEdit) {
279             me.mon(view, 'cell' + moveEditorEvent, me.moveEditorByClick, me);
280         }
281
282         view.on('render', function() {
283             // Column events
284             me.mon(headerCt, {
285                 add: me.onColumnAdd,
286                 remove: me.onColumnRemove,
287                 columnresize: me.onColumnResize,
288                 columnhide: me.onColumnHide,
289                 columnshow: me.onColumnShow,
290                 columnmove: me.onColumnMove,
291                 scope: me
292             });
293         }, me, { single: true });
294     },
295
296     startEditByClick: function() {
297         var me = this;
298         if (!me.editing || me.clicksToMoveEditor === me.clicksToEdit) {
299             me.callParent(arguments);
300         }
301     },
302
303     moveEditorByClick: function() {
304         var me = this;
305         if (me.editing) {
306             me.superclass.startEditByClick.apply(me, arguments);
307         }
308     },
309
310     // private
311     onColumnAdd: function(ct, column) {
312         if (column.isHeader) {
313             var me = this,
314                 editor;
315             
316             me.initFieldAccessors(column);
317             editor = me.getEditor();
318             
319             if (editor && editor.onColumnAdd) {
320                 editor.onColumnAdd(column);
321             }
322         }
323     },
324
325     // private
326     onColumnRemove: function(ct, column) {
327         if (column.isHeader) {
328             var me = this,
329                 editor = me.getEditor();
330     
331             if (editor && editor.onColumnRemove) {
332                 editor.onColumnRemove(column);
333             }
334             me.removeFieldAccessors(column);  
335         }
336     },
337
338     // private
339     onColumnResize: function(ct, column, width) {
340         if (column.isHeader) {
341             var me = this,
342                 editor = me.getEditor();
343     
344             if (editor && editor.onColumnResize) {
345                 editor.onColumnResize(column, width);
346             }
347         }
348     },
349
350     // private
351     onColumnHide: function(ct, column) {
352         // no isHeader check here since its already a columnhide event.
353         var me = this,
354             editor = me.getEditor();
355
356         if (editor && editor.onColumnHide) {
357             editor.onColumnHide(column);
358         }
359     },
360
361     // private
362     onColumnShow: function(ct, column) {
363         // no isHeader check here since its already a columnshow event.
364         var me = this,
365             editor = me.getEditor();
366
367         if (editor && editor.onColumnShow) {
368             editor.onColumnShow(column);
369         }
370     },
371
372     // private
373     onColumnMove: function(ct, column, fromIdx, toIdx) {
374         // no isHeader check here since its already a columnmove event.
375         var me = this,
376             editor = me.getEditor();
377
378         if (editor && editor.onColumnMove) {
379             editor.onColumnMove(column, fromIdx, toIdx);
380         }
381     },
382
383     // private
384     setColumnField: function(column, field) {
385         var me = this;
386         me.callParent(arguments);
387         me.getEditor().setField(column.field, column);
388     }
389 });