[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / logic / 10-switch.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="switch">
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" style="padding-top:10px;">
23         If msg.<input type="text" id="node-input-property" style="width: 200px;"/>
24     </div>
25     <div class="form-row">
26         <div id="node-input-rule-container-div" style="border-radius: 5px; height: 310px; padding: 5px; border: 1px solid #ccc; overflow-y:scroll;">
27         <ol id="node-input-rule-container" style=" list-style-type:none; margin: 0;">
28         </ol>
29         </div>
30         <a href="#" class="btn btn-mini" id="node-input-add-rule" style="margin-top: 4px;"><i class="fa fa-plus"></i> Add</a>
31     </div>
32     <div>
33         <select id="node-input-checkall" style="width:100%; margin-right:5px;">
34             <option value="true">checking all rules</option>
35             <option value="false">stopping after first match</option>
36         </select>
37     </div>
38 </script>
39
40 <script type="text/x-red" data-help-name="switch">
41     <p>A simple function node to route messages based on its properties.</p>
42     <p>When a message arrives, the selected property is evaluated against each
43     of the defined rules. The message is then sent to the output of <i>all</i>
44     rules that pass.</p>
45     <p>Note: the <i>otherwise</i> rule applies as a "not any of" the rules preceding it.</p>
46 </script>
47
48 <script type="text/javascript">
49     RED.nodes.registerType('switch', {
50         color: "#E2D96E",
51         category: 'function',
52         defaults: {
53             name: {value:""},
54             property: {value:"payload", required:true},
55             rules: {value:[{t:"eq", v:""}]},
56             checkall: {value:"true", required:true},
57             outputs: {value:1}
58         },
59         inputs: 1,
60         outputs: 1,
61         icon: "switch.png",
62         label: function() {
63             return this.name||"switch";
64         },
65         oneditprepare: function() {
66
67             var operators = [
68                 {v:"eq",t:"=="},
69                 {v:"neq",t:"!="},
70                 {v:"lt",t:"<"},
71                 {v:"lte",t:"<="},
72                 {v:"gt",t:">"},
73                 {v:"gte",t:">="},
74                 {v:"btwn",t:"is between"},
75                 {v:"cont",t:"contains"},
76                 {v:"regex",t:"matches regex"},
77                 {v:"true",t:"is true"},
78                 {v:"false",t:"is false"},
79                 {v:"null",t:"is null"},
80                 {v:"nnull",t:"is not null"},
81                 {v:"else",t:"otherwise"}
82             ];
83
84             function generateRule(i,rule) {
85                 var container = $('<li/>',{style:"margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"});
86                 var row = $('<div/>').appendTo(container);
87                 var row2 = $('<div/>',{style:"padding-top: 5px; text-align: right;"}).appendTo(container);
88
89                 var selectField = $('<select/>',{style:"width:120px; margin-left: 5px; text-align: center;"}).appendTo(row);
90                 for (var d in operators) {
91                     selectField.append($("<option></option>").val(operators[d].v).text(operators[d].t));
92                 }
93
94                 var valueField = $('<input/>',{class:"node-input-rule-value",type:"text",style:"margin-left: 5px; width: 145px;"}).appendTo(row);
95                 var btwnField = $('<span/>').appendTo(row);
96                 var btwnValueField = $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px; width: 50px;"}).appendTo(btwnField);
97                 btwnField.append(" and ");
98                 var btwnValue2Field = $('<input/>',{class:"node-input-rule-btwn-value2",type:"text",style:"width: 50px;margin-left:2px;"}).appendTo(btwnField);
99
100                 var finalspan = $('<span/>',{style:"float: right; margin-top: 3px;margin-right: 10px;"}).appendTo(row);
101                 finalspan.append(' send to <span class="node-input-rule-index">'+i+'</span> ');
102
103                 selectField.change(function() {
104                     var type = selectField.children("option:selected").val();
105                     if (type.length < 4) {
106                         selectField.css({"width":"60px"});
107                     } else if (type === "regex") {
108                         selectField.css({"width":"147px"});
109                     } else {
110                         selectField.css({"width":"120px"});
111                     }
112                     if (type === "btwn") {
113                         valueField.hide();
114                         btwnField.show();
115                     } else {
116                         btwnField.hide();
117                         if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
118                             valueField.hide();
119                         } else {
120                             valueField.show();
121                         }
122                     }
123                 });
124
125                 var deleteButton = $('<a/>',{href:"#",class:"btn btn-mini", style:"margin-left: 5px;"}).appendTo(finalspan);
126                 $('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton);
127
128                 deleteButton.click(function() {
129                     container.css({"background":"#fee"});
130                     container.fadeOut(300, function() {
131                         $(this).remove();
132                         $("#node-input-rule-container").children().each(function(i) {
133                             $(this).find(".node-input-rule-index").html(i+1);
134                         });
135
136                     });
137                 });
138
139                 $("#node-input-rule-container").append(container);
140
141                 selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true);
142                 if (rule.t == "btwn") {
143                     btwnValueField.val(rule.v);
144                     btwnValue2Field.val(rule.v2);
145                 } else if (typeof rule.v != "undefined") {
146                     valueField.val(rule.v);
147                 }
148                 selectField.change();
149             }
150
151             $("#node-input-add-rule").click(function() {
152                 generateRule($("#node-input-rule-container").children().length+1,{t:"",v:"",v2:""});
153                 $("#node-input-rule-container-div").scrollTop($("#node-input-rule-container-div").get(0).scrollHeight);
154             });
155
156             for (var i=0;i<this.rules.length;i++) {
157                 var rule = this.rules[i];
158                 generateRule(i+1,rule);
159             }
160
161             function switchDialogResize(ev,ui) {
162                 $("#node-input-rule-container-div").css("height",(ui.size.height-260)+"px");
163             };
164
165             $( "#dialog" ).on("dialogresize", switchDialogResize);
166             $( "#dialog" ).one("dialogopen", function(ev) {
167                 var size = $( "#dialog" ).dialog('option','sizeCache-switch');
168                 if (size) {
169                     switchDialogResize(null,{size:size});
170                 }
171             });
172             $( "#dialog" ).one("dialogclose", function(ev,ui) {
173                 $( "#dialog" ).off("dialogresize",switchDialogResize);
174             });
175         },
176         oneditsave: function() {
177             var rules = $("#node-input-rule-container").children();
178             var ruleset;
179             var node = this;
180             node.rules= [];
181             rules.each(function(i) {
182                 var rule = $(this);
183                 var type = rule.find("select option:selected").val();
184                 var r = {t:type};
185                 if (!(type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else")) {
186                     if (type === "btwn") {
187                         r.v = rule.find(".node-input-rule-btwn-value").val();
188                         r.v2 = rule.find(".node-input-rule-btwn-value2").val();
189                     } else {
190                         r.v = rule.find(".node-input-rule-value").val();
191                     }
192                 }
193                 node.rules.push(r);
194             });
195             node.outputs = node.rules.length;
196         }
197     });
198 </script>