Add Move capability to change node

This commit is contained in:
Dave Conway-Jones 2016-04-10 12:20:46 +01:00
parent 736ddaeca4
commit 2954ae917b
3 changed files with 52 additions and 24 deletions

View File

@ -465,6 +465,7 @@
"set": "set __property__",
"change": "change __property__",
"delete": "delete __property__",
"move": "move __property__",
"changeCount": "change: __count__ rules",
"regex": "Use regular expressions"
},
@ -472,6 +473,7 @@
"set": "Set",
"change": "Change",
"delete": "Delete",
"move": "Move",
"to": "to",
"search": "Search for",
"replace": "Replace with"

View File

@ -1,5 +1,5 @@
<!--
Copyright 2013, 2015 IBM Corp.
Copyright 2013, 2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@ -33,7 +33,7 @@
</script>
<script type="text/x-red" data-help-name="change">
<p>Set, change or delete properties of a message, flow context or global context.</p>
<p>Set, change, delete or move properties of a message, flow context or global context.</p>
<p>The node can specify multiple rules that will be applied in turn.</p>
<p>The available operations are:</p>
<ul>
@ -42,6 +42,7 @@
<li><b>Change</b> - search &amp; replace parts of the property. If regular expressions
are enabled, the <b>replace with</b> property can include capture groups, for example <code>$1</code></li>
<li><b>Delete</b> - delete a property.</li>
<li><b>Move</b> - move or rename a property. Equivalent to set and delete in one operation.
</ul>
</script>
@ -71,6 +72,8 @@
return this._("change.label.set",{property:"msg."+this.property});
} else if (this.action === "change") {
return this._("change.label.change",{property:"msg."+this.property});
} else if (this.action === "move") {
return this._("change.label.move",{property:"msg."+this.property});
} else {
return this._("change.label.delete",{property:"msg."+this.property});
}
@ -80,6 +83,8 @@
return this._("change.label.set",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p});
} else if (this.rules[0].t === "change") {
return this._("change.label.change",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p});
} else if (this.rules[0].t === "move") {
return this._("change.label.move",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p});
} else {
return this._("change.label.delete",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p});
}
@ -95,13 +100,13 @@
var set = this._("change.action.set");
var change = this._("change.action.change");
var del = this._("change.action.delete");
var move = this._("change.action.move");
var to = this._("change.action.to");
var search = this._("change.action.search");
var replace = this._("change.action.replace");
var regex = this._("change.label.regex");
function generateRule(rule) {
if (rule.t === "change" && rule.re) {
rule.fromt = 're';
delete rule.re;
@ -114,17 +119,19 @@
rule.tot = "str";
}
}
if (rule.t === "move" && !rule.tot) {
rule.tot = "msg";
}
var container = $('<li/>',{style:"background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"});
var row1 = $('<div/>').appendTo(container);
var row2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container);
var row3 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container);
var row4 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container);
var selectField = $('<select/>',{class:"node-input-rule-type",style:"width: 110px; margin-right: 10px;"}).appendTo(row1);
var selectOptions = [{v:"set",l:set},{v:"change",l:change},{v:"delete",l:del}];
for (var i=0;i<3;i++) {
var selectOptions = [{v:"set",l:set},{v:"change",l:change},{v:"delete",l:del},{v:"move",l:move}];
for (var i=0;i<4;i++) {
selectField.append($("<option></option>").val(selectOptions[i].v).text(selectOptions[i].l));
}
@ -137,13 +144,13 @@
.appendTo(finalspan);
$('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton);
$('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"})
.text(to)
.appendTo(row2);
var propertyValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-value",type:"text"})
.appendTo(row2)
.typedInput({default:'str',types:['msg','flow','global','str','num','bool','json']});
var row3_1 = $('<div/>').appendTo(row3);
$('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"})
.text(search)
@ -160,20 +167,35 @@
.appendTo(row3_2)
.typedInput({default:'str',types:['msg','flow','global','str','num','bool','json']});
$('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"})
.text(to)
.appendTo(row4);
var moveValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-move-value",type:"text"})
.appendTo(row4)
.typedInput({default:'msg',types:['msg','flow','global']});
selectField.change(function() {
var width = $("#node-input-rule-container").width();
var type = $(this).val();
if (type == "set") {
row2.show();
row3.hide();
row4.hide();
} else if (type == "change") {
row2.hide();
row3.show();
row4.hide();
} else if (type == "delete") {
row2.hide();
row3.hide();
row4.hide();
} else if (type == "move") {
row2.hide();
row3.hide();
row4.show();
}
});
deleteButton.click(function() {
container.css({"background":"#fee"});
container.fadeOut(300, function() {
@ -183,13 +205,15 @@
selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true);
propertyName.typedInput('value',rule.p);
propertyName.typedInput('type',rule.pt)
propertyName.typedInput('type',rule.pt);
propertyValue.typedInput('value',rule.to);
propertyValue.typedInput('type',rule.tot)
propertyValue.typedInput('type',rule.tot);
moveValue.typedInput('value',rule.to);
moveValue.typedInput('type',rule.tot);
fromValue.typedInput('value',rule.from);
fromValue.typedInput('type',rule.fromt)
fromValue.typedInput('type',rule.fromt);
toValue.typedInput('value',rule.to);
toValue.typedInput('type',rule.tot)
toValue.typedInput('type',rule.tot);
selectField.change();
$("#node-input-rule-container").append(container);
@ -205,7 +229,7 @@
pt:"msg"
}
if (rule.t === "set") {
if ((rule.t === "set")||(rule.t === "move")) {
rule.to = this.to;
} else if (rule.t === "change") {
rule.from = this.from;
@ -242,6 +266,9 @@
if (type === "set") {
r.to = rule.find(".node-input-rule-property-value").typedInput('value');
r.tot = rule.find(".node-input-rule-property-value").typedInput('type');
} else if (type === "move") {
r.to = rule.find(".node-input-rule-property-move-value").typedInput('value');
r.tot = rule.find(".node-input-rule-property-move-value").typedInput('type');
} else if (type === "change") {
r.from = rule.find(".node-input-rule-property-search-value").typedInput('value');
r.fromt = rule.find(".node-input-rule-property-search-value").typedInput('type');

View File

@ -1,5 +1,5 @@
/**
* Copyright 2013, 2015 IBM Corp.
* Copyright 2013, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -21,14 +21,13 @@ module.exports = function(RED) {
RED.nodes.createNode(this, n);
this.rules = n.rules;
if (!this.rules) {
var rule = {
t:(n.action=="replace"?"set":n.action),
p:n.property||""
}
if (rule.t === "set") {
if ((rule.t === "set")||(rule.t === "move")) {
rule.to = n.to||"";
} else if (rule.t === "change") {
rule.from = n.from||"";
@ -38,10 +37,7 @@ module.exports = function(RED) {
this.rules = [rule];
}
this.actions = [];
var valid = true;
for (var i=0;i<this.rules.length;i++) {
var rule = this.rules[i];
// Migrate to type-aware rules
@ -105,7 +101,6 @@ module.exports = function(RED) {
} else if (rule.tot === 'global') {
value = node.context().global.get(rule.to);
}
if (rule.t === 'change') {
if (rule.fromt === 'msg' || rule.fromt === 'flow' || rule.fromt === 'global') {
if (rule.fromt === "msg") {
@ -130,9 +125,8 @@ module.exports = function(RED) {
} catch (e) {
valid = false;
node.error(RED._("change.errors.invalid-from",{error:e.message}));
return
return;
}
} else {
node.error(RED._("change.errors.invalid-from",{error:"unsupported type: "+(typeof fromValue)}));
return
@ -203,7 +197,6 @@ module.exports = function(RED) {
}
}
}
}
} catch(err) {/*console.log(err.stack)*/}
return msg;
@ -212,7 +205,13 @@ module.exports = function(RED) {
var node = this;
this.on('input', function(msg) {
for (var i=0;i<this.rules.length;i++) {
msg = applyRule(msg,this.rules[i]);
if (this.rules[i].t === "move") {
var r = this.rules[i];
msg = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:r.p, tot:r.pt});
applyRule(msg,{t:"delete", p:r.p, pt:r.pt});
} else {
msg = applyRule(msg,this.rules[i]);
}
if (msg === null) {
return;
}