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     // 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         this.defaultAutoCreate =  {
149             tag: "textarea",
150             style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
151             autocomplete: "off"
152         };
153     },
154
155     /**
156      * Protected method that will not generally be called directly. It
157      * is called when the editor creates its toolbar. Override this method if you need to
158      * add custom toolbar buttons.
159      * @param {HtmlEditor} editor
160      */
161     createToolbar : function(editor){
162         Roo.log("create toolbars");
163         if (!editor.toolbars || !editor.toolbars.length) {
164             editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
165         }
166         
167         for (var i =0 ; i < editor.toolbars.length;i++) {
168             editor.toolbars[i] = Roo.factory(
169                     typeof(editor.toolbars[i]) == 'string' ?
170                         { xtype: editor.toolbars[i]} : editor.toolbars[i],
171                 Roo.form.HtmlEditor);
172             editor.toolbars[i].init(editor);
173         }
174          
175         
176     },
177
178      
179     // private
180     onRender : function(ct, position)
181     {
182         var _t = this;
183         Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
184         
185         this.wrap = this.el.wrap({
186             cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
187         });
188         
189         this.editorcore.onRender(ct, position);
190          
191         if (this.resizable) {
192             this.resizeEl = new Roo.Resizable(this.wrap, {
193                 pinned : true,
194                 wrap: true,
195                 dynamic : true,
196                 minHeight : this.height,
197                 height: this.height,
198                 handles : this.resizable,
199                 width: this.width,
200                 listeners : {
201                     resize : function(r, w, h) {
202                         _t.onResize(w,h); // -something
203                     }
204                 }
205             });
206             
207         }
208         this.createToolbar(this);
209        
210         
211         if(!this.width){
212             this.setSize(this.wrap.getSize());
213         }
214         if (this.resizeEl) {
215             this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
216             // should trigger onReize..
217         }
218     },
219
220     // private
221     onResize : function(w, h)
222     {
223         //Roo.log('resize: ' +w + ',' + h );
224         Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
225         var ew = false;
226         var eh = false;
227         
228         if(this.el ){
229             if(typeof w == 'number'){
230                 var aw = w - this.wrap.getFrameWidth('lr');
231                 this.el.setWidth(this.adjustWidth('textarea', aw));
232                 ew = aw;
233             }
234             if(typeof h == 'number'){
235                 var tbh = 0;
236                 for (var i =0; i < this.toolbars.length;i++) {
237                     // fixme - ask toolbars for heights?
238                     tbh += this.toolbars[i].tb.el.getHeight();
239                     if (this.toolbars[i].footer) {
240                         tbh += this.toolbars[i].footer.el.getHeight();
241                     }
242                 }
243                 
244                 
245                 
246                 
247                 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
248                 ah -= 5; // knock a few pixes off for look..
249                 this.el.setHeight(this.adjustWidth('textarea', ah));
250                 var eh = ah;
251             }
252         }
253         Roo.log('onResize:' + [w,h,ew,eh].join(',') );
254         this.editorcore.onResize(ew,eh);
255         
256     },
257
258     /**
259      * Toggles the editor between standard and source edit mode.
260      * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
261      */
262     toggleSourceEdit : function(sourceEditMode)
263     {
264         this.editorcore.toggleSourceEdit(sourceEditMode);
265         
266         if(this.editorcore.sourceEditMode){
267             Roo.log('editor - showing textarea');
268             
269 //            Roo.log('in');
270 //            Roo.log(this.syncValue());
271             this.editorcore.syncValue();
272             this.el.removeClass('x-hidden');
273             this.el.dom.removeAttribute('tabIndex');
274             this.el.focus();
275         }else{
276             Roo.log('editor - hiding textarea');
277 //            Roo.log('out')
278 //            Roo.log(this.pushValue()); 
279             this.editorcore.pushValue();
280             
281             this.el.addClass('x-hidden');
282             this.el.dom.setAttribute('tabIndex', -1);
283             //this.deferFocus();
284         }
285          
286         this.setSize(this.wrap.getSize());
287         this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
288     },
289  
290     // private (for BoxComponent)
291     adjustSize : Roo.BoxComponent.prototype.adjustSize,
292
293     // private (for BoxComponent)
294     getResizeEl : function(){
295         return this.wrap;
296     },
297
298     // private (for BoxComponent)
299     getPositionEl : function(){
300         return this.wrap;
301     },
302
303     // private
304     initEvents : function(){
305         this.originalValue = this.getValue();
306     },
307
308     /**
309      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
310      * @method
311      */
312     markInvalid : Roo.emptyFn,
313     /**
314      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
315      * @method
316      */
317     clearInvalid : Roo.emptyFn,
318
319     setValue : function(v){
320         Roo.form.HtmlEditor.superclass.setValue.call(this, v);
321         this.editorcore.pushValue();
322     },
323
324      
325     // private
326     deferFocus : function(){
327         this.focus.defer(10, this);
328     },
329
330     // doc'ed in Field
331     focus : function(){
332         this.editorcore.focus();
333         
334     },
335       
336
337     // private
338     onDestroy : function(){
339         
340         
341         
342         if(this.rendered){
343             
344             for (var i =0; i < this.toolbars.length;i++) {
345                 // fixme - ask toolbars for heights?
346                 this.toolbars[i].onDestroy();
347             }
348             
349             this.wrap.dom.innerHTML = '';
350             this.wrap.remove();
351         }
352     },
353
354     // private
355     onFirstFocus : function(){
356         //Roo.log("onFirstFocus");
357         this.editorcore.onFirstFocus();
358          for (var i =0; i < this.toolbars.length;i++) {
359             this.toolbars[i].onFirstFocus();
360         }
361         
362     },
363     
364     // private
365     syncValue : function()
366     {
367         this.editorcore.syncValue();
368     }
369      
370     
371     // hide stuff that is not compatible
372     /**
373      * @event blur
374      * @hide
375      */
376     /**
377      * @event change
378      * @hide
379      */
380     /**
381      * @event focus
382      * @hide
383      */
384     /**
385      * @event specialkey
386      * @hide
387      */
388     /**
389      * @cfg {String} fieldClass @hide
390      */
391     /**
392      * @cfg {String} focusClass @hide
393      */
394     /**
395      * @cfg {String} autoCreate @hide
396      */
397     /**
398      * @cfg {String} inputType @hide
399      */
400     /**
401      * @cfg {String} invalidClass @hide
402      */
403     /**
404      * @cfg {String} invalidText @hide
405      */
406     /**
407      * @cfg {String} msgFx @hide
408      */
409     /**
410      * @cfg {String} validateOnBlur @hide
411      */
412 });
413  
414