Separate the functionality of updateMainDictionarySelect

One function now updates the options, and another handles updating the value.
The options are no longer mutated due to updating this <select>'s available options.
This commit is contained in:
toasted-nutbread 2020-02-01 11:40:09 -05:00
parent 748cd27ad0
commit 55047def9d

View File

@ -272,9 +272,7 @@ class SettingsDictionaryEntryUI {
this.isDeleting = false; this.isDeleting = false;
progress.hidden = true; progress.hidden = true;
const optionsContext = getOptionsContext(); onDatabaseUpdated();
const options = await getOptionsMutable(optionsContext);
onDatabaseUpdated(options);
} }
} }
@ -359,10 +357,8 @@ async function dictSettingsInitialize() {
document.querySelector('#dict-main').addEventListener('change', (e) => onDictionaryMainChanged(e), false); document.querySelector('#dict-main').addEventListener('change', (e) => onDictionaryMainChanged(e), false);
document.querySelector('#database-enable-prefix-wildcard-searches').addEventListener('change', (e) => onDatabaseEnablePrefixWildcardSearchesChanged(e), false); document.querySelector('#database-enable-prefix-wildcard-searches').addEventListener('change', (e) => onDatabaseEnablePrefixWildcardSearchesChanged(e), false);
const optionsContext = getOptionsContext();
const options = await getOptionsMutable(optionsContext);
onDictionaryOptionsChanged(); onDictionaryOptionsChanged();
onDatabaseUpdated(options); onDatabaseUpdated();
} }
async function onDictionaryOptionsChanged() { async function onDictionaryOptionsChanged() {
@ -377,14 +373,15 @@ async function onDictionaryOptionsChanged() {
document.querySelector('#database-enable-prefix-wildcard-searches').checked = optionsFull.global.database.prefixWildcardsSupported; document.querySelector('#database-enable-prefix-wildcard-searches').checked = optionsFull.global.database.prefixWildcardsSupported;
} }
async function onDatabaseUpdated(options) { async function onDatabaseUpdated() {
try { try {
const dictionaries = await utilDatabaseGetDictionaryInfo(); const dictionaries = await utilDatabaseGetDictionaryInfo();
dictionaryUI.setDictionaries(dictionaries); dictionaryUI.setDictionaries(dictionaries);
document.querySelector('#dict-warning').hidden = (dictionaries.length > 0); document.querySelector('#dict-warning').hidden = (dictionaries.length > 0);
updateMainDictionarySelect(options, dictionaries); updateMainDictionarySelectOptions(dictionaries);
await updateMainDictionarySelectValue();
const {counts, total} = await utilDatabaseGetDictionaryCounts(dictionaries.map((v) => v.title), true); const {counts, total} = await utilDatabaseGetDictionaryCounts(dictionaries.map((v) => v.title), true);
dictionaryUI.setCounts(counts, total); dictionaryUI.setCounts(counts, total);
@ -393,7 +390,7 @@ async function onDatabaseUpdated(options) {
} }
} }
async function updateMainDictionarySelect(options, dictionaries) { function updateMainDictionarySelectOptions(dictionaries) {
const select = document.querySelector('#dict-main'); const select = document.querySelector('#dict-main');
select.textContent = ''; // Empty select.textContent = ''; // Empty
@ -403,8 +400,6 @@ async function updateMainDictionarySelect(options, dictionaries) {
option.textContent = 'Not selected'; option.textContent = 'Not selected';
select.appendChild(option); select.appendChild(option);
let value = '';
const currentValue = options.general.mainDictionary;
for (const {title, sequenced} of toIterable(dictionaries)) { for (const {title, sequenced} of toIterable(dictionaries)) {
if (!sequenced) { continue; } if (!sequenced) { continue; }
@ -412,18 +407,41 @@ async function updateMainDictionarySelect(options, dictionaries) {
option.value = title; option.value = title;
option.textContent = title; option.textContent = title;
select.appendChild(option); select.appendChild(option);
}
}
if (title === currentValue) { async function updateMainDictionarySelectValue() {
value = title; const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const value = options.general.mainDictionary;
const select = document.querySelector('#dict-main');
let selectValue = null;
for (const child of select.children) {
if (child.nodeName.toUpperCase() === 'OPTION' && child.value === value) {
selectValue = value;
break;
}
}
let missingNodeOption = select.querySelector('option[data-not-installed=true]');
if (selectValue === null) {
if (missingNodeOption === null) {
missingNodeOption = document.createElement('option');
missingNodeOption.className = 'text-muted';
missingNodeOption.value = value;
missingNodeOption.textContent = `${value} (Not installed)`;
missingNodeOption.dataset.notInstalled = 'true';
select.appendChild(missingNodeOption);
}
} else {
if (missingNodeOption !== null) {
missingNodeOption.parentNode.removeChild(missingNodeOption);
} }
} }
select.value = value; select.value = value;
if (options.general.mainDictionary !== value) {
options.general.mainDictionary = value;
settingsSaveOptions();
}
} }
async function onDictionaryMainChanged(e) { async function onDictionaryMainChanged(e) {
@ -541,9 +559,7 @@ async function onDictionaryPurge(e) {
} }
await settingsSaveOptions(); await settingsSaveOptions();
const optionsContext = getOptionsContext(); onDatabaseUpdated();
const options = await getOptionsMutable(optionsContext);
onDatabaseUpdated(options);
} catch (err) { } catch (err) {
dictionaryErrorsShow([err]); dictionaryErrorsShow([err]);
} finally { } finally {
@ -615,9 +631,7 @@ async function onDictionaryImport(e) {
dictionaryErrorsShow(errors); dictionaryErrorsShow(errors);
} }
const optionsContext = getOptionsContext(); onDatabaseUpdated();
const options = await getOptionsMutable(optionsContext);
onDatabaseUpdated(options);
} }
} catch (err) { } catch (err) {
dictionaryErrorsShow([err]); dictionaryErrorsShow([err]);