[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / logic / 15-change.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="change">
18     <div>
19         <select id="node-input-action" style="width:95%; margin-right:5px;">
20             <option value="replace">Set the value of the message property</option>
21             <option value="change">Search/replace the value of the message property</option>
22             <option value="delete">Delete the message property</option>
23         </select>
24     </div>
25     <div class="form-row" style="padding-top:10px;" id="node-prop1-row">
26         <label for="node-input-property">called</label> msg.<input type="text" id="node-input-property" style="width: 63%;"/>
27     </div>
28     <div class="form-row" id="node-from-row">
29         <label for="node-input-from" id="node-input-f"></label>
30         <input type="text" id="node-input-from" placeholder="this"/>
31     </div>
32     <div class="form-row" id="node-to-row">
33         <label for="node-input-to" id="node-input-t"></label>
34         <input type="text" id="node-input-to" placeholder="that"/>
35     </div>
36     <div class="form-row" id="node-reg-row">
37         <label>&nbsp;</label>
38         <input type="checkbox" id="node-input-reg" style="display: inline-block; width: auto; vertical-align: top;">
39         <label for="node-input-reg" style="width: 70%;">Use regular expressions ?</label>
40     </div>
41     <div class="form-tips" id="node-tip"></div>
42     <br/>
43     <div class="form-row">
44         <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
45         <input type="text" id="node-input-name" placeholder="Name">
46     </div>
47 </script>
48
49 <script type="text/x-red" data-help-name="change">
50     <p>A simple function node to change, replace, add or delete properties of a message.</p>
51     <p>When a message arrives, the selected property is modified by the defined rules.
52     The message is then sent to the output.</p>
53     <p><b>Note:</b> Replace only operates on <b>strings</b>. Anything else will be passed straight through.</p>
54 </script>
55
56 <script type="text/javascript">
57     RED.nodes.registerType('change', {
58         color: "#E2D96E",
59         category: 'function',
60         defaults: {
61             action: {value:"replace",required:true},
62             property: {value:"payload",required:true},
63             from: {value:"",validate: function(v) {
64                 if (this.action == "change" && this.reg) {
65                     try {
66                         var re = new RegExp(this.from, "g");
67                         return true;
68                     } catch(err) {
69                         return false;
70                     }
71                 }
72                 return true;
73             }},
74             to: {value:""},
75             reg: {value:false},
76             name: {value:""}
77         },
78         inputs: 1,
79         outputs: 1,
80         icon: "swap.png",
81         label: function() {
82             if (this.name) {
83                 return this.name;
84             }
85             if (this.action == "replace") {
86                 return "set msg."+this.property;
87             } else {
88                 return this.action+" msg."+this.property
89             }
90         },
91         labelStyle: function() {
92             return this.name ? "node_label_italic" : "";
93         },
94         oneditprepare: function() {
95             if (this.reg === null) { $("#node-input-reg").prop('checked', true); }
96             $("#node-input-action").change( function() {
97                 var a = $("#node-input-action").val();
98                 if (a === "replace") {
99                     $("#node-input-todo").html("called");
100                     //$("#node-input-f").html("name");
101                     $("#node-input-t").html("to");
102                     $("#node-from-row").hide();
103                     $("#node-to-row").show();
104                     $("#node-reg-row").hide();
105                     $("#node-tip").show();
106                     $("#node-tip").html("Tip: expects a new property name and either a fixed value OR the full name of another message property eg: msg.sentiment.score");
107                 }
108                 if (a === "delete") {
109                     $("#node-input-todo").html("called");
110                     //$("#node-input-f").html("called");
111                     //$("#node-input-t").html("to");
112                     $("#node-from-row").hide();
113                     $("#node-to-row").hide();
114                     $("#node-reg-row").hide();
115                     $("#node-tip").hide();
116                 }
117                 if (a === "change") {
118                     $("#node-input-todo").html("called");
119                     $("#node-input-f").html("Search for");
120                     $("#node-input-t").html("replace with");
121                     $("#node-from-row").show();
122                     $("#node-to-row").show();
123                     $("#node-reg-row").show();
124                     $("#node-tip").show();
125                     $("#node-tip").html("Tip: only works on string properties. If regular expressions are used, the <i>replace with</i> field can contain capture results, eg $1.");
126                 }
127                 //if (a === "replace") {
128                  //   $("#node-input-todo").html("called");
129                  //   //$("#node-input-f").html("with");
130                  //   $("#node-input-t").html("with");
131                  //   $("#node-from-row").hide();
132                  //   $("#node-to-row").show();
133                  //   $("#node-tip").html("Tip: accepts either a fixed value OR the full name of another msg.property eg: msg.sentiment.score");
134                 //}
135             });
136             $("#node-input-action").change();
137         }
138     });
139 </script>