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