summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/bg/js/settings/anki.js130
1 files changed, 68 insertions, 62 deletions
diff --git a/ext/bg/js/settings/anki.js b/ext/bg/js/settings/anki.js
index 0a75d229..b7fce011 100644
--- a/ext/bg/js/settings/anki.js
+++ b/ext/bg/js/settings/anki.js
@@ -17,6 +17,8 @@
*/
+// Private
+
let _ankiDataPopulated = false;
@@ -39,19 +41,6 @@ function _ankiErrorShow(error) {
}
}
-function ankiErrorShown() {
- return $('#anki-error').is(':visible');
-}
-
-function ankiFieldsToDict(selection) {
- const result = {};
- selection.each((index, element) => {
- result[$(element).data('field')] = $(element).val();
- });
-
- return result;
-}
-
async function _ankiDeckAndModelPopulate(options) {
const ankiFormat = $('#anki-format').hide();
@@ -89,6 +78,72 @@ function _ankiCreateFieldTemplate(name, value, markers) {
return content;
}
+async function _ankiFieldsPopulate(element, options) {
+ const modelName = element.val();
+ if (!modelName) {
+ return;
+ }
+
+ const tab = element.closest('.tab-pane');
+ const tabId = tab.attr('id');
+ const container = tab.find('tbody').empty();
+ const markers = ankiGetFieldMarkers(tabId);
+
+ for (const name of await utilAnkiGetModelFieldNames(modelName)) {
+ const value = options.anki[tabId].fields[name] || '';
+ const html = _ankiCreateFieldTemplate(name, value, markers);
+ container.append($(html));
+ }
+
+ tab.find('.anki-field-value').change((e) => onFormOptionsChanged(e));
+ tab.find('.marker-link').click((e) => _onAnkiMarkerClicked(e));
+}
+
+function _onAnkiMarkerClicked(e) {
+ e.preventDefault();
+ const link = e.target;
+ $(link).closest('.input-group').find('.anki-field-value').val(`{${link.text}}`).trigger('change');
+}
+
+async function _onAnkiModelChanged(e) {
+ try {
+ const element = $(e.currentTarget);
+ const tab = element.closest('.tab-pane');
+ const tabId = tab.attr('id');
+
+ const optionsContext = getOptionsContext();
+ const options = await apiOptionsGet(optionsContext);
+ await formRead(options);
+ options.anki[tabId].fields = utilBackgroundIsolate({});
+ await settingsSaveOptions();
+
+ _ankiSpinnerShow(true);
+ await _ankiFieldsPopulate(element, options);
+ _ankiErrorShow();
+ } catch (error) {
+ _ankiErrorShow(error);
+ } finally {
+ _ankiSpinnerShow(false);
+ }
+}
+
+
+// Public
+
+function ankiErrorShown() {
+ return $('#anki-error').is(':visible');
+}
+
+function ankiFieldsToDict(selection) {
+ const result = {};
+ selection.each((index, element) => {
+ result[$(element).data('field')] = $(element).val();
+ });
+
+ return result;
+}
+
+
function ankiGetFieldMarkersHtml(markers, fragment) {
const template = document.querySelector('#anki-field-marker-template').content;
if (!fragment) {
@@ -139,55 +194,6 @@ function ankiGetFieldMarkers(type) {
}
}
-async function _ankiFieldsPopulate(element, options) {
- const modelName = element.val();
- if (!modelName) {
- return;
- }
-
- const tab = element.closest('.tab-pane');
- const tabId = tab.attr('id');
- const container = tab.find('tbody').empty();
- const markers = ankiGetFieldMarkers(tabId);
-
- for (const name of await utilAnkiGetModelFieldNames(modelName)) {
- const value = options.anki[tabId].fields[name] || '';
- const html = _ankiCreateFieldTemplate(name, value, markers);
- container.append($(html));
- }
-
- tab.find('.anki-field-value').change((e) => onFormOptionsChanged(e));
- tab.find('.marker-link').click((e) => _onAnkiMarkerClicked(e));
-}
-
-function _onAnkiMarkerClicked(e) {
- e.preventDefault();
- const link = e.target;
- $(link).closest('.input-group').find('.anki-field-value').val(`{${link.text}}`).trigger('change');
-}
-
-async function _onAnkiModelChanged(e) {
- try {
- const element = $(e.currentTarget);
- const tab = element.closest('.tab-pane');
- const tabId = tab.attr('id');
-
- const optionsContext = getOptionsContext();
- const options = await apiOptionsGet(optionsContext);
- await formRead(options);
- options.anki[tabId].fields = utilBackgroundIsolate({});
- await settingsSaveOptions();
-
- _ankiSpinnerShow(true);
- await _ankiFieldsPopulate(element, options);
- _ankiErrorShow();
- } catch (error) {
- _ankiErrorShow(error);
- } finally {
- _ankiSpinnerShow(false);
- }
-}
-
function ankiInitialize() {
for (const node of document.querySelectorAll('#anki-terms-model,#anki-kanji-model')) {