summaryrefslogtreecommitdiff
path: root/ext/bg/js/dictionary.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-08-21 19:51:12 -0700
committerAlex Yatskov <alex@foosoft.net>2016-08-21 19:51:12 -0700
commita062b25178dd8d916a8cdf87cffa36b5365ab021 (patch)
treef86cf6bfd68aee3814a1e9519839204d00311bc7 /ext/bg/js/dictionary.js
parent67f906ab24acb80a8ffbad29ff8ddda5fc570cf0 (diff)
Work on DB integration
Diffstat (limited to 'ext/bg/js/dictionary.js')
-rw-r--r--ext/bg/js/dictionary.js99
1 files changed, 54 insertions, 45 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js
index 082d1479..7b885db2 100644
--- a/ext/bg/js/dictionary.js
+++ b/ext/bg/js/dictionary.js
@@ -19,10 +19,9 @@
class Dictionary {
constructor() {
- this.termDicts = {};
- this.kanjiDicts = {};
this.db = new Dexie('dict');
this.dbVer = 1;
+ this.entities = null;
}
loadDb() {
@@ -35,59 +34,69 @@ class Dictionary {
});
}
- importTermDict(name, dict) {
- this.termDicts[name] = dict;
+ initDb() {
+ this.entities = {};
+ return this.db.version(this.dbVer).stores({
+ terms: 'expression, reading',
+ entities: 'name',
+ kanji: 'character',
+ });
}
- importKanjiDict(name, dict) {
- this.kanjiDicts[name] = dict;
- }
+ importTermDict(dict) {
+ this.entities = {};
+ return this.db.terms.bulkAdd(dict.d, 'expression, reading, tags, glossary').then(() => {
+ for (const [key, value] of dict.e) {
+ this.entities[key] = value;
+ }
- findTerm(term) {
- let results = [];
+ return this.db.entities.bulkAdd(dict.e, 'name, value');
+ });
+ }
- for (const name in this.termDicts) {
- const dict = this.termDicts[name];
- if (!(term in dict.i)) {
- continue;
- }
+ importKanjiDict(dict) {
+ return this.db.kanji.bulkAdd(dict.d, 'character, onyomi, kunyomi, tags, glossary');
+ }
- const indices = dict.i[term].split(' ').map(Number);
- results = results.concat(
- indices.map(index => {
- const [e, r, t, ...g] = dict.d[index];
- return {
- expression: e,
- reading: r,
- tags: t.split(' '),
- glossary: g,
- entities: dict.e,
- id: index
- };
- })
- );
+ fetchEntities() {
+ if (this.entities !== null) {
+ return Promise.resolve(this.entities);
}
- return results;
+ this.entities = {};
+ return this.db.entities.each((row) => {
+ this.entities[row.name] = row.value;
+ }).then(() => {
+ return Promise.resolve(this.entities);
+ });
}
- findKanji(kanji) {
+ findterm(term) {
const results = [];
+ return this.db.terms.where('expression').equals(term).or('reading').equals(term).each((row) => {
+ results.push({
+ expression: row.expression,
+ reading: row.reading,
+ tags: row.tags.split(' '),
+ glossary: row.glossary,
+ entities: this.entities,
+ id: results.length
+ });
+ }).then(() => {
+ Promise.resolve(results);
+ });
+ }
- for (const name in this.kanjiDicts) {
- const def = this.kanjiDicts[name].c[kanji];
- if (def) {
- const [k, o, t, ...g] = def;
- results.push({
- character: kanji,
- kunyomi: k.split(' '),
- onyomi: o.split(' '),
- tags: t.split(' '),
- glossary: g
- });
- }
- }
-
- return results;
+ findKanji(kanji) {
+ const results = [];
+ return this.db.kanji.where('character').equals(kanji).each((row) => {
+ results.push({
+ character: row.character,
+ onyomi: row.onyomi.split(' '),
+ kunyomi: row.kunyomi.split(' '),
+ tags: row.tags.split(' '),
+ glossary: row.glossary
+ });
+ });
}
}