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