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

use output selector for specifying JSONata expression

This commit is contained in:
Hiroyasu Nishiyama 2018-12-09 20:30:35 +09:00
parent 987dbf8a92
commit 8198132ca7
3 changed files with 84 additions and 86 deletions

View File

@ -4,15 +4,9 @@
<label for="node-input-typed-complete"><i class="fa fa-list"></i> <span data-i18n="debug.output"></span></label>
<input id="node-input-typed-complete" type="text" style="width: 70%">
<input id="node-input-complete" type="hidden">
<input id="node-input-targetType" type="hidden">
</div>
<div class="form-row">
<label for="node-input-edit"><i class="fa fa-calculator"></i> <span data-i18n="debug.edit"></span></label>
<input type="text" id="node-input-edit" style="width:70%;">
<input type="hidden" id="node-input-editType">
</div>
<div class="form-row">
<label for="node-input-tosidebar"><i class="fa fa-random"></i> <span data-i18n="debug.to"></span></label>
<label for="node-input-tosidebar" style="width:70%">
@ -46,17 +40,19 @@
category: 'output',
defaults: {
name: {value:""},
edit: {value:""},
editType: {value:"none"},
active: {value:true},
tosidebar: {value:true},
console: {value:false},
tostatus: {value:false},
complete: {value:"false", required:true}
complete: {value:"false", required:true},
targetType: {value:undefined}
},
label: function() {
var suffix = "";
if (this.console === true || this.console === "true") { suffix = " ⇲"; }
if (this.targetType === "jsonata") {
return (this.name || "JSONata") + suffix;
}
if (this.complete === true || this.complete === "true") {
return (this.name||"msg") + suffix;
} else {
@ -260,13 +256,6 @@
label: RED._("node-red:debug.none"),
hasValue: false
};
$("#node-input-edit").typedInput({
default: 'none',
typeField: $("#node-input-editType"),
types:[none, 'jsonata']
});
$("#node-input-edit").change();
if (this.tosidebar === undefined) {
this.tosidebar = true;
$("#node-input-tosidebar").prop('checked', true);
@ -276,8 +265,21 @@
$("#node-input-console").prop('checked', this.console);
$("#node-input-tosidebar").prop('checked', true);
}
$("#node-input-typed-complete").typedInput({types:['msg', {value:"full",label:RED._("node-red:debug.msgobj"),hasValue:false}]});
if (this.complete === "true" || this.complete === true) {
var fullType = {
value: "full",
label: RED._("node-red:debug.msgobj"),
hasValue: false
};
$("#node-input-typed-complete").typedInput({
default: "msg",
types:['msg', fullType, "jsonata"],
typeField: $("#node-input-targetType")
});
if (this.targetType === "jsonata") {
var property = this.complete || "";
$("#node-input-typed-complete").typedInput('type','jsonata');
$("#node-input-typed-complete").typedInput('value',property);
} else if ((this.targetType === "full") || this.complete === "true" || this.complete === true) {
// show complete message object
$("#node-input-typed-complete").typedInput('type','full');
} else {

View File

@ -7,27 +7,12 @@ module.exports = function(RED) {
var useColors = RED.settings.debugUseColors || false;
util.inspect.styles.boolean = "red";
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);
}
});
}
else {
resolve(val);
}
});
}
function DebugNode(n) {
var is_edit = (n.targetType === "jsonata");
var edit_exp = is_edit ? n.complete : null;
RED.nodes.createNode(this,n);
this.name = n.name;
this.complete = (n.complete||"payload").toString();
this.complete = is_edit ? null : (n.complete||"payload").toString();
if (this.complete === "false") { this.complete = "payload"; }
this.console = ""+(n.console || false);
this.tostatus = n.tostatus || false;
@ -60,8 +45,7 @@ module.exports = function(RED) {
"60": "blue"
};
var edit = null;
if (n.editType === "jsonata") {
var edit_exp = n.edit;
if (edit_exp) {
try {
edit = RED.util.prepareJSONataExpression(edit_exp, this);
}
@ -70,18 +54,46 @@ module.exports = function(RED) {
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);
}
});
}
else {
resolve(val);
}
});
}
this.on("input",function(msg) {
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) {
if (this.complete === "true") {
// debug complete msg object
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});
}
});
if (this.console === "true") {
node.log("\n"+util.inspect(msg, {colors:useColors, depth:10}));
}
if (this.active && this.tosidebar) {
sendDebug({id:node.id, name:node.name, topic:msg.topic, msg:msg, _path:msg._path});
}
}
else {
// debug user defined msg property
@ -95,30 +107,30 @@ module.exports = function(RED) {
output = undefined;
}
}
editValue(edit, output).then(val => {
if (this.console === "true") {
if (typeof val === "string") {
node.log((val.indexOf("\n") !== -1 ? "\n" : "") + val);
} else if (typeof val === "object") {
node.log("\n"+util.inspect(val, {colors:useColors, depth:10}));
} else {
node.log(util.inspect(val, {colors:useColors}));
}
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(val);
var severity = node.severity;
if (st.length > 32) { st = st.substr(0,32) + "..."; }
node.status({fill:colors[severity], shape:"dot", text:st});
}
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});
}
if (this.active) {
if (this.tosidebar == true) {
sendDebug({id:node.id, z:node.z, name:node.name, topic:msg.topic, property:property, msg:val, _path:msg._path});
}
}
});
}
}
});
}
this.on("input", (edit_exp ? output_e : output));
}
RED.nodes.registerType("debug",DebugNode, {

View File

@ -356,25 +356,9 @@ describe('debug node', function() {
});
});
it('should publish payload with edit', function(done) {
var flow = [{id:"n1", type:"debug", name:"Debug",
editType: "jsonata", edit: '"<" & $ & ">"'}];
helper.load(debugNode, flow, function() {
var n1 = helper.getNode("n1");
websocket_test(function() {
n1.emit("input", {payload:"test"});
}, function(msg) {
JSON.parse(msg).should.eql([{
topic:"debug",data:{id:"n1",name:"Debug",msg:"<test>",
format:"string[6]",property:"payload"}
}]);
}, done);
});
});
it('should publish complete message with edit', function(done) {
var flow = [{id:"n1", type:"debug", name:"Debug", complete: "true",
editType: "jsonata", edit: '"<" & payload & ">"'}];
targetType: "jsonata", complete: '"<" & payload & ">"'}];
helper.load(debugNode, flow, function() {
var n1 = helper.getNode("n1");
websocket_test(function() {