LSform/LSformElement : permit form element to provide custom clean value method
[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
102     getLayoutBtn: function(div) {
103       var getName = new RegExp('LSform_layout_div_(.*)');
104       var name = getName.exec(div.id);
105       if (!name) {
106         return;
107       }
108       return $('LSform_layout_btn_'+name[1]);
109     },
110
111     getLayout: function(btn) {
112       var getName = new RegExp('LSform_layout_btn_(.*)');
113       var name = getName.exec(btn.id);
114       if (!name) {
115         return;
116       }
117       return $('LSform_layout_div_'+name[1]);
118     },
119
120     onTabBtnClick: function(event,li) {
121       if ($type(event)) {
122         event = new Event(event);
123         event.stop();
124         if ($type(event.target.blur)) {
125           event.target.blur();
126         }
127       }
128
129       if (this._currentTab!=li) {
130         if (this._currentTab!='default_value') {
131           this._currentTab.removeClass('LSform_layout_current');
132           var oldDiv = this.getLayout(this._currentTab);
133           if ($type(oldDiv)) {
134             oldDiv.removeClass('LSform_layout_current');
135           }
136         }
137
138         this._currentTab = li;
139         li.addClass('LSform_layout_current');
140         var div = this.getLayout(li);
141         if ($type(div)) {
142           div.addClass('LSform_layout_current');
143
144           // Focus
145           var ul = div.getElement('ul.LSform');
146           if ($type(ul)) {
147             var el = ul.getElement('input');
148             if (!$type(el)) {
149               el = ul.getElement('textarea');
150             }
151             if (!$type(el)) {
152               el = ul.getElement('select');
153             }
154             if ($type(el)) {
155               if(el.type!='hidden') {
156                 el.focus();
157               }
158             }
159           }
160         }
161       }
162
163     },
164
165     addModule: function(name,obj) {
166       this._modules[name]=obj;
167     },
168
169     initializeModule: function(fieldType,li) {
170       if ($type(this._modules[fieldType])) {
171         try {
172           this._modules[fieldType].reinitialize(li);
173         }
174         catch(e) {
175           LSdebug('No reinitialize for ' + fieldType);
176         }
177       }
178     },
179
180     addField: function(name,obj) {
181       this._fields[name]=obj;
182     },
183
184     clearFieldValue: function(name) {
185       if ($type(this._fields[name]) && $type(this._fields[name].clearValue)) {
186         this._fields[name].clearValue();
187       }
188     },
189
190     getValue: function(fieldName) {
191       var retVal = Array();
192       var inputs = this.getInput(fieldName);
193       inputs.each(function(el){
194         if (el.value!="") {
195           retVal.include(el.value);
196         }
197       },this);
198       return retVal;
199     },
200
201     getInput: function(fieldName) {
202       var retVal = Array();
203       var ul = $(fieldName);
204       if ($type(ul)) {
205         var elements = ul.getElements('input');
206         elements.combine(ul.getElements('textarea'));
207         elements.combine(ul.getElements('select'));
208
209         var getName = new RegExp('([a-zA-Z0-9]*)(\[.*\])?');
210         elements.each(function(el){
211           var name = getName.exec(el.name);
212           LSdebug(name);
213           if (name) {
214             if (name[1]==fieldName) {
215               retVal.include(el);
216             }
217           }
218         },this);
219       }
220       return retVal;
221     },
222
223     ajaxSubmit: function(event) {
224       this.checkUploadFileDefined();
225
226       if (this._ajaxSubmit) {
227         event = new Event(event);
228         event.stop();
229
230         this.LSformAjaxInput = new Element('input');
231         this.LSformAjaxInput.setProperties ({
232           type:   'hidden',
233           name:   'ajax',
234           value:  '1'
235         });
236         this.LSformAjaxInput.injectInside(this.LSform);
237
238         this.LSform.set('send',{
239           data:         this.LSform,
240           onSuccess:    this.onAjaxSubmitComplete.bind(this),
241           url:          this.LSform.get('action'),
242           imgload:      varLSdefault.loadingImgDisplay($('LSform_title'),'inside')
243         });
244         this.LSform.send();
245       }
246       else {
247         if($type(this.LSformAjaxInput)) {
248           this.LSformAjaxInput.dispose();
249         }
250       }
251     },
252
253     checkUploadFileDefined: function() {
254       this.LSform.getElements('input[type=file]').each(function(ipt) {
255         if (ipt.files.length!=0) {
256           this._ajaxSubmit=0;
257         }
258       }, this);
259     },
260
261     onAjaxSubmitComplete: function(responseText, responseXML) {
262       var data = JSON.decode(responseText);
263       if ( varLSdefault.checkAjaxReturn(data) ) {
264         this.resetErrors();
265         if ($type(data.LSformErrors) == 'object') {
266           data.LSformErrors = new Hash(data.LSformErrors);
267           data.LSformErrors.each(this.addError,this);
268         }
269       }
270     },
271
272     resetErrors: function() {
273       $$('dd.LSform-errors').each(function(dd) {
274         dd.destroy();
275       });
276       $$('dt.LSform-errors').each(function(dt) {
277         dt.removeClass('LSform-errors');
278       });
279       $$('li.LSform_layout_errors').each(function(li) {
280         li.removeClass('LSform_layout_errors');
281       });
282
283     },
284
285     addError: function(errors,name) {
286       var ul = $(name);
287       if ($type(ul)) {
288         errors = new Array(errors);
289         errors.each(function(txt){
290           var dd = new Element('dd');
291           dd.addClass('LSform');
292           dd.addClass('LSform-errors');
293           dd.set('html',txt);
294           dd.injectAfter(this.getParent());
295         },ul);
296
297         var dt = ul.getParent('dd.LSform').getPrevious('dt');
298         if ($type(dt)) {
299           dt.addClass('LSform-errors');
300         }
301
302         var layout = ul.getParent('div.LSform_layout_active');
303         if ($type(layout)) {
304           var li = this.getLayoutBtn(layout);
305           if($type(li)) {
306             li.addClass('LSform_layout_errors');
307           }
308         }
309       }
310       else {
311         this.tmp=name+" :</br><ul>";
312         errors = new Array(errors);
313         errors.each(function(error){
314           this.tmp += "<li>"+error+"</li>";
315         },this);
316         this.tmp +="</ul>";
317         this.warnBox.display(this.tmp);
318       }
319     },
320
321     onListAvailableDataEntryFormChange: function() {
322       var url=window.location.pathname+"?LSobject="+this.objecttype
323       if (this.listAvailableDataEntryForm.value!="") {
324         url+="&LSform_dataEntryForm="+this.listAvailableDataEntryForm.value;
325       }
326       document.location=url;
327     }
328 });
329 window.addEvent(window.ie ? 'load' : 'domready', function() {
330   varLSform = new LSform();
331 });