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:
parent
47f16862d0
commit
8f97ca0aac
@ -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();
|
||||
|
@ -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(' ') : []);
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
@ -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>
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user