From 1359545e13695bbd3052beabb5f6a11777d3d115 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 17 Jan 2019 17:15:53 +0000 Subject: [PATCH] Allow debug edit expression to be sent to status --- .../@node-red/nodes/core/core/58-debug.html | 10 +- .../@node-red/nodes/core/core/58-debug.js | 135 ++++++++---------- 2 files changed, 68 insertions(+), 77 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/core/58-debug.html b/packages/node_modules/@node-red/nodes/core/core/58-debug.html index a367bce0b..39d189ca4 100644 --- a/packages/node_modules/@node-red/nodes/core/core/58-debug.html +++ b/packages/node_modules/@node-red/nodes/core/core/58-debug.html @@ -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() { diff --git a/packages/node_modules/@node-red/nodes/core/core/58-debug.js b/packages/node_modules/@node-red/nodes/core/core/58-debug.js index 43b24fb39..1571605da 100644 --- a/packages/node_modules/@node-red/nodes/core/core/58-debug.js +++ b/packages/node_modules/@node-red/nodes/core/core/58-debug.js @@ -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, {