This commit is contained in:
Alex Yatskov 2016-11-05 17:30:00 -07:00
parent e4fa25894f
commit e2f1560afa
2 changed files with 33 additions and 16 deletions

View File

@ -20,7 +20,7 @@
class Dictionary { class Dictionary {
constructor() { constructor() {
this.db = null; this.db = null;
this.dbVer = 3; this.dbVer = 4;
this.entities = null; this.entities = null;
} }
@ -31,10 +31,10 @@ class Dictionary {
this.db = new Dexie('dict'); this.db = new Dexie('dict');
this.db.version(1).stores({ this.db.version(1).stores({
terms: '++id,expression,reading', terms: '++id, dictionary, expression, reading',
entities: '++,name', kanji: '++, dictionary, character',
kanji: '++,character', entities: '++, dictionary, name',
meta: 'name,value', meta: 'name, value',
}); });
} }
@ -135,21 +135,31 @@ class Dictionary {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
const entitiesLoaded = entities => { const indexLoaded = (dictionary, entities) => {
this.entities = entities || {}; this.entities = entities || {};
const rows = []; const rows = [];
for (const name in entities || {}) { for (const name in entities || {}) {
rows.push({name, value: entities[name]}); rows.push({
dictionary,
name,
value: entities[name]
});
} }
return this.db.entities.bulkAdd(rows); return this.db.entities.bulkAdd(rows);
}; };
const entriesLoaded = (entries, total, current) => { const entriesLoaded = (dictionary, entries, total, current) => {
const rows = []; const rows = [];
for (const [expression, reading, tags, ...glossary] of entries) { for (const [expression, reading, tags, ...glossary] of entries) {
rows.push({expression, reading, tags, glossary}); rows.push({
dictionary,
expression,
reading,
tags,
glossary
});
} }
return this.db.terms.bulkAdd(rows).then(() => { return this.db.terms.bulkAdd(rows).then(() => {
@ -159,7 +169,7 @@ class Dictionary {
}); });
}; };
return importJsonDb(indexUrl, entitiesLoaded, entriesLoaded); return importJsonDb(indexUrl, indexLoaded, entriesLoaded);
} }
importKanjiDict(indexUrl, callback) { importKanjiDict(indexUrl, callback) {
@ -167,10 +177,17 @@ class Dictionary {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
const entriesLoaded = (entries, total, current) => { const entriesLoaded = (dictionary, entries, total, current) => {
const rows = []; const rows = [];
for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) { for (const [character, onyomi, kunyomi, tags, ...meanings] of entries) {
rows.push({character, onyomi, kunyomi, tags, meanings}); rows.push({
dictionary,
character,
onyomi,
kunyomi,
tags,
meanings
});
} }
return this.db.kanji.bulkAdd(rows).then(() => { return this.db.kanji.bulkAdd(rows).then(() => {

View File

@ -117,11 +117,11 @@ function splitField(field) {
return field.length === 0 ? [] : field.split(' '); return field.length === 0 ? [] : field.split(' ');
} }
function importJsonDb(indexUrl, entitiesLoaded, entriesLoaded) { function importJsonDb(indexUrl, indexLoaded, entriesLoaded) {
const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/'));
return loadJson(indexUrl).then(index => { return loadJson(indexUrl).then(index => {
if (entitiesLoaded !== null) { if (indexLoaded !== null) {
return entitiesLoaded(index.entities, index.banks).then(() => index); return indexLoaded(index.title, index.entities, index.banks).then(() => index);
} }
return index; return index;
@ -129,7 +129,7 @@ function importJsonDb(indexUrl, entitiesLoaded, entriesLoaded) {
const loaders = []; const loaders = [];
for (let i = 1; i <= index.banks; ++i) { for (let i = 1; i <= index.banks; ++i) {
const bankUrl = `${indexDir}/bank_${i}.json`; const bankUrl = `${indexDir}/bank_${i}.json`;
loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(entries, index.banks, i))); loaders.push(() => loadJson(bankUrl).then(entries => entriesLoaded(index.title, entries, index.banks, i)));
} }
let chain = Promise.resolve(); let chain = Promise.resolve();