Do better remembering TypedInput values whilst switching types

This commit is contained in:
Nick O'Leary
2021-09-30 18:14:45 +01:00
parent ec27e19e3f
commit ac84b6fe3f
2 changed files with 30 additions and 9 deletions

View File

@@ -428,6 +428,10 @@
this.optionExpandButton = $('<button tabindex="0" class="red-ui-typedInput-option-expand" style="display:inline-block"></button>').appendTo(this.uiSelect);
this.optionExpandButtonIcon = $('<i class="red-ui-typedInput-icon fa fa-ellipsis-h"></i>').appendTo(this.optionExpandButton);
// Used to remember selections per-type to restore them when switching between types
this.oldValues = {};
this.type(this.options.default||this.typeList[0].value);
}catch(err) {
console.log(err.stack);
@@ -767,6 +771,24 @@
var that = this;
var opt = this.typeMap[type];
if (opt && this.propertyType !== type) {
// If previousType is !null, then this is a change of the type, rather than the initialisation
var previousType = this.typeMap[this.propertyType];
var typeChanged = !!previousType;
if (typeChanged) {
if (previousType.options) {
this.oldValues[previousType.value] = this.input.val();
} else if (previousType.hasValue === false) {
this.oldValues[previousType.value] = this.input.val();
} else {
this.oldValues["_"] = this.input.val();
}
if (opt.options || opt.hasValue === false) {
this.input.val(this.oldValues.hasOwnProperty(opt.value)?this.oldValues[opt.value]:(opt.default||[]).join(","))
} else {
this.input.val(this.oldValues.hasOwnProperty("_")?this.oldValues["_"]:(opt.default||""))
}
}
this.propertyType = type;
if (this.typeField) {
this.typeField.val(type);
@@ -865,8 +887,10 @@
// Check to see if value is a valid csv of
// options.
var currentValues = {};
var selected = [];
currentVal.split(",").forEach(function(v) {
if (v) {
selected.push(v);
currentValues[v] = true;
}
});
@@ -875,9 +899,11 @@
delete currentValues[op.value||op];
}
if (!$.isEmptyObject(currentValues)) {
selected = opt.default || [];
// Invalid, set to default/empty
this.value((opt.default||[]).join(","));
this.value(selected.join(","));
}
that._updateOptionSelectLabel(selected);
}
} else {
var selectedOption = this.optionValue||opt.options[0];
@@ -938,8 +964,6 @@
this.input.attr('type',this.defaultInputType)
}
if (opt.hasValue === false) {
this.oldValue = this.input.val();
this.input.val("");
this.elementDiv.hide();
this.valueLabelContainer.hide();
} else if (opt.valueLabel) {
@@ -952,10 +976,6 @@
this.elementDiv.hide();
opt.valueLabel.call(this,this.valueLabelContainer,this.input.val());
} else {
if (this.oldValue !== undefined) {
this.input.val(this.oldValue);
delete this.oldValue;
}
this.valueLabelContainer.hide();
this.elementDiv.show();
}