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