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

Add propertySelect to switch node

This commit is contained in:
Nick O'Leary 2015-12-30 23:09:35 +00:00
parent 1f848b205b
commit fe4dae8518
5 changed files with 138 additions and 32 deletions

View File

@ -27,11 +27,6 @@
$.widget( "nodered.propertySelect", {
_create: function() {
/*
{
options: []
}
*/
if (!nlsd && RED && RED._) {
for (var i in allOptions) {
if (allOptions.hasOwnProperty(i)) {
@ -40,6 +35,7 @@
}
}
nlsd = true;
var that = this;
this.disarmClick = false;
this.element.addClass('red-ui-propertySelect');
@ -49,6 +45,12 @@
.wrap( "<div>" )
.parent();
["Top","Right","Bottom","Left"].forEach(function(d) {
var m = that.element.css("margin"+d);
that.uiSelect.css("margin"+d,m);
that.element.css("margin"+d,0);
});
this.uiSelect.addClass("red-ui-propertySelect-container");
this.selectTrigger = $('<a href="#" class="foo"><i class="fa fa-sort-desc"></i></a>').prependTo(this.uiSelect);

View File

@ -20,7 +20,7 @@
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
</div>
<div class="form-row">
<span data-i18n="switch.label.property"></span> msg.<input type="text" id="node-input-property" style="width: 200px;"/>
<span data-i18n="switch.label.property"></span> <input type="text" id="node-input-property" style="width: 300px;"/>
</div>
<div class="form-row node-input-rule-container-row" style="margin-bottom: 0px;">
<div id="node-input-rule-container-div" style="box-sizing: border-box; border-radius: 5px; height: 310px; padding: 5px; border: 1px solid #ccc; overflow-y:scroll;">
@ -53,6 +53,7 @@
defaults: {
name: {value:""},
property: {value:"payload", required:true},
propertyType: { value:"msg" },
rules: {value:[{t:"eq", v:""}]},
checkall: {value:"true", required:true},
outputs: {value:1}
@ -64,7 +65,7 @@
return this.name||"switch";
},
oneditprepare: function() {
$("#node-input-property").propertySelect({default:this.propertyType||'msg',options:['msg','flow','global']});
var operators = [
{v:"eq",t:"=="},
{v:"neq",t:"!="},
@ -102,14 +103,14 @@
selectField.width(selectWidth);
if (type === "btwn") {
var labelWidth = rule.find(".node-input-tule-btwn-label").width();
btwnField1.width((width-256-labelWidth)/2);
btwnField2.width((width-256-labelWidth)/2);
var labelWidth = rule.find(".node-input-rule-btwn-label").width();
btwnField1.propertySelect("width",(width-selectWidth-120));
btwnField2.propertySelect("width",(width-selectWidth-120));
} else {
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
// valueField.hide();
} else {
valueField.width(width-selectWidth-120);
valueField.propertySelect("width",(width-selectWidth-120));
}
}
}
@ -118,6 +119,8 @@
var container = $('<li/>',{style:"background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"});
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: 120px;"}).appendTo(container);
$('<i style="color: #eee; cursor: move;" class="node-input-rule-handle fa fa-bars"></i>').appendTo(row);
var selectField = $('<select/>',{style:"width:120px; margin-left: 5px; text-align: center;"}).appendTo(row);
@ -125,11 +128,10 @@
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 btwnField = $('<span/>').appendTo(row);
var btwnValueField = $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px; width: 50px;"}).appendTo(btwnField);
var btwnAndLabel = $('<span/>',{class:"node-input-tule-btwn-label"}).text(" "+andLabel+" ").appendTo(btwnField);
var btwnValue2Field = $('<input/>',{class:"node-input-rule-btwn-value2",type:"text",style:"width: 50px;margin-left:2px;"}).appendTo(btwnField);
var valueField = $('<input/>',{class:"node-input-rule-value",type:"text",style:"margin-left: 5px; width: 145px;"}).appendTo(row).propertySelect({default:'str',options:['msg','flow','global','str','num']});
var btwnValueField = $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px;"}).appendTo(row).propertySelect({default:'num',options:['msg','flow','global','str','num']});
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).propertySelect({default:'num',options:['msg','flow','global','str','num']});
var finalspan = $('<span/>',{style:"float: right;margin-right: 10px;"}).appendTo(row);
finalspan.append(' &#8594; <span class="node-input-rule-index">'+i+'</span> ');
@ -145,20 +147,25 @@
var type = selectField.children("option:selected").val();
if (type === "btwn") {
valueField.hide();
btwnField.show();
valueField.parent().hide();
btwnValueField.parent().show();
} else {
btwnField.hide();
btwnValueField.parent().hide();
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
valueField.hide();
valueField.parent().hide();
} else {
valueField.show();
valueField.parent().show();
}
}
if (type === "regex") {
row2.show();
row3.hide();
} else if (type === "btwn"){
row2.hide();
row3.show();
} else {
row2.hide();
row3.hide();
}
});
@ -178,9 +185,12 @@
selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true);
if (rule.t == "btwn") {
btwnValueField.val(rule.v);
btwnValueField.propertySelect('type',rule.vt||'num');
btwnValue2Field.val(rule.v2);
btwnValue2Field.propertySelect('type',rule.v2t||'num');
} else if (typeof rule.v != "undefined") {
valueField.val(rule.v);
valueField.propertySelect('type',rule.vt||'str');
}
if (rule.case) {
caseSensitive.prop('checked',true);
@ -257,9 +267,12 @@
if (!(type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else")) {
if (type === "btwn") {
r.v = rule.find(".node-input-rule-btwn-value").val();
r.vt = rule.find(".node-input-rule-btwn-value").propertySelect('type');
r.v2 = rule.find(".node-input-rule-btwn-value2").val();
r.v2t = rule.find(".node-input-rule-btwn-value2").propertySelect('type');
} else {
r.v = rule.find(".node-input-rule-value").val();
r.vt = rule.find(".node-input-rule-value").propertySelect('type');
}
if (type === "regex") {
r.case = rule.find(".node-input-rule-case").prop("checked");
@ -267,7 +280,8 @@
}
node.rules.push(r);
});
node.outputs = node.rules.length;
this.outputs = node.rules.length;
this.propertyType = $("#node-input-property").propertySelect('type');
}
});
</script>

View File

@ -37,30 +37,46 @@ module.exports = function(RED) {
RED.nodes.createNode(this, n);
this.rules = n.rules || [];
this.property = n.property;
this.propertyType = n.propertyType || "msg";
this.checkall = n.checkall || "true";
var propertyParts = (n.property || "payload").split(".");
var node = this;
for (var i=0; i<this.rules.length; i+=1) {
var rule = this.rules[i];
if (!isNaN(Number(rule.v))) {
rule.v = Number(rule.v);
rule.v2 = Number(rule.v2);
if (!rule.vt) {
rule.vt = 'str';
}
if (rule.vt === 'str' || rule.vt === 'num') {
if (!isNaN(Number(rule.v))) {
rule.v = Number(rule.v);
}
}
if (typeof rule.v2 !== 'undefined') {
if (!rule.v2t) {
rule.v2t = 'str';
}
if (rule.v2t === 'str' || rule.v2t === 'num') {
if (!isNaN(Number(rule.v2))) {
rule.v2 = Number(rule.v2);
}
}
}
}
this.on('input', function (msg) {
var onward = [];
try {
var prop = propertyParts.reduce(function (obj, i) {
return obj[i]
}, msg);
var prop = RED.util.evaluateNodeProperty(node.property,node.propertyType,node,msg);
var elseflag = true;
for (var i=0; i<node.rules.length; i+=1) {
var rule = node.rules[i];
var test = prop;
var v1 = RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg);
var v2 = rule.v2;
if (typeof v2 !== 'undefined') {
v2 = RED.util.evaluateNodeProperty(rule.v2,rule.v2t,node,msg);
}
if (rule.t == "else") { test = elseflag; elseflag = true; }
if (operators[rule.t](test,rule.v, rule.v2, rule.case)) {
if (operators[rule.t](test,v1,v2,rule.case)) {
onward.push(msg);
elseflag = false;
if (node.checkall == "false") { break; }

View File

@ -149,6 +149,26 @@ function setMessageProperty(msg,prop,value,createMissing) {
}, msg);
}
function evaluateNodeProperty(value, type, node, msg) {
if (type === 'str') {
return ""+value;
} else if (type === 'num') {
return Number(value);
} else if (type === 'json') {
return JSON.parse(value);
} else if (type === 're') {
return new RegExp(value);
} else if (type === 'msg' && msg) {
return getMessageProperty(msg,value);
} else if (type === 'flow' && node) {
return node.context().flow.get(value);
} else if (type === 'global' && node) {
return node.context().global.get(value);
}
return value;
}
module.exports = {
ensureString: ensureString,
ensureBuffer: ensureBuffer,
@ -156,5 +176,6 @@ module.exports = {
compareObjects: compareObjects,
generateId: generateId,
getMessageProperty: getMessageProperty,
setMessageProperty: setMessageProperty
setMessageProperty: setMessageProperty,
evaluateNodeProperty: evaluateNodeProperty
};

View File

@ -162,5 +162,58 @@ describe("red/util", function() {
util.setMessageProperty(msg,"msg.a.b.c",undefined);
should.not.exist(msg.a.b);
})
});
describe('evaluateNodeProperty', function() {
it('returns string',function() {
var result = util.evaluateNodeProperty('hello','str');
result.should.eql('hello');
});
it('returns number',function() {
var result = util.evaluateNodeProperty('0123','num');
result.should.eql(123);
});
it('returns evaluated json',function() {
var result = util.evaluateNodeProperty('{"a":123}','json');
result.should.eql({a:123});
});
it('returns regex',function() {
var result = util.evaluateNodeProperty('^abc$','re');
result.toString().should.eql("/^abc$/");
});
it('returns msg property',function() {
var result = util.evaluateNodeProperty('foo.bar','msg',{},{foo:{bar:"123"}});
result.should.eql("123");
});
it('returns flow property',function() {
var result = util.evaluateNodeProperty('foo.bar','flow',{
context:function() { return {
flow: { get: function(k) {
if (k === 'foo.bar') {
return '123';
} else {
return null;
}
}}
}}
},{});
result.should.eql("123");
});
it('returns global property',function() {
var result = util.evaluateNodeProperty('foo.bar','global',{
context:function() { return {
global: { get: function(k) {
if (k === 'foo.bar') {
return '123';
} else {
return null;
}
}}
}}
},{});
result.should.eql("123");
});
})
});