node-red-dashboard
Version:
A set of dashboard nodes for Node-RED
177 lines (170 loc) • 8.6 kB
HTML
<script type="text/javascript">
RED.nodes.registerType('ui_switch',{
category: 'dashboard',
color: 'rgb(176, 223, 227)',
defaults: {
name: {value: ''},
label: {value: 'switch'},
group: {type: 'ui_group', required: true},
order: {value: 0},
width: {value: 0, validate: function(v) {
var width = v||0;
var currentGroup = $('#node-input-group').val()||this.group;
var groupNode = RED.nodes.node(currentGroup);
var valid = !groupNode || +width <= +groupNode.width;
$("#node-input-size").toggleClass("input-error",!valid);
return valid;
}
},
height: {value: 0},
passthru: {value: true},
decouple: {value: "false"},
topic: {value: ''},
style: {value: ''},
onvalue: {value: true, required:true, validate: (RED.validators.hasOwnProperty('typedInput')?RED.validators.typedInput('onvalueType'):function(v) { return true})},
onvalueType: {value: 'bool'},
onicon: {value: '' },
oncolor: {value: ''},
offvalue: {value: false, required:true, validate: (RED.validators.hasOwnProperty('typedInput')?RED.validators.typedInput('offvalueType'):function(v) { return true})},
offvalueType: {value: 'bool'},
officon: {value: ''},
offcolor: {value: ''},
},
inputs:1,
outputs:1,
icon: "ui_switch.png",
paletteLabel: 'switch',
label: function() { return this.name || (~this.label.indexOf("{{") ? null : this.label) || 'switch'; },
oneditprepare: function() {
$("#node-input-size").elementSizer({
width: "#node-input-width",
height: "#node-input-height",
group: "#node-input-group"
});
$('#node-input-custom-icons').change(function() {
var v = $(this).val();
if (v === "default") {
$(".form-row-custom-icons").hide();
}
else {
$(".form-row-custom-icons").show();
}
});
if (this.onicon !== "" || this.oncolor !== "" || this.officon !=="" || this.offcolor !== "") {
$('#node-input-custom-icons').val('custom');
}
else {
$('#node-input-custom-icons').change();
}
$('#node-input-onvalue').typedInput({
default: 'str',
typeField: $("#node-input-onvalueType"),
types: ['str','num','bool','json','bin','date']
});
$('#node-input-offvalue').typedInput({
default: 'str',
typeField: $("#node-input-offvalueType"),
types: ['str','num','bool','json','bin','date']
});
$('#node-input-passthru').change(function() {
if (this.checked) {
$('.form-row-decouple').hide();
$('#node-input-decouple').val("false");
}
else {
$('.form-row-decouple').show();
}
});
},
oneditsave: function() {
if ($('#node-input-custom-icons').val() === 'default') {
$('#node-input-onicon').val('');
$('#node-input-officon').val('');
$('#node-input-oncolor').val('');
$('#node-input-offcolor').val('');
}
}
});
</script>
<script type="text/x-red" data-template-name="ui_switch">
<div class="form-row">
<label for="node-input-group"><i class="fa fa-table"></i> Group</label>
<input type="text" id="node-input-group">
</div>
<div class="form-row">
<label><i class="fa fa-object-group"></i> Size</label>
<input type="hidden" id="node-input-width">
<input type="hidden" id="node-input-height">
<button class="editor-button" id="node-input-size"></button>
</div>
<div class="form-row">
<label for="node-input-label"><i class="fa fa-i-cursor"></i> Label</label>
<input type="text" id="node-input-label">
</div>
<div class="form-row">
<label for="node-input-custom-icons"><i class="fa fa-picture-o"></i> Icon</label>
<select id="node-input-custom-icons">
<option value="default">Default</option>
<option value="custom">Custom</option>
</select>
</div>
<div class="form-row form-row-custom-icons">
<label for="node-input-onicon" style="text-align:right;"><i class="fa fa-toggle-on"></i> On Icon</label>
<input type="text" id="node-input-onicon" style="width:120px">
<label for="node-input-oncolor" style="width:50px; text-align:right;">Colour</label>
<input type="text" id="node-input-oncolor" style="width:120px">
</div>
<div class="form-row form-row-custom-icons">
<label for="node-input-officon" style="text-align:right;"><i class="fa fa-toggle-off"></i> Off Icon</label>
<input type="text" id="node-input-officon" style="width:120px">
<label for="node-input-offcolor" style="width:50px; text-align:right;">Colour</label>
<input type="text" id="node-input-offcolor" style="width:120px">
</div>
<div class="form-row">
<label style="width:auto" for="node-input-passthru"><i class="fa fa-arrow-right"></i> If <code>msg</code> arrives on input, pass through to output: </label>
<input type="checkbox" checked id="node-input-passthru" style="display:inline-block; width:auto; vertical-align:top;">
</div>
<div class="form-row form-row-decouple">
<label for="node-input-decouple"><i class="fa fa-toggle-on"></i> Indicator</label>
<select id="node-input-decouple" style="display: inline-block; vertical-align: middle; width:70%;">
<option value="false">Switch icon shows state of the output</option>
<option value="true">Switch icon shows state of the input</option>
</select>
</div>
<div class="form-row">
<label style="width:auto" for="node-input-onvalue"><i class="fa fa-envelope-o"></i> When clicked, send:</label>
</div>
<div class="form-row">
<label for="node-input-onvalue" style="padding-left:25px; margin-right:-25px">On Payload</label>
<input type="text" id="node-input-onvalue" style="width:70%">
<input type="hidden" id="node-input-onvalueType">
</div>
<div class="form-row">
<label for="node-input-offvalue" style="padding-left:25px; margin-right:-25px">Off Payload</label>
<input type="text" id="node-input-offvalue" style="width:70%">
<input type="hidden" id="node-input-offvalueType">
</div>
<div class="form-row">
<label for="node-input-topic" style="padding-left:25px; margin-right:-25px">Topic</label>
<input type="text" id="node-input-topic">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name">
</div>
</script>
<script type="text/x-red" data-help-name="ui_switch">
<p>Adds a switch to the user interface.</p>
<p>Each change in the state of the switch will generate
a <code>msg.payload</code> with the specified <b>On</b> and <b>Off</b> values.</p>
<p>The <b>On/Off Color</b> and <b>On/Off Icon</b> are optional fields. If they are all present, the default
toggle switch will be replaced with the relevant icons and their respective colors.</p>
<p>The <b>On/Off Icon</b> field can be either <a href="https://design.google.com/icons/" target="_blank">Material Design icons</a>
<i>(e.g. 'check', 'close')</i> or <a href="https://fontawesome.com/v4.7.0/icons/" target="_blank">Font Awesome icons</a> <i>(e.g. 'fa-fire')</i>.</p>
<p>The widget can be updated by an incoming <code>msg.payload</code> with the specified values,
that must also match the type of the input (number, string, etc).</p>
<p>The label can also be set by a message property by setting
the field to the name of the property, for example <code>{{msg.topic}}</code>.</p>
<p>If a <b>Topic</b> is specified, it will be added to the output as <code>msg.topic</code>.</p>
<p>Setting <code>msg.enabled</code> to <code>false</code> will disable the switch widget.</p>
</script>