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([
|
|
|
|
['setDocumentAttribute', this._setDocumentAttribute.bind(this)],
|
2020-09-12 17:20:02 +00:00
|
|
|
['setRelativeAttribute', this._setRelativeAttribute.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)],
|
|
|
|
['toString', this._toString.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-10-11 03:17:51 +00:00
|
|
|
const {dataset: {setting: path, scope, transform, transformPre, transformPost}} = element;
|
2020-05-31 01:53:36 +00:00
|
|
|
return {
|
2020-10-11 03:17:51 +00:00
|
|
|
path,
|
|
|
|
scope,
|
|
|
|
transformPre: typeof transformPre === 'string' ? transformPre : transform,
|
|
|
|
transformPost: typeof transformPost === 'string' ? transformPost : transform
|
2020-05-31 01:53:36 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_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);
|
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 = [];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-11 03:17:51 +00:00
|
|
|
_getAncestor(node, ancestorDistance) {
|
|
|
|
if (typeof ancestorDistance === 'string') {
|
|
|
|
const ii = Number.parseInt(ancestorDistance, 10);
|
|
|
|
if (Number.isFinite(ii)) {
|
|
|
|
if (ii < 0) {
|
|
|
|
node = document.documentElement;
|
|
|
|
} else {
|
|
|
|
for (let i = 0; i < ii && node !== null; ++i) {
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getElementRelativeToAncestor(node, ancestorDistance, relativeSelector) {
|
|
|
|
const relativeElement = this._getAncestor(node, ancestorDistance);
|
|
|
|
if (relativeElement === null) { return null; }
|
|
|
|
|
|
|
|
return (
|
|
|
|
typeof relativeSelector === 'string' ?
|
|
|
|
relativeElement.querySelector(relativeSelector) :
|
|
|
|
relativeElement
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getConditionalResult(value, conditionString) {
|
|
|
|
let op = '!!';
|
|
|
|
let rhsOperand = null;
|
|
|
|
try {
|
|
|
|
if (typeof conditionString === 'string') {
|
|
|
|
const {op: op2, value: value2} = JSON.parse(conditionString);
|
|
|
|
op = (typeof op2 === 'string' ? op2 : '===');
|
|
|
|
rhsOperand = value2;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
return this._evaluateSimpleOperation(op, value, rhsOperand);
|
|
|
|
}
|
|
|
|
|
|
|
|
_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-05-30 20:20:31 +00:00
|
|
|
_setDocumentAttribute(value, metadata, element) {
|
|
|
|
document.documentElement.setAttribute(element.dataset.documentAttribute, `${value}`);
|
|
|
|
return value;
|
2020-05-30 00:33:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:20:02 +00:00
|
|
|
_setRelativeAttribute(value, metadata, element) {
|
|
|
|
const {ancestorDistance, relativeSelector, relativeAttribute} = element.dataset;
|
2020-10-11 03:17:51 +00:00
|
|
|
const relativeElement = this._getElementRelativeToAncestor(element, ancestorDistance, relativeSelector);
|
2020-09-12 17:20:02 +00:00
|
|
|
if (relativeElement !== null) {
|
2020-10-11 03:17:51 +00:00
|
|
|
relativeElement.setAttribute(relativeAttribute, `${value}`);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setVisibility(value, metadata, element) {
|
|
|
|
const {ancestorDistance, relativeSelector, visbilityCondition} = element.dataset;
|
|
|
|
const relativeElement = this._getElementRelativeToAncestor(element, ancestorDistance, relativeSelector);
|
|
|
|
if (relativeElement !== null) {
|
|
|
|
relativeElement.hidden = !this._getConditionalResult(value, visbilityCondition);
|
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-10-08 21:38:18 +00:00
|
|
|
_toNumber(value, metadata, element) {
|
|
|
|
return DOMDataBinder.convertToNumber(value, element.dataset);
|
|
|
|
}
|
|
|
|
|
|
|
|
_toString(value) {
|
|
|
|
return `${value}`;
|
|
|
|
}
|
2020-05-30 00:33:40 +00:00
|
|
|
}
|