Invert setting values in a common place

pull/21833/head
Travis Ralston 2019-01-25 09:04:56 -07:00
parent cb6f415a05
commit d136c33a87
1 changed files with 13 additions and 20 deletions

View File

@ -228,24 +228,18 @@ export default class SettingsStore {
// Check if we need to invert the setting at all. Do this after we get the setting // Check if we need to invert the setting at all. Do this after we get the setting
// handlers though, otherwise we'll fail to read the value. // handlers though, otherwise we'll fail to read the value.
let inverted = false;
if (setting.invertedSettingName) { if (setting.invertedSettingName) {
//console.warn(`Inverting ${settingName} to be ${setting.invertedSettingName} - legacy setting`); //console.warn(`Inverting ${settingName} to be ${setting.invertedSettingName} - legacy setting`);
settingName = setting.invertedSettingName; settingName = setting.invertedSettingName;
inverted = true;
} }
if (explicit) { if (explicit) {
const handler = handlers[level]; const handler = handlers[level];
if (!handler) { if (!handler) {
let value = SettingsStore._tryControllerOverride(setting, level, roomId, null, null); return SettingsStore._getFinalValue(setting, level, roomId, null, null);
if (inverted) value = !value;
return value;
} }
let value = handler.getValue(settingName, roomId); let value = handler.getValue(settingName, roomId);
value = SettingsStore._tryControllerOverride(setting, level, roomId, value, level); return SettingsStore._getFinalValue(setting, level, roomId, value, level);
if (inverted) value = !value;
return value;
} }
for (let i = minIndex; i < levelOrder.length; i++) { for (let i = minIndex; i < levelOrder.length; i++) {
@ -255,23 +249,22 @@ export default class SettingsStore {
let value = handler.getValue(settingName, roomId); let value = handler.getValue(settingName, roomId);
if (value === null || value === undefined) continue; if (value === null || value === undefined) continue;
value = SettingsStore._tryControllerOverride(setting, level, roomId, value, levelOrder[i]); return SettingsStore._getFinalValue(setting, level, roomId, value, levelOrder[i]);
if (inverted) value = !value;
return value;
} }
let value = SettingsStore._tryControllerOverride(setting, level, roomId, null, null); return SettingsStore._getFinalValue(setting, level, roomId, null, null);
if (inverted) value = !value;
return value;
} }
static _tryControllerOverride(setting, level, roomId, calculatedValue, calculatedAtLevel) { static _getFinalValue(setting, level, roomId, calculatedValue, calculatedAtLevel) {
const controller = setting.controller; let resultingValue = calculatedValue;
if (!controller) return calculatedValue;
const actualValue = controller.getValueOverride(level, roomId, calculatedValue, calculatedAtLevel); if (setting.controller) {
if (actualValue !== undefined && actualValue !== null) return actualValue; const actualValue = setting.controller.getValueOverride(level, roomId, calculatedValue, calculatedAtLevel);
return calculatedValue; if (actualValue !== undefined && actualValue !== null) resultingValue = actualValue;
}
if (setting.invertedSettingName) resultingValue = !resultingValue;
return resultingValue;
} }
/* eslint-disable valid-jsdoc */ //https://github.com/eslint/eslint/issues/7307 /* eslint-disable valid-jsdoc */ //https://github.com/eslint/eslint/issues/7307