-
Notifications
You must be signed in to change notification settings - Fork 8
/
mcu_plugin.html
2212 lines (1860 loc) · 79.8 KB
/
mcu_plugin.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
/*
node-red-mcu-plugin by @ralphwetzel
https://github.com/ralphwetzel/node-red-mcu-plugin
License: MIT
*/
-->
<!-- Additional styles for red-ui-tabs-bottom -->
<style>
.red-ui-tabs.red-ui-tabs-bottom {
border-bottom: 1px solid var(--red-ui-tab-background-active);
}
.red-ui-tabs.red-ui-tabs-bottom ul {
border-top: 1px solid var(--red-ui-primary-border-color);
}
.red-ui-tabs.red-ui-tabs-bottom ul li {
margin: 0 3px 3px 3px;
top: -1px;
border-bottom: 1px solid var(--red-ui-primary-border-color);
}
.red-ui-tabs.red-ui-tabs-bottom ul li.active{
border-top: 1px solid var(--red-ui-tab-background-active);
}
.red-ui-tab-button.red-ui-tabs-bottom {
border-top: 1px solid var(--red-ui-primary-border-color);
border-bottom: 1px solid var(--red-ui-tab-background-active);
}
.mcu-build-option-label {
display: inline-block;
min-width: 140px;
}
.mcu-build-button {
height: 33px;
line-height: 20px;
font-size: 14px;
background: rgb(7, 48, 112);
color: white !important;
box-sizing: border-box;
display: inline-block;
border: 1px solid var(--red-ui-form-input-border-color);
text-align: center;
margin:0;
cursor:pointer;
}
.mcu-build-button :focus {
outline: none;
}
.mcu-build-button-button {
border-right: none;
padding-right: 12px;
padding-left: 12px;
}
.mcu-build-button-select {
line-height: 32px;
vertical-align: middle;
padding: 0px 8px;
border-left: none;
}
.mcu-build-button-select:focus {
outline: none;
}
.attention {
border-color: transparent;
outline: solid rgb(7, 48, 112) 2px;
}
.mcu-build-progress-bar {
background: rgb(7, 48, 112);
width: 0px;
height: 2px;
margin-top: 0px;
margin-left: 4px;
margin-right: 4px;
margin-bottom: 2px;
max-width: calc(100% - 8px);
}
#mcu_console::-webkit-scrollbar {
display: none;
}
</style>
<script type="text/javascript" src="resources/@ralphwetzel/node-red-mcu-plugin/lib/flashTab.js"></script>
<script type="module">
import {ESPLoader, Transport} from "./resources/@ralphwetzel/node-red-mcu-plugin/lib/espressif.js";
globalThis._mcu = {};
globalThis._mcu.ESPLoader = ESPLoader;
globalThis._mcu.Transport = Transport;
</script>
<script type="text/javascript">
let script = document.createElement('script');
script.onload = function () {
let script = document.createElement('script');
script.src = "resources/@ralphwetzel/node-red-mcu-plugin/lib/xsserial.js";
document.head.appendChild(script);
};
script.src = "resources/@ralphwetzel/node-red-mcu-plugin/lib/xsbug.js";
document.head.appendChild(script);
</script>
<script type="text/javascript">
(function() {
var sidebarContent;
var treeList;
var objects = {};
let flowData = [];
let flowList = flowData;
let flowSelector;
let panels;
let tabs;
let tabsTop;
let console_view;
let config_view;
let buildPanelHeader;
let tabsContainer;
let selectorPanel;
let selectorPanelTipStamp = 0;
let buildPanel;
let progress_bar;
function show() {
RED.sidebar.show("node-red-mcu");
}
function onFlowAdd(ws) {
objects[ws.id] = {
id: ws.id,
element: getFlowLabel(ws),
// children:[],
deferBuild: true,
icon: "red-ui-icons red-ui-icons-flow",
gutter: getGutter(ws),
checkbox: true,
selected: (ws._mcu?.mcu == true) ?? false
}
flowData.push(objects[ws.id]);
if (flowSelector) {
flowSelector.treeList('data', flowData);
}
objects[ws.id].element.toggleClass("red-ui-info-outline-item-disabled", !!ws.disabled)
objects[ws.id].treeList.container.toggleClass("red-ui-info-outline-item-disabled", !!ws.disabled)
}
function onFlowChange(n) {
var existingObject = objects[n.id];
var label = n.label || n.id;
var newlineIndex = label.indexOf("\\n");
if (newlineIndex > -1) {
label = label.substring(0,newlineIndex)+"...";
}
existingObject.element.find(".red-ui-info-outline-item-label").text(label);
existingObject.element.toggleClass("red-ui-info-outline-item-disabled", !!n.disabled)
existingObject.treeList.container.toggleClass("red-ui-info-outline-item-disabled", !!n.disabled)
}
function onFlowsReorder(order) {
flowData = [];
for (let i=0; i<order.length; i+=1) {
flowData.push(objects[order[i]]);
}
if (flowSelector) {
flowSelector.treeList('data', flowData);
}
}
function getFlowLabel(n) {
var div = $('<div>',{class:"red-ui-info-outline-item red-ui-info-outline-item-flow"});
var contentDiv = $('<div>',{class:"red-ui-search-result-description red-ui-info-outline-item-label"}).appendTo(div);
var label = (typeof n === "string")? n : n.label;
var newlineIndex = label.indexOf("\\n");
if (newlineIndex > -1) {
label = label.substring(0,newlineIndex)+"...";
}
contentDiv.text(label);
return div;
}
function getGutter(n) {
var span = $("<span>",{class:"red-ui-info-outline-gutter red-ui-treeList-gutter-float"});
return span;
}
var empties = {};
function getEmptyItem(id) {
var item = {
empty: true,
element: $('<div class="red-ui-info-outline-item red-ui-info-outline-item-empty">').text(RED._("sidebar.info.empty")),
}
empties[id] = item;
return item;
}
function getNodeLabel(n) {
var div = $('<div>',{class:"red-ui-node-list-item red-ui-info-outline-item"});
RED.utils.createNodeIcon(n, true).appendTo(div);
div.find(".red-ui-node-label").addClass("red-ui-info-outline-item-label")
addControls(n, div);
return div;
}
function onObjectRemove(n) {
var existingObject = objects[n.id];
existingObject.treeList.remove();
delete objects[n.id]
}
function resize_sidebar(top, bottom) {
let header_height = buildPanelHeader.outerHeight();
if (selectorPanel) {
top = top || $(selectorPanel).outerHeight();
}
if (buildPanel) {
bottom = bottom || $(buildPanel).outerHeight();
}
if (tabs) {
tabs.resize();
}
if (config_view) {
let tabs_height = tabs ? $(tabs).height : 0;
config_view.editableList("width", $(config_view).width());
let cvh = bottom - 88;
config_view.editableList("height", cvh < 100 ? 100 : cvh);
if (cvh < 100) {
tabsContainer.css({"top": "147px"});
} else {
tabsContainer.css({"top": "calc(100% - 36px)"});
}
}
}
function rand_id(prefix) {
return Math.random().toString(36).replace('0.', prefix || '');
}
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
let build_config = [];
let build_config_default = {
platform: "sim/moddable_one",
port: "",
more: false,
buildmode: "0", // EXPERIMENTAL
buildtarget: "all",
debug: true,
debugtarget: "0",
release: false,
arguments: "{}",
creation: "{}",
ssid: "",
password: "",
ui: false,
pixel: "rgb565le",
rotation: "0",
cll: 4096, // CommandListLength
dll: 8192, // DisplayListLength
tc: 1, // TouchCount
px: 240, // Size of render buffer;
py: 32 // see: https://github.com/phoddie/node-red-mcu/discussions/27#discussioncomment-4317840
}
function addConfig() {
let c = {};
fill_config_if_missing(c);
build_config.push(c);
return c;
}
// returns "true" if config was altered!
function fill_config_if_missing(config) {
let changed = false;
if (!("id" in config)) {
let id = rand_id();
let unique = true;
do {
for (let i=0; i<build_config.length; i+=1) {
if (build_config[i] && build_config[i].id && build_config[i].id == id) {
unique = false;
break;
}
}
} while (unique == false)
config["id"] = id;
changed = true;
}
// manual clone...
for (let k in build_config_default) {
if (!(k in config)) {
config[k] = build_config_default[k];
changed = true;
}
}
// We omit the check here to ensure that config is restricted to the same properties as build_config_default.
// Perhaps at one time someone tries to patch in functionality ... and might consider this helpful!
return changed;
}
let mcu_plugin_config = {
"platforms": [],
"ports": [],
"experimental": 0
};
/******
* Handling class for the progress bar div
******/
class ProgressBar {
#bar;
#state;
#width;
constructor(el) {
this.#bar = el;
this.#state = true;
this.#width = 0;
}
reset() {
this.#state = true;
this.set(0);
}
stop() {
this.set(0);
this.#state = false;
}
ping() {
if (this.#width < 100) {
this.set(this.#width >= 15 ? 1 : this.#width + 1);
}
}
set(percent) {
this.#width = percent;
if (this.#state === true) {
let bar = $(this.#bar);
bar.css({"width": `${this.#width}%`});
}
}
}
RED.editor.registerEditPane("editor-tab-manifest", function(node) {
return {
label: "MCU: manifest.json",
name: "manifest.json",
iconClass: "fa fa-microchip",
create: function(container) {
this.editor = buildManifestForm(container,node);
},
resize: function(size) {
this.editor.resize();
},
close: function() {
this.editor.destroy();
this.editor = null;
},
show: function() {
this.editor.focus();
},
apply: function(editState) {
let new_manifest = this.editor.getValue();
if (node._mcu?.manifest) {
// Has existing manifest property
if (new_manifest.trim() === "") {
// New value is blank - remove the property
editState.changed = true;
editState.changes._mcu = structuredClone(node._mcu);
delete node._mcu.manifest;
} else if (node._mcu.manifest !== new_manifest) {
// New value is different
editState.changed = true;
editState.changes._mcu = structuredClone(node._mcu);
node._mcu.manifest = new_manifest;
}
} else {
// No existing manifest
if (new_manifest.trim() !== "") {
editState.changed = true;
editState.changes._mcu = structuredClone(node._mcu);
node._mcu.manifest = new_manifest;
}
}
}
};
}, function(node) {
return (node.type === "tab" && node._mcu?.mcu === true);
});
function buildManifestForm(container,node) {
var dialogForm = $('<form class="dialog-form form-horizontal" autocomplete="off"></form>').appendTo(container);
var toolbarRow = $('<div></div>').appendTo(dialogForm);
var row = $('<div class="form-row node-text-editor-row" style="position:relative; padding-top: 4px; height: 100%"></div>').appendTo(dialogForm);
var editorId = "node-info-input-info-editor-"+Math.floor(1000*Math.random());
$('<div style="height: 100%" class="node-text-editor" id="'+editorId+'" ></div>').appendTo(row);
var nodeManifestEditor = RED.editor.createEditor({
id: editorId,
mode: 'ace/mode/json',
stateId: RED.editor.generateViewStateId("node", node, "manifest"),
value: node._mcu?.manifest || ""
});
node.manifest = nodeManifestEditor;
return nodeManifestEditor;
}
RED.plugins.registerPlugin("node-red-mcu", {
onadd: function() {
// We use this to check if the runtime side of node-re-mcu-plugin
// is up & running. If not, we'll cancel registerPlugin!
new Promise((resolve, reject) => {
$.get({
url: "mcu/config/plugin",
contentType: "application/json",
success: function (response) {
resolve(response);
},
error: function (error) {
reject(error);
}
});
})
.then((data) => {
let incoming_config; // {targets: [{...}]}
try {
incoming_config = JSON.parse(data);
} catch (err) {
console.error("node-red-mcu-plugin: Failed to parse JSON config data. Setup canceled.")
return;
}
for (key in mcu_plugin_config) {
if (key in incoming_config) {
mcu_plugin_config[key] = incoming_config[key];
}
}
try {
_onAdd(mcu_plugin_config.experimental);
}
catch(error) {
console.error(error);
}
})
.catch((error) => {
console.log(error);
console.error("node-red-mcu-plugin: Runtime module not responding. Setup canceled.")
})
return;
},
onremove: function() {
// this is incomplete!
RED.events.off("flows:add", onFlowAdd)
RED.events.off("flows:remove", onObjectRemove)
RED.events.off("flows:change", onFlowChange)
RED.events.off("flows:reorder", onFlowsReorder)
RED.sidebar.removeTab("node-red-mcu");
}
})
function _onAdd(MCU_EXPERIMENTAL) {
MCU_EXPERIMENTAL ??= 0;
RED.events.on("flows:add", onFlowAdd)
RED.events.on("flows:remove", onObjectRemove)
RED.events.on("flows:change", onFlowChange)
RED.events.on("flows:reorder", onFlowsReorder)
RED.events.on("workspace:clear", () => {
objects = {};
flowData = [];
if (flowSelector) {
flowSelector.treeList('clearSelection');
flowSelector.treeList('empty');
}
})
progress_bar = new ProgressBar(".mcu-build-progress-bar");
const progress_re = /^(?:Writing at 0x)(?:[0123456789abcdef]+\.{3} \()(\d{1,3})(?: %\))/;
// Flag set when '......' printing happens;
// this ensures that a line feed is inserted afterwards - if necessary!
let waiting_dot = 0;
let mcu_console = {
log(msg) {
let mcuc = $("#mcu_console");
// If the textarea is initially scrolled to its bottom position...
let at_the_bottom = (mcuc[0].scrollHeight - mcuc.scrollTop() - mcuc[0].clientHeight < 8);
if (msg.length > 0) {
if (msg == "__flash_console") {
flashTab("__console__")
} else {
let text = mcuc.val()
// console.log(msg, msg.length, msg.charCodeAt(0));
if ("." === msg) {
waiting_dot += 1;
} else if (waiting_dot > 1 && msg[0] !== "." && msg.charCodeAt(0) !== 10) {
text += '\n';
waiting_dot = 0;
} else if ("\r\n" === msg || "\n\r" === msg) {
msg = "\n";
} else {
waiting_dot = 0;
}
text += msg;
mcuc.val(text);
if (at_the_bottom) {
// ... we scroll it there afterwards again.
mcuc.scrollTop(mcuc[0].scrollHeight);
}
res = progress_re.exec(msg);
if (res && res.length > 1) {
progress_bar.set(res[1]);
} else {
progress_bar.ping();
}
}
}
}
}
RED.comms.subscribe("mcu/stdout/#", function (topic, msg) {
mcu_console.log(msg);
});
RED.comms.subscribe("mcu/serialports", function (topic, msg) {
let ports = mcu_plugin_config.ports;
let changed = false;
for (let i = 0; i < msg.length; i += 1) {
if (ports.length < i - 1) {
ports.push(msg[i]);
changed = true;
} else if (ports[i] !== msg[i]) {
ports[i] = msg[i];
changed = true;
}
}
if (msg.length < ports.length) {
ports.length = msg.length;
changed = true;
}
if (changed) {
RED.events.emit("mcu:validate-ports");
}
})
RED.comms.subscribe("mcu/notify", function (topic, data) {
progress_bar.reset();
if (data) {
// sanitize data
// ToDo: Confirm if this is necessary!
if (data.options?.buttons)
delete data.options.buttons
RED.notify(data.message, data.options);
}
})
let flows2build = [];
// Patch mcu flag into the defaults of any type
// This is a prerequisite as the NR editor only forwards properties to the runtime,
// that are defined in the defaults.
function patch_node_type_for_mcu(nt) {
let t = RED.nodes.getType(nt);
if (!t) return;
t.defaults["_mcu"] = { value: {"mcu": false} }
// Patch the onadd function of nodes to take over the flow's _mcu status
if (nt !== "tab") {
let onadd_orig = t.onadd;
t.onadd = function() {
let n = this;
if (n && n.type !== "tab" && n.z) {
let ws = RED.nodes.workspace(n.z);
let _mcu = n._mcu ?? {};
_mcu.mcu = (RED.nodes.workspace(n.z)?._mcu?.mcu === true) ?? false;
n._mcu = _mcu;
}
if (!onadd_orig) return;
return onadd_orig.call(n);
}
}
t = RED.nodes.getType(nt);
if (!t.defaults._mcu) {
console.log("MCU: Failed to patch type '" + nt + "'.")
}
}
// First for the nodes...
RED.events.on("registry:node-type-added", function (nt) {
patch_node_type_for_mcu(nt);
});
// Same for the flow...
patch_node_type_for_mcu("tab");
content = document.createElement("div");
content.className = "red-ui-sidebar-info"
let stackContainer = $("<div>", { class: "red-ui-sidebar-info-stack" }).appendTo(content);
selectorPanel = $("<div>").css({
"overflow": "hidden",
"height": "calc(70%)"
}).appendTo(stackContainer);
let selectorPanelHeader = $("<div>", { class: "red-ui-sidebar-header" }).css({ "text-align": "left", display: "flex" }).appendTo(selectorPanel);
let selectorPanelHeaderLeft = $('<span>').css({ "flex-grow": "1", "text-align": "left" }).appendTo(selectorPanelHeader);
let selectorPanelHeaderRight = $('<span>').css({ "display": "unset" }).appendTo(selectorPanelHeader);
$("<span>").text("Flows to build for MCU").css({ width: "100%", "text-align": "left", "font-weight": "bold" }).appendTo(selectorPanelHeaderLeft);
let selectorPanelInfo = $('<span>').appendTo(selectorPanelHeaderRight);
$('<i class="fa fa-info-circle fa-lg" aria-hidden="true"></i>').css({'color': 'rgb(7, 48, 112)'}).appendTo(selectorPanelInfo);
let selectorPanelInfoTip = $('<span><span>When deployed, flows selected here are in<br><b>stand-by mode</b>, awaiting an incoming <br>MCU connection.<br>De-select them & deploy again to enable<br>standard Node-RED functionality.</span>');
let selectorPanelInfoTooltip = RED.popover.tooltip(selectorPanelInfo, selectorPanelInfoTip);
buildPanel = $("<div>").css({
"overflow": "hidden",
"height": "100%",
"min-height": "50px"
}).appendTo(stackContainer);
buildPanelHeader = $("<div>", { class: "red-ui-sidebar-header" }).css({ "text-align": "left" }).appendTo(buildPanel);
let buildPanelHeaderLeft = $('<span>').css({ "flex-grow": "1", "text-align": "left" }).appendTo(buildPanelHeader);
let buildPanelHeaderRight = $('<span>').css({ "display": "unset" }).appendTo(buildPanelHeader);
$("<span>").text("MCU Build Configurations").css({ "text-align": "left", "font-weight": "bold", "vertical-align": "middle", display: "inline-block" }).appendTo(buildPanelHeaderLeft);
tabsContainer = $('<div>', { class: "tabs-container" }).css({ "position": "absolute", "top": "calc(100% - 35px)", "width": "100%" }).appendTo(buildPanel);
let buildTabs = $('<ul>', { id: "tabs-tabs" }).appendTo(tabsContainer);
panels = RED.panels.create({
container: stackContainer,
resize: function (top, bottom) {
resize_sidebar(top, bottom);
}
})
function resize_panels() {
if (panels) {
var h = $(content).parent().height();
panels.resize(h);
}
}
RED.events.on("sidebar:resize", resize_panels);
$(window).on("resize", resize_panels);
$(window).on("focus", resize_panels);
// the flows treeList
flowSelector = $("<div>").css({ width: "100% - 8px", height: "calc(100% - 45px)", margin: "4px" }).appendTo(selectorPanel);
flowSelector.treeList({
data: flowData,
multi: true,
}).on('treelistselect', function (event, item) {
let selected = flowSelector.treeList("selected")
let f2b = [];
for (let ii = 0; ii < selected.length; ii += 1) {
let s = selected[ii];
f2b.push(s.id);
}
// TODO: This should be UNDOable!
let changed = false;
let f2bl = f2b.length;
RED.nodes.eachWorkspace(function (ws) {
if (typeof(ws._mcu) !== "object") {
delete ws._mcu;
}
ws._mcu = ws._mcu || { "mcu": false };
// c === 3: node has flag but need none; remove it
// c === 2: node got mcu flag
// c === 1: node already has flag
// c === 0: node has no flag nor needs one
let c = 0;
for (let i = 0; i < f2bl; i += 1) {
// mark the flow
if (ws.id && ws.id === f2b[i]) {
if (ws._mcu?.mcu === true) {
c = 1;
} else {
c = 2;
}
break;
}
}
if (c > 0) {
ws._mcu.mcu = true;
}
if (c < 1) {
if (ws._mcu.mcu === true) {
c = 3;
}
ws._mcu.mcu = false;
}
if (c > 1) {
ws.changed = true;
RED.events.emit("flows:change", ws);
changed = true;
}
})
RED.nodes.eachNode(function (node) {
if (typeof(node._mcu) !== "object") {
delete node._mcu;
}
node._mcu ??= { "mcu": false };
// c === 3: node has flag but need none; remove it
// c === 2: node got mcu flag
// c === 1: node already has flag
// c === 0: node has no flag nor needs one
let c = 0;
for (let i = 0; i < f2bl; i += 1) {
// mark the nodes
if (node.z && node.z === f2b[i]) {
if (node._mcu.mcu === true) {
c = 1;
} else {
c = 2;
}
break;
}
}
if (c > 0) {
node._mcu.mcu = true;
}
if (c < 1) {
if (node._mcu.mcu === true) {
c = 3;
};
node._mcu.mcu = false;
}
if (c > 1) {
node.changed = true;
RED.events.emit("nodes:change", node);
changed = true;
}
})
if (changed) {
RED.nodes.dirty(true);
RED.view.updateActive();
RED.view.redraw(true);
let now = new Date().getTime() / 1000;
if (now - selectorPanelTipStamp > 300) {
selectorPanelInfoTooltip.open();
selectorPanelTipStamp = now;
setTimeout(function() {
selectorPanelInfoTooltip.close()
}, 7500);
}
}
flows2build = f2b;
});
RED.sidebar.addTab({
id: "node-red-mcu",
label: "MCU",
name: "Node-Red MCU",
iconClass: "fa fa-microchip",
content: content,
// toolbar: footerToolbar,
enableOnEdit: true,
// action: "core:show-flow-debugger-tab"
onchange: function() {
setTimeout(resize_sidebar, 0);
}
});
let console_container;
let config_container;
let config_scroll_top;
tabs = RED.tabs.create({
id: "tabs-tabs",
vertical: false,
scrollable: true,
onchange: function (tab) {
if (console_container) {
if (tab.id == "__console__") {
console_container.show();
} else {
console_container.hide();
}
}
if (config_container) {
if (tab.id != "__console__") {
config_container.show();
if (config_view) {
setTimeout(() => {
config_view.parent().scrollTop(config_scroll_top);
}, 0);
}
} else {
config_scroll_top = config_view?.parent()?.scrollTop() ?? 0;
config_container.hide();
}
}
// this here is for the editableList
resize_sidebar()
}
});
// patch for bottom placed tabs
tabsContainer.find(".red-ui-tabs").addClass("red-ui-tabs-bottom")
tabsContainer.find(".red-ui-tab-button").addClass("red-ui-tabs-bottom")
let tab = {
id: "1",
label: "Configuration",
iconClass: "fa fa-cog",
}
tabs.addTab(tab);
let tab2 = {
id: "__console__",
label: "Console Monitor",
iconClass: "fa fa-window-maximize",
}
tabs.addTab(tab2);
tabs.resize();
bPHh = buildPanelHeader.height();
console_container = $('<div>').css({ "height": "calc(100% - 42px - 40px - 4px)", "display": "none" }).appendTo(buildPanel);
console_view = $('<textarea id="mcu_console" wrap="hard" readonly>')
.css({
"position": "relative",
"box-sizing": "border-box",
"margin-top": "4px",
"margin-left": "4px",
"margin-right": "4px",
"margin-bottom": "2px",
"font-family": "monospace",
"font-size": "9pt",
"color": "white",
"background-color": "black",
"white-space": "pre",
"overflow": "scroll",
"width": "calc(100% - 8px)",
"resize": "none",
"line-height": "normal",
"cursor": "default",
"height": "100%",
"min-height": "89px",
"scrollbar-width": "none"
})
.appendTo(console_container)
.hover(
function() {
if (ccbt) {
clearTimeout(ccbt);
}
ccbt = setTimeout(function () {
let v = console_view.val();
if (v && typeof (v) == "string" && v.length > 0) {
if (console_copy_button) {
console_copy_button.show(200);
}
}
}, 200);
},
function (evt) {
if (ccbt) {
clearTimeout(ccbt);
}
ccbt = setTimeout(function () {
let rect = evt.target.getBoundingClientRect();
let in_x = (evt.clientX > rect.left) && (evt.clientX < rect.right);
let in_y = (evt.clientY > rect.top) && (evt.clientY < rect.bottom);
if (in_x && in_y) {
return;
}
console_copy_button.hide(100);
}, 300);
}
);
$("<div class='mcu-build-progress-bar'>").appendTo(console_container);
let console_copy_button = $('<button class="red-ui-button" title="Test"><i class="fa fa-clipboard"></i></button>').appendTo(console_container);
let ccbt; // console_copy_button_timer
console_copy_button.css({
position: "absolute",
top: "calc(100% - 85px)",
left: "calc(100% - 46px)"
}).on("click", function(evt) {
if (!console_view) return;
let cp = console_view.val();
navigator.clipboard.writeText(cp);