merged mode: add main dictionary selection

This commit is contained in:
siikamiika 2017-10-05 05:21:07 +03:00
parent 03fd9dcdb4
commit cfad3b3099
7 changed files with 53 additions and 22 deletions

View File

@ -85,14 +85,14 @@ class Database {
return results; return results;
} }
async findTermsBySequence(sequence, dictionary) { async findTermsBySequence(sequence, mainDictionary) {
if (!this.db) { if (!this.db) {
throw 'Database not initialized'; throw 'Database not initialized';
} }
const results = []; const results = [];
await this.db.terms.where('sequence').equals(sequence).each(row => { await this.db.terms.where('sequence').equals(sequence).each(row => {
// if (dictionary === row.dictionary) { if (row.dictionary === mainDictionary) {
results.push({ results.push({
expression: row.expression, expression: row.expression,
reading: row.reading, reading: row.reading,
@ -104,6 +104,7 @@ class Database {
id: row.id, id: row.id,
sequence: typeof row.sequence === 'undefined' ? -1 : row.sequence sequence: typeof row.sequence === 'undefined' ? -1 : row.sequence
}); });
}
}); });
return results; return results;

View File

@ -144,10 +144,10 @@ function dictTermsGroup(definitions, dictionaries) {
return dictTermsSort(results); return dictTermsSort(results);
} }
function dictTermsMergeBySequence(definitions) { function dictTermsMergeBySequence(definitions, mainDictionary) {
const definitionsBySequence = {'-1': []}; const definitionsBySequence = {'-1': []};
for (const definition of definitions) { for (const definition of definitions) {
if (definition.sequence > 0) { if (mainDictionary === definition.dictionary && definition.sequence > 0) {
if (!definitionsBySequence[definition.sequence]) { if (!definitionsBySequence[definition.sequence]) {
definitionsBySequence[definition.sequence] = { definitionsBySequence[definition.sequence] = {
reasons: definition.reasons, reasons: definition.reasons,

View File

@ -154,6 +154,10 @@ function optionsSetDefaults(options) {
dictionaries: {}, dictionaries: {},
dictionary: {
main: ''
},
anki: { anki: {
enable: false, enable: false,
server: 'http://127.0.0.1:8765', server: 'http://127.0.0.1:8765',

View File

@ -60,7 +60,11 @@ async function formRead() {
const title = dictionary.data('title'); const title = dictionary.data('title');
const priority = parseInt(dictionary.find('.dict-priority').val(), 10); const priority = parseInt(dictionary.find('.dict-priority').val(), 10);
const enabled = dictionary.find('.dict-enabled').prop('checked'); const enabled = dictionary.find('.dict-enabled').prop('checked');
optionsNew.dictionaries[title] = {priority, enabled}; const main = dictionary.find('.dict-main').prop('checked');
if (main) {
optionsNew.dictionary.main = title;
}
optionsNew.dictionaries[title] = {priority, enabled, main};
}); });
return {optionsNew, optionsOld}; return {optionsNew, optionsOld};
@ -237,6 +241,18 @@ function dictionaryGroupsSort() {
dictGroups.append(dictGroupChildren); dictGroups.append(dictGroupChildren);
} }
function dictionarySetMain(e) {
const mainDictionary = $(e.target).closest('.dict-group');
const mainDictionaryTitle = mainDictionary.data('title');
$('.dict-group').each((index, element) => {
const dictionary = $(element);
if (dictionary.data('title') !== mainDictionaryTitle) {
dictionary.find('.dict-main').prop('checked', false);
}
});
}
async function dictionaryGroupsPopulate(options) { async function dictionaryGroupsPopulate(options) {
const dictGroups = $('#dict-groups').empty(); const dictGroups = $('#dict-groups').empty();
const dictWarning = $('#dict-warning').hide(); const dictWarning = $('#dict-warning').hide();
@ -247,13 +263,14 @@ async function dictionaryGroupsPopulate(options) {
} }
for (const dictRow of dictRowsSort(dictRows, options)) { for (const dictRow of dictRowsSort(dictRows, options)) {
const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0}; const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0, main: false};
const dictHtml = await apiTemplateRender('dictionary.html', { const dictHtml = await apiTemplateRender('dictionary.html', {
title: dictRow.title, title: dictRow.title,
version: dictRow.version, version: dictRow.version,
revision: dictRow.revision, revision: dictRow.revision,
priority: dictOptions.priority, priority: dictOptions.priority,
enabled: dictOptions.enabled enabled: dictOptions.enabled,
main: dictOptions.main
}); });
dictGroups.append($(dictHtml)); dictGroups.append($(dictHtml));
@ -265,6 +282,11 @@ async function dictionaryGroupsPopulate(options) {
dictionaryGroupsSort(); dictionaryGroupsSort();
onFormOptionsChanged(e); onFormOptionsChanged(e);
}); });
$('.dict-main').change(e => {
dictionarySetMain(e);
onFormOptionsChanged(e);
});
} }
async function onDictionaryPurge(e) { async function onDictionaryPurge(e) {
@ -308,7 +330,7 @@ async function onDictionaryImport(e) {
const options = await optionsLoad(); const options = await optionsLoad();
const summary = await utilDatabaseImport(e.target.files[0], updateProgress); const summary = await utilDatabaseImport(e.target.files[0], updateProgress);
options.dictionaries[summary.title] = {enabled: true, priority: 0}; options.dictionaries[summary.title] = {enabled: true, priority: 0, main: false};
await optionsSave(options); await optionsSave(options);
await dictionaryGroupsPopulate(options); await dictionaryGroupsPopulate(options);

View File

@ -13,7 +13,9 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p
+ alias4(((helper = (helper = helpers.revision || (depth0 != null ? depth0.revision : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"revision","hash":{},"data":data}) : helper))) + alias4(((helper = (helper = helpers.revision || (depth0 != null ? depth0.revision : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"revision","hash":{},"data":data}) : helper)))
+ "</small></h4>\n\n <div class=\"checkbox\">\n <label><input type=\"checkbox\" class=\"dict-enabled\" " + "</small></h4>\n\n <div class=\"checkbox\">\n <label><input type=\"checkbox\" class=\"dict-enabled\" "
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.enabled : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "") + ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.enabled : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "> Enable search</label>\n </div>\n <div class=\"form-group options-advanced\">\n <label for=\"dict-" + "> Enable search</label>\n <label><input type=\"radio\" class=\"dict-main\" "
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.main : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "> Set as main dictionary</label>\n </div>\n <div class=\"form-group options-advanced\">\n <label for=\"dict-"
+ alias4(((helper = (helper = helpers.title || (depth0 != null ? depth0.title : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"title","hash":{},"data":data}) : helper))) + alias4(((helper = (helper = helpers.title || (depth0 != null ? depth0.title : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"title","hash":{},"data":data}) : helper)))
+ "\">Result priority</label>\n <input type=\"number\" value=\"" + "\">Result priority</label>\n <input type=\"number\" value=\""
+ alias4(((helper = (helper = helpers.priority || (depth0 != null ? depth0.priority : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"priority","hash":{},"data":data}) : helper))) + alias4(((helper = (helper = helpers.priority || (depth0 != null ? depth0.priority : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"priority","hash":{},"data":data}) : helper)))

View File

@ -49,10 +49,11 @@ class Translator {
} }
async findTermsMerged(text, dictionaries, alphanumeric) { async findTermsMerged(text, dictionaries, alphanumeric) {
const options = await apiOptionsGet();
const titles = Object.keys(dictionaries); const titles = Object.keys(dictionaries);
const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric);
const definitionsBySequence = dictTermsMergeBySequence(definitions); const definitionsBySequence = dictTermsMergeBySequence(definitions, options.dictionary.main);
const definitionsMerged = dictTermsGroup(definitionsBySequence['-1'], dictionaries); const definitionsMerged = dictTermsGroup(definitionsBySequence['-1'], dictionaries);
for (const sequence in definitionsBySequence) { for (const sequence in definitionsBySequence) {
@ -62,7 +63,7 @@ class Translator {
const result = definitionsBySequence[sequence]; const result = definitionsBySequence[sequence];
const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence)); const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), options.dictionary.main);
const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence); const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence);
// postprocess glossaries // postprocess glossaries

View File

@ -3,6 +3,7 @@
<div class="checkbox"> <div class="checkbox">
<label><input type="checkbox" class="dict-enabled" {{#if enabled}}checked{{/if}}> Enable search</label> <label><input type="checkbox" class="dict-enabled" {{#if enabled}}checked{{/if}}> Enable search</label>
<label><input type="radio" class="dict-main" {{#if main}}checked{{/if}}> Set as main dictionary</label>
</div> </div>
<div class="form-group options-advanced"> <div class="form-group options-advanced">
<label for="dict-{{title}}">Result priority</label> <label for="dict-{{title}}">Result priority</label>