From 23230b8a7c5eb66357d5bcafb4184def13c5954b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Mon, 19 Oct 2020 20:05:45 -0400 Subject: Backup controller refactor (#946) * Update IDs * Change how default fieldTemplates is assigned * Ensure no missing fields * Fix error display during import --- ext/bg/js/options.js | 4 ++++ ext/bg/js/settings/backup-controller.js | 35 +++++++++++++++++---------------- ext/bg/settings.html | 22 ++++++++++----------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index 42a1f3d4..9e89e9bd 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -129,6 +129,10 @@ class OptionsUtil { return this._schemaValidator.getValidValueOrDefault(this._optionsSchema); } + getValidValueOrDefault(options) { + return this._schemaValidator.getValidValueOrDefault(this._optionsSchema, options); + } + createValidatingProxy(options) { return this._schemaValidator.createProxy(options, this._optionsSchema); } diff --git a/ext/bg/js/settings/backup-controller.js b/ext/bg/js/settings/backup-controller.js index 50390d5c..454ede85 100644 --- a/ext/bg/js/settings/backup-controller.js +++ b/ext/bg/js/settings/backup-controller.js @@ -37,15 +37,15 @@ class BackupController { async prepare() { await this._optionsUtil.prepare(); - this._settingsResetModal = this._modalController.getModal('settings-reset-modal'); - this._settingsImportErrorModal = this._modalController.getModal('settings-import-error-modal'); - this._settingsImportWarningModal = this._modalController.getModal('settings-import-warning-modal'); + this._settingsResetModal = this._modalController.getModal('settings-reset'); + this._settingsImportErrorModal = this._modalController.getModal('settings-import-error'); + this._settingsImportWarningModal = this._modalController.getModal('settings-import-warning'); - document.querySelector('#settings-export').addEventListener('click', this._onSettingsExportClick.bind(this), false); - document.querySelector('#settings-import').addEventListener('click', this._onSettingsImportClick.bind(this), false); + document.querySelector('#settings-export-button').addEventListener('click', this._onSettingsExportClick.bind(this), false); + document.querySelector('#settings-import-button').addEventListener('click', this._onSettingsImportClick.bind(this), false); document.querySelector('#settings-import-file').addEventListener('change', this._onSettingsImportFileChange.bind(this), false); - document.querySelector('#settings-reset').addEventListener('click', this._onSettingsResetClick.bind(this), false); - document.querySelector('#settings-reset-modal-confirm').addEventListener('click', this._onSettingsResetConfirmClick.bind(this), false); + document.querySelector('#settings-reset-button').addEventListener('click', this._onSettingsResetClick.bind(this), false); + document.querySelector('#settings-reset-confirm-button').addEventListener('click', this._onSettingsResetConfirmClick.bind(this), false); } // Private @@ -75,7 +75,7 @@ class BackupController { // Format options for (const {options} of optionsFull.profiles) { if (options.anki.fieldTemplates === fieldTemplatesDefault || !options.anki.fieldTemplates) { - delete options.anki.fieldTemplates; // Default + options.anki.fieldTemplates = null; } } @@ -152,23 +152,19 @@ class BackupController { // Importing async _settingsImportSetOptionsFull(optionsFull) { - try { - await this._settingsController.setAllSettings(optionsFull); - } catch (e) { - yomichan.logError(e); - } + await this._settingsController.setAllSettings(optionsFull); } _showSettingsImportError(error) { yomichan.logError(error); - document.querySelector('#settings-import-error-modal-message').textContent = `${error}`; + document.querySelector('#settings-import-error-message').textContent = `${error}`; this._settingsImportErrorModal.setVisible(true); } async _showSettingsImportWarnings(warnings) { const modal = this._settingsImportWarningModal; - const buttons = document.querySelectorAll('.settings-import-warning-modal-import-button'); - const messageContainer = document.querySelector('#settings-import-warning-modal-message'); + const buttons = document.querySelectorAll('.settings-import-warning-import-button'); + const messageContainer = document.querySelector('#settings-import-warning-message'); if (buttons.length === 0 || messageContainer === null) { return {result: false}; } @@ -340,6 +336,7 @@ class BackupController { // Upgrade options optionsFull = await this._optionsUtil.update(optionsFull); + optionsFull = this._optionsUtil.getValidValueOrDefault(optionsFull); // Check for warnings const sanitizationWarnings = this._settingsImportSanitizeOptions(optionsFull, true); @@ -388,6 +385,10 @@ class BackupController { const optionsFull = this._optionsUtil.getDefault(); // Assign options - await this._settingsImportSetOptionsFull(optionsFull); + try { + await this._settingsImportSetOptionsFull(optionsFull); + } catch (e) { + yomichan.logError(e); + } } } diff --git a/ext/bg/settings.html b/ext/bg/settings.html index 804574cb..df90281c 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -1111,14 +1111,14 @@

- - - + + +
-