Skip to content

Commit

Permalink
Add 'previous value' option to Switch node
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Jan 15, 2016
1 parent 392fd6f commit 81f0828
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions nodes/core/locales/en-US/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"minutes": "minutes",
"hours": "hours",
"between": "between",
"previous": "previous value",
"at": "at",
"and": "and",
"every": "every",
Expand Down
8 changes: 5 additions & 3 deletions nodes/core/logic/10-switch.html
Original file line number Diff line number Diff line change
Expand Up @@ -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:"=="},
Expand Down Expand Up @@ -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> ');
Expand Down
15 changes: 12 additions & 3 deletions nodes/core/logic/10-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 81f0828

Please sign in to comment.