This commit is contained in:
Alex Yatskov 2016-05-28 14:00:35 -07:00
parent 4103b09893
commit 281b8018a6

View File

@ -27,10 +27,13 @@ function optionsToForm(opts) {
$('#load-enamdict').prop('checked', opts.loadEnamDict); $('#load-enamdict').prop('checked', opts.loadEnamDict);
$('#select-matched-text').prop('checked', opts.selectMatchedText); $('#select-matched-text').prop('checked', opts.selectMatchedText);
$('#enable-anki-connect').prop('checked', opts.enableAnkiConnect); $('#enable-anki-connect').prop('checked', opts.enableAnkiConnect);
$('#anki-vocab-deck').val(opts.ankiVocabDeck);
$('#anki-vocab-model').val(opts.ankiVocabModel); if (opts.enableAnkiConnect) {
$('#anki-kanji-deck').val(opts.ankiKanjiDeck); $('#anki-vocab-deck').val(opts.ankiVocabDeck);
$('#anki-kanji-model').val(opts.ankiKanjiModel); $('#anki-vocab-model').val(opts.ankiVocabModel);
$('#anki-kanji-deck').val(opts.ankiKanjiDeck);
$('#anki-kanji-model').val(opts.ankiKanjiModel);
}
} }
function formToOptions(section, callback) { 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) { function onOptionsGeneralChanged(e) {
if (!e.originalEvent) { if (!e.originalEvent) {
return; 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(() => { $(document).ready(() => {
loadOptions((opts) => { loadOptions((opts) => {
optionsToForm(opts); optionsToForm(opts);
$('.options-general input').change(onOptionsGeneralChanged); $('.options-general input').change(onOptionsGeneralChanged);
$('.options-anki input, .options-anki select').change(onOptionsAnkiChanged); $('.options-anki input, .options-anki select').change(onOptionsAnkiChanged);
$('.anki-model').change(onModelChanged); $('.anki-model').change((e) => populateAnkiFields($(e.currentTarget)));
$('#enable-anki-connect').change(() => { $('#enable-anki-connect').change((e) => {
if ($('#enable-anki-connect').prop('checked')) { if ($(e.currentTarget).prop('checked')) {
$('.options-anki').fadeIn(); $('.options-anki').fadeIn();
} else { } else {
$('.options-anki').fadeOut(); $('.options-anki').fadeOut();