diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-05-28 14:00:35 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-05-28 14:00:35 -0700 |
commit | 281b8018a67c34cc5518904b6c78f1ee0c497f77 (patch) | |
tree | fd2d089df052b42464ac2e4f7093d917109b3bb1 /ext/bg/js | |
parent | 4103b09893ed27a951a5521fd469fa58f4661df6 (diff) |
Cleanup
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/options-form.js | 61 |
1 files changed, 32 insertions, 29 deletions
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 = $('<tbody>'); + names.forEach((name) => { + const row = $('<tr>'); + row.append($('<td>').text(name)); + row.append($('<input>', {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 = $('<tbody>'); - names.forEach((name) => { - const row = $('<tr>'); - row.append($('<td>').text(name)); - row.append($('<input>', {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(); |