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