3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
10 * @class Ext.ux.grid.RowEditor
12 * Plugin (ptype = 'roweditor') that adds the ability to rapidly edit full rows in a grid.
13 * A validation mode may be enabled which uses AnchorTips to notify the user of all
14 * validation errors at once.
18 Ext.ux.grid.RowEditor = Ext.extend(Ext.Panel, {
22 cls: 'x-small-editor',
23 buttonAlign: 'center',
24 baseCls: 'x-row-editor',
25 elements: 'header,footer,body',
35 commitChangesText: 'You need to commit or cancel your changes',
42 initComponent: function(){
43 Ext.ux.grid.RowEditor.superclass.initComponent.call(this);
47 * Fired before the row editor is activated.
48 * If the listener returns <tt>false</tt> the editor will not be activated.
49 * @param {Ext.ux.grid.RowEditor} roweditor This object
50 * @param {Number} rowIndex The rowIndex of the row just edited
55 * Fired when the editor is cancelled.
56 * @param {Ext.ux.grid.RowEditor} roweditor This object
57 * @param {Boolean} forced True if the cancel button is pressed, false is the editor was invalid.
62 * Fired after a row is edited and passes validation.
63 * If the listener returns <tt>false</tt> changes to the record will not be set.
64 * @param {Ext.ux.grid.RowEditor} roweditor This object
65 * @param {Object} changes Object with changes made to the record.
66 * @param {Ext.data.Record} r The Record that was edited.
67 * @param {Number} rowIndex The rowIndex of the row just edited
72 * Fired after a row is edited and passes validation. This event is fired
73 * after the store's update event is fired with this edit.
74 * @param {Ext.ux.grid.RowEditor} roweditor This object
75 * @param {Object} changes Object with changes made to the record.
76 * @param {Ext.data.Record} r The Record that was edited.
77 * @param {Number} rowIndex The rowIndex of the row just edited
86 if(this.clicksToEdit === 2){
87 grid.on('rowdblclick', this.onRowDblClick, this);
89 grid.on('rowclick', this.onRowClick, this);
91 grid.on('rowdblclick', this.onRowDblClick, this);
95 // stopEditing without saving when a record is removed from Store.
96 grid.getStore().on('remove', function() {
97 this.stopEditing(false);
102 keydown: this.onGridKey,
103 columnresize: this.verifyLayout,
104 columnmove: this.refreshFields,
105 reconfigure: this.refreshFields,
106 beforedestroy : this.beforedestroy,
107 destroy : this.destroy,
110 fn: this.positionButtons
113 grid.getColumnModel().on('hiddenchange', this.verifyLayout, this, {delay:1});
114 grid.getView().on('refresh', this.stopEditing.createDelegate(this, []));
117 beforedestroy: function() {
118 this.stopMonitoring();
119 this.grid.getStore().un('remove', this.onStoreRemove, this);
120 this.stopEditing(false);
121 Ext.destroy(this.btns, this.tooltip);
124 refreshFields: function(){
131 this.items.each(function(f){
132 if(String(this.values[f.id]) !== String(f.getValue())){
140 startEditing: function(rowIndex, doFocus){
141 if(this.editing && this.isDirty()){
142 this.showTooltip(this.commitChangesText);
145 if(Ext.isObject(rowIndex)){
146 rowIndex = this.grid.getStore().indexOf(rowIndex);
148 if(this.fireEvent('beforeedit', this, rowIndex) !== false){
150 var g = this.grid, view = g.getView(),
151 row = view.getRow(rowIndex),
152 record = g.store.getAt(rowIndex);
154 this.record = record;
155 this.rowIndex = rowIndex;
158 this.render(view.getEditorParent());
160 var w = Ext.fly(row).getWidth();
162 if(!this.initialized){
165 var cm = g.getColumnModel(), fields = this.items.items, f, val;
166 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
167 val = this.preEditValue(record, cm.getDataIndex(i));
170 this.values[f.id] = Ext.isEmpty(val) ? '' : val;
172 this.verifyLayout(true);
173 if(!this.isVisible()){
174 this.setPagePosition(Ext.fly(row).getXY());
176 this.el.setXY(Ext.fly(row).getXY(), {duration:0.15});
178 if(!this.isVisible()){
179 this.show().doLayout();
181 if(doFocus !== false){
182 this.doFocus.defer(this.focusDelay, this);
187 stopEditing : function(saveChanges){
188 this.editing = false;
189 if(!this.isVisible()){
192 if(saveChanges === false || !this.isValid()){
194 this.fireEvent('canceledit', this, saveChanges === false);
200 cm = this.grid.colModel,
201 fields = this.items.items;
202 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
204 var dindex = cm.getDataIndex(i);
205 if(!Ext.isEmpty(dindex)){
206 var oldValue = r.data[dindex],
207 value = this.postEditValue(fields[i].getValue(), oldValue, r, dindex);
208 if(String(oldValue) !== String(value)){
209 changes[dindex] = value;
215 if(hasChange && this.fireEvent('validateedit', this, changes, r, this.rowIndex) !== false){
217 Ext.iterate(changes, function(name, value){
221 this.fireEvent('afteredit', this, changes, r, this.rowIndex);
226 verifyLayout: function(force){
227 if(this.el && (this.isVisible() || force === true)){
228 var row = this.grid.getView().getRow(this.rowIndex);
229 this.setSize(Ext.fly(row).getWidth(), Ext.isIE ? Ext.fly(row).getHeight() + 9 : undefined);
230 var cm = this.grid.colModel, fields = this.items.items;
231 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
235 adjust += 3; // outer padding
240 fields[i].setWidth(cm.getColumnWidth(i) - adjust);
246 this.positionButtons();
250 slideHide : function(){
254 initFields: function(){
255 var cm = this.grid.getColumnModel(), pm = Ext.layout.ContainerLayout.prototype.parseMargins;
256 this.removeAll(false);
257 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
258 var c = cm.getColumnAt(i),
261 ed = c.displayEditor || new Ext.form.DisplayField();
264 ed.margins = pm('0 1 2 1');
265 } else if(i == len - 1){
266 ed.margins = pm('0 0 2 1');
269 ed.margins = pm('0 0 2 0');
272 ed.margins = pm('0 1 2 0');
275 ed.setWidth(cm.getColumnWidth(i));
277 if(ed.ownerCt !== this){
278 ed.on('focus', this.ensureVisible, this);
279 ed.on('specialkey', this.onKey, this);
283 this.initialized = true;
286 onKey: function(f, e){
287 if(e.getKey() === e.ENTER){
288 this.stopEditing(true);
293 onGridKey: function(e){
294 if(e.getKey() === e.ENTER && !this.isVisible()){
295 var r = this.grid.getSelectionModel().getSelected();
297 var index = this.grid.store.indexOf(r);
298 this.startEditing(index);
304 ensureVisible: function(editor){
305 if(this.isVisible()){
306 this.grid.getView().ensureVisible(this.rowIndex, this.grid.colModel.getIndexById(editor.column.id), true);
310 onRowClick: function(g, rowIndex, e){
311 if(this.clicksToEdit == 'auto'){
312 var li = this.lastClickIndex;
313 this.lastClickIndex = rowIndex;
314 if(li != rowIndex && !this.isVisible()){
318 this.startEditing(rowIndex, false);
319 this.doFocus.defer(this.focusDelay, this, [e.getPoint()]);
322 onRowDblClick: function(g, rowIndex, e){
323 this.startEditing(rowIndex, false);
324 this.doFocus.defer(this.focusDelay, this, [e.getPoint()]);
327 onRender: function(){
328 Ext.ux.grid.RowEditor.superclass.onRender.apply(this, arguments);
329 this.el.swallowEvent(['keydown', 'keyup', 'keypress']);
330 this.btns = new Ext.Panel({
335 width: (this.minButtonWidth * 2) + (this.frameWidth * 2) + (this.buttonPad * 4), // width must be specified for IE
341 width: this.minButtonWidth,
342 handler: this.stopEditing.createDelegate(this, [true])
345 text: this.cancelText,
346 width: this.minButtonWidth,
347 handler: this.stopEditing.createDelegate(this, [false])
350 this.btns.render(this.bwrap);
353 afterRender: function(){
354 Ext.ux.grid.RowEditor.superclass.afterRender.apply(this, arguments);
355 this.positionButtons();
356 if(this.monitorValid){
357 this.startMonitoring();
362 if(this.monitorValid){
363 this.startMonitoring();
365 Ext.ux.grid.RowEditor.superclass.onShow.apply(this, arguments);
369 Ext.ux.grid.RowEditor.superclass.onHide.apply(this, arguments);
370 this.stopMonitoring();
371 this.grid.getView().focusRow(this.rowIndex);
374 positionButtons: function(){
377 h = this.el.dom.clientHeight,
379 scroll = view.scroller.dom.scrollLeft,
380 bw = this.btns.getWidth(),
381 width = Math.min(g.getWidth(), g.getColumnModel().getTotalWidth());
383 this.btns.el.shift({left: (width/2)-(bw/2)+scroll, top: h - 2, stopFx: true, duration:0.2});
388 preEditValue : function(r, field){
389 var value = r.data[field];
390 return this.autoEncode && typeof value === 'string' ? Ext.util.Format.htmlDecode(value) : value;
394 postEditValue : function(value, originalValue, r, field){
395 return this.autoEncode && typeof value == 'string' ? Ext.util.Format.htmlEncode(value) : value;
398 doFocus: function(pt){
399 if(this.isVisible()){
401 cm = this.grid.getColumnModel(),
404 index = this.getTargetColumnIndex(pt);
406 for(var i = index||0, len = cm.getColumnCount(); i < len; i++){
407 c = cm.getColumnAt(i);
408 if(!c.hidden && c.getEditor()){
409 c.getEditor().focus();
416 getTargetColumnIndex: function(pt){
417 var grid = this.grid,
420 cms = grid.colModel.config,
423 for(var len = cms.length, c; c = cms[i]; i++){
425 if(Ext.fly(v.getHeaderCell(i)).getRegion().right >= x){
434 startMonitoring : function(){
435 if(!this.bound && this.monitorValid){
438 run : this.bindHandler,
439 interval : this.monitorPoll || 200,
445 stopMonitoring : function(){
454 this.items.each(function(f){
455 if(!f.isValid(true)){
464 bindHandler : function(){
466 return false; // stops binding
468 var valid = this.isValid();
469 if(!valid && this.errorSummary){
470 this.showTooltip(this.getErrorText().join(''));
472 this.btns.saveBtn.setDisabled(!valid);
473 this.fireEvent('validation', this, valid);
476 lastVisibleColumn : function() {
477 var i = this.items.getCount() - 1,
480 c = this.items.items[i];
487 showTooltip: function(msg){
488 var t = this.tooltip;
490 t = this.tooltip = new Ext.ToolTip({
494 title: this.errorText,
497 anchorToTarget: true,
501 var v = this.grid.getView(),
502 top = parseInt(this.el.dom.style.top, 10),
503 scroll = v.scroller.dom.scrollTop,
504 h = this.el.getHeight();
506 if(top + h >= scroll){
507 t.initTarget(this.lastVisibleColumn().getEl());
515 }else if(t.rendered){
520 getErrorText: function(){
522 this.items.each(function(f){
523 if(!f.isValid(true)){
524 data.push('<li>', f.getActiveError(), '</li>');
531 Ext.preg('roweditor', Ext.ux.grid.RowEditor);