59f46a5114b985168cd4d6cede9e21f2aa226bdf
[ldapsaisie.git] / public_html / includes / js / LSformElement_password_field.js
1 var LSformElement_password_field = new Class({
2     initialize: function(name,input){
3       this.name = name;
4       this.input = input;
5       this.params = varLSdefault.getParams(this.name);
6       this.initialiseLSformElement_password_field();
7     },
8     
9     initialiseLSformElement_password_field: function() {
10       // ViewHashBtn
11       if (this.params['viewHash'] && varLSform.objectdn!= "") {
12         this.viewHashBtn = new Element('img');
13         this.viewHashBtn.src = varLSdefault.imagePath('view_hash.png');
14         this.viewHashBtn.addClass('btn');
15         this.viewHashBtn.addEvent('click',this.onViewHashBtnClick.bind(this));
16         this.viewHashBtn.injectAfter(this.input);
17         varLSdefault.addHelpInfo(this.viewHashBtn,'LSformElement_password','viewHash');
18       }
19       
20       // Mail
21       if (this.params['mail']) {
22         if ((this.params.mail['canEdit']==1)||(!$type(this.params.mail['canEdit']))) {
23           this.editMailBtn = new Element('img');
24           this.editMailBtn.src = varLSdefault.imagePath('mail-edit.png');
25           this.editMailBtn.addClass('btn');
26           this.editMailBtn.addEvent('click',this.onEditMailBtnClick.bind(this));
27           this.LSmail_open = 0;
28           this.editMailBtn.injectAfter(this.input);
29           varLSdefault.addHelpInfo(this.editMailBtn,'LSformElement_password','editmail');
30         }
31         if (this.params.mail['ask']) {
32           this.mailBtn = new Element('img');
33           this.mailBtn.addClass('btn');
34           this.mailBtn.addEvent('click',this.onMailBtnClick.bind(this));
35           this.mailInput = new Element('input');
36           this.mailInput.setProperties({
37             name: 'LSformElement_password_' + this.name + '_send',
38             type: 'hidden'
39           });
40           if (this.params.mail['send']) {
41             this.mailInput.value = 1;
42             this.mailBtn.src = varLSdefault.imagePath('mail.png');
43             varLSdefault.addHelpInfo(this.mailBtn,'LSformElement_password','mail');
44           }  
45           else {
46             this.mailInput.value = 0;
47             this.mailBtn.src = varLSdefault.imagePath('nomail.png');
48             varLSdefault.addHelpInfo(this.mailBtn,'LSformElement_password','nomail');
49           }
50           this.mailBtn.injectAfter(this.input);
51           this.mailInput.injectAfter(this.mailBtn);
52         }
53       }
54       
55       // ViewBtn
56       this.viewBtn = new Element('img');
57       this.viewBtn.src = varLSdefault.imagePath('view.png');
58       this.viewBtn.addClass('btn');
59       this.viewBtn.addEvent('click',this.changeInputType.bind(this));
60       this.viewBtn.injectAfter(this.input);
61       varLSdefault.addHelpInfo(this.viewBtn,'LSformElement_password','view');
62       
63       // Verify
64       if (this.params['verify']) {
65         this.bgColor = this.input.getStyle('background-color');
66         this.verifyFx = new Fx.Tween(this.input,{property: 'background-color',duration:600});
67         this.verifyBtn = new Element('img');
68         this.verifyBtn.src = varLSdefault.imagePath('verify.png');
69         this.verifyBtn.addClass('btn');
70         this.verifyBtn.addEvent('click',this.onVerifyBtnClick.bind(this));
71         this.verifyBtn.injectAfter(this.input);
72         varLSdefault.addHelpInfo(this.verifyBtn,'LSformElement_password','verify');
73       }
74       
75       if (this.params['generate']) {
76         this.generateBtn = new Element('img');
77         this.generateBtn.src = varLSdefault.imagePath('generate.png');
78         this.generateBtn.addClass('btn');
79         this.generateBtn.addEvent('click',this.onGenerateBtnClick.bind(this));
80         this.generateBtn.injectAfter(this.input);
81         varLSdefault.addHelpInfo(this.generateBtn,'LSformElement_password','generate');
82       }
83       
84       this.initialize_input();
85     },
86     
87     initialize_input: function() {
88       // Verify
89       if (this.params['verify']) {
90         this.verifyFx = new Fx.Tween(this.input,{property: 'background-color',duration:600});
91       }
92     },
93     
94     onMailBtnClick: function() {
95       if (this.mailInput.value==0) {
96         this.mailInput.value = 1;
97         this.mailBtn.src = varLSdefault.imagePath('mail.png');
98         varLSdefault.setHelpInfo(this.mailBtn,'LSformElement_password','mail');
99       }
100       else {
101         this.mailInput.value = 0;
102         this.mailBtn.src = varLSdefault.imagePath('nomail.png');
103         varLSdefault.setHelpInfo(this.mailBtn,'LSformElement_password','nomail');
104       }
105     },
106     
107     onEditMailBtnClick: function(btn) {
108       if(!$type(this.LSmail)) {
109         this.LSmail = new LSmail();
110         this.LSmail.addEvent('close',this.onLSmailClose.bind(this));
111         this.LSmail.addEvent('valid',this.onLSmailValid.bind(this));
112       }
113       
114       var mails = []
115       if ($type(this.params.mail['mail_attr'])=='array') {
116         this.params.mail['mail_attr'].each(function(a) {
117           this.append(varLSform.getValue(a));
118         },mails);
119       }
120       else {
121         mails.append(varLSform.getValue(this.params.mail['mail_attr']));
122       }
123
124       this.LSmail_open = 1;
125       this.LSmail.setMails(mails);
126       this.LSmail.setSubject(this.params.mail['subject']);
127       this.LSmail.setMsg(this.params.mail['msg']);
128       this.LSmail.open(this.editMailBtn);
129     },
130     
131     onLSmailClose: function(LSmail) {
132       LSdebug('LSformElement_password : close LSmail');
133       this.LSmail_open = 0;
134     },
135     
136     onLSmailValid: function(LSmail) {
137       LSdebug('LSformElement_password : valid LSmail');
138       this.setMail(LSmail.getMail());
139     },
140     
141     setMail: function(mail) {
142       if ($type(mail)) {
143         if (!$type(this.msgInput)) {
144           this.msgInput = new Element('input');
145           this.msgInput.setProperties({
146             name: 'LSformElement_password_' + this.name + '_msg',
147             type: 'hidden'
148           });
149           this.msgInput.injectAfter(this.editMailBtn);
150         }
151         this.msgInput.value = JSON.encode(mail);
152       }
153     },
154     
155     onGenerateBtnClick: function() {
156       var data = {
157         template:   'LSformElement_password',
158         action:     'generatePassword',
159         attribute:  this.name,
160         objecttype: varLSform.objecttype,
161         objectdn:   varLSform.objectdn,
162         idform:     varLSform.idform
163       };
164       data.imgload=varLSdefault.loadingImgDisplay(this.generateBtn);
165       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onGenerateBtnClickComplete.bind(this)}).send();
166     },
167     
168     onGenerateBtnClickComplete: function(responseText, responseXML) {
169       var data = JSON.decode(responseText);
170       if ( varLSdefault.checkAjaxReturn(data) ) {
171         this.input.value=data.generatePassword;
172         this.changeInputType('view');
173       }
174     },
175
176     changeInputType: function(state) {
177       if (((this.input.type=='password')&&(state=='hide'))||((this.input.type=='text')&&(state=='view'))) {
178         return this.input;
179       }
180       if (this.input.type=='password') {
181         var newType = 'text';
182         this.viewBtn.src=varLSdefault.imagePath('hide.png');
183         varLSdefault.setHelpInfo(this.viewBtn,'LSformElement_password','hide');
184       }
185       else {
186         var newType = 'password';
187         this.viewBtn.src=varLSdefault.imagePath('view.png');
188         varLSdefault.setHelpInfo(this.viewBtn,'LSformElement_password','view');
189       }
190       var newInput = new Element('input');
191       newInput.setProperty('name',this.input.getProperty('name'));
192       newInput.setProperty('type',newType);
193       newInput.setProperty('class',this.input.getProperty('class'));
194       newInput.setProperty('value',this.input.getProperty('value'));
195       newInput.injectAfter(this.input);
196       this.input.destroy();
197       this.input = newInput;
198       this.initialize_input();
199       return newInput;
200     },
201     
202     onVerifyBtnClick: function() {
203       var data = {
204         template:   'LSformElement_password',
205         action:     'verifyPassword',
206         attribute:  this.name,
207         objecttype: varLSform.objecttype,
208         idform:     varLSform.idform,
209         objectdn:   varLSform.objectdn,
210         fieldValue: this.input.value
211       };
212       LSdebug(data);
213       data.imgload=varLSdefault.loadingImgDisplay(this.verifyBtn);
214       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onVerifyBtnClickComplete.bind(this)}).send();
215     },
216     
217     onVerifyBtnClickComplete: function(responseText, responseXML) {
218       var data = JSON.decode(responseText);
219       if ( varLSdefault.checkAjaxReturn(data) ) {
220         if (data.verifyPassword) {
221           // ok
222           this.verifyFx.start('#73F386');
223         }
224         else {
225           // nok
226           this.verifyFx.start('#f59a67');
227         }
228         (function(){this.verifyFx.start(this.bgColor);}).delay(1000, this);
229       }
230     },
231
232     onViewHashBtnClick: function() {
233       var data = {
234         template:   'LSformElement_password',
235         action:     'viewHash',
236         attribute:  this.name,
237         objecttype: varLSform.objecttype,
238         objectdn:   varLSform.objectdn
239       };
240       data.imgload=varLSdefault.loadingImgDisplay(this.viewHashBtn);
241       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onViewHashBtnClickComplete.bind(this)}).send();
242     },
243     
244     onViewHashBtnClickComplete: function(responseText, responseXML) {
245       var data = JSON.decode(responseText);
246       if ( varLSdefault.checkAjaxReturn(data) ) {
247         if (data.hash) {
248           // ok
249           this.input.value=data.hash;
250           this.changeInputType('view');
251         }
252       }
253     }
254 });