From 41ee167dfdfcd794634e0459185a929e814b0493 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 16 May 2021 10:01:57 -0400 Subject: Add support for generic && and || operations for settings transforms (#1681) --- .../pages/settings/generic-setting-controller.js | 30 ++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'ext/js/pages') diff --git a/ext/js/pages/settings/generic-setting-controller.js b/ext/js/pages/settings/generic-setting-controller.js index 7d6fc2e6..48a8e19a 100644 --- a/ext/js/pages/settings/generic-setting-controller.js +++ b/ext/js/pages/settings/generic-setting-controller.js @@ -159,7 +159,8 @@ class GenericSettingController { ); } - _evaluateSimpleOperation(operation, lhs, rhs) { + _evaluateSimpleOperation(operationData, lhs) { + const {op: operation, value: rhs} = operationData; switch (operation) { case '!': return !lhs; case '!!': return !!lhs; @@ -169,7 +170,20 @@ class GenericSettingController { case '<=': return lhs <= rhs; case '>': return lhs > rhs; case '<': return lhs < rhs; - default: return false; + case '&&': + for (const operationData2 of rhs) { + const result = this._evaluateSimpleOperation(operationData2, lhs); + if (!result) { return result; } + } + return true; + case '||': + for (const operationData2 of rhs) { + const result = this._evaluateSimpleOperation(operationData2, lhs); + if (result) { return result; } + } + return false; + default: + return false; } } @@ -188,7 +202,7 @@ class GenericSettingController { const {ancestorDistance, selector, condition} = data; const relativeElement = this._getRelativeElement(element, ancestorDistance, selector); if (relativeElement !== null) { - relativeElement.hidden = !this._evaluateSimpleOperation(condition.op, value, condition.value); + relativeElement.hidden = !this._evaluateSimpleOperation(condition, value); } return value; } @@ -218,11 +232,11 @@ class GenericSettingController { _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; + for (const caseData of cases) { + if (caseData.default === true) { + value = caseData.result; + } else if (this._evaluateSimpleOperation(caseData, value)) { + value = caseData.result; break; } } -- cgit v1.2.3