1 | //******************************************************** |
---|
2 | // Class LoginButton |
---|
3 | // Display a button to login or logout |
---|
4 | // |
---|
5 | // use jQuery && librairies from TwitterLogin |
---|
6 | //******************************************************** |
---|
7 | |
---|
8 | var LoginButton = Class.create( { |
---|
9 | |
---|
10 | initialize: function( param ) |
---|
11 | { |
---|
12 | // Init |
---|
13 | this.parent = param.parent ? param.parent : document.body; |
---|
14 | this.onClickLogin = param.anotherOnClickLogin ? param.anotherOnClickLogin : this.onClickLogin; |
---|
15 | this.onClickLogout = param.anotherOnClickLogout ? param.anotherOnClickLogout : this.onClickLogout; |
---|
16 | this.jSONUser = param.jSONUser ? param.jSONUser : false; |
---|
17 | this.urlLogin = param.urlLogin ? param.urlLogin : false; |
---|
18 | this.urlLogout = param.urlLogout ? param.urlLogout : false; |
---|
19 | |
---|
20 | // Texts |
---|
21 | this.connexionText = param.connexionText ? param.connexionText : loginTexts["app.connexion"]; |
---|
22 | this.loginText = param.loginText ? param.loginText : loginTexts["data.authentification.login"]; |
---|
23 | this.pwdText = param.pwdText ? param.pwdText : loginTexts["data.authentification.pwd"]; |
---|
24 | this.submitText = param.submitText ? param.submitText : loginTexts["data.authentification.sign"]; |
---|
25 | this.forgotPText = param.forgotPText ? param.forgotPText : loginTexts["data.authentification.pwdForget"]; |
---|
26 | this.forgotUNPHelpText = param.forgotUNPHelpText ? param.forgotUNPHelpText : loginTexts["data.authentification.loginForget.help"]; |
---|
27 | this.forgotUNPText = param.forgotUNPText ? param.forgotUNPText : loginTexts["data.authentification.loginForget"]; |
---|
28 | this.text1 = param.text1 ? param.text1 : loginTexts["app.admin"]; |
---|
29 | this.errorText = param.errorText ? param.errorText : loginTexts[this.errors]; |
---|
30 | }, |
---|
31 | |
---|
32 | // GETTER / SETTER ******************************************************** |
---|
33 | setJSONUser: function( jSONUser ) |
---|
34 | { |
---|
35 | this.jSONUser = jSONUser; |
---|
36 | }, |
---|
37 | |
---|
38 | getJSONUser: function() |
---|
39 | { |
---|
40 | return this.jSONUser; |
---|
41 | }, |
---|
42 | |
---|
43 | // ACTIONS ******************************************************** |
---|
44 | createLoginForm: function() |
---|
45 | { |
---|
46 | var containerLoginForm = $( document.createElement( "fieldset" ) ); |
---|
47 | containerLoginForm.attr( {id:"signin_menu"} ); |
---|
48 | this.parent.append( containerLoginForm ); |
---|
49 | |
---|
50 | var labelLogin = $( document.createElement( "label" ) ); |
---|
51 | labelLogin.attr( {for:"login"} ); |
---|
52 | labelLogin.append( this.loginText ); |
---|
53 | var inputLogin = $( document.createElement( "input" ) ); |
---|
54 | inputLogin.attr( {id:"login", name:"login", tabIndex:"4", type:"text"} ); |
---|
55 | containerLoginForm.append( labelLogin ); |
---|
56 | containerLoginForm.append( inputLogin ); |
---|
57 | |
---|
58 | var labelPwd = $( document.createElement( "label" ) ); |
---|
59 | labelPwd.attr( {for:"pwd"} ); |
---|
60 | labelPwd.append( this.pwdText ); |
---|
61 | var inputPwd = $( document.createElement( "input" ) ); |
---|
62 | inputPwd.attr( {id:"pwd", name:"pwd", tabIndex:"5", type:"password"} ); |
---|
63 | containerLoginForm.append( labelPwd ); |
---|
64 | containerLoginForm.append( inputPwd ); |
---|
65 | |
---|
66 | var inputSubmitP = $( document.createElement( "p" ) ); |
---|
67 | inputSubmitP.addClass( "remember" ); |
---|
68 | var inputSubmit = $( document.createElement( "input" ) ); |
---|
69 | inputSubmit.attr( {id:"signin_submit", value:this.submitText, tabIndex:"6", type:"submit"} ); |
---|
70 | inputSubmit.bind( "click", jQuery.proxy( this.onClickLogin, this ) ); |
---|
71 | inputSubmitP.append( inputSubmit ); |
---|
72 | containerLoginForm.append( inputSubmitP ); |
---|
73 | |
---|
74 | var inputForgotP = $( document.createElement( "p" ) ); |
---|
75 | inputForgotP.addClass( "forgot" ); |
---|
76 | var aForgotP = $( document.createElement( "a" ) ); |
---|
77 | aForgotP.attr( {id:"resend_password_link", href:"mailto:${webmaster}?subject=[MEGAPOLI] login"} ); |
---|
78 | aForgotP.append( this.forgotPText ); |
---|
79 | inputForgotP.append( aForgotP ); |
---|
80 | containerLoginForm.append( inputForgotP ); |
---|
81 | |
---|
82 | var inputForgotUNP = $( document.createElement( "p" ) ); |
---|
83 | inputForgotUNP.addClass( "username" ); |
---|
84 | var aForgotUNP = $( document.createElement( "a" ) ); |
---|
85 | aForgotUNP.attr( {id:"forgot_username_link", href:"mailto:${webmaster}?subject=[MEGAPOLI] password", title:this.forgotUNPHelpText} ); |
---|
86 | aForgotUNP.append( this.forgotUNPText ); |
---|
87 | inputForgotUNP.append( aForgotUNP ); |
---|
88 | containerLoginForm.append( inputForgotUNP ); |
---|
89 | |
---|
90 | var divErrors = $( document.createElement( "div" ) ); |
---|
91 | divErrors.attr( {id:"errors", class:"containerErrors"} ); |
---|
92 | containerLoginForm.append( divErrors ); |
---|
93 | this.containerErrors = $( "#errors" ); |
---|
94 | }, |
---|
95 | |
---|
96 | display: function() |
---|
97 | { |
---|
98 | if( this.jSONUser ) |
---|
99 | { |
---|
100 | var textLogin = this.jSONUser.name + " " + this.jSONUser.firstName; |
---|
101 | if( this.jSONUser.role && "coordinateur" == this.jSONUser.role ) |
---|
102 | textLogin += " (" + this.text1 + ")"; |
---|
103 | |
---|
104 | var loginButton = new ComplexButton( {value:textLogin, parent:this.parent, id:"button_login"} ); |
---|
105 | |
---|
106 | $( "#button_login #button_middle #button_text" ).addClass( "containerUserLogout" ); |
---|
107 | |
---|
108 | var loginMiddle = $( "#button_login #button_middle" ); |
---|
109 | var divImgLogout = $( document.createElement( "div" ) ); |
---|
110 | divImgLogout.addClass( "containerImgLogout" ); |
---|
111 | divImgLogout.append( "<img src='resources/images/utils/logout.png' width='15px' height='15px/>" ); |
---|
112 | divImgLogout.bind( "click", jQuery.proxy( this.onClickLogout, this ) ); |
---|
113 | loginMiddle.append( divImgLogout ); |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | this.createLoginForm(); |
---|
118 | |
---|
119 | var loginButton = new ComplexButton( {value:this.connexionText, parent:this.parent, id:"button_login"} ); |
---|
120 | |
---|
121 | var loginMiddle = $( "#button_login #button_middle" ); |
---|
122 | loginMiddle.empty(); |
---|
123 | loginMiddle.attr( {id:"topnav", class:"topnav complexButton_middle"} ); |
---|
124 | var aLogin = $( document.createElement( "a" ) ); |
---|
125 | aLogin.attr( {id:"button_login_a", href:"login", class:"signin"} ); |
---|
126 | aLogin.html( "<span>" + this.connexionText + "</span>" ); |
---|
127 | loginMiddle.append( aLogin ); |
---|
128 | |
---|
129 | $( ".signin" ).click( function( e ) |
---|
130 | { |
---|
131 | e.preventDefault(); |
---|
132 | $( "fieldset#signin_menu" ).toggle(); |
---|
133 | $( ".signin" ).toggleClass( "menu-open" ); |
---|
134 | } ); |
---|
135 | |
---|
136 | $( "fieldset#signin_menu" ).mouseup( function() |
---|
137 | { |
---|
138 | return false |
---|
139 | } ); |
---|
140 | |
---|
141 | $( document ).mouseup( function( e ) |
---|
142 | { |
---|
143 | if( 0 == $( e.target ).parent( "a.signin" ).length ) |
---|
144 | { |
---|
145 | $( ".signin" ).removeClass( "menu-open" ); |
---|
146 | $( "fieldset#signin_menu" ).hide(); |
---|
147 | } |
---|
148 | } ); |
---|
149 | } |
---|
150 | }, |
---|
151 | |
---|
152 | updateLogin: function() |
---|
153 | { |
---|
154 | this.parent.empty(); |
---|
155 | this.display(); |
---|
156 | }, |
---|
157 | |
---|
158 | displayLoginErrors: function ( text ) |
---|
159 | { |
---|
160 | $( "#errors" ).show(); |
---|
161 | $( "#errors" ).html( text ); |
---|
162 | }, |
---|
163 | |
---|
164 | // TODO : vérifier cette méthode |
---|
165 | showLogin: function( text ) |
---|
166 | { |
---|
167 | if( $( "fieldset#signin_menu" ) && $( ".signin" ) ) |
---|
168 | { |
---|
169 | $( "fieldset#signin_menu" ).toggle(); |
---|
170 | $( ".signin" ).toggleClass( "menu-open" ); |
---|
171 | if( text ) |
---|
172 | this.displayLoginErrors( text ); |
---|
173 | } |
---|
174 | else |
---|
175 | alert( this.displayLoginErrors( text ) ); |
---|
176 | }, |
---|
177 | |
---|
178 | // REQUESTS ******************************************************** |
---|
179 | requestLogin: function() |
---|
180 | { |
---|
181 | if( !this.urlLogin ) |
---|
182 | return; |
---|
183 | |
---|
184 | // TODO : passer en crypter ces infos !!!!! |
---|
185 | var login = $( "#login" ).val(); |
---|
186 | var pwd = $( "#pwd" ).val(); |
---|
187 | $.ajax( { |
---|
188 | url: this.urlLogin + "&login=" + login + "&pwd=" + pwd, |
---|
189 | success:jQuery.proxy( this.onHandleLogin, this ) |
---|
190 | } ); |
---|
191 | }, |
---|
192 | |
---|
193 | requestLogout: function() |
---|
194 | { |
---|
195 | if( !this.urlLogout ) |
---|
196 | return; |
---|
197 | |
---|
198 | $.ajax( { |
---|
199 | url: this.urlLogout, |
---|
200 | success:jQuery.proxy( this.onHandleLogout, this ) |
---|
201 | } ); |
---|
202 | }, |
---|
203 | |
---|
204 | // HANDLES ******************************************************** |
---|
205 | onHandleLogin: function( result ) |
---|
206 | { |
---|
207 | this.errors = jQuery.parseJSON( result ).errors; |
---|
208 | this.setJSONUser( null != jQuery.parseJSON( result ).jSONPeople ? jQuery.parseJSON( result ).jSONPeople : false ); |
---|
209 | if( null == this.errors ) |
---|
210 | this.updateLogin(); |
---|
211 | else |
---|
212 | this.displayLoginErrors( loginTexts[this.errors] ); |
---|
213 | }, |
---|
214 | |
---|
215 | onHandleLogout: function( result ) |
---|
216 | { |
---|
217 | this.errors = jQuery.parseJSON( result ).errors; |
---|
218 | if( null == this.errors ) |
---|
219 | { |
---|
220 | this.setJSONUser( false ); |
---|
221 | this.updateLogin(); |
---|
222 | } |
---|
223 | else |
---|
224 | this.displayLoginErrors( loginTexts[this.errors] ); |
---|
225 | }, |
---|
226 | |
---|
227 | // EVENTS ******************************************************** |
---|
228 | onClickLogin: function() |
---|
229 | { |
---|
230 | this.containerErrors.hide(); |
---|
231 | this.requestLogin(); |
---|
232 | }, |
---|
233 | |
---|
234 | onClickLogout: function() |
---|
235 | { |
---|
236 | this.requestLogout(); |
---|
237 | } |
---|
238 | |
---|
239 | } ); |
---|
240 | |
---|