4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-Editor'>/**
19 </span> * @class Ext.Editor
20 * @extends Ext.Component
23 * The Editor class is used to provide inline editing for elements on the page. The editor
24 * is backed by a {@link Ext.form.field.Field} that will be displayed to edit the underlying content.
25 * The editor is a floating Component, when the editor is shown it is automatically aligned to
26 * display over the top of the bound element it is editing. The Editor contains several options
27 * for how to handle key presses:
29 * <li>{@link #completeOnEnter}</li>
30 * <li>{@link #cancelOnEsc}</li>
31 * <li>{@link #swallowKeys}</li>
33 * It also has options for how to use the value once the editor has been activated:
35 * <li>{@link #revertInvalid}</li>
36 * <li>{@link #ignoreNoChange}</li>
37 * <li>{@link #updateEl}</li>
41 * <pre><code>
42 var editor = new Ext.Editor({
43 updateEl: true, // update the innerHTML of the bound element when editing completes
48 var el = Ext.get('my-text'); // The element to 'edit'
49 editor.startEdit(el); // The value of the field will be taken as the innerHTML of the element.
50 * </code></pre>
51 * {@img Ext.Editor/Ext.Editor.png Ext.Editor component}
54 Ext.define('Ext.Editor', {
56 /* Begin Definitions */
58 extend: 'Ext.Component',
60 alias: 'widget.editor',
62 requires: ['Ext.layout.component.Editor'],
66 componentLayout: 'editor',
68 <span id='Ext-Editor-cfg-field'> /**
69 </span> * @cfg {Ext.form.field.Field} field
70 * The Field object (or descendant) or config object for field
73 <span id='Ext-Editor-cfg-allowBlur'> /**
74 </span> * @cfg {Boolean} allowBlur
75 * True to {@link #completeEdit complete the editing process} if in edit mode when the
80 <span id='Ext-Editor-cfg-autoSize'> /**
81 </span> * @cfg {Boolean/Object} autoSize
82 * True for the editor to automatically adopt the size of the underlying field. Otherwise, an object
83 * can be passed to indicate where to get each dimension. The available properties are 'boundEl' and
84 * 'field'. If a dimension is not specified, it will use the underlying height/width specified on
87 * <pre><code>
88 autoSize: true // The editor will be sized to the height/width of the field
92 width: 'boundEl' // The width will be determined by the width of the boundEl, the height from the editor (21)
96 width: 'field', // Width from the field
97 height: 'boundEl' // Height from the boundEl
99 * </pre></code>
102 <span id='Ext-Editor-cfg-revertInvalid'> /**
103 </span> * @cfg {Boolean} revertInvalid
104 * True to automatically revert the field value and cancel the edit when the user completes an edit and the field
109 <span id='Ext-Editor-cfg-ignoreNoChange'> /**
110 </span> * @cfg {Boolean} [ignoreNoChange=false]
111 * True to skip the edit completion process (no save, no events fired) if the user completes an edit and
112 * the value has not changed. Applies only to string values - edits for other data types
113 * will never be ignored.
116 <span id='Ext-Editor-cfg-hideEl'> /**
117 </span> * @cfg {Boolean} [hideEl=true]
118 * False to keep the bound element visible while the editor is displayed
121 <span id='Ext-Editor-cfg-value'> /**
122 </span> * @cfg {Object} value
123 * The data value of the underlying field
127 <span id='Ext-Editor-cfg-alignment'> /**
128 </span> * @cfg {String} alignment
129 * The position to align to (see {@link Ext.Element#alignTo} for more details).
133 <span id='Ext-Editor-cfg-offsets'> /**
134 </span> * @cfg {Number[]} offsets
135 * The offsets to use when aligning (see {@link Ext.Element#alignTo} for more details.
139 <span id='Ext-Editor-cfg-shadow'> /**
140 </span> * @cfg {Boolean/String} shadow
141 * "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop" for bottom-right shadow.
145 <span id='Ext-Editor-cfg-constrain'> /**
146 </span> * @cfg {Boolean} constrain
147 * True to constrain the editor to the viewport
151 <span id='Ext-Editor-cfg-swallowKeys'> /**
152 </span> * @cfg {Boolean} swallowKeys
153 * Handle the keydown/keypress events so they don't propagate
157 <span id='Ext-Editor-cfg-completeOnEnter'> /**
158 </span> * @cfg {Boolean} completeOnEnter
159 * True to complete the edit when the enter key is pressed.
161 completeOnEnter : true,
163 <span id='Ext-Editor-cfg-cancelOnEsc'> /**
164 </span> * @cfg {Boolean} cancelOnEsc
165 * True to cancel the edit when the escape key is pressed.
169 <span id='Ext-Editor-cfg-updateEl'> /**
170 </span> * @cfg {Boolean} updateEl
171 * True to update the innerHTML of the bound element when the update completes
175 <span id='Ext-Editor-cfg-parentEl'> /**
176 </span> * @cfg {String/HTMLElement/Ext.Element} parentEl
177 * An element to render to. Defaults to the <tt>document.body</tt>.
182 baseCls: Ext.baseCSSPrefix + 'editor',
184 initComponent : function() {
186 field = me.field = Ext.ComponentManager.create(me.field, 'textfield');
190 msgTarget: field.msgTarget == 'title' ? 'title' : 'qtip'
196 // slight delay to avoid race condition with startEdits (e.g. grid view refresh)
199 specialkey: me.onSpecialKey
203 me.mon(field, 'autosize', me.onAutoSize, me, {delay: 1});
206 constrain: me.constrain
209 me.callParent(arguments);
212 <span id='Ext-Editor-event-beforestartedit'> /**
213 </span> * @event beforestartedit
214 * Fires when editing is initiated, but before the value changes. Editing can be canceled by returning
215 * false from the handler of this event.
216 * @param {Ext.Editor} this
217 * @param {Ext.Element} boundEl The underlying element bound to this editor
218 * @param {Object} value The field value being set
222 <span id='Ext-Editor-event-startedit'> /**
223 </span> * @event startedit
224 * Fires when this editor is displayed
225 * @param {Ext.Editor} this
226 * @param {Ext.Element} boundEl The underlying element bound to this editor
227 * @param {Object} value The starting field value
231 <span id='Ext-Editor-event-beforecomplete'> /**
232 </span> * @event beforecomplete
233 * Fires after a change has been made to the field, but before the change is reflected in the underlying
234 * field. Saving the change to the field can be canceled by returning false from the handler of this event.
235 * Note that if the value has not changed and ignoreNoChange = true, the editing will still end but this
236 * event will not fire since no edit actually occurred.
237 * @param {Ext.Editor} this
238 * @param {Object} value The current field value
239 * @param {Object} startValue The original field value
242 <span id='Ext-Editor-event-complete'> /**
243 </span> * @event complete
244 * Fires after editing is complete and any changed value has been written to the underlying field.
245 * @param {Ext.Editor} this
246 * @param {Object} value The current field value
247 * @param {Object} startValue The original field value
250 <span id='Ext-Editor-event-canceledit'> /**
251 </span> * @event canceledit
252 * Fires after editing has been canceled and the editor's value has been reset.
253 * @param {Ext.Editor} this
254 * @param {Object} value The user-entered field value that was discarded
255 * @param {Object} startValue The original field value that was set back into the editor after cancel
258 <span id='Ext-Editor-event-specialkey'> /**
259 </span> * @event specialkey
260 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
261 * {@link Ext.EventObject#getKey} to determine which key was pressed.
262 * @param {Ext.Editor} this
263 * @param {Ext.form.field.Field} The field attached to this editor
264 * @param {Ext.EventObject} event The event object
271 onAutoSize: function(){
272 this.doComponentLayout();
276 onRender : function(ct, position) {
279 inputEl = field.inputEl;
281 me.callParent(arguments);
285 // Ensure the field doesn't get submitted as part of any form
287 inputEl.dom.name = '';
288 if (me.swallowKeys) {
289 inputEl.swallowEvent([
290 'keypress', // *** Opera
291 'keydown' // *** all other browsers
298 onSpecialKey : function(field, event) {
300 key = event.getKey(),
301 complete = me.completeOnEnter && key == event.ENTER,
302 cancel = me.cancelOnEsc && key == event.ESC;
304 if (complete || cancel) {
306 // Must defer this slightly to prevent exiting edit mode before the field's own
307 // key nav can handle the enter key, e.g. selecting an item in a combobox list
308 Ext.defer(function() {
314 if (field.triggerBlur) {
320 this.fireEvent('specialkey', this, field, event);
323 <span id='Ext-Editor-method-startEdit'> /**
324 </span> * Starts the editing process and shows the editor.
325 * @param {String/HTMLElement/Ext.Element} el The element to edit
326 * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
327 * to the innerHTML of el.
329 startEdit : function(el, value) {
334 me.boundEl = Ext.get(el);
335 value = Ext.isDefined(value) ? value : me.boundEl.dom.innerHTML;
338 me.render(me.parentEl || document.body);
341 if (me.fireEvent('beforestartedit', me, me.boundEl, value) !== false) {
342 me.startValue = value;
345 field.setValue(value);
347 field.focus(false, 10);
348 if (field.autoSize) {
355 <span id='Ext-Editor-method-realign'> /**
356 </span> * Realigns the editor to the bound field based on the current alignment config value.
357 * @param {Boolean} autoSize (optional) True to size the field to the dimensions of the bound element.
359 realign : function(autoSize) {
361 if (autoSize === true) {
362 me.doComponentLayout();
364 me.alignTo(me.boundEl, me.alignment, me.offsets);
367 <span id='Ext-Editor-method-completeEdit'> /**
368 </span> * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
369 * @param {Boolean} [remainVisible=false] Override the default behavior and keep the editor visible after edit
371 completeEdit : function(remainVisible) {
380 // Assert combo values first
381 if (field.assertValue) {
385 value = me.getValue();
386 if (!field.isValid()) {
387 if (me.revertInvalid !== false) {
388 me.cancelEdit(remainVisible);
393 if (String(value) === String(me.startValue) && me.ignoreNoChange) {
394 me.hideEdit(remainVisible);
398 if (me.fireEvent('beforecomplete', me, value, me.startValue) !== false) {
399 // Grab the value again, may have changed in beforecomplete
400 value = me.getValue();
401 if (me.updateEl && me.boundEl) {
402 me.boundEl.update(value);
404 me.hideEdit(remainVisible);
405 me.fireEvent('complete', me, value, me.startValue);
410 onShow : function() {
413 me.callParent(arguments);
414 if (me.hideEl !== false) {
417 me.fireEvent("startedit", me.boundEl, me.startValue);
420 <span id='Ext-Editor-method-cancelEdit'> /**
421 </span> * Cancels the editing process and hides the editor without persisting any changes. The field value will be
422 * reverted to the original starting value.
423 * @param {Boolean} [remainVisible=false] Override the default behavior and keep the editor visible after cancel
425 cancelEdit : function(remainVisible) {
427 startValue = me.startValue,
431 value = me.getValue();
432 me.setValue(startValue);
433 me.hideEdit(remainVisible);
434 me.fireEvent('canceledit', me, value, startValue);
439 hideEdit: function(remainVisible) {
440 if (remainVisible !== true) {
441 this.editing = false;
447 onBlur : function() {
450 // selectSameEditor flag allows the same editor to be started without onBlur firing on itself
451 if(me.allowBlur === true && me.editing && me.selectSameEditor !== true) {
457 onHide : function() {
466 if (field.collapse) {
471 if (me.hideEl !== false) {
474 me.callParent(arguments);
477 <span id='Ext-Editor-method-setValue'> /**
478 </span> * Sets the data value of the editor
479 * @param {Object} value Any valid value supported by the underlying field
481 setValue : function(value) {
482 this.field.setValue(value);
485 <span id='Ext-Editor-method-getValue'> /**
486 </span> * Gets the data value of the editor
487 * @return {Object} The data value
489 getValue : function() {
490 return this.field.getValue();
493 beforeDestroy : function() {
496 Ext.destroy(me.field);
501 me.callParent(arguments);