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