LSform : preserve current tab between view, modify and create pages
[ldapsaisie.git] / public_html / includes / js / LSform.js
1 var LSform = new Class({
2     initialize: function(){
3       this._modules=[];
4       this._fields=[];
5       this._elements=[];
6       this._tabBtns=[];
7
8       if ($type($('LSform_idform'))) {
9         this.objecttype = $('LSform_objecttype').value;
10         this.objectdn = $('LSform_objectdn').value;
11         this.idform = $('LSform_idform').value;
12       }
13
14       this.initializeLSform();
15       this.initializeLSformLayout();
16     },
17
18     initializeLSform: function(el) {
19       this.params={};
20       if (this.idform) {
21         if (typeof(el) == 'undefined') {
22           el = document;
23         }
24         el.getElements('ul.LSform').each(function(ul) {
25           this._elements[ul.id] = new LSformElement(this,ul.id,ul);
26         }, this);
27         this.params=varLSdefault.LSjsConfig['LSform_'+this.idform];
28         if (!$type(this.params)) {
29           this.params={};
30         }
31         this._ajaxSubmit=this.params.ajaxSubmit;
32
33         this.warnBox = new LSinfosBox({
34           name: 'LSformWarnBox',
35           fxDuration: 600,
36           closeBtn: 1,
37           autoClose: 0
38         });
39
40         if ($type(this.params.warnings)) {
41           this.warnTxt = '<ul>';
42           this.params.warnings.each(function(w) {
43             this.warnTxt +='<li>'+w+'</li>';
44           },this);
45           this.warnTxt += '</ul>';
46           this.warnBox.display(this.warnTxt);
47         }
48         LSdebug(this.params);
49
50         this.listAvailableDataEntryForm=$('LSform_listAvailableDataEntryForm');
51         if ($type(this.listAvailableDataEntryForm)) {
52           this.listAvailableDataEntryForm.addEvent('change',this.onListAvailableDataEntryFormChange.bind(this));
53         }
54       }
55
56       LSforms = $$('form.LSform');
57       if ($type(LSforms[0])) {
58         this.LSform = LSforms[0];
59         this.LSform.addEvent('submit',this.ajaxSubmit.bindWithEvent(this));
60       }
61     },
62
63     initializeLSformLayout: function(el) {
64       $$('.LSform_layout').each(function(el) {
65         el.addClass('LSform_layout_active');
66       },this);
67
68       var LIs = $$('li.LSform_layout');
69       LIs.each(function(li) {
70         var Layout = this.getLayout(li);
71         if ($type(Layout)) {
72           if ($type(Layout.getElement('dt.LSform-errors'))) {
73             LSdebug('add');
74             li.addClass('LSform_layout_errors');
75           }
76           else {
77             if (!$type(Layout.getElement('dt'))) {
78               li.setStyle('display','none');
79             }
80           }
81         }
82         li.getFirst('a').addEvent('click',this.onTabBtnClick.bindWithEvent(this,li));
83       },this);
84
85       $$('li.LSform_layout a').each(function(a) {
86         this._tabBtns[a.href]=a;
87       },this);
88
89       if (LIs.length != 0) {
90         if ($type(this._tabBtns[window.location])) {
91           this._currentTab = 'default_value';
92           this._tabBtns[window.location].fireEvent('click');
93           byDefault=0;
94         }
95         else {
96           this._currentTab = 'default_value';
97           document.getElement('li.LSform_layout').getFirst('a').fireEvent('click');
98         }
99       }
100
101       var checkUrl = new RegExp('^(modify|create|view).php');
102       document.getElements('a.LSview-actions').each(function(a) {
103         if (checkUrl.exec(a.get('href'))) {
104           a.addEvent('click',this.onActionBtnClick.bindWithEvent(this,a));
105         }
106       },this);
107     },
108
109     onActionBtnClick: function(event,a) {
110       if (!location.hash) {
111         return true;
112       }
113       if ($type(event)) {
114         event = new Event(event);
115         event.stop();
116       }
117       var href=a.href;
118       var checkExistingHash=new RegExp('^([^#]+)#[^#]+$');
119       var cur = checkExistingHash.exec(href);
120       if (cur) {
121         href=cur[1]+location.hash;
122       }
123       else {
124         href=href+location.hash;
125       }
126       window.location=href;
127     },
128
129     getLayoutBtn: function(div) {
130       var getName = new RegExp('LSform_layout_div_(.*)');
131       var name = getName.exec(div.id);
132       if (!name) {
133         return;
134       }
135       return $('LSform_layout_btn_'+name[1]);
136     },
137
138     getLayoutNameFromBtn: function(btn) {
139       var getName = new RegExp('LSform_layout_btn_(.*)');
140       var name = getName.exec(btn.id);
141       if (!name) {
142         return;
143       }
144       return name[1];
145     },
146
147     getLayout: function(btn) {
148       var name = this.getLayoutNameFromBtn(btn);
149       if (!name) {
150         return;
151       }
152       return $('LSform_layout_div_'+name);
153     },
154
155     onTabBtnClick: function(event,li) {
156       if ($type(event)) {
157         event = new Event(event);
158         event.stop();
159         if ($type(event.target.blur)) {
160           event.target.blur();
161         }
162       }
163
164       if (this._currentTab!=li) {
165         if (this._currentTab!='default_value') {
166           this._currentTab.removeClass('LSform_layout_current');
167           var oldDiv = this.getLayout(this._currentTab);
168           if ($type(oldDiv)) {
169             oldDiv.removeClass('LSform_layout_current');
170           }
171         }
172
173         this._currentTab = li;
174         li.addClass('LSform_layout_current');
175         var a=li.getElement('a');
176         if ($type(a)) {
177           location.hash='#'+this.getLayoutNameFromBtn(li);
178         }
179         var div = this.getLayout(li);
180         if ($type(div)) {
181           div.addClass('LSform_layout_current');
182
183           // Focus
184           var ul = div.getElement('ul.LSform');
185           if ($type(ul)) {
186             var el = ul.getElement('input');
187             if (!$type(el)) {
188               el = ul.getElement('textarea');
189             }
190             if (!$type(el)) {
191               el = ul.getElement('select');
192             }
193             if ($type(el)) {
194               if(el.type!='hidden') {
195                 el.focus();
196               }
197             }
198           }
199         }
200       }
201
202     },
203
204     addModule: function(name,obj) {
205       this._modules[name]=obj;
206     },
207
208     initializeModule: function(fieldType,li) {
209       if ($type(this._modules[fieldType])) {
210         try {
211           this._modules[fieldType].reinitialize(li);
212         }
213         catch(e) {
214           LSdebug('No reinitialize for ' + fieldType);
215         }
216       }
217     },
218
219     addField: function(name,obj) {
220       this._fields[name]=obj;
221     },
222
223     clearFieldValue: function(name) {
224       if ($type(this._fields[name]) && $type(this._fields[name].clearValue)) {
225         this._fields[name].clearValue();
226       }
227     },
228
229     getValue: function(fieldName) {
230       var retVal = Array();
231       var inputs = this.getInput(fieldName);
232       inputs.each(function(el){
233         if (el.value!="") {
234           retVal.include(el.value);
235         }
236       },this);
237       return retVal;
238     },
239
240     getInput: function(fieldName) {
241       var retVal = Array();
242       var ul = $(fieldName);
243       if ($type(ul)) {
244         var elements = ul.getElements('input');
245         elements.combine(ul.getElements('textarea'));
246         elements.combine(ul.getElements('select'));
247
248         var getName = new RegExp('([a-zA-Z0-9]*)(\[.*\])?');
249         elements.each(function(el){
250           var name = getName.exec(el.name);
251           LSdebug(name);
252           if (name) {
253             if (name[1]==fieldName) {
254               retVal.include(el);
255             }
256           }
257         },this);
258       }
259       return retVal;
260     },
261
262     ajaxSubmit: function(event) {
263       this.checkUploadFileDefined();
264
265       if (this._ajaxSubmit) {
266         event = new Event(event);
267         event.stop();
268
269         this.LSformAjaxInput = new Element('input');
270         this.LSformAjaxInput.setProperties ({
271           type:   'hidden',
272           name:   'ajax',
273           value:  '1'
274         });
275         this.LSformAjaxInput.injectInside(this.LSform);
276
277         this.LSform.set('send',{
278           data:         this.LSform,
279           onSuccess:    this.onAjaxSubmitComplete.bind(this),
280           url:          this.LSform.get('action'),
281           imgload:      varLSdefault.loadingImgDisplay($('LSform_title'),'inside')
282         });
283         this.LSform.send();
284       }
285       else {
286         if($type(this.LSformAjaxInput)) {
287           this.LSformAjaxInput.dispose();
288         }
289       }
290     },
291
292     checkUploadFileDefined: function() {
293       this.LSform.getElements('input[type=file]').each(function(ipt) {
294         if (ipt.files.length!=0) {
295           this._ajaxSubmit=0;
296         }
297       }, this);
298     },
299
300     onAjaxSubmitComplete: function(responseText, responseXML) {
301       var data = JSON.decode(responseText);
302       if ( varLSdefault.checkAjaxReturn(data) ) {
303         this.resetErrors();
304         if ($type(data.LSformErrors) == 'object') {
305           data.LSformErrors = new Hash(data.LSformErrors);
306           data.LSformErrors.each(this.addError,this);
307         }
308       }
309     },
310
311     resetErrors: function() {
312       $$('dd.LSform-errors').each(function(dd) {
313         dd.destroy();
314       });
315       $$('dt.LSform-errors').each(function(dt) {
316         dt.removeClass('LSform-errors');
317       });
318       $$('li.LSform_layout_errors').each(function(li) {
319         li.removeClass('LSform_layout_errors');
320       });
321
322     },
323
324     addError: function(errors,name) {
325       var ul = $(name);
326       if ($type(ul)) {
327         errors = new Array(errors);
328         errors.each(function(txt){
329           var dd = new Element('dd');
330           dd.addClass('LSform');
331           dd.addClass('LSform-errors');
332           dd.set('html',txt);
333           dd.injectAfter(this.getParent());
334         },ul);
335
336         var dt = ul.getParent('dd.LSform').getPrevious('dt');
337         if ($type(dt)) {
338           dt.addClass('LSform-errors');
339         }
340
341         var layout = ul.getParent('div.LSform_layout_active');
342         if ($type(layout)) {
343           var li = this.getLayoutBtn(layout);
344           if($type(li)) {
345             li.addClass('LSform_layout_errors');
346           }
347         }
348       }
349       else {
350         this.tmp=name+" :</br><ul>";
351         errors = new Array(errors);
352         errors.each(function(error){
353           this.tmp += "<li>"+error+"</li>";
354         },this);
355         this.tmp +="</ul>";
356         this.warnBox.display(this.tmp);
357       }
358     },
359
360     onListAvailableDataEntryFormChange: function() {
361       var url=window.location.pathname+"?LSobject="+this.objecttype
362       if (this.listAvailableDataEntryForm.value!="") {
363         url+="&LSform_dataEntryForm="+this.listAvailableDataEntryForm.value;
364       }
365       document.location=url;
366     }
367 });
368 window.addEvent(window.ie ? 'load' : 'domready', function() {
369   varLSform = new LSform();
370 });