-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcounter-loop.js
131 lines (121 loc) · 4.69 KB
/
counter-loop.js
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
module.exports = function (RED) {
'use strict';
function setProperty(node, msg, name, type, value) {
if (type === 'msg') {
msg[name] = value;
} else if (type === 'flow') {
node.context().flow.set(name, value);
} else if (type === 'global') {
node.context().global.set(name, value);
}
}
function sendErrorMessage(node, text) {
node.status({
fill: 'red',
shape: 'ring',
text: text
});
node.error(text, msg);
}
function CounterLoopNode(n) {
RED.nodes.createNode(this, n);
var node = this;
node.counter = n.counter;
node.counterType = n.counterType;
node.initial = n.initial;
node.initialType = n.initialType;
node.operator = n.operator;
node.termination = n.termination;
node.terminationType = n.terminationType;
node.increment = n.increment;
node.incrementType = n.incrementType;
node.reset = n.reset;
node.resetValue = n.resetValue;
this.on('input', function (msg) {
let counter = RED.util.evaluateNodeProperty(node.counter, node.counterType, node, msg);
let initial = RED.util.evaluateNodeProperty(node.initial, node.initialType, node, msg);
let termination = RED.util.evaluateNodeProperty(node.termination, node.terminationType, node, msg);
let increment = RED.util.evaluateNodeProperty(node.increment, node.incrementType, node, msg);
if (typeof initial !== 'number') {
let text = RED._('counter-loop.errors.initialnotnumber') + initial;
sendErrorMessage(node, text);
}
if (typeof termination !== 'number') {
let text = RED._('counter-loop.errors.terminationnotnumber') + termination;
sendErrorMessage(node, text);
}
if (typeof increment !== 'number') {
let text = RED._('counter-loop.errors.incrementnotnumber') + increment;
sendErrorMessage(node, text);
}
if (counter === void 0 || counter === null || counter === '') {
// initialize
counter = initial;
} else {
counter += increment;
}
setProperty(node, msg, node.counter, node.counterType, counter);
setProperty(node, msg, node.initial, node.initialType, initial);
setProperty(node, msg, node.termination, node.terminationType, termination);
setProperty(node, msg, node.increment, node.incrementType, increment);
let isLoop = false;
switch (node.operator) {
case 'lt':
isLoop = counter < termination;
break;
case 'lte':
isLoop = counter <= termination;
break;
case 'gt':
isLoop = counter > termination;
break;
case 'gte':
isLoop = counter >= termination;
break;
case 'eq':
isLoop = counter == termination;
break;
default:
sendErrorMessage(node, RED._('counter-loop.errors.invalidoperator'));
}
if (isLoop) {
// run in loop
node.status({
fill: 'blue',
shape: 'dot',
text: 'loop'
});
node.send([null, msg]);
} else {
// exit loop
if (node.reset) {
let resetValue = null;
switch (node.resetValue) {
case 'value-null':
resetValue = null;
break;
case 'value-undefined':
resetValue = undefined;
break;
case 'value-empty':
resetValue = '';
break;
default:
// not come here
}
setProperty(node, msg, node.counter, node.counterType, resetValue);
}
node.status({
fill: 'grey',
shape: 'ring',
text: 'exit loop'
});
node.send(msg);
}
});
this.on('close', function () {
node.status({});
});
}
RED.nodes.registerType('counter-loop', CounterLoopNode);
}