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>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.Editor"></div>/**
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.
20 * @param {Object} config The config object
23 Ext.Editor = function(field, config){
25 this.field = Ext.create(field.field, 'textfield');
26 config = Ext.apply({}, field); // copy so we don't disturb original config
31 Ext.Editor.superclass.constructor.call(this, config);
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
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>.
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)
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)
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.
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)
64 <div id="cfg-Ext.Editor-value"></div>/**
66 * The data value of the underlying field (defaults to "")
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?").
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")
79 <div id="cfg-Ext.Editor-constrain"></div>/**
80 * @cfg {Boolean} constrain True to constrain the editor to the viewport
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)
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>.
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>.
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)
100 initComponent : function(){
101 Ext.Editor.superclass.initComponent.call(this);
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
112 <div id="event-Ext.Editor-startedit"></div>/**
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
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
130 <div id="event-Ext.Editor-complete"></div>/**
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
138 <div id="event-Ext.Editor-canceledit"></div>/**
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
146 <div id="event-Ext.Editor-specialkey"></div>/**
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
158 onRender : function(ct, position){
159 this.el = new Ext.Layer({
164 shadowOffset: this.shadowOffset || 4,
166 constrain: this.constrain
169 this.el.setZIndex(this.zIndex);
171 this.el.setStyle("overflow", Ext.isGecko ? "auto" : "hidden");
172 if(this.field.msgTarget != 'title'){
173 this.field.msgTarget = 'qtip';
175 this.field.inEditor = true;
176 this.mon(this.field, {
179 specialkey: this.onSpecialKey
182 this.mon(this.field, "autosize", this.el.sync, this.el, {delay:1});
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
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){
206 if(field.triggerBlur){
210 this.fireEvent('specialkey', field, e);
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.
219 startEdit : function(el, value){
223 this.boundEl = Ext.get(el);
224 var v = value !== undefined ? value : this.boundEl.dom.innerHTML;
226 this.render(this.parentEl || document.body);
228 if(this.fireEvent("beforestartedit", this, this.boundEl, v) !== false){
230 this.field.setValue(v);
232 this.el.alignTo(this.boundEl, this.alignment);
239 doAutoSize : function(){
241 var sz = this.boundEl.getSize(),
242 fs = this.field.getSize();
244 switch(this.autoSize){
246 this.setSize(sz.width, fs.height);
249 this.setSize(fs.width, sz.height);
252 this.setSize(fs.width, fs.height);
255 this.setSize(sz.width, sz.height);
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
265 setSize : function(w, h){
266 delete this.field.lastSize;
267 this.field.setSize(w, h);
269 if(Ext.isGecko2 || Ext.isOpera){
270 // prevent layer scrollbars
271 this.el.setSize(w, h);
277 <div id="method-Ext.Editor-realign"></div>/**
278 * Realigns the editor to the bound field based on the current alignment config value.
280 realign : function(){
281 this.el.alignTo(this.boundEl, this.alignment);
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)
288 completeEdit : function(remainVisible){
292 var v = this.getValue();
293 if(!this.field.isValid()){
294 if(this.revertInvalid !== false){
295 this.cancelEdit(remainVisible);
299 if(String(v) === String(this.startValue) && this.ignoreNoChange){
300 this.hideEdit(remainVisible);
303 if(this.fireEvent("beforecomplete", this, v, this.startValue) !== false){
305 if(this.updateEl && this.boundEl){
306 this.boundEl.update(v);
308 this.hideEdit(remainVisible);
309 this.fireEvent("complete", this, v, this.startValue);
316 if(this.hideEl !== false){
319 this.field.show().focus(false, true);
320 this.fireEvent("startedit", this.boundEl, this.startValue);
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)
329 cancelEdit : function(remainVisible){
331 var v = this.getValue();
332 this.setValue(this.startValue);
333 this.hideEdit(remainVisible);
334 this.fireEvent("canceledit", this, v, this.startValue);
339 hideEdit: function(remainVisible){
340 if(remainVisible !== true){
341 this.editing = false;
348 if(this.allowBlur !== true && this.editing){
360 if(this.field.collapse){
361 this.field.collapse();
364 if(this.hideEl !== false){
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
373 setValue : function(v){
374 this.field.setValue(v);
377 <div id="method-Ext.Editor-getValue"></div>/**
378 * Gets the data value of the editor
379 * @return {Mixed} The data value
381 getValue : function(){
382 return this.field.getValue();
385 beforeDestroy : function(){
386 Ext.destroy(this.field);
390 Ext.reg('editor', Ext.Editor);</pre>