From 1dd4d473bb136c607eeea3a2d655b3f56e5aeee8 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Tue, 23 Feb 2016 17:31:56 +0100 Subject: Remove explicit "Hint" button Hints are now generated for each tested program, and the user can press a button to reveal the hints. --- js/codeq/hint.js | 126 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 40 deletions(-) (limited to 'js/codeq/hint.js') diff --git a/js/codeq/hint.js b/js/codeq/hint.js index bc42694..0550a41 100644 --- a/js/codeq/hint.js +++ b/js/codeq/hint.js @@ -24,7 +24,7 @@ along with this program. If not, see . */ var firstCharacterPos = {'line': 0, 'ch': 0}, sel_no_scroll = {'scroll': false}; - codeq.makeHinter = function (jqHints, jqEditor, editor, trNamespace, problemDef, commonDef) { + codeq.makeHinter = function (jqHints, jqEditor, editor, trNamespace, problemDef, commonDef, activityHandler) { var hintCounter = 0, // for generating unique class-names hintCleaners = [], popoverHintCleaners = [], // we require separate cleaners for popups, because they are rebuilt when the editor's DOM changes @@ -51,6 +51,7 @@ along with this program. If not, see . */ popoverHintCleaners[i](); } popoverHintCleaners.length = 0; + $('div.hints > div.feedback > button.display-hints').off().remove(); }, addMark = function (start, end, style) { @@ -180,7 +181,7 @@ along with this program. If not, see . */ }, typeHandlers = { - 'static': function (template, hint) { + 'static': function (template, hint, box) { var content = prepareStaticHintContent(template, hint.indices, hint.id), args = hint ? hint.args : null, hintIndex = 0, @@ -222,15 +223,9 @@ along with this program. If not, see . */ } } else { - jqContainer.prepend(jq); + jqContainer.append(jq); } codeq.tr.translateDom(jq); - //// scroll into view if overflowing - //deltaHeight = jqHints.height() - jqHintsContainer.height(); - //if (deltaHeight > 0) { - // jqHintsContainer.scrollTop(deltaHeight); - //} - jqHintsContainer.scrollTop(0); }, jqContainer, jqButton; @@ -238,7 +233,7 @@ along with this program. If not, see . */ // hint sequence jqContainer = $('
'); jqButton = $(''); - jqHints.prepend(jqContainer); + box.append(jqContainer); jqContainer.append(jqButton); jqButton.on('click', function () { nextJqHint(); @@ -246,7 +241,7 @@ along with this program. If not, see . */ } else { // a single hint - jqContainer = jqHints; + jqContainer = box; jqButton = null; } nextJqHint(); @@ -254,7 +249,7 @@ along with this program. If not, see . */ // no hint cleaner here, a static hint remains on the screen }, - 'popup': function (template, hint) { + 'popup': function (template, hint, box) { codeq.log.debug('Processing popup hint'); var args = hint.args, style = hint.style || '', @@ -297,7 +292,7 @@ along with this program. If not, see . */ return instFunc; }, - 'dropdown': function (template, hint) { + 'dropdown': function (template, hint, box) { codeq.log.debug('Processing dropdown hint'); var completion = null, // the completion object, created in showHint() close = function () { @@ -330,6 +325,46 @@ along with this program. If not, see . */ } }, + // process and append [hints] to the element [box] + appendHints = function (hints, box) { + var finalizers = [], + i, hint, hintDef, hintContent, hintType, t, fn, ret; + + activityHandler.queueTrace({'typ': 'hint', 'feedback': hints}); + for (i = 0; i < hints.length; i++) { + hint = hints[i]; + hintDef = hintProblemDefs[hint.id] || hintCommonDefs[hint.id]; + if (!hintDef) { + codeq.log.error('Undefined hint: ' + hint.id); + continue; + } + hintContent = hintProblemTr[hint.id] || hintCommonTr[hint.id]; + if (!hintContent) { + codeq.log.error('Hint without content: ' + hint.id); + continue; + } + + t = typeof hintDef; + if (t === 'string') hintType = hintDef; // currently a hint type is a string + else if ((t === 'object') && (hintDef !== null)) hintType = hintDef.type; // but in future we may use an object, if a definition becomes more complex + else { + codeq.log.error('Cannot determine the type of hint ' + hint.id + ' from: ' + hintDef); + continue; + } + + fn = typeHandlers[hintType]; + if (!fn) codeq.log.error('Unsupported hint type: ' + hintType); + else { + ret = fn(hintContent, hint, box); + if (typeof ret === 'function') finalizers.push(ret); + } + } + // invoke any finalizers + for (i = 0; i < finalizers.length; i++) { + finalizers[i](); + } + }, + /** * When the editor updates its DOM, we have to re-register any popup hints. */ @@ -349,7 +384,12 @@ along with this program. If not, see . */ */ 'planNext': function () { if (planIdx < planDef.length) { - typeHandlers['static'](planDef[planIdx], {'id': 'plan'}); + var jqHintBox = $(''); + activityHandler.queueTrace({'typ': 'plan', 'index': planIdx}); + clearHints(); + typeHandlers['static'](planDef[planIdx], {'id': 'plan'}, jqHintBox); + jqHints.prepend(jqHintBox); + jqHintsContainer.scrollTop(0); planIdx++; } return planIdx < planDef.length; @@ -366,42 +406,48 @@ along with this program. If not, see . */ * @param {ServerHint[]} hints an array of hints from the server */ 'handle': function (hints) { - var finalizers = [], - i, hint, hintDef, hintContent, hintType, t, fn, ret; + var i, hint, hintDef, hintContent, + n_correct = 0, n_all = 0, + jqHintBox = $(''), + jqHintBtn; + // clear any existing hints clearHints(); + + // display the test_results hint first if found for (i = 0; i < hints.length; i++) { hint = hints[i]; - hintDef = hintProblemDefs[hint.id] || hintCommonDefs[hint.id]; - if (!hintDef) { - codeq.log.error('Undefined hint: ' + hint.id); - continue; - } - hintContent = hintProblemTr[hint.id] || hintCommonTr[hint.id]; - if (!hintContent) { - codeq.log.error('Hint without content: ' + hint.id); - continue; + if (hint.id === 'test_results') { + activityHandler.queueTrace({'typ': 'test', 'feedback': hint}); + n_correct = hint.args.passed + n_all = hint.args.total + hintContent = hintProblemTr[hint.id] || hintCommonTr[hint.id]; + typeHandlers['static'](hintContent, hint, jqHintBox); + hints.splice(i, 1); + break; } + } - t = typeof hintDef; - if (t === 'string') hintType = hintDef; // currently a hint type is a string - else if ((t === 'object') && (hintDef !== null)) hintType = hintDef.type; // but in future we may use an object, if a definition becomes more complex - else { - codeq.log.error('Cannot determine the type of hint ' + hint.id + ' from: ' + hintDef); - continue; + // display remaining hints + if (hints.length > 0) { + if (n_all == 0 || n_correct == n_all) { + // no test_results or program correct: show all hints immediately + appendHints(hints, jqHintBox); } - - fn = typeHandlers[hintType]; - if (!fn) codeq.log.error('Unsupported hint type: ' + hintType); else { - ret = fn(hintContent, hint); - if (typeof ret === 'function') finalizers.push(ret); + // otherwise, hide hints behind a button + jqHintBtn = $(''); + codeq.tr.translateDom(jqHintBtn); + jqHintBtn.on('click', function (e) { + jqHintBtn.off().remove(); + appendHints(hints, jqHintBox); + + }); + jqHintBox.append(jqHintBtn); } } - // invoke any finalizers - for (i = 0; i < finalizers.length; i++) { - finalizers[i](); - } + jqHints.prepend(jqHintBox); + jqHintsContainer.scrollTop(0); }, 'destroy': function () { -- cgit v1.2.1