From e358c94882ba4fea134ca0a21824055d09719143 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 15 Sep 2019 18:52:26 -0400 Subject: [PATCH] Clean up event listeners --- ext/mixed/js/display.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js index 2f7a694e..46016192 100644 --- a/ext/mixed/js/display.js +++ b/ext/mixed/js/display.js @@ -28,6 +28,7 @@ class Display { this.index = 0; this.audioCache = {}; this.optionsContext = {}; + this.eventListeners = []; this.dependencies = {}; @@ -283,6 +284,8 @@ class Display { async termsShow(definitions, options, context) { try { + this.clearEventListeners(); + if (!context || context.focus !== false) { window.focus(); } @@ -339,6 +342,8 @@ class Display { async kanjiShow(definitions, options, context) { try { + this.clearEventListeners(); + if (!context || context.focus !== false) { window.focus(); } @@ -610,8 +615,18 @@ class Display { return -1; } - addEventListeners(selector, ...args) { - this.container.querySelectorAll(selector).forEach((node) => node.addEventListener(...args)); + addEventListeners(selector, type, listener, options) { + this.container.querySelectorAll(selector).forEach((node) => { + node.addEventListener(type, listener, options); + this.eventListeners.push([node, type, listener, options]); + }); + } + + clearEventListeners() { + for (const [node, type, listener, options] of this.eventListeners) { + node.removeEventListener(type, listener, options); + } + this.eventListeners = []; } static getElementTop(element) {