summaryrefslogtreecommitdiff
path: root/js/codeq/login.js
blob: 9be1c8c5e451d86597e24864444520075cbaf8a5 (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
/**
 * Created by robert on 9/17/15.
 */

(function(){
    var loginFun = function(){
            $('#disabled').css('display', '');
            $('#disabled').css('cursor', 'wait');
            codeq.comms.connect()
                .then(function () {
                    return codeq.comms.login($('#username').val(), $('#password').val());
                })
                .then(function (data) {
                    $('#disabled').css('display', 'none');
                    $('#disabled').css('cursor', '');
                    if (data.code !== 0) throw new Error('Login failed, code: ' + data.code + ', message: ' + data.message);

                    //merge these settings into the already existing default settings
                    var sett = data.settings;
                    $.merge(true, codeq.settings, sett);
                    if('lan' in sett && sett['lan'] in codeq.supportedLangs){
                        codeq.setLang(sett['lan']);
                    }

                    codeq.globalStateMachine.transition('language');
                    //codeq.activateState('language');
                })
                .fail(function (reason) {
                    $('#disabled').css('display', 'none');
                    $('#disabled').css('cursor', '');
                    codeq.log.error('Login failed: ' + reason, reason);
                    alert('Login request failed: ' + reason);
                    $('#disabled').css('display', 'none');
                })
                .done();
        },
        formInputs = $('#username, #password');

    codeq.globalStateMachine.register('login',{
        'enter': function(){
            $('#signed-in-title').html('');
            $("#submit").on('click', loginFun);
            formInputs.on('keyup', function (ev) {
                if ((ev.keyCode || ev.which) == 13) {
                    $('#submit').trigger('click');
                }
            });
            $('#modalLogIn').modal();

            $("#screen_login").css('display', '');
            $('#disabled').css('display', 'none');
        },
        'exit' : function(){
            $("#submit").off('click', loginFun);
            formInputs.off('keyup');
            $("#screen_login").css('display', 'none');
            $('#signed-in-title').html('Signed in as '+$('#username').val());
            $("#password").val('');
        }
    });
})();