summaryrefslogtreecommitdiff
path: root/js/codeq/prolog.js
diff options
context:
space:
mode:
authorRobert Zorko <robertz@gurucue.com>2015-09-18 10:03:02 +0200
committerRobert Zorko <robertz@gurucue.com>2015-09-18 10:03:02 +0200
commit826db75df3093827fd14009e508774e3fda740e5 (patch)
tree8cf466ba64300d96bbf98b9084702c1f4e41d45c /js/codeq/prolog.js
parentf26997fc8919af9664b5c5330b1d4b4f2f33d5f1 (diff)
updated for the recent changes in hte prolog and python.js files
Diffstat (limited to 'js/codeq/prolog.js')
-rw-r--r--js/codeq/prolog.js171
1 files changed, 20 insertions, 151 deletions
diff --git a/js/codeq/prolog.js b/js/codeq/prolog.js
index 7c9ebb7..682b81e 100644
--- a/js/codeq/prolog.js
+++ b/js/codeq/prolog.js
@@ -269,7 +269,7 @@
* @param {PrologTaskDef} info
* @returns {{destroy: Function, processServerHints: Function}}
*/
- var createPrologHandler = function (info) {
+ createPrologHandler = function (info) {
var problem = info.problem,
jqDescription = $('#description'),
jqEditor = $('#code_editor'),
@@ -278,83 +278,8 @@
editor = CodeMirror(jqEditor[0], { cursorHeight: 0.85, lineNumbers: true, matchBrackets: true }),
activityHandler = makeActivityHandler(editor, problem.id),
terminal = makePrologTerminalHandler(jqConsole, editor, problem.id, activityHandler),
- hintDefs = problem.hint,
- hintCounter = 0, // for generating unique class-names
- hintCleaners = [],
- clearHints = function () {
- var i;
- for (i = hintCleaners.length - 1; i >= 0; i--) {
- hintCleaners[i]();
- }
- hintCleaners.length = 0;
- hintCounter = 0;
- },
- addMark = function (start, end) {
- var posStart = editor.posFromIndex(start),
- posEnd = editor.posFromIndex(end),
- doc = editor.getDoc(),
- mark = doc.markText(posStart, posEnd, {className: 'editor-mark _emark_' + hintCounter}),
- result = {start: posStart, end: posEnd, mark: mark, className: '_emark_' + hintCounter};
- hintCleaners.push(function () { mark.clear(); mark = null; doc = null; result.mark = null; });
- hintCounter++;
- return result;
- },
- processTemplate = function (template, args) {
- if (!args)
- return template;
- return template.replace(/\[%=(\w+)%\]/g, function(match, name) {
- return args[name];
- });
- },
- hintHandlers = {
- 'static': function (type, template, serverHint) {
- codeq.log.debug('Processing static hint');
- var message = processTemplate(template, serverHint.args);
- jqHints.append('<div class="hint-static">' + message + '</div>');
- // no hint cleaner here, a static hint remains on the screen
- },
- 'popup': function (type, template, serverHint) {
- codeq.log.debug('Processing popup hint');
- var message = processTemplate(template, serverHint.args),
- mark = addMark(serverHint.start, serverHint.end), // add the mark
- jqMark = jqEditor.find('.' + mark.className);
-
- jqMark.popover({content: message, html: true, placement: 'auto bottom', trigger: 'hover focus click', container: 'body'});
- hintCleaners.push(function () { if (jqMark) { jqMark.popover('destroy'); jqMark = null; } });
+ hinter = codeq.makeHinter(jqHints, jqEditor, editor, problem.hint);
- mark.mark.on('', function () {});
- },
- 'dropdown': function (type, template, serverHint) {
- codeq.log.debug('Processing dropdown hint');
- var completion = null, // the completion object, created in showHint()
- close = function () {
- if (completion) {
- completion.close();
- completion = null;
- }
- };
-
- if ((editor.listSelections().length > 1) || editor.somethingSelected()) {
- // showHint() doesn't work if a selection is activeparts
- }
-
- editor.showHint({
- hint: function () {
- var hints = {
- list: serverHint.choices,
- from: editor.posFromIndex(serverHint.start),
- to: editor.posFromIndex(serverHint.end)
- };
- completion = editor.state.completionActive;
- return hints;
- },
- completeOnSingleClick: true,
- completeSingle: false
- });
-
- hintCleaners.push(close);
- }
- };
editor.setValue(info.solution);
$('#title').text(problem.slug);
jqDescription.html(problem.description);
@@ -372,77 +297,6 @@
}
});
- var handler = {
- destroy: function () {
- $('#btn_code_hint').off('click');
- $('#btn_code_test').off('click');
- editor.off();//remove listenerrs from the editor
- terminal.off();//same for the terminal is the needed?
- terminal.destroy();
- jqDescription.empty();
- jqEditor.empty(); // TODO: perhaps you do not want to "free" the editor, just empty it
- jqConsole.empty(); // TODO: the same with the console
- jqHints.empty(); // TODO: just make static references to these
- jqDescription = null;
- jqEditor = null;
- jqConsole = null;
- jqHints = null;
- },
-
- /**
- * Processes and display appropriately the server hints.
- * TODO: sort hints so static and popup hints come first, and a (single) drop-down hint last
- *
- * @param {ServerHint[]} serverHints an array of hints from the server
- */
- processServerHints: function (serverHints) {
- var n = serverHints.length,
- /** number */ i,
- /** ServerHint */ serverHint,
- /** HintDefinition */ hintDef,
- hintType, hintTemplate, t, fn;
- clearHints();
- for (i = 0; i < n; i++) {
- serverHint = serverHints[i];
- hintDef = hintDefs[serverHint.id];
- if (serverHint.indices) {
- indices = serverHint.indices
- for (i = 0; i < indices.length; i++) {
- hintDef = hintDef[indices[i]];
- if (!hintDef)
- break;
- }
- }
- if (!hintDef) {
- codeq.log.error('Undefined hint ' + serverHint.id + ' with indices ' + serverHint.indices);
- continue;
- }
- t = typeof hintDef;
- if (t === 'object') {
- hintType = hintDef.type;
- hintTemplate = hintDef.message;
- }
- else if (t === 'string') {
- hintType = 'static';
- hintTemplate = hintDef;
- }
- else {
- codeq.log.error('Unsupported hint definition: ' + t);
- continue;
- }
-
- fn = hintHandlers[hintType];
- if (!fn) codeq.log.error('Unsupported hint type: ' + hintType);
- else fn(hintType, hintTemplate, serverHint);
- }
- }
- };
-
- var jqButtons = $('#block-toolbar button');
-// $(jqButtons.get(0)).on('click', function () { handler.processServerHints([{id:'x_must_be_female'}]); });
-// $(jqButtons.get(1)).on('click', function () { handler.processServerHints([{id:'popup_unknown', start: 20, end: 26}]); });
-// $(jqButtons.get(2)).on('click', function () { handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]); });
-
$('#btn_code_hint').on('click', function () {
terminal.append('hint.\n', 'input');
terminal.inputDisable();
@@ -455,7 +309,7 @@
.then(
function hintSuccess(data) {
if (data.code === 0)
- handler.processServerHints(data.hints);
+ hinter.handle(data.hints);
else
terminal.append(data.message + '\n', 'error');
},
@@ -481,7 +335,7 @@
.then(
function testSuccess(data) {
if (data.code === 0)
- handler.processServerHints(data.hints);
+ hinter.handle(data.hints);
else
terminal.append(data.message + '\n', 'error');
},
@@ -496,6 +350,21 @@
.done();
});
- return handler;
+ return {
+ destroy: function () {
+ $('#btn_code_hint').off('click');
+ $('#btn_code_test').off('click');
+ editor.off('change');
+ hinter.destroy();
+ terminal.destroy();
+ jqDescription.empty();
+ jqEditor.empty(); // TODO: perhaps you do not want to "free" the editor, just empty it
+ jqConsole.empty(); // TODO: the same with the console
+ jqDescription = null;
+ jqEditor = null;
+ jqConsole = null;
+ jqHints = null;
+ }
+ };
};
})(); \ No newline at end of file