Roo/Editor.js
[roojs1] / Roo / Editor.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 /**
13  * @class Roo.Editor
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.
16  * @constructor
17  * Create a new Editor
18  * @param {Roo.form.Field} field The Field object (or descendant)
19  * @param {Object} config The config object
20  */
21 Roo.Editor = function(field, config){
22     Roo.Editor.superclass.constructor.call(this, config);
23     this.field = field;
24     this.addEvents({
25         /**
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
32              */
33         "beforestartedit" : true,
34         /**
35              * @event startedit
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
39              */
40         "startedit" : true,
41         /**
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
50              */
51         "beforecomplete" : true,
52         /**
53              * @event complete
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
58              */
59         "complete" : true,
60         /**
61          * @event specialkey
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
66          */
67         "specialkey" : true
68     });
69 };
70
71 Roo.extend(Roo.Editor, Roo.Component, {
72     /**
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)
76      */
77     /**
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)
81      */
82     /**
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.
87      */
88     /**
89      * @cfg {Boolean} hideEl
90      * False to keep the bound element visible while the editor is displayed (defaults to true)
91      */
92     /**
93      * @cfg {Mixed} value
94      * The data value of the underlying field (defaults to "")
95      */
96     value : "",
97     /**
98      * @cfg {String} alignment
99      * The position to align to (see {@link Roo.Element#alignTo} for more details, defaults to "c-c?").
100      */
101     alignment: "c-c?",
102     /**
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")
105      */
106     shadow : "frame",
107     /**
108      * @cfg {Boolean} constrain True to constrain the editor to the viewport
109      */
110     constrain : false,
111     /**
112      * @cfg {Boolean} completeOnEnter True to complete the edit when the enter key is pressed (defaults to false)
113      */
114     completeOnEnter : false,
115     /**
116      * @cfg {Boolean} cancelOnEsc True to cancel the edit when the escape key is pressed (defaults to false)
117      */
118     cancelOnEsc : false,
119     /**
120      * @cfg {Boolean} updateEl True to update the innerHTML of the bound element when the update completes (defaults to false)
121      */
122     updateEl : false,
123
124     // private
125     onRender : function(ct, position){
126         this.el = new Roo.Layer({
127             shadow: this.shadow,
128             cls: "x-editor",
129             parentEl : ct,
130             shim : this.shim,
131             shadowOffset:4,
132             id: this.id,
133             constrain: this.constrain
134         });
135         this.el.setStyle("overflow", Roo.isGecko ? "auto" : "hidden");
136         if(this.field.msgTarget != 'title'){
137             this.field.msgTarget = 'qtip';
138         }
139         this.field.render(this.el);
140         if(Roo.isGecko){
141             this.field.el.dom.setAttribute('autocomplete', 'off');
142         }
143         this.field.on("specialkey", this.onSpecialKey, this);
144         if(this.swallowKeys){
145             this.field.el.swallowEvent(['keydown','keypress']);
146         }
147         this.field.show();
148         this.field.on("blur", this.onBlur, this);
149         if(this.field.grow){
150             this.field.on("autosize", this.el.sync,  this.el, {delay:1});
151         }
152     },
153
154     onSpecialKey : function(field, e){
155         //Roo.log('editor onSpecialKey');
156         if(this.completeOnEnter && e.getKey() == e.ENTER){
157             e.stopEvent();
158             this.completeEdit();
159             return;
160         }
161         if(e.getKey() == e.ENTER){    
162             return;
163         }
164         if(this.cancelOnEsc && e.getKey() == e.ESC){
165             this.cancelEdit();
166             return;
167         } 
168         this.fireEvent('specialkey', field, e);
169     
170     },
171
172     /**
173      * Starts the editing process and shows the editor.
174      * @param {String/HTMLElement/Element} el The element to edit
175      * @param {String} value (optional) A value to initialize the editor with. If a value is not provided, it defaults
176       * to the innerHTML of el.
177      */
178     startEdit : function(el, value){
179         if(this.editing){
180             this.completeEdit();
181         }
182         this.boundEl = Roo.get(el);
183         var v = value !== undefined ? value : this.boundEl.dom.innerHTML;
184         if(!this.rendered){
185             this.render(this.parentEl || document.body);
186         }
187         if(this.fireEvent("beforestartedit", this, this.boundEl, v) === false){
188             return;
189         }
190         this.startValue = v;
191         this.field.setValue(v);
192         if(this.autoSize){
193             var sz = this.boundEl.getSize();
194             switch(this.autoSize){
195                 case "width":
196                 this.setSize(sz.width,  "");
197                 break;
198                 case "height":
199                 this.setSize("",  sz.height);
200                 break;
201                 default:
202                 this.setSize(sz.width,  sz.height);
203             }
204         }
205         this.el.alignTo(this.boundEl, this.alignment);
206         this.editing = true;
207         if(Roo.QuickTips){
208             Roo.QuickTips.disable();
209         }
210         this.show();
211     },
212
213     /**
214      * Sets the height and width of this editor.
215      * @param {Number} width The new width
216      * @param {Number} height The new height
217      */
218     setSize : function(w, h){
219         this.field.setSize(w, h);
220         if(this.el){
221             this.el.sync();
222         }
223     },
224
225     /**
226      * Realigns the editor to the bound field based on the current alignment config value.
227      */
228     realign : function(){
229         this.el.alignTo(this.boundEl, this.alignment);
230     },
231
232     /**
233      * Ends the editing process, persists the changed value to the underlying field, and hides the editor.
234      * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after edit (defaults to false)
235      */
236     completeEdit : function(remainVisible){
237         if(!this.editing){
238             return;
239         }
240         var v = this.getValue();
241         if(this.revertInvalid !== false && !this.field.isValid()){
242             v = this.startValue;
243             this.cancelEdit(true);
244         }
245         if(String(v) === String(this.startValue) && this.ignoreNoChange){
246             this.editing = false;
247             this.hide();
248             return;
249         }
250         if(this.fireEvent("beforecomplete", this, v, this.startValue) !== false){
251             this.editing = false;
252             if(this.updateEl && this.boundEl){
253                 this.boundEl.update(v);
254             }
255             if(remainVisible !== true){
256                 this.hide();
257             }
258             this.fireEvent("complete", this, v, this.startValue);
259         }
260     },
261
262     // private
263     onShow : function(){
264         this.el.show();
265         if(this.hideEl !== false){
266             this.boundEl.hide();
267         }
268         this.field.show();
269         if(Roo.isIE && !this.fixIEFocus){ // IE has problems with focusing the first time
270             this.fixIEFocus = true;
271             this.deferredFocus.defer(50, this);
272         }else{
273             this.field.focus();
274         }
275         this.fireEvent("startedit", this.boundEl, this.startValue);
276     },
277
278     deferredFocus : function(){
279         if(this.editing){
280             this.field.focus();
281         }
282     },
283
284     /**
285      * Cancels the editing process and hides the editor without persisting any changes.  The field value will be
286      * reverted to the original starting value.
287      * @param {Boolean} remainVisible Override the default behavior and keep the editor visible after
288      * cancel (defaults to false)
289      */
290     cancelEdit : function(remainVisible){
291         if(this.editing){
292             this.setValue(this.startValue);
293             if(remainVisible !== true){
294                 this.hide();
295             }
296         }
297     },
298
299     // private
300     onBlur : function(){
301         if(this.allowBlur !== true && this.editing){
302             this.completeEdit();
303         }
304     },
305
306     // private
307     onHide : function(){
308         if(this.editing){
309             this.completeEdit();
310             return;
311         }
312         this.field.blur();
313         if(this.field.collapse){
314             this.field.collapse();
315         }
316         this.el.hide();
317         if(this.hideEl !== false){
318             this.boundEl.show();
319         }
320         if(Roo.QuickTips){
321             Roo.QuickTips.enable();
322         }
323     },
324
325     /**
326      * Sets the data value of the editor
327      * @param {Mixed} value Any valid value supported by the underlying field
328      */
329     setValue : function(v){
330         this.field.setValue(v);
331     },
332
333     /**
334      * Gets the data value of the editor
335      * @return {Mixed} The data value
336      */
337     getValue : function(){
338         return this.field.getValue();
339     }
340 });