summaryrefslogtreecommitdiff
path: root/js/codeq/navigation.js
blob: 97257813ead878b5f11c903d4aea469c3312168c (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* CodeQ: an online programming tutor.
   Copyright (C) 2015,2016 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/>. */

(function() {
    "use strict";
    var makeGlobalStateMachine = function (def) {
        var currState = null,
            waitingState = null; // state hidden with a modal form
        var stateChangeFun = function historyStateChangeHandler() {
            var historyState = History.getState();
            codeq.globalStateMachine.actualTransition.apply(
                codeq.globalStateMachine,
                $.merge([historyState.data.state],
                historyState.data.params));
        };

        History.Adapter.bind(window, 'statechange', stateChangeFun);
        // prepare the history state change listener
        return {
            'transition': function (name) {
                var newState = def[name];
                if (!newState) {
                    codeq.log.error('Cannot transition to undefined state: ' + name);
                    return;
                }
                if (newState === currState) {
                    codeq.log.info('Will not transition between identical states: ' + name);
                    return;
                }
                if (History.getState().data.state === name) {
                    // special case which happens if the user refreshes while
                    // in the login screen (history state doesn't change
                    // because it goes from login to login and the above
                    // listener doesn't trigger)
                    codeq.globalStateMachine.actualTransition.apply(
                        codeq.globalStateMachine,
                        Array.prototype.slice.apply(arguments, [0]));
                }
                try {
                    History.pushState(
                        {'state': name, 'params': Array.prototype.slice.apply(arguments, [1])},
                        'CodeQ', '?s=' + name);
                }
                catch (e) {
                    codeq.log.error('History.pushState() failed for new state ' + name + '. ' +
                                    'Error:' + e, e);
                }
            },
            'destroy': function () {
                if (currState) currState.exit();
                currState = null;
                if (waitingState) waitingState.exit();
                waitingState = null;
                History.Adapter.unbind(window, 'statechange', stateChangeFun);
            },
            'register': function (name, state, props) {
                if (name in def) {
                    codeq.log.error('State ' + name + ' is already registered, overriding.');
                }
                def[name] = state;
            },
            'actualTransition': function (name) {
                // always transition to 'login' state if no session exists
                // (except for 'signup'/'aailogin' states)
                if (!codeq.comms.getSid() && name !== "signup" && name !== "aailogin") {
                    name = 'login';
                }

                // we have already checked that newState exists, and that it is
                // different from the current state
                var newState = def[name];
                if (currState) {
                    if (newState.isModal) {
                        if (currState.isModal) {
                            currState.exit();
                        }
                        else {
                            waitingState = currState;
                            waitingState.jqScreen.css('display', 'none');
                        }
                        currState = newState;
                        currState.enter.apply(currState, Array.prototype.slice.apply(arguments, [1]));
                    }
                    else {
                        currState.exit();
                        currState = newState;
                        if (waitingState === newState) {
                            waitingState.jqScreen.css('display', '');
                            waitingState = null;
                        }
                        else {
                            if (waitingState) {
                                waitingState.exit();
                                waitingState = null;
                            }
                            currState.enter.apply(currState, Array.prototype.slice.apply(arguments, [1]));
                        }
                    }
                }
                else {
                    currState = newState;
                    currState.enter.apply(currState, Array.prototype.slice.apply(arguments, [1]));
                }
            }
        }
    };
    codeq.globalStateMachine = makeGlobalStateMachine({});

    // setup the navigation buttons
    $('#navigation-home').on('click', function (e) {
        codeq.globalStateMachine.transition('language');
        e.preventDefault();
    });
    $('#navigation-problem-list').on('click', function (e) {
        codeq.globalStateMachine.transition('problem_list');
        e.preventDefault();
    });
    $('#navigation-python').on('click', function (e) {
        codeq.globalStateMachine.transition('python');
        e.preventDefault();
    });
    $('#navigation-prolog').on('click', function (e) {
        codeq.globalStateMachine.transition('prolog');
        e.preventDefault();
    });
    $('#navigation-robot').on('click', function (e) {
        codeq.globalStateMachine.transition('robot');
        e.preventDefault();
    });
    $('#navigation-logout').on('click', function (e) {
        codeq.comms.logout()
            .then(function (data) {
                codeq.comms.disconnect();
            })
            .fail(function (reason) {
                codeq.log.error('Logout failed: ' + reason);
            })
            .done();

        /* leaving this here if we later decide to enable saml_logout
        if(codeq.samlLogin){
            codeq.comms.samlLogout()
                .then(function (data) {
                    console.log(data);
                    codeq.comms.disconnect();
                })
                .fail(function (reason) {
                    console.log(reason);
                })
                .done();
        }else{
            codeq.comms.logout()
                .then(function (data) {
                    console.log(data);
                    codeq.comms.disconnect();
                })
                .fail(function (reason) {
                    console.log(reason);
                })
                .done();
        }*/

        codeq.globalStateMachine.transition('login');
        e.preventDefault();
    });
    $('#navigation-profile').on('click', function (e) {
        codeq.globalStateMachine.transition('profile');
        e.preventDefault();
    });
    $('#navigation-about').on('click', function (e) {
        codeq.globalStateMachine.transition('about');
        e.preventDefault();
    });
    $('#navigation-change-password').on('click', function (e) {
        codeq.globalStateMachine.transition('changePassword');
        e.preventDefault();
    });
    $('#navigation-settings').on('click',function(e){
        codeq.globalStateMachine.transition('settings');
        e.preventDefault();
    });
})();