summaryrefslogtreecommitdiff
path: root/js/codeq/signup.js
blob: 7f28e9f22051d8f55c7df858d08287aaff2ed01e (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
118
119
120
121
122
/* CodeQ: an online programming tutor.
   Copyright (C) 2015 UL FRI

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

/**
 * Created by robert on 10/14/15.
 *
 */

(function(){
    "use strict";
    var jqScreen = $("#screen-signup"),
        jqUsername = $("#signup-username"),
        jqName = $("#signup-name"),
        jqEmail = $("#signup-email"),
        jqPassword = $("#signup-password"),
        jqVerify = $("#signup-password-verify"),
        jqFormSignUp = $('#signup-form'),
        jqExitSignUpBtn = $('#signup-cancel'),
        jqLangSelect = jqScreen.find('.lang-select'),
        jqNavBarRight = $('.nav.navbar-nav.navbar-right'),
        jqNavigationHomeBtn = $('#navigation-home'),
        jqMessages = jqFormSignUp.find('.error,.success'),
        redirectTimeout = null;

    codeq.globalStateMachine.register('signup',{
        'jqScreen': jqScreen,

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

            //prepare listener for successfull signup
            jqFormSignUp.on('submit', function(e) {
                jqMessages.hide();
                if (jqPassword.val() != jqVerify.val()) {
                    jqFormSignUp.find('.error.mismatch').show();
                }
                else {
                    codeq.comms.signup(jqUsername.val(), jqName.val(), jqEmail.val(), jqPassword.val())
                        .then(function (data) {
                            var jqSuccess = jqFormSignUp.find('.success'),
                                username = jqUsername.val();
                            switch (data.code) {
                            case 0:  // everything OK
                                $("#username").val(username);
                                jqSuccess.find('.username').text(username);
                                jqSuccess.show();
                                // back to login after short pause
                                redirectTimeout = setTimeout(function() {
                                    codeq.globalStateMachine.transition('login');
                                }, 2000);
                                break;
                            case 10:  // username exists
                                jqFormSignUp.find('.error.username-exists').show();
                                break;
                            default:
                                throw new Error(data.message);
                            }
                        })
                        .fail(function (reason) {
                            codeq.log.error('Signup failed: ' + reason, reason);
                            alert('Signup failed: ' + reason);
                        })
                        .done();
                }
                e.preventDefault(); // Prevent the form from submitting via the browser.
            });

            // setup language selection links
            jqLangSelect.on('click', function (e) {
                codeq.setLang($(this).data('lang'));
                e.preventDefault();
            });
        },
        'exit' : function(){
            if (redirectTimeout !== null) {
                clearTimeout(redirectTimeout);
                redirectTimeout = null;
            }
            jqMessages.hide();
            jqScreen.css('display', 'none');
            //re-enable the click listener
            jqNavigationHomeBtn.on('click', function(e){
                codeq.globalStateMachine.transition('language');
                e.preventDefault();
            });
            //disable listeners on stuff from this page only
            jqExitSignUpBtn.off('click');
            jqFormSignUp.off('submit');
            jqLangSelect.off('click');
            //show the menu buttons
            jqNavBarRight.css('display','');

            jqUsername.val('');
            jqName.val('');
            jqEmail.val('');
            jqPassword.val('');
            jqVerify.val('');
        }
    });

})();