667ccb087f95367e7766f1bedff0315864f0aa09
[ldapsaisie.git] / trunk / 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.control) && (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.addInfo(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           LSdebug(data.LSdebug);
144           this.LSdebug.display(data.LSdebug);
145         }
146         
147         if ($type(data.LSinfos)) {
148           this.LSinfos.display(data.LSinfos);
149         }
150         
151         if ($type(data.LSerror)) {
152           this.LSerror.display(data.LSerror);
153           return;
154         }
155         return true;
156       }
157       else {
158         LSdebug('Non computable return value');
159         this.loadingImgHide();
160         return;
161       }
162     },
163
164     loadingImgDisplay: function(el,position,size) {
165       this.loading_img_id++;
166       this.loading_img[this.loading_img_id] = new Element('img');
167       if (size=='big') {
168         var src = this.imagePath('loading.gif');
169       }
170       else {
171         var src = this.imagePath('ajax-loader.gif');
172       }
173       this.loading_img[this.loading_img_id].src=src;
174       if (position=='inside') {
175         this.loading_img[this.loading_img_id].injectInside(el);
176       }
177       else {
178         this.loading_img[this.loading_img_id].injectAfter(el);
179       }
180       return this.loading_img_id;
181     },
182
183     loadingImgHide: function(id) {
184       if (isNaN(id)) {
185         this.loading_img.each(function(el)  {
186           if ($type(el))
187             el.destroy();
188         },this);
189         this.loading_img_id=-1;
190       }
191       else {
192         this.loading_img[id].destroy();
193       }
194     },
195     
196     ajaxDisplayDebugAndError: function() {
197       var LSdebug_txt = $('LSdebug_txt');
198       if (LSdebug_txt) {
199         var debug = LSdebug_txt.innerHTML;
200         if (debug) {
201           this.LSdebug.display(debug.toString());
202         }
203       }
204       
205       var LSerror_txt = $('LSerror_txt');
206       if (LSerror_txt) {
207         var error=LSerror_txt.innerHTML;
208         if (error) {
209           this.LSerror.display(error.toString());
210         }
211       }
212     },
213     
214     imagePath: function(image) {
215       return this.LSjsConfig['LS_IMAGES_DIR'] + '/' + image;
216     },
217     
218     getParams: function(name) {
219       if ($type(this.LSjsConfig[name])) {
220         return this.LSjsConfig[name];
221       }
222       return new Hash();
223     },
224     
225     addHelpInfo: function(el,group,name) {
226       if ($type(this.LSjsConfig['helpInfos'])) {
227         if ($type(el)=='element') {
228           if ($type(this.LSjsConfig['helpInfos'][group])) {
229             if ($type(this.LSjsConfig['helpInfos'][group][name])) {
230               this.addTip(el);
231               el.store('tip:title',this.LSjsConfig['helpInfos'][group][name]);
232               el.store('tip:text',"");
233             }
234           }
235         }
236       }
237     },
238     
239     setHelpInfo: function(el,group,name) {
240       if ($type(this.LSjsConfig['helpInfos'])) {
241         if ($type(el)=='element') {
242           if ($type(this.LSjsConfig['helpInfos'][group])) {
243             if ($type(this.LSjsConfig['helpInfos'][group][name])) {
244               el.store('tip:title',this.LSjsConfig['helpInfos'][group][name]);
245               el.store('tip:text',"");
246             }
247           }
248         }
249       }
250     },
251     
252     addTip: function(el) {
253       this.LStips.attach(el);
254     },
255     
256     removeTip: function(el) {
257       this.LStips.detach(el);
258     },
259     
260     keepLSsession: function() {
261       LSdebug('Keep LSsession');
262       new Request({url: 'index_ajax.php', data: {}, onSuccess: this.keepLSsessionComplete.bind(this)}).send();
263     },
264     
265     keepLSsessionComplete: function() {
266       LSdebug('Keep LSsession OK');
267       this.keepLSsession.delay(this.LSjsConfig['keepLSsessionActive'],this);
268     }
269
270 });
271
272 window.addEvent(window.ie ? 'load' : 'domready', function() {
273   varLSdefault = new LSdefault();
274 });