1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

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
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 69 additions and 3 deletions

View File

@ -37,7 +37,7 @@ RED.settings = (function () {
if (key === "auth-tokens") { if (key === "auth-tokens") {
localStorage.setItem(key, JSON.stringify(value)); localStorage.setItem(key, JSON.stringify(value));
} else { } else {
userSettings[key] = value; RED.utils.setMessageProperty(userSettings,key,value);
saveUserSettings(); saveUserSettings();
} }
}; };
@ -46,16 +46,25 @@ RED.settings = (function () {
* If the key is not set in the localStorage it returns <i>undefined</i> * If the key is not set in the localStorage it returns <i>undefined</i>
* Else return the JSON parsed value * Else return the JSON parsed value
* @param key * @param key
* @param defaultIfUndefined
* @returns {*} * @returns {*}
*/ */
var get = function (key) { var get = function (key,defaultIfUndefined) {
if (!hasLocalStorage()) { if (!hasLocalStorage()) {
return undefined; return undefined;
} }
if (key === "auth-tokens") { if (key === "auth-tokens") {
return JSON.parse(localStorage.getItem(key)); return JSON.parse(localStorage.getItem(key));
} else { } else {
return userSettings[key]; try {
var v = RED.utils.getMessageProperty(userSettings,key);
if (v === undefined) {
v = defaultIfUndefined;
}
} catch(err) {
v = defaultIfUndefined;
}
return v;
} }
}; };

View File

@ -722,6 +722,62 @@ RED.utils = (function() {
return result; 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) { function separateIconPath(icon) {
var result = {module: "", file: ""}; var result = {module: "", file: ""};
if (icon) { if (icon) {
@ -957,6 +1013,7 @@ RED.utils = (function() {
return { return {
createObjectElement: buildMessageElement, createObjectElement: buildMessageElement,
getMessageProperty: getMessageProperty, getMessageProperty: getMessageProperty,
setMessageProperty: setMessageProperty,
normalisePropertyExpression: normalisePropertyExpression, normalisePropertyExpression: normalisePropertyExpression,
validatePropertyExpression: validatePropertyExpression, validatePropertyExpression: validatePropertyExpression,
separateIconPath: separateIconPath, separateIconPath: separateIconPath,