settings: main dictionary selection as dropdown

This commit is contained in:
siikamiika 2017-10-24 16:23:13 +03:00
parent df0d2beb73
commit 78442fff1a
8 changed files with 73 additions and 32 deletions

View File

@ -228,6 +228,21 @@ class Database {
} }
} }
async getTitlesWithSequences() {
if (!this.db) {
throw 'Database not initialized';
}
const titles = [];
await this.db.dictionaries.each(row => {
if (row.hasSequences) {
titles.push(row.title);
}
});
return titles;
}
async importDictionary(archive, callback) { async importDictionary(archive, callback) {
if (!this.db) { if (!this.db) {
throw 'Database not initialized'; throw 'Database not initialized';

View File

@ -203,7 +203,8 @@ function optionsSetDefaults(options) {
popupOffset: 10, popupOffset: 10,
showGuide: true, showGuide: true,
compactTags: false, compactTags: false,
compactGlossaries: false compactGlossaries: false,
mainDictionary: ''
}, },
scanning: { scanning: {

View File

@ -25,6 +25,7 @@ async function formRead() {
optionsNew.general.compactTags = $('#compact-tags').prop('checked'); optionsNew.general.compactTags = $('#compact-tags').prop('checked');
optionsNew.general.compactGlossaries = $('#compact-glossaries').prop('checked'); optionsNew.general.compactGlossaries = $('#compact-glossaries').prop('checked');
optionsNew.general.resultOutputMode = $('#result-output-mode').val(); optionsNew.general.resultOutputMode = $('#result-output-mode').val();
optionsNew.general.mainDictionary = $('#main-dictionary').val();
optionsNew.general.audioSource = $('#audio-playback-source').val(); optionsNew.general.audioSource = $('#audio-playback-source').val();
optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val()); optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val());
optionsNew.general.debugInfo = $('#show-debug-info').prop('checked'); optionsNew.general.debugInfo = $('#show-debug-info').prop('checked');
@ -62,9 +63,8 @@ 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');
const main = dictionary.find('.dict-main').prop('checked');
const allowSecondarySearches = dictionary.find('.dict-allow-secondary-searches').prop('checked'); const allowSecondarySearches = dictionary.find('.dict-allow-secondary-searches').prop('checked');
optionsNew.dictionaries[title] = {priority, enabled, main, allowSecondarySearches}; optionsNew.dictionaries[title] = {priority, enabled, allowSecondarySearches};
}); });
return {optionsNew, optionsOld}; return {optionsNew, optionsOld};
@ -85,6 +85,13 @@ function formUpdateVisibility(options) {
advanced.hide(); advanced.hide();
} }
const merge = $('.options-merge');
if (options.general.resultOutputMode === 'merge') {
merge.show();
} else {
merge.hide();
}
const debug = $('#debug'); const debug = $('#debug');
if (options.general.debugInfo) { if (options.general.debugInfo) {
const temp = utilIsolate(options); const temp = utilIsolate(options);
@ -97,6 +104,29 @@ function formUpdateVisibility(options) {
} }
} }
async function formMainDictionaryOptionsPopulate(options) {
const select = $('#main-dictionary').empty();
let titles = await utilDatabaseGetTitlesWithSequences();
titles = titles.filter(title => options.dictionaries[title].enabled);
const formOptionsHtml = [];
let mainDictionarySelected = false;
for (title of titles) {
if (options.general.mainDictionary === title) {
mainDictionarySelected = true;
}
formOptionsHtml.push(`<option value="${title}"${options.general.mainDictionary === title ? ' selected' : ''}>${title}</option>`);
}
if (!mainDictionarySelected) {
options.general.mainDictionary = '';
}
const notSelectedOptionHtml = `<option value=""${!mainDictionarySelected ? ' selected' : ''}>(Not selected)</option>`;
select.append($([notSelectedOptionHtml].concat(formOptionsHtml).join('')));
}
async function onFormOptionsChanged(e) { async function onFormOptionsChanged(e) {
try { try {
if (!e.originalEvent && !e.isTrigger) { if (!e.originalEvent && !e.isTrigger) {
@ -104,6 +134,7 @@ async function onFormOptionsChanged(e) {
} }
const {optionsNew, optionsOld} = await formRead(); const {optionsNew, optionsOld} = await formRead();
await formMainDictionaryOptionsPopulate(optionsNew);
await optionsSave(optionsNew); await optionsSave(optionsNew);
formUpdateVisibility(optionsNew); formUpdateVisibility(optionsNew);
@ -131,6 +162,7 @@ async function onReady() {
$('#compact-tags').prop('checked', options.general.compactTags); $('#compact-tags').prop('checked', options.general.compactTags);
$('#compact-glossaries').prop('checked', options.general.compactGlossaries); $('#compact-glossaries').prop('checked', options.general.compactGlossaries);
$('#result-output-mode').val(options.general.resultOutputMode); $('#result-output-mode').val(options.general.resultOutputMode);
$('#main-dictionary').val(options.general.mainDictionary);
$('#audio-playback-source').val(options.general.audioSource); $('#audio-playback-source').val(options.general.audioSource);
$('#audio-playback-volume').val(options.general.audioVolume); $('#audio-playback-volume').val(options.general.audioVolume);
$('#show-debug-info').prop('checked', options.general.debugInfo); $('#show-debug-info').prop('checked', options.general.debugInfo);
@ -172,6 +204,8 @@ async function onReady() {
ankiErrorShow(e); ankiErrorShow(e);
} }
await formMainDictionaryOptionsPopulate(options);
formUpdateVisibility(options); formUpdateVisibility(options);
} }
@ -243,18 +277,6 @@ 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();
@ -265,14 +287,13 @@ 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, main: false, allowSecondarySearches: false}; const dictOptions = options.dictionaries[dictRow.title] || {enabled: false, priority: 0, allowSecondarySearches: 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,
allowSecondarySearches: dictOptions.allowSecondarySearches allowSecondarySearches: dictOptions.allowSecondarySearches
}); });
@ -285,11 +306,6 @@ 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) {
@ -305,9 +321,11 @@ async function onDictionaryPurge(e) {
await utilDatabasePurge(); await utilDatabasePurge();
const options = await optionsLoad(); const options = await optionsLoad();
options.dictionaries = {}; options.dictionaries = {};
options.general.mainDictionary = '';
await optionsSave(options); await optionsSave(options);
await dictionaryGroupsPopulate(options); await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options);
} catch (e) { } catch (e) {
dictionaryErrorShow(e); dictionaryErrorShow(e);
} finally { } finally {
@ -333,10 +351,14 @@ 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, main: false, allowSecondarySearches: false}; options.dictionaries[summary.title] = {enabled: true, priority: 0, allowSecondarySearches: false};
if (summary.hasSequences && !options.general.mainDictionary) {
options.general.mainDictionary = summary.title;
}
await optionsSave(options); await optionsSave(options);
await dictionaryGroupsPopulate(options); await dictionaryGroupsPopulate(options);
await formMainDictionaryOptionsPopulate(options);
} catch (e) { } catch (e) {
dictionaryErrorShow(e); dictionaryErrorShow(e);
} finally { } finally {

View File

@ -15,9 +15,7 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p
+ ((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=\"checkbox options-advanced\">\n <label><input type=\"checkbox\" class=\"dict-allow-secondary-searches\" " + "> Enable search</label>\n </div>\n <div class=\"checkbox options-advanced\">\n <label><input type=\"checkbox\" class=\"dict-allow-secondary-searches\" "
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.allowSecondarySearches : 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.allowSecondarySearches : depth0),{"name":"if","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "> Allow secondary searches</label>\n </div>\n <div class=\"radio\">\n <label><input type=\"radio\" class=\"dict-main\" " + "> Allow secondary searches</label>\n </div>\n <div class=\"form-group options-advanced\">\n <label for=\"dict-"
+ ((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

@ -57,12 +57,11 @@ class Translator {
async findTermsMerged(text, dictionaries, alphanumeric) { async findTermsMerged(text, dictionaries, alphanumeric) {
const options = await apiOptionsGet(); const options = await apiOptionsGet();
const mainDictionary = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].main).concat([''])[0];
const secondarySearchTitles = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].allowSecondarySearches); const secondarySearchTitles = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].allowSecondarySearches);
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, mainDictionary); const definitionsBySequence = dictTermsMergeBySequence(definitions, options.general.mainDictionary);
const definitionsMerged = []; const definitionsMerged = [];
const mergedByTermIndices = new Set(); const mergedByTermIndices = new Set();
@ -73,7 +72,7 @@ class Translator {
const result = definitionsBySequence[sequence]; const result = definitionsBySequence[sequence];
const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), mainDictionary); const rawDefinitionsBySequence = await this.database.findTermsBySequence(Number(sequence), options.general.mainDictionary);
const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence); const definitionsByGloss = dictTermsMergeByGloss(result, rawDefinitionsBySequence);
const secondarySearchResults = []; const secondarySearchResults = [];

View File

@ -87,6 +87,10 @@ function utilDatabaseGetTitles() {
return utilBackend().translator.database.getTitles(); return utilBackend().translator.database.getTitles();
} }
function utilDatabaseGetTitlesWithSequences() {
return utilBackend().translator.database.getTitlesWithSequences();
}
function utilDatabasePurge() { function utilDatabasePurge() {
return utilBackend().translator.database.purge(); return utilBackend().translator.database.purge();
} }

View File

@ -60,6 +60,11 @@
</select> </select>
</div> </div>
<div class="form-group options-merge">
<label for="main-dictionary">Main dictionary</label>
<select class="form-control" id="main-dictionary"></select>
</div>
<div class="form-group"> <div class="form-group">
<label for="audio-playback-source">Audio playback source</label> <label for="audio-playback-source">Audio playback source</label>
<select class="form-control" id="audio-playback-source"> <select class="form-control" id="audio-playback-source">

View File

@ -7,9 +7,6 @@
<div class="checkbox options-advanced"> <div class="checkbox options-advanced">
<label><input type="checkbox" class="dict-allow-secondary-searches" {{#if allowSecondarySearches}}checked{{/if}}> Allow secondary searches</label> <label><input type="checkbox" class="dict-allow-secondary-searches" {{#if allowSecondarySearches}}checked{{/if}}> Allow secondary searches</label>
</div> </div>
<div class="radio">
<label><input type="radio" class="dict-main" {{#if main}}checked{{/if}}> Set as main dictionary</label>
</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>
<input type="number" value="{{priority}}" id="dict-{{title}}" class="form-control dict-priority"> <input type="number" value="{{priority}}" id="dict-{{title}}" class="form-control dict-priority">