Permissions toggle controller (#1347)

* Move file, rename class

* Expose required permissions in an attribute

* Update selector

* Update attribute

* Fix global declaration order
This commit is contained in:
toasted-nutbread 2021-02-04 22:17:42 -05:00 committed by GitHub
parent 47f16862d0
commit 8f97ca0aac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 19 deletions

View File

@ -20,11 +20,11 @@
* AnkiTemplatesController * AnkiTemplatesController
* AudioController * AudioController
* BackupController * BackupController
* ClipboardPopupsController
* DictionaryController * DictionaryController
* DictionaryImportController * DictionaryImportController
* GenericSettingController * GenericSettingController
* ModalController * ModalController
* PermissionsToggleController
* PopupPreviewController * PopupPreviewController
* ProfileController * ProfileController
* ScanInputsController * ScanInputsController
@ -71,8 +71,8 @@ async function setupEnvironmentInfo() {
const genericSettingController = new GenericSettingController(settingsController); const genericSettingController = new GenericSettingController(settingsController);
genericSettingController.prepare(); genericSettingController.prepare();
const clipboardPopupsController = new ClipboardPopupsController(settingsController); const permissionsToggleController = new PermissionsToggleController(settingsController);
clipboardPopupsController.prepare(); permissionsToggleController.prepare();
const popupPreviewController = new PopupPreviewController(settingsController); const popupPreviewController = new PopupPreviewController(settingsController);
popupPreviewController.prepare(); popupPreviewController.prepare();

View File

@ -19,17 +19,17 @@
* ObjectPropertyAccessor * ObjectPropertyAccessor
*/ */
class ClipboardPopupsController { class PermissionsToggleController {
constructor(settingsController) { constructor(settingsController) {
this._settingsController = settingsController; this._settingsController = settingsController;
this._toggles = null; this._toggles = null;
} }
async prepare() { async prepare() {
this._toggles = document.querySelectorAll('.clipboard-toggle'); this._toggles = document.querySelectorAll('.permissions-toggle');
for (const toggle of this._toggles) { 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('optionsChanged', this._onOptionsChanged.bind(this));
this._settingsController.on('permissionsChanged', this._onPermissionsChanged.bind(this)); this._settingsController.on('permissionsChanged', this._onPermissionsChanged.bind(this));
@ -43,7 +43,7 @@ class ClipboardPopupsController {
_onOptionsChanged({options}) { _onOptionsChanged({options}) {
const accessor = new ObjectPropertyAccessor(options); const accessor = new ObjectPropertyAccessor(options);
for (const toggle of this._toggles) { for (const toggle of this._toggles) {
const path = ObjectPropertyAccessor.getPathArray(toggle.dataset.clipboardSetting); const path = ObjectPropertyAccessor.getPathArray(toggle.dataset.permissionsSetting);
let value; let value;
try { try {
value = accessor.get(path, path.length); value = accessor.get(path, path.length);
@ -55,25 +55,25 @@ class ClipboardPopupsController {
this._updateValidity(); this._updateValidity();
} }
async _onClipboardToggleChange(e) { async _onPermissionsToggleChange(e) {
const toggle = e.currentTarget; const toggle = e.currentTarget;
let value = toggle.checked; let value = toggle.checked;
if (value) { if (value) {
toggle.checked = false; toggle.checked = false;
value = await this._settingsController.setPermissionsGranted(['clipboardRead'], true); value = await this._settingsController.setPermissionsGranted(this._getRequiredPermissions(toggle), true);
toggle.checked = value; toggle.checked = value;
} }
this._setToggleValid(toggle, true); this._setToggleValid(toggle, true);
await this._settingsController.setProfileSetting(toggle.dataset.clipboardSetting, value); await this._settingsController.setProfileSetting(toggle.dataset.permissionsSetting, value);
} }
_onPermissionsChanged({permissions: {permissions}}) { _onPermissionsChanged({permissions: {permissions}}) {
const permissionsSet = new Set(permissions); const permissionsSet = new Set(permissions);
for (const toggle of this._toggles) { 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); this._setToggleValid(toggle, valid);
} }
} }
@ -88,4 +88,16 @@ class ClipboardPopupsController {
const permissions = await this._settingsController.getAllPermissions(); const permissions = await this._settingsController.getAllPermissions();
this._onPermissionsChanged({permissions}); 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(' ') : []);
}
} }

View File

@ -20,7 +20,6 @@
* AnkiTemplatesController * AnkiTemplatesController
* AudioController * AudioController
* BackupController * BackupController
* ClipboardPopupsController
* DictionaryController * DictionaryController
* DictionaryImportController * DictionaryImportController
* DocumentFocusController * DocumentFocusController
@ -29,6 +28,7 @@
* KeyboardShortcutController * KeyboardShortcutController
* ModalController * ModalController
* NestedPopupsController * NestedPopupsController
* PermissionsToggleController
* PopupPreviewController * PopupPreviewController
* PopupWindowController * PopupWindowController
* ProfileController * ProfileController
@ -119,8 +119,8 @@ async function setupGenericSettingsController(genericSettingController) {
const nestedPopupsController = new NestedPopupsController(settingsController); const nestedPopupsController = new NestedPopupsController(settingsController);
nestedPopupsController.prepare(); nestedPopupsController.prepare();
const clipboardPopupsController = new ClipboardPopupsController(settingsController); const permissionsToggleController = new PermissionsToggleController(settingsController);
clipboardPopupsController.prepare(); permissionsToggleController.prepare();
const secondarySearchDictionaryController = new SecondarySearchDictionaryController(settingsController); const secondarySearchDictionaryController = new SecondarySearchDictionaryController(settingsController);
secondarySearchDictionaryController.prepare(); secondarySearchDictionaryController.prepare();

View File

@ -162,7 +162,7 @@
</div> </div>
<div class="checkbox options-advanced ignore-form-changes" data-hide-for-browser="firefox-mobile"> <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>
<div class="checkbox options-advanced"> <div class="checkbox options-advanced">
@ -1316,7 +1316,7 @@
<script src="/bg/js/settings/anki-templates-controller.js"></script> <script src="/bg/js/settings/anki-templates-controller.js"></script>
<script src="/bg/js/settings/audio-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/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-controller.js"></script>
<script src="/bg/js/settings/dictionary-import-controller.js"></script> <script src="/bg/js/settings/dictionary-import-controller.js"></script>
<script src="/bg/js/settings/generic-setting-controller.js"></script> <script src="/bg/js/settings/generic-setting-controller.js"></script>

View File

@ -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 class="settings-item-description">Open the search page in a new window when the clipboard contains Japanese text.</div>
</div> </div>
<div class="settings-item-right"> <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> </div></div>
<div class="settings-item"><div class="settings-item-inner"> <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 class="settings-item-description">The query on the search page will be automatically updated with text in the clipboard.</div>
</div> </div>
<div class="settings-item-right"> <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> </div></div>
<div class="settings-item"><div class="settings-item-inner settings-item-inner-wrappable"> <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/anki-templates-controller.js"></script>
<script src="/bg/js/settings/audio-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/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-controller.js"></script>
<script src="/bg/js/settings/dictionary-import-controller.js"></script> <script src="/bg/js/settings/dictionary-import-controller.js"></script>
<script src="/bg/js/settings/generic-setting-controller.js"></script> <script src="/bg/js/settings/generic-setting-controller.js"></script>