aboutsummaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/anki.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-24 13:38:48 -0400
committerGitHub <noreply@github.com>2020-05-24 13:38:48 -0400
commit3089bb7908e42e9101241476f700033df82e685d (patch)
tree47fa735e94a220d404cfc4fe86cd99d1df3c3ca6 /ext/bg/js/settings/anki.js
parenta595a0a48193f28f248191d146f5e476a2f04df6 (diff)
Settings refactor (#541)
* Remove debug info * Trigger onOptionsUpdated instead of formWrite when profile changes * Update how Anki field changes are observed * Update how general.enableClipboardPopups setting is changed * Change where ankiTemplatesUpdateValue occurs * Change where onDictionaryOptionsChanged occurs * Remove unused global declarations * Remove stray data attribute
Diffstat (limited to 'ext/bg/js/settings/anki.js')
-rw-r--r--ext/bg/js/settings/anki.js38
1 files changed, 28 insertions, 10 deletions
diff --git a/ext/bg/js/settings/anki.js b/ext/bg/js/settings/anki.js
index ba83f994..8de97554 100644
--- a/ext/bg/js/settings/anki.js
+++ b/ext/bg/js/settings/anki.js
@@ -19,16 +19,12 @@
* api
* getOptionsContext
* getOptionsMutable
- * onFormOptionsChanged
* settingsSaveOptions
* utilBackgroundIsolate
*/
// Private
-let _ankiDataPopulated = false;
-
-
function _ankiSpinnerShow(show) {
const spinner = $('#anki-spinner');
if (show) {
@@ -158,7 +154,7 @@ async function _ankiFieldsPopulate(tabId, options) {
container.appendChild(fragment);
for (const node of container.querySelectorAll('.anki-field-value')) {
- node.addEventListener('change', onFormOptionsChanged, false);
+ node.addEventListener('change', _onAnkiFieldsChanged, false);
}
for (const node of container.querySelectorAll('.marker-link')) {
node.addEventListener('click', _onAnkiMarkerClicked, false);
@@ -203,6 +199,23 @@ async function _onAnkiModelChanged(e) {
await _ankiFieldsPopulate(tabId, options);
}
+async function _onAnkiFieldsChanged() {
+ const optionsContext = getOptionsContext();
+ const options = await getOptionsMutable(optionsContext);
+
+ options.anki.terms.deck = $('#anki-terms-deck').val();
+ options.anki.terms.model = $('#anki-terms-model').val();
+ options.anki.terms.fields = utilBackgroundIsolate(ankiFieldsToDict(document.querySelectorAll('#terms .anki-field-value')));
+ options.anki.kanji.deck = $('#anki-kanji-deck').val();
+ options.anki.kanji.model = $('#anki-kanji-model').val();
+ options.anki.kanji.fields = utilBackgroundIsolate(ankiFieldsToDict(document.querySelectorAll('#kanji .anki-field-value')));
+
+ await settingsSaveOptions();
+
+ await onAnkiOptionsChanged(options);
+}
+
+
// Public
@@ -272,20 +285,25 @@ function ankiGetFieldMarkers(type) {
function ankiInitialize() {
+ $('#anki-fields-container input,#anki-fields-container select,#anki-fields-container textarea').change(_onAnkiFieldsChanged);
+
for (const node of document.querySelectorAll('#anki-terms-model,#anki-kanji-model')) {
node.addEventListener('change', _onAnkiModelChanged, false);
}
+
+ onAnkiOptionsChanged();
}
-async function onAnkiOptionsChanged(options) {
+async function onAnkiOptionsChanged(options=null) {
+ if (options === null) {
+ const optionsContext = getOptionsContext();
+ options = await getOptionsMutable(optionsContext);
+ }
+
if (!options.anki.enable) {
- _ankiDataPopulated = false;
return;
}
- if (_ankiDataPopulated) { return; }
-
await _ankiDeckAndModelPopulate(options);
- _ankiDataPopulated = true;
await Promise.all([_ankiFieldsPopulate('terms', options), _ankiFieldsPopulate('kanji', options)]);
}