-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathrange.js
75 lines (64 loc) · 2.39 KB
/
range.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
exports.id = 'range';
exports.title = 'Range';
exports.group = 'Logic';
exports.color = '#ffa824';
exports.version = '1.0.0';
exports.icon = 'arrows-h';
exports.input = true;
exports.output = 1;
exports.author = 'Martin Smola';
exports.options = { property: '' };
exports.html = `
<div class="padding">
<div data-jc="textbox" data-jc-path="property" data-jc-config="placeholder:@(e.g. path.to.property)" class="m">Property (unless the data is the value itself)</div>
<div class="row">
<div class="col-md-3">
<div data-jc="textbox" data-jc-path="input_min" data-jc-config="type:number;increment:true;align:center" class="m">@(Input min (defaults to 0))</div>
</div>
<div class="col-md-3">
<div data-jc="textbox" data-jc-path="input_max" data-jc-config="type:number;increment:true;align:center" class="m">@(Input max (defaults to 1023))</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div data-jc="textbox" data-jc-path="output_min" data-jc-config="type:number;increment:true;align:center" class="m">@(Output min (defaults to 0))</div>
</div>
<div class="col-md-3">
<div data-jc="textbox" data-jc-path="output_max" data-jc-config="type:number;increment:true;align:center" class="m">@(Output max (defaults to 100))</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div data-jc="checkbox" data-jc-path="round" class="m">@(Round output?)</div>
</div>
</div>
</div>
`;
exports.readme = `
# Range
`;
exports.install = function(instance) {
instance.on('data', function(flowdata) {
var options = instance.options;
var val = parseFloat(typeof(flowdata.data) === 'object' ? U.get(flowdata.data, options.property) : flowdata.data);
if (!val || typeof val !== 'number') {
instance.error('Value is not a number');
return;
}
if (val < options.input_min) {
instance.error('Input smaller then minimal input');
val = options.input_min;
}
if (val > options.input_max) {
instance.error('Input bigger then maximal input');
val = options.input_max;
}
var inmin = parseFloat(options.input_min || 0);
var inmax = parseFloat(options.input_max || 1023);
var outmin = parseFloat(options.output_min || 0);
var outmax = parseFloat(options.output_max || 100);
var data = outmin + (outmax - outmin) * (val - inmin) / (inmax - inmin);
flowdata.data = options.round ? Math.round(data) : data;
instance.send2(flowdata);
});
};