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