summaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/anki-templates.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-30 09:33:13 -0400
committerGitHub <noreply@github.com>2020-05-30 09:33:13 -0400
commit63a3e56367b95f7ea64a5701d17179de60ed8718 (patch)
treefc811028b127abf9c1ff1dbdbb4663ba70b60649 /ext/bg/js/settings/anki-templates.js
parent1a5a37c9e47dc4d1f1e1b3ffaf990e792140b912 (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-templates.js')
-rw-r--r--ext/bg/js/settings/anki-templates.js35
1 files changed, 18 insertions, 17 deletions
diff --git a/ext/bg/js/settings/anki-templates.js b/ext/bg/js/settings/anki-templates.js
index dd128ab8..4ceff835 100644
--- a/ext/bg/js/settings/anki-templates.js
+++ b/ext/bg/js/settings/anki-templates.js
@@ -18,19 +18,20 @@
/* global
* AnkiNoteBuilder
* api
- * getOptionsContext
- * getOptionsMutable
- * settingsSaveOptions
*/
class AnkiTemplatesController {
- constructor(ankiController) {
+ constructor(settingsController, ankiController) {
+ this._settingsController = settingsController;
this._ankiController = ankiController;
this._cachedDefinitionValue = null;
this._cachedDefinitionText = null;
+ this._defaultFieldTemplates = null;
}
- prepare() {
+ async prepare() {
+ this._defaultFieldTemplates = await api.getDefaultAnkiFieldTemplates();
+
const markers = new Set([
...this._ankiController.getFieldMarkers('terms'),
...this._ankiController.getFieldMarkers('kanji')
@@ -48,21 +49,22 @@ class AnkiTemplatesController {
$('#field-templates-reset').on('click', this._onReset.bind(this));
$('#field-templates-reset-confirm').on('click', this._onResetConfirm.bind(this));
- this.updateValue();
+ this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
+
+ const options = await this._settingsController.getOptions();
+ this._onOptionsChanged({options});
}
- async updateValue() {
- const optionsContext = getOptionsContext();
- const options = await api.optionsGet(optionsContext);
+ // Private
+
+ _onOptionsChanged({options}) {
let templates = options.anki.fieldTemplates;
- if (typeof templates !== 'string') { templates = await api.getDefaultAnkiFieldTemplates(); }
+ if (typeof templates !== 'string') { templates = this._defaultFieldTemplates; }
$('#field-templates').val(templates);
this._onValidateCompile();
}
- // Private
-
_onReset(e) {
e.preventDefault();
$('#field-template-reset-modal').modal('show');
@@ -89,10 +91,9 @@ class AnkiTemplatesController {
}
// Overwrite
- const optionsContext = getOptionsContext();
- const options = await getOptionsMutable(optionsContext);
+ const options = await this._settingsController.getOptionsMutable();
options.anki.fieldTemplates = templates;
- await settingsSaveOptions();
+ await this._settingsController.save();
// Compile
this._onValidateCompile();
@@ -133,10 +134,10 @@ class AnkiTemplatesController {
const exceptions = [];
let result = `No definition found for ${text}`;
try {
- const optionsContext = getOptionsContext();
+ const optionsContext = this._settingsController.getOptionsContext();
const definition = await this._getDefinition(text, optionsContext);
if (definition !== null) {
- const options = await api.optionsGet(optionsContext);
+ const options = await this._settingsController.getOptions();
const context = {
document: {
title: document.title