diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-09-11 22:47:08 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-09-11 22:47:08 -0700 |
commit | 4241b4f8595813b966371a02f5b2f35d7997b1e2 (patch) | |
tree | 00028af8e4ce3bd59a5b8c5b75947a92f6bb58e1 /ext/bg/js/dictionary.js | |
parent | f4314497e401e94ccd2ff88358b5720c73074612 (diff) |
WIP
Diffstat (limited to 'ext/bg/js/dictionary.js')
-rw-r--r-- | ext/bg/js/dictionary.js | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js index 0c5e4c4a..20a94f8f 100644 --- a/ext/bg/js/dictionary.js +++ b/ext/bg/js/dictionary.js @@ -19,24 +19,34 @@ class Dictionary { constructor() { + this.db = null; this.entities = null; - this.db = new Dexie('dict'); - } - - existsDb() { - return Dexie.exists('dict'); } initDb() { - this.entities = null; + this.db = new Dexie('dict'); this.db.version(1).stores({ terms: '++id,expression,reading', entities: '++,name', kanji: '++,character' }); + + this.entities = null; + } + + deleteDb() { + return this.db === null ? Promise.resolve() : this.db.delete(); + } + + existsDb() { + return Dexie.exists('dict'); } findTerm(term) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + const results = []; return this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { results.push({ @@ -58,6 +68,10 @@ class Dictionary { } findKanji(kanji) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + const results = []; return this.db.kanji.where('character').equals(kanji).each(row => { results.push({ @@ -71,8 +85,12 @@ class Dictionary { } getEntities(tags) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + if (this.entities !== null) { - return this.entities; + return Promise.resolve(this.entities); } return this.db.entities.toArray(rows => { @@ -86,8 +104,11 @@ class Dictionary { } importTermDict(indexUrl) { - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); + if (this.db === null) { + return Promise.reject('database not initialized'); + } + const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); return loadJson(indexUrl).then(index => { const entities = []; for (const [name, value] of index.ents) { @@ -129,8 +150,11 @@ class Dictionary { } importKanjiDict(indexUrl) { - const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); + if (this.db === null) { + return Promise.reject('database not initialized'); + } + const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); return loadJson(indexUrl).then(index => { const loaders = []; for (let i = 1; i <= index.banks; ++i) { |