Update Display initialization process

This commit is contained in:
toasted-nutbread 2019-10-12 14:20:54 -04:00
parent 89a8494208
commit 3e249e19ac
4 changed files with 71 additions and 26 deletions

View File

@ -33,6 +33,17 @@ class DisplaySearch extends Display {
this.introAnimationTimer = null; this.introAnimationTimer = null;
this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract}); this.dependencies = Object.assign({}, this.dependencies, {docRangeFromPoint, docSentenceExtract});
}
static create() {
const instance = new DisplaySearch();
instance.prepare();
return instance;
}
async prepare() {
try {
await this.initialize();
if (this.search !== null) { if (this.search !== null) {
this.search.addEventListener('click', (e) => this.onSearch(e), false); this.search.addEventListener('click', (e) => this.onSearch(e), false);
@ -50,6 +61,9 @@ class DisplaySearch extends Display {
} }
this.updateSearchButton(); this.updateSearchButton();
} catch (e) {
this.onError(e);
}
} }
onError(error) { onError(error) {
@ -89,7 +103,7 @@ class DisplaySearch extends Display {
this.updateSearchButton(); this.updateSearchButton();
if (valid) { if (valid) {
const {definitions} = await apiTermsFind(query, this.optionsContext); const {definitions} = await apiTermsFind(query, this.optionsContext);
this.termsShow(definitions, await apiOptionsGet(this.optionsContext)); this.termsShow(definitions, this.options);
} else { } else {
this.container.textContent = ''; this.container.textContent = '';
} }
@ -98,6 +112,10 @@ class DisplaySearch extends Display {
} }
} }
getOptionsContext() {
return this.optionsContext;
}
setIntroVisible(visible, animate) { setIntroVisible(visible, animate) {
if (this.introVisible === visible) { if (this.introVisible === visible) {
return; return;
@ -164,4 +182,4 @@ class DisplaySearch extends Display {
} }
} }
window.yomichan_search = new DisplaySearch(); window.yomichan_search = DisplaySearch.create();

View File

@ -84,6 +84,10 @@ class DisplayFloat extends Display {
super.onKeyDown(e); super.onKeyDown(e);
} }
getOptionsContext() {
return this.optionsContext;
}
autoPlayAudio() { autoPlayAudio() {
this.clearAutoPlayTimer(); this.clearAutoPlayTimer();
this.autoPlayAudioTimer = window.setTimeout(() => super.autoPlayAudio(), 400); this.autoPlayAudioTimer = window.setTimeout(() => super.autoPlayAudio(), 400);
@ -96,7 +100,9 @@ class DisplayFloat extends Display {
} }
} }
initialize(options, popupInfo, url, childrenSupported) { async initialize(options, popupInfo, url, childrenSupported) {
await super.initialize(options);
const css = options.general.customPopupCss; const css = options.general.customPopupCss;
if (css) { if (css) {
this.setStyle(css); this.setStyle(css);

View File

@ -61,11 +61,7 @@ class Popup {
const parentFrameId = (typeof this.frameId === 'number' ? this.frameId : null); const parentFrameId = (typeof this.frameId === 'number' ? this.frameId : null);
this.container.addEventListener('load', () => { this.container.addEventListener('load', () => {
this.invokeApi('initialize', { this.invokeApi('initialize', {
options: { options: options,
general: {
customPopupCss: options.general.customPopupCss
}
},
popupInfo: { popupInfo: {
id: this.id, id: this.id,
depth: this.depth, depth: this.depth,

View File

@ -29,7 +29,6 @@ class Display {
this.audioPlaying = null; this.audioPlaying = null;
this.audioFallback = null; this.audioFallback = null;
this.audioCache = {}; this.audioCache = {};
this.optionsContext = {};
this.eventListeners = []; this.eventListeners = [];
this.persistentEventListeners = []; this.persistentEventListeners = [];
@ -76,7 +75,7 @@ class Display {
context.source.source = this.context.source; context.source.source = this.context.source;
} }
const kanjiDefs = await apiKanjiFind(link.textContent, this.optionsContext); const kanjiDefs = await apiKanjiFind(link.textContent, this.getOptionsContext());
this.kanjiShow(kanjiDefs, this.options, context); this.kanjiShow(kanjiDefs, this.options, context);
} catch (e) { } catch (e) {
this.onError(e); this.onError(e);
@ -99,7 +98,7 @@ class Display {
try { try {
textSource.setEndOffset(this.options.scanning.length); textSource.setEndOffset(this.options.scanning.length);
({definitions, length} = await apiTermsFind(textSource.text(), this.optionsContext)); ({definitions, length} = await apiTermsFind(textSource.text(), this.getOptionsContext()));
if (definitions.length === 0) { if (definitions.length === 0) {
return false; return false;
} }
@ -175,6 +174,28 @@ class Display {
} }
} }
onRuntimeMessage({action, params}, sender, callback) {
const handlers = Display.runtimeMessageHandlers;
if (handlers.hasOwnProperty(action)) {
const handler = handlers[action];
handler(this, params);
callback();
}
}
getOptionsContext() {
throw new Error('Override me');
}
async initialize(options=null) {
await this.updateOptions(options);
chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this));
}
async updateOptions(options) {
this.options = options ? options : await apiOptionsGet(this.getOptionsContext());
}
setInteractive(interactive) { setInteractive(interactive) {
interactive = !!interactive; interactive = !!interactive;
if (this.interactive === interactive) { return; } if (this.interactive === interactive) { return; }
@ -314,7 +335,7 @@ class Display {
async adderButtonUpdate(modes, sequence) { async adderButtonUpdate(modes, sequence) {
try { try {
const states = await apiDefinitionsAddable(this.definitions, modes, this.optionsContext); const states = await apiDefinitionsAddable(this.definitions, modes, this.getOptionsContext());
if (!states || sequence !== this.sequence) { if (!states || sequence !== this.sequence) {
return; return;
} }
@ -416,7 +437,7 @@ class Display {
} }
} }
const noteId = await apiDefinitionAdd(definition, mode, context, this.optionsContext); const noteId = await apiDefinitionAdd(definition, mode, context, this.getOptionsContext());
if (noteId) { if (noteId) {
const index = this.definitions.indexOf(definition); const index = this.definitions.indexOf(definition);
const adderButton = this.adderButtonFind(index, mode); const adderButton = this.adderButtonFind(index, mode);
@ -446,7 +467,7 @@ class Display {
} }
const sources = this.options.audio.sources; const sources = this.options.audio.sources;
let {audio, source} = await audioGetFromSources(expression, sources, this.optionsContext, true, this.audioCache); let {audio, source} = await audioGetFromSources(expression, sources, this.getOptionsContext(), true, this.audioCache);
let info; let info;
if (audio === null) { if (audio === null) {
if (this.audioFallback === null) { if (this.audioFallback === null) {
@ -706,3 +727,7 @@ Display.onKeyDownHandlers = {
return false; return false;
} }
}; };
Display.runtimeMessageHandlers = {
optionsUpdate: (self) => self.updateOptions(null)
};