]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/Javascripts/source/prado/controls/htmlarea.js
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Web / Javascripts / source / prado / controls / htmlarea.js
1
2 /*
3  *
4  * HtmlArea (tinyMCE) wrapper
5  *
6  * @author Gabor Berczi <gabor.berczi@devworx.hu>
7  *
8 */
9
10
11 Prado.WebUI.THtmlArea = jQuery.klass(Prado.WebUI.Control,
12 {
13         initialize: function($super, options)
14         {
15                 options.ID = options.EditorOptions.elements;
16                 $super(options);
17         },
18
19     onInit : function(options)
20         {
21                 this.options = options;
22
23                 this.registerAjaxHook();
24
25                 this.registerInstance();
26         },
27
28
29
30         registerInstance: function()
31         {
32                 if (typeof tinyMCE_GZ == 'undefined')
33                         {
34                                 if (typeof tinyMCE == 'undefined')
35                                         {
36                                                 if (typeof Prado.CallbackRequest != 'undefined')
37                                                         if (typeof Prado.CallbackRequest.transport != 'undefined')
38                                                         {
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);
42                                                                 return;
43                                                         }
44                                                 throw "TinyMCE libraries must be loaded first";
45                                         }
46                                 Prado.WebUI.THtmlArea.tinyMCELoadState = 255;
47                                 this.initInstance();
48                         }
49                 else
50                         if (Prado.WebUI.THtmlArea.tinyMCELoadState==255)
51                                 this.initInstance();
52                         else
53                                 {
54                                         Prado.WebUI.THtmlArea.pendingRegistrations.push(this.options.ID);
55                                         if (Prado.WebUI.THtmlArea.tinyMCELoadState==0)
56                                         {
57                                                 Prado.WebUI.THtmlArea.tinyMCELoadState = 1;
58                                                 tinyMCE_GZ.init(
59                                                         this.options.CompressionOptions,
60                                                         this.compressedScriptsLoaded.bind(this)
61                                                 );
62                                         }
63                                 }
64         },
65
66         compressedScriptsLoaded: function()
67         {
68                 Prado.WebUI.THtmlArea.tinyMCELoadState = 255;
69                 var wrapper;
70                 while(Prado.WebUI.THtmlArea.pendingRegistrations.length>0)
71                         if (wrapper = Prado.Registry[Prado.WebUI.THtmlArea.pendingRegistrations.pop()])
72                                 wrapper.initInstance();
73         },
74
75         initInstance: function()
76         {
77                 tinyMCE.init(this.options.EditorOptions);
78         },
79
80         checkInstance: function()
81         {
82                 if (!document.getElementById(this.ID))
83                         this.deinitialize();
84         },
85
86         removePreviousInstance: function()
87         {
88                 for(var i=0;i<tinyMCE.editors.length;i++)
89                         if (tinyMCE.editors[i].id==this.ID)
90                         {
91                                 tinyMCE.editors.splice(i,1); // ugly hack, but works
92                                 this.deRegisterAjaxHook();
93                                 this.deregister();
94                                 i--;
95                         }
96         },
97
98         registerAjaxHook: function()
99         {
100                 jQuery(document).on('ajaxComplete', this.ajaxresponder.bind(this));
101         },
102
103
104         deRegisterAjaxHook: function()
105         {
106                 jQuery(document).off('ajaxComplete', this.ajaxresponder.bind(this));
107         },
108
109         ajaxresponder: function(request)
110         {
111                 if(request && (request instanceof Prado.CallbackRequest))
112                         this.checkInstance();
113         },
114
115         onDone: function()
116         {
117                 // check for previous tinyMCE registration, and try to remove it gracefully first
118                 var prev = tinyMCE.get(this.ID);
119                 if (prev)
120                 try
121                 {
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;
128                 }
129                 catch (e)
130                 {
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)
133                 }
134
135                 // doublecheck editor instance here and remove manually from tinyMCE-registry if neccessary
136                 this.removePreviousInstance();
137
138                 this.deRegisterAjaxHook();
139         }
140 });
141
142 jQuery.extend(Prado.WebUI.THtmlArea,
143 {
144         pendingRegistrations : [],
145         tinyMCELoadState : 0
146 });
147
148