From fca8ba12551433595062ef6fd541fbaffd408931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Smodi=C5=A1?= Date: Thu, 1 Oct 2015 13:25:29 +0200 Subject: Fix displaying popup hints. --- js/codeq/hint.js | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) (limited to 'js/codeq') diff --git a/js/codeq/hint.js b/js/codeq/hint.js index 6f54717..a97c14b 100644 --- a/js/codeq/hint.js +++ b/js/codeq/hint.js @@ -236,14 +236,42 @@ 'popup': function (type, template, serverHint) { codeq.log.debug('Processing popup hint'); - var message = processTemplate(template, serverHint.args), + var args = serverHint.args, mark = addMark(serverHint.start, serverHint.end), // add the mark - jqMark = jqEditor.find('.' + mark.className); + langs = codeq.availableLangs, + translations = {}, + lang, i, content, htmlPrefix; - 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; } }); - - mark.mark.on('', function () {}); + // execute templates for all languages + for (i = langs.length - 1; i >= 0; i--) { + lang = langs[i]; + content = template[lang]; + if (typeof content === 'string') { + translations[lang] = processTemplate(content, args); + } + else { + translations[lang] = 'No translation in ' + lang + ' available for ' + type + ' hint ' + serverHint.id; + codeq.log.error(translations[lang]); + } + } + // construct the wrapper element for the translation mechanism + htmlPrefix = ''; + // create the popover after all the DOM modifications have been made, otherwise only the last made popover can be triggered + return function () { + var jqMark = jqEditor.find('.' + mark.className); + jqMark.popover({ + 'content': function () { + // decide on what to display only after the popup is triggered, so we can choose the correct translation + return htmlPrefix + translations[codeq.getLang()] + ''; + }, + 'html': true, + 'placement': 'auto bottom', + 'trigger': 'hover focus click', + 'container': 'body' + }); + // remove the popup on next hint pack + hintCleaners.push(function () { if (jqMark) { jqMark.popover('destroy'); jqMark = null; } }); + }; }, 'dropdown': function (type, template, serverHint) { @@ -312,7 +340,8 @@ */ 'handle': function (serverHints) { var n = serverHints.length, - i, serverHint, hintId, hintDef, hintContent, hintType, t, fn, indices; + finalizers = [], + i, serverHint, hintId, hintDef, hintContent, hintType, t, fn, ret; clearHints(); mainLoop: @@ -346,7 +375,14 @@ fn = typeHandlers[hintType]; if (!fn) codeq.log.error('Unsupported hint type: ' + hintType); - else fn(hintType, hintContent, serverHint, hintId); + else { + ret = fn(hintType, hintContent, serverHint, hintId); + if (typeof ret === 'function') finalizers.push(ret); + } + } + // invoke any finalizers + for (i = 0; i < finalizers.length; i++) { + finalizers[i](); } }, -- cgit v1.2.1