-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcounter-loop.html
203 lines (197 loc) · 8.72 KB
/
counter-loop.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<script type="text/x-red" data-template-name="counter-loop">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="counter-loop.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]counter-loop.label.name">
</div>
<div class="form-row">
<label for="node-input-counter"><i class="fa fa-tag"></i> <span data-i18n="counter-loop.label.counter"></span></label>
<input type="text" id="node-input-counter" data-i18n="[placeholder]counter-loop.label.counter" style="width: 250px;">
<input type="hidden" id="node-input-counterType">
</div>
<div>
<label for="node-input-reset">
<input type="checkbox" id="node-input-reset" style="display:inline-block; width:15px; vertical-align:baseline;">
<span>
Set counter variable to
<select id="resetValue-select" style="width: 120px;">
<option value="value-null">null</option>
<option value="value-undefined">undefined</option>
<option value="value-empty">empty string</option>
</select>
when exiting the loop.
<input type="hidden" id="node-input-resetValue">
</span>
</div>
<div class="form-tips" style="margin-bottom: 5px;">
<span>
Tip: Counter variable needs to be <code>null</code>, <code>undefined</code> or empty string
when a flow reached this node for the first time.
</span>
</div>
<div class="form-row">
<label for="node-input-initial"><i class="fa fa-play"></i> <span data-i18n="counter-loop.label.initial"></span></label>
<input type="text" id="node-input-initial" data-i18n="[placeholder]counter-loop.label.initial" style="width: 250px;">
<input type="hidden" id="node-input-initialType">
</div>
<div class="form-row">
<label for="node-input-termination"><i class="fa fa-stop"></i> <span data-i18n="counter-loop.label.termination"></span></label>
<select id="operator-select" style="width: 70px;">
<option value="lt"><</option>
<option value="lte"><=</option>
<option value="gt">></option>
<option value="gte">>=</option>
<option value="eq">==</option>
</select>
<input type="hidden" id="node-input-operator">
<input type="text" id="node-input-termination" data-i18n="[placeholder]counter-loop.label.termination" style="width: 250px;">
<input type="hidden" id="node-input-terminationType">
</div>
<div class="form-row">
<label for="node-input-increment"><i class="fa fa-signal"></i> <span data-i18n="counter-loop.label.increment"></span></label>
<input type="text" id="node-input-increment" data-i18n="[placeholder]counter-loop.label.increment" style="width: 250px;">
<input type="hidden" id="node-input-incrementType">
</div>
<div class="form-tips">
<span>
Note: If you select message property, flow or global context property for the above fields,
if manipulate these data in the loop, Node-RED may fail due to an infinite loop.
</span>
</div>
</script>
<script type="text/x-red" data-help-name="counter-loop">
<p>Help flow looping the specified times.</p>
<p>If comparing counter variable to termination value in operator results in false, flow breaks out of loop and message is sent from the upper output port.</p>
<h3>Inputs</h3>
<dl>
<dt>Counter Variable</dt>
<dd>Define whether to use for counter variable. Set to msg property, flow context or global context. This property must be <code>null</code>, <code>undefined</code> or empty string when a flow reached this node for the first time. If not those, counter variable is not initialized.</dd>
<dt>Initialization</dt>
<dd>To initialize the counter variable with this value, set to number, msg property, flow context or global context.</dd>
<dt>Termination</dt>
<dd>If comparing this value to the counter variable results in true, flow is sent to the lower output port. Set to number, msg property, flow context or global context.</dd>
<dt>Increment</dt>
<dd>The counter variable is incremented by this value. Set to number, msg property, flow context or global context.</dd>
</dl>
<h3>Output</h3>
<p>If the comparison result is true, flow is sent to the lower output port ("true" label). If false, flow is sent to the upper output port ("false" label).</p>
<h3>Details</h3>
<p>When exiting the loop, counter variable can reset by setting to <code>null</code>, <code>undefined</code> or empty string. This is useful for multi-loop.</p>
<p>Please note if you select message property, flow or global context property for input fields, if manipulate these data in the loop, Node-RED may fail due to an infinite loop.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('counter-loop', {
category: 'function-loop',
defaults: {
name: {
value: ''
},
counter: {
value: '',
required: true
},
counterType: {
value: 'msg',
required: true
},
reset: {
value: false
},
resetValue: {
value: 'value-null',
required: true
},
initial: {
value: 0,
required: true
},
initialType: {
value: 'num',
},
operator: {
value: 'lt',
required: true
},
termination: {
value: '',
required: true
},
terminationType: {
value: 'num'
},
increment: {
value: 1,
required: true
},
incrementType: {
value: 'num'
}
},
color: "#e2d96e",
inputs: 1,
outputs: 2,
icon: "loop.png",
label: function() {
let operatorExpression;
switch (this.operator) {
case 'lt':
operatorExpression = '<';
break;
case 'lte':
operatorExpression = '<=';
break;
case 'gt':
operatorExpression = '>';
break;
case 'gte':
operatorExpression = '>=';
break;
case 'eq':
operatorExpression = '==';
break;
default:
RED.notify(this._('counter-loop.errors.invalidoperator'));
}
let relation = ' ' + operatorExpression + ' ';
let expression;
if (this.counter && this.termination) {
let termType = this.terminationType === 'num' ? '' : this.terminationType + '.';
expression = this.counterType + '.' + this.counter + relation + termType + this.termination;
}
return this.name || expression || 'counter-loop';
},
outputLabels: ['false', 'true'],
paletteLabel: 'counter-loop',
oneditprepare: function() {
if ($('#node-input-counter').val() === '') {
let n = this;
$('#node-input-counter').val('il' + n.id.replace(/[^a-zA-Z0-9]/g, ''));
}
$('#operator-select').val(this.operator);
$('#resetValue-select').val(this.resetValue);
$('#node-input-counter').typedInput({
types: ['msg', 'flow', 'global'],
typeField: $('#node-input-counterType')
});
$('#node-input-initial').typedInput({
types: ['num', 'msg', 'flow', 'global'],
typeField: $('#node-input-initialType')
});
$('#node-input-termination').typedInput({
types: ['num', 'msg', 'flow', 'global'],
typeField: $('#node-input-terminationType')
});
$('#node-input-increment').typedInput({
types: ['num', 'msg', 'flow', 'global'],
typeField: $('#node-input-incrementType')
});
$('#operator-select').on('change', function() {
let v = $(this).val();
$('#node-input-operator').val(v);
});
$('#resetValue-select').on('change', function() {
let v = $(this).val();
$('#node-input-resetValue').val(v);
});
}
});
</script>