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 - this is usally a good idea rootURL + '/roojs1/css/undoreset.css', .
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
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.
94 * @cfg {boolean} enableBlocks - default true - if the block editor (table and figure should be enabled)
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
104 * @cfg {string} bodyCls default '' default classes to add to body of editable area - usually undoreset is a good start..
108 * @cfg {String} language default en - language of text (usefull for rtl languages)
117 // private properties
118 validationEvent : false,
123 onFocus : Roo.emptyFn,
127 actionMode : 'container', // defaults to hiding it...
129 defaultAutoCreate : { // modified by initCompnoent..
131 style:"width:500px;height:300px;",
132 autocomplete: "new-password"
136 initComponent : function(){
140 * Fires when the editor is fully initialized (including the iframe)
141 * @param {HtmlEditor} this
146 * Fires when the editor is first receives the focus. Any insertion must wait
147 * until after this event.
148 * @param {HtmlEditor} this
153 * Fires before the textarea is updated with content from the editor iframe. Return false
154 * to cancel the sync.
155 * @param {HtmlEditor} this
156 * @param {String} html
161 * Fires before the iframe editor is updated with content from the textarea. Return false
162 * to cancel the push.
163 * @param {HtmlEditor} this
164 * @param {String} html
169 * Fires when the textarea is updated with content from the editor iframe.
170 * @param {HtmlEditor} this
171 * @param {String} html
176 * Fires when the iframe editor is updated with content from the textarea.
177 * @param {HtmlEditor} this
178 * @param {String} html
182 * @event editmodechange
183 * Fires when the editor switches edit modes
184 * @param {HtmlEditor} this
185 * @param {Boolean} sourceEdit True if source edit, false if standard editing.
187 editmodechange: true,
190 * Fires when on any editor (mouse up/down cursor movement etc.) - used for toolbar hooks.
191 * @param {HtmlEditor} this
196 * Fires when on first focus - needed by toolbars..
197 * @param {HtmlEditor} this
202 * Auto save the htmlEditor value as a file into Events
203 * @param {HtmlEditor} this
207 * @event savedpreview
208 * preview the saved version of htmlEditor
209 * @param {HtmlEditor} this
214 * @event stylesheetsclick
215 * Fires when press the Sytlesheets button
216 * @param {Roo.HtmlEditorCore} this
218 stylesheetsclick: true,
221 * Fires when press user pastes into the editor
222 * @param {Roo.HtmlEditorCore} this
226 this.defaultAutoCreate = {
228 style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
229 autocomplete: "new-password"
234 * Protected method that will not generally be called directly. It
235 * is called when the editor creates its toolbar. Override this method if you need to
236 * add custom toolbar buttons.
237 * @param {HtmlEditor} editor
239 createToolbar : function(editor){
240 Roo.log("create toolbars");
241 if (!editor.toolbars || !editor.toolbars.length) {
242 editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
245 for (var i =0 ; i < editor.toolbars.length;i++) {
246 editor.toolbars[i] = Roo.factory(
247 typeof(editor.toolbars[i]) == 'string' ?
248 { xtype: editor.toolbars[i]} : editor.toolbars[i],
249 Roo.form.HtmlEditor);
250 editor.toolbars[i].init(editor);
256 * get the Context selected node
257 * @returns {DomElement|boolean} selected node if active or false if none
260 getSelectedNode : function()
262 if (this.toolbars.length < 2 || !this.toolbars[1].tb) {
265 return this.toolbars[1].tb.selectedNode;
269 onRender : function(ct, position)
272 Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
274 this.wrap = this.el.wrap({
275 cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
278 this.editorcore.onRender(ct, position);
280 if (this.resizable) {
281 this.resizeEl = new Roo.Resizable(this.wrap, {
285 minHeight : this.height,
287 handles : this.resizable,
290 resize : function(r, w, h) {
291 _t.onResize(w,h); // -something
297 this.createToolbar(this);
301 this.setSize(this.wrap.getSize());
304 this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
305 // should trigger onReize..
308 this.keyNav = new Roo.KeyNav(this.el, {
313 var value = this.getValue();
315 var start = this.el.dom.selectionStart;
316 var end = this.el.dom.selectionEnd;
320 this.setValue(value.substring(0, start) + "\t" + value.substring(end));
321 this.el.dom.setSelectionRange(end + 1, end + 1);
325 var f = value.substring(0, start).split("\t");
327 if(f.pop().length != 0){
331 this.setValue(f.join("\t") + value.substring(end));
332 this.el.dom.setSelectionRange(start - 1, start - 1);
336 "home" : function(e){
339 var curr = this.el.dom.selectionStart;
340 var lines = this.getValue().split("\n");
347 this.el.dom.setSelectionRange(0, 0);
353 for (var i = 0; i < lines.length;i++) {
354 pos += lines[i].length;
364 pos -= lines[i].length;
370 this.el.dom.setSelectionRange(pos, pos);
374 this.el.dom.selectionStart = pos;
375 this.el.dom.selectionEnd = curr;
381 var curr = this.el.dom.selectionStart;
382 var lines = this.getValue().split("\n");
389 this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
395 for (var i = 0; i < lines.length;i++) {
397 pos += lines[i].length;
411 this.el.dom.setSelectionRange(pos, pos);
415 this.el.dom.selectionStart = curr;
416 this.el.dom.selectionEnd = pos;
421 doRelay : function(foo, bar, hname){
422 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
428 // if(this.autosave && this.w){
429 // this.autoSaveFn = setInterval(this.autosave, 1000);
434 onResize : function(w, h)
436 Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
441 if(typeof w == 'number'){
442 var aw = w - this.wrap.getFrameWidth('lr');
443 this.el.setWidth(this.adjustWidth('textarea', aw));
446 if(typeof h == 'number'){
448 for (var i =0; i < this.toolbars.length;i++) {
449 // fixme - ask toolbars for heights?
450 tbh += this.toolbars[i].tb.el.getHeight();
451 if (this.toolbars[i].footer) {
452 tbh += this.toolbars[i].footer.el.getHeight();
459 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
460 ah -= 5; // knock a few pixes off for look..
462 this.el.setHeight(this.adjustWidth('textarea', ah));
466 Roo.log('onResize:' + [w,h,ew,eh].join(',') );
467 this.editorcore.onResize(ew,eh);
472 * Toggles the editor between standard and source edit mode.
473 * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
475 toggleSourceEdit : function(sourceEditMode)
477 this.editorcore.toggleSourceEdit(sourceEditMode);
479 if(this.editorcore.sourceEditMode){
480 Roo.log('editor - showing textarea');
483 // Roo.log(this.syncValue());
484 this.editorcore.syncValue();
485 this.el.removeClass('x-hidden');
486 this.el.dom.removeAttribute('tabIndex');
488 this.el.dom.scrollTop = 0;
491 for (var i = 0; i < this.toolbars.length; i++) {
492 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
493 this.toolbars[i].tb.hide();
494 this.toolbars[i].footer.hide();
499 Roo.log('editor - hiding textarea');
501 // Roo.log(this.pushValue());
502 this.editorcore.pushValue();
504 this.el.addClass('x-hidden');
505 this.el.dom.setAttribute('tabIndex', -1);
507 for (var i = 0; i < this.toolbars.length; i++) {
508 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
509 this.toolbars[i].tb.show();
510 this.toolbars[i].footer.show();
517 this.setSize(this.wrap.getSize());
518 this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
520 this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
523 // private (for BoxComponent)
524 adjustSize : Roo.BoxComponent.prototype.adjustSize,
526 // private (for BoxComponent)
527 getResizeEl : function(){
531 // private (for BoxComponent)
532 getPositionEl : function(){
537 initEvents : function(){
538 this.originalValue = this.getValue();
542 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
545 markInvalid : Roo.emptyFn,
547 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
550 clearInvalid : Roo.emptyFn,
552 setValue : function(v){
553 Roo.form.HtmlEditor.superclass.setValue.call(this, v);
554 this.editorcore.pushValue();
558 * update the language in the body - really done by core
559 * @param {String} language - eg. en / ar / zh-CN etc..
561 updateLanguage : function(lang)
563 this.language = lang;
564 this.editorcore.language = lang;
565 this.editorcore.updateLanguage();
569 deferFocus : function(){
570 this.focus.defer(10, this);
575 this.editorcore.focus();
581 onDestroy : function(){
587 for (var i =0; i < this.toolbars.length;i++) {
588 // fixme - ask toolbars for heights?
589 this.toolbars[i].onDestroy();
592 this.wrap.dom.innerHTML = '';
598 onFirstFocus : function(){
599 //Roo.log("onFirstFocus");
600 this.editorcore.onFirstFocus();
601 for (var i =0; i < this.toolbars.length;i++) {
602 this.toolbars[i].onFirstFocus();
608 syncValue : function()
610 this.editorcore.syncValue();
613 pushValue : function()
615 this.editorcore.pushValue();
618 setStylesheets : function(stylesheets)
620 this.editorcore.setStylesheets(stylesheets);
623 removeStylesheets : function()
625 this.editorcore.removeStylesheets();
629 // hide stuff that is not compatible
647 * @cfg {String} fieldClass @hide
650 * @cfg {String} focusClass @hide
653 * @cfg {String} autoCreate @hide
656 * @cfg {String} inputType @hide
659 * @cfg {String} invalidClass @hide
662 * @cfg {String} invalidText @hide
665 * @cfg {String} msgFx @hide
668 * @cfg {String} validateOnBlur @hide