From 1973db21715555e7b668c83e6aace2c7499f8eaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Smodi=C5=A1?= Date: Tue, 25 Aug 2015 14:22:58 +0200 Subject: Login now fetches the list of available problems from the server. Made the query work, so terminal interaction now works. --- js/codeq/comms.js | 9 ++ js/codeq/core.js | 2 +- js/codeq/login.js | 257 ++++++++++++++++++------------------------------------ 3 files changed, 94 insertions(+), 174 deletions(-) (limited to 'js/codeq') diff --git a/js/codeq/comms.js b/js/codeq/comms.js index fb5d348..d2c2910 100644 --- a/js/codeq/comms.js +++ b/js/codeq/comms.js @@ -59,6 +59,15 @@ sendQuery: function commsSendQuery (json) { json['sid'] = codeq.sid; return send('query', codeq.jsonize(json)); + }, + + getProblem: function commsGetProblem (language, problem_group, problem) { + return send('get_problem', codeq.jsonize({ + 'sid': codeq.sid, + 'language': language, + 'problem_group': problem_group, + 'problem': problem + })); } }; })(); diff --git a/js/codeq/core.js b/js/codeq/core.js index f13a019..dd417d4 100644 --- a/js/codeq/core.js +++ b/js/codeq/core.js @@ -13,7 +13,7 @@ window.codeq = { /** * REST API URL prefix. */ - urlPrefix: '/codeq/', + urlPrefix: '/svc/', /** * Returns the number of Unicode code points in the given string. diff --git a/js/codeq/login.js b/js/codeq/login.js index d449835..af50b22 100644 --- a/js/codeq/login.js +++ b/js/codeq/login.js @@ -1,183 +1,94 @@ (function(){ - var groups = { - 'family_relations': { - 'name': 'Family relations', - 'problems': [ - {'id': 'ancestor_2', 'name': 'ancestor/2'}, - {'id': 'aunt_2', 'name': 'aunt/2'}, - {'id': 'brother_2', 'name': 'brother/2'}, - {'id': 'connected_3', 'name': 'connected/3'}, - {'id': 'cousin_2', 'name': 'cousin/2'} - ] - }, - 'lists': { - 'name': 'Lists', - 'problems': [ - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''} - ] - }, - 'sorting': { - 'name': 'Sorting', - 'problems': [ - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''} - ] - }, - 'other': { - 'name': 'Other', - 'problems': [ - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''} - ] - }, - 'sets': { - 'name': 'Sets', - 'problems': [ - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''} - ] - }, - 'trees': { - 'name': 'Trees', - 'problems': [ - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''} - ] - }, - 'license_plates': { - 'name': 'License plates', - }, - 'clp_fd': { - 'name': 'CLP(FD', - 'problems': [ - {'id': 'gcd_3', 'name': 'gcd/3'}, - {'id': 'magic_1', 'name': 'magic/1'}, - {'id': 'puzzle_abc_3', 'name': 'puzzle_abc/3'}, - {'id': 'puzzle_beth_1', 'name': 'puzzle_beth/1'}, - {'id': 'puzzle_momson_2', 'name': 'puzzle_momson/2'}, - {'id': 'puzzle_ratio_2', 'name': 'puzzle_ratio/2'}, - {'id': 'tobase_3', 'name': 'tobase/3'} - ] - }, - 'clp_r': { - 'name': 'CLP(R)', - 'problems': [ - {'id': 'bounding_box_3', 'name': 'bounding_box/3'}, - {'id': 'center_3', 'name': 'center/3'}, - {'id': 'linear_opts_3', 'name': 'linear_opts/3'}, - {'id': 'max_sum_2', 'name': 'max_sum/2'}, - {'id': 'megabytes_2', 'name': 'megabytes/2'}, - {'id': 'turkey_3', 'name': 'turkey/3'} - ] - }, - 'dcg': { - 'name': 'DCG', - 'problems': [ - {'id': 'ab_2', 'name': 'ab/2'}, - {'id': 'digit_2', 'name': 'digit/2'}, - {'id': 'expr_2', 'name': 'expr/2'}, - {'id': 'expr_3', 'name': 'expr/3'}, - {'id': 'flower_2', 'name': 'flower/2'} - ] - }, - 'denotational_semantics': { - 'name': 'Denotational semantics', - 'problems': [ - {'id': 'algol_3', 'name': 'algol/3'}, - {'id': 'algol_for_3', 'name': 'algol_for/3'}, - {'id': 'algol_if_3', 'name': 'algol_if/3'}, - {'id': 'prog_8puzzle_2', 'name': 'prog_8puzzle/2'}, - {'id': 'prog_8puzzle_3', 'name': 'prog_8puzzle/3'} - ] - }, - 'old_exams': { - 'name': 'Old exams', - 'problems': [ - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''}, - {'id': '', 'name': ''} - ] - } - }; - - $("#submit").on('click', function () { - var group = $('#problem_group').val(), - problem = $('#problems').val(); - if (!group) alert('Choose a problem group'); - else if (!problem) alert('Choose a problem'); - else { - $.ajax({ - 'type': 'POST', - 'url': '/codeq/login', - 'accepts': 'application/json', - 'contentType': 'application/json; charset=UTF-8', - 'data': JSON.stringify({ - 'username': $('#username').val(), - 'password': $('#password').val() - }), - 'processData': false, - 'dataType': 'json', - 'error': function loginErrorHandler(jqXHR, textStatus, errorThrown) { - alert('Request for login failed: ' + (errorThrown || textStatus)); - }, - 'success': function loginSuccessHandler(data, textStatus, jqXHR) { - if (data && (data.code === 0)) { - window.location = 'prolog.html#sid=' + data.sid + '/grp=' + group + '/prb=' + problem; - } - else { - alert('Login failed: code=' + data.code + ', reason=' + data.message); - } - } - }); - } - }); + var urlPrefix = '/svc/'; $(document).ready(function () { - var jqGroup = $('#problem_group'), - jqProblems = $('#problems'), - id, g, first_group, html = []; - for (id in groups) { - g = groups[id]; - html.push('\n'); - } - jqGroup.html(html.join('')); - first_group = html[1]; - html = null; + $.ajax({ + 'type': 'POST', + 'url': urlPrefix + 'list_problems', + 'accepts': 'application/json', + 'contentType': 'application/json; charset=UTF-8', + 'data': JSON.stringify({ + 'language': 'prolog' + }), + 'processData': false, + 'dataType': 'json', + 'error': function loginErrorHandler(jqXHR, textStatus, errorThrown) { + $('#disabled').css('cursor', ''); + alert('Request to obtain list of problems failed: ' + (errorThrown || textStatus)); + }, + 'success': function loginSuccessHandler(data, textStatus, jqXHR) { + 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('\n') + } + } + jqProblems.html(html.join('')); + }; - jqGroup.on('click', function () { - var g = groups[jqGroup.val()], - html = [], - problems, i, p; - if (g) { - problems = g.problems; - for (i = 0; i < problems.length; i++) { - p = problems[i]; - if (!p.id) continue; - html.push('\n') + if (data && (data.code === 0)) { + $('#disabled').css('display', 'none'); + groups = data.problems; + for (i = 0; i < groups.length; i++) { + group = groups[i]; + html.push('\n'); + mapping[group.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', function () { + var group = $('#problem_group').val(), + problem = $('#problems').val(); + if (!group) alert('Choose a problem group'); + else if (!problem) alert('Choose a problem'); + else { + $.ajax({ + 'type': 'POST', + 'url': urlPrefix + 'login', + 'accepts': 'application/json', + 'contentType': 'application/json; charset=UTF-8', + 'data': JSON.stringify({ + 'username': $('#username').val(), + 'password': $('#password').val() + }), + 'processData': false, + 'dataType': 'json', + 'error': function loginErrorHandler(jqXHR, textStatus, errorThrown) { + alert('Request for login failed: ' + (errorThrown || textStatus)); + }, + 'success': function loginSuccessHandler(data, textStatus, jqXHR) { + if (data && (data.code === 0)) { + window.location = 'prolog.html#sid=' + data.sid + '/grp=' + group + '/prb=' + problem; + } + else { + alert('Login failed: code=' + data.code + ', reason=' + data.message); + } + } + }); + } + }); + } + else { + $('#disabled').css('cursor', ''); + alert('Obtaining list of problems failed: code=' + data.code + ', reason=' + data.message); } } - jqProblems.html(html.join('')); }); - - jqGroup.val(first_group); }); })(); \ No newline at end of file -- cgit v1.2.1