Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / source / Editor.html
1 <html>
2 <head>
3   <title>The source code</title>
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
6 </head>
7 <body  onload="prettyPrint();">
8     <pre class="prettyprint lang-js">/*!
9  * Ext JS Library 3.0.3
10  * Copyright(c) 2006-2009 Ext JS, LLC
11  * licensing@extjs.com
12  * http://www.extjs.com/license
13  */
14 <div id="cls-Ext.Editor"></div>/**
15  * @class Ext.Editor
16  * @extends Ext.Component
17  * A base editor field that handles displaying/hiding on demand and has some built-in sizing and event handling logic.
18  * @constructor
19  * Create a new Editor
20  * @param {Object} config The config object
21  * @xtype editor
22  */
23 Ext.Editor = function(field, config){
24     if(field.field){
25         this.field = Ext.create(field.field, 'textfield');
26         config = Ext.apply({}, field); // copy so we don't disturb original config
27         delete config.field;
28     }else{
29         this.field = field;
30     }
31     Ext.Editor.superclass.constructor.call(this, config);
32 };
33
34 Ext.extend(Ext.Editor, Ext.Component, {
35     <div id="cfg-Ext.Editor-field"></div>/**
36     * @cfg {Ext.form.Field} field
37     * The Field object (or descendant) or config object for field
38     */
39     <div id="cfg-Ext.Editor-allowBlur"></div>/**
40      * @cfg {Boolean} allowBlur
41      * True to {@link #completeEdit complete the editing process} if in edit mode when the
42      * field is blurred. Defaults to <tt>false</tt>.
43      */
44     <div id="cfg-Ext.Editor-autoSize"></div>/**
45      * @cfg {Boolean/String} autoSize
46      * True for the editor to automatically adopt the size of the underlying field, "width" to adopt the width only,
47      * or "height" to adopt the height only, "none" to always use the field dimensions. (defaults to false)
48      */
49     <div id="cfg-Ext.Editor-revertInvalid"></div>/**
50      * @cfg {Boolean} revertInvalid
51      * True to automatically revert the field value and cancel the edit when the user completes an edit and the field
52      * validation fails (defaults to true)
53      */
54     <div id="cfg-Ext.Editor-ignoreNoChange"></div>/**
55      * @cfg {Boolean} ignoreNoChange
56      * True to skip the edit completion process (no save, no events fired) if the user completes an edit and
57      * the value has not changed (defaults to false).  Applies only to string values - edits for other data types
58      * will never be ignored.
59      */
60     <div id="cfg-Ext.Editor-hideEl"></div>/**
61      * @cfg {Boolean} hideEl
62      * False to keep the bound element visible while the editor is displayed (defaults to true)
63      */
64     <div id="cfg-Ext.Editor-value"></div>/**
65      * @cfg {Mixed} value
66      * The data value of the underlying field (defaults to "")
67      */
68     value : "",
69     <div id="cfg-Ext.Editor-alignment"></div>/**
70      * @cfg {String} alignment
71      * The position to align to (see {@link Ext.Element#alignTo} for more details, defaults to "c-c?").
72      */
73     alignment: "c-c?",
74     <div id="cfg-Ext.Editor-shadow"></div>/**
75      * @cfg {Boolean/String} shadow "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop"
76      * for bottom-right shadow (defaults to "frame")
77      */
78     shadow : "frame",
79     <div id="cfg-Ext.Editor-constrain"></div>/**
80      * @cfg {Boolean} constrain True to constrain the editor to the viewport
81      */
82     constrain : false,
83     <div id="cfg-Ext.Editor-swallowKeys"></div>/**
84      * @cfg {Boolean} swallowKeys Handle the keydown/keypress events so they don't propagate (defaults to true)
85      */
86     swallowKeys : true,
87     <div id="cfg-Ext.Editor-completeOnEnter"></div>/**
88      * @cfg {Boolean} completeOnEnter True to complete the edit when the enter key is pressed. Defaults to <tt>true</tt>.
89      */
90     completeOnEnter : true,
91     <div id="cfg-Ext.Editor-cancelOnEsc"></div>/**
92      * @cfg {Boolean} cancelOnEsc True to cancel the edit when the escape key is pressed. Defaults to <tt>true</tt>.
93      */
94     cancelOnEsc : true,
95     <div id="cfg-Ext.Editor-updateEl"></div>/**
96      * @cfg {Boolean} updateEl True to update the innerHTML of the bound element when the update completes (defaults to false)
97      */
98     updateEl : false,
99
100     initComponent : function(){
101         Ext.Editor.superclass.initComponent.call(this);
102         this.addEvents(
103             <div id="event-Ext.Editor-beforestartedit"></div>/**
104              * @event beforestartedit
105              * Fires when editing is initiated, but before the value changes.  Editing can be canceled by returning
106              * false from the handler of this event.
107              * @param {Editor} this
108              * @param {Ext.Element} boundEl The underlying element bound to this editor
109              * @param {Mixed} value The field value being set
110              */
111             "beforestartedit",
112             <div id="event-Ext.Editor-startedit"></div>/**
113              * @event startedit
114              * Fires when this editor is displayed
115              * @param {Ext.Element} boundEl The underlying element bound to this editor
116              * @param {Mixed} value The starting field value
117              */
118             "startedit",
119             <div id="event-Ext.Editor-beforecomplete"></div>/**
120              * @event beforecomplete
121              * Fires after a change has been made to the field, but before the change is reflected in the underlying
122              * field.  Saving the change to the field can be canceled by returning false from the handler of this event.
123              * Note that if the value has not changed and ignoreNoChange = true, the editing will still end but this
124              * event will not fire since no edit actually occurred.
125              * @param {Editor} this
126              * @param {Mixed} value The current field value
127              * @param {Mixed} startValue The original field value
128              */
129             "beforecomplete",
130             <div id="event-Ext.Editor-complete"></div>/**
131              * @event complete
132              * Fires after editing is complete and any changed value has been written to the underlying field.
133              * @param {Editor} this
134              * @param {Mixed} value The current field value
135              * @param {Mixed} startValue The original field value
136              */
137             "complete",
138             <div id="event-Ext.Editor-canceledit"></div>/**
139              * @event canceledit
140              * Fires after editing has been canceled and the editor's value has been reset.
141              * @param {Editor} this
142              * @param {Mixed} value The user-entered field value that was discarded
143              * @param {Mixed} startValue The original field value that was set back into the editor after cancel
144              */
145             "canceledit",
146             <div id="event-Ext.Editor-specialkey"></div>/**
147              * @event specialkey
148              * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed.  You can check
149              * {@link Ext.EventObject#getKey} to determine which key was pressed.
150              * @param {Ext.form.Field} this
151              * @param {Ext.EventObject} e The event object
152              */
153             "specialkey"
154         );
155     },
156
157     // private
158     onRender : function(ct, position){
159         this.el = new Ext.Layer({
160             shadow: this.shadow,
161             cls: "x-editor",
162             parentEl : ct,
163             shim : this.shim,
164             shadowOffset: this.shadowOffset || 4,
165             id: this.id,
166             constrain: this.constrain
167         });
168         if(this.zIndex){
169             this.el.setZIndex(this.zIndex);
170         }
171         this.el.setStyle("overflow", Ext.isGecko ? "auto" : "hidden");
172         if(this.field.msgTarget != 'title'){
173             this.field.msgTarget = 'qtip';
174         }
175         this.field.inEditor = true;
176         this.mon(this.field, {
177             scope: this,
178             blur: this.onBlur,
179             specialkey: this.onSpecialKey
180         });
181         if(this.field.grow){
182             this.mon(this.field, "autosize", this.el.sync,  this.el, {delay:1});
183         }
184         this.field.render(this.el).show();
185         this.field.getEl().dom.name = '';
186         if(this.swallowKeys){
187             this.field.el.swallowEvent([
188                 'keypress', // *** Opera
189                 'keydown'   // *** all other browsers
190             ]);
191         }
192     },
193
194     // private
195     onSpecialKey : function(field, e){
196         var key = e.getKey(),
197             complete = this.completeOnEnter && key == e.ENTER,
198             cancel = this.cancelOnEsc && key == e.ESC;
199         if(complete || cancel){
200             e.stopEvent();
201             if(complete){
202                 this.completeEdit();
203             }else{
204                 this.cancelEdit();
205             }
206             if(field.triggerBlur){
207                 field.triggerBlur(); 
208             }
209         }
210         this.fireEvent('specialkey', field, e);
211     },
212
213     <div id="method-Ext.Editor-startEdit"></div>/**
214      * Starts the editing process and shows the editor.
215      * @param {Mixed} el The element to edit
216      * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
217       * to the innerHTML of el.
218      */
219     startEdit : function(el, value){
220         if(this.editing){
221             this.completeEdit();
222         }
223         this.boundEl = Ext.get(el);
224         var v = value !== undefined ? value : this.boundEl.dom.innerHTML;
225         if(!this.rendered){
226             this.render(this.parentEl || document.body);
227         }
228         if(this.fireEvent("beforestartedit", this, this.boundEl, v) !== false){
229             this.startValue = v;
230             this.field.setValue(v);
231             this.doAutoSize();
232             this.el.alignTo(this.boundEl, this.alignment);
233             this.editing = true;
234             this.show();
235         }
236     },
237
238     // private
239     doAutoSize : function(){
240         if(this.autoSize){
241             var sz = this.boundEl.getSize(),
242                 fs = this.field.getSize();
243
244             switch(this.autoSize){
245                 case "width":
246                     this.setSize(sz.width, fs.height);
247                     break;
248                 case "height":
249                     this.setSize(fs.width, sz.height);
250                     break;
251                 case "none":
252                     this.setSize(fs.width, fs.height);
253                     break;
254                 default:
255                     this.setSize(sz.width, sz.height);
256             }
257         }
258     },
259
260     <div id="method-Ext.Editor-setSize"></div>/**
261      * Sets the height and width of this editor.
262      * @param {Number} width The new width
263      * @param {Number} height The new height
264      */
265     setSize : function(w, h){
266         delete this.field.lastSize;
267         this.field.setSize(w, h);
268         if(this.el){
269             if(Ext.isGecko2 || Ext.isOpera){
270                 // prevent layer scrollbars
271                 this.el.setSize(w, h);
272             }
273             this.el.sync();
274         }
275     },
276
277     <div id="method-Ext.Editor-realign"></div>/**
278      * Realigns the editor to the bound field based on the current alignment config value.
279      */
280     realign : function(){
281         this.el.alignTo(this.boundEl, this.alignment);
282     },
283
284     <div id="method-Ext.Editor-completeEdit"></div>/**
285      * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
286      * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after edit (defaults to false)
287      */
288     completeEdit : function(remainVisible){
289         if(!this.editing){
290             return;
291         }
292         var v = this.getValue();
293         if(!this.field.isValid()){
294             if(this.revertInvalid !== false){
295                 this.cancelEdit(remainVisible);
296             }
297             return;
298         }
299         if(String(v) === String(this.startValue) && this.ignoreNoChange){
300             this.hideEdit(remainVisible);
301             return;
302         }
303         if(this.fireEvent("beforecomplete", this, v, this.startValue) !== false){
304             v = this.getValue();
305             if(this.updateEl && this.boundEl){
306                 this.boundEl.update(v);
307             }
308             this.hideEdit(remainVisible);
309             this.fireEvent("complete", this, v, this.startValue);
310         }
311     },
312
313     // private
314     onShow : function(){
315         this.el.show();
316         if(this.hideEl !== false){
317             this.boundEl.hide();
318         }
319         this.field.show().focus(false, true);
320         this.fireEvent("startedit", this.boundEl, this.startValue);
321     },
322
323     <div id="method-Ext.Editor-cancelEdit"></div>/**
324      * Cancels the editing process and hides the editor without persisting any changes.  The field value will be
325      * reverted to the original starting value.
326      * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after
327      * cancel (defaults to false)
328      */
329     cancelEdit : function(remainVisible){
330         if(this.editing){
331             var v = this.getValue();
332             this.setValue(this.startValue);
333             this.hideEdit(remainVisible);
334             this.fireEvent("canceledit", this, v, this.startValue);
335         }
336     },
337     
338     // private
339     hideEdit: function(remainVisible){
340         if(remainVisible !== true){
341             this.editing = false;
342             this.hide();
343         }
344     },
345
346     // private
347     onBlur : function(){
348         if(this.allowBlur !== true && this.editing){
349             this.completeEdit();
350         }
351     },
352
353     // private
354     onHide : function(){
355         if(this.editing){
356             this.completeEdit();
357             return;
358         }
359         this.field.blur();
360         if(this.field.collapse){
361             this.field.collapse();
362         }
363         this.el.hide();
364         if(this.hideEl !== false){
365             this.boundEl.show();
366         }
367     },
368
369     <div id="method-Ext.Editor-setValue"></div>/**
370      * Sets the data value of the editor
371      * @param {Mixed} value Any valid value supported by the underlying field
372      */
373     setValue : function(v){
374         this.field.setValue(v);
375     },
376
377     <div id="method-Ext.Editor-getValue"></div>/**
378      * Gets the data value of the editor
379      * @return {Mixed} The data value
380      */
381     getValue : function(){
382         return this.field.getValue();
383     },
384
385     beforeDestroy : function(){
386         Ext.destroy(this.field);
387         this.field = null;
388     }
389 });
390 Ext.reg('editor', Ext.Editor);</pre>
391 </body>
392 </html>