1 //<script type="text/javascript">
5 * Copyright(c) 2006-2007, Ext JS, LLC.
11 Roo.form.HtmlEditor = function(config){
15 Roo.form.HtmlEditor.superclass.constructor.call(this, config);
20 this.editorcore = new Roo.HtmlEditorCore(Roo.apply({ owner : this} , config));
26 * @class Roo.form.HtmlEditor
27 * @extends Roo.form.Field
28 * Provides a lightweight HTML Editor component.
30 * This has been tested on Fireforx / Chrome.. IE may not be so great..
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/>
37 Roo.extend(Roo.form.HtmlEditor, Roo.form.Field, {
39 * @cfg {Boolean} clearUp
43 * @cfg {Array} toolbars Array of toolbars. - defaults to just the Standard one
48 * @cfg {String} resizable 's' or 'se' or 'e' - wrapps the element in a
53 * @cfg {Number} height (in pixels)
57 * @cfg {Number} width (in pixels)
62 * @cfg {Array} stylesheets url of stylesheets. set to [] to disable stylesheets.
69 * @cfg {Array} blacklist of css styles style attributes (blacklist overrides whitelist)
74 * @cfg {Array} whitelist of css styles style attributes (blacklist overrides whitelist)
80 * @cfg {Array} blacklist of html tags - in addition to standard blacklist.
85 * @cfg {Array} whitelist of html tags - in addition to statndard whitelist
94 validationEvent : false,
99 onFocus : Roo.emptyFn,
103 actionMode : 'container', // defaults to hiding it...
105 defaultAutoCreate : { // modified by initCompnoent..
107 style:"width:500px;height:300px;",
112 initComponent : function(){
116 * Fires when the editor is fully initialized (including the iframe)
117 * @param {HtmlEditor} this
122 * Fires when the editor is first receives the focus. Any insertion must wait
123 * until after this event.
124 * @param {HtmlEditor} this
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
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
145 * Fires when the textarea is updated with content from the editor iframe.
146 * @param {HtmlEditor} this
147 * @param {String} html
152 * Fires when the iframe editor is updated with content from the textarea.
153 * @param {HtmlEditor} this
154 * @param {String} html
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.
163 editmodechange: true,
166 * Fires when on any editor (mouse up/down cursor movement etc.) - used for toolbar hooks.
167 * @param {HtmlEditor} this
172 * Fires when on first focus - needed by toolbars..
173 * @param {HtmlEditor} this
178 * Auto save the htmlEditor value as a file into Events
179 * @param {HtmlEditor} this
183 * @event savedpreview
184 * preview the saved version of htmlEditor
185 * @param {HtmlEditor} this
189 this.defaultAutoCreate = {
191 style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
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
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?
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);
221 onRender : function(ct, position)
224 Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
226 this.wrap = this.el.wrap({
227 cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
230 this.editorcore.onRender(ct, position);
232 if (this.resizable) {
233 this.resizeEl = new Roo.Resizable(this.wrap, {
237 minHeight : this.height,
239 handles : this.resizable,
242 resize : function(r, w, h) {
243 _t.onResize(w,h); // -something
249 this.createToolbar(this);
253 this.setSize(this.wrap.getSize());
256 this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
257 // should trigger onReize..
260 this.keyNav = new Roo.KeyNav(this.el, {
265 var value = this.getValue();
267 var start = this.el.dom.selectionStart;
268 var end = this.el.dom.selectionEnd;
272 this.setValue(value.substring(0, start) + "\t" + value.substring(end));
273 this.el.dom.setSelectionRange(end + 1, end + 1);
277 var f = value.substring(0, start).split("\t");
279 if(f.pop().length != 0){
283 this.setValue(f.join("\t") + value.substring(end));
284 this.el.dom.setSelectionRange(start - 1, start - 1);
288 "home" : function(e){
291 var curr = this.el.dom.selectionStart;
292 var lines = this.getValue().split("\n");
299 this.el.dom.setSelectionRange(0, 0);
305 for (var i = 0; i < lines.length;i++) {
306 pos += lines[i].length;
316 pos -= lines[i].length;
322 this.el.dom.setSelectionRange(pos, pos);
326 this.el.dom.selectionStart = pos;
327 this.el.dom.selectionEnd = curr;
333 var curr = this.el.dom.selectionStart;
334 var lines = this.getValue().split("\n");
341 this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
347 for (var i = 0; i < lines.length;i++) {
349 pos += lines[i].length;
363 this.el.dom.setSelectionRange(pos, pos);
367 this.el.dom.selectionStart = curr;
368 this.el.dom.selectionEnd = pos;
373 doRelay : function(foo, bar, hname){
374 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
380 // if(this.autosave && this.w){
381 // this.autoSaveFn = setInterval(this.autosave, 1000);
386 onResize : function(w, h)
388 Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
393 if(typeof w == 'number'){
394 var aw = w - this.wrap.getFrameWidth('lr');
395 this.el.setWidth(this.adjustWidth('textarea', aw));
398 if(typeof h == 'number'){
400 for (var i =0; i < this.toolbars.length;i++) {
401 // fixme - ask toolbars for heights?
402 tbh += this.toolbars[i].tb.el.getHeight();
403 if (this.toolbars[i].footer) {
404 tbh += this.toolbars[i].footer.el.getHeight();
411 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
412 ah -= 5; // knock a few pixes off for look..
414 this.el.setHeight(this.adjustWidth('textarea', ah));
418 Roo.log('onResize:' + [w,h,ew,eh].join(',') );
419 this.editorcore.onResize(ew,eh);
424 * Toggles the editor between standard and source edit mode.
425 * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
427 toggleSourceEdit : function(sourceEditMode)
429 this.editorcore.toggleSourceEdit(sourceEditMode);
431 if(this.editorcore.sourceEditMode){
432 Roo.log('editor - showing textarea');
435 // Roo.log(this.syncValue());
436 this.editorcore.syncValue();
437 this.el.removeClass('x-hidden');
438 this.el.dom.removeAttribute('tabIndex');
441 for (var i = 0; i < this.toolbars.length; i++) {
442 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
443 this.toolbars[i].tb.hide();
444 this.toolbars[i].footer.hide();
449 Roo.log('editor - hiding textarea');
451 // Roo.log(this.pushValue());
452 this.editorcore.pushValue();
454 this.el.addClass('x-hidden');
455 this.el.dom.setAttribute('tabIndex', -1);
457 for (var i = 0; i < this.toolbars.length; i++) {
458 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
459 this.toolbars[i].tb.show();
460 this.toolbars[i].footer.show();
467 this.setSize(this.wrap.getSize());
468 this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
470 this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
473 // private (for BoxComponent)
474 adjustSize : Roo.BoxComponent.prototype.adjustSize,
476 // private (for BoxComponent)
477 getResizeEl : function(){
481 // private (for BoxComponent)
482 getPositionEl : function(){
487 initEvents : function(){
488 this.originalValue = this.getValue();
492 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
495 markInvalid : Roo.emptyFn,
497 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
500 clearInvalid : Roo.emptyFn,
502 setValue : function(v){
503 Roo.form.HtmlEditor.superclass.setValue.call(this, v);
504 this.editorcore.pushValue();
509 deferFocus : function(){
510 this.focus.defer(10, this);
515 this.editorcore.focus();
521 onDestroy : function(){
527 for (var i =0; i < this.toolbars.length;i++) {
528 // fixme - ask toolbars for heights?
529 this.toolbars[i].onDestroy();
532 this.wrap.dom.innerHTML = '';
538 onFirstFocus : function(){
539 //Roo.log("onFirstFocus");
540 this.editorcore.onFirstFocus();
541 for (var i =0; i < this.toolbars.length;i++) {
542 this.toolbars[i].onFirstFocus();
548 syncValue : function()
550 this.editorcore.syncValue();
553 pushValue : function()
555 this.editorcore.pushValue();
559 // hide stuff that is not compatible
577 * @cfg {String} fieldClass @hide
580 * @cfg {String} focusClass @hide
583 * @cfg {String} autoCreate @hide
586 * @cfg {String} inputType @hide
589 * @cfg {String} invalidClass @hide
592 * @cfg {String} invalidText @hide
595 * @cfg {String} msgFx @hide
598 * @cfg {String} validateOnBlur @hide