Update YAML node to support Property In/Out

This commit is contained in:
Steve-Mcl 2024-04-12 11:01:56 +01:00
parent b67c9e1bdb
commit 2621a3a628
2 changed files with 18 additions and 6 deletions

View File

@ -1,12 +1,16 @@
<script type="text/html" data-template-name="yaml"> <script type="text/html" data-template-name="yaml">
<div class="form-row"> <div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="common.label.property"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
<div class="form-row">
<label for="node-input-property"><i class="fa fa-sign-in"></i> <span data-i18n="common.label.propertyIn"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/> <input type="text" id="node-input-property" style="width:70%;"/>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <label for="node-input-propertyOut"><i class="fa fa-sign-out"></i> <span data-i18n="common.label.propertyOut"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name"> <input type="text" id="node-input-propertyOut" style="width:70%;"/>
</div> </div>
</script> </script>
@ -17,7 +21,10 @@
defaults: { defaults: {
property: {value:"payload",required:true, property: {value:"payload",required:true,
validate: RED.validators.typedInput({ type: 'msg', allowUndefined: true }), validate: RED.validators.typedInput({ type: 'msg', allowUndefined: true }),
label:RED._("node-red:common.label.property")}, label:RED._("node-red:common.label.propertyIn")},
propertyOut: {value:"payload",required:true,
validate: RED.validators.typedInput({ type: 'msg', allowUndefined: true}),
label:RED._("node-red:common.label.propertyOut")},
name: {value:""} name: {value:""}
}, },
inputs:1, inputs:1,
@ -34,6 +41,10 @@
$("#node-input-property").val("payload"); $("#node-input-property").val("payload");
} }
$("#node-input-property").typedInput({default:'msg',types:['msg']}); $("#node-input-property").typedInput({default:'msg',types:['msg']});
if (this.propertyOut === undefined) {
$("#node-input-propertyOut").val("payload");
}
$("#node-input-propertyOut").typedInput({default:'msg',types:['msg']});
} }
}); });
</script> </script>

View File

@ -5,6 +5,7 @@ module.exports = function(RED) {
function YAMLNode(n) { function YAMLNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.property = n.property||"payload"; this.property = n.property||"payload";
this.propertyOut = n.propertyOut||this.property;
var node = this; var node = this;
this.on("input", function(msg,send,done) { this.on("input", function(msg,send,done) {
var value = RED.util.getMessageProperty(msg,node.property); var value = RED.util.getMessageProperty(msg,node.property);
@ -12,7 +13,7 @@ module.exports = function(RED) {
if (typeof value === "string") { if (typeof value === "string") {
try { try {
value = yaml.load(value); value = yaml.load(value);
RED.util.setMessageProperty(msg,node.property,value); RED.util.setMessageProperty(msg,node.propertyOut,value);
send(msg); send(msg);
done(); done();
} }
@ -22,7 +23,7 @@ module.exports = function(RED) {
if (!Buffer.isBuffer(value)) { if (!Buffer.isBuffer(value)) {
try { try {
value = yaml.dump(value); value = yaml.dump(value);
RED.util.setMessageProperty(msg,node.property,value); RED.util.setMessageProperty(msg,node.propertyOut,value);
send(msg); send(msg);
done(); done();
} }