This commit is contained in:
Alex Yatskov 2016-08-22 08:48:19 -07:00
parent a062b25178
commit 9621a0cd4b
2 changed files with 55 additions and 44 deletions

View File

@ -19,43 +19,48 @@
class Dictionary { class Dictionary {
constructor() { constructor() {
this.db = new Dexie('dict'); this.db = null;
this.dbVer = 1;
this.entities = null; this.entities = null;
} }
loadDb() { loadDb() {
return this.db.open().then((db) => { this.db = null;
if (db.verno !== this.dbVer) { this.entities = null;
Promise.reject('db version mismatch');
}
return db.verno; return new Dexie('dict').open().then((db) => {
this.db = db;
}); });
} }
initDb() { resetDb() {
this.entities = {}; this.db = null;
return this.db.version(this.dbVer).stores({ this.entities = null;
terms: 'expression, reading',
entities: 'name', return new Dexie('dict').delete().then(() => {
kanji: 'character', return Promise.resolve(new Dexie('dict'));
}).then((db) => {
this.db = db;
return this.db.version(1).stores({
terms: '++id, e, r',
entities: 'n',
kanji: 'c'
});
}); });
} }
importTermDict(dict) { importTermDict(dict) {
return this.db.terms.bulkAdd(dict.d).then(() => {
this.entities = {}; this.entities = {};
return this.db.terms.bulkAdd(dict.d, 'expression, reading, tags, glossary').then(() => { for (const name in dict.e) {
for (const [key, value] of dict.e) { this.entities[name] = dict.e[name];
this.entities[key] = value;
} }
return this.db.entities.bulkAdd(dict.e, 'name, value'); return this.db.entities.bulkAdd(dict.e);
}); });
} }
importKanjiDict(dict) { importKanjiDict(dict) {
return this.db.kanji.bulkAdd(dict.d, 'character, onyomi, kunyomi, tags, glossary'); return this.db.kanji.bulkAdd(dict.d);
} }
fetchEntities() { fetchEntities() {
@ -63,9 +68,11 @@ class Dictionary {
return Promise.resolve(this.entities); return Promise.resolve(this.entities);
} }
return this.db.entities.toArray((rows) => {
this.entities = {}; this.entities = {};
return this.db.entities.each((row) => { for (const row of rows) {
this.entities[row.name] = row.value; this.entities[row.name] = row.value;
}
}).then(() => { }).then(() => {
return Promise.resolve(this.entities); return Promise.resolve(this.entities);
}); });
@ -73,14 +80,14 @@ class Dictionary {
findterm(term) { findterm(term) {
const results = []; const results = [];
return this.db.terms.where('expression').equals(term).or('reading').equals(term).each((row) => { return this.db.terms.where('e').equals(term).or('r').equals(term).each((row) => {
results.push({ results.push({
expression: row.expression, expression: row.e,
reading: row.reading, reading: row.r,
tags: row.tags.split(' '), tags: row.t.split(' '),
glossary: row.glossary, glossary: row.g,
entities: this.entities, entities: this.entities,
id: results.length id: results.id
}); });
}).then(() => { }).then(() => {
Promise.resolve(results); Promise.resolve(results);
@ -89,13 +96,13 @@ class Dictionary {
findKanji(kanji) { findKanji(kanji) {
const results = []; const results = [];
return this.db.kanji.where('character').equals(kanji).each((row) => { return this.db.kanji.where('c').equals(kanji).each((row) => {
results.push({ results.push({
character: row.character, character: row.c,
onyomi: row.onyomi.split(' '), onyomi: row.o.split(' '),
kunyomi: row.kunyomi.split(' '), kunyomi: row.k.split(' '),
tags: row.tags.split(' '), tags: row.t.split(' '),
glossary: row.glossary glossary: row.m
}); });
}); });
} }

View File

@ -41,16 +41,20 @@ class Translator {
this.loaded = true; this.loaded = true;
callback(); callback();
}).catch(() => { }).catch(() => {
this.dictionary.initDb(); return this.dictionary.resetDb().then(() => {
return Translator.loadData('bg/data/edict.json'); return Translator.loadData('bg/data/edict.json');
}).then((response) => { }).then((response) => {
this.dictionary.importTermDict(JSON.parse(response)); return this.dictionary.importTermDict(JSON.parse(response));
}).then(() => {
return Translator.loadData('bg/data/enamdict.json'); return Translator.loadData('bg/data/enamdict.json');
}).then((response) => { }).then((response) => {
this.dictionary.importTermDict(JSON.parse(response)); return this.dictionary.importTermDict(JSON.parse(response));
}).then(() => {
return Translator.loadData('bg/data/kanjidic.json'); return Translator.loadData('bg/data/kanjidic.json');
}).then((response) => { }).then((response) => {
this.dictionary.importKanjiDict(JSON.parse(response)); return this.dictionary.importKanjiDict(JSON.parse(response));
});
}).then(() => {
this.loaded = true; this.loaded = true;
callback(); callback();
}); });