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