Fix #6901 - CRM - allow comments in HTML for IE
[roojs1] / Roo / form / HtmlEditor.js
1 //<script type="text/javascript">
2
3 /*
4  * Ext JS Library 1.1.1
5  * Copyright(c) 2006-2007, Ext JS, LLC.
6  * Licence LGPL
7  * 
8  */
9  
10  
11 Roo.form.HtmlEditor = function(config){
12     
13     
14     
15     Roo.form.HtmlEditor.superclass.constructor.call(this, config);
16     
17     if (!this.toolbars) {
18         this.toolbars = [];
19     }
20     this.editorcore = new Roo.HtmlEditorCore(Roo.apply({ owner : this} , config));
21     
22     
23 };
24
25 /**
26  * @class Roo.form.HtmlEditor
27  * @extends Roo.form.Field
28  * Provides a lightweight HTML Editor component.
29  *
30  * This has been tested on Fireforx / Chrome.. IE may not be so great..
31  * 
32  * <br><br><b>Note: The focus/blur and validation marking functionality inherited from Ext.form.Field is NOT
33  * supported by this editor.</b><br/><br/>
34  * An Editor is a sensitive component that can't be used in all spots standard fields can be used. Putting an Editor within
35  * any element that has display set to 'none' can cause problems in Safari and Firefox.<br/><br/>
36  */
37 Roo.extend(Roo.form.HtmlEditor, Roo.form.Field, {
38     /**
39      * @cfg {Boolean} clearUp
40      */
41     clearUp : true,
42       /**
43      * @cfg {Array} toolbars Array of toolbars. - defaults to just the Standard one
44      */
45     toolbars : false,
46    
47      /**
48      * @cfg {String} resizable  's' or 'se' or 'e' - wrapps the element in a
49      *                        Roo.resizable.
50      */
51     resizable : false,
52      /**
53      * @cfg {Number} height (in pixels)
54      */   
55     height: 300,
56    /**
57      * @cfg {Number} width (in pixels)
58      */   
59     width: 500,
60     
61     /**
62      * @cfg {Array} stylesheets url of stylesheets. set to [] to disable stylesheets.
63      * 
64      */
65     stylesheets: false,
66     
67     
68      /**
69      * @cfg {Array} blacklist of css styles style attributes (blacklist overrides whitelist)
70      * 
71      */
72     cblack: false,
73     /**
74      * @cfg {Array} whitelist of css styles style attributes (blacklist overrides whitelist)
75      * 
76      */
77     cwhite: false,
78     
79      /**
80      * @cfg {Array} blacklist of html tags - in addition to standard blacklist.
81      * 
82      */
83     black: false,
84     /**
85      * @cfg {Array} whitelist of html tags - in addition to statndard whitelist
86      * 
87      */
88     white: false,
89     /**
90      * @cfg {boolean} allowComments - default false - allow comments in HTML source - by default they are stripped - if you are editing email you may need this.
91      */
92     allowComments: false,
93     
94     // id of frame..
95     frameId: false,
96     
97     // private properties
98     validationEvent : false,
99     deferHeight: true,
100     initialized : false,
101     activated : false,
102     
103     onFocus : Roo.emptyFn,
104     iframePad:3,
105     hideMode:'offsets',
106     
107     actionMode : 'container', // defaults to hiding it...
108     
109     defaultAutoCreate : { // modified by initCompnoent..
110         tag: "textarea",
111         style:"width:500px;height:300px;",
112         autocomplete: "new-password"
113     },
114
115     // private
116     initComponent : function(){
117         this.addEvents({
118             /**
119              * @event initialize
120              * Fires when the editor is fully initialized (including the iframe)
121              * @param {HtmlEditor} this
122              */
123             initialize: true,
124             /**
125              * @event activate
126              * Fires when the editor is first receives the focus. Any insertion must wait
127              * until after this event.
128              * @param {HtmlEditor} this
129              */
130             activate: true,
131              /**
132              * @event beforesync
133              * Fires before the textarea is updated with content from the editor iframe. Return false
134              * to cancel the sync.
135              * @param {HtmlEditor} this
136              * @param {String} html
137              */
138             beforesync: true,
139              /**
140              * @event beforepush
141              * Fires before the iframe editor is updated with content from the textarea. Return false
142              * to cancel the push.
143              * @param {HtmlEditor} this
144              * @param {String} html
145              */
146             beforepush: true,
147              /**
148              * @event sync
149              * Fires when the textarea is updated with content from the editor iframe.
150              * @param {HtmlEditor} this
151              * @param {String} html
152              */
153             sync: true,
154              /**
155              * @event push
156              * Fires when the iframe editor is updated with content from the textarea.
157              * @param {HtmlEditor} this
158              * @param {String} html
159              */
160             push: true,
161              /**
162              * @event editmodechange
163              * Fires when the editor switches edit modes
164              * @param {HtmlEditor} this
165              * @param {Boolean} sourceEdit True if source edit, false if standard editing.
166              */
167             editmodechange: true,
168             /**
169              * @event editorevent
170              * Fires when on any editor (mouse up/down cursor movement etc.) - used for toolbar hooks.
171              * @param {HtmlEditor} this
172              */
173             editorevent: true,
174             /**
175              * @event firstfocus
176              * Fires when on first focus - needed by toolbars..
177              * @param {HtmlEditor} this
178              */
179             firstfocus: true,
180             /**
181              * @event autosave
182              * Auto save the htmlEditor value as a file into Events
183              * @param {HtmlEditor} this
184              */
185             autosave: true,
186             /**
187              * @event savedpreview
188              * preview the saved version of htmlEditor
189              * @param {HtmlEditor} this
190              */
191             savedpreview: true,
192             
193             /**
194             * @event stylesheetsclick
195             * Fires when press the Sytlesheets button
196             * @param {Roo.HtmlEditorCore} this
197             */
198             stylesheetsclick: true
199         });
200         this.defaultAutoCreate =  {
201             tag: "textarea",
202             style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
203             autocomplete: "new-password"
204         };
205     },
206
207     /**
208      * Protected method that will not generally be called directly. It
209      * is called when the editor creates its toolbar. Override this method if you need to
210      * add custom toolbar buttons.
211      * @param {HtmlEditor} editor
212      */
213     createToolbar : function(editor){
214         Roo.log("create toolbars");
215         if (!editor.toolbars || !editor.toolbars.length) {
216             editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
217         }
218         
219         for (var i =0 ; i < editor.toolbars.length;i++) {
220             editor.toolbars[i] = Roo.factory(
221                     typeof(editor.toolbars[i]) == 'string' ?
222                         { xtype: editor.toolbars[i]} : editor.toolbars[i],
223                 Roo.form.HtmlEditor);
224             editor.toolbars[i].init(editor);
225         }
226          
227         
228     },
229
230      
231     // private
232     onRender : function(ct, position)
233     {
234         var _t = this;
235         Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
236         
237         this.wrap = this.el.wrap({
238             cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
239         });
240         
241         this.editorcore.onRender(ct, position);
242          
243         if (this.resizable) {
244             this.resizeEl = new Roo.Resizable(this.wrap, {
245                 pinned : true,
246                 wrap: true,
247                 dynamic : true,
248                 minHeight : this.height,
249                 height: this.height,
250                 handles : this.resizable,
251                 width: this.width,
252                 listeners : {
253                     resize : function(r, w, h) {
254                         _t.onResize(w,h); // -something
255                     }
256                 }
257             });
258             
259         }
260         this.createToolbar(this);
261        
262         
263         if(!this.width){
264             this.setSize(this.wrap.getSize());
265         }
266         if (this.resizeEl) {
267             this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
268             // should trigger onReize..
269         }
270         
271         this.keyNav = new Roo.KeyNav(this.el, {
272             
273             "tab" : function(e){
274                 e.preventDefault();
275                 
276                 var value = this.getValue();
277                 
278                 var start = this.el.dom.selectionStart;
279                 var end = this.el.dom.selectionEnd;
280                 
281                 if(!e.shiftKey){
282                     
283                     this.setValue(value.substring(0, start) + "\t" + value.substring(end));
284                     this.el.dom.setSelectionRange(end + 1, end + 1);
285                     return;
286                 }
287                 
288                 var f = value.substring(0, start).split("\t");
289                 
290                 if(f.pop().length != 0){
291                     return;
292                 }
293                 
294                 this.setValue(f.join("\t") + value.substring(end));
295                 this.el.dom.setSelectionRange(start - 1, start - 1);
296                 
297             },
298             
299             "home" : function(e){
300                 e.preventDefault();
301                 
302                 var curr = this.el.dom.selectionStart;
303                 var lines = this.getValue().split("\n");
304                 
305                 if(!lines.length){
306                     return;
307                 }
308                 
309                 if(e.ctrlKey){
310                     this.el.dom.setSelectionRange(0, 0);
311                     return;
312                 }
313                 
314                 var pos = 0;
315                 
316                 for (var i = 0; i < lines.length;i++) {
317                     pos += lines[i].length;
318                     
319                     if(i != 0){
320                         pos += 1;
321                     }
322                     
323                     if(pos < curr){
324                         continue;
325                     }
326                     
327                     pos -= lines[i].length;
328                     
329                     break;
330                 }
331                 
332                 if(!e.shiftKey){
333                     this.el.dom.setSelectionRange(pos, pos);
334                     return;
335                 }
336                 
337                 this.el.dom.selectionStart = pos;
338                 this.el.dom.selectionEnd = curr;
339             },
340             
341             "end" : function(e){
342                 e.preventDefault();
343                 
344                 var curr = this.el.dom.selectionStart;
345                 var lines = this.getValue().split("\n");
346                 
347                 if(!lines.length){
348                     return;
349                 }
350                 
351                 if(e.ctrlKey){
352                     this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
353                     return;
354                 }
355                 
356                 var pos = 0;
357                 
358                 for (var i = 0; i < lines.length;i++) {
359                     
360                     pos += lines[i].length;
361                     
362                     if(i != 0){
363                         pos += 1;
364                     }
365                     
366                     if(pos < curr){
367                         continue;
368                     }
369                     
370                     break;
371                 }
372                 
373                 if(!e.shiftKey){
374                     this.el.dom.setSelectionRange(pos, pos);
375                     return;
376                 }
377                 
378                 this.el.dom.selectionStart = curr;
379                 this.el.dom.selectionEnd = pos;
380             },
381
382             scope : this,
383
384             doRelay : function(foo, bar, hname){
385                 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
386             },
387
388             forceKeyDown: true
389         });
390         
391 //        if(this.autosave && this.w){
392 //            this.autoSaveFn = setInterval(this.autosave, 1000);
393 //        }
394     },
395
396     // private
397     onResize : function(w, h)
398     {
399         Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
400         var ew = false;
401         var eh = false;
402         
403         if(this.el ){
404             if(typeof w == 'number'){
405                 var aw = w - this.wrap.getFrameWidth('lr');
406                 this.el.setWidth(this.adjustWidth('textarea', aw));
407                 ew = aw;
408             }
409             if(typeof h == 'number'){
410                 var tbh = 0;
411                 for (var i =0; i < this.toolbars.length;i++) {
412                     // fixme - ask toolbars for heights?
413                     tbh += this.toolbars[i].tb.el.getHeight();
414                     if (this.toolbars[i].footer) {
415                         tbh += this.toolbars[i].footer.el.getHeight();
416                     }
417                 }
418                 
419                 
420                 
421                 
422                 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
423                 ah -= 5; // knock a few pixes off for look..
424 //                Roo.log(ah);
425                 this.el.setHeight(this.adjustWidth('textarea', ah));
426                 var eh = ah;
427             }
428         }
429         Roo.log('onResize:' + [w,h,ew,eh].join(',') );
430         this.editorcore.onResize(ew,eh);
431         
432     },
433
434     /**
435      * Toggles the editor between standard and source edit mode.
436      * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
437      */
438     toggleSourceEdit : function(sourceEditMode)
439     {
440         this.editorcore.toggleSourceEdit(sourceEditMode);
441         
442         if(this.editorcore.sourceEditMode){
443             Roo.log('editor - showing textarea');
444             
445 //            Roo.log('in');
446 //            Roo.log(this.syncValue());
447             this.editorcore.syncValue();
448             this.el.removeClass('x-hidden');
449             this.el.dom.removeAttribute('tabIndex');
450             this.el.focus();
451             
452             for (var i = 0; i < this.toolbars.length; i++) {
453                 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
454                     this.toolbars[i].tb.hide();
455                     this.toolbars[i].footer.hide();
456                 }
457             }
458             
459         }else{
460             Roo.log('editor - hiding textarea');
461 //            Roo.log('out')
462 //            Roo.log(this.pushValue()); 
463             this.editorcore.pushValue();
464             
465             this.el.addClass('x-hidden');
466             this.el.dom.setAttribute('tabIndex', -1);
467             
468             for (var i = 0; i < this.toolbars.length; i++) {
469                 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
470                     this.toolbars[i].tb.show();
471                     this.toolbars[i].footer.show();
472                 }
473             }
474             
475             //this.deferFocus();
476         }
477         
478         this.setSize(this.wrap.getSize());
479         this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
480         
481         this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
482     },
483  
484     // private (for BoxComponent)
485     adjustSize : Roo.BoxComponent.prototype.adjustSize,
486
487     // private (for BoxComponent)
488     getResizeEl : function(){
489         return this.wrap;
490     },
491
492     // private (for BoxComponent)
493     getPositionEl : function(){
494         return this.wrap;
495     },
496
497     // private
498     initEvents : function(){
499         this.originalValue = this.getValue();
500     },
501
502     /**
503      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
504      * @method
505      */
506     markInvalid : Roo.emptyFn,
507     /**
508      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
509      * @method
510      */
511     clearInvalid : Roo.emptyFn,
512
513     setValue : function(v){
514         Roo.form.HtmlEditor.superclass.setValue.call(this, v);
515         this.editorcore.pushValue();
516     },
517
518      
519     // private
520     deferFocus : function(){
521         this.focus.defer(10, this);
522     },
523
524     // doc'ed in Field
525     focus : function(){
526         this.editorcore.focus();
527         
528     },
529       
530
531     // private
532     onDestroy : function(){
533         
534         
535         
536         if(this.rendered){
537             
538             for (var i =0; i < this.toolbars.length;i++) {
539                 // fixme - ask toolbars for heights?
540                 this.toolbars[i].onDestroy();
541             }
542             
543             this.wrap.dom.innerHTML = '';
544             this.wrap.remove();
545         }
546     },
547
548     // private
549     onFirstFocus : function(){
550         //Roo.log("onFirstFocus");
551         this.editorcore.onFirstFocus();
552          for (var i =0; i < this.toolbars.length;i++) {
553             this.toolbars[i].onFirstFocus();
554         }
555         
556     },
557     
558     // private
559     syncValue : function()
560     {
561         this.editorcore.syncValue();
562     },
563     
564     pushValue : function()
565     {
566         this.editorcore.pushValue();
567     },
568     
569     setStylesheets : function(stylesheets)
570     {
571         this.editorcore.setStylesheets(stylesheets);
572     },
573     
574     removeStylesheets : function()
575     {
576         this.editorcore.removeStylesheets();
577     }
578      
579     
580     // hide stuff that is not compatible
581     /**
582      * @event blur
583      * @hide
584      */
585     /**
586      * @event change
587      * @hide
588      */
589     /**
590      * @event focus
591      * @hide
592      */
593     /**
594      * @event specialkey
595      * @hide
596      */
597     /**
598      * @cfg {String} fieldClass @hide
599      */
600     /**
601      * @cfg {String} focusClass @hide
602      */
603     /**
604      * @cfg {String} autoCreate @hide
605      */
606     /**
607      * @cfg {String} inputType @hide
608      */
609     /**
610      * @cfg {String} invalidClass @hide
611      */
612     /**
613      * @cfg {String} invalidText @hide
614      */
615     /**
616      * @cfg {String} msgFx @hide
617      */
618     /**
619      * @cfg {String} validateOnBlur @hide
620      */
621 });
622  
623