mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Add propertySelect to switch node
This commit is contained in:
parent
1f848b205b
commit
fe4dae8518
@ -27,11 +27,6 @@
|
|||||||
|
|
||||||
$.widget( "nodered.propertySelect", {
|
$.widget( "nodered.propertySelect", {
|
||||||
_create: function() {
|
_create: function() {
|
||||||
/*
|
|
||||||
{
|
|
||||||
options: []
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
if (!nlsd && RED && RED._) {
|
if (!nlsd && RED && RED._) {
|
||||||
for (var i in allOptions) {
|
for (var i in allOptions) {
|
||||||
if (allOptions.hasOwnProperty(i)) {
|
if (allOptions.hasOwnProperty(i)) {
|
||||||
@ -40,6 +35,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
nlsd = true;
|
nlsd = true;
|
||||||
|
|
||||||
var that = this;
|
var that = this;
|
||||||
this.disarmClick = false;
|
this.disarmClick = false;
|
||||||
this.element.addClass('red-ui-propertySelect');
|
this.element.addClass('red-ui-propertySelect');
|
||||||
@ -49,6 +45,12 @@
|
|||||||
.wrap( "<div>" )
|
.wrap( "<div>" )
|
||||||
.parent();
|
.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.uiSelect.addClass("red-ui-propertySelect-container");
|
||||||
|
|
||||||
this.selectTrigger = $('<a href="#" class="foo"><i class="fa fa-sort-desc"></i></a>').prependTo(this.uiSelect);
|
this.selectTrigger = $('<a href="#" class="foo"><i class="fa fa-sort-desc"></i></a>').prependTo(this.uiSelect);
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
|
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<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>
|
||||||
<div class="form-row node-input-rule-container-row" style="margin-bottom: 0px;">
|
<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;">
|
<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: {
|
defaults: {
|
||||||
name: {value:""},
|
name: {value:""},
|
||||||
property: {value:"payload", required:true},
|
property: {value:"payload", required:true},
|
||||||
|
propertyType: { value:"msg" },
|
||||||
rules: {value:[{t:"eq", v:""}]},
|
rules: {value:[{t:"eq", v:""}]},
|
||||||
checkall: {value:"true", required:true},
|
checkall: {value:"true", required:true},
|
||||||
outputs: {value:1}
|
outputs: {value:1}
|
||||||
@ -64,7 +65,7 @@
|
|||||||
return this.name||"switch";
|
return this.name||"switch";
|
||||||
},
|
},
|
||||||
oneditprepare: function() {
|
oneditprepare: function() {
|
||||||
|
$("#node-input-property").propertySelect({default:this.propertyType||'msg',options:['msg','flow','global']});
|
||||||
var operators = [
|
var operators = [
|
||||||
{v:"eq",t:"=="},
|
{v:"eq",t:"=="},
|
||||||
{v:"neq",t:"!="},
|
{v:"neq",t:"!="},
|
||||||
@ -102,14 +103,14 @@
|
|||||||
selectField.width(selectWidth);
|
selectField.width(selectWidth);
|
||||||
|
|
||||||
if (type === "btwn") {
|
if (type === "btwn") {
|
||||||
var labelWidth = rule.find(".node-input-tule-btwn-label").width();
|
var labelWidth = rule.find(".node-input-rule-btwn-label").width();
|
||||||
btwnField1.width((width-256-labelWidth)/2);
|
btwnField1.propertySelect("width",(width-selectWidth-120));
|
||||||
btwnField2.width((width-256-labelWidth)/2);
|
btwnField2.propertySelect("width",(width-selectWidth-120));
|
||||||
} else {
|
} else {
|
||||||
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
|
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
|
||||||
// valueField.hide();
|
// valueField.hide();
|
||||||
} else {
|
} 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 container = $('<li/>',{style:"background: #fff; 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; padding-left: 175px;"}).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);
|
$('<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);
|
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));
|
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).propertySelect({default:'str',options:['msg','flow','global','str','num']});
|
||||||
var btwnField = $('<span/>').appendTo(row);
|
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 btwnValueField = $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px; width: 50px;"}).appendTo(btwnField);
|
var btwnAndLabel = $('<span/>',{class:"node-input-rule-btwn-label"}).text(" "+andLabel+" ").appendTo(row3);
|
||||||
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:"margin-left:2px;"}).appendTo(row3).propertySelect({default:'num',options:['msg','flow','global','str','num']});
|
||||||
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-right: 10px;"}).appendTo(row);
|
var finalspan = $('<span/>',{style:"float: right;margin-right: 10px;"}).appendTo(row);
|
||||||
finalspan.append(' → <span class="node-input-rule-index">'+i+'</span> ');
|
finalspan.append(' → <span class="node-input-rule-index">'+i+'</span> ');
|
||||||
@ -145,20 +147,25 @@
|
|||||||
var type = selectField.children("option:selected").val();
|
var type = selectField.children("option:selected").val();
|
||||||
|
|
||||||
if (type === "btwn") {
|
if (type === "btwn") {
|
||||||
valueField.hide();
|
valueField.parent().hide();
|
||||||
btwnField.show();
|
btwnValueField.parent().show();
|
||||||
} else {
|
} else {
|
||||||
btwnField.hide();
|
btwnValueField.parent().hide();
|
||||||
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
|
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
|
||||||
valueField.hide();
|
valueField.parent().hide();
|
||||||
} else {
|
} else {
|
||||||
valueField.show();
|
valueField.parent().show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (type === "regex") {
|
if (type === "regex") {
|
||||||
row2.show();
|
row2.show();
|
||||||
|
row3.hide();
|
||||||
|
} else if (type === "btwn"){
|
||||||
|
row2.hide();
|
||||||
|
row3.show();
|
||||||
} else {
|
} else {
|
||||||
row2.hide();
|
row2.hide();
|
||||||
|
row3.hide();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -178,9 +185,12 @@
|
|||||||
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);
|
||||||
|
btwnValueField.propertySelect('type',rule.vt||'num');
|
||||||
btwnValue2Field.val(rule.v2);
|
btwnValue2Field.val(rule.v2);
|
||||||
|
btwnValue2Field.propertySelect('type',rule.v2t||'num');
|
||||||
} else if (typeof rule.v != "undefined") {
|
} else if (typeof rule.v != "undefined") {
|
||||||
valueField.val(rule.v);
|
valueField.val(rule.v);
|
||||||
|
valueField.propertySelect('type',rule.vt||'str');
|
||||||
}
|
}
|
||||||
if (rule.case) {
|
if (rule.case) {
|
||||||
caseSensitive.prop('checked',true);
|
caseSensitive.prop('checked',true);
|
||||||
@ -257,9 +267,12 @@
|
|||||||
if (!(type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else")) {
|
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.vt = rule.find(".node-input-rule-btwn-value").propertySelect('type');
|
||||||
r.v2 = rule.find(".node-input-rule-btwn-value2").val();
|
r.v2 = rule.find(".node-input-rule-btwn-value2").val();
|
||||||
|
r.v2t = rule.find(".node-input-rule-btwn-value2").propertySelect('type');
|
||||||
} else {
|
} else {
|
||||||
r.v = rule.find(".node-input-rule-value").val();
|
r.v = rule.find(".node-input-rule-value").val();
|
||||||
|
r.vt = rule.find(".node-input-rule-value").propertySelect('type');
|
||||||
}
|
}
|
||||||
if (type === "regex") {
|
if (type === "regex") {
|
||||||
r.case = rule.find(".node-input-rule-case").prop("checked");
|
r.case = rule.find(".node-input-rule-case").prop("checked");
|
||||||
@ -267,7 +280,8 @@
|
|||||||
}
|
}
|
||||||
node.rules.push(r);
|
node.rules.push(r);
|
||||||
});
|
});
|
||||||
node.outputs = node.rules.length;
|
this.outputs = node.rules.length;
|
||||||
|
this.propertyType = $("#node-input-property").propertySelect('type');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
@ -37,30 +37,46 @@ module.exports = function(RED) {
|
|||||||
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.propertyType = n.propertyType || "msg";
|
||||||
this.checkall = n.checkall || "true";
|
this.checkall = n.checkall || "true";
|
||||||
var propertyParts = (n.property || "payload").split(".");
|
|
||||||
var node = this;
|
var node = this;
|
||||||
|
|
||||||
for (var i=0; i<this.rules.length; i+=1) {
|
for (var i=0; i<this.rules.length; i+=1) {
|
||||||
var rule = this.rules[i];
|
var rule = this.rules[i];
|
||||||
if (!isNaN(Number(rule.v))) {
|
if (!rule.vt) {
|
||||||
rule.v = Number(rule.v);
|
rule.vt = 'str';
|
||||||
rule.v2 = Number(rule.v2);
|
}
|
||||||
|
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) {
|
this.on('input', function (msg) {
|
||||||
var onward = [];
|
var onward = [];
|
||||||
try {
|
try {
|
||||||
var prop = propertyParts.reduce(function (obj, i) {
|
var prop = RED.util.evaluateNodeProperty(node.property,node.propertyType,node,msg);
|
||||||
return obj[i]
|
|
||||||
}, msg);
|
|
||||||
var elseflag = true;
|
var elseflag = true;
|
||||||
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;
|
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 (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);
|
onward.push(msg);
|
||||||
elseflag = false;
|
elseflag = false;
|
||||||
if (node.checkall == "false") { break; }
|
if (node.checkall == "false") { break; }
|
||||||
|
@ -149,6 +149,26 @@ function setMessageProperty(msg,prop,value,createMissing) {
|
|||||||
}, msg);
|
}, 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 = {
|
module.exports = {
|
||||||
ensureString: ensureString,
|
ensureString: ensureString,
|
||||||
ensureBuffer: ensureBuffer,
|
ensureBuffer: ensureBuffer,
|
||||||
@ -156,5 +176,6 @@ module.exports = {
|
|||||||
compareObjects: compareObjects,
|
compareObjects: compareObjects,
|
||||||
generateId: generateId,
|
generateId: generateId,
|
||||||
getMessageProperty: getMessageProperty,
|
getMessageProperty: getMessageProperty,
|
||||||
setMessageProperty: setMessageProperty
|
setMessageProperty: setMessageProperty,
|
||||||
|
evaluateNodeProperty: evaluateNodeProperty
|
||||||
};
|
};
|
||||||
|
@ -162,5 +162,58 @@ describe("red/util", function() {
|
|||||||
util.setMessageProperty(msg,"msg.a.b.c",undefined);
|
util.setMessageProperty(msg,"msg.a.b.c",undefined);
|
||||||
should.not.exist(msg.a.b);
|
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");
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user