8c206dd0a2fa36635527fd487042e875d42d1bd3
[ldapsaisie.git] / trunk / includes / js / LSdefault.js
1 var LSdefault = new Class({
2     initialize: function(){
3       // LSdebug
4       this.LSdebug = $('LSdebug');
5       this.LSdebug.addEvent('dblclick',this.hideLSdebug.bind(this));
6       this.LSdebugInfos = $('LSdebug_infos');
7       this.LSdebug.setOpacity(0);
8       this.LSdebug_open = 0;
9
10       this.LSdebugHidden = $('LSdebug_hidden');
11       this.LSdebugHidden.addEvent('click',this.hideLSdebug.bind(this));
12       
13       // LSerror
14       this.LSerror = $('LSerror');
15       this.LSerror.setOpacity(0);
16       this.LSerror_open = 0;
17       this.LSerror.addEvent('dblclick',this.hideLSerror.bind(this));
18       
19       // LSinfos
20       this.LSinfos = $('LSinfos');
21
22       // FX
23       this.fx = {
24         LSdebug:  new Fx.Tween(this.LSdebug,{property: 'opacity',duration:600, fps:30}),
25         LSerror:  new Fx.Tween(this.LSerror,{property: 'opacity',duration:500, fps:30}),
26         LSinfos:  new Fx.Tween(this.LSinfos,{property: 'opacity',duration:500, fps:30})
27       };
28       
29       // LSjsConfig
30       this.LSjsConfigEl = $('LSjsConfig');
31       if ($type(this.LSjsConfigEl)) {
32         this.LSjsConfig = JSON.decode(this.LSjsConfigEl.innerHTML);
33       }
34       else {
35         this.LSjsConfig = [];
36       }
37
38       this.loading_img=[];
39       this.loading_img_id=-1;
40
41       // LSsession_topDn      
42       this.LSsession_topDn = $('LSsession_topDn');
43       if (this.LSsession_topDn) {
44         this.LSsession_topDn.addEvent('change',this.onLSsession_topDnChange.bind(this));
45       }
46       
47       // Display Infos
48       if (this.LSdebugInfos.innerHTML != '') {
49         this.displayDebugBox();
50       }
51       
52       if (this.LSerror.innerHTML != '') {
53         this.displayErrorBox();
54       }
55       
56       if (this.LSinfos.innerHTML != '') {
57         this.displayInfosBox();
58       }
59       
60       // :)
61       var getMoo = /moo$/;
62       if (getMoo.exec(window.location)) {
63         this.moo();
64       }
65       document.addEvent('keyup',this.onWantMoo.bindWithEvent(this));
66       
67       this.LStips = new Tips('.LStips');
68       
69       if ($type(this.LSjsConfig['keepLSsessionActive'])) {
70         this.LSjsConfig['keepLSsessionActive'] = (Math.round(this.LSjsConfig['keepLSsessionActive']*0.70)*1000);
71         this.keepLSsession.delay(this.LSjsConfig['keepLSsessionActive'],this);
72       }
73       
74       this.initializeLang();
75     },
76     
77     initializeLang: function() {
78       this.LSlang = $('LSlang');
79       if ($type(this.LSlang)) {
80         this.LSlang_select = $('LSlang_select');
81         if (this.LSlang_select) {
82           this.LSlang_open=0;
83           window.addEvent('click',this.closeLSlang.bind(this));
84           this.LSlang.addEvent('click',this.onLSlangClick.bind(this));
85           this.LSlang_select.getElements('img').each(function(img) {
86             img.addEvent('click',this.onSelectLSlangImgClick.bind(this,img));
87           },this);
88           document.getElements('.LSlang_hidden').each(function(el) {
89             el.dispose();
90           },this);
91         }
92       }
93     },
94     
95     onLSlangClick: function() {
96       LSdebug(this.LSlang_select);
97       var infos = this.LSlang.getCoordinates();
98       this.LSlang_select.setStyle('top',infos.bottom);
99       this.LSlang_select.setStyle('left',infos.right);
100       this.LSlang_select.setStyle('display','block');
101       this.LSlang_open=1;
102     },
103     
104     closeLSlang: function(event) {
105       event = new Event(event);
106       if (event.target.id!='LSlang') {
107         this.LSlang_select.setStyle('display','none');
108         this.LSlang_open = 0;
109       }
110     },
111     
112     onSelectLSlangImgClick: function(img) {
113       window.location='index.php?lang='+img.alt;
114     },
115
116     onWantMoo: function(event) {
117       event=new Event(event);
118       if ((event.control) && (event.shift) && (event.key=='m')) {
119         this.moo.run(null,this);
120       }
121     },
122
123     moo: function() {
124       var mooTxt = "         (__)     .ooooooooooooooooooo.\n         (oo) °°°°0 I love LdapSaisie 0\n   /------\\\/      °ooooooooooooooooooo°\n  / |    ||\n *  /\---/\\\n    ~~   ~~";
125       var ulMoo = this.LSinfos.getElement('ul'); 
126       var preMoo = new Element('pre');
127       preMoo.set('html',mooTxt);
128       if ($type(ulMoo)) {
129         ulMoo.empty();
130         var liMoo = new Element('li');
131         liMoo.injectInside(ulMoo);
132         preMoo.injectInside(liMoo);
133       }
134       else {
135         this.LSinfos.empty();
136         preMoo.injectInside(this.LSinfos);
137       }
138       this.displayInfosBox();
139     },
140
141     onLSsession_topDnChange: function() {
142       $('LSsession_topDn_form').submit();
143     },
144
145     checkAjaxReturn: function(data) {
146       this.hideLSerror();
147       if ($type(data) == 'object') {
148         if (($type(data.LSredirect)) && (!$type(data.LSdebug)) ) {
149           document.location = data.LSredirect;
150           return true;
151         }
152         
153         if ($type(data.imgload)) {
154           this.loadingImgHide(data.imgload);
155         }
156         else {
157           this.loadingImgHide();
158         }
159         
160         if ($type(data.LSdebug)) {
161           LSdebug(data.LSdebug);
162           this.displayDebug(data.LSdebug);
163         }
164         
165         if ($type(data.LSinfos)) {
166           this.displayInfos(data.LSinfos);
167         }
168         
169         if ($type(data.LSerror)) {
170           this.displayError(data.LSerror);
171           return;
172         }
173         return true;
174       }
175       else {
176         LSdebug('Non computable return value');
177         this.loadingImgHide();
178         return;
179       }
180     },
181
182     /*
183      * Set and Display Methods
184      */
185     displayError: function(html) {
186       this.LSerror.set('html',html);
187       this.displayErrorBox();
188     },
189
190     displayDebug: function(html) {
191       this.LSdebugInfos.set('html',html);
192       this.displayDebugBox();
193     },
194     
195     displayInfos: function(html) {
196       this.LSinfos.set('html',html);
197       this.displayInfosBox();
198     },
199     
200     /*
201      * Display Methods
202      */
203     displayErrorBox: function() {
204       this.LSerror.setStyle('top',getScrollTop()+10);
205       if (this.LSerror_open) {
206         return true;
207       }
208       this.fx.LSerror.start(0,0.8);
209       this.LSerror_open = 1;
210     },
211     
212     displayDebugBox: function() {
213       this.LSdebug.setStyle('top',getScrollTop()+10);
214       if (this.LSdebug_open) {
215         return true;
216       }
217       this.fx.LSdebug.start(0,0.8);
218       this.LSdebug_open = 1;
219     },
220     
221     /*
222      * Hide Methods
223      */
224     hideLSdebug: function(){
225       if (this.LSdebug_open) {
226         this.fx.LSdebug.start(0.8,0);
227         this.LSdebug_open = 0;
228       }
229     },
230     
231     hideLSerror: function(){
232       if (this.LSerror_open) {
233         this.fx.LSerror.start(0.9,0);
234         this.LSerror_open = 0;
235       }
236     },
237
238     displayInfosBox: function() {
239       this.LSinfos.setStyle('top',getScrollTop()+10);
240       this.fx.LSinfos.start(0,0.9);
241       (function(){this.fx.LSinfos.start(0.9,0);}).delay(5000, this);
242     },
243
244     loadingImgDisplay: function(el,position,size) {
245       this.loading_img_id++;
246       this.loading_img[this.loading_img_id] = new Element('img');
247       if (size=='big') {
248         var src = this.imagePath('loading.gif');
249       }
250       else {
251         var src = this.imagePath('ajax-loader.gif');
252       }
253       this.loading_img[this.loading_img_id].src=src;
254       if (position=='inside') {
255         this.loading_img[this.loading_img_id].injectInside(el);
256       }
257       else {
258         this.loading_img[this.loading_img_id].injectAfter(el);
259       }
260       return this.loading_img_id;
261     },
262
263     loadingImgHide: function(id) {
264       if (isNaN(id)) {
265         this.loading_img.each(function(el)  {
266           if (typeof(el) != 'undefined')
267             el.destroy();
268         },this);
269         this.loading_img_id=-1;
270       }
271       else {
272         this.loading_img[id].destroy();
273       }
274     },
275     
276     ajaxDisplayDebugAndError: function() {
277       var LSdebug_txt = $('LSdebug_txt');
278       if (LSdebug_txt) {
279         var debug = LSdebug_txt.innerHTML;
280         if (debug) {
281           this.displayDebug(debug.toString());
282         }
283       }
284       
285       var LSerror_txt = $('LSerror_txt');
286       if (LSerror_txt) {
287         var error=LSerror_txt.innerHTML;
288         if (error) {
289           this.displayError(error.toString());
290         }
291       }
292     },
293     
294     imagePath: function(image) {
295       return this.LSjsConfig['LS_IMAGES_DIR'] + '/' + image;
296     },
297     
298     getParams: function(name) {
299       if ($type(this.LSjsConfig[name])) {
300         return this.LSjsConfig[name];
301       }
302       return new Hash();
303     },
304     
305     addHelpInfo: function(el,group,name) {
306       if ($type(this.LSjsConfig['helpInfos'])) {
307         if ($type(el)=='element') {
308           if ($type(this.LSjsConfig['helpInfos'][group])) {
309             if ($type(this.LSjsConfig['helpInfos'][group][name])) {
310               this.addTip(el);
311               el.store('tip:title',this.LSjsConfig['helpInfos'][group][name]);
312               el.store('tip:text',"");
313             }
314           }
315         }
316       }
317     },
318     
319     setHelpInfo: function(el,group,name) {
320       if ($type(this.LSjsConfig['helpInfos'])) {
321         if ($type(el)=='element') {
322           if ($type(this.LSjsConfig['helpInfos'][group])) {
323             if ($type(this.LSjsConfig['helpInfos'][group][name])) {
324               el.store('tip:title',this.LSjsConfig['helpInfos'][group][name]);
325               el.store('tip:text',"");
326             }
327           }
328         }
329       }
330     },
331     
332     addTip: function(el) {
333       this.LStips.attach(el);
334     },
335     
336     removeTip: function(el) {
337       this.LStips.detach(el);
338     },
339     
340     keepLSsession: function() {
341       LSdebug('Keep LSsession');
342       new Request({url: 'index_ajax.php', data: {}, onSuccess: this.keepLSsessionComplete.bind(this)}).send();
343     },
344     
345     keepLSsessionComplete: function() {
346       LSdebug('Keep LSsession OK');
347       this.keepLSsession.delay(this.LSjsConfig['keepLSsessionActive'],this);
348     }
349
350 });
351
352 window.addEvent(window.ie ? 'load' : 'domready', function() {
353   varLSdefault = new LSdefault();
354 });