From bc5161dac9202ec66059f5f8eb54558e90a6e5fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Smodi=C5=A1?= Date: Thu, 1 Oct 2015 14:15:38 +0200 Subject: Bugfix: rebuild any popovers after the editor's DOM changes. --- js/codeq/hint.js | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/js/codeq/hint.js b/js/codeq/hint.js index a97c14b..fc2429a 100644 --- a/js/codeq/hint.js +++ b/js/codeq/hint.js @@ -10,9 +10,16 @@ codeq.makeHinter = function (jqHints, jqEditor, editor, trNamespace, problemDef, commonDef) { 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 + popoverCreators = [], // functions that rebuild popover handlers after the editor's DOM changes planIdx = 0, dictionary = [], jqHintsContainer = jqHints.parent(), + hintProblemDefs = problemDef.hint_type, + hintCommonDefs = commonDef.hint_type, + hintProblemTr = problemDef.hint, + hintCommonTr = commonDef.hint, + planDef = problemDef.plan, clearHints = function () { var i; @@ -20,6 +27,10 @@ hintCleaners[i](); } hintCleaners.length = 0; + for (i = popoverHintCleaners.length - 1; i >= 0; i--) { + popoverHintCleaners[i](); + } + popoverHintCleaners.length = 0; hintCounter = 0; }, @@ -240,7 +251,7 @@ mark = addMark(serverHint.start, serverHint.end), // add the mark langs = codeq.availableLangs, translations = {}, - lang, i, content, htmlPrefix; + lang, i, content, htmlPrefix, instFunc; // execute templates for all languages for (i = langs.length - 1; i >= 0; i--) { @@ -257,7 +268,7 @@ // 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 () { + instFunc = function () { var jqMark = jqEditor.find('.' + mark.className); jqMark.popover({ 'content': function () { @@ -270,8 +281,10 @@ 'container': 'body' }); // remove the popup on next hint pack - hintCleaners.push(function () { if (jqMark) { jqMark.popover('destroy'); jqMark = null; } }); + popoverHintCleaners.push(function () { if (jqMark) { jqMark.popover('destroy'); jqMark = null; } }); }; + popoverCreators.push(instFunc); + return instFunc; }, 'dropdown': function (type, template, serverHint) { @@ -305,15 +318,19 @@ hintCleaners.push(close); } + }, + + /** + * When the editor updates its DOM, we have to re-register any popup hints. + */ + onEditorUpdate = function () { + var i; + for (i = 0; i < popoverHintCleaners.length; i++) popoverHintCleaners[i](); + for (i = 0; i < popoverCreators.length; i++) popoverCreators[i](); }; codeq.tr.registerDictionary(trNamespace, dictionary); - - var hintProblemDefs = problemDef.hint_type, - hintCommonDefs = commonDef.hint_type, - hintProblemTr = problemDef.hint, - hintCommonTr = commonDef.hint, - planDef = problemDef.plan; + editor.on('update', onEditorUpdate); return { /** @@ -387,6 +404,7 @@ }, 'destroy': function () { + editor.off('update', onEditorUpdate); clearHints(); codeq.tr.unregisterDictionary(trNamespace); jqHints.empty(); -- cgit v1.2.1