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

View File

@ -137,19 +137,7 @@ class Translator {
continue;
}
const tagItems = [];
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);
}
const tags = definition.tags.map(tag => buildTag(tag, definition.tagMeta));
groups[definition.id] = {
source,
reasons,
@ -157,7 +145,7 @@ class Translator {
expression: definition.expression,
reading: definition.reading,
glossary: definition.glossary,
tags: sortTags(tagItems)
tags: sortTags(tags)
};
}
});
@ -165,20 +153,8 @@ class Translator {
processKanji(definitions) {
for (const definition of definitions) {
const tagItems = [];
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);
}
definition.tags = sortTags(tagItems);
const tags = definitions.tags.map(tag => buildTag(tag, definition.tagMeta));
definition.tags = sortTags(tags);
}
return definitions;

View File

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