3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
9 * @extends Ext.Component
10 * A base editor field that handles displaying/hiding on demand and has some built-in sizing and event handling logic.
13 * @param {Object} config The config object
16 Ext.Editor = function(field, config){
18 this.field = Ext.create(field.field, 'textfield');
19 config = Ext.apply({}, field); // copy so we don't disturb original config
24 Ext.Editor.superclass.constructor.call(this, config);
27 Ext.extend(Ext.Editor, Ext.Component, {
29 * @cfg {Ext.form.Field} field
30 * The Field object (or descendant) or config object for field
33 * @cfg {Boolean} allowBlur
34 * True to {@link #completeEdit complete the editing process} if in edit mode when the
35 * field is blurred. Defaults to <tt>false</tt>.
38 * @cfg {Boolean/String} autoSize
39 * True for the editor to automatically adopt the size of the element being edited, "width" to adopt the width only,
40 * or "height" to adopt the height only (defaults to false)
43 * @cfg {Boolean} revertInvalid
44 * True to automatically revert the field value and cancel the edit when the user completes an edit and the field
45 * validation fails (defaults to true)
48 * @cfg {Boolean} ignoreNoChange
49 * True to skip the edit completion process (no save, no events fired) if the user completes an edit and
50 * the value has not changed (defaults to false). Applies only to string values - edits for other data types
51 * will never be ignored.
54 * @cfg {Boolean} hideEl
55 * False to keep the bound element visible while the editor is displayed (defaults to true)
59 * The data value of the underlying field (defaults to "")
63 * @cfg {String} alignment
64 * The position to align to (see {@link Ext.Element#alignTo} for more details, defaults to "c-c?").
68 * @cfg {Boolean/String} shadow "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop"
69 * for bottom-right shadow (defaults to "frame")
73 * @cfg {Boolean} constrain True to constrain the editor to the viewport
77 * @cfg {Boolean} swallowKeys Handle the keydown/keypress events so they don't propagate (defaults to true)
81 * @cfg {Boolean} completeOnEnter True to complete the edit when the enter key is pressed (defaults to false)
83 completeOnEnter : false,
85 * @cfg {Boolean} cancelOnEsc True to cancel the edit when the escape key is pressed (defaults to false)
89 * @cfg {Boolean} updateEl True to update the innerHTML of the bound element when the update completes (defaults to false)
93 initComponent : function(){
94 Ext.Editor.superclass.initComponent.call(this);
97 * @event beforestartedit
98 * Fires when editing is initiated, but before the value changes. Editing can be canceled by returning
99 * false from the handler of this event.
100 * @param {Editor} this
101 * @param {Ext.Element} boundEl The underlying element bound to this editor
102 * @param {Mixed} value The field value being set
107 * Fires when this editor is displayed
108 * @param {Ext.Element} boundEl The underlying element bound to this editor
109 * @param {Mixed} value The starting field value
113 * @event beforecomplete
114 * Fires after a change has been made to the field, but before the change is reflected in the underlying
115 * field. Saving the change to the field can be canceled by returning false from the handler of this event.
116 * Note that if the value has not changed and ignoreNoChange = true, the editing will still end but this
117 * event will not fire since no edit actually occurred.
118 * @param {Editor} this
119 * @param {Mixed} value The current field value
120 * @param {Mixed} startValue The original field value
125 * Fires after editing is complete and any changed value has been written to the underlying field.
126 * @param {Editor} this
127 * @param {Mixed} value The current field value
128 * @param {Mixed} startValue The original field value
133 * Fires after editing has been canceled and the editor's value has been reset.
134 * @param {Editor} this
135 * @param {Mixed} value The user-entered field value that was discarded
136 * @param {Mixed} startValue The original field value that was set back into the editor after cancel
141 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
142 * {@link Ext.EventObject#getKey} to determine which key was pressed.
143 * @param {Ext.form.Field} this
144 * @param {Ext.EventObject} e The event object
151 onRender : function(ct, position){
152 this.el = new Ext.Layer({
157 shadowOffset: this.shadowOffset || 4,
159 constrain: this.constrain
162 this.el.setZIndex(this.zIndex);
164 this.el.setStyle("overflow", Ext.isGecko ? "auto" : "hidden");
165 if(this.field.msgTarget != 'title'){
166 this.field.msgTarget = 'qtip';
168 this.field.inEditor = true;
169 this.field.render(this.el);
171 this.field.el.dom.setAttribute('autocomplete', 'off');
173 this.mon(this.field, "specialkey", this.onSpecialKey, this);
174 if(this.swallowKeys){
175 this.field.el.swallowEvent(['keydown','keypress']);
178 this.mon(this.field, "blur", this.onBlur, this);
180 this.mon(this.field, "autosize", this.el.sync, this.el, {delay:1});
185 onSpecialKey : function(field, e){
186 var key = e.getKey();
187 if(this.completeOnEnter && key == e.ENTER){
190 }else if(this.cancelOnEsc && key == e.ESC){
193 this.fireEvent('specialkey', field, e);
195 if(this.field.triggerBlur && (key == e.ENTER || key == e.ESC || key == e.TAB)){
196 this.field.triggerBlur();
201 * Starts the editing process and shows the editor.
202 * @param {Mixed} el The element to edit
203 * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
204 * to the innerHTML of el.
206 startEdit : function(el, value){
210 this.boundEl = Ext.get(el);
211 var v = value !== undefined ? value : this.boundEl.dom.innerHTML;
213 this.render(this.parentEl || document.body);
215 if(this.fireEvent("beforestartedit", this, this.boundEl, v) === false){
219 this.field.setValue(v);
221 this.el.alignTo(this.boundEl, this.alignment);
227 doAutoSize : function(){
229 var sz = this.boundEl.getSize();
230 switch(this.autoSize){
232 this.setSize(sz.width, "");
235 this.setSize("", sz.height);
238 this.setSize(sz.width, sz.height);
244 * Sets the height and width of this editor.
245 * @param {Number} width The new width
246 * @param {Number} height The new height
248 setSize : function(w, h){
249 delete this.field.lastSize;
250 this.field.setSize(w, h);
252 if(Ext.isGecko2 || Ext.isOpera){
253 // prevent layer scrollbars
254 this.el.setSize(w, h);
261 * Realigns the editor to the bound field based on the current alignment config value.
263 realign : function(){
264 this.el.alignTo(this.boundEl, this.alignment);
268 * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
269 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after edit (defaults to false)
271 completeEdit : function(remainVisible){
275 var v = this.getValue();
276 if(!this.field.isValid()){
277 if(this.revertInvalid !== false){
278 this.cancelEdit(remainVisible);
282 if(String(v) === String(this.startValue) && this.ignoreNoChange){
283 this.hideEdit(remainVisible);
286 if(this.fireEvent("beforecomplete", this, v, this.startValue) !== false){
288 if(this.updateEl && this.boundEl){
289 this.boundEl.update(v);
291 this.hideEdit(remainVisible);
292 this.fireEvent("complete", this, v, this.startValue);
299 if(this.hideEl !== false){
303 if(Ext.isIE && !this.fixIEFocus){ // IE has problems with focusing the first time
304 this.fixIEFocus = true;
305 this.deferredFocus.defer(50, this);
309 this.fireEvent("startedit", this.boundEl, this.startValue);
312 deferredFocus : function(){
319 * Cancels the editing process and hides the editor without persisting any changes. The field value will be
320 * reverted to the original starting value.
321 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after
322 * cancel (defaults to false)
324 cancelEdit : function(remainVisible){
326 var v = this.getValue();
327 this.setValue(this.startValue);
328 this.hideEdit(remainVisible);
329 this.fireEvent("canceledit", this, v, this.startValue);
334 hideEdit: function(remainVisible){
335 if(remainVisible !== true){
336 this.editing = false;
343 if(this.allowBlur !== true && this.editing){
355 if(this.field.collapse){
356 this.field.collapse();
359 if(this.hideEl !== false){
365 * Sets the data value of the editor
366 * @param {Mixed} value Any valid value supported by the underlying field
368 setValue : function(v){
369 this.field.setValue(v);
373 * Gets the data value of the editor
374 * @return {Mixed} The data value
376 getValue : function(){
377 return this.field.getValue();
380 beforeDestroy : function(){
381 Ext.destroy(this.field);
385 Ext.reg('editor', Ext.Editor);