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