yomichan/ext/bg/js/dictionary.js

110 lines
3.0 KiB
JavaScript
Raw Normal View History

2016-03-20 02:32:35 +00:00
/*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
class Dictionary {
2016-03-21 00:15:40 +00:00
constructor() {
2016-08-22 15:48:19 +00:00
this.db = null;
2016-08-22 02:51:12 +00:00
this.entities = null;
2016-03-21 00:15:40 +00:00
}
2016-08-21 20:32:36 +00:00
loadDb() {
2016-08-22 15:48:19 +00:00
this.db = null;
this.entities = null;
2016-08-21 20:32:36 +00:00
2016-08-22 15:48:19 +00:00
return new Dexie('dict').open().then((db) => {
this.db = db;
2016-08-21 20:32:36 +00:00
});
}
2016-08-22 15:48:19 +00:00
resetDb() {
this.db = null;
this.entities = null;
return new Dexie('dict').delete().then(() => {
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'
});
2016-08-22 02:51:12 +00:00
});
2016-03-21 00:15:40 +00:00
}
2016-08-22 02:51:12 +00:00
importTermDict(dict) {
2016-08-22 15:48:19 +00:00
return this.db.terms.bulkAdd(dict.d).then(() => {
this.entities = {};
for (const name in dict.e) {
this.entities[name] = dict.e[name];
2016-08-22 02:51:12 +00:00
}
2016-03-21 00:15:40 +00:00
2016-08-22 15:48:19 +00:00
return this.db.entities.bulkAdd(dict.e);
2016-08-22 02:51:12 +00:00
});
}
2016-08-22 02:51:12 +00:00
importKanjiDict(dict) {
2016-08-22 15:48:19 +00:00
return this.db.kanji.bulkAdd(dict.d);
2016-08-22 02:51:12 +00:00
}
2016-08-22 02:51:12 +00:00
fetchEntities() {
if (this.entities !== null) {
return Promise.resolve(this.entities);
}
2016-08-22 15:48:19 +00:00
return this.db.entities.toArray((rows) => {
this.entities = {};
for (const row of rows) {
this.entities[row.name] = row.value;
}
2016-08-22 02:51:12 +00:00
}).then(() => {
return Promise.resolve(this.entities);
});
2016-03-21 00:15:40 +00:00
}
2016-08-22 02:51:12 +00:00
findterm(term) {
const results = [];
2016-08-22 15:48:19 +00:00
return this.db.terms.where('e').equals(term).or('r').equals(term).each((row) => {
2016-08-22 02:51:12 +00:00
results.push({
2016-08-22 15:48:19 +00:00
expression: row.e,
reading: row.r,
tags: row.t.split(' '),
glossary: row.g,
2016-08-22 02:51:12 +00:00
entities: this.entities,
2016-08-22 15:48:19 +00:00
id: results.id
2016-08-22 02:51:12 +00:00
});
}).then(() => {
Promise.resolve(results);
});
}
2016-04-13 06:12:20 +00:00
2016-08-22 02:51:12 +00:00
findKanji(kanji) {
const results = [];
2016-08-22 15:48:19 +00:00
return this.db.kanji.where('c').equals(kanji).each((row) => {
2016-08-22 02:51:12 +00:00
results.push({
2016-08-22 15:48:19 +00:00
character: row.c,
onyomi: row.o.split(' '),
kunyomi: row.k.split(' '),
tags: row.t.split(' '),
glossary: row.m
2016-08-22 02:51:12 +00:00
});
});
}
2016-03-20 02:32:35 +00:00
}