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