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 underlying field, "width" to adopt the width only,
40 * or "height" to adopt the height only, "none" to always use the field dimensions. (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 <tt>true</tt>.
83 completeOnEnter : true,
85 * @cfg {Boolean} cancelOnEsc True to cancel the edit when the escape key is pressed. Defaults to <tt>true</tt>.
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.mon(this.field, {
172 specialkey: this.onSpecialKey
175 this.mon(this.field, "autosize", this.el.sync, this.el, {delay:1});
177 this.field.render(this.el).show();
178 this.field.getEl().dom.name = '';
179 if(this.swallowKeys){
180 this.field.el.swallowEvent([
181 'keypress', // *** Opera
182 'keydown' // *** all other browsers
188 onSpecialKey : function(field, e){
189 var key = e.getKey(),
190 complete = this.completeOnEnter && key == e.ENTER,
191 cancel = this.cancelOnEsc && key == e.ESC;
192 if(complete || cancel){
199 if(field.triggerBlur){
203 this.fireEvent('specialkey', field, e);
207 * Starts the editing process and shows the editor.
208 * @param {Mixed} el The element to edit
209 * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
210 * to the innerHTML of el.
212 startEdit : function(el, value){
216 this.boundEl = Ext.get(el);
217 var v = value !== undefined ? value : this.boundEl.dom.innerHTML;
219 this.render(this.parentEl || document.body);
221 if(this.fireEvent("beforestartedit", this, this.boundEl, v) !== false){
223 this.field.setValue(v);
225 this.el.alignTo(this.boundEl, this.alignment);
232 doAutoSize : function(){
234 var sz = this.boundEl.getSize(),
235 fs = this.field.getSize();
237 switch(this.autoSize){
239 this.setSize(sz.width, fs.height);
242 this.setSize(fs.width, sz.height);
245 this.setSize(fs.width, fs.height);
248 this.setSize(sz.width, sz.height);
254 * Sets the height and width of this editor.
255 * @param {Number} width The new width
256 * @param {Number} height The new height
258 setSize : function(w, h){
259 delete this.field.lastSize;
260 this.field.setSize(w, h);
262 if(Ext.isGecko2 || Ext.isOpera){
263 // prevent layer scrollbars
264 this.el.setSize(w, h);
271 * Realigns the editor to the bound field based on the current alignment config value.
273 realign : function(){
274 this.el.alignTo(this.boundEl, this.alignment);
278 * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
279 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after edit (defaults to false)
281 completeEdit : function(remainVisible){
285 var v = this.getValue();
286 if(!this.field.isValid()){
287 if(this.revertInvalid !== false){
288 this.cancelEdit(remainVisible);
292 if(String(v) === String(this.startValue) && this.ignoreNoChange){
293 this.hideEdit(remainVisible);
296 if(this.fireEvent("beforecomplete", this, v, this.startValue) !== false){
298 if(this.updateEl && this.boundEl){
299 this.boundEl.update(v);
301 this.hideEdit(remainVisible);
302 this.fireEvent("complete", this, v, this.startValue);
309 if(this.hideEl !== false){
312 this.field.show().focus(false, true);
313 this.fireEvent("startedit", this.boundEl, this.startValue);
317 * Cancels the editing process and hides the editor without persisting any changes. The field value will be
318 * reverted to the original starting value.
319 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after
320 * cancel (defaults to false)
322 cancelEdit : function(remainVisible){
324 var v = this.getValue();
325 this.setValue(this.startValue);
326 this.hideEdit(remainVisible);
327 this.fireEvent("canceledit", this, v, this.startValue);
332 hideEdit: function(remainVisible){
333 if(remainVisible !== true){
334 this.editing = false;
341 if(this.allowBlur !== true && this.editing){
353 if(this.field.collapse){
354 this.field.collapse();
357 if(this.hideEl !== false){
363 * Sets the data value of the editor
364 * @param {Mixed} value Any valid value supported by the underlying field
366 setValue : function(v){
367 this.field.setValue(v);
371 * Gets the data value of the editor
372 * @return {Mixed} The data value
374 getValue : function(){
375 return this.field.getValue();
378 beforeDestroy : function(){
379 Ext.destroy(this.field);
383 Ext.reg('editor', Ext.Editor);