Change how formRead works

The function now modifies values in-place.
This commit is contained in:
toasted-nutbread 2019-09-07 15:54:00 -04:00
parent 8175f80183
commit 99cec1d23f

View File

@ -17,72 +17,68 @@
*/ */
async function formRead() { async function formRead(options) {
const optionsOld = await optionsLoad(); options.general.showGuide = $('#show-usage-guide').prop('checked');
const optionsNew = $.extend(true, {}, optionsOld); options.general.compactTags = $('#compact-tags').prop('checked');
options.general.compactGlossaries = $('#compact-glossaries').prop('checked');
options.general.autoPlayAudio = $('#auto-play-audio').prop('checked');
options.general.resultOutputMode = $('#result-output-mode').val();
options.general.audioSource = $('#audio-playback-source').val();
options.general.audioVolume = parseFloat($('#audio-playback-volume').val());
options.general.debugInfo = $('#show-debug-info').prop('checked');
options.general.showAdvanced = $('#show-advanced-options').prop('checked');
options.general.maxResults = parseInt($('#max-displayed-results').val(), 10);
options.general.popupDisplayMode = $('#popup-display-mode').val();
options.general.popupHorizontalTextPosition = $('#popup-horizontal-text-position').val();
options.general.popupVerticalTextPosition = $('#popup-vertical-text-position').val();
options.general.popupWidth = parseInt($('#popup-width').val(), 10);
options.general.popupHeight = parseInt($('#popup-height').val(), 10);
options.general.popupHorizontalOffset = parseInt($('#popup-horizontal-offset').val(), 0);
options.general.popupVerticalOffset = parseInt($('#popup-vertical-offset').val(), 10);
options.general.popupHorizontalOffset2 = parseInt($('#popup-horizontal-offset2').val(), 0);
options.general.popupVerticalOffset2 = parseInt($('#popup-vertical-offset2').val(), 10);
options.general.customPopupCss = $('#custom-popup-css').val();
optionsNew.general.showGuide = $('#show-usage-guide').prop('checked'); options.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked');
optionsNew.general.compactTags = $('#compact-tags').prop('checked'); options.scanning.touchInputEnabled = $('#touch-input-enabled').prop('checked');
optionsNew.general.compactGlossaries = $('#compact-glossaries').prop('checked'); options.scanning.selectText = $('#select-matched-text').prop('checked');
optionsNew.general.autoPlayAudio = $('#auto-play-audio').prop('checked'); options.scanning.alphanumeric = $('#search-alphanumeric').prop('checked');
optionsNew.general.resultOutputMode = $('#result-output-mode').val(); options.scanning.autoHideResults = $('#auto-hide-results').prop('checked');
optionsNew.general.audioSource = $('#audio-playback-source').val(); options.scanning.deepDomScan = $('#deep-dom-scan').prop('checked');
optionsNew.general.audioVolume = parseFloat($('#audio-playback-volume').val()); options.scanning.enableOnPopupExpressions = $('#enable-scanning-of-popup-expressions').prop('checked');
optionsNew.general.debugInfo = $('#show-debug-info').prop('checked'); options.scanning.enableOnSearchPage = $('#enable-scanning-on-search-page').prop('checked');
optionsNew.general.showAdvanced = $('#show-advanced-options').prop('checked'); options.scanning.delay = parseInt($('#scan-delay').val(), 10);
optionsNew.general.maxResults = parseInt($('#max-displayed-results').val(), 10); options.scanning.length = parseInt($('#scan-length').val(), 10);
optionsNew.general.popupDisplayMode = $('#popup-display-mode').val(); options.scanning.modifier = $('#scan-modifier-key').val();
optionsNew.general.popupHorizontalTextPosition = $('#popup-horizontal-text-position').val(); options.scanning.popupNestingMaxDepth = parseInt($('#popup-nesting-max-depth').val(), 10);
optionsNew.general.popupVerticalTextPosition = $('#popup-vertical-text-position').val();
optionsNew.general.popupWidth = parseInt($('#popup-width').val(), 10);
optionsNew.general.popupHeight = parseInt($('#popup-height').val(), 10);
optionsNew.general.popupHorizontalOffset = parseInt($('#popup-horizontal-offset').val(), 0);
optionsNew.general.popupVerticalOffset = parseInt($('#popup-vertical-offset').val(), 10);
optionsNew.general.popupHorizontalOffset2 = parseInt($('#popup-horizontal-offset2').val(), 0);
optionsNew.general.popupVerticalOffset2 = parseInt($('#popup-vertical-offset2').val(), 10);
optionsNew.general.customPopupCss = $('#custom-popup-css').val();
optionsNew.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked'); const optionsAnkiEnableOld = options.anki.enable;
optionsNew.scanning.touchInputEnabled = $('#touch-input-enabled').prop('checked'); options.anki.enable = $('#anki-enable').prop('checked');
optionsNew.scanning.selectText = $('#select-matched-text').prop('checked'); options.anki.tags = $('#card-tags').val().split(/[,; ]+/);
optionsNew.scanning.alphanumeric = $('#search-alphanumeric').prop('checked'); options.anki.sentenceExt = parseInt($('#sentence-detection-extent').val(), 10);
optionsNew.scanning.autoHideResults = $('#auto-hide-results').prop('checked'); options.anki.server = $('#interface-server').val();
optionsNew.scanning.deepDomScan = $('#deep-dom-scan').prop('checked'); options.anki.screenshot.format = $('#screenshot-format').val();
optionsNew.scanning.enableOnPopupExpressions = $('#enable-scanning-of-popup-expressions').prop('checked'); options.anki.screenshot.quality = parseInt($('#screenshot-quality').val(), 10);
optionsNew.scanning.enableOnSearchPage = $('#enable-scanning-on-search-page').prop('checked'); options.anki.fieldTemplates = $('#field-templates').val();
optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10);
optionsNew.scanning.length = parseInt($('#scan-length').val(), 10);
optionsNew.scanning.modifier = $('#scan-modifier-key').val();
optionsNew.scanning.popupNestingMaxDepth = parseInt($('#popup-nesting-max-depth').val(), 10);
optionsNew.anki.enable = $('#anki-enable').prop('checked'); if (optionsAnkiEnableOld && !ankiErrorShown()) {
optionsNew.anki.tags = $('#card-tags').val().split(/[,; ]+/); options.anki.terms.deck = $('#anki-terms-deck').val();
optionsNew.anki.sentenceExt = parseInt($('#sentence-detection-extent').val(), 10); options.anki.terms.model = $('#anki-terms-model').val();
optionsNew.anki.server = $('#interface-server').val(); options.anki.terms.fields = ankiFieldsToDict($('#terms .anki-field-value'));
optionsNew.anki.screenshot.format = $('#screenshot-format').val(); options.anki.kanji.deck = $('#anki-kanji-deck').val();
optionsNew.anki.screenshot.quality = parseInt($('#screenshot-quality').val(), 10); options.anki.kanji.model = $('#anki-kanji-model').val();
optionsNew.anki.fieldTemplates = $('#field-templates').val(); options.anki.kanji.fields = ankiFieldsToDict($('#kanji .anki-field-value'));
if (optionsOld.anki.enable && !ankiErrorShown()) {
optionsNew.anki.terms.deck = $('#anki-terms-deck').val();
optionsNew.anki.terms.model = $('#anki-terms-model').val();
optionsNew.anki.terms.fields = ankiFieldsToDict($('#terms .anki-field-value'));
optionsNew.anki.kanji.deck = $('#anki-kanji-deck').val();
optionsNew.anki.kanji.model = $('#anki-kanji-model').val();
optionsNew.anki.kanji.fields = ankiFieldsToDict($('#kanji .anki-field-value'));
} }
optionsNew.general.mainDictionary = $('#dict-main').val(); options.general.mainDictionary = $('#dict-main').val();
$('.dict-group').each((index, element) => { $('.dict-group').each((index, element) => {
const dictionary = $(element); const dictionary = $(element);
optionsNew.dictionaries[dictionary.data('title')] = { options.dictionaries[dictionary.data('title')] = {
priority: parseInt(dictionary.find('.dict-priority').val(), 10), priority: parseInt(dictionary.find('.dict-priority').val(), 10),
enabled: dictionary.find('.dict-enabled').prop('checked'), enabled: dictionary.find('.dict-enabled').prop('checked'),
allowSecondarySearches: dictionary.find('.dict-allow-secondary-searches').prop('checked') allowSecondarySearches: dictionary.find('.dict-allow-secondary-searches').prop('checked')
}; };
}); });
return {optionsNew, optionsOld};
} }
function formUpdateVisibility(options) { function formUpdateVisibility(options) {
@ -141,18 +137,22 @@ async function onFormOptionsChanged(e) {
return; return;
} }
const {optionsNew, optionsOld} = await formRead(); const options = await optionsLoad();
await optionsSave(optionsNew); const optionsAnkiEnableOld = options.anki.enable;
formUpdateVisibility(optionsNew); const optionsAnkiServerOld = options.anki.server;
await formRead(options);
await optionsSave(options);
formUpdateVisibility(options);
try { try {
const ankiUpdated = const ankiUpdated =
optionsNew.anki.enable !== optionsOld.anki.enable || options.anki.enable !== optionsAnkiEnableOld ||
optionsNew.anki.server !== optionsOld.anki.server; options.anki.server !== optionsAnkiServerOld;
if (ankiUpdated) { if (ankiUpdated) {
ankiSpinnerShow(true); ankiSpinnerShow(true);
await ankiDeckAndModelPopulate(optionsNew); await ankiDeckAndModelPopulate(options);
ankiErrorShow(); ankiErrorShow();
} }
} catch (e) { } catch (e) {
@ -566,12 +566,13 @@ async function onAnkiModelChanged(e) {
const tab = element.closest('.tab-pane'); const tab = element.closest('.tab-pane');
const tabId = tab.attr('id'); const tabId = tab.attr('id');
const {optionsNew, optionsOld} = await formRead(); const options = await optionsLoad();
optionsNew.anki[tabId].fields = {}; await formRead(options);
await optionsSave(optionsNew); options.anki[tabId].fields = {};
await optionsSave(options);
ankiSpinnerShow(true); ankiSpinnerShow(true);
await ankiFieldsPopulate(element, optionsNew); await ankiFieldsPopulate(element, options);
ankiErrorShow(); ankiErrorShow();
} catch (e) { } catch (e) {
ankiErrorShow(e); ankiErrorShow(e);