From 404f3f1b164fd924a8d34521f86bf3660acdc008 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 16 Jan 2017 22:30:53 -0800 Subject: [PATCH] unify dictionary enabled state --- ext/bg/js/options-form.js | 14 +++++--------- ext/bg/js/options.js | 4 +++- ext/bg/js/templates.js | 22 +++++----------------- ext/bg/js/util.js | 12 ++++++++++++ ext/bg/js/yomichan.js | 38 +++----------------------------------- tmpl/dictionary.html | 11 ++++------- 6 files changed, 32 insertions(+), 69 deletions(-) diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index f58c26cc..7139958a 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -55,9 +55,8 @@ function getFormData() { const dictionary = $(element); const title = dictionary.data('title'); const priority = parseInt(dictionary.find('.dict-priority').val(), 10); - const enableTerms = dictionary.find('.dict-enable-terms').prop('checked'); - const enableKanji = dictionary.find('.dict-enable-kanji').prop('checked'); - optionsNew.dictionaries[title] = {priority, enableTerms, enableKanji}; + const enabled = dictionary.find('.dict-enabled').prop('checked'); + optionsNew.dictionaries[title] = {priority, enabled}; }); return {optionsNew, optionsOld}; @@ -153,18 +152,15 @@ function populateDictionaries(options) { title: row.title, version: row.version, revision: row.revision, - hasTerms: row.hasTerms, - hasKanji: row.hasKanji, priority: dictOptions.priority, - enableTerms: dictOptions.enableTerms, - enableKanji: dictOptions.enableKanji + enabled: dictOptions.enabled }); dictGroups.append($(html)); ++dictCount; }); - $('.dict-enable-terms, .dict-enable-kanji, .dict-priority').change(onOptionsChanged); + $('.dict-enabled, .dict-priority').change(onOptionsChanged); $('.dict-delete').click(onDictionaryDelete); }).catch(error => { showDictionaryError(error); @@ -233,7 +229,7 @@ function onDictionaryImport() { optionsLoad().then(options => { database().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { - options.dictionaries[summary.title] = {enableTerms: summary.hasTerms, enableKanji: summary.hasKanji}; + options.dictionaries[summary.title] = {enabled: true, priority: 0}; return optionsSave(options).then(() => yomichan().setOptions(options)); }).then(() => { return populateDictionaries(options); diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 027a1539..4470c48a 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -97,7 +97,9 @@ function optionsVersion(options) { copy(options.anki.kanji, 'fields', options, 'ankiKanjiFields'); for (const title in options.dictionaries) { - options.dictionaries[title].priority = 0; + const dictionary = options.dictionaries[title]; + dictionary.enabled = dictionary.enableTerms || dictionary.enableKanji; + dictionary.priority = 0; } } ]; diff --git a/ext/bg/js/templates.js b/ext/bg/js/templates.js index 85b36de3..a36e4674 100644 --- a/ext/bg/js/templates.js +++ b/ext/bg/js/templates.js @@ -1,8 +1,6 @@ (function() { var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; templates['dictionary.html'] = template({"1":function(container,depth0,helpers,partials,data) { - return "disabled"; -},"3":function(container,depth0,helpers,partials,data) { return "checked"; },"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { var stack1, helper, alias1=depth0 != null ? depth0 : {}, alias2=helpers.helperMissing, alias3="function", alias4=container.escapeExpression; @@ -17,25 +15,15 @@ templates['dictionary.html'] = template({"1":function(container,depth0,helpers,p + 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))) + " v." + alias4(((helper = (helper = helpers.version || (depth0 != null ? depth0.version : depth0)) != null ? helper : alias2),(typeof helper === alias3 ? helper.call(alias1,{"name":"version","hash":{},"data":data}) : helper))) - + " -->\n \n \n \n \n \n\n
\n Dictionary data is being deleted, please be patient...\n
\n
\n
\n
\n\n
\n
\n"; + + "\" class=\"form-control dict-priority\">\n \n\n"; },"useData":true}); templates['kanji-list.html'] = template({"1":function(container,depth0,helpers,partials,data) { var stack1, helper, alias1=depth0 != null ? depth0 : {}; diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index bba4d797..dcaf1e47 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -39,6 +39,18 @@ function isKanji(c) { return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; } +function enabledDicts(options) { + const dictionaries = {}; + for (const title in options.dictionaries) { + const dictionary = options.dictionaries[title]; + if (dictionary.enabled) { + dictionaries[title] = dictionary; + } + } + + return dictionaries; +} + function promiseCallback(promise, callback) { return promise.then(result => { callback({result}); diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 494b9cda..1f337680 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -157,54 +157,22 @@ class Yomichan { } api_findKanji({text, callback}) { - const dictionaries = {}; - for (const title in this.options.dictionaries) { - const dictionary = this.options.dictionaries[title]; - if (dictionary.enableKanji) { - dictionaries[title] = dictionary; - } - } - promiseCallback( - this.translator.findKanji(text, dictionaries), + this.translator.findKanji(text, enabledDicts(this.options)), callback ); } api_findTerms({text, callback}) { - const dictionaries = {}; - for (const title in this.options.dictionaries) { - const dictionary = this.options.dictionaries[title]; - if (dictionary.enableTerms) { - dictionaries[title] = dictionary; - } - } - promiseCallback( - this.translator.findTerms( - text, - dictionaries, - this.options.general.softKatakana - ), + this.translator.findTerms(text, enabledDicts(this.options), this.options.general.softKatakana), callback ); } api_findTermsGrouped({text, callback}) { - const dictionaries = {}; - for (const title in this.options.dictionaries) { - const dictionary = this.options.dictionaries[title]; - if (dictionary.enableTerms) { - dictionaries[title] = dictionary; - } - } - promiseCallback( - this.translator.findTermsGrouped( - text, - dictionaries, - this.options.general.softKatakana - ), + this.translator.findTermsGrouped(text, enabledDicts(this.options), this.options.general.softKatakana), callback ); } diff --git a/tmpl/dictionary.html b/tmpl/dictionary.html index d9d2726a..17656747 100644 --- a/tmpl/dictionary.html +++ b/tmpl/dictionary.html @@ -17,14 +17,11 @@ +
+ +
- +
-
- -
-
- -