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