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

Add 'previous value' option to Switch node

This commit is contained in:
Nick O'Leary 2016-01-15 11:35:59 +00:00
parent 392fd6fed3
commit 81f082825d
3 changed files with 18 additions and 6 deletions

View File

@ -45,6 +45,7 @@
"minutes": "minutes",
"hours": "hours",
"between": "between",
"previous": "previous value",
"at": "at",
"and": "and",
"every": "every",

View File

@ -66,6 +66,8 @@
},
oneditprepare: function() {
var node = this;
var previousValueType = {value:"prev",label:this._("inject.previous"),hasValue:false};
$("#node-input-property").typedInput({default:this.propertyType||'msg',types:['msg','flow','global']});
var operators = [
{v:"eq",t:"=="},
@ -129,10 +131,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).typedInput({default:'str',types:['msg','flow','global','str','num']});
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']});
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']});
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-right: 10px;"}).appendTo(row);
finalspan.append(' &#8594; <span class="node-input-rule-index">'+i+'</span> ');

View File

@ -39,6 +39,7 @@ module.exports = function(RED) {
this.property = n.property;
this.propertyType = n.propertyType || "msg";
this.checkall = n.checkall || "true";
this.previousValue = null;
var node = this;
for (var i=0; i<this.rules.length; i+=1) {
var rule = this.rules[i];
@ -70,11 +71,19 @@ module.exports = function(RED) {
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') {
var v1,v2;
if (rule.vt === 'prev') {
v1 = node.previousValue;
} else {
v1 = RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg);
}
v2 = rule.v2;
if (rule.v2t === 'prev') {
v2 = node.previousValue;
} else if (typeof v2 !== 'undefined') {
v2 = RED.util.evaluateNodeProperty(rule.v2,rule.v2t,node,msg);
}
node.previousValue = prop;
if (rule.t == "else") { test = elseflag; elseflag = true; }
if (operators[rule.t](test,v1,v2,rule.case)) {
onward.push(msg);