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..
111 // private properties
112 validationEvent : false,
117 onFocus : Roo.emptyFn,
121 actionMode : 'container', // defaults to hiding it...
123 defaultAutoCreate : { // modified by initCompnoent..
125 style:"width:500px;height:300px;",
126 autocomplete: "new-password"
130 initComponent : function(){
134 * Fires when the editor is fully initialized (including the iframe)
135 * @param {HtmlEditor} this
140 * Fires when the editor is first receives the focus. Any insertion must wait
141 * until after this event.
142 * @param {HtmlEditor} this
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
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
163 * Fires when the textarea is updated with content from the editor iframe.
164 * @param {HtmlEditor} this
165 * @param {String} html
170 * Fires when the iframe editor is updated with content from the textarea.
171 * @param {HtmlEditor} this
172 * @param {String} html
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.
181 editmodechange: true,
184 * Fires when on any editor (mouse up/down cursor movement etc.) - used for toolbar hooks.
185 * @param {HtmlEditor} this
190 * Fires when on first focus - needed by toolbars..
191 * @param {HtmlEditor} this
196 * Auto save the htmlEditor value as a file into Events
197 * @param {HtmlEditor} this
201 * @event savedpreview
202 * preview the saved version of htmlEditor
203 * @param {HtmlEditor} this
208 * @event stylesheetsclick
209 * Fires when press the Sytlesheets button
210 * @param {Roo.HtmlEditorCore} this
212 stylesheetsclick: true,
215 * Fires when press user pastes into the editor
216 * @param {Roo.HtmlEditorCore} this
220 this.defaultAutoCreate = {
222 style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
223 autocomplete: "new-password"
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
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?
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);
252 onRender : function(ct, position)
255 Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
257 this.wrap = this.el.wrap({
258 cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
261 this.editorcore.onRender(ct, position);
263 if (this.resizable) {
264 this.resizeEl = new Roo.Resizable(this.wrap, {
268 minHeight : this.height,
270 handles : this.resizable,
273 resize : function(r, w, h) {
274 _t.onResize(w,h); // -something
280 this.createToolbar(this);
284 this.setSize(this.wrap.getSize());
287 this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
288 // should trigger onReize..
291 this.keyNav = new Roo.KeyNav(this.el, {
296 var value = this.getValue();
298 var start = this.el.dom.selectionStart;
299 var end = this.el.dom.selectionEnd;
303 this.setValue(value.substring(0, start) + "\t" + value.substring(end));
304 this.el.dom.setSelectionRange(end + 1, end + 1);
308 var f = value.substring(0, start).split("\t");
310 if(f.pop().length != 0){
314 this.setValue(f.join("\t") + value.substring(end));
315 this.el.dom.setSelectionRange(start - 1, start - 1);
319 "home" : function(e){
322 var curr = this.el.dom.selectionStart;
323 var lines = this.getValue().split("\n");
330 this.el.dom.setSelectionRange(0, 0);
336 for (var i = 0; i < lines.length;i++) {
337 pos += lines[i].length;
347 pos -= lines[i].length;
353 this.el.dom.setSelectionRange(pos, pos);
357 this.el.dom.selectionStart = pos;
358 this.el.dom.selectionEnd = curr;
364 var curr = this.el.dom.selectionStart;
365 var lines = this.getValue().split("\n");
372 this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
378 for (var i = 0; i < lines.length;i++) {
380 pos += lines[i].length;
394 this.el.dom.setSelectionRange(pos, pos);
398 this.el.dom.selectionStart = curr;
399 this.el.dom.selectionEnd = pos;
404 doRelay : function(foo, bar, hname){
405 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
411 // if(this.autosave && this.w){
412 // this.autoSaveFn = setInterval(this.autosave, 1000);
417 onResize : function(w, h)
419 Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
424 if(typeof w == 'number'){
425 var aw = w - this.wrap.getFrameWidth('lr');
426 this.el.setWidth(this.adjustWidth('textarea', aw));
429 if(typeof h == 'number'){
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();
442 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
443 ah -= 5; // knock a few pixes off for look..
445 this.el.setHeight(this.adjustWidth('textarea', ah));
449 Roo.log('onResize:' + [w,h,ew,eh].join(',') );
450 this.editorcore.onResize(ew,eh);
455 * Toggles the editor between standard and source edit mode.
456 * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
458 toggleSourceEdit : function(sourceEditMode)
460 this.editorcore.toggleSourceEdit(sourceEditMode);
462 if(this.editorcore.sourceEditMode){
463 Roo.log('editor - showing textarea');
466 // Roo.log(this.syncValue());
467 this.editorcore.syncValue();
468 this.el.removeClass('x-hidden');
469 this.el.dom.removeAttribute('tabIndex');
471 this.el.dom.scrollTop = 0;
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();
482 Roo.log('editor - hiding textarea');
484 // Roo.log(this.pushValue());
485 this.editorcore.pushValue();
487 this.el.addClass('x-hidden');
488 this.el.dom.setAttribute('tabIndex', -1);
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();
500 this.setSize(this.wrap.getSize());
501 this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
503 this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
506 // private (for BoxComponent)
507 adjustSize : Roo.BoxComponent.prototype.adjustSize,
509 // private (for BoxComponent)
510 getResizeEl : function(){
514 // private (for BoxComponent)
515 getPositionEl : function(){
520 initEvents : function(){
521 this.originalValue = this.getValue();
525 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
528 markInvalid : Roo.emptyFn,
530 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
533 clearInvalid : Roo.emptyFn,
535 setValue : function(v){
536 Roo.form.HtmlEditor.superclass.setValue.call(this, v);
537 this.editorcore.pushValue();
542 deferFocus : function(){
543 this.focus.defer(10, this);
548 this.editorcore.focus();
554 onDestroy : function(){
560 for (var i =0; i < this.toolbars.length;i++) {
561 // fixme - ask toolbars for heights?
562 this.toolbars[i].onDestroy();
565 this.wrap.dom.innerHTML = '';
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();
581 syncValue : function()
583 this.editorcore.syncValue();
586 pushValue : function()
588 this.editorcore.pushValue();
591 setStylesheets : function(stylesheets)
593 this.editorcore.setStylesheets(stylesheets);
596 removeStylesheets : function()
598 this.editorcore.removeStylesheets();
602 // hide stuff that is not compatible
620 * @cfg {String} fieldClass @hide
623 * @cfg {String} focusClass @hide
626 * @cfg {String} autoCreate @hide
629 * @cfg {String} inputType @hide
632 * @cfg {String} invalidClass @hide
635 * @cfg {String} invalidText @hide
638 * @cfg {String} msgFx @hide
641 * @cfg {String} validateOnBlur @hide