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