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

Allow debug edit expression to be sent to status

This commit is contained in:
Nick O'Leary 2019-01-17 17:15:53 +00:00
parent 5b2f24f842
commit 1359545e13
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 68 additions and 77 deletions

View File

@ -293,16 +293,18 @@
) {
$("#node-input-typed-complete").typedInput('value','payload');
}
if ($("#node-input-typed-complete").typedInput('type') === 'msg') {
if ($("#node-input-typed-complete").typedInput('type') === 'full') {
$("#node-tostatus-line").hide();
} else {
$("#node-tostatus-line").show();
}
else { $("#node-tostatus-line").hide(); }
});
$("#node-input-complete").on('change',function() {
if ($("#node-input-typed-complete").typedInput('type') === 'msg') {
if ($("#node-input-typed-complete").typedInput('type') === 'full') {
$("#node-tostatus-line").hide();
} else {
$("#node-tostatus-line").show();
}
else { $("#node-tostatus-line").hide(); }
});
},
oneditsave: function() {

View File

@ -8,14 +8,14 @@ module.exports = function(RED) {
util.inspect.styles.boolean = "red";
function DebugNode(n) {
var is_edit = (n.targetType === "jsonata");
var edit_exp = is_edit ? n.complete : null;
var hasEditExpression = (n.targetType === "jsonata");
var editExpression = hasEditExpression ? n.complete : null;
RED.nodes.createNode(this,n);
this.name = n.name;
this.complete = is_edit ? null : (n.complete||"payload").toString();
this.complete = hasEditExpression ? null : (n.complete||"payload").toString();
if (this.complete === "false") { this.complete = "payload"; }
this.console = ""+(n.console || false);
this.tostatus = n.tostatus || false;
this.tostatus = (this.complete !== "true") && (n.tostatus || false);
this.tosidebar = n.tosidebar;
if (this.tosidebar === undefined) { this.tosidebar = true; }
this.severity = n.severity || 40;
@ -44,48 +44,44 @@ module.exports = function(RED) {
"50": "green",
"60": "blue"
};
var edit = null;
if (edit_exp) {
var preparedEditExpression = null;
if (editExpression) {
try {
edit = RED.util.prepareJSONataExpression(edit_exp, this);
preparedEditExpression = RED.util.prepareJSONataExpression(editExpression, this);
}
catch (e) {
node.error(RED._("debug.invalid-exp", {error: edit_exp}));
node.error(RED._("debug.invalid-exp", {error: editExpression}));
return;
}
}
function editValue(exp, val) {
return new Promise((resolve, reject) => {
if (exp) {
RED.util.evaluateJSONataExpression(exp, val, (err, value) => {
if (err) {
reject(RED._("debug.invalid-exp", {error: edit_exp}));
} else {
resolve(value);
}
});
function prepareValue(msg, done) {
// Either apply the jsonata expression or...
if (preparedEditExpression) {
RED.util.evaluateJSONataExpression(preparedEditExpression, msg, (err, value) => {
if (err) {
done(RED._("debug.invalid-exp", {error: editExpression}));
} else {
done(null,{id:node.id, name:node.name, topic:msg.topic, msg:value, _path:msg._path});
}
});
} else {
// Extract the required message property
var property = "payload";
var output = msg[property];
if (node.complete !== "false" && typeof node.complete !== "undefined") {
property = node.complete;
try {
output = RED.util.getMessageProperty(msg,node.complete);
} catch(err) {
output = undefined;
}
}
else {
resolve(val);
}
});
done(null,{id:node.id, z:node.z, name:node.name, topic:msg.topic, property:property, msg:output, _path:msg._path});
}
}
function output_e(msg) {
editValue(edit, msg).then(val => {
if (this.console === "true") {
node.log("\n"+util.inspect(val, {colors:useColors, depth:10}));
}
if (this.active && this.tosidebar) {
sendDebug({id:node.id, name:node.name, topic:val.topic, msg:val, _path:val._path});
}
}).catch(err => {
node.error(err);
});
}
function output(msg) {
this.on("input", function(msg) {
if (this.complete === "true") {
// debug complete msg object
if (this.console === "true") {
@ -94,43 +90,36 @@ module.exports = function(RED) {
if (this.active && this.tosidebar) {
sendDebug({id:node.id, name:node.name, topic:msg.topic, msg:msg, _path:msg._path});
}
} else {
prepareValue(msg,function(err,msg) {
if (err) {
node.error(err);
return;
}
var output = msg.msg;
if (node.console === "true") {
if (typeof output === "string") {
node.log((output.indexOf("\n") !== -1 ? "\n" : "") + output);
} else if (typeof output === "object") {
node.log("\n"+util.inspect(output, {colors:useColors, depth:10}));
} else {
node.log(util.inspect(output, {colors:useColors}));
}
}
if (node.tostatus === true) {
var st = (typeof output === 'string')?output:util.inspect(output);
var severity = node.severity;
if (st.length > 32) { st = st.substr(0,32) + "..."; }
node.status({fill:colors[severity], shape:"dot", text:st});
}
if (node.active) {
if (node.tosidebar == true) {
sendDebug(msg);
}
}
});
}
else {
// debug user defined msg property
var property = "payload";
var output = msg[property];
if (this.complete !== "false" && typeof this.complete !== "undefined") {
property = this.complete;
try {
output = RED.util.getMessageProperty(msg,this.complete);
} catch(err) {
output = undefined;
}
}
if (this.console === "true") {
if (typeof output === "string") {
node.log((output.indexOf("\n") !== -1 ? "\n" : "") + output);
} else if (typeof output === "object") {
node.log("\n"+util.inspect(output, {colors:useColors, depth:10}));
} else {
node.log(util.inspect(output, {colors:useColors}));
}
}
if (this.tostatus === true) {
var st = util.inspect(output);
var severity = node.severity;
if (st.length > 32) { st = st.substr(0,32) + "..."; }
node.status({fill:colors[severity], shape:"dot", text:st});
}
if (this.active) {
if (this.tosidebar == true) {
sendDebug({id:node.id, z:node.z, name:node.name, topic:msg.topic, property:property, msg:output, _path:msg._path});
}
}
}
}
this.on("input", (edit_exp ? output_e : output));
})
}
RED.nodes.registerType("debug",DebugNode, {