Repository navigation
Expand file tree
/
Copy pathallocation-visualizer.html
More file actions
1171 lines (1014 loc) · 56.8 KB
/
Copy pathallocation-visualizer.html
File metadata and controls
1171 lines (1014 loc) · 56.8 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Allocation Visualizer</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
overflow: hidden;
background: #f0f0f0;
}
#controls {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 50px;
background: #2c3e50;
border-bottom: 2px solid #1a1a1a;
display: flex;
align-items: center;
padding: 0 20px;
z-index: 1000;
}
#controls button {
padding: 8px 16px;
margin-right: 10px;
background: #3498db;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
#controls button:hover {
background: #2980b9;
}
#fileInput {
display: none;
}
#fileInfo {
color: white;
margin-left: 20px;
font-size: 14px;
}
#container {
position: relative;
width: 100vw;
height: calc(100vh - 50px);
top: 50px;
}
#canvas {
position: absolute;
cursor: grab;
background: white;
}
#canvas.grabbing {
cursor: grabbing;
}
#x-headers-container {
position: absolute;
left: 60px;
background: #e8e8e8;
overflow: hidden;
}
#y-header {
position: absolute;
left: 0;
width: 60px;
background: #e8e8e8;
border-right: 2px solid #333;
overflow: hidden;
}
#corner {
position: absolute;
top: 0;
left: 0;
width: 60px;
background: #d0d0d0;
border-right: 2px solid #333;
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
color: #666;
z-index: 10;
}
</style>
</head>
<body>
<div id="controls">
<button id="openFileBtn">Open File</button>
<input type="file" id="fileInput" accept=".txt">
<span id="fileInfo"></span>
</div>
<div id="container">
<div id="corner">0</div>
<div id="x-headers-container"></div>
<canvas id="y-header"></canvas>
<canvas id="canvas"></canvas>
</div>
<script>
const CELL_SIZE = 30;
const GRID_COLOR = '#ddd';
const HEADER_TEXT_COLOR = '#333';
const HEADER_HEIGHT = 40;
// Define colors for different rectangle types
const TYPE_COLORS = {
'A': 'rgba(52, 152, 219, 0.7)', // Blue
'U': 'rgba(46, 204, 113, 0.7)', // Green
'S': 'rgba(155, 89, 182, 0.7)', // Purple
'B': 'rgba(241, 196, 15, 0.7)', // Yellow
'E': 'rgba(231, 76, 60, 0.7)', // Red
'F': 'rgba(230, 126, 34, 0.7)', // Orange
'default': 'rgba(149, 165, 166, 0.7)' // Gray
};
class GridVisualizer {
constructor() {
this.canvas = document.getElementById('canvas');
this.ctx = this.canvas.getContext('2d');
this.xHeadersContainer = document.getElementById('x-headers-container');
this.yHeader = document.getElementById('y-header');
this.yHeaderCtx = this.yHeader.getContext('2d');
this.corner = document.getElementById('corner');
this.offsetX = 0;
this.offsetY = 0;
this.isDragging = false;
this.dragStartX = 0;
this.dragStartY = 0;
this.dragOffsetX = 0;
this.dragOffsetY = 0;
this.rectangles = [];
this.expandedLBlocks = new Set();
this.xMapping = []; // Maps visual X coordinates to actual coordinates
this.headerLayers = [];
this.setupCanvas();
this.setupEventListeners();
this.setupFileHandling();
this.render();
}
setupFileHandling() {
const openFileBtn = document.getElementById('openFileBtn');
const fileInput = document.getElementById('fileInput');
const fileInfo = document.getElementById('fileInfo');
openFileBtn.addEventListener('click', () => {
fileInput.click();
});
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
this.parseFile(event.target.result);
fileInfo.textContent = `Loaded: ${file.name}`;
};
reader.readAsText(file);
}
});
}
parseFile(content) {
this.rectangles = [];
this.expandedLBlocks.clear();
const lines = content.split('\n');
let lBlockCounter = 0;
// Parse recursively with support for L blocks
const parseLines = (lineList, parentPath = '') => {
const result = [];
let i = 0;
while (i < lineList.length) {
const line = lineList[i];
const cleanLine = line.split('#')[0].trim();
if (cleanLine === '') {
i++;
continue;
}
if (cleanLine === '}') {
return result;
}
const parts = cleanLine.split(/\s+/);
const type = parts[0];
if (type === 'L' && parts.length >= 2 && cleanLine.endsWith('{')) {
const localX = parseInt(parts[1]);
if (!isNaN(localX)) {
const lBlockId = `L${lBlockCounter++}`;
const fullPath = parentPath ? `${parentPath}.${lBlockId}` : lBlockId;
const lBlock = {
type: 'L',
id: fullPath,
x: localX,
children: [],
containedA: [],
containedU: [],
containedS: [],
containedB: [],
containedE: [],
containedF: []
};
// Parse contents of L block
const innerLines = [];
i++;
let braceCount = 1;
while (i < lineList.length && braceCount > 0) {
const innerLine = lineList[i];
const cleanInner = innerLine.split('#')[0].trim();
if (cleanInner === '}') {
braceCount--;
if (braceCount === 0) break;
} else if (cleanInner.endsWith('{')) {
braceCount++;
}
innerLines.push(innerLine);
i++;
}
lBlock.children = parseLines(innerLines, fullPath);
// Recursively collect all blocks for projection
const collectBlocks = (blocks) => {
for (const block of blocks) {
if (block.type === 'A') {
lBlock.containedA.push({
y1: block.y1,
y2: block.y2
});
} else if (block.type === 'U') {
lBlock.containedU.push({
y1: block.y1,
y2: block.y2
});
} else if (block.type === 'S') {
lBlock.containedS.push({
y1: block.y1,
y2: block.y2
});
} else if (block.type === 'B') {
lBlock.containedB.push({
y1: block.y1,
y2: block.y2
});
} else if (block.type === 'E') {
lBlock.containedE.push({
y1: block.y1,
y2: block.y2
});
} else if (block.type === 'F') {
lBlock.containedF.push({
y: block.y1
});
} else if (block.type === 'L') {
collectBlocks(block.children);
}
}
};
collectBlocks(lBlock.children);
result.push(lBlock);
}
i++;
} else if (type === 'F' && parts.length >= 3) {
const rect = {
type: 'F',
x1: parseInt(parts[1]),
y1: parseInt(parts[2])
};
if (!isNaN(rect.x1) && !isNaN(rect.y1)) {
result.push(rect);
}
i++;
} else if ((type === 'A' || type === 'U' || type === 'S' || type === 'B' || type === 'E') && parts.length >= 5) {
const rect = {
type: type,
x1: parseInt(parts[1]),
y1: parseInt(parts[2]),
x2: parseInt(parts[3]),
y2: parseInt(parts[4])
};
if (!isNaN(rect.x1) && !isNaN(rect.y1) &&
!isNaN(rect.x2) && !isNaN(rect.y2)) {
result.push(rect);
}
i++;
} else {
i++;
}
}
return result;
};
this.rectangles = parseLines(lines);
this.buildXMapping();
this.render();
}
buildXMapping() {
// Build the X coordinate mapping based on expanded L blocks
this.headerLayers = [];
// Find all expanded L blocks and build header layers
const processLBlock = (lBlock, depth) => {
const layerIndex = depth - 1;
if (layerIndex >= 0) {
if (!this.headerLayers[layerIndex]) {
this.headerLayers[layerIndex] = [];
}
this.headerLayers[layerIndex].push(lBlock);
}
// Process nested expanded L blocks
for (const child of lBlock.children) {
if (child.type === 'L' && this.expandedLBlocks.has(child.id)) {
processLBlock(child, depth + 1);
}
}
};
// Process all top-level expanded L blocks
for (const block of this.rectangles) {
if (block.type === 'L' && this.expandedLBlocks.has(block.id)) {
processLBlock(block, 1);
}
}
}
// Map a visual column to actual coordinates considering expansions
getColumnInfo(visualCol) {
// Find the actual coordinate by checking each position
for (let actualX = 0; actualX <= visualCol + 100; actualX++) {
const visualX = this.actualToVisualX(actualX);
// Check if this actual position maps exactly to our visual column
if (visualX === visualCol) {
const lBlock = this.rectangles.find(b =>
b.type === 'L' && b.x === actualX);
return {
actualX: actualX,
isExpanded: lBlock ? this.expandedLBlocks.has(lBlock.id) : false,
isInsideExpansion: false,
lBlock: lBlock || null
};
}
// Check if we've passed the target - means we're in an expansion
if (visualX > visualCol) {
// Look for an expanded L block at the previous actual position
for (let checkX = actualX - 1; checkX >= 0; checkX--) {
const lBlock = this.rectangles.find(b =>
b.type === 'L' && b.x === checkX && this.expandedLBlocks.has(b.id));
if (lBlock) {
const lVisualX = this.actualToVisualX(checkX);
const expansionWidth = this.getExpandedWidth(lBlock);
const expansionStart = lVisualX - expansionWidth;
if (visualCol >= expansionStart && visualCol < lVisualX) {
return {
actualX: checkX,
localX: visualCol - expansionStart,
isExpanded: true,
isInsideExpansion: true,
lBlock: lBlock
};
}
}
}
// Shouldn't happen, but return a default
return {
actualX: actualX,
isExpanded: false,
isInsideExpansion: false,
lBlock: null
};
}
}
// Fallback for columns beyond the range
return {
actualX: visualCol,
isExpanded: false,
isInsideExpansion: false,
lBlock: null
};
}
// Convert actual X coordinate to visual X coordinate accounting for expansions
actualToVisualX(actualX) {
let visualX = actualX; // Start with the actual position
// Add extra space for each expanded L block at or before this position
for (const block of this.rectangles) {
if (block.type === 'L' && block.x <= actualX && this.expandedLBlocks.has(block.id)) {
// This L block is expanded and is at or before our target position
// Add its expansion width (the expansion happens BEFORE the L block column)
visualX += this.getExpandedWidth(block);
}
}
return visualX;
}
getExpandedWidth(lBlock) {
// Calculate the VISUAL width of an L block when expanded
// This must account for ALL nested expansions pushing content right
let maxX = 0;
let currentX = 0;
// Process children in order to account for expansions
const children = [...lBlock.children].sort((a, b) => {
const aX = a.x || a.x1 || 0;
const bX = b.x || b.x1 || 0;
return aX - bX;
});
for (const child of children) {
if (child.type === 'A' || child.type === 'U' || child.type === 'S' || child.type === 'B' || child.type === 'E') {
maxX = Math.max(maxX, child.x2 + 1);
} else if (child.type === 'F') {
maxX = Math.max(maxX, child.x1 + 1);
} else if (child.type === 'L') {
if (this.expandedLBlocks.has(child.id)) {
// Expanded nested L: adds its width BEFORE its position
const childWidth = this.getExpandedWidth(child);
// Everything after this L gets pushed by childWidth
maxX = Math.max(maxX, child.x + childWidth + 1);
// Also check if any content after this L needs more space
for (const sibling of lBlock.children) {
const sibX = sibling.x || sibling.x1 || 0;
if (sibX > child.x) {
// This sibling is pushed right by the expansion
if (sibling.type === 'A' || sibling.type === 'U' || sibling.type === 'S' || sibling.type === 'B' || sibling.type === 'E') {
maxX = Math.max(maxX, sibling.x2 + childWidth + 1);
} else if (sibling.type === 'F') {
maxX = Math.max(maxX, sibling.x1 + childWidth + 1);
} else if (sibling.type === 'L') {
const sibWidth = this.expandedLBlocks.has(sibling.id) ?
this.getExpandedWidth(sibling) : 1;
maxX = Math.max(maxX, sibling.x + childWidth + sibWidth);
}
}
}
} else {
maxX = Math.max(maxX, child.x + 1);
}
}
}
return Math.max(maxX, 1);
}
setupCanvas() {
const resize = () => {
const container = document.getElementById('container');
// Number of headers is 1 (base) plus the number of expansion levels
const numHeaders = 1 + this.headerLayers.length;
const headerHeight = numHeaders * HEADER_HEIGHT;
// Update positions
this.xHeadersContainer.style.top = '0px';
this.xHeadersContainer.style.height = headerHeight + 'px';
this.canvas.style.top = headerHeight + 'px';
this.yHeader.style.top = headerHeight + 'px';
this.corner.style.height = headerHeight + 'px';
const width = container.clientWidth - 60;
const height = container.clientHeight - headerHeight;
// Main canvas
this.canvas.width = width;
this.canvas.height = height;
this.canvas.style.width = width + 'px';
this.canvas.style.height = height + 'px';
this.canvas.style.left = '60px';
// X headers container
this.xHeadersContainer.style.width = width + 'px';
// Y header
this.yHeader.width = 60;
this.yHeader.height = height;
this.yHeader.style.width = '60px';
this.yHeader.style.height = height + 'px';
this.render();
};
resize();
window.addEventListener('resize', resize);
}
setupEventListeners() {
// Canvas dragging
this.canvas.addEventListener('mousedown', (e) => {
this.isDragging = true;
this.canvas.classList.add('grabbing');
this.dragStartX = e.clientX;
this.dragStartY = e.clientY;
this.dragOffsetX = this.offsetX;
this.dragOffsetY = this.offsetY;
});
window.addEventListener('mousemove', (e) => {
if (!this.isDragging) return;
const dx = e.clientX - this.dragStartX;
const dy = e.clientY - this.dragStartY;
this.offsetX = this.dragOffsetX + dx;
this.offsetY = this.dragOffsetY + dy;
this.offsetX = Math.min(this.offsetX, 0);
this.offsetY = Math.min(this.offsetY, 0);
this.render();
});
window.addEventListener('mouseup', () => {
this.isDragging = false;
this.canvas.classList.remove('grabbing');
});
// Header clicking for L block expansion
this.xHeadersContainer.addEventListener('click', (e) => {
const rect = this.xHeadersContainer.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const col = Math.floor((x - this.offsetX) / CELL_SIZE);
const layer = Math.floor(y / HEADER_HEIGHT);
if (col >= 0 && layer >= 0) {
// Find which L block was clicked by traversing the hierarchy
let blockToToggle = null;
// Helper to find L block at specific position and layer
const findLBlockAt = (blocks, targetCol, targetLayer, currentLayer = 0, parentOffset = 0) => {
for (const block of blocks) {
if (block.type === 'L') {
// Calculate visual position with unified logic
const blockVisualX = currentLayer === 0 ?
this.actualToVisualX(block.x) :
parentOffset + this.getVisualOffsetInParent(block, blocks);
if (this.expandedLBlocks.has(block.id)) {
const width = this.getExpandedWidth(block);
// Unified: expansion ALWAYS goes BEFORE the L column
const expansionStart = blockVisualX - width;
const expansionEnd = blockVisualX - 1;
if (targetCol >= expansionStart && targetCol <= expansionEnd) {
// Click is inside this L's expansion
if (currentLayer + 1 === targetLayer) {
// This expansion is at the target layer
const localX = targetCol - expansionStart;
const nestedL = block.children.find(c =>
c.type === 'L' && c.x === localX);
if (nestedL) {
return nestedL;
}
} else if (currentLayer + 1 < targetLayer) {
// Need to go deeper
const result = findLBlockAt(
block.children,
targetCol,
targetLayer,
currentLayer + 1,
expansionStart
);
if (result) return result;
}
} else if (targetCol === blockVisualX && currentLayer === targetLayer) {
// Click on L column itself
}
} else {
// L is collapsed
if (targetCol === blockVisualX && currentLayer === targetLayer) {
return block;
}
}
}
}
return null;
};
blockToToggle = findLBlockAt(this.rectangles, col, layer);
if (blockToToggle) {
// Toggle expansion
if (this.expandedLBlocks.has(blockToToggle.id)) {
this.expandedLBlocks.delete(blockToToggle.id);
// Also collapse all children
const collapseChildren = (block) => {
for (const child of block.children) {
if (child.type === 'L') {
this.expandedLBlocks.delete(child.id);
collapseChildren(child);
}
}
};
collapseChildren(blockToToggle);
} else {
this.expandedLBlocks.add(blockToToggle.id);
}
this.buildXMapping();
this.setupCanvas(); // Resize everything and re-render
}
}
});
}
render() {
this.renderGrid();
this.renderXHeaders();
this.renderYHeader();
this.updateCorner();
}
updateCorner() {
// Show the maximum depth of expanded L blocks
const depth = this.headerLayers.length;
this.corner.textContent = depth.toString();
}
renderGrid() {
const width = this.canvas.width;
const height = this.canvas.height;
// Clear canvas
this.ctx.clearRect(0, 0, width, height);
// Draw grid lines - infinite grid
this.ctx.strokeStyle = GRID_COLOR;
this.ctx.lineWidth = 1;
// Vertical lines - draw as many as fit on screen
const startCol = Math.max(0, Math.floor(-this.offsetX / CELL_SIZE));
const endCol = Math.ceil((width - this.offsetX) / CELL_SIZE);
for (let col = startCol; col <= endCol; col++) {
const x = col * CELL_SIZE + this.offsetX;
this.ctx.beginPath();
this.ctx.moveTo(x, 0);
this.ctx.lineTo(x, height);
this.ctx.stroke();
}
// Horizontal lines
const startRow = Math.max(0, Math.floor(-this.offsetY / CELL_SIZE));
const endRow = Math.ceil((height - this.offsetY) / CELL_SIZE);
for (let row = startRow; row <= endRow; row++) {
const y = row * CELL_SIZE + this.offsetY;
this.ctx.beginPath();
this.ctx.moveTo(0, y);
this.ctx.lineTo(width, y);
this.ctx.stroke();
}
// Render blocks
this.renderBlocks(this.rectangles);
// Draw origin lines
this.ctx.strokeStyle = '#666';
this.ctx.lineWidth = 2;
if (this.offsetX > -2 && this.offsetX < width) {
this.ctx.beginPath();
this.ctx.moveTo(this.offsetX, 0);
this.ctx.lineTo(this.offsetX, height);
this.ctx.stroke();
}
if (this.offsetY > -2 && this.offsetY < height) {
this.ctx.beginPath();
this.ctx.moveTo(0, this.offsetY);
this.ctx.lineTo(width, this.offsetY);
this.ctx.stroke();
}
}
renderBlocks(blocks, visualXOffset = -1, insideExpanded = false, depth = 0) {
// Render blocks with proper visual offset
for (const block of blocks) {
if (block.type === 'L') {
const isExpanded = this.expandedLBlocks.has(block.id);
if (isExpanded) {
const expansionWidth = this.getExpandedWidth(block);
let lVisualX, expansionStartX;
if (!insideExpanded) {
// Top-level: expansion goes BEFORE the L column
lVisualX = this.actualToVisualX(block.x);
expansionStartX = lVisualX - expansionWidth;
} else {
// Nested: expansion REPLACES the L column
lVisualX = visualXOffset + this.getVisualOffsetInParent(block, blocks);
expansionStartX = lVisualX; // Expansion AT the L position
}
this.renderBlocks(block.children, expansionStartX, true, depth + 1);
} else {
// Render collapsed L block projection
const x = insideExpanded ?
(visualXOffset + block.x) * CELL_SIZE + this.offsetX :
this.actualToVisualX(block.x) * CELL_SIZE + this.offsetX;
// Draw vertical lines for all rectangle block types
const blockTypes = [
{ name: 'A', blocks: block.containedA, color: TYPE_COLORS['A'] },
{ name: 'U', blocks: block.containedU, color: TYPE_COLORS['U'] },
{ name: 'S', blocks: block.containedS, color: TYPE_COLORS['S'] },
{ name: 'B', blocks: block.containedB, color: TYPE_COLORS['B'] },
{ name: 'E', blocks: block.containedE, color: TYPE_COLORS['E'] }
];
for (const typeInfo of blockTypes) {
if (typeInfo.blocks.length > 0) {
this.ctx.strokeStyle = typeInfo.color.replace('0.7', '0.8');
this.ctx.lineWidth = 5;
for (const rectBlock of typeInfo.blocks) {
const y1 = rectBlock.y1 * CELL_SIZE + this.offsetY;
const y2 = (rectBlock.y2 + 1) * CELL_SIZE + this.offsetY;
this.ctx.beginPath();
this.ctx.moveTo(x, Math.max(0, y1));
this.ctx.lineTo(x, Math.min(this.canvas.height, y2));
this.ctx.stroke();
}
}
}
// Draw F dots and dashed line
if (block.containedF.length > 0) {
let minY = block.containedF[0].y;
for (const fBlock of block.containedF) {
minY = Math.min(minY, fBlock.y);
const dotY = fBlock.y * CELL_SIZE + this.offsetY;
if (dotY >= -10 && dotY <= this.canvas.height + 10) {
this.ctx.fillStyle = 'rgba(231, 76, 60, 1)';
this.ctx.beginPath();
this.ctx.arc(x, dotY, 8, 0, 2 * Math.PI);
this.ctx.fill();
this.ctx.strokeStyle = '#c0392b';
this.ctx.lineWidth = 2;
this.ctx.stroke();
}
}
const startY = minY * CELL_SIZE + this.offsetY;
this.ctx.strokeStyle = TYPE_COLORS['F'];
this.ctx.lineWidth = 3;
this.ctx.setLineDash([10, 5]);
this.ctx.beginPath();
this.ctx.moveTo(x, Math.max(startY, 0));
this.ctx.lineTo(x, this.canvas.height);
this.ctx.stroke();
this.ctx.setLineDash([]);
}
}
} else if (block.type === 'A' || block.type === 'U' || block.type === 'S' || block.type === 'B' || block.type === 'E') {
// Rectangle block - use visual offset if inside expanded L, otherwise use actualToVisualX
const x1 = insideExpanded ?
(visualXOffset + block.x1) * CELL_SIZE + this.offsetX :
this.actualToVisualX(block.x1) * CELL_SIZE + this.offsetX;
const x2 = insideExpanded ?
(visualXOffset + block.x2) * CELL_SIZE + this.offsetX :
this.actualToVisualX(block.x2) * CELL_SIZE + this.offsetX;
const y = block.y1 * CELL_SIZE + this.offsetY;
const w = x2 - x1 + CELL_SIZE;
const h = (block.y2 - block.y1 + 1) * CELL_SIZE;
if (x2 >= 0 && x1 <= this.canvas.width &&
y + h >= 0 && y <= this.canvas.height) {
this.ctx.fillStyle = TYPE_COLORS[block.type];
this.ctx.fillRect(x1, y, w, h);
this.ctx.strokeStyle = TYPE_COLORS[block.type].replace('0.7', '1');
this.ctx.lineWidth = 2;
this.ctx.strokeRect(x1, y, w, h);
if (w >= 20 && h >= 20) {
this.ctx.fillStyle = '#333';
this.ctx.font = 'bold 12px sans-serif';
this.ctx.textAlign = 'center';
this.ctx.textBaseline = 'middle';
this.ctx.fillText(block.type, x1 + w/2, y + h/2);
}
}
} else if (block.type === 'F') {
// F block - use visual offset if inside expanded L, otherwise use actualToVisualX
const x = insideExpanded ?
(visualXOffset + block.x1) * CELL_SIZE + this.offsetX :
this.actualToVisualX(block.x1) * CELL_SIZE + this.offsetX;
const startY = block.y1 * CELL_SIZE + this.offsetY;
if (x >= -10 && x <= this.canvas.width + 10) {
this.ctx.strokeStyle = TYPE_COLORS['F'];
this.ctx.lineWidth = 3;
this.ctx.setLineDash([10, 5]);
this.ctx.beginPath();
this.ctx.moveTo(x, Math.max(startY, 0));
this.ctx.lineTo(x, this.canvas.height);
this.ctx.stroke();
this.ctx.setLineDash([]);
if (startY >= -10 && startY <= this.canvas.height + 10) {
this.ctx.fillStyle = 'rgba(231, 76, 60, 1)';
this.ctx.beginPath();
this.ctx.arc(x, startY, 8, 0, 2 * Math.PI);
this.ctx.fill();
this.ctx.strokeStyle = '#c0392b';
this.ctx.lineWidth = 2;
this.ctx.stroke();
}
}
}
}
}
renderXHeaders() {
// Clear existing headers
this.xHeadersContainer.innerHTML = '';
// Build complete expansion map with proper depth tracking
const expansionsByDepth = new Map();
const mapExpansions = (blocks, depth, parentExpansionStart) => {
for (const block of blocks) {
if (block.type === 'L' && this.expandedLBlocks.has(block.id)) {
const width = this.getExpandedWidth(block);
let blockVisualX, expansionStart, expansionEnd, lColumn;
if (depth === 0) {
// Top-level: expansion goes BEFORE the L column
blockVisualX = this.actualToVisualX(block.x);
expansionStart = blockVisualX - width;
expansionEnd = blockVisualX - 1;
lColumn = blockVisualX;
} else {
// Nested: expansion REPLACES the L column
// Calculate the L's position within parent
blockVisualX = parentExpansionStart + this.getVisualOffsetInParent(block, blocks);
// For nested L, expansion starts AT the L position
expansionStart = blockVisualX;
expansionEnd = blockVisualX + width - 1;
lColumn = -1; // No separate L column for nested (it's replaced by expansion)
}
if (!expansionsByDepth.has(depth)) {
expansionsByDepth.set(depth, []);
}
expansionsByDepth.get(depth).push({
block: block,
start: expansionStart,
end: expansionEnd,
lColumn: lColumn
});
// Recurse for children - they are positioned relative to THIS block's expansion start
mapExpansions(block.children, depth + 1, expansionStart);
}
}
};
// Helper to calculate visual offset accounting for sibling expansions
this.getVisualOffsetInParent = (targetBlock, siblings) => {
let offset = targetBlock.x;
// Add space for any expanded L blocks before this one
for (const sibling of siblings) {
if (sibling.type === 'L' && sibling.x < targetBlock.x && this.expandedLBlocks.has(sibling.id)) {
offset += this.getExpandedWidth(sibling);
}
}
return offset;
};
// Also add top-level expansions to the map for consistent handling
expansionsByDepth.set(-1, []);
for (const block of this.rectangles) {
if (block.type === 'L' && this.expandedLBlocks.has(block.id)) {
const width = this.getExpandedWidth(block);
const blockVisualX = this.actualToVisualX(block.x);
expansionsByDepth.get(-1).push({
block: block,
start: blockVisualX - width,
end: blockVisualX - 1,
lColumn: blockVisualX
});
}
}
mapExpansions(this.rectangles, 0, 0);
const numLayers = expansionsByDepth.size + 1;
const width = this.xHeadersContainer.clientWidth;
// Calculate visible columns
const startCol = Math.max(0, Math.floor(-this.offsetX / CELL_SIZE));
const endCol = Math.ceil((width - this.offsetX) / CELL_SIZE);
for (let layer = 0; layer < numLayers; layer++) {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = HEADER_HEIGHT;
canvas.style.position = 'absolute';
canvas.style.top = (layer * HEADER_HEIGHT) + 'px';
canvas.style.left = '0';
const ctx = canvas.getContext('2d');
// Background
ctx.fillStyle = '#e8e8e8';
ctx.fillRect(0, 0, canvas.width, HEADER_HEIGHT);
// Draw grid lines and content
for (let col = startCol; col <= endCol; col++) {
const x = col * CELL_SIZE + this.offsetX;
// Draw grid line
ctx.strokeStyle = GRID_COLOR;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, HEADER_HEIGHT);
ctx.stroke();
let content = { type: 'empty' };
// Check for expansions at THIS layer's depth
const currentLayerExpansions = layer === 0 ?
(expansionsByDepth.get(-1) || []) :
(expansionsByDepth.get(layer - 1) || []);
let inCurrentLayerExpansion = false;
// First check if we're in an expansion at this layer
for (const exp of currentLayerExpansions) {
if (col >= exp.start && col <= exp.end) {
// In an expansion at this layer - show the local coordinate
const localX = col - exp.start;
content = { type: 'number', value: localX };
inCurrentLayerExpansion = true;
break;
} else if (col === exp.lColumn && exp.lColumn >= 0) {
content = { type: 'gap' };
inCurrentLayerExpansion = true;
break;
}
}
// If not in an expansion at this layer, check if we need to show content