summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleš Smodiš <aless@guru.si>2015-08-25 14:22:58 +0200
committerAleš Smodiš <aless@guru.si>2015-08-25 14:22:58 +0200
commit1973db21715555e7b668c83e6aace2c7499f8eaa (patch)
tree62efa953e62caf98bec6f3daf4459f1d15e9a033
parent21d213dcff1367c16dc0c3f6585b8e35d7c2f0c7 (diff)
Login now fetches the list of available problems from the server.
Made the query work, so terminal interaction now works.
-rw-r--r--index.html71
-rw-r--r--js/codeq.js21
-rw-r--r--js/codeq/comms.js9
-rw-r--r--js/codeq/core.js2
-rw-r--r--js/codeq/login.js257
-rw-r--r--js/prolog.js29
-rw-r--r--prolog.html2
7 files changed, 165 insertions, 226 deletions
diff --git a/index.html b/index.html
index 803e758..d8b0566 100644
--- a/index.html
+++ b/index.html
@@ -1,37 +1,38 @@
<html>
-<head>
- <title>CodeQ Login</title>
-</head>
-<body>
-<h1>CodeQ Login</h1>
-<hr>
-<table>
- <tbody>
- <tr>
- <td>Username:</td>
- <td><input type="text" name="username" id="username"></td>
- </tr>
- <tr>
- <td>Password:</td>
- <td><input type="password" name="password" id="password"></td>
- </tr>
- <tr>
- <td>Problem:</td>
- <td>
- <select name="problem_group" id="problem_group">
- </select>
- <select name="problem" id="problems">
- </select>
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <button type="button" id="submit">Login</button>
- </td>
- </tr>
- </tbody>
-</table>
-<script src="js/jquery/jquery-1.11.3.js"></script>
-<script src="js/codeq/login.js"></script>
-</body>
+ <head>
+ <title>CodeQ Login</title>
+ </head>
+ <body style="text-align: center;">
+ <h1>CodeQ Login</h1>
+ <hr>
+ <table style="margin: 0 auto;">
+ <tbody>
+ <tr>
+ <td style="text-align: right;">Username:</td>
+ <td><input type="text" name="username" id="username"></td>
+ </tr>
+ <tr>
+ <td style="text-align: right;">Password:</td>
+ <td><input type="password" name="password" id="password"></td>
+ </tr>
+ <tr>
+ <td style="text-align: right;">Problem:</td>
+ <td>
+ <select name="problem_group" id="problem_group" style="min-width: 10em;">
+ </select>
+ <select name="problem" id="problems" style="min-width: 10em;">
+ </select>
+ </td>
+ </tr>
+ <tr>
+ <td colspan="2" style="text-align: center;">
+ <button type="button" id="submit">Login</button>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ <div id="disabled" style="position: fixed; left: 0; right: 0; top: 0; bottom: 0; cursor: wait; background-color: rgba(0, 0, 0, 0.7);"></div>
+ <script src="js/jquery/jquery-1.11.3.js"></script>
+ <script src="js/codeq/login.js"></script>
+ </body>
</html>
diff --git a/js/codeq.js b/js/codeq.js
index 9e33bac..a08d207 100644
--- a/js/codeq.js
+++ b/js/codeq.js
@@ -724,6 +724,8 @@ window.phandler = null; // TODO: this is for debug only
return n;
};
+ codeq.urlPrefix = '/svc/';
+
$(document).ready(function () {
// var s = location.hash;
// if (s.length == 0) return; // empty hash
@@ -780,9 +782,9 @@ window.phandler = null; // TODO: this is for debug only
}
else {
codeq.sid = sid;
- codeq.system.load({
+/* codeq.system.load({
type: 'text',
- url: 'prolog/problems/' + grp + '/' + prb + '/en.py',
+ url: '/prolog/problems/' + grp + '/' + prb + '/en.py',
callback: function (data, status, url) {
if (!data) return;
var info = codeq.parseDefinition(data);
@@ -791,7 +793,20 @@ window.phandler = null; // TODO: this is for debug only
// DEBUG: phandler.processServerHints([{id:'popup_unknown', start: 20, end: 26}]);
// DEBUG: phandler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]);
}
- });
+ });*/
+ codeq.comms.getProblem('prolog', grp, prb).then(
+ function getProblemOK (data) {
+ if (data.code == 0) {
+ window.phandler = codeq.createPrologHandler(data.data, grp, prb); // TODO: assignment to window for debug only
+ }
+ else {
+ alert(data.message);
+ }
+ },
+ function getProblemFail (error) {
+ alert('' + error);
+ }
+ ).done();
}
});
})();
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('<option value="', id, '">', g.name, '</option>\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('<option value="', p.identifier, '">', p.name, '</option>\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('<option value="', p.id, '">', p.name, '</option>\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('<option value="', group.identifier, '">', group.name, '</option>\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
diff --git a/js/prolog.js b/js/prolog.js
index 0a0eb48..50b9176 100644
--- a/js/prolog.js
+++ b/js/prolog.js
@@ -6,7 +6,7 @@
// a constant
var firstCharacterPos = {'line': 0, 'ch': 0};
- var makePrologTerminalHandler = function (jqConsole, editor, problem_group, problem) {
+ var makePrologTerminalHandler = function (jqConsole, editor, problem_id) {
var promptMode = true, // default: query composition; alternative: query result browsing
tcs = function terminalCommandSuccess (data) {
var t, lines, i;
@@ -36,9 +36,10 @@
'step': 'run',
'program': editor.getDoc().getValue(),
'query': command,
- 'language': 'prolog',
- 'problem_group': problem_group,
- 'problem': problem
+// 'language': 'prolog',
+// 'problem_group': problem_group,
+// 'problem': problem
+ 'problem_id': problem_id
}).then(tcs, tcf).done();
}
else {
@@ -85,8 +86,9 @@
* @param {PrologTaskDef} info
* @returns {{destroy: Function, processServerHints: Function}}
*/
- codeq.createPrologHandler = function (info, problem_group, problem) {
- var jqDescription = $('#description'),
+ codeq.createPrologHandler = function (info) {
+ var problem = info.problem,
+ jqDescription = $('#description'),
jqEditor = $('#code_editor'),
jqConsole = $('#console'),
jqHints = $('#info'),
@@ -104,9 +106,9 @@
promptHistory: false,
welcomeMessage: 'Prolog REPL.'
}),*/
- terminal = makePrologTerminalHandler(jqConsole, editor, problem_group, problem),
+ terminal = makePrologTerminalHandler(jqConsole, editor, problem.id),
/** Object.<string, HintDefinition> */
- hintDefs = info.hint,
+ hintDefs = problem.hint,
hintCounter = 0, // for generating unique class-names
hintCleaners = [],
clearHints = function () {
@@ -218,8 +220,9 @@
return dt;
};
- editor.setValue('sister(X, Y) :-\n female(X),\n parent(Z, X),\n parent(Z, Y),\n X \\== Y.'); // demo
- jqDescription.html(info.description);
+ editor.setValue(info.solution);
+ $('#title').text(problem.slug);
+ jqDescription.html(problem.description);
editor.on('change', function (instance, changeObj) {
var doc = editor.getDoc(),
@@ -297,13 +300,13 @@
var doc = editor.getDoc(),
dt = deltaActivityMillis();
// codeq.comms.sendActivity({'typ': 'slv', 'dt': dt, 'qry': });
- handler.processServerHints([{id:'list_empty'}]);
+// handler.processServerHints([{id:'list_empty'}]);
});
$('#btn_code_break').on('click', function () {
- handler.processServerHints([{id:'popup_unknown', start: 20, end: 26}]);
+// handler.processServerHints([{id:'popup_unknown', start: 20, end: 26}]);
});
$('#btn_code_hint').on('click', function () {
- handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]);
+// handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]);
});
return handler;
diff --git a/prolog.html b/prolog.html
index 7bda6e9..a83c2cd 100644
--- a/prolog.html
+++ b/prolog.html
@@ -6,7 +6,7 @@
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="format-detection" content="telephone=no">
- <!--link rel="stylesheet" href="css/jquery/jquery.terminal.css" type="text/css"-->
+ <link rel="stylesheet" href="css/jquery/jquery.terminal.css" type="text/css">
<!-- CodeMirror -->
<link rel="stylesheet" href="css/codemirror/codemirror.css" type="text/css">
<link rel="stylesheet" href="css/codemirror/show-hint.css" type="text/css">