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