Roo/form/ComboBoxArray.js
[roojs1] / Roo / form / HtmlEditor.js
1 //<script type="text/javascript">
2
3 /*
4  * Ext JS Library 1.1.1
5  * Copyright(c) 2006-2007, Ext JS, LLC.
6  * Licence LGPL
7  * 
8  */
9  
10  
11 Roo.form.HtmlEditor = function(config){
12     
13     
14     
15     Roo.form.HtmlEditor.superclass.constructor.call(this, config);
16     
17     if (!this.toolbars) {
18         this.toolbars = [];
19     }
20     this.editorcore = new Roo.HtmlEditorCore(Roo.apply({ owner : this} , config));
21     
22     
23 };
24
25 /**
26  * @class Roo.form.HtmlEditor
27  * @extends Roo.form.Field
28  * Provides a lightweight HTML Editor component.
29  *
30  * This has been tested on Fireforx / Chrome.. IE may not be so great..
31  * 
32  * <br><br><b>Note: The focus/blur and validation marking functionality inherited from Ext.form.Field is NOT
33  * supported by this editor.</b><br/><br/>
34  * An Editor is a sensitive component that can't be used in all spots standard fields can be used. Putting an Editor within
35  * any element that has display set to 'none' can cause problems in Safari and Firefox.<br/><br/>
36  */
37 Roo.extend(Roo.form.HtmlEditor, Roo.form.Field, {
38     /**
39      * @cfg {Boolean} clearUp
40      */
41     clearUp : true,
42       /**
43      * @cfg {Array} toolbars Array of toolbars. - defaults to just the Standard one
44      */
45     toolbars : false,
46    
47      /**
48      * @cfg {String} resizable  's' or 'se' or 'e' - wrapps the element in a
49      *                        Roo.resizable.
50      */
51     resizable : false,
52      /**
53      * @cfg {Number} height (in pixels)
54      */   
55     height: 300,
56    /**
57      * @cfg {Number} width (in pixels)
58      */   
59     width: 500,
60     
61     /**
62      * @cfg {Array} stylesheets url of stylesheets. set to [] to disable stylesheets.
63      * 
64      */
65     stylesheets: false,
66     
67     // id of frame..
68     frameId: false,
69     
70     // private properties
71     validationEvent : false,
72     deferHeight: true,
73     initialized : false,
74     activated : false,
75     
76     onFocus : Roo.emptyFn,
77     iframePad:3,
78     hideMode:'offsets',
79     
80     defaultAutoCreate : { // modified by initCompnoent..
81         tag: "textarea",
82         style:"width:500px;height:300px;",
83         autocomplete: "off"
84     },
85
86     // private
87     initComponent : function(){
88         this.addEvents({
89             /**
90              * @event initialize
91              * Fires when the editor is fully initialized (including the iframe)
92              * @param {HtmlEditor} this
93              */
94             initialize: true,
95             /**
96              * @event activate
97              * Fires when the editor is first receives the focus. Any insertion must wait
98              * until after this event.
99              * @param {HtmlEditor} this
100              */
101             activate: true,
102              /**
103              * @event beforesync
104              * Fires before the textarea is updated with content from the editor iframe. Return false
105              * to cancel the sync.
106              * @param {HtmlEditor} this
107              * @param {String} html
108              */
109             beforesync: true,
110              /**
111              * @event beforepush
112              * Fires before the iframe editor is updated with content from the textarea. Return false
113              * to cancel the push.
114              * @param {HtmlEditor} this
115              * @param {String} html
116              */
117             beforepush: true,
118              /**
119              * @event sync
120              * Fires when the textarea is updated with content from the editor iframe.
121              * @param {HtmlEditor} this
122              * @param {String} html
123              */
124             sync: true,
125              /**
126              * @event push
127              * Fires when the iframe editor is updated with content from the textarea.
128              * @param {HtmlEditor} this
129              * @param {String} html
130              */
131             push: true,
132              /**
133              * @event editmodechange
134              * Fires when the editor switches edit modes
135              * @param {HtmlEditor} this
136              * @param {Boolean} sourceEdit True if source edit, false if standard editing.
137              */
138             editmodechange: true,
139             /**
140              * @event editorevent
141              * Fires when on any editor (mouse up/down cursor movement etc.) - used for toolbar hooks.
142              * @param {HtmlEditor} this
143              */
144             editorevent: true,
145             /**
146              * @event firstfocus
147              * Fires when on first focus - needed by toolbars..
148              * @param {HtmlEditor} this
149              */
150             firstfocus: true,
151             /**
152              * @event autosave
153              * Auto save the htmlEditor value as a file into Events
154              * @param {HtmlEditor} this
155              */
156             autosave: true,
157             /**
158              * @event savedpreview
159              * preview the saved version of htmlEditor
160              * @param {HtmlEditor} this
161              */
162             savedpreview: true
163         });
164         this.defaultAutoCreate =  {
165             tag: "textarea",
166             style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
167             autocomplete: "off"
168         };
169     },
170
171     /**
172      * Protected method that will not generally be called directly. It
173      * is called when the editor creates its toolbar. Override this method if you need to
174      * add custom toolbar buttons.
175      * @param {HtmlEditor} editor
176      */
177     createToolbar : function(editor){
178         Roo.log("create toolbars");
179         if (!editor.toolbars || !editor.toolbars.length) {
180             editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
181         }
182         
183         for (var i =0 ; i < editor.toolbars.length;i++) {
184             editor.toolbars[i] = Roo.factory(
185                     typeof(editor.toolbars[i]) == 'string' ?
186                         { xtype: editor.toolbars[i]} : editor.toolbars[i],
187                 Roo.form.HtmlEditor);
188             editor.toolbars[i].init(editor);
189         }
190          
191         
192     },
193
194      
195     // private
196     onRender : function(ct, position)
197     {
198         var _t = this;
199         Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
200         
201         this.wrap = this.el.wrap({
202             cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
203         });
204         
205         this.editorcore.onRender(ct, position);
206          
207         if (this.resizable) {
208             this.resizeEl = new Roo.Resizable(this.wrap, {
209                 pinned : true,
210                 wrap: true,
211                 dynamic : true,
212                 minHeight : this.height,
213                 height: this.height,
214                 handles : this.resizable,
215                 width: this.width,
216                 listeners : {
217                     resize : function(r, w, h) {
218                         _t.onResize(w,h); // -something
219                     }
220                 }
221             });
222             
223         }
224         this.createToolbar(this);
225        
226         
227         if(!this.width){
228             this.setSize(this.wrap.getSize());
229         }
230         if (this.resizeEl) {
231             this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
232             // should trigger onReize..
233         }
234         
235 //        if(this.autosave && this.w){
236 //            this.autoSaveFn = setInterval(this.autosave, 1000);
237 //        }
238     },
239
240     // private
241     onResize : function(w, h)
242     {
243         //Roo.log('resize: ' +w + ',' + h );
244         Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
245         var ew = false;
246         var eh = false;
247         
248         if(this.el ){
249             if(typeof w == 'number'){
250                 var aw = w - this.wrap.getFrameWidth('lr');
251                 this.el.setWidth(this.adjustWidth('textarea', aw));
252                 ew = aw;
253             }
254             if(typeof h == 'number'){
255                 var tbh = 0;
256                 for (var i =0; i < this.toolbars.length;i++) {
257                     // fixme - ask toolbars for heights?
258                     tbh += this.toolbars[i].tb.el.getHeight();
259                     if (this.toolbars[i].footer) {
260                         tbh += this.toolbars[i].footer.el.getHeight();
261                     }
262                 }
263                 
264                 
265                 
266                 
267                 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
268                 ah -= 5; // knock a few pixes off for look..
269                 this.el.setHeight(this.adjustWidth('textarea', ah));
270                 var eh = ah;
271             }
272         }
273         Roo.log('onResize:' + [w,h,ew,eh].join(',') );
274         this.editorcore.onResize(ew,eh);
275         
276     },
277
278     /**
279      * Toggles the editor between standard and source edit mode.
280      * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
281      */
282     toggleSourceEdit : function(sourceEditMode)
283     {
284         this.editorcore.toggleSourceEdit(sourceEditMode);
285         
286         if(this.editorcore.sourceEditMode){
287             Roo.log('editor - showing textarea');
288             
289 //            Roo.log('in');
290 //            Roo.log(this.syncValue());
291             this.editorcore.syncValue();
292             this.el.removeClass('x-hidden');
293             this.el.dom.removeAttribute('tabIndex');
294             this.el.focus();
295         }else{
296             Roo.log('editor - hiding textarea');
297 //            Roo.log('out')
298 //            Roo.log(this.pushValue()); 
299             this.editorcore.pushValue();
300             
301             this.el.addClass('x-hidden');
302             this.el.dom.setAttribute('tabIndex', -1);
303             //this.deferFocus();
304         }
305          
306         this.setSize(this.wrap.getSize());
307         this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
308     },
309  
310     // private (for BoxComponent)
311     adjustSize : Roo.BoxComponent.prototype.adjustSize,
312
313     // private (for BoxComponent)
314     getResizeEl : function(){
315         return this.wrap;
316     },
317
318     // private (for BoxComponent)
319     getPositionEl : function(){
320         return this.wrap;
321     },
322
323     // private
324     initEvents : function(){
325         this.originalValue = this.getValue();
326     },
327
328     /**
329      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
330      * @method
331      */
332     markInvalid : Roo.emptyFn,
333     /**
334      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
335      * @method
336      */
337     clearInvalid : Roo.emptyFn,
338
339     setValue : function(v){
340         Roo.form.HtmlEditor.superclass.setValue.call(this, v);
341         this.editorcore.pushValue();
342     },
343
344      
345     // private
346     deferFocus : function(){
347         this.focus.defer(10, this);
348     },
349
350     // doc'ed in Field
351     focus : function(){
352         this.editorcore.focus();
353         
354     },
355       
356
357     // private
358     onDestroy : function(){
359         
360         
361         
362         if(this.rendered){
363             
364             for (var i =0; i < this.toolbars.length;i++) {
365                 // fixme - ask toolbars for heights?
366                 this.toolbars[i].onDestroy();
367             }
368             
369             this.wrap.dom.innerHTML = '';
370             this.wrap.remove();
371         }
372     },
373
374     // private
375     onFirstFocus : function(){
376         //Roo.log("onFirstFocus");
377         this.editorcore.onFirstFocus();
378          for (var i =0; i < this.toolbars.length;i++) {
379             this.toolbars[i].onFirstFocus();
380         }
381         
382     },
383     
384     // private
385     syncValue : function()
386     {
387         this.editorcore.syncValue();
388     },
389     
390     pushValue : function()
391     {
392         this.editorcore.pushValue();
393     }
394      
395     
396     // hide stuff that is not compatible
397     /**
398      * @event blur
399      * @hide
400      */
401     /**
402      * @event change
403      * @hide
404      */
405     /**
406      * @event focus
407      * @hide
408      */
409     /**
410      * @event specialkey
411      * @hide
412      */
413     /**
414      * @cfg {String} fieldClass @hide
415      */
416     /**
417      * @cfg {String} focusClass @hide
418      */
419     /**
420      * @cfg {String} autoCreate @hide
421      */
422     /**
423      * @cfg {String} inputType @hide
424      */
425     /**
426      * @cfg {String} invalidClass @hide
427      */
428     /**
429      * @cfg {String} invalidText @hide
430      */
431     /**
432      * @cfg {String} msgFx @hide
433      */
434     /**
435      * @cfg {String} validateOnBlur @hide
436      */
437 });
438  
439