summaryrefslogtreecommitdiff
path: root/js/codeq/login.js
blob: 68cf14a5abcf269f5e6527d176bca57a92982b9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * Created by robert on 9/17/15.
 */

(function(){
    var jqScreen = $("#screen_login"),
        jqNavBarRight = $('.nav.navbar-nav.navbar-right'),
        jqNavigationHomeBtn = $('#navigation-home'),
        jqAAILoginBtn = $('#aai_login_button'),
        jqSignupBtn = $('#signup_button'),
        jqSubmitLoginBtn = $("#submit"),
        jqLangSelect = jqScreen.find('.lang-select'),
        jqDisabledOverlay = $('#disabled');

    //the loginCallbackFunction is used here and in the AAI login as well
    codeq.loginCallbackFunction = function (data) {
        jqDisabledOverlay.css('display', 'none');
        jqDisabledOverlay.css('cursor', '');
        if (data.code !== 0) throw new Error('Login failed, code: ' + data.code + ', message: ' + data.message);

        //nav signed in...
        $('#signed-in-title').html(data.name||data.username||data.email||'undefined');
        //merge with profile page
        $('#profileUsername').html(data.username||data.email||'undefined');
        $('#profileName').html(data.name||'undefined');
        $('#profileEmail').html(data.email||'undefined');
        $('#profileJoined').html(new Date(data.joined).toLocaleString());
        $('#profileLastLogin').html(new Date(data["last-login"]).toLocaleString());

        //merge these settings into the already existing default settings
        var sett = data.settings;
        $.extend(codeq.settings, sett);
        if('gui_lang' in sett && sett['gui_lang'] in codeq.supportedLangs){
            codeq.setLang(sett['gui_lang']);
            $("#gui_lang_select").val(sett['gui_lang']);
        }
        $('#robot_address_input').val(codeq.settings['robot_address'] || '');
        if('gui_layout' in sett && ($.inArray(sett['gui_layout'], codeq.supportedLayouts) >= 0) ){
            codeq.setLayout(sett['gui_layout']);
            $("#gui_layout_select").val(sett['gui_layout']);
        }

        codeq.globalStateMachine.transition('language');
    };

    var loginFun = function(){
            jqDisabledOverlay.css('display', '');
            jqDisabledOverlay.css('cursor', 'wait');
            codeq.comms.connect()
                .then(function () {
                    return codeq.comms.login($('#username').val(), $('#password').val());
                })
                .then(codeq.loginCallbackFunction)
                .fail(function (reason) {
                    jqDisabledOverlay.css('display', 'none');
                    jqDisabledOverlay.css('cursor', '');
                    codeq.log.error('Login failed: ' + reason);
                    alert('Login request failed: ' + reason);
                })
                .done();
        },
        formInputs = $('#username, #password');

    codeq.globalStateMachine.register('login',{
        'enter': function(){
            jqNavigationHomeBtn.off('click');//remove the click listener of this element here
            jqNavBarRight.css('display','none');//hide settings etc.
            $('#signed-in-title').html('');

            codeq.samlLogin = false;//remove saml login flag
            $('.saml-login-hide').css('display','');//if we login the normal way we want to show the change password buttons. if we enter the saml login they will be hidden

            //setup the signup button
            jqSignupBtn.on('click', function(e){
                codeq.globalStateMachine.transition('signup');
                e.preventDefault();
            });
            //setup the AAI login btn
            jqAAILoginBtn.on('click',function(e){
                codeq.globalStateMachine.transition('aailogin');
                e.preventDefault();
            });
            jqSubmitLoginBtn.on('click', loginFun);
            formInputs.on('keyup', function (ev) {
                if ((ev.keyCode || ev.which) == 13) {
                    jqSubmitLoginBtn.trigger('click');
                }
            });

            // setup language selection links
            jqLangSelect.on('click', function (e) {
                codeq.setLang($(this).data('lang'));
                e.preventDefault();
            });

            jqScreen.css('display', '');
            jqDisabledOverlay.css('display', 'none');
        },
        'exit' : function(){
            //remove the listener from the buttons specific to this page
            jqSubmitLoginBtn.off('click', loginFun);
            formInputs.off('keyup');
            jqSignupBtn.off('click');
            jqAAILoginBtn.off('click');
            jqLangSelect.off('click');

            jqScreen.css('display', 'none');
            $("#password").val('');
            //re-enable the click listener
            jqNavigationHomeBtn.on('click', function(e){
                codeq.globalStateMachine.transition('language');
                e.preventDefault();
            });
            jqNavBarRight.css('display','');
        }
    });
})();