[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / core / 80-function.html
1 <!--
2   Copyright 2013 IBM Corp.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 -->
16
17 <script type="text/x-red" data-template-name="function">
18     <div class="form-row">
19         <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
20         <input type="text" id="node-input-name" placeholder="Name">
21     </div>
22     <div class="form-row">
23         <label for="node-input-func"><i class="fa fa-wrench"></i> Function</label>
24         <input type="hidden" id="node-input-func" autofocus="autofocus">
25         <div style="height: 250px;" class="node-text-editor" id="node-input-func-editor" ></div>
26     </div>
27     <div class="form-row">
28         <label for="node-input-outputs"><i class="fa fa-random"></i> Outputs</label>
29         <input id="node-input-outputs" style="width: 60px; height: 1.7em;" value="1">
30     </div>
31     <div class="form-tips">See the Info tab for help writing functions.</div>
32 </script>
33
34 <script type="text/x-red" data-help-name="function">
35         <p>A function block where you can write code to do more interesting things.</p>
36         <p>The message is passed in as a JavaScript object called <code>msg</code>.</p>
37         <p>By convention it will have a <code>msg.payload</code> property containing
38            the body of the message.</p>
39         <p>The function should return the messages it wants to pass on to the next nodes
40         in the flow. It can return:</p>
41         <ul>
42           <li>a single message object - passed to nodes connected to the first output</li>
43           <li>an array of message objects - passed to nodes connected to the corresponding outputs</li>
44     </ul>
45     <p>If any element of the array is itself an array of messages, multiple
46               messages are sent to the corresponding output.</p>
47         <p>If null is returned, either by itself or as an element of the array, no
48               message is passed on.</p>
49         <p>See the <a target="_new" href="http://nodered.org/docs/writing-functions.html">online documentation</a> for more help.</p>
50               
51 </script>
52
53 <script type="text/javascript">
54     RED.nodes.registerType('function',{
55         color:"#fdd0a2",
56         category: 'function',
57         defaults: {
58             name: {value:""},
59             func: {value:"\nreturn msg;"},
60             outputs: {value:1}
61         },
62         inputs:1,
63         outputs:1,
64         icon: "function.png",
65         label: function() {
66             return this.name;
67         },
68         oneditprepare: function() {
69             $( "#node-input-outputs" ).spinner({
70                 min:1
71             });
72
73             function functionDialogResize(ev,ui) {
74                 $("#node-input-func-editor").css("height",(ui.size.height-275)+"px");
75             };
76
77             $( "#dialog" ).on("dialogresize", functionDialogResize);
78             $( "#dialog" ).one("dialogopen", function(ev) {
79                 var size = $( "#dialog" ).dialog('option','sizeCache-function');
80                 if (size) {
81                     functionDialogResize(null,{size:size});
82                 }
83             });
84             $( "#dialog" ).one("dialogclose", function(ev,ui) {
85                 var height = $( "#dialog" ).dialog('option','height');
86                 $( "#dialog" ).off("dialogresize",functionDialogResize);
87             });
88             var that = this;
89             require(["orion/editor/edit"], function(edit) {
90                 that.editor = edit({
91                     parent:document.getElementById('node-input-func-editor'),
92                     lang:"js",
93                     contents: $("#node-input-func").val()
94                 });
95                 RED.library.create({
96                     url:"functions", // where to get the data from
97                     type:"function", // the type of object the library is for
98                     editor:that.editor, // the field name the main text body goes to
99                     fields:['name','outputs']
100                 });
101                 $("#node-input-name").focus();
102
103             });
104         },
105         oneditsave: function() {
106             $("#node-input-func").val(this.editor.getText())
107             delete this.editor;
108         }
109     });
110 </script>