don't wipe options when disabling anki support (fixes #27)

This commit is contained in:
Alex Yatskov 2017-01-15 11:37:05 -08:00
parent b5e1a9e260
commit a5aff370e4

View File

@ -24,7 +24,7 @@ function yomichan() {
return chrome.extension.getBackgroundPage().yomichan; return chrome.extension.getBackgroundPage().yomichan;
} }
function getFormValues() { function getFormData() {
return optionsLoad().then(optionsOld => { return optionsLoad().then(optionsOld => {
const optionsNew = $.extend(true, {}, optionsOld); const optionsNew = $.extend(true, {}, optionsOld);
@ -42,12 +42,14 @@ function getFormValues() {
optionsNew.anki.enable = $('#anki-enable').prop('checked'); optionsNew.anki.enable = $('#anki-enable').prop('checked');
optionsNew.anki.tags = $('#card-tags').val().split(/[,; ]+/); optionsNew.anki.tags = $('#card-tags').val().split(/[,; ]+/);
optionsNew.anki.sentenceExt = parseInt($('#sentence-detection-extent').val(), 10); optionsNew.anki.sentenceExt = parseInt($('#sentence-detection-extent').val(), 10);
optionsNew.anki.terms.deck = $('#anki-terms-deck').val(); if (optionsOld.anki.enable) {
optionsNew.anki.terms.model = $('#anki-terms-model').val(); optionsNew.anki.terms.deck = $('#anki-terms-deck').val();
optionsNew.anki.terms.fields = fieldsToDict($('#terms .anki-field-value')); optionsNew.anki.terms.model = $('#anki-terms-model').val();
optionsNew.anki.kanji.deck = $('#anki-kanji-deck').val(); optionsNew.anki.terms.fields = ankiFieldsToDict($('#terms .anki-field-value'));
optionsNew.anki.kanji.model = $('#anki-kanji-model').val(); optionsNew.anki.kanji.deck = $('#anki-kanji-deck').val();
optionsNew.anki.kanji.fields = fieldsToDict($('#kanji .anki-field-value')); optionsNew.anki.kanji.model = $('#anki-kanji-model').val();
optionsNew.anki.kanji.fields = ankiFieldsToDict($('#kanji .anki-field-value'));
}
$('.dict-group').each((index, element) => { $('.dict-group').each((index, element) => {
const dictionary = $(element); const dictionary = $(element);
@ -290,7 +292,7 @@ function showAnkiError(error) {
} }
} }
function fieldsToDict(selection) { function ankiFieldsToDict(selection) {
const result = {}; const result = {};
selection.each((index, element) => { selection.each((index, element) => {
result[$(element).data('field')] = $(element).val(); result[$(element).data('field')] = $(element).val();
@ -369,7 +371,7 @@ function onAnkiModelChanged(e) {
showAnkiSpinner(true); showAnkiSpinner(true);
const element = $(this); const element = $(this);
getFormValues().then(({optionsNew, optionsOld}) => { getFormData().then(({optionsNew, optionsOld}) => {
const tab = element.closest('.tab-pane'); const tab = element.closest('.tab-pane');
const tabId = tab.attr('id'); const tabId = tab.attr('id');
@ -389,7 +391,7 @@ function onOptionsChanged(e) {
return; return;
} }
getFormValues().then(({optionsNew, optionsOld}) => { getFormData().then(({optionsNew, optionsOld}) => {
return optionsSave(optionsNew).then(() => { return optionsSave(optionsNew).then(() => {
yomichan().setOptions(optionsNew); yomichan().setOptions(optionsNew);
updateVisibility(optionsNew); updateVisibility(optionsNew);