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'>/**
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
76 * field is blurred. Defaults to <tt>true</tt>.
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
105 * validation fails (defaults to true)
109 <span id='Ext-Editor-cfg-ignoreNoChange'> /**
110 </span> * @cfg {Boolean} ignoreNoChange
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 (defaults to false). 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
118 * False to keep the bound element visible while the editor is displayed (defaults to true)
121 <span id='Ext-Editor-cfg-value'> /**
122 </span> * @cfg {Mixed} value
123 * The data value of the underlying field (defaults to "")
127 <span id='Ext-Editor-cfg-alignment'> /**
128 </span> * @cfg {String} alignment
129 * The position to align to (see {@link Ext.core.Element#alignTo} for more details, defaults to "c-c?").
133 <span id='Ext-Editor-cfg-offsets'> /**
134 </span> * @cfg {Array} offsets
135 * The offsets to use when aligning (see {@link Ext.core.Element#alignTo} for more details. Defaults to <tt>[0, 0]</tt>.
139 <span id='Ext-Editor-cfg-shadow'> /**
140 </span> * @cfg {Boolean/String} shadow "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop"
141 * for bottom-right shadow (defaults to "frame")
145 <span id='Ext-Editor-cfg-constrain'> /**
146 </span> * @cfg {Boolean} constrain True to constrain the editor to the viewport
150 <span id='Ext-Editor-cfg-swallowKeys'> /**
151 </span> * @cfg {Boolean} swallowKeys Handle the keydown/keypress events so they don't propagate (defaults to true)
155 <span id='Ext-Editor-cfg-completeOnEnter'> /**
156 </span> * @cfg {Boolean} completeOnEnter True to complete the edit when the enter key is pressed. Defaults to <tt>true</tt>.
158 completeOnEnter : true,
160 <span id='Ext-Editor-cfg-cancelOnEsc'> /**
161 </span> * @cfg {Boolean} cancelOnEsc True to cancel the edit when the escape key is pressed. Defaults to <tt>true</tt>.
165 <span id='Ext-Editor-cfg-updateEl'> /**
166 </span> * @cfg {Boolean} updateEl True to update the innerHTML of the bound element when the update completes (defaults to false)
170 <span id='Ext-Editor-cfg-parentEl'> /**
171 </span> * @cfg {Mixed} parentEl An element to render to. Defaults to the <tt>document.body</tt>.
176 baseCls: Ext.baseCSSPrefix + 'editor',
178 initComponent : function() {
180 field = me.field = Ext.ComponentManager.create(me.field, 'textfield');
184 msgTarget: field.msgTarget == 'title' ? 'title' : 'qtip'
190 // slight delay to avoid race condition with startEdits (e.g. grid view refresh)
193 specialkey: me.onSpecialKey
197 me.mon(field, 'autosize', me.onAutoSize, me, {delay: 1});
200 constrain: me.constrain
203 me.callParent(arguments);
206 <span id='Ext-Editor-event-beforestartedit'> /**
207 </span> * @event beforestartedit
208 * Fires when editing is initiated, but before the value changes. Editing can be canceled by returning
209 * false from the handler of this event.
210 * @param {Ext.Editor} this
211 * @param {Ext.core.Element} boundEl The underlying element bound to this editor
212 * @param {Mixed} value The field value being set
215 <span id='Ext-Editor-event-startedit'> /**
216 </span> * @event startedit
217 * Fires when this editor is displayed
218 * @param {Ext.Editor} this
219 * @param {Ext.core.Element} boundEl The underlying element bound to this editor
220 * @param {Mixed} value The starting field value
223 <span id='Ext-Editor-event-beforecomplete'> /**
224 </span> * @event beforecomplete
225 * Fires after a change has been made to the field, but before the change is reflected in the underlying
226 * field. Saving the change to the field can be canceled by returning false from the handler of this event.
227 * Note that if the value has not changed and ignoreNoChange = true, the editing will still end but this
228 * event will not fire since no edit actually occurred.
229 * @param {Editor} this
230 * @param {Mixed} value The current field value
231 * @param {Mixed} startValue The original field value
234 <span id='Ext-Editor-event-complete'> /**
235 </span> * @event complete
236 * Fires after editing is complete and any changed value has been written to the underlying field.
237 * @param {Ext.Editor} this
238 * @param {Mixed} value The current field value
239 * @param {Mixed} startValue The original field value
242 <span id='Ext-Editor-event-canceledit'> /**
243 </span> * @event canceledit
244 * Fires after editing has been canceled and the editor's value has been reset.
245 * @param {Ext.Editor} this
246 * @param {Mixed} value The user-entered field value that was discarded
247 * @param {Mixed} startValue The original field value that was set back into the editor after cancel
250 <span id='Ext-Editor-event-specialkey'> /**
251 </span> * @event specialkey
252 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
253 * {@link Ext.EventObject#getKey} to determine which key was pressed.
254 * @param {Ext.Editor} this
255 * @param {Ext.form.field.Field} The field attached to this editor
256 * @param {Ext.EventObject} event The event object
263 onAutoSize: function(){
264 this.doComponentLayout();
268 onRender : function(ct, position) {
272 me.callParent(arguments);
276 // Ensure the field doesn't get submitted as part of any form
277 field.inputEl.dom.name = '';
278 if (me.swallowKeys) {
279 field.inputEl.swallowEvent([
280 'keypress', // *** Opera
281 'keydown' // *** all other browsers
287 onSpecialKey : function(field, event) {
289 key = event.getKey(),
290 complete = me.completeOnEnter && key == event.ENTER,
291 cancel = me.cancelOnEsc && key == event.ESC;
293 if (complete || cancel) {
295 // Must defer this slightly to prevent exiting edit mode before the field's own
296 // key nav can handle the enter key, e.g. selecting an item in a combobox list
297 Ext.defer(function() {
303 if (field.triggerBlur) {
309 this.fireEvent('specialkey', this, field, event);
312 <span id='Ext-Editor-method-startEdit'> /**
313 </span> * Starts the editing process and shows the editor.
314 * @param {Mixed} el The element to edit
315 * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
316 * to the innerHTML of el.
318 startEdit : function(el, value) {
323 me.boundEl = Ext.get(el);
324 value = Ext.isDefined(value) ? value : me.boundEl.dom.innerHTML;
327 me.render(me.parentEl || document.body);
330 if (me.fireEvent('beforestartedit', me, me.boundEl, value) !== false) {
331 me.startValue = value;
334 field.setValue(value);
336 field.focus(false, 10);
337 if (field.autoSize) {
344 <span id='Ext-Editor-method-realign'> /**
345 </span> * Realigns the editor to the bound field based on the current alignment config value.
346 * @param {Boolean} autoSize (optional) True to size the field to the dimensions of the bound element.
348 realign : function(autoSize) {
350 if (autoSize === true) {
351 me.doComponentLayout();
353 me.alignTo(me.boundEl, me.alignment, me.offsets);
356 <span id='Ext-Editor-method-completeEdit'> /**
357 </span> * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
358 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after edit (defaults to false)
360 completeEdit : function(remainVisible) {
369 // Assert combo values first
370 if (field.assertValue) {
374 value = me.getValue();
375 if (!field.isValid()) {
376 if (me.revertInvalid !== false) {
377 me.cancelEdit(remainVisible);
382 if (String(value) === String(me.startValue) && me.ignoreNoChange) {
383 me.hideEdit(remainVisible);
387 if (me.fireEvent('beforecomplete', me, value, me.startValue) !== false) {
388 // Grab the value again, may have changed in beforecomplete
389 value = me.getValue();
390 if (me.updateEl && me.boundEl) {
391 me.boundEl.update(value);
393 me.hideEdit(remainVisible);
394 me.fireEvent('complete', me, value, me.startValue);
399 onShow : function() {
402 me.callParent(arguments);
403 if (me.hideEl !== false) {
406 me.fireEvent("startedit", me.boundEl, me.startValue);
409 <span id='Ext-Editor-method-cancelEdit'> /**
410 </span> * Cancels the editing process and hides the editor without persisting any changes. The field value will be
411 * reverted to the original starting value.
412 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after
413 * cancel (defaults to false)
415 cancelEdit : function(remainVisible) {
417 startValue = me.startValue,
421 value = me.getValue();
422 me.setValue(startValue);
423 me.hideEdit(remainVisible);
424 me.fireEvent('canceledit', me, value, startValue);
429 hideEdit: function(remainVisible) {
430 if (remainVisible !== true) {
431 this.editing = false;
437 onBlur : function() {
440 // selectSameEditor flag allows the same editor to be started without onBlur firing on itself
441 if(me.allowBlur === true && me.editing && me.selectSameEditor !== true) {
447 onHide : function() {
456 if (field.collapse) {
461 if (me.hideEl !== false) {
464 me.callParent(arguments);
467 <span id='Ext-Editor-method-setValue'> /**
468 </span> * Sets the data value of the editor
469 * @param {Mixed} value Any valid value supported by the underlying field
471 setValue : function(value) {
472 this.field.setValue(value);
475 <span id='Ext-Editor-method-getValue'> /**
476 </span> * Gets the data value of the editor
477 * @return {Mixed} The data value
479 getValue : function() {
480 return this.field.getValue();
483 beforeDestroy : function() {
486 Ext.destroy(me.field);
491 me.callParent(arguments);