-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtoggle.html
124 lines (117 loc) · 4.14 KB
/
toggle.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<!--
Copyright 2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="toggle">
<div class="form-row">
<label for="node-input-onPayload"><i class="fa fa-tasks"></i> On Payload</label>
<input type="text" id="node-input-onPayload" style="width: 70%">
<input type="hidden" id="node-input-onType">
</div>
<div class="form-row">
<label for="node-input-offPayload"><i class="fa fa-tasks"></i> Off Payload</label>
<input type="text" id="node-input-offPayload" style="width: 70%">
<input type="hidden" id="node-input-offType">
</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" placeholder="Name">
</div>
</script>
<script type="text/x-red" data-help-name="toggle">
<p>This node has 2 payloads that are triggered by sending a
message with a payload of either <i>true</i> or
<i>false</i> respectively.</p>
<p>You can change these toggle values dynamically by sending a
message with the topic set to <i>setOn</i> or <i>setOff</i>.
The payload will replace the configured value.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('toggle',{
category: 'tools',
defaults: {
name: {value: ''},
onPayload: {value: '', validate:function(v) {
var ptype = $("#node-input-onType").val() || this.onType;
if (ptype === 'json') {
try {
JSON.parse(v);
return true;
} catch(err) {
return false;
}
} else if (ptype === 'flow' || ptype === 'global' ) {
return /^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]+)*/i.test(v);
} else if (ptype === 'num') {
return /^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/.test(v);
}
return true;
}},
onType: {value: 'date'},
offPayload: {value: '', validate:function(v) {
var ptype = $("#node-input-offType").val() || this.offType;
if (ptype === 'json') {
try {
JSON.parse(v);
return true;
} catch(err) {
return false;
}
} else if (ptype === 'flow' || ptype === 'global' ) {
return /^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]+)*/i.test(v);
} else if (ptype === 'num') {
return /^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/.test(v);
}
return true;
}},
offType: {value: 'date'}
},
color: 'gray',
inputs: 1,
outputs: 1,
icon: 'default',
label: function(){
return this.name || "toggle";
},
oneditprepare: function() {
if (this.onType == null) {
if (this.onPayload == "") {
this.onType = "date";
} else {
this.onType = "str";
}
} else if (this.onType === 'string' || this.onType === 'none') {
this.onType = "str";
}
$("#node-input-onType").val(this.onType);
$("#node-input-onPayload").typedInput({
default: 'str',
typeField: $("#node-input-onType"),
types:['flow','global','str','num','bool','json','date']
});
if (this.offType == null) {
if (this.offPayload == "") {
this.offType = "date";
} else {
this.offType = "str";
}
} else if (this.offType === 'string' || this.offType === 'none') {
this.offType = "str";
}
$("#node-input-offType").val(this.offType);
$("#node-input-offPayload").typedInput({
default: 'str',
typeField: $("#node-input-offType"),
types:['flow','global','str','num','bool','json','date']
});
}
});
</script>