mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Add propertySelect support to Change node
This commit is contained in:
@@ -51,7 +51,7 @@
|
||||
category: 'function',
|
||||
defaults: {
|
||||
name: {value:""},
|
||||
rules:{value:[{t:"set",p:"payload",to:""}]},
|
||||
rules:{value:[{t:"set",p:"payload",pt:"msg",to:"",tot:"str"}]},
|
||||
// legacy
|
||||
action: {value:""},
|
||||
property: {value:""},
|
||||
@@ -77,11 +77,11 @@
|
||||
} else {
|
||||
if (this.rules.length == 1) {
|
||||
if (this.rules[0].t === "set") {
|
||||
return this._("change.label.set",{property:"msg."+this.rules[0].p});
|
||||
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:"msg."+this.rules[0].p});
|
||||
return this._("change.label.change",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p});
|
||||
} else {
|
||||
return this._("change.label.delete",{property:"msg."+this.rules[0].p});
|
||||
return this._("change.label.delete",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p});
|
||||
}
|
||||
} else {
|
||||
return this._("change.label.changeCount",{count:this.rules.length});
|
||||
@@ -99,14 +99,27 @@
|
||||
var search = this._("change.action.search");
|
||||
var replace = this._("change.action.replace");
|
||||
var regex = this._("change.label.regex");
|
||||
if (this.reg === null) { $("#node-input-reg").prop('checked', true); }
|
||||
$("#node-input-action").change();
|
||||
|
||||
function resizeRule(rule,width) {
|
||||
rule.find('input[type="text"]').width(width-220);
|
||||
rule.find('input[type="text"]:not(.red-ui-propertySelect)').width(width-180);
|
||||
rule.find('.red-ui-propertySelect').propertySelect("width",width-180);
|
||||
}
|
||||
|
||||
function generateRule(rule) {
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
var container = $('<li/>',{style:"background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"});
|
||||
|
||||
var row1 = $('<div/>').appendTo(container);
|
||||
@@ -114,37 +127,28 @@
|
||||
var row2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container);
|
||||
var row3 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container);
|
||||
|
||||
var selectField = $('<select/>',{class:"node-input-rule-type",style:"width: 100px"}).appendTo(row1);
|
||||
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++) {
|
||||
selectField.append($("<option></option>").val(selectOptions[i].v).text(selectOptions[i].l));
|
||||
}
|
||||
|
||||
$('<div/>',{style:"display:inline-block; width: 50px; text-align: right;"}).text("msg.").appendTo(row1);
|
||||
var propertyName = $('<input/>',{style:"width: 220px",class:"node-input-rule-property-name",type:"text"}).appendTo(row1);
|
||||
|
||||
var propertyName = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-name",type:"text"}).appendTo(row1).propertySelect({options:['msg','flow','global']});
|
||||
|
||||
var finalspan = $('<span/>',{style:"float: right; margin-right: 10px;"}).appendTo(row1);
|
||||
var deleteButton = $('<a/>',{href:"#",class:"editor-button editor-button-small", style:"margin-top: 7px; margin-left: 5px;"}).appendTo(finalspan);
|
||||
$('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton);
|
||||
|
||||
|
||||
$('<div/>',{style:"display: inline-block;text-align:right; width:150px;padding-right: 10px; box-sizing: border-box;"}).text(to).appendTo(row2);
|
||||
var propertyValue = $('<input/>',{style:"width: 220px",class:"node-input-rule-property-value",type:"text"}).appendTo(row2);
|
||||
|
||||
$('<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).propertySelect({default:'str',options:['msg','flow','global','str','num','json']});
|
||||
var row3_1 = $('<div/>').appendTo(row3);
|
||||
$('<div/>',{style:"display: inline-block;text-align:right; width:150px;padding-right: 10px; box-sizing: border-box;"}).text(search).appendTo(row3_1);
|
||||
var fromValue = $('<input/>',{style:"width: 220px",class:"node-input-rule-property-search-value",type:"text"}).appendTo(row3_1);
|
||||
$('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"}).text(search).appendTo(row3_1);
|
||||
var fromValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-search-value",type:"text"}).appendTo(row3_1).propertySelect({default:'str',options:['msg','flow','global','str','re']});
|
||||
|
||||
var row3_2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(row3);
|
||||
$('<div/>',{style:"display: inline-block;text-align:right; width:150px;padding-right: 10px; box-sizing: border-box;"}).text(replace).appendTo(row3_2);
|
||||
var toValue = $('<input/>',{style:"width: 220px",class:"node-input-rule-property-replace-value",type:"text"}).appendTo(row3_2);
|
||||
|
||||
var row3_3 = $('<div/>',{style:"margin-top:8px;"}).appendTo(row3);
|
||||
var id = "node-input-rule-property-regex-"+Math.floor(Math.random()*10000);
|
||||
var useRegExp = $('<input/>',{id:id,class:"node-input-rule-property-re",type:"checkbox", style:"margin-left: 150px; margin-right: 10px; display: inline-block; width: auto; vertical-align: top;"}).appendTo(row3_3);
|
||||
$('<label/>',{for:id,style:"width: auto;"}).text(regex).appendTo(row3_3);
|
||||
|
||||
$('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"}).text(replace).appendTo(row3_2);
|
||||
var toValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-replace-value",type:"text"}).appendTo(row3_2).propertySelect({default:'str',options:['msg','flow','global','str','num','json']});
|
||||
|
||||
selectField.change(function() {
|
||||
var width = $("#node-input-rule-container").width();
|
||||
@@ -170,10 +174,13 @@
|
||||
|
||||
selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true);
|
||||
propertyName.val(rule.p);
|
||||
propertyName.propertySelect('type',rule.pt)
|
||||
propertyValue.val(rule.to);
|
||||
propertyValue.propertySelect('type',rule.tot)
|
||||
fromValue.val(rule.from);
|
||||
fromValue.propertySelect('type',rule.fromt)
|
||||
toValue.val(rule.to);
|
||||
useRegExp.prop('checked', rule.re);
|
||||
toValue.propertySelect('type',rule.tot)
|
||||
selectField.change();
|
||||
|
||||
$("#node-input-rule-container").append(container);
|
||||
@@ -185,7 +192,8 @@
|
||||
if (!this.rules) {
|
||||
var rule = {
|
||||
t:(this.action=="replace"?"set":this.action),
|
||||
p:this.property
|
||||
p:this.property,
|
||||
pt:"msg"
|
||||
}
|
||||
|
||||
if (rule.t === "set") {
|
||||
@@ -250,13 +258,17 @@
|
||||
var type = rule.find(".node-input-rule-type option:selected").val();
|
||||
var r = {
|
||||
t:type,
|
||||
p:rule.find(".node-input-rule-property-name").val()
|
||||
p:rule.find(".node-input-rule-property-name").val(),
|
||||
pt:rule.find(".node-input-rule-property-name").propertySelect('type')
|
||||
};
|
||||
if (type === "set") {
|
||||
r.to = rule.find(".node-input-rule-property-value").val();
|
||||
r.tot = rule.find(".node-input-rule-property-value").propertySelect('type');
|
||||
} else if (type === "change") {
|
||||
r.from = rule.find(".node-input-rule-property-search-value").val();
|
||||
r.fromt = rule.find(".node-input-rule-property-search-value").propertySelect('type');
|
||||
r.to = rule.find(".node-input-rule-property-replace-value").val();
|
||||
r.tot = rule.find(".node-input-rule-property-replace-value").propertySelect('type');
|
||||
r.re = rule.find(".node-input-rule-property-re").prop('checked');
|
||||
}
|
||||
node.rules.push(r);
|
||||
|
@@ -19,15 +19,15 @@ module.exports = function(RED) {
|
||||
|
||||
function ChangeNode(n) {
|
||||
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") {
|
||||
rule.to = n.to||"";
|
||||
} else if (rule.t === "change") {
|
||||
@@ -37,15 +37,35 @@ 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
|
||||
if (!rule.pt) {
|
||||
rule.pt = "msg";
|
||||
}
|
||||
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";
|
||||
}
|
||||
}
|
||||
if (!rule.tot) {
|
||||
rule.tot = "str";
|
||||
}
|
||||
if (!rule.fromt) {
|
||||
rule.fromt = "str";
|
||||
}
|
||||
if (rule.t === "change") {
|
||||
if (rule.re === false) {
|
||||
if (rule.fromt !== 're') {
|
||||
rule.from = rule.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
||||
}
|
||||
try {
|
||||
@@ -55,57 +75,66 @@ module.exports = function(RED) {
|
||||
this.error(RED._("change.errors.invalid-from",{error:e.message}));
|
||||
}
|
||||
}
|
||||
if (rule.tot === 'num') {
|
||||
rule.to = Number(rule.to);
|
||||
} else if (rule.tot === 'json') {
|
||||
try {
|
||||
rule.to = JSON.parse(rule.to);
|
||||
} catch(e2) {
|
||||
valid = false;
|
||||
this.error(RED._("change.errors.invalid-json"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function applyRule(msg,rule) {
|
||||
var propertyParts;
|
||||
var depth = 0;
|
||||
|
||||
propertyParts = rule.p.split(".");
|
||||
try {
|
||||
propertyParts.reduce(function(obj, i) {
|
||||
var to = rule.to;
|
||||
// Set msg from property to another msg property
|
||||
if (rule.t === "set" && rule.to.indexOf("msg.") === 0) {
|
||||
var parts = to.substring(4);
|
||||
var msgPropParts = parts.split(".");
|
||||
try {
|
||||
msgPropParts.reduce(function(ob, j) {
|
||||
to = (typeof ob[j] !== "undefined" ? ob[j] : undefined);
|
||||
return to;
|
||||
}, msg);
|
||||
} catch (err) {}
|
||||
var property = rule.p;
|
||||
var value = rule.to;
|
||||
if (rule.tot === "msg") {
|
||||
value = RED.util.getMessageProperty(msg,rule.to);
|
||||
} else if (rule.tot === 'flow') {
|
||||
value = node.context().flow.get(rule.to);
|
||||
} else if (rule.tot === 'global') {
|
||||
value = node.context().global.get(rule.to);
|
||||
}
|
||||
if (rule.pt === 'msg') {
|
||||
if (rule.t === 'delete') {
|
||||
RED.util.setMessageProperty(msg,property,undefined);
|
||||
} else if (rule.t === 'set') {
|
||||
RED.util.setMessageProperty(msg,property,value);
|
||||
} else if (rule.t === 'change') {
|
||||
var current = RED.util.getMessageProperty(msg,property);
|
||||
if (typeof current === 'string') {
|
||||
current = current.replace(rule.from,value);
|
||||
RED.util.setMessageProperty(msg,property,current);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var target;
|
||||
if (rule.pt === 'flow') {
|
||||
target = node.context().flow;
|
||||
} else if (rule.pt === 'global') {
|
||||
target = node.context().global;
|
||||
}
|
||||
if (target) {
|
||||
if (rule.t === 'delete') {
|
||||
target.set(property,undefined);
|
||||
} else if (rule.t === 'set') {
|
||||
target.set(property,value);
|
||||
} else if (rule.t === 'change') {
|
||||
var current = target.get(msg,property);
|
||||
if (typeof current === 'string') {
|
||||
current = current.replace(rule.from,value);
|
||||
target.set(property,current);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (++depth === propertyParts.length) {
|
||||
if (rule.t === "change") {
|
||||
if (typeof obj[i] === "string") {
|
||||
obj[i] = obj[i].replace(rule.from, rule.to);
|
||||
}
|
||||
} else if (rule.t === "set") {
|
||||
if (typeof to === "undefined") {
|
||||
delete(obj[i]);
|
||||
} else {
|
||||
obj[i] = to;
|
||||
}
|
||||
} else if (rule.t === "delete") {
|
||||
delete(obj[i]);
|
||||
}
|
||||
} else {
|
||||
// to property doesn't exist, don't create empty object
|
||||
if (typeof to === "undefined") {
|
||||
return;
|
||||
// setting a non-existent multilevel object, create empty parent
|
||||
} else if (!obj[i]) {
|
||||
obj[i] = {};
|
||||
}
|
||||
return obj[i];
|
||||
}
|
||||
}, msg);
|
||||
} catch (err) {}
|
||||
}
|
||||
} catch(err) {console.log(err.stack)}
|
||||
return msg;
|
||||
}
|
||||
|
||||
if (valid) {
|
||||
var node = this;
|
||||
this.on('input', function(msg) {
|
||||
|
Reference in New Issue
Block a user