Update AnkiController (#581)

* Update how fields are populated

* Update how fields are modified after a model change

* Update how _onFieldsChanged assigns fields

* Update how spinner is hidden

* Remove jQuery usage

* Use non-jQuery events
This commit is contained in:
toasted-nutbread 2020-05-30 16:22:51 -04:00 committed by GitHub
parent ad8df26b6b
commit c8810bc929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 32 deletions

View File

@ -16,7 +16,6 @@
*/ */
#anki-spinner,
#dict-spinner, #dict-import-progress, #dict-spinner, #dict-import-progress,
.storage-hidden { .storage-hidden {
display: none; display: none;

View File

@ -17,7 +17,6 @@
/* global /* global
* api * api
* utilBackgroundIsolate
*/ */
class AnkiController { class AnkiController {
@ -26,10 +25,12 @@ class AnkiController {
} }
async prepare() { async prepare() {
$('#anki-fields-container input,#anki-fields-container select,#anki-fields-container textarea').change(this._onFieldsChanged.bind(this)); for (const element of document.querySelectorAll('#anki-fields-container input,#anki-fields-container select')) {
element.addEventListener('change', this._onFieldsChanged.bind(this), false);
}
for (const node of document.querySelectorAll('#anki-terms-model,#anki-kanji-model')) { for (const element of document.querySelectorAll('#anki-terms-model,#anki-kanji-model')) {
node.addEventListener('change', this._onModelChanged.bind(this), false); element.addEventListener('change', this._onModelChanged.bind(this), false);
} }
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this)); this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
@ -97,8 +98,8 @@ class AnkiController {
await this._deckAndModelPopulate(options); await this._deckAndModelPopulate(options);
await Promise.all([ await Promise.all([
this._fieldsPopulate('terms', options), this._populateFields('terms', options.anki.terms.fields),
this._fieldsPopulate('kanji', options) this._populateFields('kanji', options.anki.kanji.fields)
]); ]);
} }
@ -111,12 +112,8 @@ class AnkiController {
} }
_spinnerShow(show) { _spinnerShow(show) {
const spinner = $('#anki-spinner'); const spinner = document.querySelector('#anki-spinner');
if (show) { spinner.hidden = !show;
spinner.show();
} else {
spinner.hide();
}
} }
_setError(error) { _setError(error) {
@ -222,15 +219,13 @@ class AnkiController {
return content; return content;
} }
async _fieldsPopulate(tabId, options) { async _populateFields(tabId, fields) {
const tab = document.querySelector(`.tab-pane[data-anki-card-type=${tabId}]`); const tab = document.querySelector(`.tab-pane[data-anki-card-type=${tabId}]`);
const container = tab.querySelector('tbody'); const container = tab.querySelector('tbody');
const markers = this.getFieldMarkers(tabId); const markers = this.getFieldMarkers(tabId);
const fragment = document.createDocumentFragment(); const fragment = document.createDocumentFragment();
const fields = options.anki[tabId].fields; for (const [name, value] of Object.entries(fields)) {
for (const name of Object.keys(fields)) {
const value = fields[name];
const html = this._createFieldTemplate(name, value, markers); const html = this._createFieldTemplate(name, value, markers);
fragment.appendChild(html); fragment.appendChild(html);
} }
@ -249,7 +244,7 @@ class AnkiController {
_onMarkerClicked(e) { _onMarkerClicked(e) {
e.preventDefault(); e.preventDefault();
const link = e.currentTarget; const link = e.currentTarget;
const input = $(link).closest('.input-group').find('.anki-field-value')[0]; const input = link.closest('.input-group').querySelector('.anki-field-value');
input.value = `{${link.textContent}}`; input.value = `{${link.textContent}}`;
input.dispatchEvent(new Event('change')); input.dispatchEvent(new Event('change'));
} }
@ -276,23 +271,27 @@ class AnkiController {
fields[name] = ''; fields[name] = '';
} }
const options = await this._settingsController.getOptionsMutable(); await this._settingsController.setProfileSetting(`anki["${tabId}"].fields`, fields);
options.anki[tabId].fields = utilBackgroundIsolate(fields); await this._populateFields(tabId, fields);
await this._settingsController.save();
await this._fieldsPopulate(tabId, options);
} }
async _onFieldsChanged() { async _onFieldsChanged() {
const options = await this._settingsController.getOptionsMutable(); const termsDeck = document.querySelector('#anki-terms-deck').value;
const termsModel = document.querySelector('#anki-terms-model').value;
const termsFields = this._fieldsToDict(document.querySelectorAll('#terms .anki-field-value'));
const kanjiDeck = document.querySelector('#anki-kanji-deck').value;
const kanjiModel = document.querySelector('#anki-kanji-model').value;
const kanjiFields = this._fieldsToDict(document.querySelectorAll('#kanji .anki-field-value'));
options.anki.terms.deck = $('#anki-terms-deck').val(); const targets = [
options.anki.terms.model = $('#anki-terms-model').val(); {action: 'set', path: 'anki.terms.deck', value: termsDeck},
options.anki.terms.fields = utilBackgroundIsolate(this._fieldsToDict(document.querySelectorAll('#terms .anki-field-value'))); {action: 'set', path: 'anki.terms.model', value: termsModel},
options.anki.kanji.deck = $('#anki-kanji-deck').val(); {action: 'set', path: 'anki.terms.fields', value: termsFields},
options.anki.kanji.model = $('#anki-kanji-model').val(); {action: 'set', path: 'anki.kanji.deck', value: kanjiDeck},
options.anki.kanji.fields = utilBackgroundIsolate(this._fieldsToDict(document.querySelectorAll('#kanji .anki-field-value'))); {action: 'set', path: 'anki.kanji.model', value: kanjiModel},
{action: 'set', path: 'anki.kanji.fields', value: kanjiFields}
];
await this._settingsController.save(); await this._settingsController.modifyProfileSettings(targets);
} }
} }

View File

@ -770,7 +770,7 @@
<div> <div>
<div> <div>
<img src="/mixed/img/spinner.gif" class="pull-right" id="anki-spinner" alt> <img src="/mixed/img/spinner.gif" class="pull-right" id="anki-spinner" alt hidden>
<h3>Anki Options</h3> <h3>Anki Options</h3>
</div> </div>