4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../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-method-constructor'><span id='Ext-Editor'>/**
19 </span></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}
55 * @param {Object} config The config object
58 Ext.define('Ext.Editor', {
60 /* Begin Definitions */
62 extend: 'Ext.Component',
64 alias: 'widget.editor',
66 requires: ['Ext.layout.component.Editor'],
70 componentLayout: 'editor',
72 <span id='Ext-Editor-cfg-field'> /**
73 </span> * @cfg {Ext.form.field.Field} field
74 * The Field object (or descendant) or config object for field
77 <span id='Ext-Editor-cfg-allowBlur'> /**
78 </span> * @cfg {Boolean} allowBlur
79 * True to {@link #completeEdit complete the editing process} if in edit mode when the
80 * field is blurred. Defaults to <tt>true</tt>.
84 <span id='Ext-Editor-cfg-autoSize'> /**
85 </span> * @cfg {Boolean/Object} autoSize
86 * True for the editor to automatically adopt the size of the underlying field. Otherwise, an object
87 * can be passed to indicate where to get each dimension. The available properties are 'boundEl' and
88 * 'field'. If a dimension is not specified, it will use the underlying height/width specified on
91 * <pre><code>
92 autoSize: true // The editor will be sized to the height/width of the field
96 width: 'boundEl' // The width will be determined by the width of the boundEl, the height from the editor (21)
100 width: 'field', // Width from the field
101 height: 'boundEl' // Height from the boundEl
103 * </pre></code>
106 <span id='Ext-Editor-cfg-revertInvalid'> /**
107 </span> * @cfg {Boolean} revertInvalid
108 * True to automatically revert the field value and cancel the edit when the user completes an edit and the field
109 * validation fails (defaults to true)
113 <span id='Ext-Editor-cfg-ignoreNoChange'> /**
114 </span> * @cfg {Boolean} ignoreNoChange
115 * True to skip the edit completion process (no save, no events fired) if the user completes an edit and
116 * the value has not changed (defaults to false). Applies only to string values - edits for other data types
117 * will never be ignored.
120 <span id='Ext-Editor-cfg-hideEl'> /**
121 </span> * @cfg {Boolean} hideEl
122 * False to keep the bound element visible while the editor is displayed (defaults to true)
125 <span id='Ext-Editor-cfg-value'> /**
126 </span> * @cfg {Mixed} value
127 * The data value of the underlying field (defaults to "")
131 <span id='Ext-Editor-cfg-alignment'> /**
132 </span> * @cfg {String} alignment
133 * The position to align to (see {@link Ext.core.Element#alignTo} for more details, defaults to "c-c?").
137 <span id='Ext-Editor-cfg-offsets'> /**
138 </span> * @cfg {Array} offsets
139 * The offsets to use when aligning (see {@link Ext.core.Element#alignTo} for more details. Defaults to <tt>[0, 0]</tt>.
143 <span id='Ext-Editor-cfg-shadow'> /**
144 </span> * @cfg {Boolean/String} shadow "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop"
145 * for bottom-right shadow (defaults to "frame")
149 <span id='Ext-Editor-cfg-constrain'> /**
150 </span> * @cfg {Boolean} constrain True to constrain the editor to the viewport
154 <span id='Ext-Editor-cfg-swallowKeys'> /**
155 </span> * @cfg {Boolean} swallowKeys Handle the keydown/keypress events so they don't propagate (defaults to true)
159 <span id='Ext-Editor-cfg-completeOnEnter'> /**
160 </span> * @cfg {Boolean} completeOnEnter True to complete the edit when the enter key is pressed. Defaults to <tt>true</tt>.
162 completeOnEnter : true,
164 <span id='Ext-Editor-cfg-cancelOnEsc'> /**
165 </span> * @cfg {Boolean} cancelOnEsc True to cancel the edit when the escape key is pressed. Defaults to <tt>true</tt>.
169 <span id='Ext-Editor-cfg-updateEl'> /**
170 </span> * @cfg {Boolean} updateEl True to update the innerHTML of the bound element when the update completes (defaults to false)
174 <span id='Ext-Editor-cfg-parentEl'> /**
175 </span> * @cfg {Mixed} parentEl An element to render to. Defaults to the <tt>document.body</tt>.
180 baseCls: Ext.baseCSSPrefix + 'editor',
182 initComponent : function() {
184 field = me.field = Ext.ComponentManager.create(me.field, 'textfield');
188 msgTarget: field.msgTarget == 'title' ? 'title' : 'qtip'
194 // slight delay to avoid race condition with startEdits (e.g. grid view refresh)
197 specialkey: me.onSpecialKey
201 me.mon(field, 'autosize', me.onAutoSize, me, {delay: 1});
204 constrain: me.constrain
207 me.callParent(arguments);
210 <span id='Ext-Editor-event-beforestartedit'> /**
211 </span> * @event beforestartedit
212 * Fires when editing is initiated, but before the value changes. Editing can be canceled by returning
213 * false from the handler of this event.
214 * @param {Ext.Editor} this
215 * @param {Ext.core.Element} boundEl The underlying element bound to this editor
216 * @param {Mixed} value The field value being set
219 <span id='Ext-Editor-event-startedit'> /**
220 </span> * @event startedit
221 * Fires when this editor is displayed
222 * @param {Ext.Editor} this
223 * @param {Ext.core.Element} boundEl The underlying element bound to this editor
224 * @param {Mixed} value The starting field value
227 <span id='Ext-Editor-event-beforecomplete'> /**
228 </span> * @event beforecomplete
229 * Fires after a change has been made to the field, but before the change is reflected in the underlying
230 * field. Saving the change to the field can be canceled by returning false from the handler of this event.
231 * Note that if the value has not changed and ignoreNoChange = true, the editing will still end but this
232 * event will not fire since no edit actually occurred.
233 * @param {Editor} this
234 * @param {Mixed} value The current field value
235 * @param {Mixed} startValue The original field value
238 <span id='Ext-Editor-event-complete'> /**
239 </span> * @event complete
240 * Fires after editing is complete and any changed value has been written to the underlying field.
241 * @param {Ext.Editor} this
242 * @param {Mixed} value The current field value
243 * @param {Mixed} startValue The original field value
246 <span id='Ext-Editor-event-canceledit'> /**
247 </span> * @event canceledit
248 * Fires after editing has been canceled and the editor's value has been reset.
249 * @param {Ext.Editor} this
250 * @param {Mixed} value The user-entered field value that was discarded
251 * @param {Mixed} startValue The original field value that was set back into the editor after cancel
254 <span id='Ext-Editor-event-specialkey'> /**
255 </span> * @event specialkey
256 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
257 * {@link Ext.EventObject#getKey} to determine which key was pressed.
258 * @param {Ext.Editor} this
259 * @param {Ext.form.field.Field} The field attached to this editor
260 * @param {Ext.EventObject} event The event object
267 onAutoSize: function(){
268 this.doComponentLayout();
272 onRender : function(ct, position) {
276 me.callParent(arguments);
280 // Ensure the field doesn't get submitted as part of any form
281 field.inputEl.dom.name = '';
282 if (me.swallowKeys) {
283 field.inputEl.swallowEvent([
284 'keypress', // *** Opera
285 'keydown' // *** all other browsers
291 onSpecialKey : function(field, event) {
293 key = event.getKey(),
294 complete = me.completeOnEnter && key == event.ENTER,
295 cancel = me.cancelOnEsc && key == event.ESC;
297 if (complete || cancel) {
299 // Must defer this slightly to prevent exiting edit mode before the field's own
300 // key nav can handle the enter key, e.g. selecting an item in a combobox list
301 Ext.defer(function() {
307 if (field.triggerBlur) {
313 this.fireEvent('specialkey', this, field, event);
316 <span id='Ext-Editor-method-startEdit'> /**
317 </span> * Starts the editing process and shows the editor.
318 * @param {Mixed} el The element to edit
319 * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
320 * to the innerHTML of el.
322 startEdit : function(el, value) {
327 me.boundEl = Ext.get(el);
328 value = Ext.isDefined(value) ? value : me.boundEl.dom.innerHTML;
331 me.render(me.parentEl || document.body);
334 if (me.fireEvent('beforestartedit', me, me.boundEl, value) !== false) {
335 me.startValue = value;
338 field.setValue(value);
340 field.focus(false, 10);
341 if (field.autoSize) {
348 <span id='Ext-Editor-method-realign'> /**
349 </span> * Realigns the editor to the bound field based on the current alignment config value.
350 * @param {Boolean} autoSize (optional) True to size the field to the dimensions of the bound element.
352 realign : function(autoSize) {
354 if (autoSize === true) {
355 me.doComponentLayout();
357 me.alignTo(me.boundEl, me.alignment, me.offsets);
360 <span id='Ext-Editor-method-completeEdit'> /**
361 </span> * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
362 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after edit (defaults to false)
364 completeEdit : function(remainVisible) {
373 // Assert combo values first
374 if (field.assertValue) {
378 value = me.getValue();
379 if (!field.isValid()) {
380 if (me.revertInvalid !== false) {
381 me.cancelEdit(remainVisible);
386 if (String(value) === String(me.startValue) && me.ignoreNoChange) {
387 me.hideEdit(remainVisible);
391 if (me.fireEvent('beforecomplete', me, value, me.startValue) !== false) {
392 // Grab the value again, may have changed in beforecomplete
393 value = me.getValue();
394 if (me.updateEl && me.boundEl) {
395 me.boundEl.update(value);
397 me.hideEdit(remainVisible);
398 me.fireEvent('complete', me, value, me.startValue);
403 onShow : function() {
406 me.callParent(arguments);
407 if (me.hideEl !== false) {
410 me.fireEvent("startedit", me.boundEl, me.startValue);
413 <span id='Ext-Editor-method-cancelEdit'> /**
414 </span> * Cancels the editing process and hides the editor without persisting any changes. The field value will be
415 * reverted to the original starting value.
416 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after
417 * cancel (defaults to false)
419 cancelEdit : function(remainVisible) {
421 startValue = me.startValue,
425 value = me.getValue();
426 me.setValue(startValue);
427 me.hideEdit(remainVisible);
428 me.fireEvent('canceledit', me, value, startValue);
433 hideEdit: function(remainVisible) {
434 if (remainVisible !== true) {
435 this.editing = false;
441 onBlur : function() {
444 // selectSameEditor flag allows the same editor to be started without onBlur firing on itself
445 if(me.allowBlur === true && me.editing && me.selectSameEditor !== true) {
451 onHide : function() {
460 if (field.collapse) {
465 if (me.hideEl !== false) {
468 me.callParent(arguments);
471 <span id='Ext-Editor-method-setValue'> /**
472 </span> * Sets the data value of the editor
473 * @param {Mixed} value Any valid value supported by the underlying field
475 setValue : function(value) {
476 this.field.setValue(value);
479 <span id='Ext-Editor-method-getValue'> /**
480 </span> * Gets the data value of the editor
481 * @return {Mixed} The data value
483 getValue : function() {
484 return this.field.getValue();
487 beforeDestroy : function() {
490 Ext.destroy(me.field);
495 me.callParent(arguments);