new features: Add Triggered Mode to Ping Node (#643)

This commit is contained in:
Stephen McLaughlin
2020-04-03 16:07:41 +01:00
committed by GitHub
parent 3087e8e2a1
commit 437c29525d
7 changed files with 336 additions and 60 deletions

View File

@@ -1,10 +1,17 @@
<script type="text/x-red" data-template-name="ping">
<div class="form-row">
<script type="text/html" data-template-name="ping">
<div class="form-row" id="div-node-input-host">
<label for="node-input-host"><i class="fa fa-dot-circle-o"></i> <span data-i18n="ping.label.target"></span></label>
<input type="text" id="node-input-host" placeholder="www.google.com">
<input type="text" id="node-input-host" placeholder="192.168.0.1, www.google.com">
</div>
<div class="form-row">
<label for="node-input-mode"><i class="fa fa-wrench"></i> <span data-i18n="ping.label.mode"></label>
<select type="text" id="node-input-mode" style="width: 70%">
<option value="timed" data-i18n="ping.label.mode_option.timed"></option>
<option value="triggered" data-i18n="ping.label.mode_option.triggered"></option>
</select>
</div>
<div class="form-row" id="div-node-input-timer">
<label for="node-input-timer"><i class="fa fa-repeat"></i> <span data-i18n="ping.label.ping"></label>
<input type="text" id="node-input-timer" placeholder="20">
</div>
@@ -15,13 +22,48 @@
</script>
<script type="text/javascript">
RED.nodes.registerType('ping',{
category: 'network-input',
var timerParameterValidator = function(node,v){
var mode = getMode(node)
if(mode === "triggered"){
return true;
}
if(v == ""){
return false;
}
return RED.validators.number()(v);
}
var hostParameterValidator = function(node,v){
var mode = getMode(node)
if(mode === "triggered"){
return true;
}
return v != ""
}
var getMode = function(node){
if(node){
return node.mode
} else {
let $mode = $( "#node-input-mode" );
return $mode.val();
}
}
RED.nodes.registerType("ping",{
category: "network-input",
color:"#fdf0c2",
defaults: {
mode: {value:"timed"},
name: {value:""},
host: {value:"",required:true},
timer: {value:"20", required:true, validate:RED.validators.number()}
host: {value:"", validate: function(v){
return hostParameterValidator(this,v) ;
}
},
timer: {value:"20", validate: function(v){
return timerParameterValidator(this,v) ;
}
},
inputs: {value:0}
},
inputs:0,
outputs:1,
@@ -30,10 +72,37 @@
return this._("ping.ping");
},
label: function() {
return this.name||this.host;
let lbl = this.name||this.host||this._("ping.ping");
if(lbl.length > 20){
lbl = lbl.substring(0,17) + "..."
}
return lbl;
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function () {
let node = this;
let $timer = $("#div-node-input-timer");
$("#node-input-mode").val(node.mode);
let $mode = $( "#node-input-mode" );
function updateControlsVisibility(){
node.mode = $mode.val();
switch (node.mode) {
case "triggered":
node.inputs = 1;
node._def.inputs = 1
$timer.hide();
break;
default:
node.inputs = 0;
node._def.inputs = 0;
$timer.show();
break;
}
}
$mode.change(updateControlsVisibility);
updateControlsVisibility();
}
});
</script>