aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/anki-templates.js
diff options
context:
space:
mode:
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