diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2024-01-31 08:38:30 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-31 13:38:30 +0000 |
commit | 87ed7c8affd3ade9d3cd2d9ed1a61dd5f224e473 (patch) | |
tree | be727294e31ef21e8a3f634734610e69e4a155ac /ext/js/pages | |
parent | 3e419aa562aab03ca20421aaf7e4d1a39194a5b4 (diff) |
Module refactoring (#588)
* Convert PronunciationGenerator into static functions
* Convert DictionaryDataUtil into static functions
* Convert AnkiNoteDataCreator into static functions
* Convert MediaUtil into static functions
* Convert RegexUtil into static functions
* Convert StringUtil into static functions
* Convert ArrayBufferUtil into static functions
* Convert AnkiUtil into static functions
* Convert PermissionsUtil into static functions
* Convert ProfileConditionsUtil into static functions
Diffstat (limited to 'ext/js/pages')
-rw-r--r-- | ext/js/pages/action-popup-main.js | 8 | ||||
-rw-r--r-- | ext/js/pages/settings/anki-controller.js | 23 | ||||
-rw-r--r-- | ext/js/pages/settings/backup-controller.js | 7 | ||||
-rw-r--r-- | ext/js/pages/settings/permissions-origin-controller.js | 5 | ||||
-rw-r--r-- | ext/js/pages/settings/permissions-toggle-controller.js | 13 | ||||
-rw-r--r-- | ext/js/pages/settings/recommended-permissions-controller.js | 5 | ||||
-rw-r--r-- | ext/js/pages/settings/settings-controller.js | 11 |
7 files changed, 34 insertions, 38 deletions
diff --git a/ext/js/pages/action-popup-main.js b/ext/js/pages/action-popup-main.js index b978d989..86201e83 100644 --- a/ext/js/pages/action-popup-main.js +++ b/ext/js/pages/action-popup-main.js @@ -16,7 +16,7 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import {PermissionsUtil} from '../data/permissions-util.js'; +import {getAllPermissions, hasRequiredPermissionsForOptions} from '../data/permissions-util.js'; import {querySelectorNotNull} from '../dom/query-selector.js'; import {HotkeyHelpController} from '../input/hotkey-help-controller.js'; import {yomitan} from '../yomitan.js'; @@ -25,8 +25,6 @@ class DisplayController { constructor() { /** @type {?import('settings').Options} */ this._optionsFull = null; - /** @type {PermissionsUtil} */ - this._permissionsUtil = new PermissionsUtil(); } /** */ @@ -286,8 +284,8 @@ class DisplayController { * @param {import('settings').ProfileOptions} options */ async _updatePermissionsWarnings(options) { - const permissions = await this._permissionsUtil.getAllPermissions(); - if (this._permissionsUtil.hasRequiredPermissionsForOptions(permissions, options)) { return; } + const permissions = await getAllPermissions(); + if (hasRequiredPermissionsForOptions(permissions, options)) { return; } const warnings = /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll('.action-open-permissions,.permissions-required-warning')); for (const node of warnings) { diff --git a/ext/js/pages/settings/anki-controller.js b/ext/js/pages/settings/anki-controller.js index d1faf491..09ab3c03 100644 --- a/ext/js/pages/settings/anki-controller.js +++ b/ext/js/pages/settings/anki-controller.js @@ -21,7 +21,8 @@ import {EventListenerCollection} from '../../core/event-listener-collection.js'; import {ExtensionError} from '../../core/extension-error.js'; import {log} from '../../core/logger.js'; import {toError} from '../../core/to-error.js'; -import {AnkiUtil} from '../../data/anki-util.js'; +import {stringContainsAnyFieldMarker} from '../../data/anki-util.js'; +import {getRequiredPermissionsForAnkiFieldValue, hasPermissions, setPermissionsGranted} from '../../data/permissions-util.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; import {SelectorObserver} from '../../dom/selector-observer.js'; import {ObjectPropertyAccessor} from '../../general/object-property-accessor.js'; @@ -211,7 +212,7 @@ export class AnkiController { * @returns {string[]} */ getRequiredPermissions(fieldValue) { - return this._settingsController.permissionsUtil.getRequiredPermissionsForAnkiFieldValue(fieldValue); + return getRequiredPermissionsForAnkiFieldValue(fieldValue); } // Private @@ -738,7 +739,7 @@ class AnkiCardController { */ _validateField(node, index) { let valid = (node.dataset.hasPermissions !== 'false'); - if (valid && index === 0 && !AnkiUtil.stringContainsAnyFieldMarker(node.value)) { + if (valid && index === 0 && !stringContainsAnyFieldMarker(node.value)) { valid = false; } node.dataset.invalid = `${!valid}`; @@ -936,7 +937,7 @@ class AnkiCardController { */ async _requestPermissions(permissions) { try { - await this._settingsController.permissionsUtil.setPermissionsGranted({permissions}, true); + await setPermissionsGranted({permissions}, true); } catch (e) { log.error(e); } @@ -952,12 +953,12 @@ class AnkiCardController { const permissions = this._ankiController.getRequiredPermissions(fieldValue); if (permissions.length > 0) { node.dataset.requiredPermission = permissions.join(' '); - const hasPermissions = await ( + const hasPermissions2 = await ( request ? - this._settingsController.permissionsUtil.setPermissionsGranted({permissions}, true) : - this._settingsController.permissionsUtil.hasPermissions({permissions}) + setPermissionsGranted({permissions}, true) : + hasPermissions({permissions}) ); - node.dataset.hasPermissions = `${hasPermissions}`; + node.dataset.hasPermissions = `${hasPermissions2}`; } else { delete node.dataset.requiredPermission; delete node.dataset.hasPermissions; @@ -977,15 +978,15 @@ class AnkiCardController { if (typeof requiredPermission !== 'string') { continue; } const requiredPermissionArray = (requiredPermission.length === 0 ? [] : requiredPermission.split(' ')); - let hasPermissions = true; + let hasPermissions2 = true; for (const permission of requiredPermissionArray) { if (!permissionsSet.has(permission)) { - hasPermissions = false; + hasPermissions2 = false; break; } } - inputField.dataset.hasPermissions = `${hasPermissions}`; + inputField.dataset.hasPermissions = `${hasPermissions2}`; this._validateField(inputField, i); } } diff --git a/ext/js/pages/settings/backup-controller.js b/ext/js/pages/settings/backup-controller.js index f2eccd1e..053cc96b 100644 --- a/ext/js/pages/settings/backup-controller.js +++ b/ext/js/pages/settings/backup-controller.js @@ -22,7 +22,8 @@ import {log} from '../../core/logger.js'; import {toError} from '../../core/to-error.js'; import {isObject} from '../../core/utilities.js'; import {OptionsUtil} from '../../data/options-util.js'; -import {ArrayBufferUtil} from '../../data/sandbox/array-buffer-util.js'; +import {getAllPermissions} from '../../data/permissions-util.js'; +import {arrayBufferUtf8Decode} from '../../data/sandbox/array-buffer-util.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; import {yomitan} from '../../yomitan.js'; import {DictionaryController} from './dictionary-controller.js'; @@ -135,7 +136,7 @@ export class BackupController { const optionsFull = await this._settingsController.getOptionsFull(); const environment = await yomitan.api.getEnvironmentInfo(); const fieldTemplatesDefault = await yomitan.api.getDefaultAnkiFieldTemplates(); - const permissions = await this._settingsController.permissionsUtil.getAllPermissions(); + const permissions = await getAllPermissions(); // Format options for (const {options} of optionsFull.profiles) { @@ -425,7 +426,7 @@ export class BackupController { async _importSettingsFile(file) { if (this._optionsUtil === null) { throw new Error('OptionsUtil invalid'); } - const dataString = ArrayBufferUtil.arrayBufferUtf8Decode(await this._readFileArrayBuffer(file)); + const dataString = arrayBufferUtf8Decode(await this._readFileArrayBuffer(file)); /** @type {import('backup-controller').BackupData} */ const data = parseJson(dataString); diff --git a/ext/js/pages/settings/permissions-origin-controller.js b/ext/js/pages/settings/permissions-origin-controller.js index a0f23af6..9447e3cc 100644 --- a/ext/js/pages/settings/permissions-origin-controller.js +++ b/ext/js/pages/settings/permissions-origin-controller.js @@ -18,6 +18,7 @@ import {EventListenerCollection} from '../../core/event-listener-collection.js'; import {toError} from '../../core/to-error.js'; +import {getAllPermissions, setPermissionsGranted} from '../../data/permissions-util.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; export class PermissionsOriginController { @@ -140,7 +141,7 @@ export class PermissionsOriginController { /** */ async _updatePermissions() { - const permissions = await this._settingsController.permissionsUtil.getAllPermissions(); + const permissions = await getAllPermissions(); this._onPermissionsChanged({permissions}); } @@ -152,7 +153,7 @@ export class PermissionsOriginController { async _setOriginPermissionEnabled(origin, enabled) { let added = false; try { - added = await this._settingsController.permissionsUtil.setPermissionsGranted({origins: [origin]}, enabled); + added = await setPermissionsGranted({origins: [origin]}, enabled); } catch (e) { const errorContainer = /** @type {HTMLElement} */ (this._errorContainer); errorContainer.hidden = false; diff --git a/ext/js/pages/settings/permissions-toggle-controller.js b/ext/js/pages/settings/permissions-toggle-controller.js index c775aa12..25204dce 100644 --- a/ext/js/pages/settings/permissions-toggle-controller.js +++ b/ext/js/pages/settings/permissions-toggle-controller.js @@ -16,6 +16,7 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +import {getAllPermissions, hasPermissions, setPermissionsGranted} from '../../data/permissions-util.js'; import {ObjectPropertyAccessor} from '../../general/object-property-accessor.js'; export class PermissionsToggleController { @@ -85,11 +86,11 @@ export class PermissionsToggleController { toggle.checked = valuePre; const permissions = this._getRequiredPermissions(toggle); try { - value = await this._settingsController.permissionsUtil.setPermissionsGranted({permissions}, value); + value = await setPermissionsGranted({permissions}, value); } catch (error) { value = valuePre; try { - value = await this._settingsController.permissionsUtil.hasPermissions({permissions}); + value = await hasPermissions({permissions}); } catch (error2) { // NOP } @@ -111,13 +112,13 @@ export class PermissionsToggleController { const permissionsSet = new Set(typeof permissions2 !== 'undefined' ? permissions2 : []); for (const toggle of /** @type {NodeListOf<HTMLInputElement>} */ (this._toggles)) { const {permissionsSetting} = toggle.dataset; - const hasPermissions = this._hasAll(permissionsSet, this._getRequiredPermissions(toggle)); + const hasPermissions2 = this._hasAll(permissionsSet, this._getRequiredPermissions(toggle)); if (typeof permissionsSetting === 'string') { - const valid = !toggle.checked || hasPermissions; + const valid = !toggle.checked || hasPermissions2; this._setToggleValid(toggle, valid); } else { - toggle.checked = hasPermissions; + toggle.checked = hasPermissions2; } } } @@ -134,7 +135,7 @@ export class PermissionsToggleController { /** */ async _updateValidity() { - const permissions = await this._settingsController.permissionsUtil.getAllPermissions(); + const permissions = await getAllPermissions(); this._onPermissionsChanged({permissions}); } diff --git a/ext/js/pages/settings/recommended-permissions-controller.js b/ext/js/pages/settings/recommended-permissions-controller.js index 84a4ef10..b7bb1ea8 100644 --- a/ext/js/pages/settings/recommended-permissions-controller.js +++ b/ext/js/pages/settings/recommended-permissions-controller.js @@ -18,6 +18,7 @@ import {EventListenerCollection} from '../../core/event-listener-collection.js'; import {toError} from '../../core/to-error.js'; +import {getAllPermissions, setPermissionsGranted} from '../../data/permissions-util.js'; export class RecommendedPermissionsController { /** @@ -77,7 +78,7 @@ export class RecommendedPermissionsController { /** */ async _updatePermissions() { - const permissions = await this._settingsController.permissionsUtil.getAllPermissions(); + const permissions = await getAllPermissions(); this._onPermissionsChanged({permissions}); } @@ -89,7 +90,7 @@ export class RecommendedPermissionsController { async _setOriginPermissionEnabled(origin, enabled) { let added = false; try { - added = await this._settingsController.permissionsUtil.setPermissionsGranted({origins: [origin]}, enabled); + added = await setPermissionsGranted({origins: [origin]}, enabled); } catch (e) { if (this._errorContainer !== null) { this._errorContainer.hidden = false; diff --git a/ext/js/pages/settings/settings-controller.js b/ext/js/pages/settings/settings-controller.js index 25f5e8ad..49fa9c9e 100644 --- a/ext/js/pages/settings/settings-controller.js +++ b/ext/js/pages/settings/settings-controller.js @@ -20,7 +20,7 @@ import {EventDispatcher} from '../../core/event-dispatcher.js'; import {EventListenerCollection} from '../../core/event-listener-collection.js'; import {generateId, isObject} from '../../core/utilities.js'; import {OptionsUtil} from '../../data/options-util.js'; -import {PermissionsUtil} from '../../data/permissions-util.js'; +import {getAllPermissions} from '../../data/permissions-util.js'; import {HtmlTemplateCollection} from '../../dom/html-template-collection.js'; import {yomitan} from '../../yomitan.js'; @@ -41,8 +41,6 @@ export class SettingsController extends EventDispatcher { /** @type {HtmlTemplateCollection} */ this._templates = new HtmlTemplateCollection(); this._templates.load(document); - /** @type {PermissionsUtil} */ - this._permissionsUtil = new PermissionsUtil(); } /** @type {string} */ @@ -60,11 +58,6 @@ export class SettingsController extends EventDispatcher { this._setProfileIndex(value, true); } - /** @type {PermissionsUtil} */ - get permissionsUtil() { - return this._permissionsUtil; - } - /** */ async prepare() { yomitan.on('optionsUpdated', this._onOptionsUpdated.bind(this)); @@ -338,7 +331,7 @@ export class SettingsController extends EventDispatcher { const eventName = 'permissionsChanged'; if (!this.hasListeners(eventName)) { return; } - const permissions = await this._permissionsUtil.getAllPermissions(); + const permissions = await getAllPermissions(); this.trigger(eventName, {permissions}); } |