Allow RED.settings.get/set to use full property desc

This commit is contained in:
Nick O'Leary
2019-05-21 17:19:12 +01:00
parent 5cb888328e
commit afa25df1af
2 changed files with 69 additions and 3 deletions

View File

@@ -722,6 +722,62 @@ RED.utils = (function() {
return result;
}
function setMessageProperty(msg,prop,value,createMissing) {
if (typeof createMissing === 'undefined') {
createMissing = (typeof value !== 'undefined');
}
if (prop.indexOf('msg.')===0) {
prop = prop.substring(4);
}
var msgPropParts = normalisePropertyExpression(prop);
var depth = 0;
var length = msgPropParts.length;
var obj = msg;
var key;
for (var i=0;i<length-1;i++) {
key = msgPropParts[i];
if (typeof key === 'string' || (typeof key === 'number' && !Array.isArray(obj))) {
if (obj.hasOwnProperty(key)) {
obj = obj[key];
} else if (createMissing) {
if (typeof msgPropParts[i+1] === 'string') {
obj[key] = {};
} else {
obj[key] = [];
}
obj = obj[key];
} else {
return null;
}
} else if (typeof key === 'number') {
// obj is an array
if (obj[key] === undefined) {
if (createMissing) {
if (typeof msgPropParts[i+1] === 'string') {
obj[key] = {};
} else {
obj[key] = [];
}
obj = obj[key];
} else {
return null;
}
} else {
obj = obj[key];
}
}
}
key = msgPropParts[length-1];
if (typeof value === "undefined") {
if (typeof key === 'number' && Array.isArray(obj)) {
obj.splice(key,1);
} else {
delete obj[key]
}
} else {
obj[key] = value;
}
}
function separateIconPath(icon) {
var result = {module: "", file: ""};
if (icon) {
@@ -957,6 +1013,7 @@ RED.utils = (function() {
return {
createObjectElement: buildMessageElement,
getMessageProperty: getMessageProperty,
setMessageProperty: setMessageProperty,
normalisePropertyExpression: normalisePropertyExpression,
validatePropertyExpression: validatePropertyExpression,
separateIconPath: separateIconPath,