Fix Delay node milliseconds label to read ms.

(and add use strict)
This commit is contained in:
Dave C-J 2014-05-23 23:15:28 +01:00
parent dcc0adf2f7
commit b67e70e09f
2 changed files with 33 additions and 26 deletions

View File

@ -36,7 +36,7 @@
<option value="days">Days</option> <option value="days">Days</option>
</select> </select>
</div> </div>
<div id="rate-details" class="form-row"> <div id="rate-details" class="form-row">
<label for="node-input-rate"><i class="icon-time"></i> To</label> <label for="node-input-rate"><i class="icon-time"></i> To</label>
<input type="text" id="node-input-rate" placeholder="1" style="direction:rtl; width:30px !important"> <input type="text" id="node-input-rate" placeholder="1" style="direction:rtl; width:30px !important">
@ -54,7 +54,7 @@
<div id="random-details" class="form-row"> <div id="random-details" class="form-row">
<label for="node-input-randomFirst"><i class="icon-time"></i> Between</label> <label for="node-input-randomFirst"><i class="icon-time"></i> Between</label>
<input type="text" id="node-input-randomFirst" placeholder="" style="directon:rtl; width:30px !important"> <input type="text" id="node-input-randomFirst" placeholder="" style="directon:rtl; width:30px !important">
<label for="node-input-randomLast" style="width:20px"> & </label> <label for="node-input-randomLast" style="width:20px"> & </label>
<input type="text" id="node-input-randomLast" placeholder="" style="directon:rtl; width:30px !important"> <input type="text" id="node-input-randomLast" placeholder="" style="directon:rtl; width:30px !important">
<select id="node-input-randomUnits" style="width:140px !important"> <select id="node-input-randomUnits" style="width:140px !important">
<option value="milliseconds">Milliseconds</option> <option value="milliseconds">Milliseconds</option>
@ -64,12 +64,12 @@
<option value="days">Days</option> <option value="days">Days</option>
</select> </select>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
</script> </script>
<!-- Next, some simple help text is provided for the node. --> <!-- Next, some simple help text is provided for the node. -->
@ -101,6 +101,7 @@
label: function() { // sets the default label contents label: function() { // sets the default label contents
if (this.pauseType == "delay") { if (this.pauseType == "delay") {
var units = this.timeoutUnits ? this.timeoutUnits.charAt(0) : "s"; var units = this.timeoutUnits ? this.timeoutUnits.charAt(0) : "s";
if (this.timeoutUnits == "milliseconds") { units = "ms"; }
return this.name||"delay "+this.timeout+" " + units; return this.name||"delay "+this.timeout+" " + units;
} else if (this.pauseType == "rate") { } else if (this.pauseType == "rate") {
var units = this.rateUnits ? this.rateUnits.charAt(0) : "s"; var units = this.rateUnits ? this.rateUnits.charAt(0) : "s";
@ -116,10 +117,10 @@
oneditprepare: function() { oneditprepare: function() {
$( "#node-input-timeout" ).spinner({min:1,max:60}); $( "#node-input-timeout" ).spinner({min:1,max:60});
$( "#node-input-rate" ).spinner({min:1}); $( "#node-input-rate" ).spinner({min:1});
$( "#node-input-randomFirst" ).spinner({min:0}); $( "#node-input-randomFirst" ).spinner({min:0});
$( "#node-input-randomLast" ).spinner({min:1}); $( "#node-input-randomLast" ).spinner({min:1});
if (this.pauseType == "delay") { if (this.pauseType == "delay") {
$("#delay-details").show(); $("#delay-details").show();
$("#rate-details").hide(); $("#rate-details").hide();
@ -129,23 +130,23 @@
$("#rate-details").show(); $("#rate-details").show();
$("#random-details").hide(); $("#random-details").hide();
} else if (this.pauseType == "random") { } else if (this.pauseType == "random") {
$("#delay-details").hide(); $("#delay-details").hide();
$("#rate-details").hide(); $("#rate-details").hide();
$("#random-details").show(); $("#random-details").show();
} }
if (!this.timeoutUnits) { if (!this.timeoutUnits) {
$("#node-input-timeoutUnits option").filter(function() { $("#node-input-timeoutUnits option").filter(function() {
return $(this).val() == 'seconds'; return $(this).val() == 'seconds';
}).attr('selected', true); }).attr('selected', true);
} }
if (!this.randomUnits) { if (!this.randomUnits) {
$("#node-input-randomUnits option").filter(function() { $("#node-input-randomUnits option").filter(function() {
return $(this).val() == 'seconds'; return $(this).val() == 'seconds';
}).attr('selected', true); }).attr('selected', true);
} }
$("#node-input-pauseType").on("change",function() { $("#node-input-pauseType").on("change",function() {
if (this.value == "delay") { if (this.value == "delay") {
$("#delay-details").show(); $("#delay-details").show();

View File

@ -16,6 +16,7 @@
//Simple node to introduce a pause into a flow //Simple node to introduce a pause into a flow
module.exports = function(RED) { module.exports = function(RED) {
"use strict";
function random(n) { function random(n) {
var wait = n.randomFirst + (n.diff * Math.random()); var wait = n.randomFirst + (n.diff * Math.random());
if (n.buffer.length > 0) { if (n.buffer.length > 0) {
@ -25,15 +26,15 @@ module.exports = function(RED) {
n.randomID = -1; n.randomID = -1;
} }
} }
function DelayNode(n) { function DelayNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.pauseType = n.pauseType; this.pauseType = n.pauseType;
this.timeoutUnits = n.timeoutUnits; this.timeoutUnits = n.timeoutUnits;
this.randomUnits = n.randomUnits; this.randomUnits = n.randomUnits;
this.rateUnits = n.rateUnits; this.rateUnits = n.rateUnits;
if (n.timeoutUnits === "milliseconds") { if (n.timeoutUnits === "milliseconds") {
this.timeout = n.timeout; this.timeout = n.timeout;
} else if (n.timeoutUnits === "seconds") { } else if (n.timeoutUnits === "seconds") {
@ -45,7 +46,7 @@ module.exports = function(RED) {
} else if (n.timeoutUnits === "days") { } else if (n.timeoutUnits === "days") {
this.timeout = n.timeout * (24 * 60 * 60 * 1000); this.timeout = n.timeout * (24 * 60 * 60 * 1000);
} }
if (n.rateUnits === "second") { if (n.rateUnits === "second") {
this.rate = 1000/n.rate; this.rate = 1000/n.rate;
} else if (n.rateUnits === "minute") { } else if (n.rateUnits === "minute") {
@ -55,7 +56,7 @@ module.exports = function(RED) {
} else if (n.rateUnits === "day") { } else if (n.rateUnits === "day") {
this.rate = (24 * 60 * 60 * 1000)/n.rate; this.rate = (24 * 60 * 60 * 1000)/n.rate;
} }
if (n.randomUnits === "milliseconds") { if (n.randomUnits === "milliseconds") {
this.randomFirst = n.randomFirst; this.randomFirst = n.randomFirst;
this.randomLast = n.randomLast; this.randomLast = n.randomLast;
@ -72,7 +73,7 @@ module.exports = function(RED) {
this.randomFirst = n.randomFirst * (24 * 60 * 60 * 1000); this.randomFirst = n.randomFirst * (24 * 60 * 60 * 1000);
this.randomLast = n.randomLast * (24 * 60 * 60 * 1000); this.randomLast = n.randomLast * (24 * 60 * 60 * 1000);
} }
this.diff = this.randomLast - this.randomFirst; this.diff = this.randomLast - this.randomFirst;
this.name = n.name; this.name = n.name;
this.idList = []; this.idList = [];
@ -82,7 +83,7 @@ module.exports = function(RED) {
this.lastSent = Date.now(); this.lastSent = Date.now();
this.drop = n.drop; this.drop = n.drop;
var node = this; var node = this;
if (this.pauseType === "delay") { if (this.pauseType === "delay") {
this.on("input", function(msg) { this.on("input", function(msg) {
var id; var id;
@ -92,19 +93,22 @@ module.exports = function(RED) {
}, node.timeout); }, node.timeout);
this.idList.push(id); this.idList.push(id);
}); });
this.on("close", function() { this.on("close", function() {
for (var i=0; i<this.idList.length; i++ ) { for (var i=0; i<this.idList.length; i++ ) {
clearTimeout(this.idList[i]); clearTimeout(this.idList[i]);
} }
this.idList = []; this.idList = [];
}); });
} else if (this.pauseType === "rate") { } else if (this.pauseType === "rate") {
this.on("input", function(msg) { this.on("input", function(msg) {
if (!node.drop) { if (!node.drop) {
if ( node.intervalID !== -1) { if ( node.intervalID !== -1) {
node.buffer.push(msg); node.buffer.push(msg);
if (node.buffer.length > 0) {
node.status({text:node.buffer.length});
}
if (node.buffer.length > 1000) { if (node.buffer.length > 1000) {
node.warn(this.name + " buffer exceeded 1000 messages"); node.warn(this.name + " buffer exceeded 1000 messages");
} }
@ -114,10 +118,12 @@ module.exports = function(RED) {
if (node.buffer.length === 0) { if (node.buffer.length === 0) {
clearInterval(node.intervalID); clearInterval(node.intervalID);
node.intervalID = -1; node.intervalID = -1;
node.status({text:""});
} }
if (node.buffer.length > 0) { if (node.buffer.length > 0) {
node.send(node.buffer.shift()); node.send(node.buffer.shift());
node.status({text:node.buffer.length});
} }
},node.rate); },node.rate);
} }
@ -129,12 +135,12 @@ module.exports = function(RED) {
} }
} }
}); });
this.on("close", function() { this.on("close", function() {
clearInterval(this.intervalID); clearInterval(this.intervalID);
this.buffer = []; this.buffer = [];
}); });
} else if (this.pauseType === "random") { } else if (this.pauseType === "random") {
this.on("input",function(msg){ this.on("input",function(msg){
node.buffer.push(msg); node.buffer.push(msg);
@ -143,7 +149,7 @@ module.exports = function(RED) {
node.randomID = setTimeout(function() {random(node);},wait); node.randomID = setTimeout(function() {random(node);},wait);
} }
}); });
this.on("close", function (){ this.on("close", function (){
if (this.randomID !== -1) { if (this.randomID !== -1) {
clearTimeout(this.randomID); clearTimeout(this.randomID);