4 * HtmlArea (tinyMCE) wrapper
6 * @author Gabor Berczi <gabor.berczi@devworx.hu>
11 Prado.WebUI.THtmlArea = jQuery.klass(Prado.WebUI.Control,
13 initialize: function($super, options)
15 options.ID = options.EditorOptions.elements;
19 onInit : function(options)
21 this.options = options;
23 this.registerAjaxHook();
25 this.registerInstance();
30 registerInstance: function()
32 if (typeof tinyMCE_GZ == 'undefined')
34 if (typeof tinyMCE == 'undefined')
36 if (typeof Prado.CallbackRequest != 'undefined')
37 if (typeof Prado.CallbackRequest.transport != 'undefined')
39 // we're in a callback
40 // try it again in some time, as tinyMCE is most likely still loading
41 this.setTimeout(this.registerInstance.bind(this), 50);
44 throw "TinyMCE libraries must be loaded first";
46 Prado.WebUI.THtmlArea.tinyMCELoadState = 255;
50 if (Prado.WebUI.THtmlArea.tinyMCELoadState==255)
54 Prado.WebUI.THtmlArea.pendingRegistrations.push(this.options.ID);
55 if (Prado.WebUI.THtmlArea.tinyMCELoadState==0)
57 Prado.WebUI.THtmlArea.tinyMCELoadState = 1;
59 this.options.CompressionOptions,
60 this.compressedScriptsLoaded.bind(this)
66 compressedScriptsLoaded: function()
68 Prado.WebUI.THtmlArea.tinyMCELoadState = 255;
70 while(Prado.WebUI.THtmlArea.pendingRegistrations.length>0)
71 if (wrapper = Prado.Registry[Prado.WebUI.THtmlArea.pendingRegistrations.pop()])
72 wrapper.initInstance();
75 initInstance: function()
77 tinyMCE.init(this.options.EditorOptions);
80 checkInstance: function()
82 if (!document.getElementById(this.ID))
86 removePreviousInstance: function()
88 for(var i=0;i<tinyMCE.editors.length;i++)
89 if (tinyMCE.editors[i].id==this.ID)
91 tinyMCE.editors.splice(i,1); // ugly hack, but works
92 this.deRegisterAjaxHook();
98 registerAjaxHook: function()
100 jQuery(document).on('ajaxComplete', this.ajaxresponder.bind(this));
104 deRegisterAjaxHook: function()
106 jQuery(document).off('ajaxComplete', this.ajaxresponder.bind(this));
109 ajaxresponder: function(request)
111 if(request && (request instanceof Prado.CallbackRequest))
112 this.checkInstance();
117 // check for previous tinyMCE registration, and try to remove it gracefully first
118 var prev = tinyMCE.get(this.ID);
122 tinyMCE.execCommand('mceFocus', false, this.ID);
123 // when removed, tinyMCE restores its content to the textarea. If the textarea content has been
124 // updated in this same callback, it will be overwritten with the old content. Workaround this.
125 var curtext = jQuery('#'+this.ID).get(0).value;
126 tinyMCE.execCommand('mceRemoveControl', false, this.ID);
127 jQuery('#'+this.ID).get(0).value = curtext;
131 // suppress error here in case editor can't be properly removed
132 // (happens when <textarea> has been removed from DOM tree without deinitialzing the tinyMCE editor first)
135 // doublecheck editor instance here and remove manually from tinyMCE-registry if neccessary
136 this.removePreviousInstance();
138 this.deRegisterAjaxHook();
142 jQuery.extend(Prado.WebUI.THtmlArea,
144 pendingRegistrations : [],