mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Handle changing value before type without reseting TypedInput
This commit is contained in:
parent
d0ec055222
commit
0cc061196d
@ -345,6 +345,47 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// For a type with options, check value is a valid selection
|
||||||
|
// If !opt.multiple, returns the valid option object
|
||||||
|
// if opt.multiple, returns an array of valid option objects
|
||||||
|
// If not valid, returns null;
|
||||||
|
|
||||||
|
function isOptionValueValid(opt, currentVal) {
|
||||||
|
if (!opt.multiple) {
|
||||||
|
for (var i=0;i<opt.options.length;i++) {
|
||||||
|
op = opt.options[i];
|
||||||
|
if (typeof op === "string" && op === currentVal) {
|
||||||
|
return {value:currentVal}
|
||||||
|
} else if (op.value === currentVal) {
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Check to see if value is a valid csv of
|
||||||
|
// options.
|
||||||
|
var currentValues = {};
|
||||||
|
var selected = [];
|
||||||
|
currentVal.split(",").forEach(function(v) {
|
||||||
|
if (v) {
|
||||||
|
currentValues[v] = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
for (var i=0;i<opt.options.length;i++) {
|
||||||
|
op = opt.options[i];
|
||||||
|
var val = typeof op === "string" ? op : op.value;
|
||||||
|
if (currentValues.hasOwnProperty(val)) {
|
||||||
|
delete currentValues[val];
|
||||||
|
selected.push(typeof op === "string" ? {value:op} : op.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!$.isEmptyObject(currentValues)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return selected
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var nlsd = false;
|
var nlsd = false;
|
||||||
|
|
||||||
$.widget( "nodered.typedInput", {
|
$.widget( "nodered.typedInput", {
|
||||||
@ -408,6 +449,8 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.defaultInputType = this.input.attr('type');
|
this.defaultInputType = this.input.attr('type');
|
||||||
|
// Used to remember selections per-type to restore them when switching between types
|
||||||
|
this.oldValues = {};
|
||||||
|
|
||||||
this.uiSelect.addClass("red-ui-typedInput-container");
|
this.uiSelect.addClass("red-ui-typedInput-container");
|
||||||
|
|
||||||
@ -512,9 +555,6 @@
|
|||||||
this.optionExpandButton = $('<button tabindex="0" class="red-ui-typedInput-option-expand" style="display:inline-block"></button>').appendTo(this.uiSelect);
|
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);
|
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);
|
this.type(this.options.default||this.typeList[0].value);
|
||||||
}catch(err) {
|
}catch(err) {
|
||||||
console.log(err.stack);
|
console.log(err.stack);
|
||||||
@ -859,22 +899,44 @@
|
|||||||
return this.propertyType;
|
return this.propertyType;
|
||||||
} else {
|
} else {
|
||||||
var that = this;
|
var that = this;
|
||||||
|
var previousValue = null;
|
||||||
var opt = this.typeMap[type];
|
var opt = this.typeMap[type];
|
||||||
if (opt && this.propertyType !== type) {
|
if (opt && this.propertyType !== type) {
|
||||||
// If previousType is !null, then this is a change of the type, rather than the initialisation
|
// If previousType is !null, then this is a change of the type, rather than the initialisation
|
||||||
var previousType = this.typeMap[this.propertyType];
|
var previousType = this.typeMap[this.propertyType];
|
||||||
var typeChanged = !!previousType;
|
var typeChanged = !!previousType;
|
||||||
|
previousValue = this.input.val();
|
||||||
|
|
||||||
if (typeChanged) {
|
if (typeChanged) {
|
||||||
if (previousType.options && opt.hasValue !== true) {
|
if (previousType.options && opt.hasValue !== true) {
|
||||||
this.oldValues[previousType.value] = this.input.val();
|
this.oldValues[previousType.value] = previousValue;
|
||||||
} else if (previousType.hasValue === false) {
|
} else if (previousType.hasValue === false) {
|
||||||
this.oldValues[previousType.value] = this.input.val();
|
this.oldValues[previousType.value] = previousValue;
|
||||||
} else {
|
} else {
|
||||||
this.oldValues["_"] = this.input.val();
|
this.oldValues["_"] = previousValue;
|
||||||
}
|
}
|
||||||
if ((opt.options && opt.hasValue !== true) || opt.hasValue === false) {
|
if ((opt.options && opt.hasValue !== true) || opt.hasValue === false) {
|
||||||
this.input.val(this.oldValues.hasOwnProperty(opt.value)?this.oldValues[opt.value]:(opt.default||[]).join(","))
|
if (this.oldValues.hasOwnProperty(opt.value)) {
|
||||||
|
this.input.val(this.oldValues[opt.value]);
|
||||||
|
} else {
|
||||||
|
// No old value for the option type.
|
||||||
|
// It is possible code has called 'value' then 'type'
|
||||||
|
// to set the selected option. This is what the Inject/Switch/Change
|
||||||
|
// nodes did before 2.1.
|
||||||
|
// So we need to be careful to not reset the value if it is a valid option.
|
||||||
|
var validOptions = isOptionValueValid(opt,previousValue);
|
||||||
|
if (previousValue && validOptions) {
|
||||||
|
this.input.val(previousValue);
|
||||||
|
} else {
|
||||||
|
if (typeof opt.default === "string") {
|
||||||
|
this.input.val(opt.default);
|
||||||
|
} else if (Array.isArray(opt.default)) {
|
||||||
|
this.input.val(opt.default.join(","))
|
||||||
|
} else {
|
||||||
|
this.input.val("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.input.val(this.oldValues.hasOwnProperty("_")?this.oldValues["_"]:(opt.default||""))
|
this.input.val(this.oldValues.hasOwnProperty("_")?this.oldValues["_"]:(opt.default||""))
|
||||||
}
|
}
|
||||||
@ -951,22 +1013,12 @@
|
|||||||
|
|
||||||
var op;
|
var op;
|
||||||
if (!opt.hasValue) {
|
if (!opt.hasValue) {
|
||||||
var validValue = false;
|
// Check the value is valid for the available options
|
||||||
var currentVal = this.input.val();
|
var validValues = isOptionValueValid(opt,this.input.val());
|
||||||
if (!opt.multiple) {
|
if (!opt.multiple) {
|
||||||
for (var i=0;i<opt.options.length;i++) {
|
if (validValues) {
|
||||||
op = opt.options[i];
|
that._updateOptionSelectLabel(validValues)
|
||||||
if (typeof op === "string" && op === currentVal) {
|
} else {
|
||||||
that._updateOptionSelectLabel({value:currentVal});
|
|
||||||
validValue = true;
|
|
||||||
break;
|
|
||||||
} else if (op.value === currentVal) {
|
|
||||||
that._updateOptionSelectLabel(op);
|
|
||||||
validValue = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!validValue) {
|
|
||||||
op = opt.options[0];
|
op = opt.options[0];
|
||||||
if (typeof op === "string") {
|
if (typeof op === "string") {
|
||||||
this.value(op);
|
this.value(op);
|
||||||
@ -977,26 +1029,13 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Check to see if value is a valid csv of
|
if (!validValues) {
|
||||||
// options.
|
validValues = (opt.default || []).map(function(v) {
|
||||||
var currentValues = {};
|
return typeof v === "string"?v:v.value
|
||||||
var selected = [];
|
|
||||||
currentVal.split(",").forEach(function(v) {
|
|
||||||
if (v) {
|
|
||||||
selected.push(v);
|
|
||||||
currentValues[v] = true;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
for (var i=0;i<opt.options.length;i++) {
|
this.value(validValues.join(","));
|
||||||
op = opt.options[i];
|
|
||||||
delete currentValues[op.value||op];
|
|
||||||
}
|
}
|
||||||
if (!$.isEmptyObject(currentValues)) {
|
that._updateOptionSelectLabel(validValues);
|
||||||
selected = opt.default || [];
|
|
||||||
// Invalid, set to default/empty
|
|
||||||
this.value(selected.join(","));
|
|
||||||
}
|
|
||||||
that._updateOptionSelectLabel(selected);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var selectedOption = this.optionValue||opt.options[0];
|
var selectedOption = this.optionValue||opt.options[0];
|
||||||
|
Loading…
Reference in New Issue
Block a user