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 {string} bodyCls- default '' default classes to add to body of editable area - usually undoreset is a good start..
103 // private properties
104 validationEvent : false,
109 onFocus : Roo.emptyFn,
113 actionMode : 'container', // defaults to hiding it...
115 defaultAutoCreate : { // modified by initCompnoent..
117 style:"width:500px;height:300px;",
118 autocomplete: "new-password"
122 initComponent : function(){
126 * Fires when the editor is fully initialized (including the iframe)
127 * @param {HtmlEditor} this
132 * Fires when the editor is first receives the focus. Any insertion must wait
133 * until after this event.
134 * @param {HtmlEditor} this
139 * Fires before the textarea is updated with content from the editor iframe. Return false
140 * to cancel the sync.
141 * @param {HtmlEditor} this
142 * @param {String} html
147 * Fires before the iframe editor is updated with content from the textarea. Return false
148 * to cancel the push.
149 * @param {HtmlEditor} this
150 * @param {String} html
155 * Fires when the textarea is updated with content from the editor iframe.
156 * @param {HtmlEditor} this
157 * @param {String} html
162 * Fires when the iframe editor is updated with content from the textarea.
163 * @param {HtmlEditor} this
164 * @param {String} html
168 * @event editmodechange
169 * Fires when the editor switches edit modes
170 * @param {HtmlEditor} this
171 * @param {Boolean} sourceEdit True if source edit, false if standard editing.
173 editmodechange: true,
176 * Fires when on any editor (mouse up/down cursor movement etc.) - used for toolbar hooks.
177 * @param {HtmlEditor} this
182 * Fires when on first focus - needed by toolbars..
183 * @param {HtmlEditor} this
188 * Auto save the htmlEditor value as a file into Events
189 * @param {HtmlEditor} this
193 * @event savedpreview
194 * preview the saved version of htmlEditor
195 * @param {HtmlEditor} this
200 * @event stylesheetsclick
201 * Fires when press the Sytlesheets button
202 * @param {Roo.HtmlEditorCore} this
204 stylesheetsclick: true,
207 * Fires when press user pastes into the editor
208 * @param {Roo.HtmlEditorCore} this
212 this.defaultAutoCreate = {
214 style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
215 autocomplete: "new-password"
220 * Protected method that will not generally be called directly. It
221 * is called when the editor creates its toolbar. Override this method if you need to
222 * add custom toolbar buttons.
223 * @param {HtmlEditor} editor
225 createToolbar : function(editor){
226 Roo.log("create toolbars");
227 if (!editor.toolbars || !editor.toolbars.length) {
228 editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
231 for (var i =0 ; i < editor.toolbars.length;i++) {
232 editor.toolbars[i] = Roo.factory(
233 typeof(editor.toolbars[i]) == 'string' ?
234 { xtype: editor.toolbars[i]} : editor.toolbars[i],
235 Roo.form.HtmlEditor);
236 editor.toolbars[i].init(editor);
244 onRender : function(ct, position)
247 Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
249 this.wrap = this.el.wrap({
250 cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
253 this.editorcore.onRender(ct, position);
255 if (this.resizable) {
256 this.resizeEl = new Roo.Resizable(this.wrap, {
260 minHeight : this.height,
262 handles : this.resizable,
265 resize : function(r, w, h) {
266 _t.onResize(w,h); // -something
272 this.createToolbar(this);
276 this.setSize(this.wrap.getSize());
279 this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
280 // should trigger onReize..
283 this.keyNav = new Roo.KeyNav(this.el, {
288 var value = this.getValue();
290 var start = this.el.dom.selectionStart;
291 var end = this.el.dom.selectionEnd;
295 this.setValue(value.substring(0, start) + "\t" + value.substring(end));
296 this.el.dom.setSelectionRange(end + 1, end + 1);
300 var f = value.substring(0, start).split("\t");
302 if(f.pop().length != 0){
306 this.setValue(f.join("\t") + value.substring(end));
307 this.el.dom.setSelectionRange(start - 1, start - 1);
311 "home" : function(e){
314 var curr = this.el.dom.selectionStart;
315 var lines = this.getValue().split("\n");
322 this.el.dom.setSelectionRange(0, 0);
328 for (var i = 0; i < lines.length;i++) {
329 pos += lines[i].length;
339 pos -= lines[i].length;
345 this.el.dom.setSelectionRange(pos, pos);
349 this.el.dom.selectionStart = pos;
350 this.el.dom.selectionEnd = curr;
356 var curr = this.el.dom.selectionStart;
357 var lines = this.getValue().split("\n");
364 this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
370 for (var i = 0; i < lines.length;i++) {
372 pos += lines[i].length;
386 this.el.dom.setSelectionRange(pos, pos);
390 this.el.dom.selectionStart = curr;
391 this.el.dom.selectionEnd = pos;
396 doRelay : function(foo, bar, hname){
397 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
403 // if(this.autosave && this.w){
404 // this.autoSaveFn = setInterval(this.autosave, 1000);
409 onResize : function(w, h)
411 Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
416 if(typeof w == 'number'){
417 var aw = w - this.wrap.getFrameWidth('lr');
418 this.el.setWidth(this.adjustWidth('textarea', aw));
421 if(typeof h == 'number'){
423 for (var i =0; i < this.toolbars.length;i++) {
424 // fixme - ask toolbars for heights?
425 tbh += this.toolbars[i].tb.el.getHeight();
426 if (this.toolbars[i].footer) {
427 tbh += this.toolbars[i].footer.el.getHeight();
434 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
435 ah -= 5; // knock a few pixes off for look..
437 this.el.setHeight(this.adjustWidth('textarea', ah));
441 Roo.log('onResize:' + [w,h,ew,eh].join(',') );
442 this.editorcore.onResize(ew,eh);
447 * Toggles the editor between standard and source edit mode.
448 * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
450 toggleSourceEdit : function(sourceEditMode)
452 this.editorcore.toggleSourceEdit(sourceEditMode);
454 if(this.editorcore.sourceEditMode){
455 Roo.log('editor - showing textarea');
458 // Roo.log(this.syncValue());
459 this.editorcore.syncValue();
460 this.el.removeClass('x-hidden');
461 this.el.dom.removeAttribute('tabIndex');
463 this.el.dom.scrollTop = 0;
466 for (var i = 0; i < this.toolbars.length; i++) {
467 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
468 this.toolbars[i].tb.hide();
469 this.toolbars[i].footer.hide();
474 Roo.log('editor - hiding textarea');
476 // Roo.log(this.pushValue());
477 this.editorcore.pushValue();
479 this.el.addClass('x-hidden');
480 this.el.dom.setAttribute('tabIndex', -1);
482 for (var i = 0; i < this.toolbars.length; i++) {
483 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
484 this.toolbars[i].tb.show();
485 this.toolbars[i].footer.show();
492 this.setSize(this.wrap.getSize());
493 this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
495 this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
498 // private (for BoxComponent)
499 adjustSize : Roo.BoxComponent.prototype.adjustSize,
501 // private (for BoxComponent)
502 getResizeEl : function(){
506 // private (for BoxComponent)
507 getPositionEl : function(){
512 initEvents : function(){
513 this.originalValue = this.getValue();
517 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
520 markInvalid : Roo.emptyFn,
522 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
525 clearInvalid : Roo.emptyFn,
527 setValue : function(v){
528 Roo.form.HtmlEditor.superclass.setValue.call(this, v);
529 this.editorcore.pushValue();
534 deferFocus : function(){
535 this.focus.defer(10, this);
540 this.editorcore.focus();
546 onDestroy : function(){
552 for (var i =0; i < this.toolbars.length;i++) {
553 // fixme - ask toolbars for heights?
554 this.toolbars[i].onDestroy();
557 this.wrap.dom.innerHTML = '';
563 onFirstFocus : function(){
564 //Roo.log("onFirstFocus");
565 this.editorcore.onFirstFocus();
566 for (var i =0; i < this.toolbars.length;i++) {
567 this.toolbars[i].onFirstFocus();
573 syncValue : function()
575 this.editorcore.syncValue();
578 pushValue : function()
580 this.editorcore.pushValue();
583 setStylesheets : function(stylesheets)
585 this.editorcore.setStylesheets(stylesheets);
588 removeStylesheets : function()
590 this.editorcore.removeStylesheets();
594 // hide stuff that is not compatible
612 * @cfg {String} fieldClass @hide
615 * @cfg {String} focusClass @hide
618 * @cfg {String} autoCreate @hide
621 * @cfg {String} inputType @hide
624 * @cfg {String} invalidClass @hide
627 * @cfg {String} invalidText @hide
630 * @cfg {String} msgFx @hide
633 * @cfg {String} validateOnBlur @hide