summaryrefslogtreecommitdiff
path: root/js/codeq/hint.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/codeq/hint.js')
-rw-r--r--js/codeq/hint.js126
1 files changed, 86 insertions, 40 deletions
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 <http://www.gnu.org/licenses/>. */
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 <http://www.gnu.org/licenses/>. */
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 <http://www.gnu.org/licenses/>. */
},
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 <http://www.gnu.org/licenses/>. */
}
}
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 <http://www.gnu.org/licenses/>. */
// hint sequence
jqContainer = $('<div class="hint-static-group"></div>');
jqButton = $('<a class="hint-static-link" ' + ta(trButton) + '></a>');
- 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 <http://www.gnu.org/licenses/>. */
}
else {
// a single hint
- jqContainer = jqHints;
+ jqContainer = box;
jqButton = null;
}
nextJqHint();
@@ -254,7 +249,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
// 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 <http://www.gnu.org/licenses/>. */
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 <http://www.gnu.org/licenses/>. */
}
},
+ // 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 <http://www.gnu.org/licenses/>. */
*/
'planNext': function () {
if (planIdx < planDef.length) {
- typeHandlers['static'](planDef[planIdx], {'id': 'plan'});
+ var jqHintBox = $('<div class="feedback"></div>');
+ 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 <http://www.gnu.org/licenses/>. */
* @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 = $('<div class="feedback"></div>'),
+ 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 = $('<button class="display-hints" data-tkey="btn_hint">Hint</button>');
+ 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 () {