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