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