aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-11-06 11:01:35 -0800
committerAlex Yatskov <alex@foosoft.net>2016-11-06 11:01:35 -0800
commit3f606a0ab00a084e7f22b232cdd5a7006ab7d636 (patch)
tree986300aaccef69f137a6395aa9dcc9b86798012d
parent84fd2d13f5d0f7321d0aa708902dd94cdbd3bac8 (diff)
Combined import
-rw-r--r--ext/bg/js/dictionary.js28
-rw-r--r--ext/bg/js/options-form.js2
-rw-r--r--ext/bg/js/translator.js8
-rw-r--r--ext/bg/js/util.js48
4 files changed, 47 insertions, 39 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js
index aa5f0b47..5859350b 100644
--- a/ext/bg/js/dictionary.js
+++ b/ext/bg/js/dictionary.js
@@ -20,7 +20,7 @@
class Dictionary {
constructor() {
this.db = null;
- this.dbVer = 4;
+ this.dbVer = 5;
this.entities = null;
}
@@ -131,7 +131,7 @@ class Dictionary {
});
}
- getDictionaries() {
+ getNames() {
if (this.db === null) {
return Promise.reject('database not initialized');
}
@@ -139,7 +139,7 @@ class Dictionary {
return this.db.dictionaries.toArray();
}
- importTermDict(indexUrl, callback) {
+ importDb(indexUrl, callback) {
if (this.db === null) {
return Promise.reject('database not initialized');
}
@@ -161,7 +161,7 @@ class Dictionary {
});
};
- const entriesLoaded = (dictionary, version, entries, total, current) => {
+ const termsLoaded = (dictionary, version, entries, total, current) => {
const rows = [];
for (const [expression, reading, tags, ...glossary] of entries) {
rows.push({
@@ -175,24 +175,12 @@ class Dictionary {
return this.db.terms.bulkAdd(rows).then(() => {
if (callback) {
- callback(current, total, indexUrl);
+ callback(total, current, indexUrl);
}
});
};
- return importJsonDb(indexUrl, indexLoaded, entriesLoaded);
- }
-
- importKanjiDict(indexUrl, callback) {
- if (this.db === null) {
- return Promise.reject('database not initialized');
- }
-
- const indexLoaded = (dictionary, version) => {
- return this.db.dictionaries.add({dictionary, version});
- };
-
- const entriesLoaded = (dictionary, version, entries, total, current) => {
+ const kanjiLoaded = (dictionary, version, entries, total, current) => {
const rows = [];
for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) {
rows.push({
@@ -207,11 +195,11 @@ class Dictionary {
return this.db.kanji.bulkAdd(rows).then(() => {
if (callback) {
- callback(current, total, indexUrl);
+ callback(total, current, indexUrl);
}
});
};
- return importJsonDb(indexUrl, indexLoaded, entriesLoaded);
+ return importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded);
}
}
diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js
index 5327a04a..060ee269 100644
--- a/ext/bg/js/options-form.js
+++ b/ext/bg/js/options-form.js
@@ -158,7 +158,7 @@ function populateDictionaries() {
// const dictGroups = $('.dictionaries');
// dictGroups.empty();
- // yomichan().translator.dictionary.getDictionaries().then(rows => {
+ // yomichan().translator.dictionary.getNames().then(rows => {
// for (const row of rows) {
// const dictPanel = $('<div>', {class: 'dictionary panel panel-default'});
// const dictRow = $('<div>', {class: 'panel-body row'}).appendTo(dictPanel);
diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js
index 44f37e31..8e0c522f 100644
--- a/ext/bg/js/translator.js
+++ b/ext/bg/js/translator.js
@@ -46,7 +46,7 @@ class Translator {
}
const banks = {};
- const bankCallback = (loaded, total, indexUrl) => {
+ const bankCallback = (total, loaded, indexUrl) => {
banks[indexUrl] = {loaded, total};
let percent = 0.0;
@@ -62,9 +62,9 @@ class Translator {
};
return Promise.all([
- this.dictionary.importTermDict('bg/data/edict/index.json', bankCallback),
- this.dictionary.importTermDict('bg/data/enamdict/index.json', bankCallback),
- this.dictionary.importKanjiDict('bg/data/kanjidic/index.json', bankCallback),
+ this.dictionary.importDb('bg/data/edict/index.json', bankCallback),
+ this.dictionary.importDb('bg/data/enamdict/index.json', bankCallback),
+ this.dictionary.importDb('bg/data/kanjidic/index.json', bankCallback),
]).then(() => {
return this.dictionary.sealDb();
}).then(() => {
diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js
index ac365135..eaa7dd01 100644
--- a/ext/bg/js/util.js
+++ b/ext/bg/js/util.js
@@ -43,15 +43,6 @@ function promiseCallback(promise, callback) {
});
}
-function loadJson(url) {
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest();
- xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText)));
- xhr.open('GET', chrome.extension.getURL(url));
- xhr.send();
- });
-}
-
function sortTags(tags) {
return tags.sort((v1, v2) => {
const order1 = v1.order;
@@ -117,19 +108,48 @@ function splitField(field) {
return field.length === 0 ? [] : field.split(' ');
}
-function importJsonDb(indexUrl, indexLoaded, entriesLoaded) {
+function loadJson(url) {
+ return new Promise((resolve, reject) => {
+ const xhr = new XMLHttpRequest();
+ xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText)));
+ xhr.open('GET', chrome.extension.getURL(url));
+ xhr.send();
+ });
+}
+
+function importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
return loadJson(indexUrl).then(index => {
if (indexLoaded !== null) {
- return indexLoaded(index.title, index.version, index.entities, index.banks).then(() => index);
+ return indexLoaded(index.title, index.version).then(() => index);
}
return index;
}).then(index => {
const loaders = [];
- for (let i = 1; i <= index.banks; ++i) {
- const bankUrl = `${indexDir}/bank_${i}.json`;
- loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(index.title, index.version, entries, index.banks, i)));
+ const banksTotal = index.termBanks + index.kanjiBanks;
+ let banksLoaded = 0;
+
+ for (let i = 1; i <= index.termBanks; ++i) {
+ const bankUrl = `${indexDir}/term_bank_${i}.json`;
+ loaders.push(() => loadJson(bankUrl).then(entries => termsLoaded(
+ index.title,
+ index.version,
+ entries,
+ banksTotal,
+ banksLoaded++
+ )));
+ }
+
+ for (let i = 1; i <= index.kanjiBanks; ++i) {
+ const bankUrl = `${indexDir}/kanji_bank_${i}.json`;
+ loaders.push(() => loadJson(bankUrl).then(entries => kanjiLoaded(
+ index.title,
+ index.version,
+ entries,
+ banksTotal,
+ banksLoaded++
+ )));
}
let chain = Promise.resolve();