[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / core / 80-template.html
1 <!--
2   Copyright 2013,2014 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="template">
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-template"><i class="fa fa-file-code-o"></i> Template</label>
24         <input type="hidden" id="node-input-template" autofocus="autofocus">
25         <div style="height: 250px;" class="node-text-editor" id="node-input-template-editor" ></div>
26     </div>
27     <div class="form-row">
28         <label for="node-input-field"><i class="fa fa-edit"></i> Property</label>
29         msg.<input type="text" id="node-input-field" placeholder="payload" style="width: 64%;">
30     </div>
31 </script>
32
33 <script type="text/x-red" data-help-name="template">
34     <p>Creates a new message based on the provided template.</p>
35     <p>This uses the <i><a href="http://mustache.github.io/mustache.5.html" target="_new">mustache</a></i> format.</p>
36     <p>For example, when a template of:
37     <pre>Hello {{name}}. Today is {{date}}</pre>
38     <p>receives a message containing:
39     <pre>{
40   name: "Fred",
41   date: "Monday"
42   payload: ...
43 }</pre>
44     <p>The resulting payload will be:
45     <pre>Hello Fred. Today is Monday</pre>
46 </script>
47
48 <script type="text/javascript">
49     RED.nodes.registerType('template',{
50         color:"rgb(243, 181, 103)",
51         category: 'function',
52         defaults: {
53             name: {value:""},
54             field: {value:"payload"},
55             template: {value:"This is the payload: {{payload}}!"},
56         },
57         inputs:1,
58         outputs:1,
59         icon: "template.png",
60         label: function() {
61             return this.name;
62         },
63         oneditprepare: function() {
64
65             function templateDialogResize(ev,ui) {
66                 $("#node-input-template-editor").css("height",(ui.size.height-200)+"px");
67             };
68
69             $( "#dialog" ).on("dialogresize", templateDialogResize);
70             $( "#dialog" ).one("dialogopen", function(ev) {
71                 var size = $( "#dialog" ).dialog('option','sizeCache-template');
72                 if (size) {
73                     templateDialogResize(null,{size:size});
74                 }
75             });
76             $( "#dialog" ).one("dialogclose", function(ev,ui) {
77                 var height = $( "#dialog" ).dialog('option','height');
78                 $( "#dialog" ).off("dialogresize",templateDialogResize);
79             });
80
81             var that = this;
82             require(["orion/editor/edit"], function(edit) {
83                 that.editor = edit({
84                     parent:document.getElementById('node-input-template-editor'),
85                     lang:"html",
86                     contents: $("#node-input-template").val()
87                 });
88                 RED.library.create({
89                     url:"templates", // where to get the data from
90                     type:"template", // the type of object the library is for
91                     editor:that.editor, // the field name the main text body goes to
92                     fields:['name','field']
93                 });
94                 $("#node-input-name").focus();
95             });
96         },
97         oneditsave: function() {
98             $("#node-input-template").val(this.editor.getText())
99             delete this.editor;
100         }
101     });
102 </script>