This commit is contained in:
Alex Yatskov 2016-08-11 20:56:49 -07:00
parent eb7456089a
commit de2029fb7a
2 changed files with 41 additions and 16 deletions

View File

@ -31,7 +31,17 @@ function fieldsToDict(selection) {
}
function modelIdToFieldOptKey(id) {
return {'anki-term-model': 'ankiTermFields', 'anki-kanji-model': 'ankiKanjiFields'}[id];
return {
'anki-term-model': 'ankiTermFields',
'anki-kanji-model': 'ankiKanjiFields'
}[id];
}
function modelIdToTags(id) {
return {
'anki-term-model': ['audio', 'expression', 'glossary', 'glossary-list', 'reading', 'sentence', 'tags', 'url'],
'anki-kanji-model': ['character', 'glossary', 'glossary-list', 'kunyomi', 'onyomi', 'url'],
}[id];
}
function formToOptions(section, callback) {
@ -111,33 +121,50 @@ function populateAnkiFields(element, opts) {
return;
}
const optKey = modelIdToFieldOptKey(element.attr('id'));
const modelId = element.attr('id');
const optKey = modelIdToFieldOptKey(modelId);
const tags = modelIdToTags(modelId);
yomichan().api_getModelFieldNames({modelName, callback: (names) => {
const table = element.closest('.tab-pane').find('.anki-fields');
table.find('tbody').remove();
const body = $('<tbody>');
const tbody = $('<tbody>');
names.forEach((name) => {
const button = $('<button>', {type: 'button', class: 'btn btn-default dropdown-toggle'});
button.attr('data-toggle', 'dropdown').dropdown();
const tagItems = $('<ul>', {class: 'dropdown-menu dropdown-menu-right'});
for (const tag of tags) {
const link = $('<a>', {href: '#'}).text(`{${tag}}`);
link.click((e) => {
e.preventDefault();
link.closest('.input-group').find('.anki-field-value').val(link.text()).trigger('change');
});
tagItems.append($('<li>').append(link));
}
const groupBtn = $('<div>', {class: 'input-group-btn'});
groupBtn.append($('<button>', {type: 'button', class: 'btn btn-default dropdown-toggle'}).append($('<span>', {class: 'caret'})));
groupBtn.append(button.append($('<span>', {class: 'caret'})));
groupBtn.append(tagItems);
const group = $('<div>', {class: 'input-group'});
group.append($('<input>', {class: 'anki-field-value form-control', value: opts[optKey][name] || ''}).data('field', name).change(onOptionsAnkiChanged));
group.append($('<input>', {type: 'text', class: 'anki-field-value form-control', value: opts[optKey][name] || ''}).data('field', name).change(onOptionsAnkiChanged));
group.append(groupBtn);
const row = $('<tr>');
row.append($('<td>', {class: 'col-sm-2'}).text(name));
row.append($('<td>', {class: 'col-sm-10'}).append(group));
body.append(row);
tbody.append(row);
});
table.append(body);
table.append(tbody);
}});
}
function onOptionsGeneralChanged(e) {
if (!e.originalEvent) {
if (!e.originalEvent && !e.isTrigger) {
return;
}
@ -156,11 +183,13 @@ function onOptionsGeneralChanged(e) {
}
function onOptionsAnkiChanged(e) {
if (e.originalEvent) {
formToOptions('anki', (opts) => {
saveOptions(opts, () => yomichan().setOptions(opts));
});
if (!e.originalEvent && !e.isTrigger) {
return;
}
formToOptions('anki', (opts) => {
saveOptions(opts, () => yomichan().setOptions(opts));
});
}
function onAnkiModelChanged(e) {

View File

@ -128,8 +128,6 @@
<tbody>
</tbody>
</table>
<p>Allowed tags: <code>{audio}</code>, <code>{expression}</code>, <code>{reading}</code>, <code>{glossary}</code>, <code>{glossary-list}</code>, <code>{tags}</code>, <code>{url}</code>, <code>{sentence}</code></p>
</div>
<div id="kanji" class="tab-pane fade">
@ -157,8 +155,6 @@
<tbody>
</tbody>
</table>
<p>Allowed tags: <code>{character}</code>, <code>{onyomi}</code>, <code>{kunyomi}</code>, <code>{glossary}</code>, <code>{glossary-list}</code>, <code>{url}</code></p>
</div>
</div>
</form>