summaryrefslogtreecommitdiff
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.js216
1 files changed, 111 insertions, 105 deletions
diff --git a/ext/bg/js/settings/anki-templates.js b/ext/bg/js/settings/anki-templates.js
index 0dadb433..dd128ab8 100644
--- a/ext/bg/js/settings/anki-templates.js
+++ b/ext/bg/js/settings/anki-templates.js
@@ -17,141 +17,147 @@
/* global
* AnkiNoteBuilder
- * ankiGetFieldMarkers
- * ankiGetFieldMarkersHtml
* api
* getOptionsContext
* getOptionsMutable
* settingsSaveOptions
*/
-function onAnkiFieldTemplatesReset(e) {
- e.preventDefault();
- $('#field-template-reset-modal').modal('show');
-}
-
-async function onAnkiFieldTemplatesResetConfirm(e) {
- e.preventDefault();
+class AnkiTemplatesController {
+ constructor(ankiController) {
+ this._ankiController = ankiController;
+ this._cachedDefinitionValue = null;
+ this._cachedDefinitionText = null;
+ }
- $('#field-template-reset-modal').modal('hide');
+ prepare() {
+ const markers = new Set([
+ ...this._ankiController.getFieldMarkers('terms'),
+ ...this._ankiController.getFieldMarkers('kanji')
+ ]);
+ const fragment = this._ankiController.getFieldMarkersHtml(markers);
+
+ const list = document.querySelector('#field-templates-list');
+ list.appendChild(fragment);
+ for (const node of list.querySelectorAll('.marker-link')) {
+ node.addEventListener('click', this._onMarkerClicked.bind(this), false);
+ }
- const value = await api.getDefaultAnkiFieldTemplates();
+ $('#field-templates').on('change', this._onChanged.bind(this));
+ $('#field-template-render').on('click', this._onRender.bind(this));
+ $('#field-templates-reset').on('click', this._onReset.bind(this));
+ $('#field-templates-reset-confirm').on('click', this._onResetConfirm.bind(this));
- const element = document.querySelector('#field-templates');
- element.value = value;
- element.dispatchEvent(new Event('change'));
-}
+ this.updateValue();
+ }
-function ankiTemplatesInitialize() {
- const markers = new Set(ankiGetFieldMarkers('terms').concat(ankiGetFieldMarkers('kanji')));
- const fragment = ankiGetFieldMarkersHtml(markers);
+ async updateValue() {
+ const optionsContext = getOptionsContext();
+ const options = await api.optionsGet(optionsContext);
+ let templates = options.anki.fieldTemplates;
+ if (typeof templates !== 'string') { templates = await api.getDefaultAnkiFieldTemplates(); }
+ $('#field-templates').val(templates);
- const list = document.querySelector('#field-templates-list');
- list.appendChild(fragment);
- for (const node of list.querySelectorAll('.marker-link')) {
- node.addEventListener('click', onAnkiTemplateMarkerClicked, false);
+ this._onValidateCompile();
}
- $('#field-templates').on('change', onAnkiFieldTemplatesChanged);
- $('#field-template-render').on('click', onAnkiTemplateRender);
- $('#field-templates-reset').on('click', onAnkiFieldTemplatesReset);
- $('#field-templates-reset-confirm').on('click', onAnkiFieldTemplatesResetConfirm);
+ // Private
- ankiTemplatesUpdateValue();
-}
+ _onReset(e) {
+ e.preventDefault();
+ $('#field-template-reset-modal').modal('show');
+ }
-async function ankiTemplatesUpdateValue() {
- const optionsContext = getOptionsContext();
- const options = await api.optionsGet(optionsContext);
- let templates = options.anki.fieldTemplates;
- if (typeof templates !== 'string') { templates = await api.getDefaultAnkiFieldTemplates(); }
- $('#field-templates').val(templates);
+ async _onResetConfirm(e) {
+ e.preventDefault();
- onAnkiTemplatesValidateCompile();
-}
+ $('#field-template-reset-modal').modal('hide');
-const ankiTemplatesValidateGetDefinition = (() => {
- let cachedValue = null;
- let cachedText = null;
+ const value = await api.getDefaultAnkiFieldTemplates();
- return async (text, optionsContext) => {
- if (cachedText !== text) {
- const {definitions} = await api.termsFind(text, {}, optionsContext);
- if (definitions.length === 0) { return null; }
+ const element = document.querySelector('#field-templates');
+ element.value = value;
+ element.dispatchEvent(new Event('change'));
+ }
- cachedValue = definitions[0];
- cachedText = text;
+ async _onChanged(e) {
+ // Get value
+ let templates = e.currentTarget.value;
+ if (templates === await api.getDefaultAnkiFieldTemplates()) {
+ // Default
+ templates = null;
}
- return cachedValue;
- };
-})();
-
-async function ankiTemplatesValidate(infoNode, field, mode, showSuccessResult, invalidateInput) {
- const text = document.querySelector('#field-templates-preview-text').value || '';
- const exceptions = [];
- let result = `No definition found for ${text}`;
- try {
+
+ // Overwrite
const optionsContext = getOptionsContext();
- const definition = await ankiTemplatesValidateGetDefinition(text, optionsContext);
- if (definition !== null) {
- const options = await api.optionsGet(optionsContext);
- const context = {
- document: {
- title: document.title
- }
- };
- let templates = options.anki.fieldTemplates;
- if (typeof templates !== 'string') { templates = await api.getDefaultAnkiFieldTemplates(); }
- const ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: api.templateRender.bind(api)});
- result = await ankiNoteBuilder.formatField(field, definition, mode, context, options, templates, exceptions);
- }
- } catch (e) {
- exceptions.push(e);
+ const options = await getOptionsMutable(optionsContext);
+ options.anki.fieldTemplates = templates;
+ await settingsSaveOptions();
+
+ // Compile
+ this._onValidateCompile();
}
- const hasException = exceptions.length > 0;
- infoNode.hidden = !(showSuccessResult || hasException);
- infoNode.textContent = hasException ? exceptions.map((e) => `${e}`).join('\n') : (showSuccessResult ? result : '');
- infoNode.classList.toggle('text-danger', hasException);
- if (invalidateInput) {
- const input = document.querySelector('#field-templates');
- input.classList.toggle('is-invalid', hasException);
+ _onValidateCompile() {
+ const infoNode = document.querySelector('#field-template-compile-result');
+ this._validate(infoNode, '{expression}', 'term-kanji', false, true);
}
-}
-async function onAnkiFieldTemplatesChanged(e) {
- // Get value
- let templates = e.currentTarget.value;
- if (templates === await api.getDefaultAnkiFieldTemplates()) {
- // Default
- templates = null;
+ _onMarkerClicked(e) {
+ e.preventDefault();
+ document.querySelector('#field-template-render-text').value = `{${e.target.textContent}}`;
}
- // Overwrite
- const optionsContext = getOptionsContext();
- const options = await getOptionsMutable(optionsContext);
- options.anki.fieldTemplates = templates;
- await settingsSaveOptions();
+ _onRender(e) {
+ e.preventDefault();
- // Compile
- onAnkiTemplatesValidateCompile();
-}
+ const field = document.querySelector('#field-template-render-text').value;
+ const infoNode = document.querySelector('#field-template-render-result');
+ infoNode.hidden = true;
+ this._validate(infoNode, field, 'term-kanji', true, false);
+ }
-function onAnkiTemplatesValidateCompile() {
- const infoNode = document.querySelector('#field-template-compile-result');
- ankiTemplatesValidate(infoNode, '{expression}', 'term-kanji', false, true);
-}
+ async _getDefinition(text, optionsContext) {
+ if (this._cachedDefinitionText !== text) {
+ const {definitions} = await api.termsFind(text, {}, optionsContext);
+ if (definitions.length === 0) { return null; }
-function onAnkiTemplateMarkerClicked(e) {
- e.preventDefault();
- document.querySelector('#field-template-render-text').value = `{${e.target.textContent}}`;
-}
+ this._cachedDefinitionValue = definitions[0];
+ this._cachedDefinitionText = text;
+ }
+ return this._cachedDefinitionValue;
+ }
-function onAnkiTemplateRender(e) {
- e.preventDefault();
+ async _validate(infoNode, field, mode, showSuccessResult, invalidateInput) {
+ const text = document.querySelector('#field-templates-preview-text').value || '';
+ const exceptions = [];
+ let result = `No definition found for ${text}`;
+ try {
+ const optionsContext = getOptionsContext();
+ const definition = await this._getDefinition(text, optionsContext);
+ if (definition !== null) {
+ const options = await api.optionsGet(optionsContext);
+ const context = {
+ document: {
+ title: document.title
+ }
+ };
+ let templates = options.anki.fieldTemplates;
+ if (typeof templates !== 'string') { templates = await api.getDefaultAnkiFieldTemplates(); }
+ const ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: api.templateRender.bind(api)});
+ result = await ankiNoteBuilder.formatField(field, definition, mode, context, options, templates, exceptions);
+ }
+ } catch (e) {
+ exceptions.push(e);
+ }
- const field = document.querySelector('#field-template-render-text').value;
- const infoNode = document.querySelector('#field-template-render-result');
- infoNode.hidden = true;
- ankiTemplatesValidate(infoNode, field, 'term-kanji', true, false);
+ const hasException = exceptions.length > 0;
+ infoNode.hidden = !(showSuccessResult || hasException);
+ infoNode.textContent = hasException ? exceptions.map((e) => `${e}`).join('\n') : (showSuccessResult ? result : '');
+ infoNode.classList.toggle('text-danger', hasException);
+ if (invalidateInput) {
+ const input = document.querySelector('#field-templates');
+ input.classList.toggle('is-invalid', hasException);
+ }
+ }
}