This commit is contained in:
Alex Yatskov 2016-08-23 22:22:09 -07:00
parent 6366d9bd8e
commit 8b5f74f99b

View File

@ -27,13 +27,6 @@ class Dictionary {
return Dexie.exists('dict'); return Dexie.exists('dict');
} }
loadDb() {
this.db = null;
this.entities = null;
return this.initDb().open();
}
initDb() { initDb() {
this.db = new Dexie('dict'); this.db = new Dexie('dict');
this.db.version(1).stores({ this.db.version(1).stores({
@ -45,23 +38,11 @@ class Dictionary {
return this.db; return this.db;
} }
importKanjiDict(dict) { loadDb() {
return this.db.kanji.bulkAdd(dict.d); this.db = null;
} this.entities = null;
fetchEntities() { return this.initDb().open();
if (this.entities !== null) {
return Promise.resolve(this.entities);
}
return this.db.entities.toArray((rows) => {
this.entities = {};
for (const row of rows) {
this.entities[row.name] = row.value;
}
}).then(() => {
return Promise.resolve(this.entities);
});
} }
findTerm(term) { findTerm(term) {
@ -93,18 +74,24 @@ class Dictionary {
}); });
} }
// importTermDict(dict) { getEntities() {
// return this.db.terms.bulkAdd(dict.d).then(() => { if (this.entities !== null) {
// this.entities = {}; return Promise.resolve(this.entities);
// for (const name in dict.e) { }
// this.entities[name] = dict.e[name];
// }
// return this.db.entities.bulkAdd(dict.e); return this.db.entities.toArray((rows) => {
// }); this.entities = {};
// } for (const row of rows) {
this.entities[row.name] = row.value;
}
}).then(() => {
return Promise.resolve(this.entities);
});
}
importTermDict(indexUrl) { importTermDict(indexUrl) {
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
return Dictionary.loadJson(indexUrl).then((index) => { return Dictionary.loadJson(indexUrl).then((index) => {
const entities = []; const entities = [];
for (const [name, value] of index.ents) { for (const [name, value] of index.ents) {
@ -121,9 +108,7 @@ class Dictionary {
} }
}).then(() => { }).then(() => {
const loaders = []; const loaders = [];
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); for (let i = 0; i <= index.refs; ++i) {
for (let i = 0; i < index.refs; ++i) {
const refUrl = `${indexDir}/ref_${i}.json`; const refUrl = `${indexDir}/ref_${i}.json`;
loaders.push(() => { loaders.push(() => {
return Dictionary.loadJson(refUrl).then((refs) => { return Dictionary.loadJson(refUrl).then((refs) => {
@ -147,6 +132,34 @@ class Dictionary {
}); });
} }
importKanjiDict(indexUrl) {
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
return Dictionary.loadJson(indexUrl).then((index) => {
const loaders = [];
for (let i = 0; i <= index.refs; ++i) {
const refUrl = `${indexDir}/ref_${i}.json`;
loaders.push(() => {
return Dictionary.loadJson(refUrl).then((refs) => {
const rows = [];
for (const [character, onyomi, kunyomi, tags, ...glossary] of refs) {
rows.push({character, onyomi, kunyomi, tags, glossary});
}
return this.db.kanji.bulkAdd(rows);
});
});
}
let chain = Promise.resolve();
for (const loader of loaders) {
chain = chain.then(loader);
}
return chain;
});
}
static loadJson(url) { static loadJson(url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();