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
211 this.defaultAutoCreate = {
213 style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
214 autocomplete: "new-password"
219 * Protected method that will not generally be called directly. It
220 * is called when the editor creates its toolbar. Override this method if you need to
221 * add custom toolbar buttons.
222 * @param {HtmlEditor} editor
224 createToolbar : function(editor){
225 Roo.log("create toolbars");
226 if (!editor.toolbars || !editor.toolbars.length) {
227 editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
230 for (var i =0 ; i < editor.toolbars.length;i++) {
231 editor.toolbars[i] = Roo.factory(
232 typeof(editor.toolbars[i]) == 'string' ?
233 { xtype: editor.toolbars[i]} : editor.toolbars[i],
234 Roo.form.HtmlEditor);
235 editor.toolbars[i].init(editor);
243 onRender : function(ct, position)
246 Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
248 this.wrap = this.el.wrap({
249 cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
252 this.editorcore.onRender(ct, position);
254 if (this.resizable) {
255 this.resizeEl = new Roo.Resizable(this.wrap, {
259 minHeight : this.height,
261 handles : this.resizable,
264 resize : function(r, w, h) {
265 _t.onResize(w,h); // -something
271 this.createToolbar(this);
275 this.setSize(this.wrap.getSize());
278 this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
279 // should trigger onReize..
282 this.keyNav = new Roo.KeyNav(this.el, {
287 var value = this.getValue();
289 var start = this.el.dom.selectionStart;
290 var end = this.el.dom.selectionEnd;
294 this.setValue(value.substring(0, start) + "\t" + value.substring(end));
295 this.el.dom.setSelectionRange(end + 1, end + 1);
299 var f = value.substring(0, start).split("\t");
301 if(f.pop().length != 0){
305 this.setValue(f.join("\t") + value.substring(end));
306 this.el.dom.setSelectionRange(start - 1, start - 1);
310 "home" : function(e){
313 var curr = this.el.dom.selectionStart;
314 var lines = this.getValue().split("\n");
321 this.el.dom.setSelectionRange(0, 0);
327 for (var i = 0; i < lines.length;i++) {
328 pos += lines[i].length;
338 pos -= lines[i].length;
344 this.el.dom.setSelectionRange(pos, pos);
348 this.el.dom.selectionStart = pos;
349 this.el.dom.selectionEnd = curr;
355 var curr = this.el.dom.selectionStart;
356 var lines = this.getValue().split("\n");
363 this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
369 for (var i = 0; i < lines.length;i++) {
371 pos += lines[i].length;
385 this.el.dom.setSelectionRange(pos, pos);
389 this.el.dom.selectionStart = curr;
390 this.el.dom.selectionEnd = pos;
395 doRelay : function(foo, bar, hname){
396 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
402 // if(this.autosave && this.w){
403 // this.autoSaveFn = setInterval(this.autosave, 1000);
408 onResize : function(w, h)
410 Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
415 if(typeof w == 'number'){
416 var aw = w - this.wrap.getFrameWidth('lr');
417 this.el.setWidth(this.adjustWidth('textarea', aw));
420 if(typeof h == 'number'){
422 for (var i =0; i < this.toolbars.length;i++) {
423 // fixme - ask toolbars for heights?
424 tbh += this.toolbars[i].tb.el.getHeight();
425 if (this.toolbars[i].footer) {
426 tbh += this.toolbars[i].footer.el.getHeight();
433 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
434 ah -= 5; // knock a few pixes off for look..
436 this.el.setHeight(this.adjustWidth('textarea', ah));
440 Roo.log('onResize:' + [w,h,ew,eh].join(',') );
441 this.editorcore.onResize(ew,eh);
446 * Toggles the editor between standard and source edit mode.
447 * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
449 toggleSourceEdit : function(sourceEditMode)
451 this.editorcore.toggleSourceEdit(sourceEditMode);
453 if(this.editorcore.sourceEditMode){
454 Roo.log('editor - showing textarea');
457 // Roo.log(this.syncValue());
458 this.editorcore.syncValue();
459 this.el.removeClass('x-hidden');
460 this.el.dom.removeAttribute('tabIndex');
462 this.el.dom.scrollTop = 0;
465 for (var i = 0; i < this.toolbars.length; i++) {
466 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
467 this.toolbars[i].tb.hide();
468 this.toolbars[i].footer.hide();
473 Roo.log('editor - hiding textarea');
475 // Roo.log(this.pushValue());
476 this.editorcore.pushValue();
478 this.el.addClass('x-hidden');
479 this.el.dom.setAttribute('tabIndex', -1);
481 for (var i = 0; i < this.toolbars.length; i++) {
482 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
483 this.toolbars[i].tb.show();
484 this.toolbars[i].footer.show();
491 this.setSize(this.wrap.getSize());
492 this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
494 this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
497 // private (for BoxComponent)
498 adjustSize : Roo.BoxComponent.prototype.adjustSize,
500 // private (for BoxComponent)
501 getResizeEl : function(){
505 // private (for BoxComponent)
506 getPositionEl : function(){
511 initEvents : function(){
512 this.originalValue = this.getValue();
516 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
519 markInvalid : Roo.emptyFn,
521 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
524 clearInvalid : Roo.emptyFn,
526 setValue : function(v){
527 Roo.form.HtmlEditor.superclass.setValue.call(this, v);
528 this.editorcore.pushValue();
533 deferFocus : function(){
534 this.focus.defer(10, this);
539 this.editorcore.focus();
545 onDestroy : function(){
551 for (var i =0; i < this.toolbars.length;i++) {
552 // fixme - ask toolbars for heights?
553 this.toolbars[i].onDestroy();
556 this.wrap.dom.innerHTML = '';
562 onFirstFocus : function(){
563 //Roo.log("onFirstFocus");
564 this.editorcore.onFirstFocus();
565 for (var i =0; i < this.toolbars.length;i++) {
566 this.toolbars[i].onFirstFocus();
572 syncValue : function()
574 this.editorcore.syncValue();
577 pushValue : function()
579 this.editorcore.pushValue();
582 setStylesheets : function(stylesheets)
584 this.editorcore.setStylesheets(stylesheets);
587 removeStylesheets : function()
589 this.editorcore.removeStylesheets();
593 // hide stuff that is not compatible
611 * @cfg {String} fieldClass @hide
614 * @cfg {String} focusClass @hide
617 * @cfg {String} autoCreate @hide
620 * @cfg {String} inputType @hide
623 * @cfg {String} invalidClass @hide
626 * @cfg {String} invalidText @hide
629 * @cfg {String} msgFx @hide
632 * @cfg {String} validateOnBlur @hide