mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
parent
fa2d2771a7
commit
c52db897b3
@ -28,13 +28,26 @@
|
|||||||
<label for="node-input-outputs"><i class="icon-random"></i> Outputs</label>
|
<label for="node-input-outputs"><i class="icon-random"></i> Outputs</label>
|
||||||
<input id="node-input-outputs" style="width: 60px; height: 1.7em;" value="1">
|
<input id="node-input-outputs" style="width: 60px; height: 1.7em;" value="1">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-tips">See the Info tab for help writing functions.</div>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="function">
|
<script type="text/x-red" data-help-name="function">
|
||||||
<p>The generic function block where you can write code to do more interesting things.</p>
|
<p>A function block where you can write code to do more interesting things.</p>
|
||||||
<p>You can have multiple outputs - in which case the function expects you to return an array of messages... <pre>return [msg,msg2,...];</pre></p>
|
<p>The message is passed in as a JavaScript object called <code>msg</code>.</p>
|
||||||
<p>You may return null for any or all outputs if you want to.</p>
|
<p>By convention it will have a <code>msg.payload</code> property containing
|
||||||
<p>You can save your functions to a library (button to right of name field) - and likewise pick from that library.</p>
|
the body of the message.</p>
|
||||||
|
<p>The function should return the messages it wants to pass on to the next nodes
|
||||||
|
in the flow. It can return:</p>
|
||||||
|
<ul>
|
||||||
|
<li>a single message object - passed to nodes connected to the first output</li>
|
||||||
|
<li>an array of message objects - passed to nodes connected to the corresponding outputs</li>
|
||||||
|
</ul>
|
||||||
|
<p>If any element of the array is itself an array of messages, multiple
|
||||||
|
messages are sent to the corresponding output.</p>
|
||||||
|
<p>If null is returned, either by itself or as an element of the array, no
|
||||||
|
message is passed on.</p>
|
||||||
|
<p>See the <a target="_new" href="http://nodered.org/docs/writing-functions.html">online documentation</a> for more help.</p>
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
@ -43,7 +56,7 @@
|
|||||||
category: 'function',
|
category: 'function',
|
||||||
defaults: {
|
defaults: {
|
||||||
name: {value:""},
|
name: {value:""},
|
||||||
func: {value:"// The received message is stored in 'msg'\n// It will have at least a 'payload' property:\n// console.log(msg.payload);\n// The 'context' object is available to store state\n// between invocations of the function\n// context = {};\n\n\nreturn msg;"},
|
func: {value:"\nreturn msg;"},
|
||||||
outputs: {value:1}
|
outputs: {value:1}
|
||||||
},
|
},
|
||||||
inputs:1,
|
inputs:1,
|
||||||
@ -58,7 +71,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
function functionDialogResize(ev,ui) {
|
function functionDialogResize(ev,ui) {
|
||||||
$("#node-input-func-editor").css("height",(ui.size.height-235)+"px");
|
$("#node-input-func-editor").css("height",(ui.size.height-275)+"px");
|
||||||
};
|
};
|
||||||
|
|
||||||
$( "#dialog" ).on("dialogresize", functionDialogResize);
|
$( "#dialog" ).on("dialogresize", functionDialogResize);
|
||||||
|
@ -14,58 +14,59 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
module.exports = function(RED) {
|
||||||
|
|
||||||
var util = require("util");
|
var util = require("util");
|
||||||
var vm = require("vm");
|
var vm = require("vm");
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var fspath = require('path');
|
var fspath = require('path');
|
||||||
|
|
||||||
function FunctionNode(n) {
|
function FunctionNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.name = n.name;
|
this.name = n.name;
|
||||||
this.func = n.func;
|
this.func = n.func;
|
||||||
var functionText = "var results = (function(msg){"+this.func+"\n})(msg);";
|
var functionText = "var results = (function(msg){"+this.func+"\n})(msg);";
|
||||||
this.topic = n.topic;
|
this.topic = n.topic;
|
||||||
this.context = {global:RED.settings.functionGlobalContext || {}};
|
this.context = {global:RED.settings.functionGlobalContext || {}};
|
||||||
try {
|
try {
|
||||||
this.script = vm.createScript(functionText);
|
this.script = vm.createScript(functionText);
|
||||||
this.on("input", function(msg) {
|
this.on("input", function(msg) {
|
||||||
if (msg != null) {
|
if (msg != null) {
|
||||||
var sandbox = {msg:msg,console:console,util:util,Buffer:Buffer,context:this.context};
|
var sandbox = {msg:msg,console:console,util:util,Buffer:Buffer,context:this.context};
|
||||||
try {
|
try {
|
||||||
this.script.runInNewContext(sandbox);
|
this.script.runInNewContext(sandbox);
|
||||||
var results = sandbox.results;
|
var results = sandbox.results;
|
||||||
|
|
||||||
if (results == null) {
|
if (results == null) {
|
||||||
results = [];
|
results = [];
|
||||||
} else if (results.length == null) {
|
} else if (results.length == null) {
|
||||||
results = [results];
|
results = [results];
|
||||||
}
|
}
|
||||||
if (msg._topic) {
|
if (msg._topic) {
|
||||||
for (var m in results) {
|
for (var m in results) {
|
||||||
if (results[m]) {
|
if (results[m]) {
|
||||||
if (util.isArray(results[m])) {
|
if (util.isArray(results[m])) {
|
||||||
for (var n in results[m]) {
|
for (var n in results[m]) {
|
||||||
results[m][n]._topic = msg._topic;
|
results[m][n]._topic = msg._topic;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
results[m]._topic = msg._topic;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
results[m]._topic = msg._topic;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.send(results);
|
||||||
|
|
||||||
|
} catch(err) {
|
||||||
|
this.error(err.message);
|
||||||
}
|
}
|
||||||
this.send(results);
|
|
||||||
|
|
||||||
} catch(err) {
|
|
||||||
this.error(err.message);
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
} catch(err) {
|
||||||
} catch(err) {
|
this.error(err.message);
|
||||||
this.error(err.message);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RED.nodes.registerType("function",FunctionNode);
|
||||||
|
RED.library.register("functions");
|
||||||
}
|
}
|
||||||
|
|
||||||
RED.nodes.registerType("function",FunctionNode);
|
|
||||||
RED.library.register("functions");
|
|
||||||
|
Loading…
Reference in New Issue
Block a user