Clean up event listeners

This commit is contained in:
toasted-nutbread 2019-09-15 18:52:26 -04:00
parent 1a7ad30111
commit e358c94882

View File

@ -28,6 +28,7 @@ class Display {
this.index = 0; this.index = 0;
this.audioCache = {}; this.audioCache = {};
this.optionsContext = {}; this.optionsContext = {};
this.eventListeners = [];
this.dependencies = {}; this.dependencies = {};
@ -283,6 +284,8 @@ class Display {
async termsShow(definitions, options, context) { async termsShow(definitions, options, context) {
try { try {
this.clearEventListeners();
if (!context || context.focus !== false) { if (!context || context.focus !== false) {
window.focus(); window.focus();
} }
@ -339,6 +342,8 @@ class Display {
async kanjiShow(definitions, options, context) { async kanjiShow(definitions, options, context) {
try { try {
this.clearEventListeners();
if (!context || context.focus !== false) { if (!context || context.focus !== false) {
window.focus(); window.focus();
} }
@ -610,8 +615,18 @@ class Display {
return -1; return -1;
} }
addEventListeners(selector, ...args) { addEventListeners(selector, type, listener, options) {
this.container.querySelectorAll(selector).forEach((node) => node.addEventListener(...args)); 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) { static getElementTop(element) {