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.
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.
98 validationEvent : false,
103 onFocus : Roo.emptyFn,
107 actionMode : 'container', // defaults to hiding it...
109 defaultAutoCreate : { // modified by initCompnoent..
111 style:"width:500px;height:300px;",
112 autocomplete: "new-password"
116 initComponent : function(){
120 * Fires when the editor is fully initialized (including the iframe)
121 * @param {HtmlEditor} this
126 * Fires when the editor is first receives the focus. Any insertion must wait
127 * until after this event.
128 * @param {HtmlEditor} this
133 * Fires before the textarea is updated with content from the editor iframe. Return false
134 * to cancel the sync.
135 * @param {HtmlEditor} this
136 * @param {String} html
141 * Fires before the iframe editor is updated with content from the textarea. Return false
142 * to cancel the push.
143 * @param {HtmlEditor} this
144 * @param {String} html
149 * Fires when the textarea is updated with content from the editor iframe.
150 * @param {HtmlEditor} this
151 * @param {String} html
156 * Fires when the iframe editor is updated with content from the textarea.
157 * @param {HtmlEditor} this
158 * @param {String} html
162 * @event editmodechange
163 * Fires when the editor switches edit modes
164 * @param {HtmlEditor} this
165 * @param {Boolean} sourceEdit True if source edit, false if standard editing.
167 editmodechange: true,
170 * Fires when on any editor (mouse up/down cursor movement etc.) - used for toolbar hooks.
171 * @param {HtmlEditor} this
176 * Fires when on first focus - needed by toolbars..
177 * @param {HtmlEditor} this
182 * Auto save the htmlEditor value as a file into Events
183 * @param {HtmlEditor} this
187 * @event savedpreview
188 * preview the saved version of htmlEditor
189 * @param {HtmlEditor} this
194 * @event stylesheetsclick
195 * Fires when press the Sytlesheets button
196 * @param {Roo.HtmlEditorCore} this
198 stylesheetsclick: true
200 this.defaultAutoCreate = {
202 style:'width: ' + this.width + 'px;height: ' + this.height + 'px;',
203 autocomplete: "new-password"
208 * Protected method that will not generally be called directly. It
209 * is called when the editor creates its toolbar. Override this method if you need to
210 * add custom toolbar buttons.
211 * @param {HtmlEditor} editor
213 createToolbar : function(editor){
214 Roo.log("create toolbars");
215 if (!editor.toolbars || !editor.toolbars.length) {
216 editor.toolbars = [ new Roo.form.HtmlEditor.ToolbarStandard() ]; // can be empty?
219 for (var i =0 ; i < editor.toolbars.length;i++) {
220 editor.toolbars[i] = Roo.factory(
221 typeof(editor.toolbars[i]) == 'string' ?
222 { xtype: editor.toolbars[i]} : editor.toolbars[i],
223 Roo.form.HtmlEditor);
224 editor.toolbars[i].init(editor);
232 onRender : function(ct, position)
235 Roo.form.HtmlEditor.superclass.onRender.call(this, ct, position);
237 this.wrap = this.el.wrap({
238 cls:'x-html-editor-wrap', cn:{cls:'x-html-editor-tb'}
241 this.editorcore.onRender(ct, position);
243 if (this.resizable) {
244 this.resizeEl = new Roo.Resizable(this.wrap, {
248 minHeight : this.height,
250 handles : this.resizable,
253 resize : function(r, w, h) {
254 _t.onResize(w,h); // -something
260 this.createToolbar(this);
264 this.setSize(this.wrap.getSize());
267 this.resizeEl.resizeTo.defer(100, this.resizeEl,[ this.width,this.height ] );
268 // should trigger onReize..
271 this.keyNav = new Roo.KeyNav(this.el, {
276 var value = this.getValue();
278 var start = this.el.dom.selectionStart;
279 var end = this.el.dom.selectionEnd;
283 this.setValue(value.substring(0, start) + "\t" + value.substring(end));
284 this.el.dom.setSelectionRange(end + 1, end + 1);
288 var f = value.substring(0, start).split("\t");
290 if(f.pop().length != 0){
294 this.setValue(f.join("\t") + value.substring(end));
295 this.el.dom.setSelectionRange(start - 1, start - 1);
299 "home" : function(e){
302 var curr = this.el.dom.selectionStart;
303 var lines = this.getValue().split("\n");
310 this.el.dom.setSelectionRange(0, 0);
316 for (var i = 0; i < lines.length;i++) {
317 pos += lines[i].length;
327 pos -= lines[i].length;
333 this.el.dom.setSelectionRange(pos, pos);
337 this.el.dom.selectionStart = pos;
338 this.el.dom.selectionEnd = curr;
344 var curr = this.el.dom.selectionStart;
345 var lines = this.getValue().split("\n");
352 this.el.dom.setSelectionRange(this.getValue().length, this.getValue().length);
358 for (var i = 0; i < lines.length;i++) {
360 pos += lines[i].length;
374 this.el.dom.setSelectionRange(pos, pos);
378 this.el.dom.selectionStart = curr;
379 this.el.dom.selectionEnd = pos;
384 doRelay : function(foo, bar, hname){
385 return Roo.KeyNav.prototype.doRelay.apply(this, arguments);
391 // if(this.autosave && this.w){
392 // this.autoSaveFn = setInterval(this.autosave, 1000);
397 onResize : function(w, h)
399 Roo.form.HtmlEditor.superclass.onResize.apply(this, arguments);
404 if(typeof w == 'number'){
405 var aw = w - this.wrap.getFrameWidth('lr');
406 this.el.setWidth(this.adjustWidth('textarea', aw));
409 if(typeof h == 'number'){
411 for (var i =0; i < this.toolbars.length;i++) {
412 // fixme - ask toolbars for heights?
413 tbh += this.toolbars[i].tb.el.getHeight();
414 if (this.toolbars[i].footer) {
415 tbh += this.toolbars[i].footer.el.getHeight();
422 var ah = h - this.wrap.getFrameWidth('tb') - tbh;// this.tb.el.getHeight();
423 ah -= 5; // knock a few pixes off for look..
425 this.el.setHeight(this.adjustWidth('textarea', ah));
429 Roo.log('onResize:' + [w,h,ew,eh].join(',') );
430 this.editorcore.onResize(ew,eh);
435 * Toggles the editor between standard and source edit mode.
436 * @param {Boolean} sourceEdit (optional) True for source edit, false for standard
438 toggleSourceEdit : function(sourceEditMode)
440 this.editorcore.toggleSourceEdit(sourceEditMode);
442 if(this.editorcore.sourceEditMode){
443 Roo.log('editor - showing textarea');
446 // Roo.log(this.syncValue());
447 this.editorcore.syncValue();
448 this.el.removeClass('x-hidden');
449 this.el.dom.removeAttribute('tabIndex');
452 for (var i = 0; i < this.toolbars.length; i++) {
453 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
454 this.toolbars[i].tb.hide();
455 this.toolbars[i].footer.hide();
460 Roo.log('editor - hiding textarea');
462 // Roo.log(this.pushValue());
463 this.editorcore.pushValue();
465 this.el.addClass('x-hidden');
466 this.el.dom.setAttribute('tabIndex', -1);
468 for (var i = 0; i < this.toolbars.length; i++) {
469 if(this.toolbars[i] instanceof Roo.form.HtmlEditor.ToolbarContext){
470 this.toolbars[i].tb.show();
471 this.toolbars[i].footer.show();
478 this.setSize(this.wrap.getSize());
479 this.onResize(this.wrap.getSize().width, this.wrap.getSize().height);
481 this.fireEvent('editmodechange', this, this.editorcore.sourceEditMode);
484 // private (for BoxComponent)
485 adjustSize : Roo.BoxComponent.prototype.adjustSize,
487 // private (for BoxComponent)
488 getResizeEl : function(){
492 // private (for BoxComponent)
493 getPositionEl : function(){
498 initEvents : function(){
499 this.originalValue = this.getValue();
503 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
506 markInvalid : Roo.emptyFn,
508 * Overridden and disabled. The editor element does not support standard valid/invalid marking. @hide
511 clearInvalid : Roo.emptyFn,
513 setValue : function(v){
514 Roo.form.HtmlEditor.superclass.setValue.call(this, v);
515 this.editorcore.pushValue();
520 deferFocus : function(){
521 this.focus.defer(10, this);
526 this.editorcore.focus();
532 onDestroy : function(){
538 for (var i =0; i < this.toolbars.length;i++) {
539 // fixme - ask toolbars for heights?
540 this.toolbars[i].onDestroy();
543 this.wrap.dom.innerHTML = '';
549 onFirstFocus : function(){
550 //Roo.log("onFirstFocus");
551 this.editorcore.onFirstFocus();
552 for (var i =0; i < this.toolbars.length;i++) {
553 this.toolbars[i].onFirstFocus();
559 syncValue : function()
561 this.editorcore.syncValue();
564 pushValue : function()
566 this.editorcore.pushValue();
569 setStylesheets : function(stylesheets)
571 this.editorcore.setStylesheets(stylesheets);
574 removeStylesheets : function()
576 this.editorcore.removeStylesheets();
580 // hide stuff that is not compatible
598 * @cfg {String} fieldClass @hide
601 * @cfg {String} focusClass @hide
604 * @cfg {String} autoCreate @hide
607 * @cfg {String} inputType @hide
610 * @cfg {String} invalidClass @hide
613 * @cfg {String} invalidText @hide
616 * @cfg {String} msgFx @hide
619 * @cfg {String} validateOnBlur @hide