diff options
-rw-r--r-- | ext/bg/js/options-form.js | 53 | ||||
-rw-r--r-- | ext/bg/options.html | 4 |
2 files changed, 41 insertions, 16 deletions
diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index bb9f762b..979f5377 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -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) { diff --git a/ext/bg/options.html b/ext/bg/options.html index 331e5e98..71468412 100644 --- a/ext/bg/options.html +++ b/ext/bg/options.html @@ -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> |