]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Web/Javascripts/source/prado/controls/htmlarea.js
Add Baculum
[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 = Class.create(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                 var obj = this;
24                 this.ajaxresponder = {
25                         onComplete : function(request) 
26                         {
27                                 if(request && (request instanceof Prado.AjaxRequest))
28                                         obj.checkInstance();
29                         }
30                 };
31                 this.registerAjaxHook();
32
33                 this.registerInstance();
34         },
35         
36         registerInstance: function()
37         {
38                 if (typeof tinyMCE_GZ == 'undefined')
39                         {
40                                 if (typeof tinyMCE == 'undefined')
41                                         {
42                                                 if (typeof Prado.CallbackRequest != 'undefined')
43                                                         if (typeof Prado.CallbackRequest.transport != 'undefined')
44                                                         {
45                                                                 // we're in a callback
46                                                                 // try it again in some time, as tinyMCE is most likely still loading
47                                                                 this.setTimeout(this.registerInstance.bind(this), 50); 
48                                                                 return;
49                                                         }
50                                                 throw "TinyMCE libraries must be loaded first";
51                                         }
52                                 Prado.WebUI.THtmlArea.tinyMCELoadState = 255;
53                                 this.initInstance();
54                         }
55                 else
56                         if (Prado.WebUI.THtmlArea.tinyMCELoadState==255)
57                                 this.initInstance();
58                         else
59                                 {
60                                         Prado.WebUI.THtmlArea.pendingRegistrations.push(this.options.ID);
61                                         if (Prado.WebUI.THtmlArea.tinyMCELoadState==0)
62                                         {
63                                                 Prado.WebUI.THtmlArea.tinyMCELoadState = 1;
64                                                 tinyMCE_GZ.init(
65                                                         this.options.CompressionOptions,
66                                                         this.compressedScriptsLoaded.bind(this)
67                                                 );
68                                         }
69                                 }
70         },
71         
72         compressedScriptsLoaded: function()
73         {
74                 Prado.WebUI.THtmlArea.tinyMCELoadState = 255;
75                 var wrapper;
76                 while(Prado.WebUI.THtmlArea.pendingRegistrations.length>0)
77                         if (wrapper = Prado.Registry.get(Prado.WebUI.THtmlArea.pendingRegistrations.pop()))
78                                 wrapper.initInstance();
79         },
80
81         initInstance: function()
82         {
83                 tinyMCE.init(this.options.EditorOptions);
84         },
85
86         checkInstance: function()
87         {
88                 if (!document.getElementById(this.ID))
89                         this.deinitialize();
90         },
91
92         removePreviousInstance: function()
93         {
94                 for(var i=0;i<tinyMCE.editors.length;i++)
95                         if (tinyMCE.editors[i].id==this.ID)
96                         {
97                                 tinyMCE.editors.splice(i,1); // ugly hack, but works
98                                 this.deRegisterAjaxHook();
99                                 this.deregister();
100                                 i--;
101                         }
102         },
103
104         registerAjaxHook: function()
105         {
106                 if (typeof(Ajax)!="undefined")
107                         if (typeof(Ajax.Responders)!="undefined")
108                                 Ajax.Responders.register(this.ajaxresponder);
109         },
110
111
112         deRegisterAjaxHook: function()
113         {
114                 if (typeof(Ajax)!="undefined")
115                         if (typeof(Ajax.Responders)!="undefined")
116                                 Ajax.Responders.unregister(this.ajaxresponder);
117         },
118
119         onDone: function()
120         {
121                 // check for previous tinyMCE registration, and try to remove it gracefully first
122                 var prev = tinyMCE.get(this.ID);
123                 if (prev)
124                 try
125                 {
126                         tinyMCE.execCommand('mceFocus', false, this.ID); 
127                         // when removed, tinyMCE restores its content to the textarea. If the textarea content has been
128                         // updated in this same callback, it will be overwritten with the old content. Workaround this.
129                         var curtext = $(this.ID).value;
130                         tinyMCE.execCommand('mceRemoveControl', false, this.ID);
131                         $(this.ID).value = curtext;
132                 }
133                 catch (e) 
134                 {
135                         // suppress error here in case editor can't be properly removed
136                         // (happens when <textarea> has been removed from DOM tree without deinitialzing the tinyMCE editor first)
137                 }
138
139                 // doublecheck editor instance here and remove manually from tinyMCE-registry if neccessary
140                 this.removePreviousInstance();
141
142                 this.deRegisterAjaxHook();
143         }
144 });
145
146 Object.extend(Prado.WebUI.THtmlArea, 
147 {
148         pendingRegistrations : [],
149         tinyMCELoadState : 0
150 });
151
152