2013-10-13 21:28:21 +02:00
<!--
2016-05-20 23:13:28 +02:00
Copyright 2013, 2016 IBM Corp.
2013-10-13 21:28:21 +02:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
< script type = "text/x-red" data-template-name = "switch" >
< div class = "form-row" >
2015-05-10 22:47:22 +02:00
< label for = "node-input-name" > < i class = "fa fa-tag" > < / i > < span data-i18n = "common.label.name" > < / span > < / label >
2015-05-26 22:11:14 +02:00
< input type = "text" id = "node-input-name" data-i18n = "[placeholder]common.label.name" >
2013-10-13 21:28:21 +02:00
< / div >
2014-11-13 18:21:12 +01:00
< div class = "form-row" >
2015-12-31 00:09:35 +01:00
< span data-i18n = "switch.label.property" > < / span > < input type = "text" id = "node-input-property" style = "width: 300px;" / >
2013-10-13 21:28:21 +02:00
< / div >
2016-05-20 23:13:28 +02:00
< div class = "form-row node-input-rule-container-row" >
< ol id = "node-input-rule-container" > < / ol >
2014-11-13 18:21:12 +01:00
< / div >
< div class = "form-row" >
2013-11-22 17:24:22 +01:00
< select id = "node-input-checkall" style = "width:100%; margin-right:5px;" >
2015-05-10 22:47:22 +02:00
< option value = "true" data-i18n = "switch.checkall" > < / option >
< option value = "false" data-i18n = "switch.stopfirst" > < / option >
2013-11-22 17:24:22 +01:00
< / select >
< / div >
2013-10-13 21:28:21 +02:00
< / script >
< script type = "text/x-red" data-help-name = "switch" >
2016-01-02 17:57:01 +01:00
< p > A node to route messages based on property values.< / p >
2013-11-22 17:24:22 +01:00
< 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 >
rules that pass.< / p >
2016-02-12 14:17:21 +01:00
< p > < b > Note< / b > : the < i > otherwise< / i > rule applies as a "not any of" the rules preceding it.< / p >
2013-10-13 21:28:21 +02:00
< / script >
< script type = "text/javascript" >
2013-11-22 17:24:22 +01:00
RED.nodes.registerType('switch', {
color: "#E2D96E",
2013-10-13 21:28:21 +02:00
category: 'function',
defaults: {
name: {value:""},
2013-11-22 17:24:22 +01:00
property: {value:"payload", required:true},
2015-12-31 00:09:35 +01:00
propertyType: { value:"msg" },
2013-11-22 17:24:22 +01:00
rules: {value:[{t:"eq", v:""}]},
checkall: {value:"true", required:true},
outputs: {value:1}
2013-10-13 21:28:21 +02:00
},
2013-11-22 17:24:22 +01:00
inputs: 1,
outputs: 1,
2013-10-13 21:28:21 +02:00
icon: "switch.png",
label: function() {
2015-05-28 00:07:31 +02:00
return this.name||"switch";
2013-10-13 21:28:21 +02:00
},
oneditprepare: function() {
2016-01-04 17:53:32 +01:00
var node = this;
2016-01-15 12:35:59 +01:00
var previousValueType = {value:"prev",label:this._("inject.previous"),hasValue:false};
2016-01-04 16:11:24 +01:00
$("#node-input-property").typedInput({default:this.propertyType||'msg',types:['msg','flow','global']});
2013-10-13 21:28:21 +02:00
var operators = [
{v:"eq",t:"=="},
{v:"neq",t:"!="},
{v:"lt",t:"< "},
{v:"lte",t:"< ="},
{v:"gt",t:">"},
{v:"gte",t:">="},
2015-05-28 00:07:31 +02:00
{v:"btwn",t:this._("switch.rules.btwn")},
{v:"cont",t:this._("switch.rules.cont")},
{v:"regex",t:this._("switch.rules.regex")},
{v:"true",t:this._("switch.rules.true")},
{v:"false",t:this._("switch.rules.false")},
{v:"null",t:this._("switch.rules.null")},
{v:"nnull",t:this._("switch.rules.nnull")},
{v:"else",t:this._("switch.rules.else")}
2013-10-13 21:28:21 +02:00
];
2013-11-22 17:24:22 +01:00
2015-10-04 23:14:49 +02:00
var andLabel = this._("switch.and");
var caseLabel = this._("switch.ignorecase");
2016-05-20 23:13:28 +02:00
function resizeRule(rule) {
var newWidth = rule.width();
2015-10-03 23:30:28 +02:00
var selectField = rule.find("select");
2016-05-20 23:13:28 +02:00
var type = selectField.children("option:selected").val()||"";
2015-10-03 23:30:28 +02:00
var valueField = rule.find(".node-input-rule-value");
var btwnField1 = rule.find(".node-input-rule-btwn-value");
var btwnField2 = rule.find(".node-input-rule-btwn-value2");
var selectWidth;
if (type.length < 4 ) {
selectWidth = 60;
} else if (type === "regex") {
selectWidth = 147;
} else {
selectWidth = 120;
}
selectField.width(selectWidth);
if (type === "btwn") {
2016-05-20 23:13:28 +02:00
btwnField1.typedInput("width",(newWidth-selectWidth-80));
btwnField2.typedInput("width",(newWidth-selectWidth-80));
2015-10-03 23:30:28 +02:00
} else {
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
// valueField.hide();
} else {
2016-05-20 23:13:28 +02:00
valueField.typedInput("width",(newWidth-selectWidth-80));
2015-10-03 23:30:28 +02:00
}
}
}
2016-05-20 23:13:28 +02:00
$("#node-input-rule-container").css('min-height','300px').css('min-width','450px').editableList({
addItem: function(container,i,opt) {
var rule = opt||{t:"",v:"",v2:""};
var row = $('< div / > ').appendTo(container);
var row2 = $('< div / > ',{style:"padding-top: 5px; padding-left: 175px;"}).appendTo(container);
var row3 = $('< div / > ',{style:"padding-top: 5px; padding-left: 102px;"}).appendTo(container);
var selectField = $('< select / > ',{style:"width:120px; margin-left: 5px; text-align: center;"}).appendTo(row);
for (var d in operators) {
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).typedInput({default:'str',types:['msg','flow','global','str','num',previousValueType]});
var btwnValueField = $('< input / > ',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px;"}).appendTo(row).typedInput({default:'num',types:['msg','flow','global','str','num',previousValueType]});
var btwnAndLabel = $('< span / > ',{class:"node-input-rule-btwn-label"}).text(" "+andLabel+" ").appendTo(row3);
var btwnValue2Field = $('< input / > ',{class:"node-input-rule-btwn-value2",type:"text",style:"margin-left:2px;"}).appendTo(row3).typedInput({default:'num',types:['msg','flow','global','str','num',previousValueType]});
var finalspan = $('< span / > ',{style:"float: right;margin-top: 6px;margin-right: 10px;"}).appendTo(row);
finalspan.append(' → < span class = "node-input-rule-index" > '+(i+1)+'< / span > ');
var caseSensitive = $('< input / > ',{id:"node-input-rule-case-"+i,class:"node-input-rule-case",type:"checkbox",style:"width:auto;vertical-align:top"}).appendTo(row2);
$('< label / > ',{for:"node-input-rule-case-"+i,style:"margin-left: 3px;"}).text(caseLabel).appendTo(row2);
selectField.change(function() {
resizeRule(container);
var type = selectField.children("option:selected").val();
if (type === "btwn") {
2015-12-31 00:09:35 +01:00
valueField.parent().hide();
2016-05-20 23:13:28 +02:00
btwnValueField.parent().show();
} else {
btwnValueField.parent().hide();
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
valueField.parent().hide();
} else {
valueField.parent().show();
}
}
if (type === "regex") {
row2.show();
row3.hide();
} else if (type === "btwn"){
row2.hide();
row3.show();
2013-10-13 21:28:21 +02:00
} else {
2016-05-20 23:13:28 +02:00
row2.hide();
row3.hide();
2013-10-13 21:28:21 +02:00
}
2016-05-20 23:13:28 +02:00
});
selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true);
if (rule.t == "btwn") {
btwnValueField.typedInput('value',rule.v);
btwnValueField.typedInput('type',rule.vt||'num');
btwnValue2Field.typedInput('value',rule.v2);
btwnValue2Field.typedInput('type',rule.v2t||'num');
} else if (typeof rule.v != "undefined") {
valueField.typedInput('value',rule.v);
valueField.typedInput('type',rule.vt||'str');
2013-10-13 21:28:21 +02:00
}
2016-05-20 23:13:28 +02:00
if (rule.case) {
caseSensitive.prop('checked',true);
2015-10-04 23:14:49 +02:00
} else {
2016-05-20 23:13:28 +02:00
caseSensitive.prop('checked',false);
2015-10-04 23:14:49 +02:00
}
2016-05-20 23:13:28 +02:00
selectField.change();
},
deleteItem: function(opt) {
var rules = $("#node-input-rule-container").editableList('items');
rules.each(function(i) { $(this).find(".node-input-rule-index").html(i+1); });
},
resizeItem: resizeRule,
updateOrder: function(rules) {
var rules = $("#node-input-rule-container").editableList('items');
rules.each(function(i) { $(this).find(".node-input-rule-index").html(i+1); });
},
sortable: true,
deletable: true
2013-10-13 21:28:21 +02:00
});
2013-11-22 17:24:22 +01:00
2013-10-13 21:28:21 +02:00
for (var i=0;i< this.rules.length ; i + + ) {
var rule = this.rules[i];
2016-05-20 23:13:28 +02:00
$("#node-input-rule-container").editableList('addItem',rule);
2013-10-13 21:28:21 +02:00
}
},
oneditsave: function() {
2016-05-20 23:13:28 +02:00
var rules = $("#node-input-rule-container").editableList('items');
2013-10-13 21:28:21 +02:00
var ruleset;
var node = this;
node.rules= [];
rules.each(function(i) {
var rule = $(this);
var type = rule.find("select option:selected").val();
var r = {t:type};
2013-11-22 17:24:22 +01:00
if (!(type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else")) {
2013-10-13 21:28:21 +02:00
if (type === "btwn") {
2016-01-04 15:15:10 +01:00
r.v = rule.find(".node-input-rule-btwn-value").typedInput('value');
2016-01-03 23:26:47 +01:00
r.vt = rule.find(".node-input-rule-btwn-value").typedInput('type');
2016-01-04 15:15:10 +01:00
r.v2 = rule.find(".node-input-rule-btwn-value2").typedInput('value');
2016-01-03 23:26:47 +01:00
r.v2t = rule.find(".node-input-rule-btwn-value2").typedInput('type');
2013-10-13 21:28:21 +02:00
} else {
2016-01-04 15:15:10 +01:00
r.v = rule.find(".node-input-rule-value").typedInput('value');
2016-01-03 23:26:47 +01:00
r.vt = rule.find(".node-input-rule-value").typedInput('type');
2013-10-13 21:28:21 +02:00
}
2015-10-04 23:14:49 +02:00
if (type === "regex") {
r.case = rule.find(".node-input-rule-case").prop("checked");
}
2013-10-13 21:28:21 +02:00
}
node.rules.push(r);
});
2015-12-31 00:09:35 +01:00
this.outputs = node.rules.length;
2016-01-03 23:26:47 +01:00
this.propertyType = $("#node-input-property").typedInput('type');
2016-01-04 17:53:32 +01:00
},
oneditresize: function(size) {
var rows = $("#dialog-form>div:not(.node-input-rule-container-row)");
var height = size.height;
for (var i=0;i< rows.size ( ) ; i + + ) {
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-input-rule-container-row");
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
2016-05-20 23:13:28 +02:00
$("#node-input-rule-container").editableList('height',height);
2013-10-13 21:28:21 +02:00
}
});
< / script >