Refactor
This commit is contained in:
parent
aef347c67c
commit
44d2b5ec56
@ -19,13 +19,20 @@
|
|||||||
|
|
||||||
class Translator {
|
class Translator {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.loaded = false;
|
||||||
|
this.paths = {
|
||||||
|
rules: 'bg/data/rules.json',
|
||||||
|
edict: 'bg/data/edict.csv',
|
||||||
|
enamdict: 'bg/data/enamdict.csv',
|
||||||
|
kanjidic: 'bg/data/kanjidic.csv'
|
||||||
|
};
|
||||||
|
|
||||||
this.dictionary = new Dictionary();
|
this.dictionary = new Dictionary();
|
||||||
this.deinflector = new Deinflector();
|
this.deinflector = new Deinflector();
|
||||||
this.initialized = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadData(paths, callback) {
|
loadData(callback) {
|
||||||
if (this.initialized) {
|
if (this.loaded) {
|
||||||
callback();
|
callback();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -33,7 +40,7 @@ class Translator {
|
|||||||
const pendingLoads = [];
|
const pendingLoads = [];
|
||||||
for (const key of ['rules', 'edict', 'enamdict', 'kanjidic']) {
|
for (const key of ['rules', 'edict', 'enamdict', 'kanjidic']) {
|
||||||
pendingLoads.push(key);
|
pendingLoads.push(key);
|
||||||
Translator.loadData(paths[key], (response) => {
|
Translator.loadData(this.paths[key], (response) => {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case 'rules':
|
case 'rules':
|
||||||
this.deinflector.setRules(JSON.parse(response));
|
this.deinflector.setRules(JSON.parse(response));
|
||||||
@ -49,7 +56,7 @@ class Translator {
|
|||||||
|
|
||||||
pendingLoads.splice(pendingLoads.indexOf(key), 1);
|
pendingLoads.splice(pendingLoads.indexOf(key), 1);
|
||||||
if (pendingLoads.length === 0) {
|
if (pendingLoads.length === 0) {
|
||||||
this.initialized = true;
|
this.loaded = true;
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -19,20 +19,13 @@
|
|||||||
|
|
||||||
class Yomichan {
|
class Yomichan {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.res = {
|
Handlebars.partials = Handlebars.templates;
|
||||||
rules: 'bg/data/rules.json',
|
|
||||||
edict: 'bg/data/edict.csv',
|
|
||||||
enamdict: 'bg/data/enamdict.csv',
|
|
||||||
kanjidic: 'bg/data/kanjidic.csv'
|
|
||||||
};
|
|
||||||
|
|
||||||
this.translator = new Translator();
|
this.translator = new Translator();
|
||||||
this.updateState('disabled');
|
this.updateState('disabled');
|
||||||
|
|
||||||
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
|
chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
|
||||||
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
|
chrome.browserAction.onClicked.addListener(this.onBrowserAction.bind(this));
|
||||||
|
|
||||||
Handlebars.partials = Handlebars.templates;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessage(request, sender, callback) {
|
onMessage(request, sender, callback) {
|
||||||
@ -75,7 +68,7 @@ class Yomichan {
|
|||||||
break;
|
break;
|
||||||
case 'loading':
|
case 'loading':
|
||||||
chrome.browserAction.setBadgeText({text: '...'});
|
chrome.browserAction.setBadgeText({text: '...'});
|
||||||
this.translator.loadData(this.res, () => this.updateState('enabled'));
|
this.translator.loadData(() => this.updateState('enabled'));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user