Update how anki settings are updated

This commit is contained in:
toasted-nutbread 2019-12-01 22:16:58 -05:00
parent 3e1b080cd3
commit b4cdbf0263
2 changed files with 34 additions and 29 deletions

View File

@ -17,6 +17,9 @@
*/ */
let _ankiDataPopulated = false;
function ankiSpinnerShow(show) { function ankiSpinnerShow(show) {
const spinner = $('#anki-spinner'); const spinner = $('#anki-spinner');
if (show) { if (show) {
@ -165,11 +168,7 @@ function onAnkiMarkerClicked(e) {
async function onAnkiModelChanged(e) { async function onAnkiModelChanged(e) {
try { try {
if (!e.originalEvent) { const element = $(e.currentTarget);
return;
}
const element = $(this);
const tab = element.closest('.tab-pane'); const tab = element.closest('.tab-pane');
const tabId = tab.attr('id'); const tabId = tab.attr('id');
@ -188,3 +187,30 @@ async function onAnkiModelChanged(e) {
ankiSpinnerShow(false); ankiSpinnerShow(false);
} }
} }
function ankiInitialize() {
for (const node of document.querySelectorAll('#anki-terms-model,#anki-kanji-model')) {
node.addEventListener('change', (e) => onAnkiModelChanged(e), false);
}
}
async function onAnkiOptionsChanged(options) {
if (!options.anki.enable) {
_ankiDataPopulated = false;
return;
}
if (_ankiDataPopulated) { return; }
try {
ankiSpinnerShow(true);
await ankiDeckAndModelPopulate(options);
ankiErrorShow();
_ankiDataPopulated = true;
} catch (e) {
ankiErrorShow(e);
} finally {
ankiSpinnerShow(false);
}
}

View File

@ -143,20 +143,14 @@ async function formWrite(options) {
$('#field-templates').val(options.anki.fieldTemplates); $('#field-templates').val(options.anki.fieldTemplates);
onAnkiTemplatesValidateCompile(); onAnkiTemplatesValidateCompile();
await onAnkiOptionsChanged(options);
await onDictionaryOptionsChanged(options); await onDictionaryOptionsChanged(options);
try {
await ankiDeckAndModelPopulate(options);
} catch (e) {
ankiErrorShow(e);
}
formUpdateVisibility(options); formUpdateVisibility(options);
} }
function formSetupEventListeners() { function formSetupEventListeners() {
$('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change((e) => onFormOptionsChanged(e)); $('input, select, textarea').not('.anki-model').not('.ignore-form-changes *').change((e) => onFormOptionsChanged(e));
$('.anki-model').change((e) => onAnkiModelChanged(e));
} }
function formUpdateVisibility(options) { function formUpdateVisibility(options) {
@ -180,28 +174,12 @@ async function onFormOptionsChanged(e) {
const optionsContext = getOptionsContext(); const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext); const options = await apiOptionsGet(optionsContext);
const optionsAnkiEnableOld = options.anki.enable;
const optionsAnkiServerOld = options.anki.server;
await formRead(options); await formRead(options);
await settingsSaveOptions(); await settingsSaveOptions();
formUpdateVisibility(options); formUpdateVisibility(options);
try { await onAnkiOptionsChanged(options);
const ankiUpdated =
options.anki.enable !== optionsAnkiEnableOld ||
options.anki.server !== optionsAnkiServerOld;
if (ankiUpdated) {
ankiSpinnerShow(true);
await ankiDeckAndModelPopulate(options);
ankiErrorShow();
}
} catch (error) {
ankiErrorShow(error);
} finally {
ankiSpinnerShow(false);
}
} }
@ -254,6 +232,7 @@ async function onReady() {
await audioSettingsInitialize(); await audioSettingsInitialize();
await profileOptionsSetup(); await profileOptionsSetup();
await dictSettingsInitialize(); await dictSettingsInitialize();
ankiInitialize();
ankiTemplatesInitialize(); ankiTemplatesInitialize();
storageInfoInitialize(); storageInfoInitialize();