Fix dictionary settings not using the current profile

This commit is contained in:
toasted-nutbread 2019-11-12 20:13:25 -05:00
parent 72e7a42c8d
commit db4a303365
2 changed files with 27 additions and 10 deletions

View File

@ -21,28 +21,40 @@ let dictionaryUI = null;
class SettingsDictionaryListUI { class SettingsDictionaryListUI {
constructor(container, template, extraContainer, extraTemplate, optionsDictionaries) { constructor(container, template, extraContainer, extraTemplate) {
this.container = container; this.container = container;
this.template = template; this.template = template;
this.extraContainer = extraContainer; this.extraContainer = extraContainer;
this.extraTemplate = extraTemplate; this.extraTemplate = extraTemplate;
this.optionsDictionaries = optionsDictionaries; this.optionsDictionaries = null;
this.dictionaries = null;
this.dictionaryEntries = []; this.dictionaryEntries = [];
this.extra = null; this.extra = null;
document.querySelector('#dict-delete-confirm').addEventListener('click', (e) => this.onDictionaryConfirmDelete(e), false); document.querySelector('#dict-delete-confirm').addEventListener('click', (e) => this.onDictionaryConfirmDelete(e), false);
} }
setOptionsDictionaries(optionsDictionaries) {
this.optionsDictionaries = optionsDictionaries;
if (this.dictionaries !== null) {
this.setDictionaries(this.dictionaries);
}
}
setDictionaries(dictionaries) { setDictionaries(dictionaries) {
for (const dictionaryEntry of this.dictionaryEntries) { for (const dictionaryEntry of this.dictionaryEntries) {
dictionaryEntry.cleanup(); dictionaryEntry.cleanup();
} }
this.dictionaryEntries = []; this.dictionaryEntries = [];
this.dictionaries = toIterable(dictionaries);
if (this.optionsDictionaries === null) {
return;
}
let changed = false; let changed = false;
for (const dictionaryInfo of toIterable(dictionaries)) { for (const dictionaryInfo of this.dictionaries) {
if (this.createEntry(dictionaryInfo)) { if (this.createEntry(dictionaryInfo)) {
changed = true; changed = true;
} }
@ -52,7 +64,7 @@ class SettingsDictionaryListUI {
const titles = this.dictionaryEntries.map(e => e.dictionaryInfo.title); const titles = this.dictionaryEntries.map(e => e.dictionaryInfo.title);
const removeKeys = Object.keys(this.optionsDictionaries).filter(key => titles.indexOf(key) < 0); const removeKeys = Object.keys(this.optionsDictionaries).filter(key => titles.indexOf(key) < 0);
if (removeKeys.length >= 0) { if (removeKeys.length > 0) {
for (const key of toIterable(removeKeys)) { for (const key of toIterable(removeKeys)) {
delete this.optionsDictionaries[key]; delete this.optionsDictionaries[key];
} }
@ -331,15 +343,11 @@ class SettingsDictionaryExtraUI {
async function dictSettingsInitialize() { async function dictSettingsInitialize() {
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
dictionaryUI = new SettingsDictionaryListUI( dictionaryUI = new SettingsDictionaryListUI(
document.querySelector('#dict-groups'), document.querySelector('#dict-groups'),
document.querySelector('#dict-template'), document.querySelector('#dict-template'),
document.querySelector('#dict-groups-extra'), document.querySelector('#dict-groups-extra'),
document.querySelector('#dict-extra-template'), document.querySelector('#dict-extra-template')
options.dictionaries
); );
dictionaryUI.save = () => settingsSaveOptions(); dictionaryUI.save = () => settingsSaveOptions();
@ -349,9 +357,17 @@ async function dictSettingsInitialize() {
document.querySelector('#dict-file').addEventListener('change', (e) => onDictionaryImport(e), false); document.querySelector('#dict-file').addEventListener('change', (e) => onDictionaryImport(e), false);
document.querySelector('#dict-main').addEventListener('change', (e) => onDictionaryMainChanged(e), false); document.querySelector('#dict-main').addEventListener('change', (e) => onDictionaryMainChanged(e), false);
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
onDictionaryOptionsChanged(options);
onDatabaseUpdated(options); onDatabaseUpdated(options);
} }
async function onDictionaryOptionsChanged(options) {
if (dictionaryUI === null) { return; }
dictionaryUI.setOptionsDictionaries(options.dictionaries);
}
async function onDatabaseUpdated(options) { async function onDatabaseUpdated(options) {
try { try {
const dictionaries = await utilDatabaseGetDictionaryInfo(); const dictionaries = await utilDatabaseGetDictionaryInfo();

View File

@ -135,6 +135,7 @@ async function formWrite(options) {
$('#field-templates').val(options.anki.fieldTemplates); $('#field-templates').val(options.anki.fieldTemplates);
onAnkiTemplatesValidateCompile(); onAnkiTemplatesValidateCompile();
await onDictionaryOptionsChanged(options);
try { try {
await ankiDeckAndModelPopulate(options); await ankiDeckAndModelPopulate(options);