-
Notifications
You must be signed in to change notification settings - Fork 0
/
a-to-as-to-b.html
1338 lines (1105 loc) · 446 KB
/
a-to-as-to-b.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
<html lang="en"><!--
MIT License, Copyright (c) 2024 Harri Kähkönen (https://github.com/epicharri)
--><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Epic Diagrams</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
box-sizing: border-box;
}
</style>
<script type="text/javascript">
class ControlPanel {
constructor() {
this.isPointerInside = false; // To track if the pointer is inside the SVG
this.iconColor = "#ffffff"; // Set the icon color to white
this.controlPanelBackgroundColor = "#000000"; // Set the control panel background color to black
this.controlPanelHeight = 84; // pixels
this.controlPanelWidth = 0;
this.controlPanelX = 100;
this.controlPanelY = 100;
this.iconWidth = 48; // pixels
this.iconHeight = 48;
this.gap = 72; // pixels
this.paused = true;
this.tempPaused = false;
this.renderedPacketsIndex = -1;
this.renderedPackets = [];
this.packetJustRendering = -1;
this.sleepAfterPacketInMs = 1000;
this.waitingAfterPacket = false;
this.svgElement = null;
this.renderingForward = true; // Default is to render forward. If false, render backward
this.renderedPacketGroups = [];
this.strokesAnimationTime = 1000; // ms
this.transformDurationMilliseconds = 500;
this.transformCompleted = false;
this.strokesRendered = false;
this.playButton = null;
this.pauseButton = null;
this.skipPreviousButton = null;
this.skipNextButton = null;
this.toFullscreenButton = null;
this.exitFullscreenButton = null;
this.playingBackwards = false;
this.firstBackwardsPacketIndex = null;
this.renderingAlsoBackwards = null;
this.packetTooltipsArray = [];
this.packetDivArray = [];
this.visibleTooltips = [];
this.packetRectArray = [];
this.blockMap = null;
this.blockTooltipsArray = [];
this.blocksBasedOnTooltipRect = null;
this.tooltipsBasedOnBlockDiv = null;
this.visibleBlockTooltips = [];
}
setPositionOfControlPanel() {
const svgWidth = parseFloat(this.svgElement.getAttribute("width"));
const svgHeight = parseFloat(this.svgElement.getAttribute("height"));
const x = svgWidth / 2 - this.controlPanelWidth / 2;
const y = svgHeight - this.controlPanelHeight;
this.controlPanelX = x;
this.controlPanelY = y;
}
addTooltipEventListeners() {
// Array to store references to the last 3 visible tooltip elements
this.visibleTooltips = [];
// Add event listeners for each div in packetDivArray
this.packetRectArray.forEach((rect, index) => {
console.log("Packet index, rect", index, rect);
if (index < this.packetTooltipsArray.length) {
const tooltip = this.packetTooltipsArray[index];
// Mouseover event to show tooltip temporarily
rect.addEventListener("mouseover", () => {
if (!tooltip.isPermanent) {
tooltip.style.display = "block";
}
console.log("Tooltip rect mouse over", tooltip, rect);
});
// Mouseout event to hide tooltip if not permanently visible
rect.addEventListener("mouseout", () => {
if (!tooltip.isPermanent) {
tooltip.style.display = "none";
}
console.log("Tooltip rect mouse out", tooltip, rect);
});
// Click event to toggle permanent visibility
rect.addEventListener("click", () => {
console.log("Tooltip rect click", tooltip, rect);
// Toggle permanent visibility status
tooltip.isPermanent = !tooltip.isPermanent;
if (tooltip.isPermanent) {
// Show tooltip and add it to visibleTooltips array
tooltip.style.display = "block";
this.visibleTooltips.push(tooltip);
// Ensure only the last 3 tooltips are permanently visible
if (this.visibleTooltips.length > 3) {
// Remove the oldest tooltip from permanent visibility
const oldestTooltip = this.visibleTooltips.shift();
oldestTooltip.style.display = "none";
oldestTooltip.isPermanent = false;
}
} else {
// Hide tooltip and remove from visibleTooltips array
tooltip.style.display = "none";
this.visibleTooltips = this.visibleTooltips.filter(t => t !== tooltip);
}
});
tooltip.addEventListener("click", () => {
if (tooltip.isPermanent) {
tooltip.style.display = "none";
tooltip.isPermanent = false;
this.visibleTooltips = this.visibleTooltips.filter(t => t !== tooltip);
}
});
}
});
}
createRenderedPacketGroupsArray() {
this.renderedPacketGroups = Array.from(this.svgElement.querySelectorAll("g.packet-and-strokes-group"));
this.firstBackwardsPacketIndex = this.renderedPacketGroups.length; // Default value: off by one of the array, if no backwards packets
this.renderingAlsoBackwards = false;
console.log("Rendered packet groups", this.renderedPacketGroups);
this.renderedPacketGroups.sort((a, b) => {
const aSequenceNumber = parseInt(a.getAttribute("sequence-number"));
const bSequenceNumber = parseInt(b.getAttribute("sequence-number"));
return aSequenceNumber - bSequenceNumber;
});
this.renderedPacketGroups.forEach(packetGroup => {
const isFirstBackwardsPacket = packetGroup.classList.contains("first-backwards-packet");
if (isFirstBackwardsPacket) {
this.firstBackwardsPacketIndex = parseInt(packetGroup.getAttribute("sequence-number"));
this.renderingAlsoBackwards = true;
}
});
}
play() {
this.svgElement = document.querySelector("#svg-block-diagram");
this.createRenderedPacketGroupsArray();
this.createRenderedPacketsArray();
this.createPacketTooltipsArray();
// this.createPacketDivArray();
this.createPacketRectsArrayAndAddTheListeners();
this.createBlockMap().then(() => {
this.addEventListenersToBlocksAndTheirTooltips();
this.createControlPanel();
this.addEventListeners();
this.addIconEventListeners();
this.addSVGPointerListeners();
// this.addTooltipEventListeners();
this.playAnimation();
});
}
giveDatasetOfElement(element) {
return new Promise((resolve) => {
const dataset = element.dataset;
resolve(dataset);
});
}
createTooltipOnBlockAndBlockOnTooltipMaps(blocks) {
return new Promise((resolve) => {
blocks.forEach(block => {
const blockId = block.id;
const blockDiv = block.querySelector("div");
const blockDivId = blockDiv.id;
this.blockMap[blockDivId] = block;
// const tooltipId = block.dataset.tooltipId; //getAttribute("data-tooltip-id");
// const tooltipRectId = block.dataset.tooltipRectId; //getAttribute("data-tooltip-rect-id");
this.giveDatasetOfElement(block).then((datasetOfBlock) => {
console.log("Dataset of block", datasetOfBlock);
console.log("Dataset of block, Block div id, block, tooltipId, tooltipRectId:",
datasetOfBlock, blockDivId, block, datasetOfBlock.tooltipId, datasetOfBlock.tooltipRectId);
if (datasetOfBlock.tooltipId) {
this.tooltipsBasedOnBlockDiv[blockDivId] = this.svgElement.getElementById(datasetOfBlock.tooltipId);
}
if (datasetOfBlock.tooltipRectId) {
this.blocksBasedOnTooltipRect[datasetOfBlock.tooltipRectId] = block;
}
});
});
resolve();
});
}
createBlockMap() {
return new Promise((resolve) => {
this.tooltipsBasedOnBlockDiv = {};
this.blocksBasedOnTooltipRect = {};
console.log("Creating block map");
this.blockMap = {};
this.createArrayOfElementsWithClassName("block").then((blocks) => {
this.createTooltipOnBlockAndBlockOnTooltipMaps(blocks).then(() => {
this.createArrayOfElementsWithClassName("title").then((titles) => {
this.createTooltipOnBlockAndBlockOnTooltipMaps(titles).then(() => {
console.log("Tooltips based on block div", this.tooltipsBasedOnBlockDiv);
console.log("Blocks based on tooltip rect", this.blocksBasedOnTooltipRect);
resolve();
});
});
}
);
});
});
}
addEventListenersToBlocksAndTheirTooltips() {
this.addEventListenersToBlocks();
this.addEventListenersToBlockTooltips();
}
addEventListenersToBlockTooltips() {
this.createBlockTooltipsArray().then(() => {
this.blockTooltipsArray.forEach((blockTooltip, index) => {
blockTooltip.isPermanent = false;
blockTooltip.style.display = "none";
blockTooltip.querySelector("rect").addEventListener("click", () => {
console.log("Block tooltip click", blockTooltip);
if (blockTooltip.isPermanent) {
blockTooltip.isPermanent = false;
blockTooltip.style.display = "none";
this.visibleBlockTooltips = this.visibleBlockTooltips.filter(tooltip => tooltip !== blockTooltip);
} else {
blockTooltip.style.display = "none";
}
});
});
});
}
addEventListenersToBlocks() {
this.visibleBlockTooltips = [];
for (const [blockDivId, block] of Object.entries(this.blockMap)) {
console.log("Block div id, block, data-tooltip-id", blockDivId, block, block.getAttribute("data-tooltip-id"));
block.addEventListener("mouseover", () => {
const tooltipId = block.dataset.tooltipId;
console.log("Block mouse over, tooltipId", block, tooltipId);
const tooltip = this.svgElement.querySelector(`#${tooltipId}`);
if (tooltip) {
tooltip.style.display = "block";
}
});
block.addEventListener("mouseout", () => {
const tooltipId = block.dataset.tooltipId;
console.log("Block mouse over, tooltipId", block, tooltipId);
const tooltip = this.svgElement.querySelector(`#${tooltipId}`);
if (tooltip && !tooltip.isPermanent) {
tooltip.style.display = "none";
}
});
block.addEventListener("click", () => {
console.log("Block click", block);
const tooltipId = block.dataset.tooltipId;
const tooltip = this.svgElement.querySelector(`#${tooltipId}`);
if (tooltip) {
if (tooltip.isPermanent) {
tooltip.style.display = "none";
tooltip.isPermanent = false;
} else {
if (this.visibleBlockTooltips.length === 1) {
// remove the first tooltip if there are already 3 visible
const firstTooltip = this.visibleBlockTooltips.shift(); // Remove the first tooltip
firstTooltip.style.display = "none";
firstTooltip.isPermanent = false;
}
tooltip.style.display = "block";
tooltip.isPermanent = true;
this.visibleBlockTooltips.push(tooltip);
}
}
});
}
}
// Creates an array of block tooltip groups based on the group ID's.
createBlockTooltipsArray() {
return new Promise((resolve) => {
this.blockTooltipsArray = [];
this.createArrayOfElementsWithClassName("blockTooltipGroup").then((blockTooltipsArrayCreated) => {
this.blockTooltipsArray = blockTooltipsArrayCreated;
console.log("Block tooltips array", this.blockTooltipsArray);
resolve();
});
});
}
closeAllTooltips() {
this.packetTooltipsArray.forEach(tooltip => {
tooltip.style.display = "none";
tooltip.isPermanent = false;
});
this.blockTooltipsArray.forEach(tooltip => {
tooltip.style.display = "none";
tooltip.isPermanent = false;
});
this.visibleTooltips = [];
}
createPacketRectsArrayAndAddTheListeners() {
this.packetRectArray = [];
this.createArrayOfElementsWithClassName("packetRect").then((packetRects) => {
this.packetRectArray = packetRects;
this.packetRectArray.sort((a, b) => {
const aSequenceNumber = parseInt(a.getAttribute("sequence-number"));
const bSequenceNumber = parseInt(b.getAttribute("sequence-number"));
return aSequenceNumber - bSequenceNumber;
});
this.addTooltipEventListeners();
console.log("Packet rects array", this.packetRectArray);
});
}
createPacketDivArray() {
this.packetDivArray = [];
this.packetDivArray = Array.from(this.svgElement.querySelectorAll(".packetDiv"));
this.packetDivArray.sort((a, b) => {
const aSequenceNumber = parseInt(a.getAttribute("sequence-number"));
const bSequenceNumber = parseInt(b.getAttribute("sequence-number"));
return aSequenceNumber - bSequenceNumber;
});
}
createArrayOfElementsWithClassName(className) {
return new Promise((resolve) => {
// Function to retrieve elements and check if they are available
const retrieveElements = () => {
const elements = this.svgElement.getElementsByClassName(className);
console.log("Elements with class name", className, elements);
// If elements are found, convert to array and resolve
if (elements.length > 0) {
resolve(Array.from(elements));
} else {
// Retry after a short delay if elements are not yet available
setTimeout(retrieveElements, 10);
}
};
// Start trying to retrieve the elements
retrieveElements();
});
}
createPacketTooltipsArray() {
this.packetTooltipsArray = [];
console.log('this.svgElement:', this.svgElement);
this.createArrayOfElementsWithClassName("PacketTooltipGroup").then((packetTooltips) => {
this.packetTooltipsArray = packetTooltips;
console.log("Packet tooltips array", this.packetTooltipsArray);
this.packetTooltipsArray.sort((a, b) => {
const aSequenceNumber = parseInt(a.getAttribute("sequence-number"));
const bSequenceNumber = parseInt(b.getAttribute("sequence-number"));
return aSequenceNumber - bSequenceNumber;
});
console.log("Packet tooltips array after sorting", this.packetTooltipsArray);
});
}
createRenderedPacketsArray() {
this.renderedPackets = [];
const foreignObjects = Array.from(this.svgElement.querySelectorAll("foreignObject.packet"));
foreignObjects.sort((a, b) => {
const aSequenceNumber = parseInt(a.getAttribute("sequence-number"));
const bSequenceNumber = parseInt(b.getAttribute("sequence-number"));
return aSequenceNumber - bSequenceNumber;
});
const allPacketStrokeClips = document.querySelectorAll(".clip-path-group");
console.log("All packet stroke clips", allPacketStrokeClips);
foreignObjects.forEach(foreignObject => {
const sequenceNumber = foreignObject.getAttribute("sequence-number");
console.log("Sequence number", sequenceNumber);
const packetStrokeClips = Array.from(this.svgElement.querySelectorAll(`.clip-path-group[packet-sequence-number="${sequenceNumber}"]`));
console.log("Packet stroke clips", packetStrokeClips);
packetStrokeClips.sort((a, b) => {
const aSequenceNumber = parseInt(a.getAttribute("reverse-index"));
const bSequenceNumber = parseInt(b.getAttribute("reverse-index"));
return aSequenceNumber - bSequenceNumber;
});
this.renderedPackets.push({
packet: foreignObject,
strokes: packetStrokeClips
});
console.log("Packet and strokes in control panel", this.renderedPackets[this.renderedPackets.length - 1]);
});
}
// Helper function to sleep for a specified time
async sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async waitUntilPacketTransformationCompleted() {
while (!(this.transformCompleted || this.strokesRendered)) {
await this.sleep(10); // Check every 10ms
}
}
async waitUntilUnpaused(ms) {
while (this.paused || this.tempPaused) {
await this.sleep(ms); // Check every 10ms
}
}
async waitUntilUnTempPaused(ms) {
while (this.tempPaused) {
await this.sleep(ms); // Check every 10ms
}
}
compareToPreviousPacketForToVisible(stroke) {
let shallAnimate = true;
const packetSequenceNumber = parseInt(stroke.getAttribute("packet-sequence-number"));
console.log("Packet sequence number", packetSequenceNumber);
let differenceOfPrevious = -1;
if (!this.renderingForward) differenceOfPrevious = 1;
const previousPacketSequenceNumber = packetSequenceNumber + differenceOfPrevious;
if ((previousPacketSequenceNumber < 0) || (previousPacketSequenceNumber > this.renderedPackets.length - 1)) return shallAnimate;
const previousPacketAndStrokes = this.renderedPackets[previousPacketSequenceNumber];
if (!previousPacketAndStrokes) return shallAnimate;
const previousPacketStrokes = previousPacketAndStrokes.strokes;
if (!previousPacketStrokes) return shallAnimate;
const reverseIndex = parseInt(stroke.getAttribute("reverse-index"));
const previousPacketStroke = previousPacketStrokes.find(
previousStroke => previousStroke.getAttribute("reverse-index") === stroke.getAttribute("reverse-index"));
if (!previousPacketStroke) return shallAnimate;
console.log("shallAnimate is set to false");
console.log("Previous packet and current packet reverseIndexes", previousPacketStroke.getAttribute("reverse-index"), reverseIndex);
shallAnimate = false;
return shallAnimate;
}
compareToPreviousPacketForToHide(stroke, strokes) {
let shallAnimate = {
animateHiding: false,
strokesToHide: [],
previousPacketAndStrokesClone: null
};
const packetSequenceNumber = parseInt(stroke.getAttribute("packet-sequence-number"));
console.log("Packet sequence number", packetSequenceNumber);
let differenceOfPrevious = -1;
if (!this.renderingForward) differenceOfPrevious = 1;
const previousPacketSequenceNumber = packetSequenceNumber + differenceOfPrevious;
if ((previousPacketSequenceNumber < 0) || (previousPacketSequenceNumber > this.renderedPackets.length - 1)) return shallAnimate;
const previousPacketAndStrokes = this.renderedPackets[previousPacketSequenceNumber];
if (!previousPacketAndStrokes) return shallAnimate;
const previousPacketStrokes = previousPacketAndStrokes.strokes;
if (!previousPacketStrokes) return shallAnimate;
const previousStrokesClone = previousPacketAndStrokes.strokes.clone();
if ((!previousStrokesClone) || (previousStrokesClone.length === 0)) return shallAnimate;
const previousPacketClone = previousPacketAndStrokes.packet.clone();
const previousPacketAndStrokesClone = {
packet: previousPacketClone,
strokes: previousStrokesClone
};
const currentReverseIndexes = strokes.map(stroke => parseInt(stroke.getAttribute("reverse-index")));
const previousReverseIndexes = previousStrokesClone.map(stroke => parseInt(stroke.getAttribute("reverse-index")));
shallAnimate.strokesToHide = [];
for (let previousStroke of previousStrokesClone) {
const previousStrokeReverseIndex = parseInt(previousStroke.getAttribute("reverse-index"));
if (!currentReverseIndexes.includes(previousStrokeReverseIndex)) {
previousStroke.x = parseInt(previousStroke.getAttribute("x"));
shallAnimate.strokesToHide.push(previousStroke);
}
}
shallAnimate.previousPacketAndStrokesClone = previousPacketAndStrokesClone;
shallAnimate.animateHiding = shallAnimate.strokesToHide.length > 0;
return shallAnimate;
}
async animateStrokeLines(stroke, shallAnimate) {
if (!shallAnimate) {
stroke.style.display = "block";
} else {
const lines = stroke.querySelectorAll("line");
let numberOfLines = lines.length;
for (let line of lines) {
line.style.display = "none";
}
stroke.style.display = "block";
const animationTime = this.strokesAnimationTime / numberOfLines;
for (let line of lines) {
await this.sleep(animationTime).then(() => {
line.style.display = "block";
});
}
}
}
async appendChildToSvgElement(element) {
return new Promise((resolve) => {
this.svgElement.appendChild(element);
element.offsetHeight; // Trigger reflow to make sure DOM is updated
requestAnimationFrame(() => {
resolve();
});
});
}
async moveCloneOfPacketGroupToPosition(theSequenceNumber, theDestinationSequenceNumber) {
this.transformCompleted = false;
const sequenceNumber = theSequenceNumber;
const destinationSequenceNumber = theDestinationSequenceNumber;
if (typeof (sequenceNumber) === "string") sequenceNumber = parseInt(sequenceNumber);
if (typeof (destinationSequenceNumber) === "string") destinationSequenceNumber = parseInt(destinationSequenceNumber);
if (sequenceNumber < 0 || sequenceNumber >= this.renderedPacketGroups.length) {
console.log("Invalid sequence number", sequenceNumber);
return null;
}
if (destinationSequenceNumber < 0 || destinationSequenceNumber >= this.renderedPacketGroups.length) {
console.log("Invalid sequence number (destination)", sequenceNumber);
return null;
}
const destinationPacketAndStrokesGroup = this.renderedPacketGroups[destinationSequenceNumber];
const packetAndStrokesGroupOriginal = this.renderedPacketGroups[sequenceNumber];
const packetAndStrokesGroupClone = packetAndStrokesGroupOriginal.cloneNode(true);
packetAndStrokesGroupClone.id = packetAndStrokesGroupClone.id + "-clone";
packetAndStrokesGroupClone.classList.remove("packet-and-strokes-group");
packetAndStrokesGroupClone.classList.add("packet-and-strokes-group-clone");
console.log("Packet and strokes group clone", packetAndStrokesGroupClone);
const foreignObject = destinationPacketAndStrokesGroup.querySelector("foreignObject.packet");
const endX = parseFloat(foreignObject.getAttribute("x"));
const endY = parseFloat(foreignObject.getAttribute("y"));
const startForeignObject = packetAndStrokesGroupClone.querySelector("foreignObject.packet");
const startXString = startForeignObject.getAttribute("x");
const startYString = startForeignObject.getAttribute("y");
const startX = parseFloat(startXString);
const startY = parseFloat(startYString);
const dx = endX - startX;
const dy = endY - startY;
packetAndStrokesGroupClone.setAttribute('transform', `translate(0, 0)`);
const animateTransform = document.createElementNS('http://www.w3.org/2000/svg', 'animateTransform');
animateTransform.setAttribute('attributeName', 'transform');
animateTransform.setAttribute('type', 'translate');
animateTransform.setAttribute('from', '0 0');
animateTransform.setAttribute('to', `0 0`); // 'to' is set to 0 0 so that the element does not flicker
animateTransform.setAttribute('dur', `${this.transformDurationMilliseconds / 1000}s`);
animateTransform.setAttribute('fill', 'freeze');
packetAndStrokesGroupClone.appendChild(animateTransform);
await this.appendChildToSvgElement(packetAndStrokesGroupClone);
animateTransform.setAttribute('to', `${dx} ${dy}`);
animateTransform.beginElement();
animateTransform.addEventListener('endEvent', () => {
setTimeout(() => {
packetAndStrokesGroupClone.remove();
}, 100);
this.transformCompleted = true;
});
}
countTheNumberOfStrokesToAnimateVisible(strokes) {
const numberOfStrokesToAnimate =
strokes.map(stroke => this.compareToPreviousPacketForToVisible(stroke) ? 1 : 0).reduce((acc, val) => acc + val, 0);
return numberOfStrokesToAnimate;
}
async playStrokesOfPacket(strokes) {
this.strokesRendered = false;
await this.renderStrokesOfPacket(strokes).then(() => {
this.strokesRendered = true;
}
);
}
async renderStrokesOfPacket(strokes) {
for (let index in strokes) {
let stroke = strokes[index];
const shallAnimateForToVisible = this.compareToPreviousPacketForToVisible(stroke);
await this.animateStrokeLines(stroke, shallAnimateForToVisible);
}
}
async sleepAfterPacketAndPlayNext() {
this.tempPaused = true;
await this.sleep(this.sleepAfterPacketInMs);
this.tempPaused = false;
await this.skipNextButtonClicked();
}
async reset() {
this.renderedPacketsIndex = -1;
await this.pauseButtonClicked();
await this.setRenderedPacketAndStrokesVisibleToIndex(-1);
}
async playAnimation() {
for (const [key, value] of Object.entries(this.blockMap)) {
console.log("Key, value, value.dataset, tooltipId, tooltipRectId: ", key, value, value.dataset, value.dataset.tooltipId, value.dataset.tooltipRectId);
}
await this.waitUntilUnpaused();
await this.skipNextButtonClicked();
while (true) {
await this.waitUntilUnpaused(10);
await this.waitUntilPacketTransformationCompleted();
await this.sleepAfterPacketAndPlayNext();
}
}
async setPacketVisible(packetAndStrokes) {
packetAndStrokes.packet.style.display = "block";
}
async setHidden(element) {
return new Promise((resolve) => {
element.style.display = "none";
element.offsetHeight; // Trigger reflow to make sure DOM is updated
requestAnimationFrame(() => {
resolve();
});
});
}
setPacketHidden(packetAndStrokes) {
packetAndStrokes.packet.style.display = "none";
}
// async setRenderedPacketAndStrokesHiddenFromIndex() {
// let index = this.renderedPacketsIndex;
// for (let i = 0; i < index; i++) {
// if (i < 0) continue;
// let packetAndStrokes = this.renderedPackets[i];
// this.setPacketVisible(packetAndStrokes);
// for (let stroke of packetAndStrokes.strokes) {
// stroke.style.display = "block";
// }
// }
// for (let i = index; i < this.renderedPackets.length; i++) {
// if (i < 0) continue;
// let packetAndStrokes = this.renderedPackets[i];
// packetAndStrokes.packet.style.display = "none";
// for (let stroke of packetAndStrokes.strokes) {
// stroke.style.display = "none";
// }
// }
// await this.sleep(this.sleepAfterPacketInMs).then(() => {
// });
// }
async setPacketTooltipVisible(index) {
return new Promise((resolve) => {
this.packetTooltipsArray.forEach((tooltip, i) => {
if (i !== index) {
if (!tooltip.isPermanent) {
tooltip.style.display = "none";
console.log("Tooltip set to hidden:", tooltip);
}
} else {
tooltip.style.display = "block";
console.log("Tooltip set to visible:", tooltip);
}
});
console.log("Packet tooltips array", this.packetTooltipsArray);
requestAnimationFrame(() => {
resolve();
});
});
}
async setRectAgain(index) {
return new Promise(async (resolve) => {
if (index >= 0 && index < this.packetRectArray.length) {
const rect = this.packetRectArray[index];
rect.remove();
this.svgElement.appendChild(rect);
}
requestAnimationFrame(() => {
resolve();
});
});
}
async setRenderedPacketAndStrokesVisibleBeforeIndex(index) {
return new Promise(async (resolve) => {
let firstVisibleIndex = 0;
if ((this.renderingAlsoBackwards) && (index >= this.firstBackwardsPacketIndex)) {
firstVisibleIndex = this.firstBackwardsPacketIndex;
}
for (let i = 0; i < firstVisibleIndex; i++) {
if (i >= this.renderedPackets.length) continue;
let packetAndStrokes = this.renderedPackets[i];
packetAndStrokes.packet.style.display = "none";
for (let stroke of packetAndStrokes.strokes) {
stroke.style.display = "none";
}
}
for (let i = index; i < this.renderedPackets.length; i++) { // i
if (i < 0) continue;
let packetAndStrokes = this.renderedPackets[i];
packetAndStrokes.packet.style.display = "none";
for (let stroke of packetAndStrokes.strokes) {
stroke.style.display = "none";
}
}
for (let i = firstVisibleIndex; i < index; i++) {
if (i >= this.renderedPackets.length) continue;
let packetAndStrokes = this.renderedPackets[i];
this.setPacketVisible(packetAndStrokes).then(() => {
for (let stroke of packetAndStrokes.strokes) {
stroke.style.display = "block";
}
});
await this.setRectAgain(i);
}
const element = this.renderedPackets[0].packet;
element.offsetHeight; // Trigger reflow to make sure DOM is updated
requestAnimationFrame(() => {
resolve();
});
});
}
async renderAnimationOfNextPacket(index) {
return new Promise(async (resolve) => {
if (!(index >= this.renderedPackets.length || index < 0)) {
let packetAndStrokes = this.renderedPackets[index];
const allStrokes = packetAndStrokes.strokes;
const numberOfStrokesToAnimate = this.countTheNumberOfStrokesToAnimateVisible(allStrokes);
console.log("Number of strokes to animate", numberOfStrokesToAnimate);
const animationTime = this.strokesAnimationTime * numberOfStrokesToAnimate;
await this.moveCloneOfPacketGroupToPosition(index - 1, index);
await this.sleep(this.transformDurationMilliseconds);
await this.setPacketVisible(packetAndStrokes);
await this.playStrokesOfPacket(packetAndStrokes.strokes);
await this.setRectAgain(index);
if (this.renderedPacketsIndex === this.renderedPackets.length) {
this.pauseButtonClicked();
}
// await this.sleep(this.sleepAfterPacketInMs).then(() => { });
}
const element = this.renderedPackets[0].packet;
element.offsetHeight; // Trigger reflow to make sure DOM is updated
requestAnimationFrame(() => {
resolve();
});
});
}
async setRenderedPacketAndStrokesVisibleToIndex() {
let index = this.renderedPacketsIndex;
await this.setRenderedPacketAndStrokesVisibleBeforeIndex(index);
await this.renderAnimationOfNextPacket(index);
await this.setPacketTooltipVisible(index);
}
createControlPanel() {
const controlPanelElement = document.getElementById('control-panel');
if (controlPanelElement) {
console.log("Control panel exists");
this.controlPanel = controlPanelElement;
return;
}
this.controlPanel = document.createElementNS("http://www.w3.org/2000/svg", "g");
this.controlPanel.setAttribute("id", "control-panel");
this.controlPanelWidth = 5 * this.gap + this.gap - this.iconWidth;
this.setPositionOfControlPanel();
let startX = this.controlPanelX + this.gap - this.iconWidth;
let startY = this.controlPanelY + this.controlPanelHeight / 2 - this.iconHeight / 2;
const icons = [
{
'id': 'play', 'x': startX, 'y': startY,
'path': "M320-200v-560l440 280-440 280Zm80-280Zm0 134 210-134-210-134v268Z",
'display': "block"
},
{
'id': 'pause', 'x': startX, 'y': startY,
'path': "M520-200v-560h240v560H520Zm-320 0v-560h240v560H200Zm400-80h80v-400h-80v400Zm-320 0h80v-400h-80v400Zm0-400v400-400Zm320 0v400-400Z",
'display': "none"
},
{
'id': 'skipPrevious', 'x': startX + this.gap, 'y': startY,
'path': "M220-240v-480h80v480h-80Zm520 0L380-480l360-240v480Zm-80-240Zm0 90v-180l-136 90 136 90Z",
'display': "block"
},
{
'id': 'skipNext', 'x': startX + 2 * this.gap, 'y': startY,
'path': "M660-240v-480h80v480h-80Zm-440 0v-480l360 240-360 240Zm80-240Zm0 90 136-90-136-90v180Z",
'display': "block"
},
{
'id': 'replay', 'x': startX + 3 * this.gap, 'y': startY,
'path': "M480-80q-75 0-140.5-28.5t-114-77q-48.5-48.5-77-114T120-440h80q0 117 81.5 198.5T480-160q117 0 198.5-81.5T760-440q0-117-81.5-198.5T480-720h-6l62 62-56 58-160-160 160-160 56 58-62 62h6q75 0 140.5 28.5t114 77q48.5 48.5 77 114T840-440q0 75-28.5 140.5t-77 114q-48.5 48.5-114 77T480-80Z",
'display': "block"
},
{
'id': 'toFullscreen', 'x': startX + 4 * this.gap, 'y': startY,
'path': "M120-120v-200h80v120h120v80H120Zm520 0v-80h120v-120h80v200H640ZM120-640v-200h200v80H200v120h-80Zm640 0v-120H640v-80h200v200h-80Z",
'display': "block"
},
{
'id': 'exitFullscreen', 'x': startX + 4 * this.gap, 'y': startY,
'path': "M240-120v-120H120v-80h200v200h-80Zm400 0v-200h200v80H720v120h-80ZM120-640v-80h120v-120h80v200H120Zm520 0v-200h80v120h120v80H640Z",
'display': "none"
}
];
const controlPanelRect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
controlPanelRect.setAttribute("x", this.controlPanelX);
controlPanelRect.setAttribute("y", this.controlPanelY);
controlPanelRect.setAttribute("width", `${this.controlPanelWidth}px`);
controlPanelRect.setAttribute("height", `${this.controlPanelHeight}px`);
controlPanelRect.setAttribute("rx", "10");
controlPanelRect.setAttribute("fill", this.controlPanelBackgroundColor);
this.controlPanel.appendChild(controlPanelRect);
let yPosition = startY;
icons.forEach(icon => {
const iconElement = this.createIconElement(icon);
this.controlPanel.appendChild(iconElement.icon);
this.controlPanel.appendChild(iconElement.rect);
});
this.svgElement.appendChild(this.controlPanel);
}
createIconElement(icon) {
const id = icon.id;
const xPosition = icon.x;
const yPosition = icon.y;
const path = icon.path;
const display = icon.display;
const iconSvgElement = document.createElementNS("http://www.w3.org/2000/svg", "svg");
iconSvgElement.setAttribute("id", `${id}-svg`);
iconSvgElement.setAttribute("href", `#${id}`);
iconSvgElement.setAttribute("x", xPosition);
console.log("xPosition", xPosition);
iconSvgElement.setAttribute("y", yPosition);
iconSvgElement.setAttribute("width", `${this.iconWidth}px`);
iconSvgElement.setAttribute("height", `${this.iconHeight}px`);
iconSvgElement.setAttribute("viewBox", "0 -960 960 960");
iconSvgElement.setAttribute("fill", this.iconColor);
iconSvgElement.setAttribute("display", display);
iconSvgElement.style.cursor = "pointer";
const pathElement = document.createElementNS("http://www.w3.org/2000/svg", "path");
pathElement.setAttribute("d", path);
iconSvgElement.appendChild(pathElement);
let theScale = 1.3;
let scaledX = xPosition - ((theScale - 1) / 2) * this.iconWidth;
let scaledY = yPosition - ((theScale - 1) / 2) * this.iconHeight;
let scaledWidth = this.iconWidth * theScale;
let scaledHeight = this.iconHeight * theScale;
iconSvgElement.setAttribute("scaled-x", scaledX);
iconSvgElement.setAttribute("scaled-y", scaledY);
iconSvgElement.setAttribute("scaled-width", scaledWidth);
iconSvgElement.setAttribute("scaled-height", scaledHeight);
const rectElement = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rectElement.setAttribute("id", `${id}`);
rectElement.setAttribute("x", xPosition);
rectElement.setAttribute("y", yPosition);
rectElement.setAttribute("width", `${this.iconWidth}px`);
rectElement.setAttribute("height", `${this.iconHeight}px`);
rectElement.setAttribute("fill", "transparent");
rectElement.setAttribute("display", display);
rectElement.classList.add("icon-rect");
rectElement.style.cursor = "pointer";
const iconElementMap = {
'icon': iconSvgElement,
'rect': rectElement
}
return iconElementMap;
}
addIconEventListeners() {
const iconRectElements = Array.from(this.controlPanel.getElementsByClassName("icon-rect"));
iconRectElements.forEach(rectElement => {
const iconSvgElement = document.getElementById(`${rectElement.id}-svg`);
const x = iconSvgElement.getAttribute("x");
const y = iconSvgElement.getAttribute("y");
const scaledX = iconSvgElement.getAttribute("scaled-x");
const scaledY = iconSvgElement.getAttribute("scaled-y");
const scaledWidth = iconSvgElement.getAttribute("scaled-width");
const scaledHeight = iconSvgElement.getAttribute("scaled-height");
console.log("iconSvgElement, rectElement, scaled x, y, width, height", iconSvgElement, rectElement, scaledX, scaledY, scaledWidth, scaledHeight);
rectElement.addEventListener("mouseenter", () => {