aboutsummaryrefslogtreecommitdiff
path: root/ext/js/pages/settings/dictionary-import-controller.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-12-16 19:55:02 -0500
committerGitHub <noreply@github.com>2023-12-17 00:55:02 +0000
commit95ad1ae1ef4a53802c12eab4c9b1545af0333aa1 (patch)
treec08016ccb3265a5931baf413cd9d78fcbf600ae9 /ext/js/pages/settings/dictionary-import-controller.js
parent77d27113d347b4724302f1c72de1f238e04aeead (diff)
Safer query selector (#364)
* Add querySelectorNotNull helper function * Use querySelectorNotNull * Updates * Update settings * Remove unused * Update * Update function calls * More updates * Update types * Remove obsolete code
Diffstat (limited to 'ext/js/pages/settings/dictionary-import-controller.js')
-rw-r--r--ext/js/pages/settings/dictionary-import-controller.js58
1 files changed, 11 insertions, 47 deletions
diff --git a/ext/js/pages/settings/dictionary-import-controller.js b/ext/js/pages/settings/dictionary-import-controller.js
index d1255e11..35b7c461 100644
--- a/ext/js/pages/settings/dictionary-import-controller.js
+++ b/ext/js/pages/settings/dictionary-import-controller.js
@@ -18,6 +18,7 @@
import {log} from '../../core.js';
import {ExtensionError} from '../../core/extension-error.js';
+import {querySelectorNotNull} from '../../dom/query-selector.js';
import {DictionaryWorker} from '../../language/dictionary-worker.js';
import {yomitan} from '../../yomitan.js';
import {DictionaryController} from './dictionary-controller.js';
@@ -37,22 +38,18 @@ export class DictionaryImportController {
this._statusFooter = statusFooter;
/** @type {boolean} */
this._modifying = false;
- /** @type {?HTMLButtonElement} */
- this._purgeButton = null;
- /** @type {?HTMLButtonElement} */
- this._purgeConfirmButton = null;
- /** @type {?HTMLButtonElement} */
- this._importFileButton = null;
- /** @type {?HTMLInputElement} */
- this._importFileInput = null;
+ /** @type {HTMLButtonElement} */
+ this._purgeButton = querySelectorNotNull(document, '#dictionary-delete-all-button');
+ /** @type {HTMLButtonElement} */
+ this._purgeConfirmButton = querySelectorNotNull(document, '#dictionary-confirm-delete-all-button');
+ /** @type {HTMLButtonElement} */
+ this._importFileButton = querySelectorNotNull(document, '#dictionary-import-file-button');
+ /** @type {HTMLInputElement} */
+ this._importFileInput = querySelectorNotNull(document, '#dictionary-import-file-input');
/** @type {?import('./modal.js').Modal} */
this._purgeConfirmModal = null;
- /** @type {?HTMLElement} */
- this._errorContainer = null;
- /** @type {?HTMLElement} */
- this._spinner = null;
- /** @type {?HTMLElement} */
- this._purgeNotification = null;
+ /** @type {HTMLElement} */
+ this._errorContainer = querySelectorNotNull(document, '#dictionary-error');
/** @type {[originalMessage: string, newMessage: string][]} */
this._errorToStringOverrides = [
[
@@ -68,14 +65,7 @@ export class DictionaryImportController {
/** */
async prepare() {
- this._purgeButton = /** @type {HTMLButtonElement} */ (document.querySelector('#dictionary-delete-all-button'));
- this._purgeConfirmButton = /** @type {HTMLButtonElement} */ (document.querySelector('#dictionary-confirm-delete-all-button'));
- this._importFileButton = /** @type {HTMLButtonElement} */ (document.querySelector('#dictionary-import-file-button'));
- this._importFileInput = /** @type {HTMLInputElement} */ (document.querySelector('#dictionary-import-file-input'));
this._purgeConfirmModal = this._modalController.getModal('dictionary-confirm-delete-all');
- this._errorContainer = /** @type {HTMLElement} */ (document.querySelector('#dictionary-error'));
- this._spinner = /** @type {HTMLElement} */ (document.querySelector('#dictionary-spinner'));
- this._purgeNotification = /** @type {HTMLElement} */ (document.querySelector('#dictionary-delete-all-status'));
this._purgeButton.addEventListener('click', this._onPurgeButtonClick.bind(this), false);
this._purgeConfirmButton.addEventListener('click', this._onPurgeConfirmButtonClick.bind(this), false);
@@ -123,14 +113,11 @@ export class DictionaryImportController {
async _purgeDatabase() {
if (this._modifying) { return; }
- const purgeNotification = this._purgeNotification;
const prevention = this._preventPageExit();
try {
this._setModifying(true);
this._hideErrors();
- this._setSpinnerVisible(true);
- if (purgeNotification !== null) { purgeNotification.hidden = false; }
await yomitan.api.purgeDatabase();
const errors = await this._clearDictionarySettings();
@@ -142,8 +129,6 @@ export class DictionaryImportController {
this._showErrors([error instanceof Error ? error : new Error(`${error}`)]);
} finally {
prevention.end();
- if (purgeNotification !== null) { purgeNotification.hidden = true; }
- this._setSpinnerVisible(false);
this._setModifying(false);
this._triggerStorageChanged();
}
@@ -156,7 +141,6 @@ export class DictionaryImportController {
if (this._modifying) { return; }
const statusFooter = this._statusFooter;
- const importInfo = /** @type {HTMLElement} */ (document.querySelector('#dictionary-import-info'));
const progressSelector = '.dictionary-import-progress';
const progressContainers = /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll(`#dictionaries-modal ${progressSelector}`));
const progressBars = /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll(`${progressSelector} .progress-bar`));
@@ -168,7 +152,6 @@ export class DictionaryImportController {
try {
this._setModifying(true);
this._hideErrors();
- this._setSpinnerVisible(true);
for (const progress of progressContainers) { progress.hidden = false; }
@@ -204,11 +187,6 @@ export class DictionaryImportController {
const fileCount = files.length;
for (let i = 0; i < fileCount; ++i) {
- if (importInfo !== null && fileCount > 1) {
- importInfo.hidden = false;
- importInfo.textContent = `(${i + 1} of ${fileCount})`;
- }
-
statusPrefix = `Importing dictionary${fileCount > 1 ? ` (${i + 1} of ${fileCount})` : ''}`;
onProgress({
stepIndex: -1,
@@ -226,11 +204,6 @@ export class DictionaryImportController {
prevention.end();
for (const progress of progressContainers) { progress.hidden = true; }
if (statusFooter !== null) { statusFooter.setTaskActive(progressSelector, false); }
- if (importInfo !== null) {
- importInfo.textContent = '';
- importInfo.hidden = true;
- }
- this._setSpinnerVisible(false);
this._setModifying(false);
this._triggerStorageChanged();
}
@@ -313,15 +286,6 @@ export class DictionaryImportController {
}
/**
- * @param {boolean} visible
- */
- _setSpinnerVisible(visible) {
- if (this._spinner !== null) {
- this._spinner.hidden = !visible;
- }
- }
-
- /**
* @returns {import('settings-controller').PageExitPrevention}
*/
_preventPageExit() {