summaryrefslogtreecommitdiff
path: root/ext/bg/js/settings
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-30 21:53:36 -0400
committerGitHub <noreply@github.com>2020-05-30 21:53:36 -0400
commitdb209c9116135528bbe0bffcd9763e9914da2441 (patch)
treea57f4bb22a67168f78521e43aff859cea3609369 /ext/bg/js/settings
parentce7f9dd09a85f182a9ea98c3366bed6c2ecbb1b4 (diff)
Generic settings controller merge (#584)
* Update how optionsContext is assigned to targets * Add getSettings and modifySettings * Merge DOMSettingsBinder into GenericSettingController * Remove old DOMSettingsBinder
Diffstat (limited to 'ext/bg/js/settings')
-rw-r--r--ext/bg/js/settings/dom-settings-binder.js122
-rw-r--r--ext/bg/js/settings/generic-setting-controller.js100
-rw-r--r--ext/bg/js/settings/settings-controller.js26
3 files changed, 107 insertions, 141 deletions
diff --git a/ext/bg/js/settings/dom-settings-binder.js b/ext/bg/js/settings/dom-settings-binder.js
deleted file mode 100644
index 07da4f37..00000000
--- a/ext/bg/js/settings/dom-settings-binder.js
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * Copyright (C) 2020 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/>.
- */
-
-/* global
- * DOMDataBinder
- * api
- */
-
-class DOMSettingsBinder {
- constructor({getOptionsContext, source=null, transforms=null}) {
- this._getOptionsContext = getOptionsContext;
- this._source = source;
- this._defaultScope = 'profile';
- this._dataBinder = new DOMDataBinder({
- selector: '[data-setting]',
- createElementMetadata: this._createElementMetadata.bind(this),
- compareElementMetadata: this._compareElementMetadata.bind(this),
- getValues: this._getValues.bind(this),
- setValues: this._setValues.bind(this)
- });
- this._transforms = new Map(transforms !== null ? transforms : []);
- }
-
- observe(element) {
- this._dataBinder.observe(element);
- }
-
- disconnect() {
- this._dataBinder.disconnect();
- }
-
- refresh() {
- this._dataBinder.refresh();
- }
-
- // Private
-
- _createElementMetadata(element) {
- return {
- path: element.dataset.setting,
- scope: element.dataset.scope,
- transformPre: element.dataset.transformPre,
- transformPost: element.dataset.transformPost
- };
- }
-
- _compareElementMetadata(metadata1, metadata2) {
- return (
- metadata1.path === metadata2.path &&
- metadata1.scope === metadata2.scope &&
- metadata1.transformPre === metadata2.transformPre &&
- metadata1.transformPost === metadata2.transformPost
- );
- }
-
- async _getValues(targets) {
- const settingsTargets = [];
- for (const {metadata: {path, scope}} of targets) {
- const target = {
- path,
- scope: scope || this._defaultScope
- };
- if (target.scope === 'profile') {
- target.optionsContext = this._getOptionsContext();
- }
- settingsTargets.push(target);
- }
- return this._transformResults(await api.getSettings(settingsTargets), targets);
- }
-
- async _setValues(targets) {
- const settingsTargets = [];
- for (const {metadata, value, element} of targets) {
- const {path, scope, transformPre} = metadata;
- const target = {
- path,
- scope: scope || this._defaultScope,
- action: 'set',
- value: this._transform(value, transformPre, metadata, element)
- };
- if (target.scope === 'profile') {
- target.optionsContext = this._getOptionsContext();
- }
- settingsTargets.push(target);
- }
- return this._transformResults(await api.modifySettings(settingsTargets, this._source), targets);
- }
-
- _transform(value, transform, metadata, element) {
- if (typeof transform === 'string') {
- const transformFunction = this._transforms.get(transform);
- if (typeof transformFunction !== 'undefined') {
- value = transformFunction(value, metadata, element);
- }
- }
- return value;
- }
-
- _transformResults(values, targets) {
- return values.map((value, i) => {
- const error = value.error;
- if (error) { return jsonToError(error); }
- const {metadata, element} = targets[i];
- const result = this._transform(value.result, metadata.transformPost, metadata, element);
- return {result};
- });
- }
-}
diff --git a/ext/bg/js/settings/generic-setting-controller.js b/ext/bg/js/settings/generic-setting-controller.js
index aa3118e5..bdea8e3d 100644
--- a/ext/bg/js/settings/generic-setting-controller.js
+++ b/ext/bg/js/settings/generic-setting-controller.js
@@ -16,37 +16,107 @@
*/
/* globals
- * DOMSettingsBinder
- * utilBackgroundIsolate
+ * DOMDataBinder
*/
class GenericSettingController {
constructor(settingsController) {
this._settingsController = settingsController;
- this._settingsBinder = null;
+ this._defaultScope = 'profile';
+ this._dataBinder = new DOMDataBinder({
+ selector: '[data-setting]',
+ createElementMetadata: this._createElementMetadata.bind(this),
+ compareElementMetadata: this._compareElementMetadata.bind(this),
+ getValues: this._getValues.bind(this),
+ setValues: this._setValues.bind(this)
+ });
+ this._transforms = new Map([
+ ['setDocumentAttribute', this._setDocumentAttribute.bind(this)],
+ ['splitTags', this._splitTags.bind(this)],
+ ['joinTags', this._joinTags.bind(this)]
+ ]);
}
async prepare() {
- this._settingsBinder = new DOMSettingsBinder({
- getOptionsContext: () => this._settingsController.getOptionsContext(),
- source: this._settingsController.source,
- transforms: [
- ['setDocumentAttribute', this._setDocumentAttribute.bind(this)],
- ['splitTags', this._splitTags.bind(this)],
- ['joinTags', this._joinTags.bind(this)]
- ]
- });
- this._settingsBinder.observe(document.body);
-
+ this._dataBinder.observe(document.body);
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
}
// Private
_onOptionsChanged() {
- this._settingsBinder.refresh();
+ this._dataBinder.refresh();
+ }
+
+ _createElementMetadata(element) {
+ return {
+ path: element.dataset.setting,
+ scope: element.dataset.scope,
+ transformPre: element.dataset.transformPre,
+ transformPost: element.dataset.transformPost
+ };
+ }
+
+ _compareElementMetadata(metadata1, metadata2) {
+ return (
+ metadata1.path === metadata2.path &&
+ metadata1.scope === metadata2.scope &&
+ metadata1.transformPre === metadata2.transformPre &&
+ metadata1.transformPost === metadata2.transformPost
+ );
+ }
+
+ async _getValues(targets) {
+ const defaultScope = this._defaultScope;
+ const settingsTargets = [];
+ for (const {metadata: {path, scope}} of targets) {
+ const target = {
+ path,
+ scope: scope || defaultScope
+ };
+ settingsTargets.push(target);
+ }
+ return this._transformResults(await this._settingsController.getSettings(settingsTargets), targets);
}
+ async _setValues(targets) {
+ const defaultScope = this._defaultScope;
+ const settingsTargets = [];
+ for (const {metadata, value, element} of targets) {
+ const {path, scope, transformPre} = metadata;
+ const target = {
+ path,
+ scope: scope || defaultScope,
+ action: 'set',
+ value: this._transform(value, transformPre, metadata, element)
+ };
+ settingsTargets.push(target);
+ }
+ return this._transformResults(await this._settingsController.modifySettings(settingsTargets), targets);
+ }
+
+ _transformResults(values, targets) {
+ return values.map((value, i) => {
+ const error = value.error;
+ if (error) { return jsonToError(error); }
+ const {metadata, element} = targets[i];
+ const result = this._transform(value.result, metadata.transformPost, metadata, element);
+ return {result};
+ });
+ }
+
+ _transform(value, transform, metadata, element) {
+ if (typeof transform === 'string') {
+ const transformFunction = this._transforms.get(transform);
+ if (typeof transformFunction !== 'undefined') {
+ value = transformFunction(value, metadata, element);
+ }
+ }
+ return value;
+ }
+
+ // Transforms
+
_setDocumentAttribute(value, metadata, element) {
document.documentElement.setAttribute(element.dataset.documentAttribute, `${value}`);
return value;
diff --git a/ext/bg/js/settings/settings-controller.js b/ext/bg/js/settings/settings-controller.js
index 4c902dff..87dea408 100644
--- a/ext/bg/js/settings/settings-controller.js
+++ b/ext/bg/js/settings/settings-controller.js
@@ -73,12 +73,20 @@ class SettingsController extends EventDispatcher {
this._setProfileIndex(profileIndex);
}
+ async getSettings(targets) {
+ return await this._getSettings(targets, {});
+ }
+
async getGlobalSettings(targets) {
return await this._getSettings(targets, {scope: 'global'});
}
async getProfileSettings(targets) {
- return await this._getSettings(targets, {scope: 'profile', optionsContext: this.getOptionsContext()});
+ return await this._getSettings(targets, {scope: 'profile'});
+ }
+
+ async modifySettings(targets) {
+ return await this._modifySettings(targets, {});
}
async modifyGlobalSettings(targets) {
@@ -86,7 +94,7 @@ class SettingsController extends EventDispatcher {
}
async modifyProfileSettings(targets) {
- return await this._modifySettings(targets, {scope: 'profile', optionsContext: this.getOptionsContext()});
+ return await this._modifySettings(targets, {scope: 'profile'});
}
async setGlobalSetting(path, value) {
@@ -120,13 +128,23 @@ class SettingsController extends EventDispatcher {
this.trigger('optionsChanged', {options, optionsContext});
}
+ _setupTargets(targets, extraFields) {
+ return targets.map((target) => {
+ target = Object.assign({}, extraFields, target);
+ if (target.scope === 'profile') {
+ target.optionsContext = this.getOptionsContext();
+ }
+ return target;
+ });
+ }
+
async _getSettings(targets, extraFields) {
- targets = targets.map((target) => Object.assign({}, target, extraFields));
+ targets = this._setupTargets(targets, extraFields);
return await api.getSettings(targets);
}
async _modifySettings(targets, extraFields) {
- targets = targets.map((target) => Object.assign({}, target, extraFields));
+ targets = this._setupTargets(targets, extraFields);
return await api.modifySettings(targets, this._source);
}
}