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