Ignore:
Timestamp:
11/24/11 17:32:33 (13 years ago)
Author:
vmipsl
Message:

loginModule

File:
1 copied

Legend:

Unmodified
Added
Removed
  • ether_megapoli/trunk/web/resources/js/classesForJQuery/LoginButton.js

    r238 r265  
    11//******************************************************** 
    2 // Class Button 
    3 // Display a button 
     2// Class LoginButton 
     3// Display a button to login or logout 
    44// 
    5 // use jQuery 
    6 // str value: Value to display in the button 
    7 // dom parent: in which dom element to draw the button 
    8 // int id: id of the dom element 
    9 // func onclick: callback function when button clicked 
    10 // str specificClass 
    11 // classNameToAdd : use to avoid button's style for button_left, button_right and button_middle when button.css is used with pagination.css 
    12 // useLonelyMidleImage : if true, we use image for parts left and right, otherwise, we only use an image for the middle 
     5// use jQuery && librairies from TwitterLogin 
    136//******************************************************** 
    147 
    15 var ComplexButton = Class.create( { 
     8var LoginButton = Class.create( { 
    169 
    1710    initialize: function( param ) 
    1811    { 
    1912        // Init 
    20         this.id = param.id ? param.id : ""; 
    21         this.parent = param.parent ? param.parent : false; 
    22         this.value = param.value ? param.value : ""; 
    23         this.specificClass = param.specificClass ? param.specificClass : false; 
    24         this.callbackOnClick = param.onClick ? param.onClick : false; 
    25         this.classNameToAdd = param.classNameToAdd ? param.classNameToAdd : false; 
    26         this.useLonelyMidleImage = param.useLonelyMidleImage ? param.useLonelyMidleImage : false; 
    27         this.boolDisabled = false; 
    28         this.boolSelected = false; 
    29         this.boolDisplayed = false; 
    30  
    31         // Create button elements 
    32         this.divContainer = $( document.createElement( "div" ) ); 
    33         this.divContainer.attr( {id:this.id, class:"complexButton"} ); 
    34         if( this.specificClass ) 
    35             this.divContainer.addClass( this.specificClass ); 
    36         if( this.parent ) 
    37             this.parent.append( this.divContainer ); 
    38  
    39         this.divLeft = $( document.createElement( "div" ) ); 
    40         if( this.classNameToAdd ) 
    41             this.divLeft.addClass( "complexButton_left_" + this.classNameToAdd ); 
    42         else if( !this.useLonelyMidleImage ) 
    43             this.divLeft.addClass( "complexButton_left" ); 
    44         this.divContainer.append( this.divLeft ); 
    45  
    46         this.divMiddle = $( document.createElement( "div" ) ); 
    47         this.divMiddle.attr( {id:"button_middle"} ); 
    48         if( this.classNameToAdd ) 
    49             this.divMiddle.addClass( "complexButton_middle_" + this.classNameToAdd ); 
    50         else if( this.useLonelyMidleImage ) 
    51             this.divMiddle.addClass( "complexButton_lonely_middle" ); 
    52         else 
    53             this.divMiddle.addClass( "complexButton_middle" ); 
    54         this.divContainer.append( this.divMiddle ); 
    55  
    56         this.divText = $( document.createElement( "div" ) ); 
    57         this.divText.attr( {id:"button_text"} ); 
    58         this.divText.addClass( "complexButton_text" ); 
    59         this.divText.html( this.value ); 
    60         this.divMiddle.append( this.divText ); 
    61  
    62         this.divRight = $( document.createElement( "div" ) ); 
    63         if( this.classNameToAdd ) 
    64             this.divRight.addClass( "complexButton_right_" + this.classNameToAdd ); 
    65         else if( !this.useLonelyMidleImage ) 
    66             this.divRight.addClass( "complexButton_right" ); 
    67         this.divContainer.append( this.divRight ); 
    68  
    69         // Define button events 
    70         this.divContainer.bind( 'click', this, this.onClick ); 
    71     }, 
    72  
    73     // Actions ******************************************************** 
    74  
    75     appendChild :function( parent ) 
    76     { 
    77         this.parent = parent; 
    78         this.parent.append( this.divContainer ); 
    79     }, 
    80  
    81     setDisabled:function( value ) 
    82     { 
    83         if( value ) 
    84             this.disable(); 
    85         else 
    86             this.enable(); 
    87     }, 
    88  
    89     disable : function() 
    90     { 
    91         this.boolDisabled = true; 
    92         this.divContainer.addClassName( "disable" ); 
    93     }, 
    94  
    95     enable : function() 
    96     { 
    97         this.boolDisabled = false; 
    98         this.divContainer.removeClassName( "disable" ); 
    99     }, 
    100  
    101     setSelected : function( value ) 
    102     { 
    103         this.boolSelected = value; 
    104         if( this.boolSelected ) 
    105             this.divContainer.addClassName( "selected" ); 
    106         else 
    107             this.divContainer.removeClassName( "selected" ); 
    108     }, 
    109  
    110     show : function() 
    111     { 
    112         this.boolDisplayed = true; 
    113         this.divContainer.style.display = ""; 
    114     }, 
    115  
    116     hide : function() 
    117     { 
    118         this.boolDisplayed = false; 
    119         this.divContainer.style.display = "none"; 
    120     }, 
    121  
    122     // Getter / Setter ******************************************************** 
    123  
    124     getContainer: function() 
    125     { 
    126         return this.divContainer; 
    127     }, 
    128  
    129     getDisable : function() 
    130     { 
    131         return this.boolDisabled; 
    132     }, 
    133  
    134     getSelect: function() 
    135     { 
    136         return this.boolSelected; 
    137     }, 
    138  
    139     setValue : function( value ) 
    140     { 
    141         this.value = value; 
    142         this.divText.html( this.value ); 
    143     }, 
    144  
    145     setCallbackOnClick : function( value ) 
    146     { 
    147         this.callbackOnClick = value; 
    148     }, 
    149  
    150     // Events ******************************************************** 
    151  
    152     onClick : function( event ) 
    153     { 
    154         var contextEvent = event.data; 
    155         if( !contextEvent.getDisable() && (undefined == event.detail || 1 == event.detail) && contextEvent.callbackOnClick ) 
    156             contextEvent.callbackOnClick(); 
     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(); 
    157237    } 
     238 
    158239} ); 
     240 
Note: See TracChangeset for help on using the changeset viewer.