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