-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfunc-exec.html
More file actions
145 lines (134 loc) · 4.59 KB
/
func-exec.html
File metadata and controls
145 lines (134 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!--
Copyright 2013, 2015 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="func-exec">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span>Name</span></label>
<input type="text" id="node-input-name" placeholder="Enter a name">
</div>
<div class="form-row" style="margin-bottom: 0px;">
<label for="node-input-func"><i class="fa fa-wrench"></i> <span>Function</span></label>
<input type="hidden" id="node-input-func" autofocus="autofocus">
<input type="hidden" id="node-input-noerr">
</div>
<div class="form-row node-text-editor-row">
<div style="height: 250px;" class="node-text-editor" id="node-input-func-editor" ></div>
</div>
<div class="form-row">
<label for="node-input-outputs"><i class="fa fa-random"></i> <span>Outputs</span></label>
<input id="node-input-outputs" style="width: 60px; height: 1.7em;" value="1">
</div>
<div class="form-tips"><span>See the Info tab for help writing functions.</span></div>
</script>
<script type="text/x-red" data-help-name="func-exec">
<p>A function block where you can write your own code to execute func-exec nodejs library.</p>
<p><em>Example of using func-exec inside this function block:</em></p>
<p><code>
var exec = child_process.exec('cat *.js bad_file | wc -l', (error, stdout, stderr) => { <br/>
if (error) { <br/>
console.error(`exec error: ${error}`); <br/>
return; <br/>
} <br/>
console.log(`stdout: ${stdout}`); <br/>
console.log(`stderr: ${stderr}`); <br/>
callback(msg); <br/>
}); <br/>
</code></p>
<p>Supported libraries: child_process, request, console, util.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('func-exec', {
color : "#FFCC66",
category : 'function',
defaults : {
name : {
value : ""
},
func : {
value : "\nreturn msg;"
},
outputs : {
value : 1
},
noerr : {
value : 0,
required : true,
validate : function(v) {
return ((!v) || (v === 0)) ? true : false;
}
}
},
inputs : 1,
outputs : 1,
align: "left",
icon : "function.png",
label : function() {
return this.name || "func-exec";
},
oneditprepare : function() {
var that = this;
$("#node-input-outputs").spinner({
min : 1
});
function functionDialogResize() {
var rows = $("#dialog-form>div:not(.node-text-editor-row)");
var height = $("#dialog-form").height();
for (var i = 0; i < rows.size(); i++) {
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-text-editor-row");
height -= (parseInt(editorRow.css("marginTop")) + parseInt(editorRow.css("marginBottom")));
$(".node-text-editor").css("height", height + "px");
that.editor.resize();
}
$("#dialog").on("dialogresize", functionDialogResize);
$("#dialog").one("dialogopen", function(ev) {
var size = $("#dialog").dialog('option', 'sizeCache-function');
if (size) {
$("#dialog").dialog('option', 'width', size.width);
$("#dialog").dialog('option', 'height', size.height);
functionDialogResize();
}
});
$("#dialog").one("dialogclose", function(ev, ui) {
var height = $("#dialog").dialog('option', 'height');
$("#dialog").off("dialogresize", functionDialogResize);
});
this.editor = RED.editor.createEditor({
id : 'node-input-func-editor',
mode : 'ace/mode/javascript',
value : $("#node-input-func").val()
});
RED.library.create({
url : "functions", // where to get the data from
type : "function", // the type of object the library is for
editor : this.editor, // the field name the main text body goes to
mode : "ace/mode/javascript",
fields : ['name', 'outputs']
});
this.editor.focus();
},
oneditsave : function() {
var annot = this.editor.getSession().getAnnotations();
this.noerr = 0;
$("#node-input-noerr").val(0);
for (var k = 0; k < annot.length; k++) {
if (annot[k].type === "error") {
$("#node-input-noerr").val(annot.length);
this.noerr = annot.length;
}
}
$("#node-input-func").val(this.editor.getValue());
delete this.editor;
}
});
</script>