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