diff options
author | Robert Zorko <robertz@gurucue.com> | 2015-09-29 18:14:46 +0200 |
---|---|---|
committer | Robert Zorko <robertz@gurucue.com> | 2015-09-29 18:14:46 +0200 |
commit | b852541b2d23413471c55cb4ee9e5a3e18533d71 (patch) | |
tree | 99130de662fdb1329d8e8d85b035a7a9d8b86b48 /js | |
parent | 41412acda7dbc4f13a892455e3f287427639dffb (diff) | |
parent | 85e530fc7d2cf091734f0d126968d8c50defb65c (diff) |
resolved merge conflict
Diffstat (limited to 'js')
-rw-r--r-- | js/codeq/hint.js | 68 | ||||
-rw-r--r-- | js/codeq/login.js | 4 | ||||
-rw-r--r-- | js/codeq/problem.js | 70 | ||||
-rw-r--r-- | js/codeq/prolog.js | 8 | ||||
-rw-r--r-- | js/codeq/python.js | 8 |
5 files changed, 89 insertions, 69 deletions
diff --git a/js/codeq/hint.js b/js/codeq/hint.js index 1b97afc..d30f934 100644 --- a/js/codeq/hint.js +++ b/js/codeq/hint.js @@ -7,7 +7,7 @@ var firstCharacterPos = {'line': 0, 'ch': 0}, sel_no_scroll = {'scroll': false}; - codeq.makeHinter = function (jqHints, jqEditor, editor, trNamespace, hintDefsA, commonHintDefsA, planDef) { + codeq.makeHinter = function (jqHints, jqEditor, editor, trNamespace, problemDef, commonDef) { var hintCounter = 0, // for generating unique class-names hintCleaners = [], planIdx = 0, @@ -133,25 +133,15 @@ codeq.tr.registerDictionary(trNamespace, dictionary); - // TODO: below is a temporary code to bridge the old implementation with the new data format - if (planDef.sl) planDef = planDef.sl; - else planDef = planDef.en || []; - var hintDefs = {}, t1, t2, k; - if (hintDefsA.sl) t1 = hintDefsA.sl; - else t1 = hintDefsA.en || {}; - if (commonHintDefsA.sl) t2 = commonHintDefsA.sl; - else t2 = commonHintDefsA.en || {}; - for (k in t2) { - if (!t2.hasOwnProperty(k)) continue; - hintDefs[k] = t2[k]; - } - for (k in t1) { - if (!t1.hasOwnProperty(k)) continue; - hintDefs[k] = t1[k]; - } + var hintProblemDefs = problemDef.hint_type, + hintCommonDefs = commonDef.hint_type, + hintProblemTr = problemDef.hint, + hintCommonTr = commonDef.hint, + planDef = problemDef.plan.sl; return { - /** Display the next "planning" hint and return whether there are + /** + * Display the next "planning" hint and return whether there are * any more available. */ 'planNext': function () { @@ -170,46 +160,50 @@ */ 'handle': function (serverHints) { var n = serverHints.length, - /** number */ i, - /** ServerHint */ serverHint, - /** HintDefinition */ hintDef, - hintType, hintTemplate, t, fn, indices; + i, serverHint, hintId, hintDef, hintContent, hintType, hintTemplate, t, fn, indices; clearHints(); mainLoop: for (i = 0; i < n; i++) { serverHint = serverHints[i]; - hintDef = hintDefs[serverHint.id]; + hintId = serverHint.id; + hintDef = hintProblemDefs[hintId]; + if (hintDef) { + hintContent = hintProblemTr[hintId]; + } + else { + hintDef = hintCommonDefs[hintId]; + hintContent = hintCommonTr[hintId]; + } if (!hintDef) { - codeq.log.error('Undefined hint ' + serverHint.id); + codeq.log.error('Undefined hint: ' + hintId); + continue; + } + if (!hintContent) { + codeq.log.error('Hint without content: ' + hintId); continue; } if (serverHint.indices) { indices = serverHint.indices; for (i = 0; i < indices.length; i++) { - hintDef = hintDef[indices[i]]; - if (!hintDef) { - codeq.log.error('Undefined hint ' + serverHint.id + ' with indices ' + serverHint.indices); + hintContent = hintContent[indices[i]]; + if (!hintContent) { + codeq.log.error('Cannot reference hint ' + hintId + ' with indices ' + serverHint.indices); continue mainLoop; } } } + t = typeof hintDef; - if ((t === 'string') || (hintDef instanceof Array)) { - hintType = 'static'; - hintTemplate = hintDef; - } - else if (t === 'object') { - hintType = hintDef.type; - hintTemplate = hintDef.message; - } + if (t === 'string') hintType = hintDef; + else if ((t === 'object') && (hintDef !== null)) hintType = hintDef.type; else { - codeq.log.error('Unsupported hint definition: ' + t); + codeq.log.error('Cannot determine type of hint ' + hintId + ' from: ' + hintDef); continue; } fn = typeHandlers[hintType]; if (!fn) codeq.log.error('Unsupported hint type: ' + hintType); - else fn(hintType, hintTemplate, serverHint); + else fn(hintType, hintContent.sl, serverHint); } }, diff --git a/js/codeq/login.js b/js/codeq/login.js index e3204d3..e5a446b 100644 --- a/js/codeq/login.js +++ b/js/codeq/login.js @@ -36,8 +36,9 @@ }; codeq.globalStateMachine.register('login',{ 'enter': function(){ + $('#signed-in-title').html(''); $("#submit").on('click', loginFun); - //$('#modalLogin').modal(); + $('#modalLogIn').modal(); $("#screen_login").css('display', ''); $('#disabled').css('display', 'none'); @@ -45,6 +46,7 @@ 'exit' : function(){ $("#submit").off('click', loginFun); $("#screen_login").css('display', 'none'); + $('#signed-in-title').html('Signed in as '+$('#username').val()); $("#password").val(''); } }); diff --git a/js/codeq/problem.js b/js/codeq/problem.js index 7ae54d8..d07e5fa 100644 --- a/js/codeq/problem.js +++ b/js/codeq/problem.js @@ -10,13 +10,23 @@ // processed form, hint key -> translation language -> value // ================================================================================ + defaultHintCondition = function (translation) { + // must contain at least one translation + var key; + if (!translation || typeof translation !== 'object') return false; + for (key in translation) { + if (!translation.hasOwnProperty(key)) continue; + return true; + } + return false; + }, processHints = function (rawTranslations) { var defaultHint = {}, // here we put all the hints with their default translations allHints = {}, // the result allHintKeys = [], tr, key, i, lang, hint, h, j; // find the default hint translations, they will form the basis of default hints - tr = chooseDefaultTranslation(rawTranslations, 'hint') || {}; + tr = chooseDefaultTranslation(rawTranslations, 'hint', defaultHintCondition) || {}; for (key in tr) { // copy the hints if (!tr.hasOwnProperty(key)) continue; defaultHint[key] = tr[key]; @@ -26,7 +36,7 @@ for (i = langs.length - 1; i >= 0; i--) { lang = langs[i]; tr = rawTranslations[lang]; - if (!tr || !tr.hint) continue; // skip unavailable translations or translations with no hints + if (!tr || !defaultHintCondition(tr.hint)) continue; // skip unavailable translations or translations with no hints hint = tr.hint; for (key in hint) { if (!hint.hasOwnProperty(key) || !hint[key]) continue; @@ -37,22 +47,26 @@ } } // create all translations for hints + for (i = allHintKeys.length - 1; i >= 0; i--) { + allHints[allHintKeys[i]] = {}; // create keys with no translations, we'll fill them in the next loop + } for (i = langs.length - 1; i >= 0; i--) { lang = langs[i]; tr = rawTranslations[lang]; // set up hints - if (!tr || !tr.hint) { + if (!tr || !defaultHintCondition(tr.hint)) { // there's no hint in the current language, copy the default in its entirety - allHints[lang] = defaultHint; + for (j = allHintKeys.length - 1; j >= 0; j--) { + key = allHintKeys[j]; + allHints[key][lang] = defaultHint[key]; + } } else { // make a copy of all hints, using the default hint value where a hint value is missing - hint = {}; - allHints[lang] = hint; h = tr.hint; - for (j = allHintKeys.length; j >= 0; j--) { + for (j = allHintKeys.length - 1; j >= 0; j--) { key = allHintKeys[j]; - hint[key] = h[key] || defaultHint[key]; + allHints[key][lang] = h[key] || defaultHint[key]; } } } @@ -64,9 +78,14 @@ // processed form, hint key -> translation language -> value // ================================================================================ + defaultPlanCondition = function (translation) { + // default plan must be non-empty + if (!translation || !(translation instanceof Array)) return false; + return translation.length > 0; + }, processPlans = function (rawTranslations) { // find the default plan translation - var defaultPlan = chooseDefaultTranslation(rawTranslations, 'plan') || [], + var defaultPlan = chooseDefaultTranslation(rawTranslations, 'plan', defaultPlanCondition) || [], allPlans = {}, // the result i, lang, tr; // create all translations for plan @@ -74,12 +93,12 @@ lang = langs[i]; tr = rawTranslations[lang]; // set up plan - if (!tr || !tr.plan) { - // there's no plan in the current language, copy the default plan - allPlans[lang] = defaultPlan; + if (tr && defaultPlanCondition(tr.plan)) { + allPlans[lang] = tr.plan; } else { - allPlans[lang] = tr.plan; + // there's no plan in the current language, copy the default plan + allPlans[lang] = defaultPlan; } } return allPlans; @@ -111,7 +130,7 @@ /** * convert the input translations (arg0) for given keys (arg1..argN) - * so each keys holds all its translations for ever language + * so each keys holds all its translations for every language */ convertTranslations = function () { var translations = arguments[0] || {}, @@ -187,7 +206,10 @@ 'language': languageIdentifier, // 'prolog', 'python', ... 'html': html.join(''), // the DOM structure (without textual content), as HTML text 'refs': problemReferences, // array of problem info {g: group, p: problem, id: problem_id}, referenced from DOM <a> elements - 'hints': processHints(rawTranslations) // hint translations: keyword -> lang -> value + 'commonDef': { + 'hint': processHints(rawTranslations), // hint translations: keyword -> lang -> value + 'hint_type': data.hint_type || {} + } }; }, @@ -218,7 +240,7 @@ .spread(function (userProblemData, generalProblemData) { if (userProblemData.code !== 0) throw new Error('Failed to obtain user problem data, code: ' + userProblemData.code + ', message: ' + userProblemData.message); if (!generalProblemData) throw new Error('General problem data is not defined'); - codeq.globalStateMachine.transition(language, generalProblemData, data.hints, userProblemData.solution); + codeq.globalStateMachine.transition(language, generalProblemData, data.commonDef, userProblemData.solution); }) ) .fail(function (reason) { @@ -233,14 +255,15 @@ // Problem definition processing // ================================================================================ - chooseDefaultTranslation = function (rawTranslations, translationKey) { + chooseDefaultTranslation = function (rawTranslations, translationKey, condition) { var tr = rawTranslations.en, // try English as the default - lang; - if (tr && tr[translationKey]) return tr[translationKey]; - for (lang in rawTranslations) { // find a translation with hints - if (!rawTranslations.hasOwnProperty(lang) || rawTranslations[lang]) continue; + lang, value; + if (typeof condition !== 'function') condition = function (x) { return !!x; }; + if (tr && condition(tr[translationKey])) return tr[translationKey]; + for (lang in rawTranslations) { // find a translation with content + if (!rawTranslations.hasOwnProperty(lang)) continue; tr = rawTranslations[lang]; - if (tr[translationKey]) return tr[translationKey]; + if (tr && condition(tr[translationKey])) return tr[translationKey]; } return null; // default must be chosen by the caller }, @@ -254,7 +277,8 @@ 'id': rawData.id, 'translations': convertTranslations(rawTranslations, 'title', 'name', 'slug', 'description'), // GUI translations: keyword -> lang -> value 'hint': processHints(rawTranslations), // hint translations: keyword -> lang -> value - 'plan': processPlans(rawTranslations) // plan translations: keyword -> lang -> value + 'plan': processPlans(rawTranslations), // plan translations: keyword -> lang -> value + 'hint_type': rawData.hint_type || {} }; }, diff --git a/js/codeq/prolog.js b/js/codeq/prolog.js index 87ac353..bcced1c 100644 --- a/js/codeq/prolog.js +++ b/js/codeq/prolog.js @@ -66,14 +66,14 @@ }; var prologHandler; //created when we enter the prolog state and destroyed once we leave it codeq.globalStateMachine.register('prolog', { - 'enter': function (problemDef, commonHints, currentSolution) { + 'enter': function (problemDef, commonDef, currentSolution) { $('#navigation-language').css('display', ''); $('#navigation-problem').css('display', ''); $("#navigation-prolog").addClass("active"); $('#navigation-prolog').css('display', ''); jqScreen.css('display', '');//we have to show the screen now so the code editor shows its initial values correctly - prologHandler = createPrologHandler(problemDef, commonHints, currentSolution); + prologHandler = createPrologHandler(problemDef, commonDef, currentSolution); subScreens = codeq.makeStateMachine(substates); subScreens.transition(jqDescription.data(stateNameTag)); /* Q.delay(100).then(function(){ @@ -253,7 +253,7 @@ * @param {PrologTaskDef} info * @returns {{destroy: Function, processServerHints: Function}} */ - createPrologHandler = function (problemDef, commonHints, currentSolution) { + createPrologHandler = function (problemDef, commonDef, currentSolution) { var //problem = info.problem, jqDescriptionContent = jqDescription.find('.description'), jqEditor = jqCode.find('.code_editor'), @@ -264,7 +264,7 @@ }), activityHandler = makeActivityHandler(editor, problemDef.id), terminal = makePrologTerminalHandler(jqTerminal, editor, problemDef.id, activityHandler), - hinter = codeq.makeHinter(jqHints, jqEditor, editor, 'prolog_hints', problemDef.hint, commonHints, problemDef.plan), + hinter = codeq.makeHinter(jqHints, jqEditor, editor, 'prolog_hints', problemDef, commonDef), commError = function (error) { alert(error); }; diff --git a/js/codeq/python.js b/js/codeq/python.js index e20fe06..8a9e92f 100644 --- a/js/codeq/python.js +++ b/js/codeq/python.js @@ -71,14 +71,14 @@ }; var pythonHandler; //created when we enter the python state and destroyed once we leave it codeq.globalStateMachine.register('python', { - 'enter': function (problemDef, commonHints, currentSolution) { + 'enter': function (problemDef, commonDef, currentSolution) { $('#navigation-language').css('display', ''); $('#navigation-problem').css('display', ''); $("#navigation-python").addClass("active"); $('#navigation-python').css('display', ''); jqScreen.css('display', '');//we have to show the screen now so the code editor shows its initial values correctly - pythonHandler = createPythonHandler(problemDef, commonHints, currentSolution); + pythonHandler = createPythonHandler(problemDef, commonDef, currentSolution); subScreens = codeq.makeStateMachine(substates); subScreens.transition(jqDescription.data(stateNameTag)); /* Q.delay(100).then(function(){ @@ -211,7 +211,7 @@ * @param {PrologTaskDef} info * @returns {{destroy: Function, processServerHints: Function}} */ - var createPythonHandler = function (problemDef, commonHints, currentSolution) { + var createPythonHandler = function (problemDef, commonDef, currentSolution) { var //problem = info.problem, jqDescriptionContent = jqDescription.find('.description'), jqEditor = jqCode.find('.code_editor'), @@ -223,7 +223,7 @@ }), activityHandler = makeActivityHandler(editor, problemDef.id), terminal = makePythonTerminalHandler(jqTerminal, editor, problemDef.id, activityHandler), - hinter = codeq.makeHinter(jqHints, jqEditor, editor, 'python_hints', problemDef.hint, commonHints, problemDef.plan), + hinter = codeq.makeHinter(jqHints, jqEditor, editor, 'python_hints', problemDef, commonDef), commError = function (error) { alert(error); }; |