Roo/form/ComboBoxArray.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     defaultAutoCreate : { // modified by initCompnoent..
104         tag: "textarea",
105         style:"width:500px;height:300px;",
106         autocomplete: "off"
107     },
108
109     // private
110     initComponent : function(){
111         this.addEvents({
112             /**
113              * @event initialize
114              * Fires when the editor is fully initialized (including the iframe)
115              * @param {HtmlEditor} this
116              */
117             initialize: true,
118             /**
119              * @event activate
120              * Fires when the editor is first receives the focus. Any insertion must wait
121              * until after this event.
122              * @param {HtmlEditor} this
123              */
124             activate: true,
125              /**
126              * @event beforesync
127              * Fires before the textarea is updated with content from the editor iframe. Return false
128              * to cancel the sync.
129              * @param {HtmlEditor} this
130              * @param {String} html
131              */
132             beforesync: true,
133              /**
134              * @event beforepush
135              * Fires before the iframe editor is updated with content from the textarea. Return false
136              * to cancel the push.
137              * @param {HtmlEditor} this
138              * @param {String} html
139              */
140             beforepush: true,
141              /**
142              * @event sync
143              * Fires when the textarea is updated with content from the editor iframe.
144              * @param {HtmlEditor} this
145              * @param {String} html
146              */
147             sync: true,
148              /**
149              * @event push
150              * Fires when the iframe editor is updated with content from the textarea.
151              * @param {HtmlEditor} this
152              * @param {String} html
153              */
154             push: true,
155              /**
156              * @event editmodechange
157              * Fires when the editor switches edit modes
158              * @param {HtmlEditor} this
159              * @param {Boolean} sourceEdit True if source edit, false if standard editing.
160              */
161             editmodechange: true,
162             /**
163              * @event editorevent
164              * Fires when on any editor (mouse up/down cursor movement etc.) - used for toolbar hooks.
165              * @param {HtmlEditor} this
166              */
167             editorevent: true,
168             /**
169              * @event firstfocus
170              * Fires when on first focus - needed by toolbars..
171              * @param {HtmlEditor} this
172              */
173             firstfocus: true,
174             /**
175              * @event autosave
176              * Auto save the htmlEditor value as a file into Events
177              * @param {HtmlEditor} this
178              */
179             autosave: true,
180             /**
181              * @event savedpreview
182              * preview the saved version of htmlEditor
183              * @param {HtmlEditor} this
184              */
185             savedpreview: true
186         });
187         this.defaultAutoCreate =  {
188             tag: "textarea",
189             style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
190             autocomplete: "off"
191         };
192     },
193
194     /**
195      * Protected method that will not generally be called directly. It
196      * is called when the editor creates its toolbar. Override this method if you need to
197      * add custom toolbar buttons.
198      * @param {HtmlEditor} editor
199      */
200     createToolbar : function(editor){
201         Roo.log("create toolbars");
202         if (!editor.toolbars || !editor.toolbars.length) {
203             editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
204         }
205         
206         for (var i =0 ; i < editor.toolbars.length;i++) {
207             editor.toolbars[i] = Roo.factory(
208                     typeof(editor.toolbars[i]) == 'string' ?
209                         { xtype: editor.toolbars[i]} : editor.toolbars[i],
210                 Roo.form.HtmlEditor);
211             editor.toolbars[i].init(editor);
212         }
213          
214         
215     },
216
217      
218     // private
219     onRender : function(ct, position)
220     {
221         var _t = this;
222         Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
223         
224         this.wrap = this.el.wrap({
225             cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
226         });
227         
228         this.editorcore.onRender(ct, position);
229          
230         if (this.resizable) {
231             this.resizeEl = new Roo.Resizable(this.wrap, {
232                 pinned : true,
233                 wrap: true,
234                 dynamic : true,
235                 minHeight : this.height,
236                 height: this.height,
237                 handles : this.resizable,
238                 width: this.width,
239                 listeners : {
240                     resize : function(r, w, h) {
241                         _t.onResize(w,h); // -something
242                     }
243                 }
244             });
245             
246         }
247         this.createToolbar(this);
248        
249         
250         if(!this.width){
251             this.setSize(this.wrap.getSize());
252         }
253         if (this.resizeEl) {
254             this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
255             // should trigger onReize..
256         }
257         
258 //        if(this.autosave && this.w){
259 //            this.autoSaveFn = setInterval(this.autosave, 1000);
260 //        }
261     },
262
263     // private
264     onResize : function(w, h)
265     {
266         //Roo.log('resize: ' +w + ',' + h );
267         Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
268         var ew = false;
269         var eh = false;
270         
271         if(this.el ){
272             if(typeof w == 'number'){
273                 var aw = w - this.wrap.getFrameWidth('lr');
274                 this.el.setWidth(this.adjustWidth('textarea', aw));
275                 ew = aw;
276             }
277             if(typeof h == 'number'){
278                 var tbh = 0;
279                 for (var i =0; i < this.toolbars.length;i++) {
280                     // fixme - ask toolbars for heights?
281                     tbh += this.toolbars[i].tb.el.getHeight();
282                     if (this.toolbars[i].footer) {
283                         tbh += this.toolbars[i].footer.el.getHeight();
284                     }
285                 }
286                 
287                 
288                 
289                 
290                 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
291                 ah -= 5; // knock a few pixes off for look..
292                 this.el.setHeight(this.adjustWidth('textarea', ah));
293                 var eh = ah;
294             }
295         }
296         Roo.log('onResize:' + [w,h,ew,eh].join(',') );
297         this.editorcore.onResize(ew,eh);
298         
299     },
300
301     /**
302      * Toggles the editor between standard and source edit mode.
303      * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
304      */
305     toggleSourceEdit : function(sourceEditMode)
306     {
307         this.editorcore.toggleSourceEdit(sourceEditMode);
308         
309         if(this.editorcore.sourceEditMode){
310             Roo.log('editor - showing textarea');
311             
312 //            Roo.log('in');
313 //            Roo.log(this.syncValue());
314             this.editorcore.syncValue();
315             this.el.removeClass('x-hidden');
316             this.el.dom.removeAttribute('tabIndex');
317             this.el.focus();
318         }else{
319             Roo.log('editor - hiding textarea');
320 //            Roo.log('out')
321 //            Roo.log(this.pushValue()); 
322             this.editorcore.pushValue();
323             
324             this.el.addClass('x-hidden');
325             this.el.dom.setAttribute('tabIndex', -1);
326             //this.deferFocus();
327         }
328          
329         this.setSize(this.wrap.getSize());
330         this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
331     },
332  
333     // private (for BoxComponent)
334     adjustSize : Roo.BoxComponent.prototype.adjustSize,
335
336     // private (for BoxComponent)
337     getResizeEl : function(){
338         return this.wrap;
339     },
340
341     // private (for BoxComponent)
342     getPositionEl : function(){
343         return this.wrap;
344     },
345
346     // private
347     initEvents : function(){
348         this.originalValue = this.getValue();
349     },
350
351     /**
352      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
353      * @method
354      */
355     markInvalid : Roo.emptyFn,
356     /**
357      * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
358      * @method
359      */
360     clearInvalid : Roo.emptyFn,
361
362     setValue : function(v){
363         Roo.form.HtmlEditor.superclass.setValue.call(this, v);
364         this.editorcore.pushValue();
365     },
366
367      
368     // private
369     deferFocus : function(){
370         this.focus.defer(10, this);
371     },
372
373     // doc'ed in Field
374     focus : function(){
375         this.editorcore.focus();
376         
377     },
378       
379
380     // private
381     onDestroy : function(){
382         
383         
384         
385         if(this.rendered){
386             
387             for (var i =0; i < this.toolbars.length;i++) {
388                 // fixme - ask toolbars for heights?
389                 this.toolbars[i].onDestroy();
390             }
391             
392             this.wrap.dom.innerHTML = '';
393             this.wrap.remove();
394         }
395     },
396
397     // private
398     onFirstFocus : function(){
399         //Roo.log("onFirstFocus");
400         this.editorcore.onFirstFocus();
401          for (var i =0; i < this.toolbars.length;i++) {
402             this.toolbars[i].onFirstFocus();
403         }
404         
405     },
406     
407     // private
408     syncValue : function()
409     {
410         this.editorcore.syncValue();
411     },
412     
413     pushValue : function()
414     {
415         this.editorcore.pushValue();
416     }
417      
418     
419     // hide stuff that is not compatible
420     /**
421      * @event blur
422      * @hide
423      */
424     /**
425      * @event change
426      * @hide
427      */
428     /**
429      * @event focus
430      * @hide
431      */
432     /**
433      * @event specialkey
434      * @hide
435      */
436     /**
437      * @cfg {String} fieldClass @hide
438      */
439     /**
440      * @cfg {String} focusClass @hide
441      */
442     /**
443      * @cfg {String} autoCreate @hide
444      */
445     /**
446      * @cfg {String} inputType @hide
447      */
448     /**
449      * @cfg {String} invalidClass @hide
450      */
451     /**
452      * @cfg {String} invalidText @hide
453      */
454     /**
455      * @cfg {String} msgFx @hide
456      */
457     /**
458      * @cfg {String} validateOnBlur @hide
459      */
460 });
461  
462