LSformElement_password : Added a view hashed password button.
[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 mail = varLSform.getValue(this.params.mail['mail_attr']);
115
116       this.LSmail_open = 1;
117       this.LSmail.setMails(mail);
118       this.LSmail.setSubject(this.params.mail['subject']);
119       this.LSmail.setMsg(this.params.mail['msg']);
120       this.LSmail.open(this.editMailBtn);
121     },
122     
123     onLSmailClose: function(LSmail) {
124       LSdebug('LSformElement_password : close LSmail');
125       this.LSmail_open = 0;
126     },
127     
128     onLSmailValid: function(LSmail) {
129       LSdebug('LSformElement_password : valid LSmail');
130       this.setMail(LSmail.getMail());
131     },
132     
133     setMail: function(mail) {
134       if ($type(mail)) {
135         if (!$type(this.msgInput)) {
136           this.msgInput = new Element('input');
137           this.msgInput.setProperties({
138             name: 'LSformElement_password_' + this.name + '_msg',
139             type: 'hidden'
140           });
141           this.msgInput.injectAfter(this.editMailBtn);
142         }
143         this.msgInput.value = JSON.encode(mail);
144       }
145     },
146     
147     onGenerateBtnClick: function() {
148       var data = {
149         template:   'LSformElement_password',
150         action:     'generatePassword',
151         attribute:  this.name,
152         objecttype: varLSform.objecttype,
153         objectdn:   varLSform.objectdn,
154         idform:     varLSform.idform
155       };
156       data.imgload=varLSdefault.loadingImgDisplay(this.generateBtn);
157       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onGenerateBtnClickComplete.bind(this)}).send();
158     },
159     
160     onGenerateBtnClickComplete: function(responseText, responseXML) {
161       var data = JSON.decode(responseText);
162       if ( varLSdefault.checkAjaxReturn(data) ) {
163         this.input.value=data.generatePassword;
164         this.changeInputType('view');
165       }
166     },
167
168     changeInputType: function(state) {
169       if (((this.input.type=='password')&&(state=='hide'))||((this.input.type=='text')&&(state=='view'))) {
170         return this.input;
171       }
172       if (this.input.type=='password') {
173         var newType = 'text';
174         this.viewBtn.src=varLSdefault.imagePath('hide.png');
175         varLSdefault.setHelpInfo(this.viewBtn,'LSformElement_password','hide');
176       }
177       else {
178         var newType = 'password';
179         this.viewBtn.src=varLSdefault.imagePath('view.png');
180         varLSdefault.setHelpInfo(this.viewBtn,'LSformElement_password','view');
181       }
182       var newInput = new Element('input');
183       newInput.setProperty('name',this.input.getProperty('name'));
184       newInput.setProperty('type',newType);
185       newInput.setProperty('class',this.input.getProperty('class'));
186       newInput.setProperty('value',this.input.getProperty('value'));
187       newInput.injectAfter(this.input);
188       this.input.destroy();
189       this.input = newInput;
190       this.initialize_input();
191       return newInput;
192     },
193     
194     onVerifyBtnClick: function() {
195       var data = {
196         template:   'LSformElement_password',
197         action:     'verifyPassword',
198         attribute:  this.name,
199         objecttype: varLSform.objecttype,
200         idform:     varLSform.idform,
201         objectdn:   varLSform.objectdn,
202         fieldValue: this.input.value
203       };
204       LSdebug(data);
205       data.imgload=varLSdefault.loadingImgDisplay(this.verifyBtn);
206       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onVerifyBtnClickComplete.bind(this)}).send();
207     },
208     
209     onVerifyBtnClickComplete: function(responseText, responseXML) {
210       var data = JSON.decode(responseText);
211       if ( varLSdefault.checkAjaxReturn(data) ) {
212         if (data.verifyPassword) {
213           // ok
214           this.verifyFx.start('#73F386');
215         }
216         else {
217           // nok
218           this.verifyFx.start('#f59a67');
219         }
220         (function(){this.verifyFx.start(this.bgColor);}).delay(1000, this);
221       }
222     },
223
224     onViewHashBtnClick: function() {
225       var data = {
226         template:   'LSformElement_password',
227         action:     'viewHash',
228         attribute:  this.name,
229         objecttype: varLSform.objecttype,
230         objectdn:   varLSform.objectdn
231       };
232       data.imgload=varLSdefault.loadingImgDisplay(this.viewHashBtn);
233       new Request({url: 'index_ajax.php', data: data, onSuccess: this.onViewHashBtnClickComplete.bind(this)}).send();
234     },
235     
236     onViewHashBtnClickComplete: function(responseText, responseXML) {
237       var data = JSON.decode(responseText);
238       if ( varLSdefault.checkAjaxReturn(data) ) {
239         if (data.hash) {
240           // ok
241           this.input.value=data.hash;
242           this.changeInputType('view');
243         }
244       }
245     }
246 });