-
Notifications
You must be signed in to change notification settings - Fork 2
/
map.html
731 lines (670 loc) · 30.4 KB
/
map.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
<!-- This is the config node for the mappings -->
<script type="text/x-red" data-template-name="map-config">
<div class="form-row">
<label for="node-config-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-config-input-lhsName"><i class="icon-tag"></i> LHS Name</label>
<input type="text" id="node-config-input-lhsName" placeholder="Left-hand side Name">
</div>
<div class="form-row">
<label for="node-config-input-rhsName"><i class="icon-tag"></i> RHS Name</label>
<input type="text" id="node-config-input-rhsName" placeholder="Right-hand side Name">
</div>
<div class="form-row node-config-input-mapping-container-row">
<ol id="node-config-input-mapping-container"></ol>
</div>
</script>
<script type="text/x-red" data-help-name="map-config">
<p>Configure mappings to be used by the various nodes provided by this plugin.</p>
<h3>Details</h3>
The following properties can be configured on this configuration node:
<ul>
<li><b>Name</b><br/>
The name of this configuration node; this can be used to differentiate between
different mapping configurations in your nodes. For example, if you have RfxCom
based flows, you could have a configuration named <i>RfxComIn</i> for mapping
input device id's to friendly names, and a configuration named <i>RfxComOut</i>
for mapping friendly device names to device id's.</li>
<li><b>LHS Name</b><br/>
The name of the left-hand side of the mappings. For example, if your left-hand
side of the mapping contains device id's (for input devices), you could set
this property to <i>Device id</i>. Likewise, if your left-hand side contains
device names (for output devices), you could set this property to
<i>Device name</i>.</li>
<li><b>Rhs Name</b><br/>
The name of the right-hand side of the mappings. For example, if your right-hand
side of the mapping contains device id's (for output devices), you could set this
property to <i>Device id</i>. Likewise, if your right-hand side contains device
names (for input devices), you could set this property to <i>Device name</i>.</li>
<li><b>Mappings</b><br/>
By using the <i>Add</i> and <i>Remove</i> buttons, you can modify the actual
mappings. For example, for the <i>RfxComIn</i> configuration, the left-hand
side of these mappings would contain the device id like 'AC/0x01010101/1',
and the right-hand side would contain the device name like 'Motion Sensor 1'.
Likewise, for the <i>RfxComOut</i> configuration, the left-hand side of these
mappings would contain the device name like 'light-entrance', and the right-hand
side would contain the device id like 'AC/0x02020202/1'.</li>
</ul>
</script>
<script type="text/javascript">
RED.mapFunctions = {
/**
* Find select by id based on given property name, determine current value based on
* given property name and node, then call updateLhsOrRhsSelect to update the select.
*/
updateLhsOrRhsSelectById: function(config, node, propertyName, readOnly) {
RED.mapFunctions.updateLhsOrRhsSelect($('#node-input-'+propertyName), config, !node ? null : node[propertyName], readOnly);
},
/**
* This function will update the given select as follows:
* - Remove all previous options
* - Append the LHS and RHS options, based on lhsName and rhsName from the given config
* - Mark these options as disabled (display:none) if readOnly flag is set
* - Select an option based on the given selectedValue
*/
updateLhsOrRhsSelect: function(select, config, selectedValue, readOnly) {
select.find('option').remove().end(); // Remove any old options
select.append($("<option/>", {
value: 'lhs',
text: !config?'lhs':config.lhsName,
css: readOnly ? {'display':'none'}:{}
}));
select.append($("<option/>", {
value: 'rhs',
text: !config?'rhs':config.rhsName,
css: readOnly ? {'display':'none'}:{}
}));
if ( selectedValue ) {
select.val(selectedValue);
}
},
/**
* Find select by id based on given property name, determine current value based on
* given property name and node, then call updateMappingSelect to update the select.
*/
updateMappingSelectById: function(config, node, propertyName) {
RED.mapFunctions.updateMappingSelect($('#node-input-'+propertyName), config, !node ? null : node[propertyName]);
},
/**
* This function will update the given select as follows:
* - Remove all previous options
* - Append an option for each mapping defined in the given config
* - Select an option based on the given selectedValue
*/
updateMappingSelect: function(select, config, selectedValue) {
if (config) {
select.find('option').remove().end(); // Remove any old options;
$.each(config.mappings, function(i, mapping) {
var $option = $("<option/>", {
value: mapping.lhs+"|"+mapping.rhs,
text: mapping.lhs+' - '+mapping.rhs
});
select.append($option);
});
if ( selectedValue ) {
select.val(selectedValue);
}
}
}
}
RED.nodes.registerType('map-config', {
category: 'config',
defaults: {
name: {value:"", required:true},
lhsName: {value:"lhs", required:true},
rhsName: {value:"rhs", required:true},
mappings: {value:[{lhs:"lhs", rhs:"rhs"}]}
},
label: function () {
return this.name;
},
oneditprepare: function() {
var node = this;
$("#node-config-input-mapping-container").css('min-height','250px').css('min-width','450px').editableList({
addItem: function(container,i,opt) {
var mapping = opt.mapping || {lhs:node.lhsName, rhs:node.rhsName};
container.css({
overflow: 'hidden',
whiteSpace: 'nowrap'
});
var row = $('<div/>').appendTo(container);
var lhsField = $('<input/>',{class:"node-config-input-mapping-lhs",type:"text",style:"width:40%; margin-left: 5px;"}).appendTo(row).val(mapping.lhs);
var rhsField = $('<input/>',{class:"node-config-input-mapping-rhs",type:"text",style:"width:40%; margin-left: 5px;"}).appendTo(row).val(mapping.rhs);
},
sortable: true,
removable: true
});
for (var i=0;i<node.mappings.length;i++) {
var mapping = node.mappings[i];
$("#node-config-input-mapping-container").editableList('addItem',{mapping:mapping,i:i});
}
},
oneditsave: function() {
var mappings = $("#node-config-input-mapping-container").editableList('items');
var node = this;
node.mappings = [];
mappings.each(function(i) {
var mapping = $(this);
var r = {};
r.lhs = mapping.find(".node-config-input-mapping-lhs").val();
r.rhs = mapping.find(".node-config-input-mapping-rhs").val();
node.mappings.push(r);
});
},
oneditresize: function(size) {
var rows = $("#dialog-form>div:not(.node-config-input-mapping-container-row)");
var height = size.height;
for (var i=0;i<rows.size();i++) {
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-config-input-mapping-container-row");
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
$("#node-config-input-mapping-container").editableList('height',height);
}
});
</script>
<!-- This node allows for mapping message properties -->
<script type="text/x-red" data-template-name="map-map">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row node-input-config">
<label for="node-input-config"><i class="icon-bullhorn"></i> Config</label>
<input type="text" id="node-input-config">
</div>
<hr/>
<div class="form-row">
<label for="node-input-inLhsOrRhs"><i class="fa fa-ellipsis-h"></i> Get</label>
<select id="node-input-inLhsOrRhs"></select>
</div>
<div class="form-row">
<label for="node-input-in"><i class="fa fa-ellipsis-h"></i> From</label>
<input type="text" id="node-input-in" placeholder="payload">
<input type="hidden" id="node-input-inType">
</div>
<hr/>
<div class="form-row">
<label for="node-input-outLhsOrRhs"><i class="fa fa-ellipsis-h"></i> Set</label>
<select id="node-input-outLhsOrRhs" style="background: #eee;cursor:no-drop;"></select>
</div>
<div class="form-row">
<label for="node-input-out"><i class="fa fa-ellipsis-h"></i> On</label>
<input type="text" id="node-input-out" placeholder="payload">
<input type="hidden" id="node-input-outType">
</div>
<hr/>
<div class="form-row">
<input type="checkbox" id="node-input-caseInsensitive" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-caseInsensitive" style="width:90%"><i class="fa fa-tag"></i> Ignore Case</label>
</div>
<div class="form-row">
<input type="checkbox" id="node-input-forwardIfNoMatch" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-forwardIfNoMatch" style="width:90%"><i class="fa fa-tag"></i> Forward message if no matching mapping is found</label>
</div>
<div class="form-row" id="row-defaultIfNoMatch">
<label for="node-input-defaultIfNoMatch" style="padding-left:20px"><i class="icon-bullhorn"></i> Output value</label>
<input type="text" id="node-input-defaultIfNoMatch">
</div>
</script>
<!-- Help text for map -->
<script type="text/x-red" data-help-name="map-map">
<p>Map a message input property to a message output property based on the configured mappings.</p>
<p>
For example, configured with a mapping between device id's and corresponding
device names, for each incoming message this node can look up the device id
from a configurable message property, and add the corresponding device name
to the outgoing message.
</p>
<h3>Details</h3>
The following properties can be configured on this node:
<ul>
<li><b>Name</b><br/>
The name of this node.</li>
<li><b>Config</b><br/>
The mapping configuration to use.</li>
<li><b>Get</b><br/>
Specify whether the incoming message contains values from the left-hand side or
right-hand side of the configured mappings.
The options in this select box show the <i>LHS Name</i> and <i>RHS Name</i> as
defined in the mapping configuration, for example allowing you to select whether
the incoming message contains a 'device id' or 'device name'.</li>
<li><b>From</b><br/>
Specify from which property on the incoming message to retrieve the value to be
mapped. For example, this could contain something like <code>msg.topic</code>,
<code>msg.topicName</code>, <code>msg.payload</code>, <code>msg.deviceId</code>,
or <code>msg.deviceName</code>.</li>
<li><b>Set</b><br/>
Specify whether the left-hand side or right-hand side of the configured mappings
should be added to the outgoing message. You cannot change this setting;
if <i>Get</i> is configured to use the left-hand side of the mapping, then this
property will be set to the right-hand side of the mapping, and vice versa.</li>
<li><b>On</b><br/>
Specify to which property on the outgoing message the mapped value should be written.
For example, this could contain something like <code>msg.topic</code>,
<code>msg.topicName</code>, <code>msg.payload</code>, <code>msg.deviceId</code>,
or <code>msg.deviceName</code>.</li>
<li><b>Ignore Case</b><br/>
If enabled, the node will use a case-insensitive look-up of the incoming message
property value in the configured mappings. For example, an incoming message
containing 'LIGHT' as the device name will be matched against a mapping containing
'light' as the device name if this setting is enabled, but will not be matched if
this setting is disabled.</li>
<li><b>Forward message if no matching mapping is found</b><br/>
If disabled, no message will be forwarded to subsequent nodes if no mapping can be found
for a given input message value. If enabled, you can configure a default value to
be added to the outgoing message in case no mapping can be found. </li>
<li><b>Output Value</b><br/>
In case no mapping can be found for a given input message value, this property defined
the value to be written to the outgoing message. This default value can reference
message properties using Mustache template syntax, for example <i>Unknown: {{{topic}}}</i>.
This value will be written to the outgoing message property defined in the <b>On</b> configuration
option.</li>
</ul>
</script>
<script type="text/javascript">
RED.nodes.registerType('map-map', {
category: 'mappings',
paletteLabel: 'map',
defaults: {
name: {value:""},
config: {value:"", type:"map-config", required:true},
in: {value:"payload", validate:RED.validators.typedInput("inType"), required:true},
inType: {value:"msg", required:true},
inLhsOrRhs: {value:"lhs", required:true},
out: {value:"payload", validate:RED.validators.typedInput("outType"), required:true},
outType: {value:"msg", required:true},
outLhsOrRhs: {value:"rhs", required:true},
caseInsensitive: {value:true},
forwardIfNoMatch: {value:true},
defaultIfNoMatch: {value:"Unknown: {{{payload}}}"}
},
color:"#E2D96E",
inputs:1,
outputs:1,
icon: "swap.png",
label: function () {
return this.name || "Map";
},
labelStyle: function () {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function() {
var node = this;
if (!node.in) {
node.in = 'payload';
$("#node-input-in").val("payload");
}
if (!node.inType) {
node.inType = 'msg';
}
if (!node.inLhsOrRhs) {
node.inLhsOrRhs = 'lhs';
}
$("#node-input-in").typedInput({
default: 'msg',
types: ['msg'],
typeField: $("#node-input-inType")
});
if (!node.out) {
node.out = 'payload';
$("#node-input-out").val("payload");
}
if (!node.outType) {
node.outType = 'msg';
}
$("#node-input-out").typedInput({
default: 'msg',
types: ['msg'],
typeField: $("#node-input-outType")
});
$('#node-input-inLhsOrRhs').change(function() {
$('#node-input-outLhsOrRhs').val( $(this).val()==='rhs' ? 'lhs' : 'rhs' );
});
$("#node-input-config").change(function() {
var config = RED.nodes.node($(this).val());
RED.mapFunctions.updateLhsOrRhsSelectById(config, node, "inLhsOrRhs", false);
RED.mapFunctions.updateLhsOrRhsSelectById(config, node, "outLhsOrRhs", true);
$('#node-input-inLhsOrRhs').change();
});
$("#node-input-config").change();
$("#node-input-forwardIfNoMatch").change(function() {
if ( $(this).is(":checked") ) {
$("#row-defaultIfNoMatch").show();
} else {
$("#row-defaultIfNoMatch").hide();
}
});
$("#node-input-forwardIfNoMatch").change();
$('#node-input-in').data('prev-val', $('#node-input-in').val());
$('#node-input-in').change(function(){
var prev = $(this).data('prev-val');
var current = $(this).val();
var defaultIfNoMatchElt = $("#node-input-defaultIfNoMatch");
if (defaultIfNoMatchElt.val()==='Unknown: {{{'+prev+'}}}') {
defaultIfNoMatchElt.val('Unknown: {{{'+current+'}}}');
}
$('#node-input-in').data('prev-val', current);
});
}
});
</script>
<!-- This node allows for setting a message property based on a given mapping -->
<script type="text/x-red" data-template-name="map-set">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row node-input-config">
<label for="node-input-config"><i class="icon-bullhorn"></i> Config</label>
<input type="text" id="node-input-config">
</div>
<hr/>
<div class="form-row">
<label for="node-input-from"><i class="fa fa-ellipsis-h"></i> From</label>
<select id="node-input-from" style="width:250px;"></select>
</div>
<div class="form-row">
<label for="node-input-outLhsOrRhs"><i class="fa fa-ellipsis-h"></i> Set</label>
<select id="node-input-outLhsOrRhs" style="width:250px;"></select>
</div>
<div class="form-row">
<label for="node-input-out"><i class="fa fa-ellipsis-h"></i> On</label>
<input type="text" id="node-input-out" placeholder="payload" style="width:250px;">
<input type="hidden" id="node-input-outType">
</div>
</script>
<!-- Help text for map -->
<script type="text/x-red" data-help-name="map-set">
<p>Set a message output property based on the configured mapping.</p>
<p>
For example, configured with a mapping between device id's and corresponding
device names, this node can add either the device id or device name for a
specific device to every incoming message.
</p>
<p>
Within a flow, this allows for selecting a specific device from the list of
configured mappings, for example to send a command to that specific device.
</p>
<h3>Details</h3>
The following properties can be configured on this node:
<ul>
<li><b>Name</b><br/>
The name of this node.</li>
<li><b>Config</b><br/>
The mapping configuration to use.</li>
<li><b>From</b><br/>
Select the mapping from which to retrieve either the right-hand side or left-hand side
value to be added to the outgoing message.</li>
<li><b>Set</b><br/>
Specify whether the left-hand side or right-hand side of the selected mapping should
be added to the outgoing message. For example, given the example above,
this property allows for specifying whether you want to add either the <i>device id</i>
or <i>device name</i> to the outgoing message.</li>
<li><b>On</b><br/>
Specify to which property on the outgoing message the value should be written.
For example, this could contain something like <code>msg.topic</code>,
<code>msg.topicName</code>, <code>msg.payload</code>, <code>msg.deviceId</code>,
or <code>msg.deviceName</code>.</li>
</ul>
</script>
<script type="text/javascript">
RED.nodes.registerType('map-set', {
category: 'mappings',
paletteLabel: 'set',
defaults: {
name: {value:""},
config: {value:"", type:"map-config", required:true},
from: {value:"", required:true},
out: {value:"payload", validate:RED.validators.typedInput("outType"), required:true},
outType: {value:"msg", required:true},
outLhsOrRhs: {value:"rhs", required:true}
},
color:"#E2D96E",
inputs:1,
outputs:1,
icon: "swap.png",
label: function () {
return this.name || "Set";
},
labelStyle: function () {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function() {
var node = this;
if (!node.out) {
node.out = 'payload';
$("#node-input-out").val("payload");
}
if (!node.outType) {
node.outType = 'msg';
}
if (!node.outLhsOrRhs) {
node.outLhsOrRhs = 'rhs';
}
$("#node-input-out").typedInput({
default: 'msg',
types: ['msg'],
typeField: $("#node-input-outType")
});
$("#node-input-config").change(function() {
var config = RED.nodes.node($(this).val());
RED.mapFunctions.updateLhsOrRhsSelectById(config, node, "outLhsOrRhs", false);
RED.mapFunctions.updateMappingSelectById(config, node, "from");
});
$("#node-input-config").change();
}
});
</script>
<!-- This node provides a switch implementation for a given mapping configuration -->
<script type="text/x-red" data-template-name="map-switch">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
<input type="hidden" id="node-input-outputs"/>
</div>
<div class="form-row node-input-config">
<label for="node-input-config"><i class="icon-bullhorn"></i> Config</label>
<input type="text" id="node-input-config">
</div>
<hr/>
<div class="form-row">
<label for="node-input-inLhsOrRhs"><i class="fa fa-ellipsis-h"></i> Get</label>
<select id="node-input-inLhsOrRhs"></select>
</div>
<div class="form-row">
<label for="node-input-in"><i class="fa fa-ellipsis-h"></i> From</label>
<input type="text" id="node-input-in" placeholder="payload">
<input type="hidden" id="node-input-inType">
</div>
<hr/>
<div class="form-row">
<label for="node-input-outLhsOrRhs"><i class="fa fa-ellipsis-h"></i> Output Name</label>
<select id="node-input-outLhsOrRhs"></select>
</div>
<hr/>
<div class="form-row node-input-switch-container-row">
<ol id="node-input-switch-container"></ol>
</div>
<div class="form-row">
<input type="checkbox" id="node-input-caseInsensitive" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-caseInsensitive" style="width:90%"><i class="fa fa-tag"></i> Ignore Case</label>
</div>
</script>
<!-- Help text for map -->
<script type="text/x-red" data-help-name="map-switch">
<p>Route messages by matching an input message property against the configured mappings.</p>
<p>
For example, configured with a mapping between device id's and corresponding
device names, this node can route incoming messages to different outputs, based
on the device id (or device name) specified in the incoming message.
<p>
<h3>Details</h3>
The following properties can be configured on this node:
<ul>
<li><b>Name</b><br/>
The name of this node.</li>
<li><b>Config</b><br/>
The mapping configuration to use.</li>
<li><b>Get</b><br/>
Specify whether the incoming message contains values from the left-hand side or
right-hand side of the configured mappings.
The options in this select box show the <i>LHS Name</i> and <i>RHS Name</i> as
defined in the mapping configuration, for example allowing you to select whether
the incoming message contains a 'device id' or 'device name'.</li>
<li><b>From</b><br/>
Specify from which property on the incoming message to retrieve the value on which
the switch should be performed. For example, this could contain something like
<code>msg.topic</code>, <code>msg.topicName</code>, <code>msg.payload</code>,
<code>msg.deviceId</code>, or <code>msg.deviceName</code>.</li>
<li><b>Output Name</b><br/>
Specify whether the switch output label (as shown in the flow editor) should show
the left-hand side or right-hand side of the configured mapping. Given the
example above, the output label could show either the device id or device name.</li>
<li><b>Switch List</b><br/>
By using the <i>Add</i> and <i>Remove</i> buttons, you can add switch outputs
matching the given mapping.</li>
<li><b>Ignore Case</b><br/>
If enabled, the node will use a case-insensitive look-up of the incoming message
property value in the configured mappings. For example, an incoming message
containing 'LIGHT' as the device name will be matched against a mapping containing
'light' as the device name if this setting is enabled, but will not be matched if
this setting is disabled.</li>
</ul>
</script>
<script type="text/javascript">
RED.nodes.registerType('map-switch', {
category: 'mappings',
paletteLabel: 'switch',
defaults: {
name: {value:""},
config: {value:"", type:"map-config", required:true},
outLhsOrRhs: {value:"rhs", required:true},
in: {value:"payload", validate:RED.validators.typedInput("inType"), required:true},
inType: {value:"msg", required:true},
inLhsOrRhs: {value:"lhs", required:true},
caseInsensitive: {value:true},
switchValues: {value:[]},
outputs: {value:1}
},
color:"#E2D96E",
inputs:1,
outputs:1,
icon: "switch.png",
label: function () {
return this.name || "Switch";
},
labelStyle: function () {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function() {
var node = this;
if (!node.in) {
node.in = 'payload';
$("#node-input-in").val("payload");
}
if (!node.inType) {
node.inType = 'msg';
}
if (!node.inLhsOrRhs) {
node.inLhsOrRhs = 'lhs';
}
$("#node-input-in").typedInput({
default: 'msg',
types: ['msg'],
typeField: $("#node-input-inType")
});
if (!node.outLhsOrRhs) {
node.outLhsOrRhs = 'rhs';
}
$("#node-input-out").typedInput({
default: 'msg',
types: ['msg'],
typeField: $("#node-input-outType")
});
$("#node-input-switch-container").css('min-height','250px').css('min-width','450px').editableList({
addItem: function(container,i,opt) {
var config = RED.nodes.node($("#node-input-config").val());
var mappings = config.mappings || [];
var switchValue = opt.switchValue;
container.css({
overflow: 'hidden',
whiteSpace: 'nowrap'
});
var row = $('<div/>').appendTo(container);
var select = $('<select/>',{class:"node-input-switch-select",type:"text",style:"width:80%; margin-left: 5px;"}).appendTo(row);
RED.mapFunctions.updateMappingSelect(select, config, switchValue);
var finalspan = $('<span/>',{style:"float: right;margin-top: 6px;"}).appendTo(row);
finalspan.append(' → <span class="node-config-input-mapping-index">'+(i+1)+'</span> ');
},
removeItem: function(item) {
var mappingRows = $("#node-input-switch-container").editableList('items');
mappingRows.each(function(i,item) {
item.find(".node-config-input-mapping-index").html(i+1);
});
},
sortItems: function(items) {
items.each(function(i,item) {
item.find(".node-config-input-mapping-index").html(i+1);
});
},
sortable: true,
removable: true
});
$("#node-input-config").change(function() {
var config = RED.nodes.node($(this).val());
RED.mapFunctions.updateLhsOrRhsSelectById(config, node, "inLhsOrRhs", false);
RED.mapFunctions.updateLhsOrRhsSelectById(config, node, "outLhsOrRhs", false);
if (config) {
if (node.switchValues) {
var container = $("#node-input-switch-container");
container.editableList('empty');
for (var i=0;i<node.switchValues.length;i++) {
var switchValue = node.switchValues[i];
container.editableList('addItem',{switchValue:switchValue,i:i});
}
}
}
});
$("#node-input-config").change();
},
oneditsave: function() {
var outputsContainer = $("#node-input-switch-container").editableList('items');
var node = this;
node.switchValues = [];
outputsContainer.each(function(i) {
var value = $(this).find(".node-input-switch-select").val();
node.switchValues.push(value);
});
$("#node-input-outputs").val(node.switchValues.length);
},
oneditresize: function(size) {
var rows = $("#dialog-form>div:not(.node-input-switch-row)");
var height = size.height;
for (var i=0;i<rows.size();i++) {
height -= $(rows[i]).outerHeight(true);
}
var editorRow = $("#dialog-form>div.node-input-switch-container-row");
height -= (parseInt(editorRow.css("marginTop"))+parseInt(editorRow.css("marginBottom")));
$("#node-input-switch-container").editableList('height',height);
},
outputLabels: function(index) {
var node = this;
var switchValue = node&&node.switchValues ? node.switchValues[index] : null;
var label = "";
if (switchValue) {
var config = RED.nodes.node(node.config);
var mappings = config.mappings || [];
var mapping = mappings.find(function(mapping) {
return mapping.lhs+'|'+mapping.rhs===switchValue;
});
if (mapping) {
label = mapping[node.outLhsOrRhs];
}
return label;
}
},
});
</script>