This commit is contained in:
Alex Yatskov 2016-11-13 11:37:54 -08:00
parent 320a82146b
commit 7598de2234
2 changed files with 12 additions and 7 deletions

View File

@ -133,7 +133,7 @@ class Database {
const promises = []; const promises = [];
for (const dictionary of dictionaries) { for (const dictionary of dictionaries) {
if (this.entities.hasOwnProperty(dictionary)) { if (this.entities[dictionary]) {
promises.push(Promise.resolve(this.entities[dictionary])); promises.push(Promise.resolve(this.entities[dictionary]));
} else { } else {
const entities = this.entities[dictionary] = {}; const entities = this.entities[dictionary] = {};
@ -146,14 +146,14 @@ class Database {
} }
return Promise.all(promises).then(results => { return Promise.all(promises).then(results => {
const entries = {}; const entities = {};
for (const result of results) { for (const result of results) {
for (const name in result) { for (const name in result) {
entries[name] = result[name]; entities[name] = result[name];
} }
} }
return entries; return entities;
}); });
} }
@ -243,7 +243,9 @@ class Database {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
let summary = null;
const indexLoaded = (title, version, entities, hasTerms, hasKanji) => { const indexLoaded = (title, version, entities, hasTerms, hasKanji) => {
summary = {title, hasTerms, hasKanji, version};
return this.db.dictionaries.where('title').equals(title).count().then(count => { return this.db.dictionaries.where('title').equals(title).count().then(count => {
if (count > 0) { if (count > 0) {
return Promise.reject(`dictionary "${title}" is already imported`); return Promise.reject(`dictionary "${title}" is already imported`);
@ -301,6 +303,6 @@ class Database {
}); });
}; };
return importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded); return importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded).then(() => summary);
} }
} }

View File

@ -127,7 +127,7 @@ function populateDictionaries(opts) {
return database().getDictionaries().then(rows => { return database().getDictionaries().then(rows => {
rows.forEach(row => { rows.forEach(row => {
const dictOpts = opts.dictionaries[row.title] || {enableTerms: true, enableKanji: false}; const dictOpts = opts.dictionaries[row.title] || {enableTerms: false, enableKanji: false};
const html = Handlebars.templates['dictionary.html']({ const html = Handlebars.templates['dictionary.html']({
title: row.title, title: row.title,
version: row.version, version: row.version,
@ -198,7 +198,10 @@ function onDictionaryImport() {
const dictUrl = $('#dict-url'); const dictUrl = $('#dict-url');
loadOptions().then(opts => { loadOptions().then(opts => {
database().importDictionary(dictUrl.val(), callback).then(() => { database().importDictionary(dictUrl.val(), callback).then(summary => {
opts.dictionaries[summary.title] = {hasTerms: summary.hasTerms, hasKanji: summary.hasKanji};
return saveOptions(opts);
}).then(() => {
return populateDictionaries(opts); return populateDictionaries(opts);
}).catch(error => { }).catch(error => {
dictError.show().find('span').text(error); dictError.show().find('span').text(error);