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