Add PermissionsOriginController (#1603)

This commit is contained in:
toasted-nutbread 2021-04-08 22:52:28 -04:00 committed by GitHub
parent c2aa51fdbb
commit 51c9bef032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 244 additions and 1 deletions

35
ext/css/permissions.css Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2021 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#permissions-origin-list {
display: grid;
grid-template-columns: auto 1fr auto;
grid-template-rows: auto;
align-items: center;
justify-content: center;
}
.permissions-origin-index {
margin: 0 0.5em;
}
input[type=text].permissions-origin-input {
width: auto;
justify-self: stretch;
}
.permissions-origin-button {
justify-self: center;
margin-left: 0.5em;
}

View File

@ -17,6 +17,8 @@
/* global
* DocumentFocusController
* ModalController
* PermissionsOriginController
* PermissionsToggleController
* PersistentStorageController
* SettingsController
@ -95,12 +97,18 @@ function setupPermissionsToggles() {
permissionsCheckboxes[i].checked = permissions[i];
}
const modalController = new ModalController();
modalController.prepare();
const settingsController = new SettingsController(0);
settingsController.prepare();
const permissionsToggleController = new PermissionsToggleController(settingsController);
permissionsToggleController.prepare();
const permissionsOriginController = new PermissionsOriginController(settingsController);
permissionsOriginController.prepare();
const persistentStorageController = new PersistentStorageController();
persistentStorageController.prepare();
@ -108,7 +116,7 @@ function setupPermissionsToggles() {
document.documentElement.dataset.loaded = 'true';
const settingsDisplayController = new SettingsDisplayController(settingsController, null);
const settingsDisplayController = new SettingsDisplayController(settingsController, modalController);
settingsDisplayController.prepare();
} catch (e) {
log.error(e);

View File

@ -0,0 +1,126 @@
/*
* Copyright (C) 2021 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
class PermissionsOriginController {
constructor(settingsController) {
this._settingsController = settingsController;
this._originContainer = null;
this._originEmpty = null;
this._originToggleNodes = null;
this._addOriginInput = null;
this._errorContainer = null;
this._originContainerChildren = [];
this._eventListeners = new EventListenerCollection();
}
async prepare() {
this._originContainer = document.querySelector('#permissions-origin-list');
this._originEmpty = document.querySelector('#permissions-origin-list-empty');
this._originToggleNodes = document.querySelectorAll('.permissions-origin-toggle');
this._addOriginInput = document.querySelector('#permissions-origin-new-input');
this._errorContainer = document.querySelector('#permissions-origin-list-error');
const addButton = document.querySelector('#permissions-origin-add');
for (const node of this._originToggleNodes) {
node.addEventListener('change', this._onOriginToggleChange.bind(this), false);
}
addButton.addEventListener('click', this._onAddButtonClick.bind(this), false);
this._settingsController.on('permissionsChanged', this._onPermissionsChanged.bind(this));
await this._updatePermissions();
}
// Private
_onPermissionsChanged({permissions}) {
this._eventListeners.removeAllEventListeners();
for (const node of this._originContainerChildren) {
if (node.parentNode === null) { continue; }
node.parentNode.removeChild(node);
}
this._originContainerChildren = [];
const originsSet = new Set(permissions.origins);
for (const node of this._originToggleNodes) {
node.checked = originsSet.has(node.dataset.origin);
}
let any = false;
const excludeOrigins = new Set([
'<all_urls>'
]);
const fragment = document.createDocumentFragment();
for (const origin of permissions.origins) {
if (excludeOrigins.has(origin)) { continue; }
const node = this._settingsController.instantiateTemplateFragment('permissions-origin');
const input = node.querySelector('.permissions-origin-input');
const menuButton = node.querySelector('.permissions-origin-button');
input.value = origin;
this._eventListeners.addEventListener(menuButton, 'menuClose', this._onOriginMenuClose.bind(this, origin), false);
this._originContainerChildren.push(...node.children);
fragment.appendChild(node);
any = true;
}
this._originContainer.insertBefore(fragment, this._originContainer.firstChild);
this._originEmpty.hidden = any;
this._errorContainer.hidden = true;
}
_onOriginToggleChange(e) {
const node = e.currentTarget;
const value = node.checked;
node.checked = !value;
const {origin} = node.dataset;
this._setOriginPermissionEnabled(origin, value);
}
_onOriginMenuClose(origin) {
this._setOriginPermissionEnabled(origin, false);
}
_onAddButtonClick() {
this._addOrigin();
}
async _addOrigin() {
const origin = this._addOriginInput.value;
const added = await this._setOriginPermissionEnabled(origin, true);
if (added) {
this._addOriginInput.value = '';
}
}
async _updatePermissions() {
const permissions = await this._settingsController.permissionsUtil.getAllPermissions();
this._onPermissionsChanged({permissions});
}
async _setOriginPermissionEnabled(origin, enabled) {
let added = false;
try {
added = await this._settingsController.permissionsUtil.setPermissionsGranted({origins: [origin]}, enabled);
} catch (e) {
this._errorContainer.hidden = false;
this._errorContainer.textContent = e.message;
}
if (!added) { return false; }
await this._updatePermissions();
return true;
}
}

View File

@ -13,6 +13,7 @@
<link rel="icon" type="image/png" href="/images/icon128.png" sizes="128x128">
<link rel="stylesheet" type="text/css" href="/css/material.css">
<link rel="stylesheet" type="text/css" href="/css/settings.css">
<link rel="stylesheet" type="text/css" href="/css/permissions.css">
</head>
<body>
@ -175,6 +176,14 @@
<label class="toggle"><input type="checkbox" id="storage-persistent-checkbox"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
</div>
</div></div>
<div class="settings-item settings-item-button" data-modal-action="show,origins"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-label">Configure allowed origins&hellip;</div>
</div>
<div class="settings-item-right open-panel-button-container">
<button class="icon-button"><span class="icon-button-inner"><span class="icon" data-icon="material-right-arrow"></span></span></button>
</div>
</div></div>
</div>
<div class="footer-padding"></div>
@ -182,6 +191,66 @@
</div>
</div></div>
<!-- Auxiliary content -->
<div id="popup-menus"></div>
<!-- Keyboard shortcuts templates -->
<div id="origins-modal" class="modal" tabindex="-1" role="dialog" hidden><div class="modal-content">
<div class="modal-header">
<div class="modal-title">Allowed Origins</div>
<div class="modal-header-button-container">
<div class="modal-header-button-group">
<button class="icon-button modal-header-button" data-modal-action="expand"><span class="icon-button-inner"><span class="icon" data-icon="expand"></span></span></button>
<button class="icon-button modal-header-button" data-modal-action="collapse"><span class="icon-button-inner"><span class="icon" data-icon="collapse"></span></span></button>
</div>
</div>
</div>
<div class="modal-body">
<div class="settings-item"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-label">Allow access to all URLs</div>
</div>
<div class="settings-item-right">
<label class="toggle"><input type="checkbox" class="permissions-origin-toggle" data-origin="&lt;all_urls&gt;"><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">
<div class="settings-item-left">
<div class="settings-item-label">Additional origins</div>
</div>
</div>
<div class="settings-item-children">
<div id="permissions-origin-list" class="generic-list">
<div class="permissions-origin-index generic-list-index-prefix"></div>
<input type="text" class="permissions-origin-input horizontal-flex-fill" autocomplete="off" spellcheck="false" id="permissions-origin-new-input">
<button class="low-emphasis permissions-origin-button" id="permissions-origin-add">Add</button>
</div>
<div id="permissions-origin-list-empty" class="margin-above">
No additional origins specified.
</div>
<div id="permissions-origin-list-error" class="margin-above danger-text" hidden></div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="low-emphasis" data-modal-action="hide">Close</button>
</div>
</div></div>
<template id="permissions-origin-template">
<div class="permissions-origin-index generic-list-index-prefix"></div>
<input type="text" class="permissions-origin-input horizontal-flex-fill" autocomplete="off" spellcheck="false" readonly>
<button class="icon-button permissions-origin-menu-button permissions-origin-button" data-menu="permissions-origin-menu" data-menu-position="below left"><span class="icon-button-inner"><span class="icon" data-icon="kebab-menu"></span></span></button>
</template>
<template id="permissions-origin-menu-template"><div class="popup-menu-container" tabindex="-1" role="dialog"><div class="popup-menu"><div class="popup-menu-body">
<button class="popup-menu-item" data-menu-action="remove">Remove</button>
</div></div></div></template>
<!-- Scripts -->
<script src="/js/core.js"></script>
@ -193,7 +262,12 @@
<script src="/js/data/permissions-util.js"></script>
<script src="/js/dom/document-focus-controller.js"></script>
<script src="/js/dom/html-template-collection.js"></script>
<script src="/js/dom/panel-element.js"></script>
<script src="/js/dom/popup-menu.js"></script>
<script src="/js/dom/selector-observer.js"></script>
<script src="/js/pages/settings/modal.js"></script>
<script src="/js/pages/settings/modal-controller.js"></script>
<script src="/js/pages/settings/permissions-origin-controller.js"></script>
<script src="/js/pages/settings/permissions-toggle-controller.js"></script>
<script src="/js/pages/settings/persistent-storage-controller.js"></script>
<script src="/js/pages/settings/settings-controller.js"></script>