summaryrefslogtreecommitdiff
path: root/js/codeq/stateMachine.js
blob: 720400d2c4d7cff23e209c69564a5de633066965 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
 * Created by robert on 9/15/15.
 */

codeq.makeStateMachine = function(def){
    var currState = null;
    return {
        'transition': function(name){
            if(currState !== null) currState.exit();
            currState = def[name];
            currState.enter();
        },
        'destroy': function(){
            if(currState !== null) currState.exit();
            currState = null;
        }
    }
}




var loginFun = function(){
    var identifier = $('#problem_group').val().split('/'),
        problem = $('#problems').val();
    if (identifier.length < 2) alert('Choose a problem group');
    else if (!problem) alert('Choose a problem');
    else {
        $('#disabled').css('display', '');
        codeq.comms.login($('#username').val(), $('#password').val())
            .then(function (data) {
                $('#disabled').css('display', 'none');
                if (data.code !== 0) throw new Error('Login failed, code: ' + data.code + ', message: ' + data.message);
            })
            .then(function () {
                return codeq.comms.getProblem(identifier[0], identifier[1], 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 (identifier[0]) {
                    case 'prolog':
                        // TODO: assignment to window for debug only
                        //$('#screen_login').css('display', 'none');
                        //$('#screen_prolog').css('display', '');
                        codeq.globalStateMachine.transition('prolog');
                        window.phandler = codeq.createPrologHandler(data.data);

                        var jq = $('#console textarea').on('focus',function(){
                            codeq.log.debug('focus');
                        }).on('blur',function(){
                            codeq.log.debug('blur');
                        });

                        var c = $('#console').on('focus',function(){
                            codeq.log.debug('console focus');
                        }).on('blur',function(){
                            codeq.log.debug('console blur');
                        });

                        codeq.log.debug("textareas: "+jq.length );
                        codeq.log.debug("consols: "+c.length );

                        break;
                    case 'python':
                        // TODO: assignment to window for debug only
                        //$('#screen_login').css('display', 'none');
                        //$('#screen_prolog').css('display', '');
                        codeq.globalStateMachine.transition('prolog');
                        window.phandler = codeq.createPythonHandler(data.data);
                        $('#console textarea').on('focus',function(){
                            codeq.log.debug('focus');
                        });
                        $('#console textarea').on('blur',function(){
                            codeq.log.debug('blur');
                        });

                        break;
                    default:
                        alert('Unknown language: ' + identifier[0]);
                        break;
                }
            })
            .fail(function (reason) {
                $('#disabled').css('display', 'none');
                alert('Login request failed: ' + reason);
            })
            .done();
    }
}

codeq.globalStateMachine = codeq.makeStateMachine({
    'login':{
        'enter': function(){
            codeq.comms.connect().then(function () {
                return codeq.comms.send({'action': 'list_problems'});//currently problem list and the actual login are still in the same state
            }).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.language + '/' + group.identifier.group;
                            var name = group.name.language + ': ' + group.name.group;
                            html.push('<option value="', identifier, '">', name, '</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").on('click', loginFun);
                    }
                    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(){
            $("#submit").off('click', loginFun);
            $("#screen_login").css('display', 'none');
        }
    },

    'prolog':{
        'enter': function(){
            $('#screen_prolog').css('display', '');
            removeFocusFromElements();
            problems = codeq.makeStateMachine(substates);
            //problems.transition('instructions');
            problems.transition('instructions');
        },
        'exit': function(){
            $('#screen_prolog').css('display', 'none');
            problems.destroy();
        }
    }
});

/*
the following variables are used in the sub-state machine representing the 4 different panels in the codeq app
 */
var problems,//this will the actual state machine
    description = $('div.col-lg-3.col-md-6.col-sm-12:has(#description)'),//lets actually find the needed divs for later use
    code = $('div.col-lg-3.col-md-6.col-sm-12:has(#code_editor)'),
    consoleDiv = $('div.col-lg-3.col-md-6.col-sm-12:has(#console)'),//named 'consoleDiv', because 'console' is already in use
    info = $('div.col-lg-3.col-md-6.col-sm-12:has(#info)'),

    //some functions used inside the enter and exit functions of the state machine
    eventName = 'mousedown',//event name of the event which will trigger the transition between these substates
    mouseDownEventFunctionDescription = function(){
        problems.transition('instructions');
    },
    mouseDownEventFunctionCode = function(){
        problems.transition('code');
    },
    mouseDownEventFunctionConsole = function(){
        problems.transition('console');
    },
    mouseDownEventFunctionInfo = function(){
        problems.transition('info');
    },
    removeListenersFromDivs = function(){
        description.off(eventName,mouseDownEventFunctionDescription);
        code.off(eventName,mouseDownEventFunctionCode);
        consoleDiv.off(eventName,mouseDownEventFunctionConsole);
        info.off(eventName,mouseDownEventFunctionInfo);
    },
    removeBlockClassesFromDivs = function(){
        description.removeClass('block');
        code.removeClass('block');
        consoleDiv.removeClass('block');
        info.removeClass('block');
    },
    removeChangedClassesFromDivs = function(){
        description.removeClass('block-focus block-less-width block-less-height block-less-everything').addClass('block');//these class names were chosen for the view where the screen is partitioned in 4 quarters (2 by 2)
        code.removeClass('block-focus block-less-width block-less-height block-less-everything').addClass('block');
        consoleDiv.removeClass('block-focus block-less-width block-less-height block-less-everything').addClass('block');
        info.removeClass('block-focus block-less-width block-less-height block-less-everything').addClass('block');
    },
    setTransitionsBetweenDivs = function(current){
        if(current !== description)description.on(eventName,mouseDownEventFunctionDescription);
        if(current !== code)code.on( eventName,mouseDownEventFunctionCode);
        if(current !== info)info.on( eventName,mouseDownEventFunctionInfo);
        if(current !== consoleDiv)consoleDiv.on( eventName,mouseDownEventFunctionConsole);

        /*if(current !== description)description.on( eventName,function(){
            problems.transition('instructions');
        });
        if(current !== info)info.on( eventName,function(){
            problems.transition('info');
        });
        if(current !== code)code.on( eventName,function(){
            problems.transition('code');
        });
        if(current !== consoleDiv)consoleDiv.on( eventName,function(){
            problems.transition('console');
        });*/
    },
    removeFocusFromElements = function(){

    },

    //the states of the sub-state machine
    substates = {
        'instructions':{
            'enter': function(){
                removeBlockClassesFromDivs();

                description.addClass('block-focus');
                code.addClass('block-less-width');
                consoleDiv.addClass('block-less-height');
                info.addClass('block-less-everything');

                setTransitionsBetweenDivs(description);
            },
            'exit': function(){
                removeFocusFromElements();
                removeChangedClassesFromDivs();
                removeListenersFromDivs();
            }
        },
            'code':{
            'enter': function(){
                removeBlockClassesFromDivs();

                description.addClass('block-less-width');
                code.addClass('block-focus');
                consoleDiv.addClass('block-less-everything');
                info.addClass('block-less-height');

                setTransitionsBetweenDivs(code);
            },
            'exit': function(){
                removeFocusFromElements();
                removeChangedClassesFromDivs();
                removeListenersFromDivs();
            }
        },
            'info':{
            'enter': function(){
                removeBlockClassesFromDivs();

                description.addClass('block-less-everything');
                code.addClass('block-less-height');
                consoleDiv.addClass('block-less-width');
                info.addClass('block-focus');

                setTransitionsBetweenDivs(info);
            },
            'exit': function(){
                //$('div.col-lg-3.col-md-6.col-sm-12').addClass('block');
                removeFocusFromElements();
                removeChangedClassesFromDivs();
                removeListenersFromDivs();
            }
        },
            'console':{
            'enter': function(){
                removeBlockClassesFromDivs();

                description.addClass('block-less-height');
                code.addClass('block-less-everything');
                consoleDiv.addClass('block-focus');
                info.addClass('block-less-width');

                setTransitionsBetweenDivs(consoleDiv);
            },
            'exit': function(){
                //$('span.cursor.blink').removeClass('blink');

                removeFocusFromElements();
                removeChangedClassesFromDivs();
                removeListenersFromDivs();
            }
        }
    };