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 | |
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
-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 | ||||
-rw-r--r-- | ext/bg/settings.html | 4 | ||||
-rw-r--r-- | ext/bg/settings2.html | 6 |
5 files changed, 31 insertions, 19 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(); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index b0a44437..3064e97a 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -162,7 +162,7 @@ </div> <div class="checkbox options-advanced ignore-form-changes" data-hide-for-browser="firefox-mobile"> - <label><input type="checkbox" class="clipboard-toggle" data-clipboard-setting="clipboard.enableBackgroundMonitor"> Enable native popups when copying Japanese text</label> + <label><input type="checkbox" class="permissions-toggle" data-permissions-setting="clipboard.enableBackgroundMonitor" data-required-permissions="clipboardRead"> Enable native popups when copying Japanese text</label> </div> <div class="checkbox options-advanced"> @@ -1316,7 +1316,7 @@ <script src="/bg/js/settings/anki-templates-controller.js"></script> <script src="/bg/js/settings/audio-controller.js"></script> <script src="/bg/js/settings/backup-controller.js"></script> - <script src="/bg/js/settings/clipboard-popups-controller.js"></script> + <script src="/bg/js/settings/permissions-toggle-controller.js"></script> <script src="/bg/js/settings/dictionary-controller.js"></script> <script src="/bg/js/settings/dictionary-import-controller.js"></script> <script src="/bg/js/settings/generic-setting-controller.js"></script> diff --git a/ext/bg/settings2.html b/ext/bg/settings2.html index 9c366ecd..afb7af20 100644 --- a/ext/bg/settings2.html +++ b/ext/bg/settings2.html @@ -1573,7 +1573,7 @@ <div class="settings-item-description">Open the search page in a new window when the clipboard contains Japanese text.</div> </div> <div class="settings-item-right"> - <label class="toggle"><input type="checkbox" class="clipboard-toggle" data-clipboard-setting="clipboard.enableBackgroundMonitor"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label> + <label class="toggle"><input type="checkbox" class="permissions-toggle" data-permissions-setting="clipboard.enableBackgroundMonitor" data-required-permissions="clipboardRead"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label> </div> </div></div> <div class="settings-item"><div class="settings-item-inner"> @@ -1583,7 +1583,7 @@ <div class="settings-item-description">The query on the search page will be automatically updated with text in the clipboard.</div> </div> <div class="settings-item-right"> - <label class="toggle"><input type="checkbox" class="clipboard-toggle" data-clipboard-setting="clipboard.enableSearchPageMonitor"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label> + <label class="toggle"><input type="checkbox" class="permissions-toggle" data-permissions-setting="clipboard.enableSearchPageMonitor" data-required-permissions="clipboardRead"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label> </div> </div></div> <div class="settings-item"><div class="settings-item-inner settings-item-inner-wrappable"> @@ -3219,7 +3219,7 @@ <script src="/bg/js/settings/anki-templates-controller.js"></script> <script src="/bg/js/settings/audio-controller.js"></script> <script src="/bg/js/settings/backup-controller.js"></script> -<script src="/bg/js/settings/clipboard-popups-controller.js"></script> +<script src="/bg/js/settings/permissions-toggle-controller.js"></script> <script src="/bg/js/settings/dictionary-controller.js"></script> <script src="/bg/js/settings/dictionary-import-controller.js"></script> <script src="/bg/js/settings/generic-setting-controller.js"></script> |