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

View File

@ -7,27 +7,12 @@ module.exports = function(RED) {
var useColors = RED.settings.debugUseColors || false; var useColors = RED.settings.debugUseColors || false;
util.inspect.styles.boolean = "red"; 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) { function DebugNode(n) {
var is_edit = (n.targetType === "jsonata");
var edit_exp = is_edit ? n.complete : null;
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.name = n.name; 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"; } if (this.complete === "false") { this.complete = "payload"; }
this.console = ""+(n.console || false); this.console = ""+(n.console || false);
this.tostatus = n.tostatus || false; this.tostatus = n.tostatus || false;
@ -60,8 +45,7 @@ module.exports = function(RED) {
"60": "blue" "60": "blue"
}; };
var edit = null; var edit = null;
if (n.editType === "jsonata") { if (edit_exp) {
var edit_exp = n.edit;
try { try {
edit = RED.util.prepareJSONataExpression(edit_exp, this); edit = RED.util.prepareJSONataExpression(edit_exp, this);
} }
@ -71,9 +55,24 @@ module.exports = function(RED) {
} }
} }
this.on("input",function(msg) { function editValue(exp, val) {
if (this.complete === "true") { return new Promise((resolve, reject) => {
// debug complete msg object 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 output_e(msg) {
editValue(edit, msg).then(val => { editValue(edit, msg).then(val => {
if (this.console === "true") { if (this.console === "true") {
node.log("\n"+util.inspect(val, {colors:useColors, depth:10})); node.log("\n"+util.inspect(val, {colors:useColors, depth:10}));
@ -81,8 +80,21 @@ module.exports = function(RED) {
if (this.active && this.tosidebar) { if (this.active && this.tosidebar) {
sendDebug({id:node.id, name:node.name, topic:val.topic, msg:val, _path:val._path}); 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
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 { else {
// debug user defined msg property // debug user defined msg property
var property = "payload"; var property = "payload";
@ -95,30 +107,30 @@ module.exports = function(RED) {
output = undefined; output = undefined;
} }
} }
editValue(edit, output).then(val => {
if (this.console === "true") { if (this.console === "true") {
if (typeof val === "string") { if (typeof output === "string") {
node.log((val.indexOf("\n") !== -1 ? "\n" : "") + val); node.log((output.indexOf("\n") !== -1 ? "\n" : "") + output);
} else if (typeof val === "object") { } else if (typeof output === "object") {
node.log("\n"+util.inspect(val, {colors:useColors, depth:10})); node.log("\n"+util.inspect(output, {colors:useColors, depth:10}));
} else { } else {
node.log(util.inspect(val, {colors:useColors})); node.log(util.inspect(output, {colors:useColors}));
} }
} }
if (this.tostatus === true) { if (this.tostatus === true) {
var st = util.inspect(val); var st = util.inspect(output);
var severity = node.severity; var severity = node.severity;
if (st.length > 32) { st = st.substr(0,32) + "..."; } if (st.length > 32) { st = st.substr(0,32) + "..."; }
node.status({fill:colors[severity], shape:"dot", text:st}); node.status({fill:colors[severity], shape:"dot", text:st});
} }
if (this.active) { if (this.active) {
if (this.tosidebar == true) { if (this.tosidebar == true) {
sendDebug({id:node.id, z:node.z, name:node.name, topic:msg.topic, property:property, msg:val, _path:msg._path}); 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, { 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) { it('should publish complete message with edit', function(done) {
var flow = [{id:"n1", type:"debug", name:"Debug", complete: "true", var flow = [{id:"n1", type:"debug", name:"Debug", complete: "true",
editType: "jsonata", edit: '"<" & payload & ">"'}]; targetType: "jsonata", complete: '"<" & payload & ">"'}];
helper.load(debugNode, flow, function() { helper.load(debugNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
websocket_test(function() { websocket_test(function() {