From 5be864bb6f1f911bec143566c768bf735a373743 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 17 Dec 2016 21:26:46 -0800 Subject: [PATCH] WIP --- ext/bg/js/database.js | 25 +++++++++++++------------ ext/bg/js/translator.js | 32 ++++---------------------------- ext/bg/js/util.js | 7 ++++--- 3 files changed, 21 insertions(+), 43 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 980e9e40..6f474e77 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -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 }); } diff --git a/ext/bg/js/translator.js b/ext/bg/js/translator.js index 13bf1427..726922ac 100644 --- a/ext/bg/js/translator.js +++ b/ext/bg/js/translator.js @@ -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; diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index dc606a73..f44ffbc8 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -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);