diff --git a/client/js/views/auto_complete_control.js b/client/js/views/auto_complete_control.js index 6c8bc5a..5300412 100644 --- a/client/js/views/auto_complete_control.js +++ b/client/js/views/auto_complete_control.js @@ -61,9 +61,27 @@ class AutoCompleteControl { this.results = []; this.activeResult = -1; + this.mutationObserver = new MutationObserver( + mutations => { + for (let mutation of mutations) { + for (let node of mutation.removedNodes) { + if (node.contains(input)) { + this.uninstall(); + return; + } + } + } + }); + this.install(); } + uninstall() { + window.clearTimeout(this.showTimeout); + this.mutationObserver.disconnect(); + document.body.removeChild(this.suggestionDiv); + } + install() { if (!this.input) { throw new Error('Input element was not found'); @@ -75,6 +93,9 @@ class AutoCompleteControl { this.input.setAttribute('data-autocomplete', true); this.input.setAttribute('autocomplete', 'off'); + this.mutationObserver.observe( + document.body, {childList: true, subtree: true}); + this.input.addEventListener( 'keydown', e => { @@ -154,9 +175,7 @@ class AutoCompleteControl { } hide() { - if (this.showTimeout) { - window.clearTimeout(this.showTimeout); - } + window.clearTimeout(this.showTimeout); this.suggestionDiv.style.display = 'none'; this.isVisible = false; }