8e8e61fbc13597453edd533c38510054da39f42d
[ldapsaisie.git] / public_html / includes / js / LSdefault.js
1 var LSdefault = new Class({
2     initialize: function(){
3       // LSdebug
4       this.LSdebug = new LSinfosBox({
5         name: 'LSdebug',
6         fxDuration: 600,
7         closeBtn: 1,
8         autoClose: 0
9       });
10       this.LSdebugInfos = $('LSdebug_txt');
11       
12       // LSerror
13       this.LSerror = new LSinfosBox({
14         name: 'LSerror',
15         opacity: 0.9,
16         autoClose: 10000
17       });
18       this.LSerror_div = $('LSerror_txt');
19       
20       // LSinfos
21       this.LSinfos = new LSinfosBox({name: 'LSinfos'});
22       this.LSinfos_div = $('LSinfos_txt');
23
24       // LSjsConfig
25       this.LSjsConfigEl = $('LSjsConfig');
26       if ($type(this.LSjsConfigEl)) {
27         this.LSjsConfig = JSON.decode(this.LSjsConfigEl.innerHTML);
28       }
29       else {
30         this.LSjsConfig = [];
31       }
32
33       this.loading_img=[];
34       this.loading_img_id=-1;
35
36       // LSsession_topDn      
37       this.LSsession_topDn = $('LSsession_topDn');
38       if (this.LSsession_topDn) {
39         this.LSsession_topDn.addEvent('change',this.onLSsession_topDnChange.bind(this));
40       }
41       
42       // Display Infos
43       if (this.LSdebugInfos.innerHTML != '') {
44         this.LSdebug.display(this.LSdebugInfos.innerHTML);
45       }
46       
47       if (this.LSerror_div.innerHTML != '') {
48         this.LSerror.display(this.LSerror_div.innerHTML);
49       }
50       
51       if (this.LSinfos_div.innerHTML != '') {
52         this.LSinfos.display(this.LSinfos_div.innerHTML);
53       }
54       
55       // :)
56       var getMoo = /moo$/;
57       if (getMoo.exec(window.location)) {
58         this.moo();
59       }
60       document.addEvent('keyup',this.onWantMoo.bindWithEvent(this));
61       
62       this.LStips = new Tips('.LStips');
63       
64       if ($type(this.LSjsConfig['keepLSsessionActive'])) {
65         this.LSjsConfig['keepLSsessionActive'] = (Math.round(this.LSjsConfig['keepLSsessionActive']*0.70)*1000);
66         this.keepLSsession.delay(this.LSjsConfig['keepLSsessionActive'],this);
67       }
68       
69       this.initializeLang();
70     },
71     
72     initializeLang: function() {
73       this.LSlang = $('LSlang');
74       if ($type(this.LSlang)) {
75         this.LSlang_select = $('LSlang_select');
76         if (this.LSlang_select) {
77           this.LSlang_open=0;
78           window.addEvent('click',this.closeLSlang.bind(this));
79           this.LSlang.addEvent('click',this.onLSlangClick.bind(this));
80           this.LSlang_select.getElements('img').each(function(img) {
81             img.addEvent('click',this.onSelectLSlangImgClick.bind(this,img));
82           },this);
83           document.getElements('.LSlang_hidden').each(function(el) {
84             el.dispose();
85           },this);
86         }
87       }
88     },
89     
90     onLSlangClick: function() {
91       LSdebug(this.LSlang_select);
92       var infos = this.LSlang.getCoordinates();
93       this.LSlang_select.setStyle('top',infos.bottom);
94       this.LSlang_select.setStyle('left',infos.right);
95       this.LSlang_select.setStyle('display','block');
96       this.LSlang_open=1;
97     },
98     
99     closeLSlang: function(event) {
100       event = new Event(event);
101       if (event.target.id!='LSlang') {
102         this.LSlang_select.setStyle('display','none');
103         this.LSlang_open = 0;
104       }
105     },
106     
107     onSelectLSlangImgClick: function(img) {
108       window.location='index.php?lang='+img.alt;
109     },
110
111     onWantMoo: function(event) {
112       event=new Event(event);
113       if ((event.shift) && (event.key=='m')) {
114         this.moo.run(null,this);
115       }
116     },
117
118     moo: function() {
119       var mooTxt = "<pre>         (__)     .ooooooooooooooooooo.\n         (oo) °°°°0 I love LdapSaisie 0\n   /------\\\/      °ooooooooooooooooooo°\n  / |    ||\n *  /\---/\\\n    ~~   ~~</pre>";
120       this.LSinfos.displayOrAdd(mooTxt);
121     },
122
123     onLSsession_topDnChange: function() {
124       $('LSsession_topDn_form').submit();
125     },
126
127     checkAjaxReturn: function(data) {
128       this.LSerror.close(0);
129       if ($type(data) == 'object') {
130         if (($type(data.LSredirect)) && (!$type(data.LSdebug)) ) {
131           document.location = data.LSredirect;
132           return true;
133         }
134         
135         if ($type(data.imgload)) {
136           this.loadingImgHide(data.imgload);
137         }
138         else {
139           this.loadingImgHide();
140         }
141         
142         if ($type(data.LSdebug)) {
143           this.LSdebug.displayOrAdd(data.LSdebug);
144         }
145         
146         if ($type(data.LSinfos)) {
147           this.LSinfos.displayOrAdd(data.LSinfos);
148         }
149         
150         if ($type(data.LSerror)) {
151           this.LSerror.displayOrAdd(data.LSerror);
152           return;
153         }
154         return true;
155       }
156       else {
157         LSdebug('Non computable return value');
158         this.loadingImgHide();
159         return;
160       }
161     },
162
163     loadingImgDisplay: function(el,position,size) {
164       this.loading_img_id++;
165       this.loading_img[this.loading_img_id] = new Element('img');
166       if (size=='big') {
167         var src = this.imagePath('loading.gif');
168       }
169       else {
170         var src = this.imagePath('ajax-loader.gif');
171       }
172       this.loading_img[this.loading_img_id].src=src;
173       if (position=='inside') {
174         this.loading_img[this.loading_img_id].injectInside(el);
175       }
176       else {
177         this.loading_img[this.loading_img_id].injectAfter(el);
178       }
179       return this.loading_img_id;
180     },
181
182     loadingImgHide: function(id) {
183       if (isNaN(id)) {
184         this.loading_img.each(function(el)  {
185           if ($type(el))
186             el.destroy();
187         },this);
188         this.loading_img_id=-1;
189       }
190       else {
191         this.loading_img[id].destroy();
192       }
193     },
194     
195     ajaxDisplayDebugAndError: function() {
196       var LSdebug_txt = $('LSdebug_txt_ajax');
197       if (LSdebug_txt) {
198         var debug = LSdebug_txt.innerHTML;
199         if (debug) {
200           this.LSdebug.displayOrAdd(debug);
201         }
202       }
203       
204       var LSerror_txt = $('LSerror_txt_ajax');
205       if (LSerror_txt) {
206         var error=LSerror_txt.innerHTML;
207         if (error) {
208           this.LSerror.displayOrAdd(error);
209         }
210       }
211     },
212     
213     imagePath: function(image) {
214       return this.LSjsConfig['LS_IMAGES_DIR'] + '/' + image;
215     },
216     
217     getParams: function(name) {
218       if ($type(this.LSjsConfig[name])) {
219         return this.LSjsConfig[name];
220       }
221       return new Hash();
222     },
223     
224     addHelpInfo: function(el,group,name) {
225       if ($type(this.LSjsConfig['helpInfos'])) {
226         if ($type(el)=='element') {
227           if ($type(this.LSjsConfig['helpInfos'][group])) {
228             if ($type(this.LSjsConfig['helpInfos'][group][name])) {
229               this.addTip(el);
230               el.store('tip:title',this.LSjsConfig['helpInfos'][group][name]);
231               el.store('tip:text',"");
232             }
233           }
234         }
235       }
236     },
237     
238     setHelpInfo: function(el,group,name) {
239       if ($type(this.LSjsConfig['helpInfos'])) {
240         if ($type(el)=='element') {
241           if ($type(this.LSjsConfig['helpInfos'][group])) {
242             if ($type(this.LSjsConfig['helpInfos'][group][name])) {
243               el.store('tip:title',this.LSjsConfig['helpInfos'][group][name]);
244               el.store('tip:text',"");
245             }
246           }
247         }
248       }
249     },
250     
251     addTip: function(el) {
252       this.LStips.attach(el);
253     },
254     
255     removeTip: function(el) {
256       this.LStips.detach(el);
257     },
258     
259     keepLSsession: function() {
260       LSdebug('Keep LSsession');
261       new Request({url: 'index_ajax.php', data: {}, onSuccess: this.keepLSsessionComplete.bind(this)}).send();
262     },
263     
264     keepLSsessionComplete: function() {
265       LSdebug('Keep LSsession OK');
266       this.keepLSsession.delay(this.LSjsConfig['keepLSsessionActive'],this);
267     },
268     
269     log: function(data) {
270       this.LSdebug.addInfo(data);
271     }
272
273 });
274
275 window.addEvent(window.ie ? 'load' : 'domready', function() {
276   varLSdefault = new LSdefault();
277 });