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

use typedInput for filename on file nodes

This commit is contained in:
Steve-Mcl 2022-04-14 23:55:57 +01:00
parent a7932da207
commit 6e35a9f682
2 changed files with 69 additions and 6 deletions

View File

@ -3,6 +3,7 @@
<div class="form-row node-input-filename"> <div class="form-row node-input-filename">
<label for="node-input-filename"><i class="fa fa-file"></i> <span data-i18n="file.label.filename"></span></label> <label for="node-input-filename"><i class="fa fa-file"></i> <span data-i18n="file.label.filename"></span></label>
<input id="node-input-filename" type="text"> <input id="node-input-filename" type="text">
<input type="hidden" id="node-input-filenameType">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-overwriteFile"><i class="fa fa-random"></i> <span data-i18n="file.label.action"></span></label> <label for="node-input-overwriteFile"><i class="fa fa-random"></i> <span data-i18n="file.label.action"></span></label>
@ -38,6 +39,7 @@
<div class="form-row"> <div class="form-row">
<label for="node-input-filename"><i class="fa fa-file"></i> <span data-i18n="file.label.filename"></span></label> <label for="node-input-filename"><i class="fa fa-file"></i> <span data-i18n="file.label.filename"></span></label>
<input id="node-input-filename" type="text" data-i18n="[placeholder]file.label.filename"> <input id="node-input-filename" type="text" data-i18n="[placeholder]file.label.filename">
<input type="hidden" id="node-input-filenameType">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-format"><i class="fa fa-sign-out"></i> <span data-i18n="file.label.outputas"></span></label> <label for="node-input-format"><i class="fa fa-sign-out"></i> <span data-i18n="file.label.outputas"></span></label>
@ -197,6 +199,7 @@
defaults: { defaults: {
name: {value:""}, name: {value:""},
filename: {value:""}, filename: {value:""},
filenameType: {value:""},
appendNewline: {value:true}, appendNewline: {value:true},
createDir: {value:false}, createDir: {value:false},
overwriteFile: {value:"false"}, overwriteFile: {value:"false"},
@ -207,10 +210,12 @@
outputs:1, outputs:1,
icon: "file-out.svg", icon: "file-out.svg",
label: function() { label: function() {
var fn = this.filename;
if(this.filenameType != "str") { fn = ""; }
if (this.overwriteFile === "delete") { if (this.overwriteFile === "delete") {
return this.name||this._("file.label.deletelabel",{file:this.filename}); return this.name||this._("file.label.deletelabel",{file:fn});
} else { } else {
return this.name||this.filename||this._("file.label.write"); return this.name||fn||this._("file.label.write");
} }
}, },
paletteLabel: RED._("node-red:file.label.write"), paletteLabel: RED._("node-red:file.label.write"),
@ -229,6 +234,17 @@
value: "setbymsg", value: "setbymsg",
label: node._("file.encoding.setbymsg") label: node._("file.encoding.setbymsg")
}).text(label).appendTo(encSel); }).text(label).appendTo(encSel);
$("#node-input-filename").typedInput({
default: "str",
types:[{ value: "str", label:"", icon:"red/images/typedInput/az.svg"}, "msg", "jsonata", "env"],
typeField: $("#node-input-filenameType")
});
if(!node.filename && !node.filenameType) {
node.filename = "filename"
node.filenameType = "msg"
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
}
encodings.forEach(function(item) { encodings.forEach(function(item) {
if(Array.isArray(item)) { if(Array.isArray(item)) {
var group = $("<optgroup/>", { var group = $("<optgroup/>", {
@ -267,6 +283,7 @@
defaults: { defaults: {
name: {value:""}, name: {value:""},
filename: {value:""}, filename: {value:""},
filenameType: {value:""},
format: {value:"utf8"}, format: {value:"utf8"},
chunk: {value:false}, chunk: {value:false},
sendError: {value: false}, sendError: {value: false},
@ -291,7 +308,9 @@
}, },
icon: "file-in.svg", icon: "file-in.svg",
label: function() { label: function() {
return this.name||this.filename||this._("file.label.read"); var fn = this.filename;
if(this.filenameType != "str") { fn = ""; }
return this.name||fn||this._("file.label.read");
}, },
paletteLabel: RED._("node-red:file.label.read"), paletteLabel: RED._("node-red:file.label.read"),
labelStyle: function() { labelStyle: function() {
@ -305,6 +324,17 @@
value: "none", value: "none",
label: label label: label
}).text(label).appendTo(encSel); }).text(label).appendTo(encSel);
$("#node-input-filename").typedInput({
default: "str",
types:[{ value: "str", label:"", icon:"red/images/typedInput/az.svg"}, "msg", "jsonata", "env"],
typeField: $("#node-input-filenameType")
});
if(!node.filename && !node.filenameType) {
node.filename = "filename"
node.filenameType = "msg"
$("#node-input-filename").typedInput("type", node.filenameType);
$("#node-input-filename").typedInput("value", node.filename);
}
encodings.forEach(function(item) { encodings.forEach(function(item) {
if(Array.isArray(item)) { if(Array.isArray(item)) {
var group = $("<optgroup/>", { var group = $("<optgroup/>", {

View File

@ -39,6 +39,7 @@ module.exports = function(RED) {
// Write/delete a file // Write/delete a file
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.filename = n.filename; this.filename = n.filename;
this.filenameType = n.filenameTpye;
this.appendNewline = n.appendNewline; this.appendNewline = n.appendNewline;
this.overwriteFile = n.overwriteFile.toString(); this.overwriteFile = n.overwriteFile.toString();
this.createDir = n.createDir || false; this.createDir = n.createDir || false;
@ -50,7 +51,22 @@ module.exports = function(RED) {
node.closeCallback = null; node.closeCallback = null;
function processMsg(msg,nodeSend, done) { function processMsg(msg,nodeSend, done) {
var filename = node.filename || msg.filename || ""; var filename = node.filename || "";
//Pre V3 compatibility - if filename and filenameType are empty, check msg.filename
if(!node.filenameType && !node.filename) {
msg.filename = "filename";
node.filenameType = "msg";
}
RED.util.evaluateNodeProperty(node.filename,node.filenameType,node,msg,(err,value) => {
if (err) {
node.error(err,msg);
return done();
} else {
filename = value;
}
});
var fullFilename = filename; var fullFilename = filename;
if (filename && RED.settings.fileWorkingDirectory && !path.isAbsolute(filename)) { if (filename && RED.settings.fileWorkingDirectory && !path.isAbsolute(filename)) {
fullFilename = path.resolve(path.join(RED.settings.fileWorkingDirectory,filename)); fullFilename = path.resolve(path.join(RED.settings.fileWorkingDirectory,filename));
@ -158,7 +174,7 @@ module.exports = function(RED) {
done(); done();
}); });
} }
if (node.filename) { if (node.filenameType === "str") {
// Static filename - write and reuse the stream next time // Static filename - write and reuse the stream next time
node.wstream.write(buf, function() { node.wstream.write(buf, function() {
nodeSend(msg); nodeSend(msg);
@ -256,6 +272,7 @@ module.exports = function(RED) {
// Read a file // Read a file
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.filename = n.filename; this.filename = n.filename;
this.filenameType = n.filenameType;
this.format = n.format; this.format = n.format;
this.chunk = false; this.chunk = false;
this.encoding = n.encoding || "none"; this.encoding = n.encoding || "none";
@ -270,8 +287,24 @@ module.exports = function(RED) {
var node = this; var node = this;
this.on("input",function(msg, nodeSend, nodeDone) { this.on("input",function(msg, nodeSend, nodeDone) {
var filename = (node.filename || msg.filename || "").replace(/\t|\r|\n/g,''); var filename = node.filename || "";
//Pre V3 compatibility - if filename and filenameType are empty, check msg.filename
if(!node.filenameType && !node.filename) {
msg.filename = "filename";
node.filenameType = "msg";
}
RED.util.evaluateNodeProperty(node.filename,node.filenameType,node,msg,(err,value) => {
if (err) {
node.error(err,msg);
return done();
} else {
filename = (value || "").replace(/\t|\r|\n/g,'');
}
});
var fullFilename = filename; var fullFilename = filename;
var filePath = "";
if (filename && RED.settings.fileWorkingDirectory && !path.isAbsolute(filename)) { if (filename && RED.settings.fileWorkingDirectory && !path.isAbsolute(filename)) {
fullFilename = path.resolve(path.join(RED.settings.fileWorkingDirectory,filename)); fullFilename = path.resolve(path.join(RED.settings.fileWorkingDirectory,filename));
} }