diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2020-05-30 09:33:13 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-30 09:33:13 -0400 |
commit | 63a3e56367b95f7ea64a5701d17179de60ed8718 (patch) | |
tree | fc811028b127abf9c1ff1dbdbb4663ba70b60649 /ext/bg/js/settings/anki.js | |
parent | 1a5a37c9e47dc4d1f1e1b3ffaf990e792140b912 (diff) |
Use SettingsController (#576)
* Use settingsController internally in settings/main.js
* Replace modifyingProfileChange with SettingsController.optionsContextChanged
* Update ClipboardPopupsController to use SettingsController
* Store reference to checkbox
* Use this._settingsController for everything
* Change where current profile is initially assigned from
* Remove some unnecessary async calls
* Move setup calls
* Update AnkiTemplatesController to use SettingsController
* Cache default field templates
* Update AnkiController to use SettingsController
* Update AudioController to use SettingsController
* Update SettingsBackup to use SettingsController
* Update DictionaryController to use SettingsController
* Update GenericSettingController to use SettingsController
* Update ProfileController to use SettingsController
* Remove unused
* Remove unused
* Replace some uses of api.options* functions
* Fix missing awaits
* Fix invalid function
Diffstat (limited to 'ext/bg/js/settings/anki.js')
-rw-r--r-- | ext/bg/js/settings/anki.js | 53 |
1 files changed, 24 insertions, 29 deletions
diff --git a/ext/bg/js/settings/anki.js b/ext/bg/js/settings/anki.js index d110ef39..d099239d 100644 --- a/ext/bg/js/settings/anki.js +++ b/ext/bg/js/settings/anki.js @@ -17,38 +17,25 @@ /* global * api - * getOptionsContext - * getOptionsMutable - * settingsSaveOptions * utilBackgroundIsolate */ class AnkiController { - prepare() { + constructor(settingsController) { + this._settingsController = settingsController; + } + + async prepare() { $('#anki-fields-container input,#anki-fields-container select,#anki-fields-container textarea').change(this._onFieldsChanged.bind(this)); for (const node of document.querySelectorAll('#anki-terms-model,#anki-kanji-model')) { node.addEventListener('change', this._onModelChanged.bind(this), false); } - this.optionsChanged(); - } + this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this)); - async optionsChanged(options=null) { - if (options === null) { - const optionsContext = getOptionsContext(); - options = await getOptionsMutable(optionsContext); - } - - if (!options.anki.enable) { - return; - } - - await this._deckAndModelPopulate(options); - await Promise.all([ - this._fieldsPopulate('terms', options), - this._fieldsPopulate('kanji', options) - ]); + const options = await this._settingsController.getOptions(); + this._onOptionsChanged({options}); } getFieldMarkers(type) { @@ -103,6 +90,18 @@ class AnkiController { // Private + async _onOptionsChanged({options}) { + if (!options.anki.enable) { + return; + } + + await this._deckAndModelPopulate(options); + await Promise.all([ + this._fieldsPopulate('terms', options), + this._fieldsPopulate('kanji', options) + ]); + } + _fieldsToDict(elements) { const result = {}; for (const element of elements) { @@ -277,17 +276,15 @@ class AnkiController { fields[name] = ''; } - const optionsContext = getOptionsContext(); - const options = await getOptionsMutable(optionsContext); + const options = await this._settingsController.getOptionsMutable(); options.anki[tabId].fields = utilBackgroundIsolate(fields); - await settingsSaveOptions(); + await this._settingsController.save(); await this._fieldsPopulate(tabId, options); } async _onFieldsChanged() { - const optionsContext = getOptionsContext(); - const options = await getOptionsMutable(optionsContext); + const options = await this._settingsController.getOptionsMutable(); options.anki.terms.deck = $('#anki-terms-deck').val(); options.anki.terms.model = $('#anki-terms-model').val(); @@ -296,8 +293,6 @@ class AnkiController { options.anki.kanji.model = $('#anki-kanji-model').val(); options.anki.kanji.fields = utilBackgroundIsolate(this._fieldsToDict(document.querySelectorAll('#kanji .anki-field-value'))); - await settingsSaveOptions(); - - await this.optionsChanged(options); + await this._settingsController.save(); } } |