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