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