source: ether_megapoli/trunk/web/resources/js/classesForJQuery/LoginButton.js @ 278

Last change on this file since 278 was 278, checked in by vmipsl, 13 years ago

Data protocole, inscription

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