diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-02-04 22:17:42 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-04 22:17:42 -0500 |
commit | 8f97ca0aacd9a9f36038aa680cc6b9929450fbc5 (patch) | |
tree | 098eac7465562f7407cb9978c083378c7d6bcbb3 /ext/bg/js | |
parent | 47f16862d0d55ccf9755075741c85d9e6e662e73 (diff) |
Permissions toggle controller (#1347)
* Move file, rename class
* Expose required permissions in an attribute
* Update selector
* Update attribute
* Fix global declaration order
Diffstat (limited to 'ext/bg/js')
-rw-r--r-- | ext/bg/js/settings/main.js | 6 | ||||
-rw-r--r-- | ext/bg/js/settings/permissions-toggle-controller.js (renamed from ext/bg/js/settings/clipboard-popups-controller.js) | 28 | ||||
-rw-r--r-- | ext/bg/js/settings2/settings-main.js | 6 |
3 files changed, 26 insertions, 14 deletions
diff --git a/ext/bg/js/settings/main.js b/ext/bg/js/settings/main.js index 0ce0b2db..9786d196 100644 --- a/ext/bg/js/settings/main.js +++ b/ext/bg/js/settings/main.js @@ -20,11 +20,11 @@ * AnkiTemplatesController * AudioController * BackupController - * ClipboardPopupsController * DictionaryController * DictionaryImportController * GenericSettingController * ModalController + * PermissionsToggleController * PopupPreviewController * ProfileController * ScanInputsController @@ -71,8 +71,8 @@ async function setupEnvironmentInfo() { const genericSettingController = new GenericSettingController(settingsController); genericSettingController.prepare(); - const clipboardPopupsController = new ClipboardPopupsController(settingsController); - clipboardPopupsController.prepare(); + const permissionsToggleController = new PermissionsToggleController(settingsController); + permissionsToggleController.prepare(); const popupPreviewController = new PopupPreviewController(settingsController); popupPreviewController.prepare(); diff --git a/ext/bg/js/settings/clipboard-popups-controller.js b/ext/bg/js/settings/permissions-toggle-controller.js index cd1c2057..07db1cf8 100644 --- a/ext/bg/js/settings/clipboard-popups-controller.js +++ b/ext/bg/js/settings/permissions-toggle-controller.js @@ -19,17 +19,17 @@ * ObjectPropertyAccessor */ -class ClipboardPopupsController { +class PermissionsToggleController { constructor(settingsController) { this._settingsController = settingsController; this._toggles = null; } async prepare() { - this._toggles = document.querySelectorAll('.clipboard-toggle'); + this._toggles = document.querySelectorAll('.permissions-toggle'); for (const toggle of this._toggles) { - toggle.addEventListener('change', this._onClipboardToggleChange.bind(this), false); + toggle.addEventListener('change', this._onPermissionsToggleChange.bind(this), false); } this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this)); this._settingsController.on('permissionsChanged', this._onPermissionsChanged.bind(this)); @@ -43,7 +43,7 @@ class ClipboardPopupsController { _onOptionsChanged({options}) { const accessor = new ObjectPropertyAccessor(options); for (const toggle of this._toggles) { - const path = ObjectPropertyAccessor.getPathArray(toggle.dataset.clipboardSetting); + const path = ObjectPropertyAccessor.getPathArray(toggle.dataset.permissionsSetting); let value; try { value = accessor.get(path, path.length); @@ -55,25 +55,25 @@ class ClipboardPopupsController { this._updateValidity(); } - async _onClipboardToggleChange(e) { + async _onPermissionsToggleChange(e) { const toggle = e.currentTarget; let value = toggle.checked; if (value) { toggle.checked = false; - value = await this._settingsController.setPermissionsGranted(['clipboardRead'], true); + value = await this._settingsController.setPermissionsGranted(this._getRequiredPermissions(toggle), true); toggle.checked = value; } this._setToggleValid(toggle, true); - await this._settingsController.setProfileSetting(toggle.dataset.clipboardSetting, value); + await this._settingsController.setProfileSetting(toggle.dataset.permissionsSetting, value); } _onPermissionsChanged({permissions: {permissions}}) { const permissionsSet = new Set(permissions); for (const toggle of this._toggles) { - const valid = !toggle.checked || permissionsSet.has('clipboardRead'); + const valid = !toggle.checked || this._hasAll(permissionsSet, this._getRequiredPermissions(toggle)); this._setToggleValid(toggle, valid); } } @@ -88,4 +88,16 @@ class ClipboardPopupsController { const permissions = await this._settingsController.getAllPermissions(); this._onPermissionsChanged({permissions}); } + + _hasAll(set, values) { + for (const value of values) { + if (!set.has(value)) { return false; } + } + return true; + } + + _getRequiredPermissions(toggle) { + const requiredPermissions = toggle.dataset.requiredPermissions; + return (typeof requiredPermissions === 'string' && requiredPermissions.length > 0 ? requiredPermissions.split(' ') : []); + } } diff --git a/ext/bg/js/settings2/settings-main.js b/ext/bg/js/settings2/settings-main.js index 31c5b0fa..f4a38d85 100644 --- a/ext/bg/js/settings2/settings-main.js +++ b/ext/bg/js/settings2/settings-main.js @@ -20,7 +20,6 @@ * AnkiTemplatesController * AudioController * BackupController - * ClipboardPopupsController * DictionaryController * DictionaryImportController * DocumentFocusController @@ -29,6 +28,7 @@ * KeyboardShortcutController * ModalController * NestedPopupsController + * PermissionsToggleController * PopupPreviewController * PopupWindowController * ProfileController @@ -119,8 +119,8 @@ async function setupGenericSettingsController(genericSettingController) { const nestedPopupsController = new NestedPopupsController(settingsController); nestedPopupsController.prepare(); - const clipboardPopupsController = new ClipboardPopupsController(settingsController); - clipboardPopupsController.prepare(); + const permissionsToggleController = new PermissionsToggleController(settingsController); + permissionsToggleController.prepare(); const secondarySearchDictionaryController = new SecondarySearchDictionaryController(settingsController); secondarySearchDictionaryController.prepare(); |