4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
14 * @extends Roo.Component
15 * A base editor field that handles displaying/hiding on demand and has some built-in sizing and event handling logic.
18 * @param {Roo.form.Field} field The Field object (or descendant)
19 * @param {Object} config The config object
21 Roo.Editor = function(field, config){
22 Roo.Editor.superclass.constructor.call(this, config);
26 * @event beforestartedit
27 * Fires when editing is initiated, but before the value changes. Editing can be canceled by returning
28 * false from the handler of this event.
29 * @param {Editor} this
30 * @param {Roo.Element} boundEl The underlying element bound to this editor
31 * @param {Mixed} value The field value being set
33 "beforestartedit" : true,
36 * Fires when this editor is displayed
37 * @param {Roo.Element} boundEl The underlying element bound to this editor
38 * @param {Mixed} value The starting field value
42 * @event beforecomplete
43 * Fires after a change has been made to the field, but before the change is reflected in the underlying
44 * field. Saving the change to the field can be canceled by returning false from the handler of this event.
45 * Note that if the value has not changed and ignoreNoChange = true, the editing will still end but this
46 * event will not fire since no edit actually occurred.
47 * @param {Editor} this
48 * @param {Mixed} value The current field value
49 * @param {Mixed} startValue The original field value
51 "beforecomplete" : true,
54 * Fires after editing is complete and any changed value has been written to the underlying field.
55 * @param {Editor} this
56 * @param {Mixed} value The current field value
57 * @param {Mixed} startValue The original field value
62 * Fires when any key related to navigation (arrows, tab, enter, esc, etc.) is pressed. You can check
63 * {@link Roo.EventObject#getKey} to determine which key was pressed.
64 * @param {Roo.form.Field} this
65 * @param {Roo.EventObject} e The event object
71 Roo.extend(Roo.Editor, Roo.Component, {
73 * @cfg {Boolean/String} autosize
74 * True for the editor to automatically adopt the size of the underlying field, "width" to adopt the width only,
75 * or "height" to adopt the height only (defaults to false)
78 * @cfg {Boolean} revertInvalid
79 * True to automatically revert the field value and cancel the edit when the user completes an edit and the field
80 * validation fails (defaults to true)
83 * @cfg {Boolean} ignoreNoChange
84 * True to skip the the edit completion process (no save, no events fired) if the user completes an edit and
85 * the value has not changed (defaults to false). Applies only to string values - edits for other data types
86 * will never be ignored.
89 * @cfg {Boolean} hideEl
90 * False to keep the bound element visible while the editor is displayed (defaults to true)
94 * The data value of the underlying field (defaults to "")
98 * @cfg {String} alignment
99 * The position to align to (see {@link Roo.Element#alignTo} for more details, defaults to "c-c?").
103 * @cfg {Boolean/String} shadow "sides" for sides/bottom only, "frame" for 4-way shadow, and "drop"
104 * for bottom-right shadow (defaults to "frame")
108 * @cfg {Boolean} constrain True to constrain the editor to the viewport
112 * @cfg {Boolean} completeOnEnter True to complete the edit when the enter key is pressed (defaults to false)
114 completeOnEnter : false,
116 * @cfg {Boolean} cancelOnEsc True to cancel the edit when the escape key is pressed (defaults to false)
120 * @cfg {Boolean} updateEl True to update the innerHTML of the bound element when the update completes (defaults to false)
125 onRender : function(ct, position){
126 this.el = new Roo.Layer({
133 constrain: this.constrain
135 this.el.setStyle("overflow", Roo.isGecko ? "auto" : "hidden");
136 if(this.field.msgTarget != 'title'){
137 this.field.msgTarget = 'qtip';
139 this.field.render(this.el);
141 this.field.el.dom.setAttribute('autocomplete', 'off');
143 this.field.on("specialkey", this.onSpecialKey, this);
144 if(this.swallowKeys){
145 this.field.el.swallowEvent(['keydown','keypress']);
148 this.field.on("blur", this.onBlur, this);
150 this.field.on("autosize", this.el.sync, this.el, {delay:1});
154 onSpecialKey : function(field, e){
155 Roo.log('editor onSpecialKey');
156 if(this.completeOnEnter && e.getKey() == e.ENTER){
159 }else if(this.cancelOnEsc && e.getKey() == e.ESC){
162 this.fireEvent('specialkey', field, e);
167 * Starts the editing process and shows the editor.
168 * @param {String/HTMLElement/Element} el The element to edit
169 * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
170 * to the innerHTML of el.
172 startEdit : function(el, value){
176 this.boundEl = Roo.get(el);
177 var v = value !== undefined ? value : this.boundEl.dom.innerHTML;
179 this.render(this.parentEl || document.body);
181 if(this.fireEvent("beforestartedit", this, this.boundEl, v) === false){
185 this.field.setValue(v);
187 var sz = this.boundEl.getSize();
188 switch(this.autoSize){
190 this.setSize(sz.width, "");
193 this.setSize("", sz.height);
196 this.setSize(sz.width, sz.height);
199 this.el.alignTo(this.boundEl, this.alignment);
202 Roo.QuickTips.disable();
208 * Sets the height and width of this editor.
209 * @param {Number} width The new width
210 * @param {Number} height The new height
212 setSize : function(w, h){
213 this.field.setSize(w, h);
220 * Realigns the editor to the bound field based on the current alignment config value.
222 realign : function(){
223 this.el.alignTo(this.boundEl, this.alignment);
227 * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
228 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after edit (defaults to false)
230 completeEdit : function(remainVisible){
234 var v = this.getValue();
235 if(this.revertInvalid !== false && !this.field.isValid()){
237 this.cancelEdit(true);
239 if(String(v) === String(this.startValue) && this.ignoreNoChange){
240 this.editing = false;
244 if(this.fireEvent("beforecomplete", this, v, this.startValue) !== false){
245 this.editing = false;
246 if(this.updateEl && this.boundEl){
247 this.boundEl.update(v);
249 if(remainVisible !== true){
252 this.fireEvent("complete", this, v, this.startValue);
259 if(this.hideEl !== false){
263 if(Roo.isIE && !this.fixIEFocus){ // IE has problems with focusing the first time
264 this.fixIEFocus = true;
265 this.deferredFocus.defer(50, this);
269 this.fireEvent("startedit", this.boundEl, this.startValue);
272 deferredFocus : function(){
279 * Cancels the editing process and hides the editor without persisting any changes. The field value will be
280 * reverted to the original starting value.
281 * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after
282 * cancel (defaults to false)
284 cancelEdit : function(remainVisible){
286 this.setValue(this.startValue);
287 if(remainVisible !== true){
295 if(this.allowBlur !== true && this.editing){
307 if(this.field.collapse){
308 this.field.collapse();
311 if(this.hideEl !== false){
315 Roo.QuickTips.enable();
320 * Sets the data value of the editor
321 * @param {Mixed} value Any valid value supported by the underlying field
323 setValue : function(v){
324 this.field.setValue(v);
328 * Gets the data value of the editor
329 * @return {Mixed} The data value
331 getValue : function(){
332 return this.field.getValue();