Add function for (de)activating event listeners in Display
This commit is contained in:
parent
cc72514ce6
commit
89a8494208
@ -30,14 +30,17 @@ class Display {
|
|||||||
this.audioFallback = null;
|
this.audioFallback = null;
|
||||||
this.audioCache = {};
|
this.audioCache = {};
|
||||||
this.optionsContext = {};
|
this.optionsContext = {};
|
||||||
|
|
||||||
this.eventListeners = [];
|
this.eventListeners = [];
|
||||||
|
this.persistentEventListeners = [];
|
||||||
|
this.interactive = false;
|
||||||
|
this.eventListenersActive = false;
|
||||||
|
|
||||||
this.dependencies = {};
|
this.dependencies = {};
|
||||||
|
|
||||||
this.windowScroll = new WindowScroll();
|
this.windowScroll = new WindowScroll();
|
||||||
|
|
||||||
document.addEventListener('keydown', this.onKeyDown.bind(this));
|
this.setInteractive(true);
|
||||||
document.addEventListener('wheel', this.onWheel.bind(this), {passive: false});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onError(error) {
|
onError(error) {
|
||||||
@ -172,9 +175,48 @@ class Display {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setInteractive(interactive) {
|
||||||
|
interactive = !!interactive;
|
||||||
|
if (this.interactive === interactive) { return; }
|
||||||
|
this.interactive = interactive;
|
||||||
|
|
||||||
|
if (interactive) {
|
||||||
|
Display.addEventListener(this.persistentEventListeners, document, 'keydown', this.onKeyDown.bind(this), false);
|
||||||
|
Display.addEventListener(this.persistentEventListeners, document, 'wheel', this.onWheel.bind(this), {passive: false});
|
||||||
|
} else {
|
||||||
|
Display.clearEventListeners(this.persistentEventListeners);
|
||||||
|
}
|
||||||
|
this.setEventListenersActive(this.eventListenersActive);
|
||||||
|
}
|
||||||
|
|
||||||
|
setEventListenersActive(active) {
|
||||||
|
active = !!active && this.interactive;
|
||||||
|
if (this.eventListenersActive === active) { return; }
|
||||||
|
this.eventListenersActive = active;
|
||||||
|
|
||||||
|
if (active) {
|
||||||
|
this.addEventListeners('.action-add-note', 'click', this.onNoteAdd.bind(this));
|
||||||
|
this.addEventListeners('.action-view-note', 'click', this.onNoteView.bind(this));
|
||||||
|
this.addEventListeners('.action-play-audio', 'click', this.onAudioPlay.bind(this));
|
||||||
|
this.addEventListeners('.kanji-link', 'click', this.onKanjiLookup.bind(this));
|
||||||
|
this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this));
|
||||||
|
if (this.options.scanning.enablePopupSearch) {
|
||||||
|
this.addEventListeners('.glossary-item', 'click', this.onTermLookup.bind(this));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Display.clearEventListeners(this.eventListeners);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addEventListeners(selector, type, listener, options) {
|
||||||
|
this.container.querySelectorAll(selector).forEach((node) => {
|
||||||
|
Display.addEventListener(this.eventListeners, node, type, listener, options);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async termsShow(definitions, options, context) {
|
async termsShow(definitions, options, context) {
|
||||||
try {
|
try {
|
||||||
this.clearEventListeners();
|
this.setEventListenersActive(false);
|
||||||
|
|
||||||
if (!context || context.focus !== false) {
|
if (!context || context.focus !== false) {
|
||||||
window.focus();
|
window.focus();
|
||||||
@ -215,14 +257,7 @@ class Display {
|
|||||||
this.autoPlayAudio();
|
this.autoPlayAudio();
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addEventListeners('.action-add-note', 'click', this.onNoteAdd.bind(this));
|
this.setEventListenersActive(true);
|
||||||
this.addEventListeners('.action-view-note', 'click', this.onNoteView.bind(this));
|
|
||||||
this.addEventListeners('.action-play-audio', 'click', this.onAudioPlay.bind(this));
|
|
||||||
this.addEventListeners('.kanji-link', 'click', this.onKanjiLookup.bind(this));
|
|
||||||
this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this));
|
|
||||||
if (this.options.scanning.enablePopupSearch) {
|
|
||||||
this.addEventListeners('.glossary-item', 'click', this.onTermLookup.bind(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.adderButtonUpdate(['term-kanji', 'term-kana'], sequence);
|
await this.adderButtonUpdate(['term-kanji', 'term-kana'], sequence);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -232,7 +267,7 @@ class Display {
|
|||||||
|
|
||||||
async kanjiShow(definitions, options, context) {
|
async kanjiShow(definitions, options, context) {
|
||||||
try {
|
try {
|
||||||
this.clearEventListeners();
|
this.setEventListenersActive(false);
|
||||||
|
|
||||||
if (!context || context.focus !== false) {
|
if (!context || context.focus !== false) {
|
||||||
window.focus();
|
window.focus();
|
||||||
@ -265,9 +300,7 @@ class Display {
|
|||||||
const {index, scroll} = context || {};
|
const {index, scroll} = context || {};
|
||||||
this.entryScrollIntoView(index || 0, scroll);
|
this.entryScrollIntoView(index || 0, scroll);
|
||||||
|
|
||||||
this.addEventListeners('.action-add-note', 'click', this.onNoteAdd.bind(this));
|
this.setEventListenersActive(true);
|
||||||
this.addEventListeners('.action-view-note', 'click', this.onNoteView.bind(this));
|
|
||||||
this.addEventListeners('.source-term', 'click', this.onSourceTermView.bind(this));
|
|
||||||
|
|
||||||
await this.adderButtonUpdate(['kanji'], sequence);
|
await this.adderButtonUpdate(['kanji'], sequence);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -544,18 +577,16 @@ class Display {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
addEventListeners(selector, type, listener, options) {
|
static addEventListener(eventListeners, object, type, listener, options) {
|
||||||
this.container.querySelectorAll(selector).forEach((node) => {
|
object.addEventListener(type, listener, options);
|
||||||
node.addEventListener(type, listener, options);
|
eventListeners.push([object, type, listener, options]);
|
||||||
this.eventListeners.push([node, type, listener, options]);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clearEventListeners() {
|
static clearEventListeners(eventListeners) {
|
||||||
for (const [node, type, listener, options] of this.eventListeners) {
|
for (const [object, type, listener, options] of eventListeners) {
|
||||||
node.removeEventListener(type, listener, options);
|
object.removeEventListener(type, listener, options);
|
||||||
}
|
}
|
||||||
this.eventListeners = [];
|
eventListeners.length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static getElementTop(element) {
|
static getElementTop(element) {
|
||||||
|
Loading…
Reference in New Issue
Block a user