Profile conditions fixes and improvements (#782)

* Fix operator changes

* Fix default value not changing during type change

* Fix incorrect display value being assigned

* Refactor _updateValueInput
This commit is contained in:
toasted-nutbread 2020-09-07 13:22:34 -04:00 committed by GitHub
parent 7a59ca2926
commit 59559fc560
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -501,21 +501,29 @@ class ProfileConditionUI {
const operators = this._getDescriptorOperators(type); const operators = this._getDescriptorOperators(type);
const operator = operators.length > 0 ? operators[0].name : ''; const operator = operators.length > 0 ? operators[0].name : '';
const operatorDetails = this._getOperatorDetails(type, operator); const operatorDetails = this._getOperatorDetails(type, operator);
const {defaultValue} = operatorDetails;
this._updateSelect(this._operatorInput, this._operatorOptionContainer, operators, operator); this._updateSelect(this._operatorInput, this._operatorOptionContainer, operators, operator);
this._updateValueInput(operatorDetails.defaultValue, operatorDetails); this._updateValueInput(defaultValue, operatorDetails);
this.settingsController.setGlobalSetting(this.getPath('type'), type); this.settingsController.modifyGlobalSettings([
{action: 'set', path: this.getPath('type'), value: type},
{action: 'set', path: this.getPath('operator'), value: operator},
{action: 'set', path: this.getPath('value'), value: defaultValue}
]);
} }
_onOperatorChange(e) { _onOperatorChange(e) {
const type = this._typeInput.value; const type = this._typeInput.value;
const operator = e.currentTarget.value; const operator = e.currentTarget.value;
const operatorDetails = this._getOperatorDetails(type, operator); const operatorDetails = this._getOperatorDetails(type, operator);
const settingsModifications = [{action: 'set', path: this.getPath('operator'), value: operator}];
if (operatorDetails.resetDefaultOnChange) { if (operatorDetails.resetDefaultOnChange) {
const okay = this._updateValueInput(operatorDetails.defaultValue, operatorDetails); const {defaultValue} = operatorDetails;
const okay = this._updateValueInput(defaultValue, operatorDetails);
if (okay) { if (okay) {
this.settingsController.setGlobalSetting(this.getPath('operator'), operator); settingsModifications.push({action: 'set', path: this.getPath('value'), value: defaultValue});
} }
} }
this.settingsController.modifyGlobalSettings(settingsModifications);
} }
_onValueInputChange({validate, normalize}, e) { _onValueInputChange({validate, normalize}, e) {
@ -560,7 +568,6 @@ class ProfileConditionUI {
this._value = value; this._value = value;
if (okay) { if (okay) {
const normalizedValue = this._normalizeValue(value, normalize); const normalizedValue = this._normalizeValue(value, normalize);
node.value = normalizedValue;
this.settingsController.setGlobalSetting(this.getPath('value'), normalizedValue); this.settingsController.setGlobalSetting(this.getPath('value'), normalizedValue);
} }
} }
@ -617,40 +624,45 @@ class ProfileConditionUI {
_updateValueInput(value, {type, validate, normalize}) { _updateValueInput(value, {type, validate, normalize}) {
this._inputEventListeners.removeAllEventListeners(); this._inputEventListeners.removeAllEventListeners();
let inputType = 'text';
let inputValue = value;
let inputStep = null;
const events = [];
const inputData = {validate, normalize}; const inputData = {validate, normalize};
const node = this._valueInput; const node = this._valueInput;
node.classList.remove('is-invalid');
this._value = value;
switch (type) { switch (type) {
case 'integer': case 'integer':
{ inputType = 'number';
node.type = 'number'; inputStep = '1';
node.step = '1'; events.push([node, 'change', this._onValueInputChange.bind(this, inputData), false]);
node.value = value;
this._inputEventListeners.addEventListener(node, 'change', this._onValueInputChange.bind(this, inputData), false);
}
break; break;
case 'modifierKeys': case 'modifierKeys':
{ {
const modifiers = this._splitValue(value); const modifiers = this._splitValue(value);
const {displayValue} = this._getModifierKeyStrings(modifiers); const {displayValue} = this._getModifierKeyStrings(modifiers);
node.type = 'text'; inputValue = displayValue;
node.removeAttribute('step'); events.push([node, 'keydown', this._onModifierKeyDown.bind(this, inputData), false]);
node.value = displayValue;
this._inputEventListeners.addEventListener(node, 'keydown', this._onModifierKeyDown.bind(this, inputData), false);
} }
break; break;
default: // 'string' default: // 'string'
{ events.push([node, 'change', this._onValueInputChange.bind(this, inputData), false]);
node.type = 'text';
node.removeAttribute('step');
node.value = value;
this._inputEventListeners.addEventListener(node, 'change', this._onValueInputChange.bind(this, inputData), false);
}
break; break;
} }
this._value = value;
node.classList.remove('is-invalid');
node.type = inputType;
node.value = inputValue;
if (typeof inputStep === 'string') {
node.step = inputStep;
} else {
node.removeAttribute('step');
}
for (const args of events) {
this._inputEventListeners.addEventListener(...args);
}
this._validateValue(value, validate); this._validateValue(value, validate);
} }