2013-11-24 23:25:35 +01:00
|
|
|
|
|
|
|
<script type="text/x-red" data-template-name="change">
|
2015-03-18 17:20:21 +01:00
|
|
|
<div class="form-row">
|
2015-05-10 22:47:22 +02:00
|
|
|
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
|
2016-06-27 00:48:59 +02:00
|
|
|
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
|
2013-11-24 23:25:35 +01:00
|
|
|
</div>
|
2015-03-18 17:20:21 +01:00
|
|
|
<div class="form-row" style="margin-bottom:0;">
|
2015-05-10 22:47:22 +02:00
|
|
|
<label><i class="fa fa-list"></i> <span data-i18n="change.label.rules"></span></label>
|
2013-11-24 23:25:35 +01:00
|
|
|
</div>
|
2016-05-20 23:13:28 +02:00
|
|
|
<div class="form-row node-input-rule-container-row">
|
|
|
|
<ol id="node-input-rule-container"></ol>
|
2013-11-24 23:25:35 +01:00
|
|
|
</div>
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/x-red" data-help-name="change">
|
2016-04-10 13:20:46 +02:00
|
|
|
<p>Set, change, delete or move properties of a message, flow context or global context.</p>
|
2017-05-23 18:26:23 +02:00
|
|
|
<p>The node can specify multiple rules that will be applied in the order they are defined.</p>
|
|
|
|
<h3>Details</h3>
|
2015-03-18 17:20:21 +01:00
|
|
|
<p>The available operations are:</p>
|
2017-04-26 22:52:33 +02:00
|
|
|
<dl class="message-properties">
|
|
|
|
<dt>Set</dt>
|
|
|
|
<dd>set a property. The value can be a variety of different types, or
|
|
|
|
can be taken from an existing message or context property.</dd>
|
|
|
|
<dt>Change</dt>
|
|
|
|
<dd>search & replace parts of the property. If regular expressions
|
|
|
|
are enabled, the "replace with" property can include capture groups, for
|
|
|
|
example <code>$1</code>. Replace will only change the type if there
|
|
|
|
is a complete match.</dd>
|
|
|
|
<dt>Delete</dt>
|
|
|
|
<dd>delete a property.</dd>
|
|
|
|
<dt>Move</dt>
|
2017-10-11 14:31:37 +02:00
|
|
|
<dd>move or rename a property.</dd>
|
2017-04-26 22:52:33 +02:00
|
|
|
</dl>
|
2017-05-03 18:17:36 +02:00
|
|
|
<p>The "expression" type uses the <a href="http://jsonata.org/" target="_new">JSONata</a>
|
2017-05-23 18:26:23 +02:00
|
|
|
query and expression language.
|
2017-05-03 18:17:36 +02:00
|
|
|
</p>
|
2013-11-24 23:25:35 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<script type="text/javascript">
|
|
|
|
RED.nodes.registerType('change', {
|
|
|
|
color: "#E2D96E",
|
|
|
|
category: 'function',
|
|
|
|
defaults: {
|
2015-03-18 17:20:21 +01:00
|
|
|
name: {value:""},
|
2015-12-29 23:19:32 +01:00
|
|
|
rules:{value:[{t:"set",p:"payload",pt:"msg",to:"",tot:"str"}]},
|
2015-03-18 17:20:21 +01:00
|
|
|
// legacy
|
|
|
|
action: {value:""},
|
|
|
|
property: {value:""},
|
|
|
|
from: {value:""},
|
2013-11-24 23:25:35 +01:00
|
|
|
to: {value:""},
|
2015-03-18 17:20:21 +01:00
|
|
|
reg: {value:false}
|
2013-11-24 23:25:35 +01:00
|
|
|
},
|
|
|
|
inputs: 1,
|
|
|
|
outputs: 1,
|
|
|
|
icon: "swap.png",
|
|
|
|
label: function() {
|
2018-07-10 13:40:31 +02:00
|
|
|
function prop2name(type, key) {
|
|
|
|
var result = RED.utils.parseContextKey(key);
|
|
|
|
return type +"." +result.key;
|
|
|
|
}
|
2014-03-30 00:05:46 +01:00
|
|
|
if (this.name) {
|
|
|
|
return this.name;
|
|
|
|
}
|
2015-03-18 17:20:21 +01:00
|
|
|
if (!this.rules) {
|
|
|
|
if (this.action === "replace") {
|
2015-05-28 00:07:31 +02:00
|
|
|
return this._("change.label.set",{property:"msg."+this.property});
|
2015-03-18 17:20:21 +01:00
|
|
|
} else if (this.action === "change") {
|
2015-05-28 00:07:31 +02:00
|
|
|
return this._("change.label.change",{property:"msg."+this.property});
|
2016-04-10 13:20:46 +02:00
|
|
|
} else if (this.action === "move") {
|
|
|
|
return this._("change.label.move",{property:"msg."+this.property});
|
2015-03-18 17:20:21 +01:00
|
|
|
} else {
|
2015-05-28 00:07:31 +02:00
|
|
|
return this._("change.label.delete",{property:"msg."+this.property});
|
2015-03-18 17:20:21 +01:00
|
|
|
}
|
2014-03-30 00:05:46 +01:00
|
|
|
} else {
|
2015-03-18 17:20:21 +01:00
|
|
|
if (this.rules.length == 1) {
|
|
|
|
if (this.rules[0].t === "set") {
|
2018-07-10 13:40:31 +02:00
|
|
|
return this._("change.label.set",{property:prop2name((this.rules[0].pt||"msg"), this.rules[0].p)});
|
2015-03-18 17:20:21 +01:00
|
|
|
} else if (this.rules[0].t === "change") {
|
2018-07-10 13:40:31 +02:00
|
|
|
return this._("change.label.change",{property:prop2name((this.rules[0].pt||"msg"), this.rules[0].p)});
|
2016-04-10 13:20:46 +02:00
|
|
|
} else if (this.rules[0].t === "move") {
|
2018-07-10 13:40:31 +02:00
|
|
|
return this._("change.label.move",{property:prop2name((this.rules[0].pt||"msg"), this.rules[0].p)});
|
2015-03-18 17:20:21 +01:00
|
|
|
} else {
|
2018-07-10 13:40:31 +02:00
|
|
|
return this._("change.label.delete",{property:prop2name((this.rules[0].pt||"msg"), this.rules[0].p)});
|
2015-03-18 17:20:21 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-05-28 00:07:31 +02:00
|
|
|
return this._("change.label.changeCount",{count:this.rules.length});
|
2015-03-18 17:20:21 +01:00
|
|
|
}
|
2014-03-30 00:05:46 +01:00
|
|
|
}
|
2013-11-24 23:25:35 +01:00
|
|
|
},
|
|
|
|
labelStyle: function() {
|
|
|
|
return this.name ? "node_label_italic" : "";
|
|
|
|
},
|
|
|
|
oneditprepare: function() {
|
2015-05-28 22:55:22 +02:00
|
|
|
var set = this._("change.action.set");
|
|
|
|
var change = this._("change.action.change");
|
|
|
|
var del = this._("change.action.delete");
|
2016-04-10 13:20:46 +02:00
|
|
|
var move = this._("change.action.move");
|
2015-05-28 22:55:22 +02:00
|
|
|
var to = this._("change.action.to");
|
|
|
|
var search = this._("change.action.search");
|
|
|
|
var replace = this._("change.action.replace");
|
|
|
|
var regex = this._("change.label.regex");
|
2015-07-14 00:21:03 +02:00
|
|
|
|
2016-05-20 23:13:28 +02:00
|
|
|
function resizeRule(rule) {
|
|
|
|
var newWidth = rule.width();
|
2018-01-25 15:02:04 +01:00
|
|
|
rule.find('.red-ui-typedInput').typedInput("width",newWidth-130);
|
2016-05-20 23:13:28 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
$('#node-input-rule-container').css('min-height','300px').css('min-width','450px').editableList({
|
|
|
|
addItem: function(container,i,opt) {
|
2016-07-04 12:21:58 +02:00
|
|
|
var rule = opt;
|
|
|
|
if (!rule.hasOwnProperty('t')) {
|
|
|
|
rule = {t:"set",p:"payload",to:"",tot:"str"};
|
|
|
|
}
|
2016-05-20 23:13:28 +02:00
|
|
|
if (rule.t === "change" && rule.re) {
|
|
|
|
rule.fromt = 're';
|
|
|
|
delete rule.re;
|
|
|
|
}
|
|
|
|
if (rule.t === "set" && !rule.tot) {
|
|
|
|
if (rule.to.indexOf("msg.") === 0 && !rule.tot) {
|
|
|
|
rule.to = rule.to.substring(4);
|
|
|
|
rule.tot = "msg";
|
|
|
|
} else {
|
|
|
|
rule.tot = "str";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rule.t === "move" && !rule.tot) {
|
2015-12-29 23:19:32 +01:00
|
|
|
rule.tot = "msg";
|
|
|
|
}
|
2018-01-25 15:02:04 +01:00
|
|
|
container.css({
|
|
|
|
overflow: 'hidden',
|
|
|
|
whiteSpace: 'nowrap'
|
|
|
|
});
|
2016-05-20 23:13:28 +02:00
|
|
|
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);
|
2015-07-14 00:21:03 +02:00
|
|
|
|
2016-05-20 23:13:28 +02:00
|
|
|
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},{v:"move",l:move}];
|
2016-10-10 12:10:36 +02:00
|
|
|
for (var i=0; i<4; i++) {
|
2016-05-20 23:13:28 +02:00
|
|
|
selectField.append($("<option></option>").val(selectOptions[i].v).text(selectOptions[i].l));
|
|
|
|
}
|
2015-03-18 17:20:21 +01:00
|
|
|
|
2016-11-08 14:29:01 +01:00
|
|
|
var propertyName = $('<input/>',{class:"node-input-rule-property-name",type:"text"})
|
2016-05-20 23:13:28 +02:00
|
|
|
.appendTo(row1)
|
|
|
|
.typedInput({types:['msg','flow','global']});
|
2016-04-10 13:20:46 +02:00
|
|
|
|
2016-05-20 23:13:28 +02:00
|
|
|
$('<div/>',{style:"display:inline-block;text-align:right; width:120px; padding-right:10px; box-sizing:border-box;"})
|
|
|
|
.text(to)
|
|
|
|
.appendTo(row2);
|
2016-11-08 14:29:01 +01:00
|
|
|
var propertyValue = $('<input/>',{class:"node-input-rule-property-value",type:"text"})
|
2016-05-20 23:13:28 +02:00
|
|
|
.appendTo(row2)
|
2018-05-21 16:10:06 +02:00
|
|
|
.typedInput({default:'str',types:['msg','flow','global','str','num','bool','json','bin','date','jsonata','env']});
|
2015-07-14 00:21:03 +02:00
|
|
|
|
2016-05-20 23:13:28 +02:00
|
|
|
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)
|
|
|
|
.appendTo(row3_1);
|
2016-11-08 14:29:01 +01:00
|
|
|
var fromValue = $('<input/>',{class:"node-input-rule-property-search-value",type:"text"})
|
2016-05-20 23:13:28 +02:00
|
|
|
.appendTo(row3_1)
|
2018-05-21 16:10:06 +02:00
|
|
|
.typedInput({default:'str',types:['msg','flow','global','str','re','num','bool','env']});
|
2015-03-18 17:20:21 +01:00
|
|
|
|
2016-05-20 23:13:28 +02:00
|
|
|
var row3_2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(row3);
|
|
|
|
$('<div/>',{style:"display:inline-block;text-align:right; width:120px; padding-right:10px; box-sizing:border-box;"})
|
|
|
|
.text(replace)
|
|
|
|
.appendTo(row3_2);
|
2016-11-08 14:29:01 +01:00
|
|
|
var toValue = $('<input/>',{class:"node-input-rule-property-replace-value",type:"text"})
|
2016-05-20 23:13:28 +02:00
|
|
|
.appendTo(row3_2)
|
2018-05-21 16:10:06 +02:00
|
|
|
.typedInput({default:'str',types:['msg','flow','global','str','num','bool','json','bin','env']});
|
2016-04-10 13:20:46 +02:00
|
|
|
|
2016-05-20 23:13:28 +02:00
|
|
|
$('<div/>',{style:"display:inline-block;text-align:right; width:120px; padding-right:10px; box-sizing:border-box;"})
|
|
|
|
.text(to)
|
|
|
|
.appendTo(row4);
|
2016-11-08 14:29:01 +01:00
|
|
|
var moveValue = $('<input/>',{class:"node-input-rule-property-move-value",type:"text"})
|
2016-05-20 23:13:28 +02:00
|
|
|
.appendTo(row4)
|
|
|
|
.typedInput({default:'msg',types:['msg','flow','global']});
|
2016-04-14 03:09:41 +02:00
|
|
|
|
2016-05-20 23:13:28 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
resizeRule(container);
|
2015-03-18 17:20:21 +01:00
|
|
|
});
|
2015-07-14 00:21:03 +02:00
|
|
|
|
2016-07-04 12:21:58 +02:00
|
|
|
selectField.val(rule.t);
|
2016-05-20 23:13:28 +02:00
|
|
|
propertyName.typedInput('value',rule.p);
|
|
|
|
propertyName.typedInput('type',rule.pt);
|
|
|
|
propertyValue.typedInput('value',rule.to);
|
|
|
|
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);
|
|
|
|
toValue.typedInput('value',rule.to);
|
|
|
|
toValue.typedInput('type',rule.tot);
|
|
|
|
selectField.change();
|
2016-04-14 03:09:41 +02:00
|
|
|
|
2016-05-20 23:13:28 +02:00
|
|
|
var newWidth = $("#node-input-rule-container").width();
|
|
|
|
resizeRule(container);
|
|
|
|
},
|
|
|
|
resizeItem: resizeRule,
|
2016-05-29 23:25:30 +02:00
|
|
|
removable: true,
|
2016-05-20 23:13:28 +02:00
|
|
|
sortable: true
|
2015-03-18 17:20:21 +01:00
|
|
|
});
|
2015-07-14 00:21:03 +02:00
|
|
|
|
2015-03-18 17:20:21 +01:00
|
|
|
if (!this.rules) {
|
|
|
|
var rule = {
|
|
|
|
t:(this.action=="replace"?"set":this.action),
|
2015-12-29 23:19:32 +01:00
|
|
|
p:this.property,
|
|
|
|
pt:"msg"
|
2017-04-26 22:52:33 +02:00
|
|
|
};
|
2015-07-14 00:21:03 +02:00
|
|
|
|
2016-04-10 13:20:46 +02:00
|
|
|
if ((rule.t === "set")||(rule.t === "move")) {
|
2015-03-18 17:20:21 +01:00
|
|
|
rule.to = this.to;
|
|
|
|
} else if (rule.t === "change") {
|
|
|
|
rule.from = this.from;
|
|
|
|
rule.to = this.to;
|
|
|
|
rule.re = this.reg;
|
|
|
|
}
|
2015-07-14 00:21:03 +02:00
|
|
|
|
2015-03-18 17:20:21 +01:00
|
|
|
delete this.to;
|
|
|
|
delete this.from;
|
|
|
|
delete this.reg;
|
|
|
|
delete this.action;
|
|
|
|
delete this.property;
|
2015-07-14 00:21:03 +02:00
|
|
|
|
2015-03-18 17:20:21 +01:00
|
|
|
this.rules = [rule];
|
|
|
|
}
|
2015-07-14 00:21:03 +02:00
|
|
|
|
2016-10-10 12:10:36 +02:00
|
|
|
for (var i=0; i<this.rules.length; i++) {
|
2016-05-20 23:13:28 +02:00
|
|
|
var rule = this.rules[i];
|
|
|
|
$("#node-input-rule-container").editableList('addItem',rule);
|
2015-03-18 17:20:21 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
oneditsave: function() {
|
2016-05-20 23:13:28 +02:00
|
|
|
var rules = $("#node-input-rule-container").editableList('items');
|
2015-03-18 17:20:21 +01:00
|
|
|
var ruleset;
|
|
|
|
var node = this;
|
|
|
|
node.rules= [];
|
|
|
|
rules.each(function(i) {
|
|
|
|
var rule = $(this);
|
2016-07-04 12:21:58 +02:00
|
|
|
var type = rule.find(".node-input-rule-type").val();
|
2015-03-18 17:20:21 +01:00
|
|
|
var r = {
|
|
|
|
t:type,
|
2016-01-04 15:15:10 +01:00
|
|
|
p:rule.find(".node-input-rule-property-name").typedInput('value'),
|
2016-01-03 23:26:47 +01:00
|
|
|
pt:rule.find(".node-input-rule-property-name").typedInput('type')
|
2015-03-18 17:20:21 +01:00
|
|
|
};
|
|
|
|
if (type === "set") {
|
2016-01-04 15:15:10 +01:00
|
|
|
r.to = rule.find(".node-input-rule-property-value").typedInput('value');
|
2016-01-03 23:26:47 +01:00
|
|
|
r.tot = rule.find(".node-input-rule-property-value").typedInput('type');
|
2016-04-10 13:20:46 +02:00
|
|
|
} 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');
|
2015-03-18 17:20:21 +01:00
|
|
|
} else if (type === "change") {
|
2016-01-04 15:15:10 +01:00
|
|
|
r.from = rule.find(".node-input-rule-property-search-value").typedInput('value');
|
2016-01-03 23:26:47 +01:00
|
|
|
r.fromt = rule.find(".node-input-rule-property-search-value").typedInput('type');
|
2016-01-04 15:15:10 +01:00
|
|
|
r.to = rule.find(".node-input-rule-property-replace-value").typedInput('value');
|
2016-01-03 23:26:47 +01:00
|
|
|
r.tot = rule.find(".node-input-rule-property-replace-value").typedInput('type');
|
2015-03-18 17:20:21 +01:00
|
|
|
}
|
|
|
|
node.rules.push(r);
|
|
|
|
});
|
2016-01-04 17:53:32 +01:00
|
|
|
},
|
|
|
|
oneditresize: function(size) {
|
|
|
|
var rows = $("#dialog-form>div:not(.node-input-rule-container-row)");
|
|
|
|
var height = size.height;
|
2016-10-10 12:10:36 +02:00
|
|
|
for (var i=0; i<rows.size(); i++) {
|
2016-01-04 17:53:32 +01:00
|
|
|
height -= $(rows[i]).outerHeight(true);
|
|
|
|
}
|
|
|
|
var editorRow = $("#dialog-form>div.node-input-rule-container-row");
|
|
|
|
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
|
|
|
|
|
2016-05-20 23:13:28 +02:00
|
|
|
$("#node-input-rule-container").editableList('height',height);
|
2013-11-24 23:25:35 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|