3 * Copyright(c) 2006-2010 Ext JS, Inc.
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>true</tt>.
39 * @cfg {Boolean/String} autoSize
40 * True for the editor to automatically adopt the size of the underlying field, "width" to adopt the width only,
41 * or "height" to adopt the height only, "none" to always use the field dimensions. (defaults to false)
44 * @cfg {Boolean} revertInvalid
45 * True to automatically revert the field value and cancel the edit when the user completes an edit and the field
46 * validation fails (defaults to true)
49 * @cfg {Boolean} ignoreNoChange
50 * True to skip the edit completion process (no save, no events fired) if the user completes an edit and
51 * the value has not changed (defaults to false). Applies only to string values - edits for other data types
52 * will never be ignored.
55 * @cfg {Boolean} hideEl
56 * False to keep the bound element visible while the editor is displayed (defaults to true)
60 * The data value of the underlying field (defaults to "")
64 * @cfg {String} alignment
65 * The position to align to (see {@link Ext.Element#alignTo} for more details, defaults to "c-c?").
69 * @cfg {Array} offsets
70 * The offsets to use when aligning (see {@link Ext.Element#alignTo} for more details. Defaults to <tt>[0, 0]</tt>.
74 * @cfg {Boolean/String} shadow "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop"
75 * for bottom-right shadow (defaults to "frame")
79 * @cfg {Boolean} constrain True to constrain the editor to the viewport
83 * @cfg {Boolean} swallowKeys Handle the keydown/keypress events so they don't propagate (defaults to true)
87 * @cfg {Boolean} completeOnEnter True to complete the edit when the enter key is pressed. Defaults to <tt>true</tt>.
89 completeOnEnter : true,
91 * @cfg {Boolean} cancelOnEsc True to cancel the edit when the escape key is pressed. Defaults to <tt>true</tt>.
95 * @cfg {Boolean} updateEl True to update the innerHTML of the bound element when the update completes (defaults to false)
99 initComponent : function(){
100 Ext.Editor.superclass.initComponent.call(this);
103 * @event beforestartedit
104 * Fires when editing is initiated, but before the value changes. Editing can be canceled by returning
105 * false from the handler of this event.
106 * @param {Editor} this
107 * @param {Ext.Element} boundEl The underlying element bound to this editor
108 * @param {Mixed} value The field value being set
113 * Fires when this editor is displayed
114 * @param {Ext.Element} boundEl The underlying element bound to this editor
115 * @param {Mixed} value The starting field value
119 * @event beforecomplete
120 * Fires after a change has been made to the field, but before the change is reflected in the underlying
121 * field. Saving the change to the field can be canceled by returning false from the handler of this event.
122 * Note that if the value has not changed and ignoreNoChange = true, the editing will still end but this
123 * event will not fire since no edit actually occurred.
124 * @param {Editor} this
125 * @param {Mixed} value The current field value
126 * @param {Mixed} startValue The original field value
131 * Fires after editing is complete and any changed value has been written to the underlying field.
132 * @param {Editor} this
133 * @param {Mixed} value The current field value
134 * @param {Mixed} startValue The original field value
139 * Fires after editing has been canceled and the editor's value has been reset.
140 * @param {Editor} this
141 * @param {Mixed} value The user-entered field value that was discarded
142 * @param {Mixed} startValue The original field value that was set back into the editor after cancel
147 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
148 * {@link Ext.EventObject#getKey} to determine which key was pressed.
149 * @param {Ext.form.Field} this
150 * @param {Ext.EventObject} e The event object
157 onRender : function(ct, position){
158 this.el = new Ext.Layer({
163 shadowOffset: this.shadowOffset || 4,
165 constrain: this.constrain
168 this.el.setZIndex(this.zIndex);
170 this.el.setStyle("overflow", Ext.isGecko ? "auto" : "hidden");
171 if(this.field.msgTarget != 'title'){
172 this.field.msgTarget = 'qtip';
174 this.field.inEditor = true;
175 this.mon(this.field, {
178 specialkey: this.onSpecialKey
181 this.mon(this.field, "autosize", this.el.sync, this.el, {delay:1});
183 this.field.render(this.el).show();
184 this.field.getEl().dom.name = '';
185 if(this.swallowKeys){
186 this.field.el.swallowEvent([
187 'keypress', // *** Opera
188 'keydown' // *** all other browsers
194 onSpecialKey : function(field, e){
195 var key = e.getKey(),
196 complete = this.completeOnEnter && key == e.ENTER,
197 cancel = this.cancelOnEsc && key == e.ESC;
198 if(complete || cancel){
205 if(field.triggerBlur){
209 this.fireEvent('specialkey', field, e);
213 * Starts the editing process and shows the editor.
214 * @param {Mixed} el The element to edit
215 * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
216 * to the innerHTML of el.
218 startEdit : function(el, value){
222 this.boundEl = Ext.get(el);
223 var v = value !== undefined ? value : this.boundEl.dom.innerHTML;
225 this.render(this.parentEl || document.body);
227 if(this.fireEvent("beforestartedit", this, this.boundEl, v) !== false){
230 this.field.setValue(v);
238 doAutoSize : function(){
240 var sz = this.boundEl.getSize(),
241 fs = this.field.getSize();
243 switch(this.autoSize){
245 this.setSize(sz.width, fs.height);
248 this.setSize(fs.width, sz.height);
251 this.setSize(fs.width, fs.height);
254 this.setSize(sz.width, sz.height);
260 * Sets the height and width of this editor.
261 * @param {Number} width The new width
262 * @param {Number} height The new height
264 setSize : function(w, h){
265 delete this.field.lastSize;
266 this.field.setSize(w, h);
268 if(Ext.isGecko2 || Ext.isOpera){
269 // prevent layer scrollbars
270 this.el.setSize(w, h);
277 * Realigns the editor to the bound field based on the current alignment config value.
278 * @param {Boolean} autoSize (optional) True to size the field to the dimensions of the bound element.
280 realign : function(autoSize){
281 if(autoSize === true){
284 this.el.alignTo(this.boundEl, this.alignment, this.offsets);
288 * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
289 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after edit (defaults to false)
291 completeEdit : function(remainVisible){
295 // Assert combo values first
296 if (this.field.assertValue) {
297 this.field.assertValue();
299 var v = this.getValue();
300 if(!this.field.isValid()){
301 if(this.revertInvalid !== false){
302 this.cancelEdit(remainVisible);
306 if(String(v) === String(this.startValue) && this.ignoreNoChange){
307 this.hideEdit(remainVisible);
310 if(this.fireEvent("beforecomplete", this, v, this.startValue) !== false){
312 if(this.updateEl && this.boundEl){
313 this.boundEl.update(v);
315 this.hideEdit(remainVisible);
316 this.fireEvent("complete", this, v, this.startValue);
323 if(this.hideEl !== false){
326 this.field.show().focus(false, true);
327 this.fireEvent("startedit", this.boundEl, this.startValue);
331 * Cancels the editing process and hides the editor without persisting any changes. The field value will be
332 * reverted to the original starting value.
333 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after
334 * cancel (defaults to false)
336 cancelEdit : function(remainVisible){
338 var v = this.getValue();
339 this.setValue(this.startValue);
340 this.hideEdit(remainVisible);
341 this.fireEvent("canceledit", this, v, this.startValue);
346 hideEdit: function(remainVisible){
347 if(remainVisible !== true){
348 this.editing = false;
355 // selectSameEditor flag allows the same editor to be started without onBlur firing on itself
356 if(this.allowBlur === true && this.editing && this.selectSameEditor !== true){
368 if(this.field.collapse){
369 this.field.collapse();
372 if(this.hideEl !== false){
378 * Sets the data value of the editor
379 * @param {Mixed} value Any valid value supported by the underlying field
381 setValue : function(v){
382 this.field.setValue(v);
386 * Gets the data value of the editor
387 * @return {Mixed} The data value
389 getValue : function(){
390 return this.field.getValue();
393 beforeDestroy : function(){
394 Ext.destroyMembers(this, 'field');
396 delete this.parentEl;
400 Ext.reg('editor', Ext.Editor);