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