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