Fix #7586 - more events for editor
[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             */
126            imagedelete: true  
127     });
128     Roo.bootstrap.form.HtmlEditor.superclass.constructor.call(this, config);
129     if (!this.toolbars) {
130         this.toolbars = [];
131     }
132     
133     this.editorcore = new Roo.HtmlEditorCore(Roo.apply({ owner : this} , config));
134     
135 };
136
137
138 Roo.extend(Roo.bootstrap.form.HtmlEditor, Roo.bootstrap.form.TextArea,  {
139     
140     
141       /**
142      * @cfg {Array|boolean} toolbars Array of toolbars, or names of toolbars. - true for standard, and false for none.
143      */
144     toolbars : true,
145     
146      /**
147     * @cfg {Array} buttons Array of toolbar's buttons. - defaults to empty
148     */
149     btns : [],
150    
151      /**
152      * @cfg {String} resize  (none|both|horizontal|vertical) - css resize of element
153      */
154     resize : false,
155      /**
156      * @cfg {Number} height (in pixels)
157      */   
158     height: 300,
159    /**
160      * @cfg {Number} width (in pixels)
161      */   
162     width: false,
163     
164     /**
165      * @cfg {Array} stylesheets url of stylesheets. set to [] to disable stylesheets.
166      * 
167      */
168     stylesheets: false,
169     
170     // id of frame..
171     frameId: false,
172     
173     // private properties
174     validationEvent : false,
175     deferHeight: true,
176     initialized : false,
177     activated : false,
178     
179     onFocus : Roo.emptyFn,
180     iframePad:3,
181     hideMode:'offsets',
182     
183     tbContainer : false,
184     
185     bodyCls : '',
186     
187     toolbarContainer :function() {
188         return this.wrap.select('.x-html-editor-tb',true).first();
189     },
190
191     /**
192      * Protected method that will not generally be called directly. It
193      * is called when the editor creates its toolbar. Override this method if you need to
194      * add custom toolbar buttons.
195      * @param {HtmlEditor} editor
196      */
197     createToolbar : function()
198     {
199         //Roo.log('renewing');
200         //Roo.log("create toolbars");
201         if (this.toolbars === false) {
202             return;
203         }
204         if (this.toolbars === true) {
205             this.toolbars = [ 'Standard' ];
206         }
207         
208         var ar = Array.from(this.toolbars);
209         this.toolbars = [];
210         ar.forEach(function(t,i) {
211             if (typeof(t) == 'string') {
212                 t = {
213                     xtype : t
214                 };
215             }
216             if (typeof(t) == 'object' && typeof(t.xtype) == 'string') {
217                 t.editor = this;
218                 t.xns = t.xns || Roo.bootstrap.form.HtmlEditorToolbar;
219                 t = Roo.factory(t);
220             }
221             this.toolbars[i] = t;
222             this.toolbars[i].render(this.toolbarContainer());
223         }, this);
224         
225         
226     },
227
228      
229     // private
230     onRender : function(ct, position)
231     {
232        // Roo.log("Call onRender: " + this.xtype);
233         var _t = this;
234         Roo.bootstrap.form.HtmlEditor.superclass.onRender.call(this, ct, position);
235       
236         this.wrap = this.inputEl().wrap({
237             cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
238         });
239         
240         this.editorcore.onRender(ct, position);
241          
242          
243         this.createToolbar(this);
244        
245         
246           
247         
248     },
249
250     // private
251     onResize : function(w, h)
252     {
253         Roo.log('resize: ' +w + ',' + h );
254         Roo.bootstrap.form.HtmlEditor.superclass.onResize.apply(this, arguments);
255         var ew = false;
256         var eh = false;
257         
258         if(this.inputEl() ){
259             if(typeof w == 'number'){
260                 var aw = w - this.wrap.getFrameWidth('lr');
261                 this.inputEl().setWidth(this.adjustWidth('textarea', aw));
262                 ew = aw;
263             }
264             if(typeof h == 'number'){
265                  var tbh = -11;  // fixme it needs to tool bar size!
266                 for (var i =0; i < this.toolbars.length;i++) {
267                     // fixme - ask toolbars for heights?
268                     tbh += this.toolbars[i].el.getHeight();
269                     //if (this.toolbars[i].footer) {
270                     //    tbh += this.toolbars[i].footer.el.getHeight();
271                     //}
272                 }
273               
274                 
275                 
276                 
277                 
278                 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
279                 ah -= 5; // knock a few pixes off for look..
280                 this.inputEl().setHeight(this.adjustWidth('textarea', ah));
281                 var eh = ah;
282             }
283         }
284         Roo.log('onResize:' + [w,h,ew,eh].join(',') );
285         this.editorcore.onResize(ew,eh);
286         
287     },
288
289     /**
290      * Toggles the editor between standard and source edit mode.
291      * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
292      */
293     toggleSourceEdit : function(sourceEditMode)
294     {
295         this.editorcore.toggleSourceEdit(sourceEditMode);
296         
297         if(this.editorcore.sourceEditMode){
298             Roo.log('editor - showing textarea');
299             
300 //            Roo.log('in');
301 //            Roo.log(this.syncValue());
302             this.syncValue();
303             this.inputEl().removeClass(['hide', 'x-hidden']);
304             this.inputEl().dom.removeAttribute('tabIndex');
305             this.inputEl().focus();
306         }else{
307             Roo.log('editor - hiding textarea');
308 //            Roo.log('out')
309 //            Roo.log(this.pushValue()); 
310             this.pushValue();
311             
312             this.inputEl().addClass(['hide', 'x-hidden']);
313             this.inputEl().dom.setAttribute('tabIndex', -1);
314             //this.deferFocus();
315         }
316          
317         //if(this.resizable){
318         //    this.setSize(this.wrap.getSize());
319         //}
320         
321         this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
322     },
323  
324     // private (for BoxComponent)
325     adjustSize : Roo.BoxComponent.prototype.adjustSize,
326
327     // private (for BoxComponent)
328     getResizeEl : function(){
329         return this.wrap;
330     },
331
332     // private (for BoxComponent)
333     getPositionEl : function(){
334         return this.wrap;
335     },
336
337     // private
338     initEvents : function(){
339         this.originalValue = this.getValue();
340     },
341
342 //    /**
343 //     * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
344 //     * @method
345 //     */
346 //    markInvalid : Roo.emptyFn,
347 //    /**
348 //     * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
349 //     * @method
350 //     */
351 //    clearInvalid : Roo.emptyFn,
352
353     setValue : function(v){
354         Roo.bootstrap.form.HtmlEditor.superclass.setValue.call(this, v);
355         this.editorcore.pushValue();
356     },
357
358      
359     // private
360     deferFocus : function(){
361         this.focus.defer(10, this);
362     },
363
364     // doc'ed in Field
365     focus : function(){
366         this.editorcore.focus();
367         
368     },
369       
370
371     // private
372     onDestroy : function(){
373         
374         
375         
376         if(this.rendered){
377             
378             for (var i =0; i < this.toolbars.length;i++) {
379                 // fixme - ask toolbars for heights?
380                 this.toolbars[i].onDestroy();
381             }
382             
383             this.wrap.dom.innerHTML = '';
384             this.wrap.remove();
385         }
386     },
387
388     // private
389     onFirstFocus : function(){
390         //Roo.log("onFirstFocus");
391         this.editorcore.onFirstFocus();
392          for (var i =0; i < this.toolbars.length;i++) {
393             this.toolbars[i].onFirstFocus();
394         }
395         
396     },
397     
398     // private
399     syncValue : function()
400     {   
401         this.editorcore.syncValue();
402     },
403     
404     pushValue : function()
405     {   
406         this.editorcore.pushValue();
407     }
408      
409     
410     // hide stuff that is not compatible
411     /**
412      * @event blur
413      * @hide
414      */
415     /**
416      * @event change
417      * @hide
418      */
419     /**
420      * @event focus
421      * @hide
422      */
423     /**
424      * @event specialkey
425      * @hide
426      */
427     /**
428      * @cfg {String} fieldClass @hide
429      */
430     /**
431      * @cfg {String} focusClass @hide
432      */
433     /**
434      * @cfg {String} autoCreate @hide
435      */
436     /**
437      * @cfg {String} inputType @hide
438      */
439      
440     /**
441      * @cfg {String} invalidText @hide
442      */
443     /**
444      * @cfg {String} msgFx @hide
445      */
446     /**
447      * @cfg {String} validateOnBlur @hide
448      */
449 });
450  
451     
452    
453    
454    
455