mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Allow a node to declare what settings should be made available to the editor. (#1185)
* Implement register/exportNodeSettings. * Change normaliseRegisterTypeName to normaliseNodeTypeName. Force it to name in a camel case.
This commit is contained in:
committed by
Nick O'Leary
parent
fca77a868f
commit
34089aec70
@@ -493,6 +493,7 @@ function enableNodeSet(typeOrId) {
|
||||
delete config.err;
|
||||
config.enabled = true;
|
||||
nodeConfigCache = null;
|
||||
settings.enableNodeSettings(config.types);
|
||||
return saveNodeList().then(function() {
|
||||
return filterNodeInfo(config);
|
||||
});
|
||||
@@ -515,6 +516,7 @@ function disableNodeSet(typeOrId) {
|
||||
// TODO: persist setting
|
||||
config.enabled = false;
|
||||
nodeConfigCache = null;
|
||||
settings.disableNodeSettings(config.types);
|
||||
return saveNodeList().then(function() {
|
||||
return filterNodeInfo(config);
|
||||
});
|
||||
|
@@ -18,10 +18,12 @@ var when = require("when");
|
||||
var clone = require("clone");
|
||||
var assert = require("assert");
|
||||
var log = require("./log");
|
||||
var util = require("./util");
|
||||
|
||||
var userSettings = null;
|
||||
var globalSettings = null;
|
||||
var nodeSettings = null;
|
||||
var disableNodeSettings = null;
|
||||
var storage = null;
|
||||
|
||||
var persistentSettings = {
|
||||
@@ -40,6 +42,7 @@ var persistentSettings = {
|
||||
}
|
||||
globalSettings = null;
|
||||
nodeSettings = {};
|
||||
disableNodeSettings = {};
|
||||
},
|
||||
load: function(_storage) {
|
||||
storage = _storage;
|
||||
@@ -103,24 +106,49 @@ var persistentSettings = {
|
||||
storage = null;
|
||||
},
|
||||
registerNodeSettings: function(type, opts) {
|
||||
//console.log(type,opts);
|
||||
// 1. TODO: validate the option names are allowed for the node type
|
||||
|
||||
// 2. store this information against the node type
|
||||
nodeSettings[type] = opts;
|
||||
|
||||
|
||||
// TODO: remove the node settings if the node is disabled/removed from runtime
|
||||
try {
|
||||
for (var property in opts) {
|
||||
if (opts.hasOwnProperty(property)) {
|
||||
var normalisedType = util.normaliseNodeTypeName(type);
|
||||
if (!property.startsWith(normalisedType)) {
|
||||
throw new Error("The name of node setting property " + property + " must start with \"" + normalisedType + "\" (case sensitive).");
|
||||
}
|
||||
}
|
||||
}
|
||||
nodeSettings[type] = opts;
|
||||
} catch (err) {
|
||||
console.log(err.toString());
|
||||
}
|
||||
},
|
||||
exportNodeSettings: function(safeSettings) {
|
||||
// 1. forEach type in nodeSettings...
|
||||
// 2. forEach setting for that type...
|
||||
// 3. if globalSettings has a property with the required name...
|
||||
// 4. set safeSettings.property to that value
|
||||
// 5. else if the setting has a default 'value' provided
|
||||
// 6. set safeSettings.property to that value
|
||||
safeSettings["nodeSettings"] = {};
|
||||
for (var type in nodeSettings) {
|
||||
if (nodeSettings.hasOwnProperty(type) && !disableNodeSettings[type]) {
|
||||
var nodeTypeSettings = nodeSettings[type];
|
||||
for (var property in nodeTypeSettings) {
|
||||
if (nodeTypeSettings.hasOwnProperty(property)) {
|
||||
var setting = nodeTypeSettings[property];
|
||||
if (userSettings.hasOwnProperty(property)) {
|
||||
safeSettings["nodeSettings"][property] = userSettings[property];
|
||||
} else if (setting.exportable) {
|
||||
safeSettings["nodeSettings"][property] = setting.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return safeSettings;
|
||||
},
|
||||
enableNodeSettings: function(types) {
|
||||
types.forEach(function(type) {
|
||||
disableNodeSettings[type] = false;
|
||||
});
|
||||
},
|
||||
disableNodeSettings: function(types) {
|
||||
types.forEach(function(type) {
|
||||
disableNodeSettings[type] = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -328,6 +328,18 @@ function evaluateNodeProperty(value, type, node, msg) {
|
||||
return value;
|
||||
}
|
||||
|
||||
function normaliseNodeTypeName(name) {
|
||||
var result = name.replace(/[^a-zA-Z0-9]/g, " ");
|
||||
result = result.trim();
|
||||
result = result.replace(/ +/g, " ");
|
||||
result = result.replace(/ ./g,
|
||||
function(s) {
|
||||
return s.charAt(1).toUpperCase();
|
||||
}
|
||||
);
|
||||
result = result.charAt(0).toLowerCase() + result.slice(1);
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ensureString: ensureString,
|
||||
@@ -338,5 +350,6 @@ module.exports = {
|
||||
getMessageProperty: getMessageProperty,
|
||||
setMessageProperty: setMessageProperty,
|
||||
evaluateNodeProperty: evaluateNodeProperty,
|
||||
normalisePropertyExpression: normalisePropertyExpression
|
||||
normalisePropertyExpression: normalisePropertyExpression,
|
||||
normaliseNodeTypeName: normaliseNodeTypeName
|
||||
};
|
||||
|
Reference in New Issue
Block a user