2020-05-30 00:33:40 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* globals
|
2020-05-31 01:53:36 +00:00
|
|
|
* DOMDataBinder
|
2020-05-30 00:33:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
class GenericSettingController {
|
2020-05-30 13:33:13 +00:00
|
|
|
constructor(settingsController) {
|
|
|
|
this._settingsController = settingsController;
|
2020-05-31 01:53:36 +00:00
|
|
|
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([
|
2020-12-31 18:10:23 +00:00
|
|
|
['setAttribute', this._setAttribute.bind(this)],
|
2020-10-11 03:17:51 +00:00
|
|
|
['setVisibility', this._setVisibility.bind(this)],
|
2020-05-31 01:53:36 +00:00
|
|
|
['splitTags', this._splitTags.bind(this)],
|
2020-10-08 21:38:18 +00:00
|
|
|
['joinTags', this._joinTags.bind(this)],
|
|
|
|
['toNumber', this._toNumber.bind(this)],
|
2020-12-31 18:38:51 +00:00
|
|
|
['toString', this._toString.bind(this)],
|
|
|
|
['conditionalConvert', this._conditionalConvert.bind(this)]
|
2020-05-31 01:53:36 +00:00
|
|
|
]);
|
2020-05-30 00:33:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 13:33:13 +00:00
|
|
|
async prepare() {
|
2020-05-31 01:53:36 +00:00
|
|
|
this._dataBinder.observe(document.body);
|
2020-05-30 13:33:13 +00:00
|
|
|
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
|
2020-05-30 00:33:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 01:38:29 +00:00
|
|
|
async refresh() {
|
|
|
|
await this._dataBinder.refresh();
|
|
|
|
}
|
|
|
|
|
2020-05-30 00:33:40 +00:00
|
|
|
// Private
|
|
|
|
|
2020-05-30 20:20:31 +00:00
|
|
|
_onOptionsChanged() {
|
2020-05-31 01:53:36 +00:00
|
|
|
this._dataBinder.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
_createElementMetadata(element) {
|
2020-12-31 18:10:23 +00:00
|
|
|
const {dataset: {setting: path, scope, transform: transformRaw}} = element;
|
|
|
|
let transforms;
|
|
|
|
if (typeof transformRaw === 'string') {
|
|
|
|
transforms = JSON.parse(transformRaw);
|
|
|
|
if (!Array.isArray(transforms)) { transforms = [transforms]; }
|
|
|
|
} else {
|
|
|
|
transforms = [];
|
|
|
|
}
|
2020-05-31 01:53:36 +00:00
|
|
|
return {
|
2020-10-11 03:17:51 +00:00
|
|
|
path,
|
|
|
|
scope,
|
2020-12-31 18:10:23 +00:00
|
|
|
transforms,
|
|
|
|
transformRaw
|
2020-05-31 01:53:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_compareElementMetadata(metadata1, metadata2) {
|
|
|
|
return (
|
|
|
|
metadata1.path === metadata2.path &&
|
|
|
|
metadata1.scope === metadata2.scope &&
|
2020-12-31 18:10:23 +00:00
|
|
|
metadata1.transformRaw === metadata2.transformRaw
|
2020-05-31 01:53:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-05-30 00:33:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 01:53:36 +00:00
|
|
|
async _setValues(targets) {
|
|
|
|
const defaultScope = this._defaultScope;
|
|
|
|
const settingsTargets = [];
|
2020-12-31 18:10:23 +00:00
|
|
|
for (const {metadata: {path, scope, transforms}, value, element} of targets) {
|
|
|
|
const transformedValue = this._applyTransforms(value, transforms, 'pre', element);
|
2020-05-31 01:53:36 +00:00
|
|
|
const target = {
|
|
|
|
path,
|
|
|
|
scope: scope || defaultScope,
|
|
|
|
action: 'set',
|
2020-12-31 18:10:23 +00:00
|
|
|
value: transformedValue
|
2020-05-31 01:53:36 +00:00
|
|
|
};
|
|
|
|
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); }
|
2020-12-31 18:10:23 +00:00
|
|
|
const {metadata: {transforms}, element} = targets[i];
|
|
|
|
const result = this._applyTransforms(value.result, transforms, 'post', element);
|
2020-05-31 01:53:36 +00:00
|
|
|
return {result};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-31 18:10:23 +00:00
|
|
|
_applyTransforms(value, transforms, step, element) {
|
|
|
|
for (const transform of transforms) {
|
|
|
|
const transformStep = transform.step;
|
|
|
|
if (typeof transformStep !== 'undefined' && transformStep !== step) { continue; }
|
|
|
|
|
|
|
|
const transformFunction = this._transforms.get(transform.type);
|
|
|
|
if (typeof transformFunction === 'undefined') { continue; }
|
|
|
|
|
|
|
|
value = transformFunction(value, transform, element);
|
2020-05-31 01:53:36 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-10-11 03:17:51 +00:00
|
|
|
_getAncestor(node, ancestorDistance) {
|
2020-12-31 18:10:23 +00:00
|
|
|
if (ancestorDistance < 0) {
|
|
|
|
return document.documentElement;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < ancestorDistance && node !== null; ++i) {
|
|
|
|
node = node.parentNode;
|
2020-10-11 03:17:51 +00:00
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2020-12-31 18:10:23 +00:00
|
|
|
_getRelativeElement(node, ancestorDistance, selector) {
|
|
|
|
const selectorRoot = (
|
|
|
|
typeof ancestorDistance === 'number' ?
|
|
|
|
this._getAncestor(node, ancestorDistance) :
|
|
|
|
document
|
|
|
|
);
|
|
|
|
if (selectorRoot === null) { return null; }
|
2020-10-11 03:17:51 +00:00
|
|
|
|
|
|
|
return (
|
2020-12-31 18:10:23 +00:00
|
|
|
typeof selector === 'string' ?
|
|
|
|
selectorRoot.querySelector(selector) :
|
|
|
|
(selectorRoot === document ? document.documentElement : selectorRoot)
|
2020-10-11 03:17:51 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_evaluateSimpleOperation(operation, lhs, rhs) {
|
|
|
|
switch (operation) {
|
|
|
|
case '!': return !lhs;
|
|
|
|
case '!!': return !!lhs;
|
|
|
|
case '===': return lhs === rhs;
|
|
|
|
case '!==': return lhs !== rhs;
|
|
|
|
case '>=': return lhs >= rhs;
|
|
|
|
case '<=': return lhs <= rhs;
|
|
|
|
case '>': return lhs > rhs;
|
|
|
|
case '<': return lhs < rhs;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 01:53:36 +00:00
|
|
|
// Transforms
|
|
|
|
|
2020-12-31 18:10:23 +00:00
|
|
|
_setAttribute(value, data, element) {
|
|
|
|
const {ancestorDistance, selector, attribute} = data;
|
|
|
|
const relativeElement = this._getRelativeElement(element, ancestorDistance, selector);
|
2020-09-12 17:20:02 +00:00
|
|
|
if (relativeElement !== null) {
|
2020-12-31 18:10:23 +00:00
|
|
|
relativeElement.setAttribute(attribute, `${value}`);
|
2020-10-11 03:17:51 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-12-31 18:10:23 +00:00
|
|
|
_setVisibility(value, data, element) {
|
|
|
|
const {ancestorDistance, selector, condition} = data;
|
|
|
|
const relativeElement = this._getRelativeElement(element, ancestorDistance, selector);
|
2020-10-11 03:17:51 +00:00
|
|
|
if (relativeElement !== null) {
|
2020-12-31 18:10:23 +00:00
|
|
|
relativeElement.hidden = !this._evaluateSimpleOperation(condition.op, value, condition.value);
|
2020-09-12 17:20:02 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-05-30 20:20:31 +00:00
|
|
|
_splitTags(value) {
|
|
|
|
return `${value}`.split(/[,; ]+/).filter((v) => !!v);
|
2020-05-30 00:33:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-30 20:20:31 +00:00
|
|
|
_joinTags(value) {
|
|
|
|
return value.join(' ');
|
2020-05-30 00:33:40 +00:00
|
|
|
}
|
2020-09-12 17:20:02 +00:00
|
|
|
|
2020-12-31 18:10:23 +00:00
|
|
|
_toNumber(value, data) {
|
|
|
|
let {constraints} = data;
|
|
|
|
if (!isObject(constraints)) { constraints = {}; }
|
|
|
|
return DOMDataBinder.convertToNumber(value, constraints);
|
2020-10-08 21:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_toString(value) {
|
|
|
|
return `${value}`;
|
|
|
|
}
|
2020-12-31 18:38:51 +00:00
|
|
|
|
|
|
|
_conditionalConvert(value, data) {
|
|
|
|
const {cases} = data;
|
|
|
|
if (Array.isArray(cases)) {
|
|
|
|
for (const {op, value: value2, default: isDefault, result} of cases) {
|
|
|
|
if (isDefault === true) {
|
|
|
|
value = result;
|
|
|
|
} else if (this._evaluateSimpleOperation(op, value, value2)) {
|
|
|
|
value = result;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2020-05-30 00:33:40 +00:00
|
|
|
}
|