From a18f3be559b9a8298f93f04837d400a1fff0b2b9 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 7 Nov 2016 08:29:21 -0800 Subject: Renaming things --- ext/bg/background.html | 2 +- ext/bg/js/database.js | 219 ++++++++++++++++++++++++++++++++++++++++++++++ ext/bg/js/dictionary.js | 219 ---------------------------------------------- ext/bg/js/options-form.js | 4 +- ext/bg/js/translator.js | 18 ++-- 5 files changed, 231 insertions(+), 231 deletions(-) create mode 100644 ext/bg/js/database.js delete mode 100644 ext/bg/js/dictionary.js diff --git a/ext/bg/background.html b/ext/bg/background.html index 1bb36cf8..49fc6d0f 100644 --- a/ext/bg/background.html +++ b/ext/bg/background.html @@ -9,7 +9,7 @@ - + diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js new file mode 100644 index 00000000..19e3228f --- /dev/null +++ b/ext/bg/js/database.js @@ -0,0 +1,219 @@ +/* + * Copyright (C) 2016 Alex Yatskov + * Author: Alex Yatskov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +class Database { + constructor() { + this.db = null; + this.dbVer = 6; + this.entities = null; + } + + init() { + if (this.db !== null) { + return Promise.reject('database already initialized'); + } + + this.db = new Dexie('dict'); + this.db.version(1).stores({ + terms: '++id, dictionary, expression, reading', + kanji: '++, dictionary, character', + entities: '++, dictionary', + dictionaries: '++, title, version', + meta: 'name, value', + }); + } + + prepare() { + this.init(); + + return this.db.meta.get('version').then(row => { + return row ? row.value : 0; + }).catch(() => { + return 0; + }).then(version => { + if (this.dbVer === version) { + return true; + } + + const db = this.db; + this.db.close(); + this.db = null; + + return db.delete().then(() => { + this.init(); + return false; + }); + }); + } + + seal() { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + + return this.db.meta.put({name: 'version', value: this.dbVer}); + } + + 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({ + expression: row.expression, + reading: row.reading, + tags: splitField(row.tags), + glossary: row.glossary, + id: row.id + }); + }).then(() => { + return this.getEntities(); + }).then(entities => { + for (const result of results) { + result.entities = entities; + } + + return results; + }); + } + + 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({ + character: row.character, + onyomi: splitField(row.onyomi), + kunyomi: splitField(row.kunyomi), + tags: splitField(row.tags), + glossary: row.meanings + }); + }).then(() => results); + } + + getEntities(tags) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + + 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; + } + + return this.entities; + }); + } + + getDictionaries() { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + + return this.db.dictionaries.toArray(); + } + + deleteDictionary(title) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + + return this.db.terms.where('dictionary').equals(title).delete().then(() => { + return this.db.kanji.where('dictionary').equals(title).delete(); + }).then(() => { + return this.db.entities.where('dictionary').equals(title).delete(); + }).then(() => { + return this.db.dictionaries.where('title').equals(title).delete(); + }); + } + + importDictionary(indexUrl, callback) { + if (this.db === null) { + return Promise.reject('database not initialized'); + } + + const indexLoaded = (title, version, entities, hasTerms, hasKanji) => { + return this.db.dictionaries.add({title, version, hasTerms, hasKanji}).then(() => { + this.entities = entities || {}; + + const rows = []; + for (const name in entities || {}) { + rows.push({ + name, + value: entities[name], + dictionary: title + }); + } + + return this.db.entities.bulkAdd(rows); + }); + }; + + const termsLoaded = (title, entries, total, current) => { + const rows = []; + for (const [expression, reading, tags, ...glossary] of entries) { + rows.push({ + expression, + reading, + tags, + glossary, + dictionary: title + }); + } + + return this.db.terms.bulkAdd(rows).then(() => { + if (callback) { + callback(total, current, indexUrl); + } + }); + }; + + const kanjiLoaded = (title, entries, total, current) => { + const rows = []; + for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { + rows.push({ + character, + onyomi, + kunyomi, + tags, + meanings, + dictionary: title + }); + } + + return this.db.kanji.bulkAdd(rows).then(() => { + if (callback) { + callback(total, current, indexUrl); + } + }); + }; + + return importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded); + } +} diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js deleted file mode 100644 index a7bca2f1..00000000 --- a/ext/bg/js/dictionary.js +++ /dev/null @@ -1,219 +0,0 @@ -/* - * Copyright (C) 2016 Alex Yatskov - * Author: Alex Yatskov - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -class Dictionary { - constructor() { - this.db = null; - this.dbVer = 6; - this.entities = null; - } - - initDb() { - if (this.db !== null) { - return Promise.reject('database already initialized'); - } - - this.db = new Dexie('dict'); - this.db.version(1).stores({ - terms: '++id, dictionary, expression, reading', - kanji: '++, dictionary, character', - entities: '++, dictionary', - dictionaries: '++, title, version', - meta: 'name, value', - }); - } - - prepareDb() { - this.initDb(); - - return this.db.meta.get('version').then(row => { - return row ? row.value : 0; - }).catch(() => { - return 0; - }).then(version => { - if (this.dbVer === version) { - return true; - } - - const db = this.db; - this.db.close(); - this.db = null; - - return db.delete().then(() => { - this.initDb(); - return false; - }); - }); - } - - sealDb() { - if (this.db === null) { - return Promise.reject('database not initialized'); - } - - return this.db.meta.put({name: 'version', value: this.dbVer}); - } - - 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({ - expression: row.expression, - reading: row.reading, - tags: splitField(row.tags), - glossary: row.glossary, - id: row.id - }); - }).then(() => { - return this.getEntities(); - }).then(entities => { - for (const result of results) { - result.entities = entities; - } - - return results; - }); - } - - 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({ - character: row.character, - onyomi: splitField(row.onyomi), - kunyomi: splitField(row.kunyomi), - tags: splitField(row.tags), - glossary: row.meanings - }); - }).then(() => results); - } - - getEntities(tags) { - if (this.db === null) { - return Promise.reject('database not initialized'); - } - - 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; - } - - return this.entities; - }); - } - - getDictionaries() { - if (this.db === null) { - return Promise.reject('database not initialized'); - } - - return this.db.dictionaries.toArray(); - } - - deleteDictionary(title) { - if (this.db === null) { - return Promise.reject('database not initialized'); - } - - return this.db.terms.where('dictionary').equals(title).delete().then(() => { - return this.db.kanji.where('dictionary').equals(title).delete(); - }).then(() => { - return this.db.entities.where('dictionary').equals(title).delete(); - }).then(() => { - return this.db.dictionaries.where('title').equals(title).delete(); - }); - } - - importDictionary(indexUrl, callback) { - if (this.db === null) { - return Promise.reject('database not initialized'); - } - - const indexLoaded = (title, version, entities, hasTerms, hasKanji) => { - return this.db.dictionaries.add({title, version, hasTerms, hasKanji}).then(() => { - this.entities = entities || {}; - - const rows = []; - for (const name in entities || {}) { - rows.push({ - name, - value: entities[name], - dictionary: title - }); - } - - return this.db.entities.bulkAdd(rows); - }); - }; - - const termsLoaded = (title, entries, total, current) => { - const rows = []; - for (const [expression, reading, tags, ...glossary] of entries) { - rows.push({ - expression, - reading, - tags, - glossary, - dictionary: title - }); - } - - return this.db.terms.bulkAdd(rows).then(() => { - if (callback) { - callback(total, current, indexUrl); - } - }); - }; - - const kanjiLoaded = (title, entries, total, current) => { - const rows = []; - for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { - rows.push({ - character, - onyomi, - kunyomi, - tags, - meanings, - dictionary: title - }); - } - - return this.db.kanji.bulkAdd(rows).then(() => { - if (callback) { - callback(total, current, indexUrl); - } - }); - }; - - return importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded); - } -} diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 2f2fe6fd..14143d81 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -166,7 +166,7 @@ function populateDictionaries(opts) { const container = $('.dicts'); container.empty(); - yomichan().translator.dictionary.getDictionaries().then(rows => { + yomichan().translator.database.getDictionaries().then(rows => { rows.forEach(row => { const dictOpts = opts.dictionaries[row.title] || {enableTerms: true, enableKanji: false}; const html = Handlebars.templates['dictionary.html']({ @@ -184,7 +184,7 @@ function populateDictionaries(opts) { $('.dict-delete').click(e => { const dict = $(e.target).closest('.dict'); const title = dict.data('title'); - yomichan().translator.dictionary.deleteDictionary(title); + yomichan().translator.database.deleteDictionary(title); dict.slideUp(); }); diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 0a0a6109..e65acd60 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -21,7 +21,7 @@ class Translator { constructor() { this.loaded = false; this.tagMeta = null; - this.dictionary = new Dictionary(); + this.database = new Database(); this.deinflector = new Deinflector(); } @@ -35,7 +35,7 @@ class Translator { return loadJson('bg/data/tags.json'); }).then(tagMeta => { this.tagMeta = tagMeta; - return this.dictionary.prepareDb(); + return this.database.prepare(); }).then(exists => { if (exists) { return; @@ -62,11 +62,11 @@ class Translator { }; return Promise.all([ - this.dictionary.importDictionary('bg/data/edict/index.json', bankCallback), - this.dictionary.importDictionary('bg/data/enamdict/index.json', bankCallback), - this.dictionary.importDictionary('bg/data/kanjidic/index.json', bankCallback), + this.database.importDictionary('bg/data/edict/index.json', bankCallback), + this.database.importDictionary('bg/data/enamdict/index.json', bankCallback), + this.database.importDictionary('bg/data/kanjidic/index.json', bankCallback), ]).then(() => { - return this.dictionary.sealDb(); + return this.database.seal(); }).then(() => { if (callback) { callback({state: 'end', progress: 100.0}); @@ -84,7 +84,7 @@ class Translator { for (let i = text.length; i > 0; --i) { deinflectPromises.push( this.deinflector.deinflect(text.slice(0, i), term => { - return this.dictionary.findTerm(term).then(definitions => definitions.map(definition => definition.tags)); + return this.database.findTerm(term).then(definitions => definitions.map(definition => definition.tags)); }).then(deinflects => { const processPromises = []; for (const deinflect of deinflects) { @@ -143,7 +143,7 @@ class Translator { for (const c of text) { if (!processed[c]) { - promises.push(this.dictionary.findKanji(c).then((definitions) => definitions)); + promises.push(this.database.findKanji(c).then((definitions) => definitions)); processed[c] = true; } } @@ -152,7 +152,7 @@ class Translator { } processTerm(groups, source, tags, rules, root) { - return this.dictionary.findTerm(root).then(definitions => { + return this.database.findTerm(root).then(definitions => { for (const definition of definitions) { if (definition.id in groups) { continue; -- cgit v1.2.3