diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-05-22 13:41:52 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-05-22 13:41:52 -0700 |
commit | 7c06a224514ed4288122160bfac84f9ae7521897 (patch) | |
tree | 5f458b8b916ec7cbcf4613609d13e18d9669de67 /ext/bg/js | |
parent | 997947caedc109ba3e21017b38de36e21c4a7255 (diff) |
Field selector working
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/options-form.js | 49 | ||||
-rw-r--r-- | ext/bg/js/yomichan.js | 4 |
2 files changed, 42 insertions, 11 deletions
diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 31756486..06be044a 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -17,6 +17,10 @@ */ +function yomichan() { + return chrome.extension.getBackgroundPage().yomichan; +} + function optionsToForm(opts) { $('#activateOnStartup').prop('checked', opts.activateOnStartup); $('#enableAnkiConnect').prop('checked', opts.enableAnkiConnect); @@ -35,21 +39,21 @@ function formToOptions() { }); } -function updateAnkiFormDataVis(opts) { +function updateAnkiFormVis(opts) { if (opts.enableAnkiConnect) { - updateAnkiFormData(); + populateAnkiDeckAndModel(); $('.options-anki').fadeIn(); } else { $('.options-anki').fadeOut(); } } -function updateAnkiFormData() { - const yomichan = chrome.extension.getBackgroundPage().yomichan; +function populateAnkiDeckAndModel() { + const yomi = yomichan(); const ankiDeck = $('.ankiDeck'); ankiDeck.find('option').remove(); - yomichan.api_getDeckNames({callback: (names) => { + yomi.api_getDeckNames({callback: (names) => { if (names !== null) { names.forEach((name) => ankiDeck.append($('<option/>', {value: name, text: name}))); } @@ -57,25 +61,52 @@ function updateAnkiFormData() { const ankiModel = $('.ankiModel'); ankiModel.find('option').remove(); - yomichan.api_getModelNames({callback: (names) => { + yomi.api_getModelNames({callback: (names) => { if (names !== null) { names.forEach((name) => ankiModel.append($('<option/>', {value: name, text: name}))); + $('.ankiModel').trigger('change'); } }}); + } function onOptionsChanged() { const opts = formToOptions(); saveOptions(opts, () => { - chrome.extension.getBackgroundPage().yomichan.setOptions(opts); - updateAnkiFormDataVis(opts); + yomichan().setOptions(opts); + updateAnkiFormVis(opts); }); } +function onModelChanged() { + const modelName = $(this).val(); + if (modelName === null) { + return; + } + + yomichan().api_getModelFieldNames({modelName, callback: (names) => { + const table = $(this).closest('.tab-pane').find('.ankiFields'); + table.find('tbody').remove(); + + const body = $('<tbody>'); + names.forEach((name) => { + const row = $('<tr>'); + row.append($('<td>').text(name)); + row.append($('<input>', {class: 'ankiFieldValue form-control'}).data('field', name)); + body.append(row); + }); + + table.append(body); + }}); +} + $(document).ready(() => { loadOptions((opts) => { optionsToForm(opts); - updateAnkiFormDataVis(opts); + $('input').on('input paste change', onOptionsChanged); + $('.ankiModel').change(onModelChanged); + + updateAnkiFormVis(opts); }); }); diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js index 1714e778..d62f97aa 100644 --- a/ext/bg/js/yomichan.js +++ b/ext/bg/js/yomichan.js @@ -156,8 +156,8 @@ class Yomichan { this.ankiInvoke('modelNames', {}, null, callback); } - api_getModelFieldNames({callback}) { - this.ankiInvoke('modelFieldNames', {}, null, callback); + api_getModelFieldNames({modelName, callback}) { + this.ankiInvoke('modelFieldNames', {modelName}, null, callback); } api_getOptions({callback}) { |