Add functions for modifying mutable options

This commit is contained in:
toasted-nutbread 2019-12-11 21:11:07 -05:00
parent 17277a6450
commit b1f72905cf
5 changed files with 28 additions and 20 deletions

View File

@ -154,7 +154,7 @@ async function _onAnkiModelChanged(e) {
}
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
options.anki[tabId].fields = utilBackgroundIsolate(fields);
await settingsSaveOptions();

View File

@ -21,7 +21,7 @@ let audioSourceUI = null;
async function audioSettingsInitialize() {
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
audioSourceUI = new AudioSourceUI.Container(
options.audio.sources,
document.querySelector('.audio-source-list'),

View File

@ -273,7 +273,7 @@ class SettingsDictionaryEntryUI {
progress.hidden = true;
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
onDatabaseUpdated(options);
}
}
@ -360,7 +360,7 @@ async function dictSettingsInitialize() {
document.querySelector('#database-enable-prefix-wildcard-searches').addEventListener('change', (e) => onDatabaseEnablePrefixWildcardSearchesChanged(e), false);
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
onDictionaryOptionsChanged(options);
onDatabaseUpdated(options);
}
@ -425,7 +425,7 @@ async function updateMainDictionarySelect(options, dictionaries) {
async function onDictionaryMainChanged(e) {
const value = e.target.value;
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
options.general.mainDictionary = value;
settingsSaveOptions();
}
@ -531,14 +531,14 @@ async function onDictionaryPurge(e) {
dictionarySpinnerShow(true);
await utilDatabasePurge();
for (const {options} of toIterable((await apiOptionsGetFull()).profiles)) {
for (const {options} of toIterable((await getOptionsFullMutable()).profiles)) {
options.dictionaries = utilBackgroundIsolate({});
options.general.mainDictionary = '';
}
await settingsSaveOptions();
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
onDatabaseUpdated(options);
} catch (err) {
dictionaryErrorsShow([err]);
@ -593,7 +593,7 @@ async function onDictionaryImport(e) {
}
const {result, errors} = await utilDatabaseImport(files[i], updateProgress, importDetails);
for (const {options} of toIterable((await apiOptionsGetFull()).profiles)) {
for (const {options} of toIterable((await getOptionsFullMutable()).profiles)) {
const dictionaryOptions = SettingsDictionaryListUI.createDictionaryOptions();
dictionaryOptions.enabled = true;
options.dictionaries[result.title] = dictionaryOptions;
@ -611,7 +611,7 @@ async function onDictionaryImport(e) {
}
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
onDatabaseUpdated(options);
}
} catch (err) {
@ -630,7 +630,7 @@ async function onDictionaryImport(e) {
async function onDatabaseEnablePrefixWildcardSearchesChanged(e) {
const optionsFull = await apiOptionsGetFull();
const optionsFull = await getOptionsFullMutable();
const v = !!e.target.checked;
if (optionsFull.global.database.prefixWildcardsSupported === v) { return; }
optionsFull.global.database.prefixWildcardsSupported = !!e.target.checked;

View File

@ -16,6 +16,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function getOptionsMutable(optionsContext) {
return utilBackend().getOptions(optionsContext);
}
function getOptionsFullMutable() {
return utilBackend().getFullOptions();
}
async function formRead(options) {
options.general.enable = $('#enable').prop('checked');
options.general.showGuide = $('#show-usage-guide').prop('checked');
@ -164,7 +172,7 @@ function formUpdateVisibility(options) {
async function onFormOptionsChanged() {
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
await formRead(options);
await settingsSaveOptions();
@ -190,7 +198,7 @@ async function onOptionsUpdate({source}) {
if (source === thisSource) { return; }
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
await formWrite(options);
}

View File

@ -27,7 +27,7 @@ function getOptionsContext() {
async function profileOptionsSetup() {
const optionsFull = await apiOptionsGetFull();
const optionsFull = await getOptionsFullMutable();
currentProfileIndex = optionsFull.profileCurrent;
profileOptionsSetupEventListeners();
@ -120,7 +120,7 @@ async function profileOptionsUpdateTarget(optionsFull) {
profileFormWrite(optionsFull);
const optionsContext = getOptionsContext();
const options = await apiOptionsGet(optionsContext);
const options = await getOptionsMutable(optionsContext);
await formWrite(options);
}
@ -164,13 +164,13 @@ async function onProfileOptionsChanged(e) {
return;
}
const optionsFull = await apiOptionsGetFull();
const optionsFull = await getOptionsFullMutable();
await profileFormRead(optionsFull);
await settingsSaveOptions();
}
async function onTargetProfileChanged() {
const optionsFull = await apiOptionsGetFull();
const optionsFull = await getOptionsFullMutable();
const index = tryGetIntegerValue('#profile-target', 0, optionsFull.profiles.length);
if (index === null || currentProfileIndex === index) {
return;
@ -182,7 +182,7 @@ async function onTargetProfileChanged() {
}
async function onProfileAdd() {
const optionsFull = await apiOptionsGetFull();
const optionsFull = await getOptionsFullMutable();
const profile = utilBackgroundIsolate(optionsFull.profiles[currentProfileIndex]);
profile.name = profileOptionsCreateCopyName(profile.name, optionsFull.profiles, 100);
optionsFull.profiles.push(profile);
@ -210,7 +210,7 @@ async function onProfileRemove(e) {
async function onProfileRemoveConfirm() {
$('#profile-remove-modal').modal('hide');
const optionsFull = await apiOptionsGetFull();
const optionsFull = await getOptionsFullMutable();
if (optionsFull.profiles.length <= 1) {
return;
}
@ -234,7 +234,7 @@ function onProfileNameChanged() {
}
async function onProfileMove(offset) {
const optionsFull = await apiOptionsGetFull();
const optionsFull = await getOptionsFullMutable();
const index = currentProfileIndex + offset;
if (index < 0 || index >= optionsFull.profiles.length) {
return;
@ -267,7 +267,7 @@ async function onProfileCopy() {
async function onProfileCopyConfirm() {
$('#profile-copy-modal').modal('hide');
const optionsFull = await apiOptionsGetFull();
const optionsFull = await getOptionsFullMutable();
const index = tryGetIntegerValue('#profile-copy-source', 0, optionsFull.profiles.length);
if (index === null || index === currentProfileIndex) {
return;