1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add "otherwise" option to switch node - also added ability to stop checking after first match or keep matching (as-is today). Fixes Issue #88

This commit is contained in:
Dave C-J 2013-11-22 16:24:22 +00:00
parent 3054b04378
commit ff8db09fd9
2 changed files with 77 additions and 77 deletions

View File

@ -29,34 +29,41 @@
</div> </div>
<a href="#" class="btn btn-mini" id="node-input-add-rule" style="margin-top: 4px;"><i class="icon-plus"></i> Add</a> <a href="#" class="btn btn-mini" id="node-input-add-rule" style="margin-top: 4px;"><i class="icon-plus"></i> Add</a>
</div> </div>
<div>
<select id="node-input-checkall" style="width:100%; margin-right:5px;">
<option value="true">checking all rules</option>
<option value="false">stopping after first match</option>
</select>
</div>
</script> </script>
<script type="text/x-red" data-help-name="switch"> <script type="text/x-red" data-help-name="switch">
<p>A simple function node to route messages based on its properties.</p> <p>A simple function node to route messages based on its properties.</p>
<p>When a message arrives, the selected property is evaluated against each <p>When a message arrives, the selected property is evaluated against each
of the defined rules. The message is then sent to the output of <i>all</i> of the defined rules. The message is then sent to the output of <i>all</i>
rules that pass.</p> rules that pass.</p>
<p>Note: the <i>otherwise</i> rule applies as a "not any of" the rules preceeding it.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('switch',{ RED.nodes.registerType('switch', {
color:"#E2D96E", color: "#E2D96E",
category: 'function', category: 'function',
defaults: { defaults: {
name: {value:""}, name: {value:""},
property: {value: "payload",required:true}, property: {value:"payload", required:true},
rules:{value:[{t:"eq",v:""}]}, rules: {value:[{t:"eq", v:""}]},
outputs:{value:1} checkall: {value:"true", required:true},
outputs: {value:1}
}, },
inputs:1, inputs: 1,
outputs:1, outputs: 1,
icon: "switch.png", icon: "switch.png",
label: function() { label: function() {
return this.name; return this.name;
}, },
oneditprepare: function() { oneditprepare: function() {
var operators = [ var operators = [
{v:"eq",t:"=="}, {v:"eq",t:"=="},
{v:"neq",t:"!="}, {v:"neq",t:"!="},
@ -70,30 +77,29 @@
{v:"true",t:"is true"}, {v:"true",t:"is true"},
{v:"false",t:"is false"}, {v:"false",t:"is false"},
{v:"null",t:"is null"}, {v:"null",t:"is null"},
{v:"nnull",t:"is not null"} {v:"nnull",t:"is not null"},
{v:"else",t:"otherwise"}
]; ];
function generateRule(i,rule) { function generateRule(i,rule) {
var container = $('<li/>',{style:"margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"}); var container = $('<li/>',{style:"margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"});
var row = $('<div/>').appendTo(container); var row = $('<div/>').appendTo(container);
var row2 = $('<div/>',{style:"padding-top: 5px; text-align: right;"}).appendTo(container); var row2 = $('<div/>',{style:"padding-top: 5px; text-align: right;"}).appendTo(container);
var selectField = $('<select/>',{style:"width:120px; margin-left: 5px; text-align: center;"}).appendTo(row); var selectField = $('<select/>',{style:"width:120px; margin-left: 5px; text-align: center;"}).appendTo(row);
for (var d in operators) { for (var d in operators) {
selectField.append($("<option></option>").val(operators[d].v).text(operators[d].t)); selectField.append($("<option></option>").val(operators[d].v).text(operators[d].t));
} }
var valueField = $('<input/>',{class:"node-input-rule-value",type:"text",style:"margin-left: 5px; width: 145px;"}).appendTo(row); var valueField = $('<input/>',{class:"node-input-rule-value",type:"text",style:"margin-left: 5px; width: 145px;"}).appendTo(row);
var btwnField = $('<span/>').appendTo(row); var btwnField = $('<span/>').appendTo(row);
var btwnValueField = $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px; width: 50px;"}).appendTo(btwnField); var btwnValueField = $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px; width: 50px;"}).appendTo(btwnField);
btwnField.append(" and "); btwnField.append(" and ");
var btwnValue2Field = $('<input/>',{class:"node-input-rule-btwn-value2",type:"text",style:"width: 50px;margin-left:2px;"}).appendTo(btwnField); var btwnValue2Field = $('<input/>',{class:"node-input-rule-btwn-value2",type:"text",style:"width: 50px;margin-left:2px;"}).appendTo(btwnField);
var finalspan = $('<span/>',{style:"float: right; margin-top: 3px;margin-right: 10px;"}).appendTo(row); var finalspan = $('<span/>',{style:"float: right; margin-top: 3px;margin-right: 10px;"}).appendTo(row);
finalspan.append(' send to <span class="node-input-rule-index">'+i+'</span> '); finalspan.append(' send to <span class="node-input-rule-index">'+i+'</span> ');
selectField.change(function() { selectField.change(function() {
var type = selectField.children("option:selected").val(); var type = selectField.children("option:selected").val();
if (type.length < 4) { if (type.length < 4) {
@ -108,17 +114,17 @@
btwnField.show(); btwnField.show();
} else { } else {
btwnField.hide(); btwnField.hide();
if (type === "true" || type === "false" || type === "null" || type === "nnull") { if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
valueField.hide(); valueField.hide();
} else { } else {
valueField.show(); valueField.show();
} }
} }
}); });
var deleteButton = $('<a/>',{href:"#",class:"btn btn-mini", style:"margin-left: 5px;"}).appendTo(finalspan); var deleteButton = $('<a/>',{href:"#",class:"btn btn-mini", style:"margin-left: 5px;"}).appendTo(finalspan);
$('<i/>',{class:"icon-remove"}).appendTo(deleteButton); $('<i/>',{class:"icon-remove"}).appendTo(deleteButton);
deleteButton.click(function() { deleteButton.click(function() {
container.css({"background":"#fee"}); container.css({"background":"#fee"});
container.fadeOut(300, function() { container.fadeOut(300, function() {
@ -126,12 +132,12 @@
$("#node-input-rule-container").children().each(function(i) { $("#node-input-rule-container").children().each(function(i) {
$(this).find(".node-input-rule-index").html(i+1); $(this).find(".node-input-rule-index").html(i+1);
}); });
}); });
}); });
$("#node-input-rule-container").append(container); $("#node-input-rule-container").append(container);
selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true); selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true);
if (rule.t == "btwn") { if (rule.t == "btwn") {
btwnValueField.val(rule.v); btwnValueField.val(rule.v);
@ -140,20 +146,18 @@
valueField.val(rule.v); valueField.val(rule.v);
} }
selectField.change(); selectField.change();
} }
$("#node-input-add-rule").click(function() { $("#node-input-add-rule").click(function() {
generateRule($("#node-input-rule-container").children().length+1,{t:"",v:"",v2:""}); generateRule($("#node-input-rule-container").children().length+1,{t:"",v:"",v2:""});
$("#node-input-rule-container-div").scrollTop($("#node-input-rule-container-div").get(0).scrollHeight); $("#node-input-rule-container-div").scrollTop($("#node-input-rule-container-div").get(0).scrollHeight);
}); });
for (var i=0;i<this.rules.length;i++) { for (var i=0;i<this.rules.length;i++) {
var rule = this.rules[i]; var rule = this.rules[i];
generateRule(i+1,rule); generateRule(i+1,rule);
} }
function switchDialogResize(ev,ui) { function switchDialogResize(ev,ui) {
$("#node-input-rule-container-div").css("height",(ui.size.height-260)+"px"); $("#node-input-rule-container-div").css("height",(ui.size.height-260)+"px");
}; };
@ -172,15 +176,13 @@
oneditsave: function() { oneditsave: function() {
var rules = $("#node-input-rule-container").children(); var rules = $("#node-input-rule-container").children();
var ruleset; var ruleset;
var node = this; var node = this;
node.rules= []; node.rules= [];
rules.each(function(i) { rules.each(function(i) {
var rule = $(this); var rule = $(this);
var type = rule.find("select option:selected").val(); var type = rule.find("select option:selected").val();
var r = {t:type}; var r = {t:type};
if (!(type === "true" || type === "false" || type === "null" || type === "nnull")) { if (!(type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else")) {
if (type === "btwn") { if (type === "btwn") {
r.v = rule.find(".node-input-rule-btwn-value").val(); r.v = rule.find(".node-input-rule-btwn-value").val();
r.v2 = rule.find(".node-input-rule-btwn-value2").val(); r.v2 = rule.find(".node-input-rule-btwn-value2").val();
@ -189,11 +191,8 @@
} }
} }
node.rules.push(r); node.rules.push(r);
}); });
node.outputs = node.rules.length; node.outputs = node.rules.length;
} }
}); });
</script> </script>

View File

@ -14,51 +14,52 @@
* limitations under the License. * limitations under the License.
**/ **/
var RED = require(process.env.NODE_RED_HOME+"/red/red"); var RED = require(process.env.NODE_RED_HOME + "/red/red");
var operators = { var operators = {
'eq':function(a,b) { return a == b; }, 'eq': function(a, b) { return a == b; },
'neq':function(a,b) { return a != b; }, 'neq': function(a, b) { return a != b; },
'lt':function(a,b) { return a < b; }, 'lt': function(a, b) { return a < b; },
'lte':function(a,b) { return a <= b; }, 'lte': function(a, b) { return a <= b; },
'gt':function(a,b) { return a > b; }, 'gt': function(a, b) { return a > b; },
'gte':function(a,b) { return a >= b; }, 'gte': function(a, b) { return a >= b; },
'btwn':function(a,b,c) { return a >= b && a <= c; }, 'btwn': function(a, b, c) { return a >= b && a <= c; },
'cont':function(a,b) { return (a+"").indexOf(b) != -1; }, 'cont': function(a, b) { return (a + "").indexOf(b) != -1; },
'regex': function(a,b) { return (a+"").match(new RegExp(b)); }, 'regex': function(a, b) { return (a + "").match(new RegExp(b)); },
'true': function(a) { return a === true; }, 'true': function(a) { return a === true; },
'false': function(a) { return a === false; }, 'false': function(a) { return a === false; },
'null': function(a) { return a === null; }, 'null': function(a) { return a === null; },
'nnull': function(a) { return a !== null; } 'nnull': function(a) { return a !== null; },
'else': function(a) { return a === true; }
}; };
function SwitchNode(n) { function SwitchNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this, n);
this.rules = n.rules;
this.rules = n.rules; this.property = n.property;
this.property = n.property; this.checkall = n.checkall || "true";
var propertyParts = n.property.split("."),
var propertyParts = n.property.split("."); node = this;
var node = this; this.on('input', function (msg) {
var onward = [];
this.on('input',function(msg) { var prop = propertyParts.reduce(function (obj, i) {
var onward = []; return obj[i]
var prop = propertyParts.reduce(function(obj,i) { }, msg);
return obj[i] var elseflag = true;
},msg); for (var i=0; i<node.rules.length; i+=1) {
for (var i=0;i<node.rules.length;i+=1) { var rule = node.rules[i];
var rule = node.rules[i]; var test = prop;
if (operators[rule.t](prop,rule.v,rule.v2)) { if (rule.t == "else") { test = elseflag; elseflag = true; }
onward.push(msg); if (operators[rule.t](test,rule.v, rule.v2)) {
} else { onward.push(msg);
onward.push(null); elseflag = false;
} if (node.checkall == "false") { break; }
} } else {
this.send(onward); onward.push(null);
}); }
}
this.send(onward);
});
} }
RED.nodes.registerType("switch", SwitchNode);
RED.nodes.registerType("switch",SwitchNode);