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

Add timestamp as a default typedInput

and update Inject and change nodes to match,
and add some tests.
This commit is contained in:
Dave Conway-Jones 2016-04-18 14:38:32 +01:00
parent f55f85aa14
commit 59b34c2b3f
11 changed files with 53 additions and 26 deletions

View File

@ -22,7 +22,8 @@
num: {value:"num",label:"number",icon:"red/images/typedInput/09.png",validate:/^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/}, num: {value:"num",label:"number",icon:"red/images/typedInput/09.png",validate:/^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/},
bool: {value:"bool",label:"boolean",icon:"red/images/typedInput/bool.png",options:["true","false"]}, bool: {value:"bool",label:"boolean",icon:"red/images/typedInput/bool.png",options:["true","false"]},
json: {value:"json",label:"JSON",icon:"red/images/typedInput/json.png", validate: function(v) { try{JSON.parse(v);return true;}catch(e){return false;}}}, json: {value:"json",label:"JSON",icon:"red/images/typedInput/json.png", validate: function(v) { try{JSON.parse(v);return true;}catch(e){return false;}}},
re: {value:"re",label:"regular expression",icon:"red/images/typedInput/re.png"} re: {value:"re",label:"regular expression",icon:"red/images/typedInput/re.png"},
date: {value:"date",label:"timestamp",hasValue:false}
}; };
var nlsd = false; var nlsd = false;

View File

@ -242,7 +242,7 @@
$("#node-input-payload").typedInput({ $("#node-input-payload").typedInput({
default: 'str', default: 'str',
typeField: $("#node-input-payloadType"), typeField: $("#node-input-payloadType"),
types:['flow','global','str','num','bool','json',{value:"date",label:this._("inject.timestamp"),hasValue:false}] types:['flow','global','str','num','bool','json','date']
}); });
$("#inject-time-type-select").change(function() { $("#inject-time-type-select").change(function() {

View File

@ -46,7 +46,7 @@ module.exports = function(RED) {
} }
if (this.once) { if (this.once) {
setTimeout( function(){ node.emit("input",{}); }, 100); setTimeout( function() { node.emit("input",{}); }, 100 );
} }
this.on("input",function(msg) { this.on("input",function(msg) {

View File

@ -206,10 +206,10 @@ module.exports = function(RED) {
} }
}); });
this.on("close", function() { this.on("close", function() {
while(node.outstandingTimers.length > 0) { while (node.outstandingTimers.length > 0) {
clearTimeout(node.outstandingTimers.pop()) clearTimeout(node.outstandingTimers.pop())
} }
while(node.outstandingIntervals.length > 0) { while (node.outstandingIntervals.length > 0) {
clearInterval(node.outstandingIntervals.pop()) clearInterval(node.outstandingIntervals.pop())
} }
}) })

View File

@ -17,14 +17,14 @@
<script type="text/x-red" data-template-name="change"> <script type="text/x-red" data-template-name="change">
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
<input type="text" id="node-input-name" style="width: 370px;" data-i18n="[placeholder]common.label.name"> <input type="text" id="node-input-name" style="width:370px;" data-i18n="[placeholder]common.label.name">
</div> </div>
<div class="form-row" style="margin-bottom:0;"> <div class="form-row" style="margin-bottom:0;">
<label><i class="fa fa-list"></i> <span data-i18n="change.label.rules"></span></label> <label><i class="fa fa-list"></i> <span data-i18n="change.label.rules"></span></label>
</div> </div>
<div class="form-row node-input-rule-container-row" style="margin-bottom: 0px;"> <div class="form-row node-input-rule-container-row" style="margin-bottom:0px;">
<div id="node-input-rule-container-div" style="box-sizing: border-box; border-radius: 5px; height: 300px; padding: 5px; border: 1px solid #ccc; overflow-y:scroll;"> <div id="node-input-rule-container-div" style="box-sizing:border-box; border-radius:5px; height:300px; padding:5px; border:1px solid #ccc; overflow-y:scroll;">
<ol id="node-input-rule-container" style=" list-style-type:none; margin: 0;"></ol> <ol id="node-input-rule-container" style=" list-style-type:none; margin:0;"></ol>
</div> </div>
</div> </div>
<div class="form-row"> <div class="form-row">
@ -123,54 +123,54 @@
rule.tot = "msg"; rule.tot = "msg";
} }
var container = $('<li/>',{style:"background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"}); var container = $('<li/>',{style:"background:#fff; margin:0; padding:8px 0px; border-bottom:1px solid #ccc;"});
var row1 = $('<div/>').appendTo(container); var row1 = $('<div/>').appendTo(container);
var row2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container); var row2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container);
var row3 = $('<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 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 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}]; 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++) { for (var i=0;i<4;i++) {
selectField.append($("<option></option>").val(selectOptions[i].v).text(selectOptions[i].l)); selectField.append($("<option></option>").val(selectOptions[i].v).text(selectOptions[i].l));
} }
var propertyName = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-name",type:"text"}) var propertyName = $('<input/>',{style:"width:250px",class:"node-input-rule-property-name",type:"text"})
.appendTo(row1) .appendTo(row1)
.typedInput({types:['msg','flow','global']}); .typedInput({types:['msg','flow','global']});
var finalspan = $('<span/>',{style:"float: right; margin-right: 10px;"}) var finalspan = $('<span/>',{style:"float:right; margin-right:10px;"})
.appendTo(row1); .appendTo(row1);
var deleteButton = $('<a/>',{href:"#",class:"editor-button editor-button-small", style:"margin-top: 7px; margin-left: 5px;"}) var deleteButton = $('<a/>',{href:"#",class:"editor-button editor-button-small", style:"margin-top:7px; margin-left:5px;"})
.appendTo(finalspan); .appendTo(finalspan);
$('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton); $('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton);
$('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"}) $('<div/>',{style:"display:inline-block;text-align:right; width:120px; padding-right:10px; box-sizing:border-box;"})
.text(to) .text(to)
.appendTo(row2); .appendTo(row2);
var propertyValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-value",type:"text"}) var propertyValue = $('<input/>',{style:"width:250px",class:"node-input-rule-property-value",type:"text"})
.appendTo(row2) .appendTo(row2)
.typedInput({default:'str',types:['msg','flow','global','str','num','bool','json']}); .typedInput({default:'str',types:['msg','flow','global','str','num','bool','json','date']});
var row3_1 = $('<div/>').appendTo(row3); var row3_1 = $('<div/>').appendTo(row3);
$('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"}) $('<div/>',{style:"display:inline-block;text-align:right; width:120px; padding-right:10px; box-sizing:border-box;"})
.text(search) .text(search)
.appendTo(row3_1); .appendTo(row3_1);
var fromValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-search-value",type:"text"}) var fromValue = $('<input/>',{style:"width:250px",class:"node-input-rule-property-search-value",type:"text"})
.appendTo(row3_1) .appendTo(row3_1)
.typedInput({default:'str',types:['msg','flow','global','str','re','num','bool']}); .typedInput({default:'str',types:['msg','flow','global','str','re','num','bool']});
var row3_2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(row3); 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;"}) $('<div/>',{style:"display:inline-block;text-align:right; width:120px; padding-right:10px; box-sizing:border-box;"})
.text(replace) .text(replace)
.appendTo(row3_2); .appendTo(row3_2);
var toValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-replace-value",type:"text"}) var toValue = $('<input/>',{style:"width:250px",class:"node-input-rule-property-replace-value",type:"text"})
.appendTo(row3_2) .appendTo(row3_2)
.typedInput({default:'str',types:['msg','flow','global','str','num','bool','json']}); .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;"}) $('<div/>',{style:"display:inline-block;text-align:right; width:120px; padding-right:10px; box-sizing:border-box;"})
.text(to) .text(to)
.appendTo(row4); .appendTo(row4);
var moveValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-move-value",type:"text"}) var moveValue = $('<input/>',{style:"width:250px",class:"node-input-rule-property-move-value",type:"text"})
.appendTo(row4) .appendTo(row4)
.typedInput({default:'msg',types:['msg','flow','global']}); .typedInput({default:'msg',types:['msg','flow','global']});

View File

@ -100,6 +100,8 @@ module.exports = function(RED) {
value = node.context().flow.get(rule.to); value = node.context().flow.get(rule.to);
} else if (rule.tot === 'global') { } else if (rule.tot === 'global') {
value = node.context().global.get(rule.to); value = node.context().global.get(rule.to);
} else if (rule.tot === 'date') {
value = Date.now();
} }
if (rule.t === 'change') { if (rule.t === 'change') {
if (rule.fromt === 'msg' || rule.fromt === 'flow' || rule.fromt === 'global') { if (rule.fromt === 'msg' || rule.fromt === 'flow' || rule.fromt === 'global') {

View File

@ -222,7 +222,8 @@
"num": "number", "num": "number",
"re": "regular expression", "re": "regular expression",
"bool": "boolean", "bool": "boolean",
"json": "JSON" "json": "JSON",
"date": "timestamp"
} }
} }
} }

View File

@ -62,7 +62,6 @@ module.exports = {
} }
themeSettings = null; themeSettings = null;
theme = settings.editorTheme; theme = settings.editorTheme;
}, },
app: function() { app: function() {

View File

@ -182,6 +182,8 @@ function evaluateNodeProperty(value, type, node, msg) {
return JSON.parse(value); return JSON.parse(value);
} else if (type === 're') { } else if (type === 're') {
return new RegExp(value); return new RegExp(value);
} else if (type === 'date') {
return Date.now();
} else if (type === 'msg' && msg) { } else if (type === 'msg' && msg) {
return getMessageProperty(msg,value); return getMessageProperty(msg,value);
} else if (type === 'flow' && node) { } else if (type === 'flow' && node) {

View File

@ -242,7 +242,6 @@ describe('change Node', function() {
}); });
}); });
it('changes the value to a number', function(done) { it('changes the value to a number', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"123","tot":"num"}],"name":"changeNode","wires":[["helperNode1"]]}, var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"123","tot":"num"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
@ -260,6 +259,7 @@ describe('change Node', function() {
changeNode1.receive({payload:""}); changeNode1.receive({payload:""});
}); });
}); });
it('changes the value to a js object', function(done) { it('changes the value to a js object', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":'{"a":123}',"tot":"json"}],"name":"changeNode","wires":[["helperNode1"]]}, var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":'{"a":123}',"tot":"json"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
@ -278,6 +278,24 @@ describe('change Node', function() {
}); });
}); });
it('sets the value of the message property to the current timestamp', function(done) {
var flow = [{"id":"changeNode1","type":"change","rules":[{"t":"set","p":"ts","pt":"msg","to":"","tot":"date"}],"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
var rule = helper.getNode("changeNode1").rules[0];
helperNode1.on("input", function(msg) {
try {
(Date.now() - msg.ts).should.be.approximately(0,50);
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:Date.now()});
});
});
}); });
describe('#change', function() { describe('#change', function() {

View File

@ -210,6 +210,10 @@ describe("red/util", function() {
var result = util.evaluateNodeProperty('^abc$','re'); var result = util.evaluateNodeProperty('^abc$','re');
result.toString().should.eql("/^abc$/"); result.toString().should.eql("/^abc$/");
}); });
it('returns date',function() {
var result = util.evaluateNodeProperty('','date');
(Date.now() - result).should.be.approximately(0,50);
});
it('returns msg property',function() { it('returns msg property',function() {
var result = util.evaluateNodeProperty('foo.bar','msg',{},{foo:{bar:"123"}}); var result = util.evaluateNodeProperty('foo.bar','msg',{},{foo:{bar:"123"}});
result.should.eql("123"); result.should.eql("123");