From 281b8018a67c34cc5518904b6c78f1ee0c497f77 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 28 May 2016 14:00:35 -0700 Subject: Cleanup --- ext/bg/js/options-form.js | 61 +++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 29 deletions(-) (limited to 'ext') diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 0a39d3ea..6001d145 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -27,10 +27,13 @@ function optionsToForm(opts) { $('#load-enamdict').prop('checked', opts.loadEnamDict); $('#select-matched-text').prop('checked', opts.selectMatchedText); $('#enable-anki-connect').prop('checked', opts.enableAnkiConnect); - $('#anki-vocab-deck').val(opts.ankiVocabDeck); - $('#anki-vocab-model').val(opts.ankiVocabModel); - $('#anki-kanji-deck').val(opts.ankiKanjiDeck); - $('#anki-kanji-model').val(opts.ankiKanjiModel); + + if (opts.enableAnkiConnect) { + $('#anki-vocab-deck').val(opts.ankiVocabDeck); + $('#anki-vocab-model').val(opts.ankiVocabModel); + $('#anki-kanji-deck').val(opts.ankiKanjiDeck); + $('#anki-kanji-model').val(opts.ankiKanjiModel); + } } function formToOptions(section, callback) { @@ -78,6 +81,28 @@ function populateAnkiDeckAndModel() { }}); } +function populateAnkiFields(control) { + const modelName = control.val(); + if (modelName === null) { + return; + } + + yomichan().api_getModelFieldNames({modelName, callback: (names) => { + const table = control.closest('.tab-pane').find('.anki-fields'); + table.find('tbody').remove(); + + const body = $(''); + names.forEach((name) => { + const row = $(''); + row.append($('').text(name)); + row.append($('', {class: 'anki-field-value form-control'}).data('field', name)); + body.append(row); + }); + + table.append(body); + }}); +} + function onOptionsGeneralChanged(e) { if (!e.originalEvent) { return; @@ -101,37 +126,15 @@ function onOptionsAnkiChanged(e) { } } -function onModelChanged() { - const modelName = $(this).val(); - if (modelName === null) { - return; - } - - yomichan().api_getModelFieldNames({modelName, callback: (names) => { - const table = $(this).closest('.tab-pane').find('.anki-fields'); - table.find('tbody').remove(); - - const body = $(''); - names.forEach((name) => { - const row = $(''); - row.append($('').text(name)); - row.append($('', {class: 'anki-field-value form-control'}).data('field', name)); - body.append(row); - }); - - table.append(body); - }}); -} - $(document).ready(() => { loadOptions((opts) => { optionsToForm(opts); $('.options-general input').change(onOptionsGeneralChanged); $('.options-anki input, .options-anki select').change(onOptionsAnkiChanged); - $('.anki-model').change(onModelChanged); - $('#enable-anki-connect').change(() => { - if ($('#enable-anki-connect').prop('checked')) { + $('.anki-model').change((e) => populateAnkiFields($(e.currentTarget))); + $('#enable-anki-connect').change((e) => { + if ($(e.currentTarget).prop('checked')) { $('.options-anki').fadeIn(); } else { $('.options-anki').fadeOut(); -- cgit v1.2.3