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);
250 * get the Context selected node
251 * @returns {DomElement|boolean} selected node if active or false if none
254 getSelectedNode : function()
256 if (this.toolbars.length < 2 || !this.toolbars[1].tb) {
259 return this.toolbars[1].tb.selectedNode;
263 onRender : function(ct, position)
266 Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
268 this.wrap = this.el.wrap({
269 cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
272 this.editorcore.onRender(ct, position);
274 if (this.resizable) {
275 this.resizeEl = new Roo.Resizable(this.wrap, {
279 minHeight : this.height,
281 handles : this.resizable,
284 resize : function(r, w, h) {
285 _t.onResize(w,h); // -something
291 this.createToolbar(this);
295 this.setSize(this.wrap.getSize());
298 this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
299 // should trigger onReize..
302 this.keyNav = new Roo.KeyNav(this.el, {
307 var value = this.getValue();
309 var start = this.el.dom.selectionStart;
310 var end = this.el.dom.selectionEnd;
314 this.setValue(value.substring(0, start) + "\t" + value.substring(end));
315 this.el.dom.setSelectionRange(end + 1, end + 1);
319 var f = value.substring(0, start).split("\t");
321 if(f.pop().length != 0){
325 this.setValue(f.join("\t") + value.substring(end));
326 this.el.dom.setSelectionRange(start - 1, start - 1);
330 "home" : function(e){
333 var curr = this.el.dom.selectionStart;
334 var lines = this.getValue().split("\n");
341 this.el.dom.setSelectionRange(0, 0);
347 for (var i = 0; i < lines.length;i++) {
348 pos += lines[i].length;
358 pos -= lines[i].length;
364 this.el.dom.setSelectionRange(pos, pos);
368 this.el.dom.selectionStart = pos;
369 this.el.dom.selectionEnd = curr;
375 var curr = this.el.dom.selectionStart;
376 var lines = this.getValue().split("\n");
383 this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
389 for (var i = 0; i < lines.length;i++) {
391 pos += lines[i].length;
405 this.el.dom.setSelectionRange(pos, pos);
409 this.el.dom.selectionStart = curr;
410 this.el.dom.selectionEnd = pos;
415 doRelay : function(foo, bar, hname){
416 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
422 // if(this.autosave && this.w){
423 // this.autoSaveFn = setInterval(this.autosave, 1000);
428 onResize : function(w, h)
430 Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
435 if(typeof w == 'number'){
436 var aw = w - this.wrap.getFrameWidth('lr');
437 this.el.setWidth(this.adjustWidth('textarea', aw));
440 if(typeof h == 'number'){
442 for (var i =0; i < this.toolbars.length;i++) {
443 // fixme - ask toolbars for heights?
444 tbh += this.toolbars[i].tb.el.getHeight();
445 if (this.toolbars[i].footer) {
446 tbh += this.toolbars[i].footer.el.getHeight();
453 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
454 ah -= 5; // knock a few pixes off for look..
456 this.el.setHeight(this.adjustWidth('textarea', ah));
460 Roo.log('onResize:' + [w,h,ew,eh].join(',') );
461 this.editorcore.onResize(ew,eh);
466 * Toggles the editor between standard and source edit mode.
467 * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
469 toggleSourceEdit : function(sourceEditMode)
471 this.editorcore.toggleSourceEdit(sourceEditMode);
473 if(this.editorcore.sourceEditMode){
474 Roo.log('editor - showing textarea');
477 // Roo.log(this.syncValue());
478 this.editorcore.syncValue();
479 this.el.removeClass('x-hidden');
480 this.el.dom.removeAttribute('tabIndex');
482 this.el.dom.scrollTop = 0;
485 for (var i = 0; i < this.toolbars.length; i++) {
486 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
487 this.toolbars[i].tb.hide();
488 this.toolbars[i].footer.hide();
493 Roo.log('editor - hiding textarea');
495 // Roo.log(this.pushValue());
496 this.editorcore.pushValue();
498 this.el.addClass('x-hidden');
499 this.el.dom.setAttribute('tabIndex', -1);
501 for (var i = 0; i < this.toolbars.length; i++) {
502 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
503 this.toolbars[i].tb.show();
504 this.toolbars[i].footer.show();
511 this.setSize(this.wrap.getSize());
512 this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
514 this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
517 // private (for BoxComponent)
518 adjustSize : Roo.BoxComponent.prototype.adjustSize,
520 // private (for BoxComponent)
521 getResizeEl : function(){
525 // private (for BoxComponent)
526 getPositionEl : function(){
531 initEvents : function(){
532 this.originalValue = this.getValue();
536 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
539 markInvalid : Roo.emptyFn,
541 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
544 clearInvalid : Roo.emptyFn,
546 setValue : function(v){
547 Roo.form.HtmlEditor.superclass.setValue.call(this, v);
548 this.editorcore.pushValue();
553 deferFocus : function(){
554 this.focus.defer(10, this);
559 this.editorcore.focus();
565 onDestroy : function(){
571 for (var i =0; i < this.toolbars.length;i++) {
572 // fixme - ask toolbars for heights?
573 this.toolbars[i].onDestroy();
576 this.wrap.dom.innerHTML = '';
582 onFirstFocus : function(){
583 //Roo.log("onFirstFocus");
584 this.editorcore.onFirstFocus();
585 for (var i =0; i < this.toolbars.length;i++) {
586 this.toolbars[i].onFirstFocus();
592 syncValue : function()
594 this.editorcore.syncValue();
597 pushValue : function()
599 this.editorcore.pushValue();
602 setStylesheets : function(stylesheets)
604 this.editorcore.setStylesheets(stylesheets);
607 removeStylesheets : function()
609 this.editorcore.removeStylesheets();
613 // hide stuff that is not compatible
631 * @cfg {String} fieldClass @hide
634 * @cfg {String} focusClass @hide
637 * @cfg {String} autoCreate @hide
640 * @cfg {String} inputType @hide
643 * @cfg {String} invalidClass @hide
646 * @cfg {String} invalidText @hide
649 * @cfg {String} msgFx @hide
652 * @cfg {String} validateOnBlur @hide