From 78442fff1a995632f2b315994f2540459ee20eca Mon Sep 17 00:00:00 2001 From: siikamiika Date: Tue, 24 Oct 2017 16:23:13 +0300 Subject: settings: main dictionary selection as dropdown --- ext/bg/js/database.js | 15 +++++++++++ ext/bg/js/options.js | 3 ++- ext/bg/js/settings.js | 66 ++++++++++++++++++++++++++++++++----------------- ext/bg/js/templates.js | 4 +-- ext/bg/js/translator.js | 5 ++-- ext/bg/js/util.js | 4 +++ ext/bg/settings.html | 5 ++++ tmpl/dictionary.html | 3 --- 8 files changed, 73 insertions(+), 32 deletions(-) diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index 9e90e6de..fcf8ef3f 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -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) { if (!this.db) { throw 'Database not initialized'; diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 2cb7dbec..4d18c166 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -203,7 +203,8 @@ function optionsSetDefaults(options) { popupOffset: 10, showGuide: true, compactTags: false, - compactGlossaries: false + compactGlossaries: false, + mainDictionary: '' }, scanning: { diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index 37e5126e..36f2bdf9 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -25,6 +25,7 @@ async function formRead() { optionsNew.general.compactTags = $('#compact-tags').prop('checked'); optionsNew.general.compactGlossaries = $('#compact-glossaries').prop('checked'); optionsNew.general.resultOutputMode = $('#result-output-mode').val(); + optionsNew.general.mainDictionary = $('#main-dictionary').val(); optionsNew.general.audioSource = $('#audio-playback-source').val(); optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val()); optionsNew.general.debugInfo = $('#show-debug-info').prop('checked'); @@ -62,9 +63,8 @@ async function formRead() { const title = dictionary.data('title'); const priority = parseInt(dictionary.find('.dict-priority').val(), 10); 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'); - optionsNew.dictionaries[title] = {priority, enabled, main, allowSecondarySearches}; + optionsNew.dictionaries[title] = {priority, enabled, allowSecondarySearches}; }); return {optionsNew, optionsOld}; @@ -85,6 +85,13 @@ function formUpdateVisibility(options) { advanced.hide(); } + const merge = $('.options-merge'); + if (options.general.resultOutputMode === 'merge') { + merge.show(); + } else { + merge.hide(); + } + const debug = $('#debug'); if (options.general.debugInfo) { 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(``); + } + + if (!mainDictionarySelected) { + options.general.mainDictionary = ''; + } + + const notSelectedOptionHtml = ``; + + select.append($([notSelectedOptionHtml].concat(formOptionsHtml).join(''))); +} + async function onFormOptionsChanged(e) { try { if (!e.originalEvent && !e.isTrigger) { @@ -104,6 +134,7 @@ async function onFormOptionsChanged(e) { } const {optionsNew, optionsOld} = await formRead(); + await formMainDictionaryOptionsPopulate(optionsNew); await optionsSave(optionsNew); formUpdateVisibility(optionsNew); @@ -131,6 +162,7 @@ async function onReady() { $('#compact-tags').prop('checked', options.general.compactTags); $('#compact-glossaries').prop('checked', options.general.compactGlossaries); $('#result-output-mode').val(options.general.resultOutputMode); + $('#main-dictionary').val(options.general.mainDictionary); $('#audio-playback-source').val(options.general.audioSource); $('#audio-playback-volume').val(options.general.audioVolume); $('#show-debug-info').prop('checked', options.general.debugInfo); @@ -172,6 +204,8 @@ async function onReady() { ankiErrorShow(e); } + await formMainDictionaryOptionsPopulate(options); + formUpdateVisibility(options); } @@ -243,18 +277,6 @@ function dictionaryGroupsSort() { 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) { const dictGroups = $('#dict-groups').empty(); const dictWarning = $('#dict-warning').hide(); @@ -265,14 +287,13 @@ async function dictionaryGroupsPopulate(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', { title: dictRow.title, version: dictRow.version, revision: dictRow.revision, priority: dictOptions.priority, enabled: dictOptions.enabled, - main: dictOptions.main, allowSecondarySearches: dictOptions.allowSecondarySearches }); @@ -285,11 +306,6 @@ async function dictionaryGroupsPopulate(options) { dictionaryGroupsSort(); onFormOptionsChanged(e); }); - - $('.dict-main').change(e => { - dictionarySetMain(e); - onFormOptionsChanged(e); - }); } async function onDictionaryPurge(e) { @@ -305,9 +321,11 @@ async function onDictionaryPurge(e) { await utilDatabasePurge(); const options = await optionsLoad(); options.dictionaries = {}; + options.general.mainDictionary = ''; await optionsSave(options); await dictionaryGroupsPopulate(options); + await formMainDictionaryOptionsPopulate(options); } catch (e) { dictionaryErrorShow(e); } finally { @@ -333,10 +351,14 @@ async function onDictionaryImport(e) { const options = await optionsLoad(); 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 dictionaryGroupsPopulate(options); + await formMainDictionaryOptionsPopulate(options); } catch (e) { dictionaryErrorShow(e); } finally { diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index e5640394..9f72e661 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -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 : "") + "> Enable search\n \n
\n \n
\n
\n \n
\n
\n \n
\n
\n \n options.dictionaries[dict].main).concat([''])[0]; const secondarySearchTitles = Object.keys(options.dictionaries).filter(dict => options.dictionaries[dict].allowSecondarySearches); const titles = Object.keys(dictionaries); const {length, definitions} = await this.findTerms(text, dictionaries, alphanumeric); - const definitionsBySequence = dictTermsMergeBySequence(definitions, mainDictionary); + const definitionsBySequence = dictTermsMergeBySequence(definitions, options.general.mainDictionary); const definitionsMerged = []; const mergedByTermIndices = new Set(); @@ -73,7 +72,7 @@ class Translator { 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 secondarySearchResults = []; diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 91b16e79..60f6452b 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -87,6 +87,10 @@ function utilDatabaseGetTitles() { return utilBackend().translator.database.getTitles(); } +function utilDatabaseGetTitlesWithSequences() { + return utilBackend().translator.database.getTitlesWithSequences(); +} + function utilDatabasePurge() { return utilBackend().translator.database.purge(); } diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 408d5aab..8c457652 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -60,6 +60,11 @@
+
+ + +
+
Allow secondary searches
-
- -
-- cgit v1.2.3