3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.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',
37 initComponent: function(){
38 Ext.ux.grid.RowEditor.superclass.initComponent.call(this);
42 * Fired before the row editor is activated.
43 * If the listener returns <tt>false</tt> the editor will not be activated.
44 * @param {Ext.ux.grid.RowEditor} roweditor This object
45 * @param {Number} rowIndex The rowIndex of the row just edited
50 * Fired after a row is edited and passes validation.
51 * If the listener returns <tt>false</tt> changes to the record will not be set.
52 * @param {Ext.ux.grid.RowEditor} roweditor This object
53 * @param {Object} changes Object with changes made to the record.
54 * @param {Ext.data.Record} r The Record that was edited.
55 * @param {Number} rowIndex The rowIndex of the row just edited
60 * Fired after a row is edited and passes validation. This event is fired
61 * after the store's update event is fired with this edit.
62 * @param {Ext.ux.grid.RowEditor} roweditor This object
63 * @param {Object} changes Object with changes made to the record.
64 * @param {Ext.data.Record} r The Record that was edited.
65 * @param {Number} rowIndex The rowIndex of the row just edited
74 if(this.clicksToEdit === 2){
75 grid.on('rowdblclick', this.onRowDblClick, this);
77 grid.on('rowclick', this.onRowClick, this);
79 grid.on('rowdblclick', this.onRowDblClick, this);
83 // stopEditing without saving when a record is removed from Store.
84 grid.getStore().on('remove', function() {
85 this.stopEditing(false);
90 keydown: this.onGridKey,
91 columnresize: this.verifyLayout,
92 columnmove: this.refreshFields,
93 reconfigure: this.refreshFields,
94 destroy : this.destroy,
97 fn: this.positionButtons
100 grid.getColumnModel().on('hiddenchange', this.verifyLayout, this, {delay:1});
101 grid.getView().on('refresh', this.stopEditing.createDelegate(this, []));
104 refreshFields: function(){
111 this.items.each(function(f){
112 if(String(this.values[f.id]) !== String(f.getValue())){
120 startEditing: function(rowIndex, doFocus){
121 if(this.editing && this.isDirty()){
122 this.showTooltip('You need to commit or cancel your changes');
126 if(typeof rowIndex == 'object'){
127 rowIndex = this.grid.getStore().indexOf(rowIndex);
129 if(this.fireEvent('beforeedit', this, rowIndex) !== false){
130 var g = this.grid, view = g.getView();
131 var row = view.getRow(rowIndex);
132 var record = g.store.getAt(rowIndex);
133 this.record = record;
134 this.rowIndex = rowIndex;
137 this.render(view.getEditorParent());
139 var w = Ext.fly(row).getWidth();
141 if(!this.initialized){
144 var cm = g.getColumnModel(), fields = this.items.items, f, val;
145 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
146 val = this.preEditValue(record, cm.getDataIndex(i));
149 this.values[f.id] = val || '';
151 this.verifyLayout(true);
152 if(!this.isVisible()){
153 this.setPagePosition(Ext.fly(row).getXY());
155 this.el.setXY(Ext.fly(row).getXY(), {duration:0.15});
157 if(!this.isVisible()){
158 this.show().doLayout();
160 if(doFocus !== false){
161 this.doFocus.defer(this.focusDelay, this);
166 stopEditing : function(saveChanges){
167 this.editing = false;
168 if(!this.isVisible()){
171 if(saveChanges === false || !this.isValid()){
175 var changes = {}, r = this.record, hasChange = false;
176 var cm = this.grid.colModel, fields = this.items.items;
177 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
179 var dindex = cm.getDataIndex(i);
180 if(!Ext.isEmpty(dindex)){
181 var oldValue = r.data[dindex];
182 var value = this.postEditValue(fields[i].getValue(), oldValue, r, dindex);
183 if(String(oldValue) !== String(value)){
184 changes[dindex] = value;
190 if(hasChange && this.fireEvent('validateedit', this, changes, r, this.rowIndex) !== false){
192 for(var k in changes){
193 if(changes.hasOwnProperty(k)){
194 r.set(k, changes[k]);
198 this.fireEvent('afteredit', this, changes, r, this.rowIndex);
203 verifyLayout: function(force){
204 if(this.el && (this.isVisible() || force === true)){
205 var row = this.grid.getView().getRow(this.rowIndex);
206 this.setSize(Ext.fly(row).getWidth(), Ext.isIE ? Ext.fly(row).getHeight() + (Ext.isBorderBox ? 9 : 0) : undefined);
207 var cm = this.grid.colModel, fields = this.items.items;
208 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
212 adjust += 0; // outer padding
215 adjust += 3; // outer padding
220 fields[i].setWidth(cm.getColumnWidth(i) - adjust);
226 this.positionButtons();
230 slideHide : function(){
234 initFields: function(){
235 var cm = this.grid.getColumnModel(), pm = Ext.layout.ContainerLayout.prototype.parseMargins;
236 this.removeAll(false);
237 for(var i = 0, len = cm.getColumnCount(); i < len; i++){
238 var c = cm.getColumnAt(i);
239 var ed = c.getEditor();
241 ed = c.displayEditor || new Ext.form.DisplayField();
244 ed.margins = pm('0 1 2 1');
245 } else if(i == len - 1){
246 ed.margins = pm('0 0 2 1');
248 ed.margins = pm('0 1 2');
250 ed.setWidth(cm.getColumnWidth(i));
252 if(ed.ownerCt !== this){
253 ed.on('focus', this.ensureVisible, this);
254 ed.on('specialkey', this.onKey, this);
258 this.initialized = true;
261 onKey: function(f, e){
262 if(e.getKey() === e.ENTER){
263 this.stopEditing(true);
268 onGridKey: function(e){
269 if(e.getKey() === e.ENTER && !this.isVisible()){
270 var r = this.grid.getSelectionModel().getSelected();
272 var index = this.grid.store.indexOf(r);
273 this.startEditing(index);
279 ensureVisible: function(editor){
280 if(this.isVisible()){
281 this.grid.getView().ensureVisible(this.rowIndex, this.grid.colModel.getIndexById(editor.column.id), true);
285 onRowClick: function(g, rowIndex, e){
286 if(this.clicksToEdit == 'auto'){
287 var li = this.lastClickIndex;
288 this.lastClickIndex = rowIndex;
289 if(li != rowIndex && !this.isVisible()){
293 this.startEditing(rowIndex, false);
294 this.doFocus.defer(this.focusDelay, this, [e.getPoint()]);
297 onRowDblClick: function(g, rowIndex, e){
298 this.startEditing(rowIndex, false);
299 this.doFocus.defer(this.focusDelay, this, [e.getPoint()]);
302 onRender: function(){
303 Ext.ux.grid.RowEditor.superclass.onRender.apply(this, arguments);
304 this.el.swallowEvent(['keydown', 'keyup', 'keypress']);
305 this.btns = new Ext.Panel({
310 width: (this.minButtonWidth * 2) + (this.frameWidth * 2) + (this.buttonPad * 4), // width must be specified for IE
315 text: this.saveText || 'Save',
316 width: this.minButtonWidth,
317 handler: this.stopEditing.createDelegate(this, [true])
320 text: this.cancelText || 'Cancel',
321 width: this.minButtonWidth,
322 handler: this.stopEditing.createDelegate(this, [false])
325 this.btns.render(this.bwrap);
328 afterRender: function(){
329 Ext.ux.grid.RowEditor.superclass.afterRender.apply(this, arguments);
330 this.positionButtons();
331 if(this.monitorValid){
332 this.startMonitoring();
337 if(this.monitorValid){
338 this.startMonitoring();
340 Ext.ux.grid.RowEditor.superclass.onShow.apply(this, arguments);
344 Ext.ux.grid.RowEditor.superclass.onHide.apply(this, arguments);
345 this.stopMonitoring();
346 this.grid.getView().focusRow(this.rowIndex);
349 positionButtons: function(){
351 var h = this.el.dom.clientHeight;
352 var view = this.grid.getView();
353 var scroll = view.scroller.dom.scrollLeft;
354 var width = view.mainBody.getWidth();
355 var bw = this.btns.getWidth();
356 this.btns.el.shift({left: (width/2)-(bw/2)+scroll, top: h - 2, stopFx: true, duration:0.2});
361 preEditValue : function(r, field){
362 var value = r.data[field];
363 return this.autoEncode && typeof value === 'string' ? Ext.util.Format.htmlDecode(value) : value;
367 postEditValue : function(value, originalValue, r, field){
368 return this.autoEncode && typeof value == 'string' ? Ext.util.Format.htmlEncode(value) : value;
371 doFocus: function(pt){
372 if(this.isVisible()){
375 index = this.getTargetColumnIndex(pt);
377 var cm = this.grid.getColumnModel();
378 for(var i = index||0, len = cm.getColumnCount(); i < len; i++){
379 var c = cm.getColumnAt(i);
380 if(!c.hidden && c.getEditor()){
381 c.getEditor().focus();
388 getTargetColumnIndex: function(pt){
389 var grid = this.grid, v = grid.view;
391 var cms = grid.colModel.config;
392 var i = 0, match = false;
393 for(var len = cms.length, c; c = cms[i]; i++){
395 if(Ext.fly(v.getHeaderCell(i)).getRegion().right >= x){
404 startMonitoring : function(){
405 if(!this.bound && this.monitorValid){
408 run : this.bindHandler,
409 interval : this.monitorPoll || 200,
415 stopMonitoring : function(){
424 this.items.each(function(f){
425 if(!f.isValid(true)){
434 bindHandler : function(){
436 return false; // stops binding
438 var valid = this.isValid();
439 if(!valid && this.errorSummary){
440 this.showTooltip(this.getErrorText().join(''));
442 this.btns.saveBtn.setDisabled(!valid);
443 this.fireEvent('validation', this, valid);
446 showTooltip: function(msg){
447 var t = this.tooltip;
449 t = this.tooltip = new Ext.ToolTip({
456 anchorToTarget: true,
460 t.initTarget(this.items.last().getEl());
470 getErrorText: function(){
472 this.items.each(function(f){
473 if(!f.isValid(true)){
474 data.push('<li>', f.activeError, '</li>');
481 Ext.preg('roweditor', Ext.ux.grid.RowEditor);
483 Ext.override(Ext.form.Field, {
484 markInvalid : function(msg){
485 if(!this.rendered || this.preventMark){ // not rendered
488 msg = msg || this.invalidText;
490 var mt = this.getMessageHandler();
493 }else if(this.msgTarget){
494 this.el.addClass(this.invalidClass);
495 var t = Ext.getDom(this.msgTarget);
498 t.style.display = this.msgDisplay;
501 this.activeError = msg;
502 this.fireEvent('invalid', this, msg);
506 Ext.override(Ext.ToolTip, {
507 doAutoWidth : function(){
508 var bw = this.body.getTextWidth();
510 bw = Math.max(bw, this.header.child('span').getTextWidth(this.title));
512 bw += this.getFrameWidth() + (this.closable ? 20 : 0) + this.body.getPadding("lr") + 20;
513 this.setWidth(bw.constrain(this.minWidth, this.maxWidth));
515 // IE7 repaint bug on initial show
516 if(Ext.isIE7 && !this.repainted){
518 this.repainted = true;