6b93d75ba9920cf91bb03b20d68f79579953b2e3
[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}),
25         LSerror:  new Fx.Tween(this.LSerror,{property: 'opacity',duration:500}),
26         LSinfos:  new Fx.Tween(this.LSinfos,{property: 'opacity',duration:500})
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
75     onWantMoo: function(event) {
76       event=new Event(event);
77       if ((event.control) && (event.shift) && (event.key=='m')) {
78         this.moo.run(null,this);
79       }
80     },
81
82     moo: function() {
83       var mooTxt = "         (__)     .ooooooooooooooooooo.\n         (oo) °°°°0 I love LdapSaisie 0\n   /------\\\/      °ooooooooooooooooooo°\n  / |    ||\n *  /\---/\\\n    ~~   ~~";
84       var ulMoo = this.LSinfos.getElement('ul'); 
85       var preMoo = new Element('pre');
86       preMoo.set('html',mooTxt);
87       if ($type(ulMoo)) {
88         ulMoo.empty();
89         var liMoo = new Element('li');
90         liMoo.injectInside(ulMoo);
91         preMoo.injectInside(liMoo);
92       }
93       else {
94         this.LSinfos.empty();
95         preMoo.injectInside(this.LSinfos);
96       }
97       this.displayInfosBox();
98     },
99
100     onLSsession_topDnChange: function() {
101       $('LSsession_topDn_form').submit();
102     },
103
104     checkAjaxReturn: function(data) {
105       if ($type(data) == 'object') {
106         if (($type(data.LSredirect)) && (!$type(data.LSdebug)) ) {
107           document.location = data.LSredirect;
108           return true;
109         }
110         
111         if ($type(data.imgload)) {
112           this.loadingImgHide(data.imgload);
113         }
114         else {
115           this.loadingImgHide();
116         }
117         
118         if ($type(data.LSdebug)) {
119           LSdebug(data.LSdebug);
120           this.displayDebug(data.LSdebug);
121         }
122         
123         if ($type(data.LSinfos)) {
124           this.displayInfos(data.LSinfos);
125         }
126         
127         if ($type(data.LSerror)) {
128           this.displayError(data.LSerror);
129           return;
130         }
131         return true;
132       }
133       else {
134         LSdebug('Non computable return value');
135         this.loadingImgHide();
136         return;
137       }
138     },
139
140     /*
141      * Set and Display Methods
142      */
143     displayError: function(html) {
144       this.LSerror.set('html',html);
145       this.displayErrorBox();
146     },
147
148     displayDebug: function(html) {
149       this.LSdebugInfos.set('html',html);
150       this.displayDebugBox();
151     },
152     
153     displayInfos: function(html) {
154       this.LSinfos.set('html',html);
155       this.displayInfosBox();
156     },
157     
158     /*
159      * Display Methods
160      */
161     displayErrorBox: function() {
162       this.LSerror.setStyle('top',getScrollTop()+10);
163       if (this.LSerror_open) {
164         return true;
165       }
166       this.fx.LSerror.start(0,0.8);
167       this.LSerror_open = 1;
168     },
169     
170     displayDebugBox: function() {
171       this.LSdebug.setStyle('top',getScrollTop()+10);
172       if (this.LSdebug_open) {
173         return true;
174       }
175       this.fx.LSdebug.start(0,0.8);
176       this.LSdebug_open = 1;
177     },
178     
179     /*
180      * Hide Methods
181      */
182     hideLSdebug: function(){
183       if (this.LSdebug_open) {
184         this.fx.LSdebug.start(0.8,0);
185         this.LSdebug_open = 0;
186       }
187     },
188     
189     hideLSerror: function(){
190       if (this.LSerror_open) {
191         this.fx.LSerror.start(0.9,0);
192         this.LSerror_open = 0;
193       }
194     },
195
196     displayInfosBox: function() {
197       this.LSinfos.setStyle('top',getScrollTop()+10);
198       this.fx.LSinfos.start(0,0.9);
199       (function(){this.fx.LSinfos.start(0.9,0);}).delay(5000, this);
200     },
201
202     loadingImgDisplay: function(el,position,size) {
203       this.loading_img_id++;
204       this.loading_img[this.loading_img_id] = new Element('img');
205       if (size=='big') {
206         var src = this.imagePath('loading.gif');
207       }
208       else {
209         var src = this.imagePath('ajax-loader.gif');
210       }
211       this.loading_img[this.loading_img_id].src=src;
212       if (position=='inside') {
213         this.loading_img[this.loading_img_id].injectInside(el);
214       }
215       else {
216         this.loading_img[this.loading_img_id].injectAfter(el);
217       }
218       return this.loading_img_id;
219     },
220
221     loadingImgHide: function(id) {
222       if (isNaN(id)) {
223         this.loading_img.each(function(el)  {
224           if (typeof(el) != 'undefined')
225             el.destroy();
226         },this);
227         this.loading_img_id=-1;
228       }
229       else {
230         this.loading_img[id].destroy();
231       }
232     },
233     
234     ajaxDisplayDebugAndError: function() {
235       var LSdebug_txt = $('LSdebug_txt');
236       if (LSdebug_txt) {
237         var debug = LSdebug_txt.innerHTML;
238         if (debug) {
239           this.displayDebug(debug.toString());
240         }
241       }
242       
243       var LSerror_txt = $('LSerror_txt');
244       if (LSerror_txt) {
245         var error=LSerror_txt.innerHTML;
246         if (error) {
247           this.displayError(error.toString());
248         }
249       }
250     },
251     
252     imagePath: function(image) {
253       return this.LSjsConfig['LS_IMAGES_DIR'] + '/' + image;
254     },
255     
256     getParams: function(name) {
257       if ($type(this.LSjsConfig[name])) {
258         return this.LSjsConfig[name];
259       }
260       return new Hash();
261     },
262     
263     addHelpInfo: function(el,group,name) {
264       if ($type(this.LSjsConfig['helpInfos'])) {
265         if ($type(el)=='element') {
266           if ($type(this.LSjsConfig['helpInfos'][group])) {
267             if ($type(this.LSjsConfig['helpInfos'][group][name])) {
268               this.addTip(el);
269               el.store('tip:title',this.LSjsConfig['helpInfos'][group][name]);
270               el.store('tip:text',"");
271             }
272           }
273         }
274       }
275     },
276     
277     setHelpInfo: function(el,group,name) {
278       if ($type(this.LSjsConfig['helpInfos'])) {
279         if ($type(el)=='element') {
280           if ($type(this.LSjsConfig['helpInfos'][group])) {
281             if ($type(this.LSjsConfig['helpInfos'][group][name])) {
282               el.store('tip:title',this.LSjsConfig['helpInfos'][group][name]);
283               el.store('tip:text',"");
284             }
285           }
286         }
287       }
288     },
289     
290     addTip: function(el) {
291       this.LStips.attach(el);
292     },
293     
294     removeTip: function(el) {
295       this.LStips.detach(el);
296     },
297     
298     keepLSsession: function() {
299       LSdebug('Keep LSsession');
300       data: {}
301       new Request({url: 'index_ajax.php', data: {}, onSuccess: this.keepLSsessionComplete.bind(this)}).send();
302     },
303     
304     keepLSsessionComplete: function() {
305       LSdebug('Keep LSsession OK');
306       this.keepLSsession.delay(this.LSjsConfig['keepLSsessionActive'],this);
307     }
308
309 });
310
311 window.addEvent(window.ie ? 'load' : 'domready', function() {
312   varLSdefault = new LSdefault();
313 });