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
206 this.defaultAutoCreate = {
208 style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
209 autocomplete: "new-password"
214 * Protected method that will not generally be called directly. It
215 * is called when the editor creates its toolbar. Override this method if you need to
216 * add custom toolbar buttons.
217 * @param {HtmlEditor} editor
219 createToolbar : function(editor){
220 Roo.log("create toolbars");
221 if (!editor.toolbars || !editor.toolbars.length) {
222 editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
225 for (var i =0 ; i < editor.toolbars.length;i++) {
226 editor.toolbars[i] = Roo.factory(
227 typeof(editor.toolbars[i]) == 'string' ?
228 { xtype: editor.toolbars[i]} : editor.toolbars[i],
229 Roo.form.HtmlEditor);
230 editor.toolbars[i].init(editor);
238 onRender : function(ct, position)
241 Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
243 this.wrap = this.el.wrap({
244 cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
247 this.editorcore.onRender(ct, position);
249 if (this.resizable) {
250 this.resizeEl = new Roo.Resizable(this.wrap, {
254 minHeight : this.height,
256 handles : this.resizable,
259 resize : function(r, w, h) {
260 _t.onResize(w,h); // -something
266 this.createToolbar(this);
270 this.setSize(this.wrap.getSize());
273 this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
274 // should trigger onReize..
277 this.keyNav = new Roo.KeyNav(this.el, {
282 var value = this.getValue();
284 var start = this.el.dom.selectionStart;
285 var end = this.el.dom.selectionEnd;
289 this.setValue(value.substring(0, start) + "\t" + value.substring(end));
290 this.el.dom.setSelectionRange(end + 1, end + 1);
294 var f = value.substring(0, start).split("\t");
296 if(f.pop().length != 0){
300 this.setValue(f.join("\t") + value.substring(end));
301 this.el.dom.setSelectionRange(start - 1, start - 1);
305 "home" : function(e){
308 var curr = this.el.dom.selectionStart;
309 var lines = this.getValue().split("\n");
316 this.el.dom.setSelectionRange(0, 0);
322 for (var i = 0; i < lines.length;i++) {
323 pos += lines[i].length;
333 pos -= lines[i].length;
339 this.el.dom.setSelectionRange(pos, pos);
343 this.el.dom.selectionStart = pos;
344 this.el.dom.selectionEnd = curr;
350 var curr = this.el.dom.selectionStart;
351 var lines = this.getValue().split("\n");
358 this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
364 for (var i = 0; i < lines.length;i++) {
366 pos += lines[i].length;
380 this.el.dom.setSelectionRange(pos, pos);
384 this.el.dom.selectionStart = curr;
385 this.el.dom.selectionEnd = pos;
390 doRelay : function(foo, bar, hname){
391 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
397 // if(this.autosave && this.w){
398 // this.autoSaveFn = setInterval(this.autosave, 1000);
403 onResize : function(w, h)
405 Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
410 if(typeof w == 'number'){
411 var aw = w - this.wrap.getFrameWidth('lr');
412 this.el.setWidth(this.adjustWidth('textarea', aw));
415 if(typeof h == 'number'){
417 for (var i =0; i < this.toolbars.length;i++) {
418 // fixme - ask toolbars for heights?
419 tbh += this.toolbars[i].tb.el.getHeight();
420 if (this.toolbars[i].footer) {
421 tbh += this.toolbars[i].footer.el.getHeight();
428 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
429 ah -= 5; // knock a few pixes off for look..
431 this.el.setHeight(this.adjustWidth('textarea', ah));
435 Roo.log('onResize:' + [w,h,ew,eh].join(',') );
436 this.editorcore.onResize(ew,eh);
441 * Toggles the editor between standard and source edit mode.
442 * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
444 toggleSourceEdit : function(sourceEditMode)
446 this.editorcore.toggleSourceEdit(sourceEditMode);
448 if(this.editorcore.sourceEditMode){
449 Roo.log('editor - showing textarea');
452 // Roo.log(this.syncValue());
453 this.editorcore.syncValue();
454 this.el.removeClass('x-hidden');
455 this.el.dom.removeAttribute('tabIndex');
457 this.el.dom.scrollTop = 0;
460 for (var i = 0; i < this.toolbars.length; i++) {
461 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
462 this.toolbars[i].tb.hide();
463 this.toolbars[i].footer.hide();
468 Roo.log('editor - hiding textarea');
470 // Roo.log(this.pushValue());
471 this.editorcore.pushValue();
473 this.el.addClass('x-hidden');
474 this.el.dom.setAttribute('tabIndex', -1);
476 for (var i = 0; i < this.toolbars.length; i++) {
477 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
478 this.toolbars[i].tb.show();
479 this.toolbars[i].footer.show();
486 this.setSize(this.wrap.getSize());
487 this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
489 this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
492 // private (for BoxComponent)
493 adjustSize : Roo.BoxComponent.prototype.adjustSize,
495 // private (for BoxComponent)
496 getResizeEl : function(){
500 // private (for BoxComponent)
501 getPositionEl : function(){
506 initEvents : function(){
507 this.originalValue = this.getValue();
511 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
514 markInvalid : Roo.emptyFn,
516 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
519 clearInvalid : Roo.emptyFn,
521 setValue : function(v){
522 Roo.form.HtmlEditor.superclass.setValue.call(this, v);
523 this.editorcore.pushValue();
528 deferFocus : function(){
529 this.focus.defer(10, this);
534 this.editorcore.focus();
540 onDestroy : function(){
546 for (var i =0; i < this.toolbars.length;i++) {
547 // fixme - ask toolbars for heights?
548 this.toolbars[i].onDestroy();
551 this.wrap.dom.innerHTML = '';
557 onFirstFocus : function(){
558 //Roo.log("onFirstFocus");
559 this.editorcore.onFirstFocus();
560 for (var i =0; i < this.toolbars.length;i++) {
561 this.toolbars[i].onFirstFocus();
567 syncValue : function()
569 this.editorcore.syncValue();
572 pushValue : function()
574 this.editorcore.pushValue();
577 setStylesheets : function(stylesheets)
579 this.editorcore.setStylesheets(stylesheets);
582 removeStylesheets : function()
584 this.editorcore.removeStylesheets();
588 // hide stuff that is not compatible
606 * @cfg {String} fieldClass @hide
609 * @cfg {String} focusClass @hide
612 * @cfg {String} autoCreate @hide
615 * @cfg {String} inputType @hide
618 * @cfg {String} invalidClass @hide
621 * @cfg {String} invalidText @hide
624 * @cfg {String} msgFx @hide
627 * @cfg {String} validateOnBlur @hide