unify dictionary enabled state

This commit is contained in:
Alex Yatskov 2017-01-16 22:30:53 -08:00
parent 268e00435f
commit 404f3f1b16
6 changed files with 32 additions and 69 deletions

View File

@ -55,9 +55,8 @@ function getFormData() {
const dictionary = $(element); const dictionary = $(element);
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 enableTerms = dictionary.find('.dict-enable-terms').prop('checked'); const enabled = dictionary.find('.dict-enabled').prop('checked');
const enableKanji = dictionary.find('.dict-enable-kanji').prop('checked'); optionsNew.dictionaries[title] = {priority, enabled};
optionsNew.dictionaries[title] = {priority, enableTerms, enableKanji};
}); });
return {optionsNew, optionsOld}; return {optionsNew, optionsOld};
@ -153,18 +152,15 @@ function populateDictionaries(options) {
title: row.title, title: row.title,
version: row.version, version: row.version,
revision: row.revision, revision: row.revision,
hasTerms: row.hasTerms,
hasKanji: row.hasKanji,
priority: dictOptions.priority, priority: dictOptions.priority,
enableTerms: dictOptions.enableTerms, enabled: dictOptions.enabled
enableKanji: dictOptions.enableKanji
}); });
dictGroups.append($(html)); dictGroups.append($(html));
++dictCount; ++dictCount;
}); });
$('.dict-enable-terms, .dict-enable-kanji, .dict-priority').change(onOptionsChanged); $('.dict-enabled, .dict-priority').change(onOptionsChanged);
$('.dict-delete').click(onDictionaryDelete); $('.dict-delete').click(onDictionaryDelete);
}).catch(error => { }).catch(error => {
showDictionaryError(error); showDictionaryError(error);
@ -233,7 +229,7 @@ function onDictionaryImport() {
optionsLoad().then(options => { optionsLoad().then(options => {
database().importDictionary(dictUrl.val(), (total, current) => setProgress(current / total * 100.0)).then(summary => { 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)); return optionsSave(options).then(() => yomichan().setOptions(options));
}).then(() => { }).then(() => {
return populateDictionaries(options); return populateDictionaries(options);

View File

@ -97,7 +97,9 @@ function optionsVersion(options) {
copy(options.anki.kanji, 'fields', options, 'ankiKanjiFields'); copy(options.anki.kanji, 'fields', options, 'ankiKanjiFields');
for (const title in options.dictionaries) { 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;
} }
} }
]; ];

View File

@ -1,8 +1,6 @@
(function() { (function() {
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['dictionary.html'] = template({"1":function(container,depth0,helpers,partials,data) { templates['dictionary.html'] = template({"1":function(container,depth0,helpers,partials,data) {
return "disabled";
},"3":function(container,depth0,helpers,partials,data) {
return "checked"; return "checked";
},"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { },"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; 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))) + 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)))
+ " <small>v." + " <small>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))) + 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)))
+ "</small></h4> -->\n <!-- </div> -->\n <!-- <div class=\"col-xs-4 text-right disabled\"> -->\n <!-- <button type=\"button\" class=\"dict-group-controls dict-delete btn btn-danger\">Delete</button> -->\n <!-- </div> -->\n <!-- </div> -->\n\n <div class=\"dict-delete-progress\">\n Dictionary data is being deleted, please be patient...\n <div class=\"progress\">\n <div class=\"progress-bar progress-bar-striped progress-bar-danger\" style=\"width: 0%\"></div>\n </div>\n </div>\n\n <div class=\"form-group dict-group-controls\">\n <label for=\"dict-" + "</small></h4> -->\n <!-- </div> -->\n <!-- <div class=\"col-xs-4 text-right disabled\"> -->\n <!-- <button type=\"button\" class=\"dict-group-controls dict-delete btn btn-danger\">Delete</button> -->\n <!-- </div> -->\n <!-- </div> -->\n\n <div class=\"dict-delete-progress\">\n Dictionary data is being deleted, please be patient...\n <div class=\"progress\">\n <div class=\"progress-bar progress-bar-striped progress-bar-danger\" style=\"width: 0%\"></div>\n </div>\n </div>\n\n <div class=\"checkbox dict-group-controls\">\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 : "")
+ "> Enable search</label>\n </div>\n <div class=\"form-group dict-group-controls\">\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)))
+ "\">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)))
+ "\" id=\"dict-" + "\" id=\"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)))
+ "\" class=\"form-control dict-priority\">\n </div>\n <div class=\"checkbox dict-group-controls " + "\" class=\"form-control dict-priority\">\n </div>\n</div>\n";
+ ((stack1 = helpers.unless.call(alias1,(depth0 != null ? depth0.hasTerms : depth0),{"name":"unless","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "\">\n <label><input type=\"checkbox\" class=\"dict-enable-terms\" "
+ ((stack1 = helpers.unless.call(alias1,(depth0 != null ? depth0.hasTerms : depth0),{"name":"unless","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ " "
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.enableTerms : depth0),{"name":"if","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "> Enable term search</label>\n </div>\n <div class=\"checkbox dict-group-controls "
+ ((stack1 = helpers.unless.call(alias1,(depth0 != null ? depth0.hasKanji : depth0),{"name":"unless","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "\">\n <label><input type=\"checkbox\" class=\"dict-enable-kanji\" "
+ ((stack1 = helpers.unless.call(alias1,(depth0 != null ? depth0.hasKanji : depth0),{"name":"unless","hash":{},"fn":container.program(1, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ " "
+ ((stack1 = helpers["if"].call(alias1,(depth0 != null ? depth0.enableKanji : depth0),{"name":"if","hash":{},"fn":container.program(3, data, 0),"inverse":container.noop,"data":data})) != null ? stack1 : "")
+ "> Enable Kanji search</label>\n </div>\n</div>\n";
},"useData":true}); },"useData":true});
templates['kanji-list.html'] = template({"1":function(container,depth0,helpers,partials,data) { templates['kanji-list.html'] = template({"1":function(container,depth0,helpers,partials,data) {
var stack1, helper, alias1=depth0 != null ? depth0 : {}; var stack1, helper, alias1=depth0 != null ? depth0 : {};

View File

@ -39,6 +39,18 @@ function isKanji(c) {
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0; 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) { function promiseCallback(promise, callback) {
return promise.then(result => { return promise.then(result => {
callback({result}); callback({result});

View File

@ -157,54 +157,22 @@ class Yomichan {
} }
api_findKanji({text, callback}) { 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( promiseCallback(
this.translator.findKanji(text, dictionaries), this.translator.findKanji(text, enabledDicts(this.options)),
callback callback
); );
} }
api_findTerms({text, 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( promiseCallback(
this.translator.findTerms( this.translator.findTerms(text, enabledDicts(this.options), this.options.general.softKatakana),
text,
dictionaries,
this.options.general.softKatakana
),
callback callback
); );
} }
api_findTermsGrouped({text, 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( promiseCallback(
this.translator.findTermsGrouped( this.translator.findTermsGrouped(text, enabledDicts(this.options), this.options.general.softKatakana),
text,
dictionaries,
this.options.general.softKatakana
),
callback callback
); );
} }

View File

@ -17,14 +17,11 @@
</div> </div>
</div> </div>
<div class="checkbox dict-group-controls">
<label><input type="checkbox" class="dict-enabled" {{#if enabled}}checked{{/if}}> Enable search</label>
</div>
<div class="form-group dict-group-controls"> <div class="form-group dict-group-controls">
<label for="dict-{{title}}">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">
</div> </div>
<div class="checkbox dict-group-controls {{#unless hasTerms}}disabled{{/unless}}">
<label><input type="checkbox" class="dict-enable-terms" {{#unless hasTerms}}disabled{{/unless}} {{#if enableTerms}}checked{{/if}}> Enable term search</label>
</div>
<div class="checkbox dict-group-controls {{#unless hasKanji}}disabled{{/unless}}">
<label><input type="checkbox" class="dict-enable-kanji" {{#unless hasKanji}}disabled{{/unless}} {{#if enableKanji}}checked{{/if}}> Enable Kanji search</label>
</div>
</div> </div>