This commit is contained in:
Alex Yatskov 2016-12-17 21:26:46 -08:00
parent 5c755043fa
commit 5be864bb6f
3 changed files with 21 additions and 43 deletions

View File

@ -64,14 +64,15 @@ class Database {
reading: row.reading, reading: row.reading,
tags: splitField(row.tags), tags: splitField(row.tags),
glossary: row.glossary, glossary: row.glossary,
dictionary: row.dictionary,
id: row.id id: row.id
}); });
} }
}).then(() => { }).then(() => {
return this.getTagMeta(dictionaries); return this.cacheTagMeta(dictionaries);
}).then(tagMeta => { }).then(() => {
for (const result of results) { for (const result of results) {
result.tagMeta = tagMeta; result.tagMeta = this.tagMetaCache[result.dictionary] || {};
} }
return results; return results;
@ -95,17 +96,17 @@ class Database {
}); });
} }
}).then(() => { }).then(() => {
return this.getTagMeta(dictionaries); return this.cacheTagMeta(dictionaries);
}).then(tagMeta => { }).then(() => {
for (const result of results) { for (const result of results) {
result.tagMeta = tagMeta; result.tagMeta = this.tagMetaCache[result.dictionary] || {};
} }
return results; return results;
}); });
} }
getTagMeta(dictionaries) { cacheTagMeta(dictionaries) {
if (this.db === null) { if (this.db === null) {
return Promise.reject('database not initialized'); return Promise.reject('database not initialized');
} }
@ -119,7 +120,7 @@ class Database {
const tagMeta = this.tagMetaCache[dictionary] = {}; const tagMeta = this.tagMetaCache[dictionary] = {};
promises.push( promises.push(
this.db.tagMeta.where('dictionary').equals(dictionary).each(row => { this.db.tagMeta.where('dictionary').equals(dictionary).each(row => {
tagMeta[row.tag] = { tagMeta[row.name] = {
category: row.category, category: row.category,
notes: row.notes, notes: row.notes,
order: row.order order: row.order
@ -128,7 +129,7 @@ class Database {
); );
} }
return Promise.all(promises).then(() => this.tagMetaCache); return Promise.all(promises);
} }
getDictionaries() { getDictionaries() {
@ -232,9 +233,9 @@ class Database {
const meta = tagMeta[tag]; const meta = tagMeta[tag];
rows.push({ rows.push({
name: tag, name: tag,
category: meta.category, category: meta.category || 'default',
notes: meta.notes, notes: meta.notes || '',
order: meta.order, order: meta.order || Number.MAX_SAFE_INTEGER,
dictionary: title dictionary: title
}); });
} }

View File

@ -137,19 +137,7 @@ class Translator {
continue; continue;
} }
const tagItems = []; const tags = definition.tags.map(tag => buildTag(tag, definition.tagMeta));
for (const tag of definition.tags) {
const tagItem = {
name: tag,
category: 'default',
order: Number.MAX_SAFE_INTEGER,
notes: ''
};
applyTagMeta(tagItem, definition.tagMeta);
tagItems.push(tagItem);
}
groups[definition.id] = { groups[definition.id] = {
source, source,
reasons, reasons,
@ -157,7 +145,7 @@ class Translator {
expression: definition.expression, expression: definition.expression,
reading: definition.reading, reading: definition.reading,
glossary: definition.glossary, glossary: definition.glossary,
tags: sortTags(tagItems) tags: sortTags(tags)
}; };
} }
}); });
@ -165,20 +153,8 @@ class Translator {
processKanji(definitions) { processKanji(definitions) {
for (const definition of definitions) { for (const definition of definitions) {
const tagItems = []; const tags = definitions.tags.map(tag => buildTag(tag, definition.tagMeta));
for (const tag of definition.tags) { definition.tags = sortTags(tags);
const tagItem = {
name: tag,
category: 'default',
order: Number.MAX_SAFE_INTEGER,
notes: ''
};
applyTagMeta(tagItem, definition.tagMeta);
tagItems.push(tagItem);
}
definition.tags = sortTags(tagItems);
} }
return definitions; return definitions;

View File

@ -96,8 +96,9 @@ function sortTermDefs(definitions) {
}); });
} }
function applyTagMeta(tag, meta) { function buildTag(name, meta) {
const symbol = tag.name.split(':')[0]; const tag = {name};
const symbol = name.split(':')[0];
for (const prop in meta[symbol] || {}) { for (const prop in meta[symbol] || {}) {
tag[prop] = meta[symbol][prop]; tag[prop] = meta[symbol][prop];
} }
@ -141,7 +142,7 @@ function importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
return indexLoaded( return indexLoaded(
index.title, index.title,
index.version, index.version,
index.entities, index.tagMeta,
index.termBanks > 0, index.termBanks > 0,
index.kanjiBanks > 0 index.kanjiBanks > 0
).then(() => index); ).then(() => index);