Roo/form/HtmlEditor.js
[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         });
211         this.defaultAutoCreate =  {
212             tag: "textarea",
213             style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
214             autocomplete: "new-password"
215         };
216     },
217
218     /**
219      * Protected method that will not generally be called directly. It
220      * is called when the editor creates its toolbar. Override this method if you need to
221      * add custom toolbar buttons.
222      * @param {HtmlEditor} editor
223      */
224     createToolbar : function(editor){
225         Roo.log("create toolbars");
226         if (!editor.toolbars || !editor.toolbars.length) {
227             editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
228         }
229         
230         for (var i =0 ; i < editor.toolbars.length;i++) {
231             editor.toolbars[i] = Roo.factory(
232                     typeof(editor.toolbars[i]) == 'string' ?
233                         { xtype: editor.toolbars[i]} : editor.toolbars[i],
234                 Roo.form.HtmlEditor);
235             editor.toolbars[i].init(editor);
236         }
237          
238         
239     },
240
241      
242     // private
243     onRender : function(ct, position)
244     {
245         var _t = this;
246         Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
247         
248         this.wrap = this.el.wrap({
249             cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
250         });
251         
252         this.editorcore.onRender(ct, position);
253          
254         if (this.resizable) {
255             this.resizeEl = new Roo.Resizable(this.wrap, {
256                 pinned : true,
257                 wrap: true,
258                 dynamic : true,
259                 minHeight : this.height,
260                 height: this.height,
261                 handles : this.resizable,
262                 width: this.width,
263                 listeners : {
264                     resize : function(r, w, h) {
265                         _t.onResize(w,h); // -something
266                     }
267                 }
268             });
269             
270         }
271         this.createToolbar(this);
272        
273         
274         if(!this.width){
275             this.setSize(this.wrap.getSize());
276         }
277         if (this.resizeEl) {
278             this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
279             // should trigger onReize..
280         }
281         
282         this.keyNav = new Roo.KeyNav(this.el, {
283             
284             "tab" : function(e){
285                 e.preventDefault();
286                 
287                 var value = this.getValue();
288                 
289                 var start = this.el.dom.selectionStart;
290                 var end = this.el.dom.selectionEnd;
291                 
292                 if(!e.shiftKey){
293                     
294                     this.setValue(value.substring(0, start) + "\t" + value.substring(end));
295                     this.el.dom.setSelectionRange(end + 1, end + 1);
296                     return;
297                 }
298                 
299                 var f = value.substring(0, start).split("\t");
300                 
301                 if(f.pop().length != 0){
302                     return;
303                 }
304                 
305                 this.setValue(f.join("\t") + value.substring(end));
306                 this.el.dom.setSelectionRange(start - 1, start - 1);
307                 
308             },
309             
310             "home" : function(e){
311                 e.preventDefault();
312                 
313                 var curr = this.el.dom.selectionStart;
314                 var lines = this.getValue().split("\n");
315                 
316                 if(!lines.length){
317                     return;
318                 }
319                 
320                 if(e.ctrlKey){
321                     this.el.dom.setSelectionRange(0, 0);
322                     return;
323                 }
324                 
325                 var pos = 0;
326                 
327                 for (var i = 0; i < lines.length;i++) {
328                     pos += lines[i].length;
329                     
330                     if(i != 0){
331                         pos += 1;
332                     }
333                     
334                     if(pos < curr){
335                         continue;
336                     }
337                     
338                     pos -= lines[i].length;
339                     
340                     break;
341                 }
342                 
343                 if(!e.shiftKey){
344                     this.el.dom.setSelectionRange(pos, pos);
345                     return;
346                 }
347                 
348                 this.el.dom.selectionStart = pos;
349                 this.el.dom.selectionEnd = curr;
350             },
351             
352             "end" : function(e){
353                 e.preventDefault();
354                 
355                 var curr = this.el.dom.selectionStart;
356                 var lines = this.getValue().split("\n");
357                 
358                 if(!lines.length){
359                     return;
360                 }
361                 
362                 if(e.ctrlKey){
363                     this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
364                     return;
365                 }
366                 
367                 var pos = 0;
368                 
369                 for (var i = 0; i < lines.length;i++) {
370                     
371                     pos += lines[i].length;
372                     
373                     if(i != 0){
374                         pos += 1;
375                     }
376                     
377                     if(pos < curr){
378                         continue;
379                     }
380                     
381                     break;
382                 }
383                 
384                 if(!e.shiftKey){
385                     this.el.dom.setSelectionRange(pos, pos);
386                     return;
387                 }
388                 
389                 this.el.dom.selectionStart = curr;
390                 this.el.dom.selectionEnd = pos;
391             },
392
393             scope : this,
394
395             doRelay : function(foo, bar, hname){
396                 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
397             },
398
399             forceKeyDown: true
400         });
401         
402 //        if(this.autosave && this.w){
403 //            this.autoSaveFn = setInterval(this.autosave, 1000);
404 //        }
405     },
406
407     // private
408     onResize : function(w, h)
409     {
410         Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
411         var ew = false;
412         var eh = false;
413         
414         if(this.el ){
415             if(typeof w == 'number'){
416                 var aw = w - this.wrap.getFrameWidth('lr');
417                 this.el.setWidth(this.adjustWidth('textarea', aw));
418                 ew = aw;
419             }
420             if(typeof h == 'number'){
421                 var tbh = 0;
422                 for (var i =0; i < this.toolbars.length;i++) {
423                     // fixme - ask toolbars for heights?
424                     tbh += this.toolbars[i].tb.el.getHeight();
425                     if (this.toolbars[i].footer) {
426                         tbh += this.toolbars[i].footer.el.getHeight();
427                     }
428                 }
429                 
430                 
431                 
432                 
433                 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
434                 ah -= 5; // knock a few pixes off for look..
435 //                Roo.log(ah);
436                 this.el.setHeight(this.adjustWidth('textarea', ah));
437                 var eh = ah;
438             }
439         }
440         Roo.log('onResize:' + [w,h,ew,eh].join(',') );
441         this.editorcore.onResize(ew,eh);
442         
443     },
444
445     /**
446      * Toggles the editor between standard and source edit mode.
447      * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
448      */
449     toggleSourceEdit : function(sourceEditMode)
450     {
451         this.editorcore.toggleSourceEdit(sourceEditMode);
452         
453         if(this.editorcore.sourceEditMode){
454             Roo.log('editor - showing textarea');
455             
456 //            Roo.log('in');
457 //            Roo.log(this.syncValue());
458             this.editorcore.syncValue();
459             this.el.removeClass('x-hidden');
460             this.el.dom.removeAttribute('tabIndex');
461             this.el.focus();
462             this.el.dom.scrollTop = 0;
463             
464             
465             for (var i = 0; i < this.toolbars.length; i++) {
466                 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
467                     this.toolbars[i].tb.hide();
468                     this.toolbars[i].footer.hide();
469                 }
470             }
471             
472         }else{
473             Roo.log('editor - hiding textarea');
474 //            Roo.log('out')
475 //            Roo.log(this.pushValue()); 
476             this.editorcore.pushValue();
477             
478             this.el.addClass('x-hidden');
479             this.el.dom.setAttribute('tabIndex', -1);
480             
481             for (var i = 0; i < this.toolbars.length; i++) {
482                 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
483                     this.toolbars[i].tb.show();
484                     this.toolbars[i].footer.show();
485                 }
486             }
487             
488             //this.deferFocus();
489         }
490         
491         this.setSize(this.wrap.getSize());
492         this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
493         
494         this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
495     },
496  
497     // private (for BoxComponent)
498     adjustSize : Roo.BoxComponent.prototype.adjustSize,
499
500     // private (for BoxComponent)
501     getResizeEl : function(){
502         return this.wrap;
503     },
504
505     // private (for BoxComponent)
506     getPositionEl : function(){
507         return this.wrap;
508     },
509
510     // private
511     initEvents : function(){
512         this.originalValue = this.getValue();
513     },
514
515     /**
516      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
517      * @method
518      */
519     markInvalid : Roo.emptyFn,
520     /**
521      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
522      * @method
523      */
524     clearInvalid : Roo.emptyFn,
525
526     setValue : function(v){
527         Roo.form.HtmlEditor.superclass.setValue.call(this, v);
528         this.editorcore.pushValue();
529     },
530
531      
532     // private
533     deferFocus : function(){
534         this.focus.defer(10, this);
535     },
536
537     // doc'ed in Field
538     focus : function(){
539         this.editorcore.focus();
540         
541     },
542       
543
544     // private
545     onDestroy : function(){
546         
547         
548         
549         if(this.rendered){
550             
551             for (var i =0; i < this.toolbars.length;i++) {
552                 // fixme - ask toolbars for heights?
553                 this.toolbars[i].onDestroy();
554             }
555             
556             this.wrap.dom.innerHTML = '';
557             this.wrap.remove();
558         }
559     },
560
561     // private
562     onFirstFocus : function(){
563         //Roo.log("onFirstFocus");
564         this.editorcore.onFirstFocus();
565          for (var i =0; i < this.toolbars.length;i++) {
566             this.toolbars[i].onFirstFocus();
567         }
568         
569     },
570     
571     // private
572     syncValue : function()
573     {
574         this.editorcore.syncValue();
575     },
576     
577     pushValue : function()
578     {
579         this.editorcore.pushValue();
580     },
581     
582     setStylesheets : function(stylesheets)
583     {
584         this.editorcore.setStylesheets(stylesheets);
585     },
586     
587     removeStylesheets : function()
588     {
589         this.editorcore.removeStylesheets();
590     }
591      
592     
593     // hide stuff that is not compatible
594     /**
595      * @event blur
596      * @hide
597      */
598     /**
599      * @event change
600      * @hide
601      */
602     /**
603      * @event focus
604      * @hide
605      */
606     /**
607      * @event specialkey
608      * @hide
609      */
610     /**
611      * @cfg {String} fieldClass @hide
612      */
613     /**
614      * @cfg {String} focusClass @hide
615      */
616     /**
617      * @cfg {String} autoCreate @hide
618      */
619     /**
620      * @cfg {String} inputType @hide
621      */
622     /**
623      * @cfg {String} invalidClass @hide
624      */
625     /**
626      * @cfg {String} invalidText @hide
627      */
628     /**
629      * @cfg {String} msgFx @hide
630      */
631     /**
632      * @cfg {String} validateOnBlur @hide
633      */
634 });
635  
636