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