Field selector working

This commit is contained in:
Alex Yatskov 2016-05-22 13:41:52 -07:00
parent 997947caed
commit 7c06a22451
3 changed files with 46 additions and 15 deletions

View File

@ -17,6 +17,10 @@
*/ */
function yomichan() {
return chrome.extension.getBackgroundPage().yomichan;
}
function optionsToForm(opts) { function optionsToForm(opts) {
$('#activateOnStartup').prop('checked', opts.activateOnStartup); $('#activateOnStartup').prop('checked', opts.activateOnStartup);
$('#enableAnkiConnect').prop('checked', opts.enableAnkiConnect); $('#enableAnkiConnect').prop('checked', opts.enableAnkiConnect);
@ -35,21 +39,21 @@ function formToOptions() {
}); });
} }
function updateAnkiFormDataVis(opts) { function updateAnkiFormVis(opts) {
if (opts.enableAnkiConnect) { if (opts.enableAnkiConnect) {
updateAnkiFormData(); populateAnkiDeckAndModel();
$('.options-anki').fadeIn(); $('.options-anki').fadeIn();
} else { } else {
$('.options-anki').fadeOut(); $('.options-anki').fadeOut();
} }
} }
function updateAnkiFormData() { function populateAnkiDeckAndModel() {
const yomichan = chrome.extension.getBackgroundPage().yomichan; const yomi = yomichan();
const ankiDeck = $('.ankiDeck'); const ankiDeck = $('.ankiDeck');
ankiDeck.find('option').remove(); ankiDeck.find('option').remove();
yomichan.api_getDeckNames({callback: (names) => { yomi.api_getDeckNames({callback: (names) => {
if (names !== null) { if (names !== null) {
names.forEach((name) => ankiDeck.append($('<option/>', {value: name, text: name}))); names.forEach((name) => ankiDeck.append($('<option/>', {value: name, text: name})));
} }
@ -57,25 +61,52 @@ function updateAnkiFormData() {
const ankiModel = $('.ankiModel'); const ankiModel = $('.ankiModel');
ankiModel.find('option').remove(); ankiModel.find('option').remove();
yomichan.api_getModelNames({callback: (names) => { yomi.api_getModelNames({callback: (names) => {
if (names !== null) { if (names !== null) {
names.forEach((name) => ankiModel.append($('<option/>', {value: name, text: name}))); names.forEach((name) => ankiModel.append($('<option/>', {value: name, text: name})));
$('.ankiModel').trigger('change');
} }
}}); }});
} }
function onOptionsChanged() { function onOptionsChanged() {
const opts = formToOptions(); const opts = formToOptions();
saveOptions(opts, () => { saveOptions(opts, () => {
chrome.extension.getBackgroundPage().yomichan.setOptions(opts); yomichan().setOptions(opts);
updateAnkiFormDataVis(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(() => { $(document).ready(() => {
loadOptions((opts) => { loadOptions((opts) => {
optionsToForm(opts); optionsToForm(opts);
updateAnkiFormDataVis(opts);
$('input').on('input paste change', onOptionsChanged); $('input').on('input paste change', onOptionsChanged);
$('.ankiModel').change(onModelChanged);
updateAnkiFormVis(opts);
}); });
}); });

View File

@ -156,8 +156,8 @@ class Yomichan {
this.ankiInvoke('modelNames', {}, null, callback); this.ankiInvoke('modelNames', {}, null, callback);
} }
api_getModelFieldNames({callback}) { api_getModelFieldNames({modelName, callback}) {
this.ankiInvoke('modelFieldNames', {}, null, callback); this.ankiInvoke('modelFieldNames', {modelName}, null, callback);
} }
api_getOptions({callback}) { api_getOptions({callback}) {

View File

@ -93,14 +93,14 @@
</div> </div>
</div> </div>
<table class="table table-bordered"> <table class="table table-bordered ankiFields">
<thead> <thead>
<tr> <tr>
<th>Field</th> <th>Field</th>
<th>Value</th> <th>Value</th>
</tr> </tr>
</thead> </thead>
<tbody class="ankiFields"> <tbody>
</tbody> </tbody>
</table> </table>
</div> </div>
@ -119,14 +119,14 @@
</div> </div>
</div> </div>
<table class="table table-bordered"> <table class="table table-bordered ankiFields">
<thead> <thead>
<tr> <tr>
<th>Field</th> <th>Field</th>
<th>Value</th> <th>Value</th>
</tr> </tr>
</thead> </thead>
<tbody class="ankiFields"> <tbody>
</tbody> </tbody>
</table> </table>
</div> </div>