summaryrefslogtreecommitdiff
path: root/js/codeq/problem.js
blob: 6debf6d6ca8556129ac44dfe238068d3e1060c59 (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
/**
 * Created by robert on 9/18/15.
 */

/**
 * Created by robert on 9/18/15.
 */

(function(){
    codeq.globalStateMachine.register('problem',{
        'enter': function(language){
            $('#disabled').css('display', '');
            $('#disabled').css('cursor', 'wait');
            codeq.comms.send({'action': 'list_problems', 'language':language})
                .then(
                function success(data) {
                    var i, groups, group, problems, problem, first_group,
                        jqGroup = $('#problem_group'),
                        jqProblems = $('#problems'),
                        html = [],
                        mapping = {},
                        onGroupChange = function () {
                            var problems = mapping[jqGroup.val()],
                                html = [],
                                i, p;
                            if (problems) {
                                for (i = 0; i < problems.length; i++) {
                                    p = problems[i];
                                    html.push('<option value="', p.identifier, '">', p.name, '</option>\n')
                                }
                            }
                            jqProblems.html(html.join(''));
                        };

                    if (data && (data.code === 0)) {
                        $('#disabled').css('display', 'none');
                        groups = data.problems;
                        for (i = 0; i < groups.length; i++) {
                            group = groups[i];
                            var identifier = group.identifier.group;
                            html.push('<option value="', identifier, '">', group.name.group, '</option>\n');
                            mapping[identifier] = group.problems;
                        }
                        jqGroup.html(html.join(''));
                        first_group = html[1];
                        html = null;

                        jqGroup.on('click', onGroupChange);
                        jqGroup.val(first_group);
                        onGroupChange();

                        $("#submit_problem").on('click', function(){
                            var identifier = $('#problem_group').val(),//.split('/'),
                                problem = $('#problems').val();
                            if(!identifier) alert('Choose a problem group');
                            else if (!problem) alert('Choose a problem');
                            else {
                                $('#disabled').css('display', '');
                                codeq.comms.getProblem(language, identifier, problem)
                                    .then(function (data) {
                                        if (data.code !== 0) throw new Error('Failed to obtain problem data, code: ' + data.code + ', message: ' + data.message);
                                        $('#disabled').css('display', 'none');
                                        switch (language) {//(identifier[0]) {
                                            case 'prolog':
                                                codeq.globalStateMachine.transition('prolog', data);
                                                break;
                                            case 'python':
                                                codeq.globalStateMachine.transition('python', data);
                                                break;
                                            default:
                                                alert('Unknown language: ' + language);
                                                break;
                                        }
                                    })
                                    .fail(function (reason) {
                                        $('#disabled').css('display', 'none');
                                        alert('Login request failed: ' + reason);
                                    })
                                    .done();
                            }
                        });
                        $('#screen_problem').css('display', '');
                    }
                    else {
                        $('#disabled').css('cursor', '');
                        alert('Obtaining list of problems failed: code=' + data.code + ', reason=' + data.message);
                    }
                },

                function failure(reason) {
                    $('#disabled').css('cursor', '');
                    alert('Request to obtain list of problems failed: ' + reason);
                }
            ).done();
        },
        'exit' : function(){
            $('#problem_group').off();
            $("#submit_problem").off();
            $("#problem_group option").remove();//empty the selects
            $("#problems option").remove();
            $("#screen_problem").css('display', 'none');
        }
    });
})();