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