2 Copyright 2013 IBM Corp.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
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">
22 <div class="form-row" style="padding-top:10px;">
23 If msg.<input type="text" id="node-input-property" style="width: 200px;"/>
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;">
30 <a href="#" class="btn btn-mini" id="node-input-add-rule" style="margin-top: 4px;"><i class="fa fa-plus"></i> Add</a>
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>
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>
45 <p>Note: the <i>otherwise</i> rule applies as a "not any of" the rules preceding it.</p>
48 <script type="text/javascript">
49 RED.nodes.registerType('switch', {
54 property: {value:"payload", required:true},
55 rules: {value:[{t:"eq", v:""}]},
56 checkall: {value:"true", required:true},
63 return this.name||"switch";
65 oneditprepare: function() {
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"}
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);
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));
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);
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> ');
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"});
110 selectField.css({"width":"120px"});
112 if (type === "btwn") {
117 if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
125 var deleteButton = $('<a/>',{href:"#",class:"btn btn-mini", style:"margin-left: 5px;"}).appendTo(finalspan);
126 $('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton);
128 deleteButton.click(function() {
129 container.css({"background":"#fee"});
130 container.fadeOut(300, function() {
132 $("#node-input-rule-container").children().each(function(i) {
133 $(this).find(".node-input-rule-index").html(i+1);
139 $("#node-input-rule-container").append(container);
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);
148 selectField.change();
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);
156 for (var i=0;i<this.rules.length;i++) {
157 var rule = this.rules[i];
158 generateRule(i+1,rule);
161 function switchDialogResize(ev,ui) {
162 $("#node-input-rule-container-div").css("height",(ui.size.height-260)+"px");
165 $( "#dialog" ).on("dialogresize", switchDialogResize);
166 $( "#dialog" ).one("dialogopen", function(ev) {
167 var size = $( "#dialog" ).dialog('option','sizeCache-switch');
169 switchDialogResize(null,{size:size});
172 $( "#dialog" ).one("dialogclose", function(ev,ui) {
173 $( "#dialog" ).off("dialogresize",switchDialogResize);
176 oneditsave: function() {
177 var rules = $("#node-input-rule-container").children();
181 rules.each(function(i) {
183 var type = rule.find("select option:selected").val();
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();
190 r.v = rule.find(".node-input-rule-value").val();
195 node.outputs = node.rules.length;