Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
1221 lines (1029 loc) · 40.5 KB
/
Copy pathapp.js
File metadata and controls
1221 lines (1029 loc) · 40.5 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
// Mapbox Configuration
mapboxgl.accessToken = 'pk.eyJ1IjoiZXBvb2siLCJhIjoiQjBxamU5RSJ9.srKOyc2kfn-OudQVdVXSxA';
// Initialize map
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/epook/cmap6hwka006b01sc6nhmh0kv',
center: [15, 52], // Center on Europe
zoom: 4
});
// Timeline Configuration
const START_YEAR = 1933;
const END_YEAR = 1946;
const EVENTS_PER_YEAR = 5;
const TOTAL_YEARS = END_YEAR - START_YEAR + 1;
const TOTAL_EVENTS = TOTAL_YEARS * EVENTS_PER_YEAR;
// Events from CSV (will be loaded)
let events = [];
let currentYear = START_YEAR;
let currentEventIndex = 25; // Start at 1938 (5 years * 5 events per year)
let mapMarkers = new Map(); // Map of eventId -> marker object
let animatedEvents = new Set(); // Track which events have been animated
let currentBordersYear = null; // Track currently loaded borders
let hoveredEventId = null; // Track currently hovered event for line/circle highlighting
// DOM Elements
const yearDisplay = document.getElementById('year-display');
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
const timelineMarkers = document.getElementById('timeline-markers');
const timelineProgress = document.getElementById('timeline-progress');
const eventPanel = document.getElementById('event-panel');
const closePanel = document.getElementById('close-panel');
const infoBox = document.getElementById('info-box');
const infoText = document.getElementById('info-text');
// Get coordinates based on event title and description
function getEventCoordinates(rubrik, ingress) {
const coordinateMap = {
// Swedish events - Stockholm
'Sveriges utlänningslag': [18.0686, 59.3293],
'Barnkvoten': [18.0686, 59.3293],
'Samlingsregeringen': [18.0686, 59.3293],
'Räddningsaktioner': [18.0686, 59.3293],
// German events - Berlin
'Namnlagen': [13.4050, 52.5200],
'J-stämpeln': [13.4050, 52.5200],
'Judiska företag förbjuds': [13.4050, 52.5200],
'Novemberpogromen': [13.4050, 52.5200],
'Koncentrationsläger': [13.2633, 52.7667], // Sachsenhausen
'Aktion T4 inleds': [13.4050, 52.5200],
'Bofasta romer': [13.4050, 52.5200],
'Den gula stjärnan': [13.4050, 52.5200],
'Deportation av Nazitysklands judar': [13.4050, 52.5200],
'Aktion T4 avslutas': [13.4050, 52.5200],
'Wannseekonferensen': [13.1644, 52.4344], // Wannsee
'Attentat mot Hitler': [13.4050, 52.5200],
'Förstör bevisen': [13.4050, 52.5200],
'Tyskland kapitulerar': [13.4050, 52.5200],
'Nürnbergrättegångarna': [11.0773, 49.4521], // Nuremberg
// Austria
'Anschluss': [16.3738, 48.2082], // Vienna
// France
'Eviankonferensen': [6.5894, 46.4011], // Évian-les-Bains
'Frankrike kapitulerar': [2.3522, 48.8566], // Paris
'Dagen D': [-0.5760, 49.3200], // Normandy
// Czech Republic
'Tjeckien invaderas': [14.4378, 50.0755], // Prague
// Russia/Soviet Union
'Molotov-Ribbentrop-pakten': [37.6173, 55.7558], // Moscow
'Operation Barbarossa': [37.6173, 55.7558], // Moscow
'Stalingrad': [44.5169, 48.7080], // Volgograd
// Poland
'Kriget börjar': [21.0122, 52.2297], // Warsaw
'Getton': [21.0122, 52.2297], // Warsaw
'Auschwitz I börjar byggas': [19.2034, 50.0347], // Oświęcim
'Auschwitz II börjar byggas': [19.2034, 50.0347], // Oświęcim
'Auschwitz-Birkenau befrias': [19.2034, 50.0347], // Oświęcim
'Operation Reinhard': [22.0534, 52.6260], // Treblinka
'De första judarna gasas ihjäl i Chełmno': [18.7290, 52.1456], // Chełmno
'Deportation av och mord på romer': [19.2034, 50.0347], // Auschwitz
'Aktion "Skördefesten"': [22.5667, 51.2500], // Lublin
'"Aktion ""Skördefesten"""': [22.5667, 51.2500], // Lublin (CSV format)
'Majdanek befrias': [22.6050, 51.2220], // Majdanek/Lublin
'Pogromen i Kielce': [20.6286, 50.8703], // Kielce
// Denmark
'Danmark kapitulerar': [12.5683, 55.6761], // Copenhagen
'De danska judarna räddas': [12.5683, 55.6761], // Copenhagen
// Luxembourg
'Luxemburg kapitulerar': [6.1296, 49.6116], // Luxembourg City
// Netherlands
'Nederländerna kapitulerar': [4.9041, 52.3676], // Amsterdam
// Belgium
'Belgien kapitulerar': [4.3517, 50.8503], // Brussels
// Norway
'Norge kapitulerar': [10.7522, 59.9139], // Oslo
'Norska judar deporteras': [10.7522, 59.9139], // Oslo
// Belarus
'Massakern i Prypjat-träsken': [26.0951, 52.1229], // Pinsk
// Ukraine
'Massakern vid Kamjanets-Podilskyj': [26.5850, 48.6847],
'Massakern vid Babyn Jar': [30.5234, 50.4501], // Kyiv
// Latvia
'Massakern vid Rumbula': [24.1052, 56.9496], // Riga
// USA
'Världskrig': [-157.9637, 21.3649], // Pearl Harbor
'Förenta Nationerna': [-122.4194, 37.7749], // San Francisco
// Egypt
'Slaget vid El Alamein': [28.9550, 30.8170],
// Italy
'Invasionen av Sicilien': [13.3615, 38.1157], // Palermo
'Italien kapitulerar': [12.4964, 41.9028], // Rome
// Hungary
'Deportation av Ungerns judar': [19.0402, 47.4979], // Budapest
// UK
'Internationell protest': [-0.1276, 51.5074], // London
// Japan
'Japan kapitulerar': [139.6917, 35.6895] // Tokyo
};
// Debug: log if coordinate not found
const coords = coordinateMap[rubrik];
if (!coords) {
console.log('No coordinates found for:', rubrik);
}
// Return coordinates if found, otherwise return default Europe center
return coords || [15, 52];
}
// Parse date to sort events chronologically
function parseEventDate(dateStr) {
// Handle various date formats: "1938-01-01", "december 1938", "1938-03-12"
const yearMatch = dateStr.match(/\d{4}/);
if (!yearMatch) return new Date(0);
const year = parseInt(yearMatch[0]);
// Try to extract month
const monthNames = {
'jan': 0, 'feb': 1, 'mar': 2, 'apr': 3, 'maj': 4, 'may': 4,
'jun': 5, 'jul': 6, 'aug': 7, 'sep': 8, 'okt': 9, 'oct': 9,
'nov': 10, 'dec': 11, 'december': 11, 'januari': 0, 'februari': 1,
'mars': 2, 'april': 3, 'juni': 5, 'juli': 6, 'augusti': 7,
'september': 8, 'oktober': 9, 'november': 10
};
let month = 0;
let day = 1;
// Check for ISO date format (YYYY-MM-DD)
const isoMatch = dateStr.match(/(\d{4})-(\d{2})-(\d{2})/);
if (isoMatch) {
month = parseInt(isoMatch[2]) - 1;
day = parseInt(isoMatch[3]);
} else {
// Check for month name
const lowerDate = dateStr.toLowerCase();
for (const [name, num] of Object.entries(monthNames)) {
if (lowerDate.includes(name)) {
month = num;
break;
}
}
}
return new Date(year, month, day);
}
// Parse CSV line handling quoted fields
function parseCSVLine(line) {
const result = [];
let current = '';
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"') {
inQuotes = !inQuotes;
} else if (char === ',' && !inQuotes) {
result.push(current.trim());
current = '';
} else {
current += char;
}
}
result.push(current.trim());
return result;
}
// Parse CSV and load events
async function loadEvents() {
try {
const response = await fetch('events.csv');
const csvText = await response.text();
const lines = csvText.split('\n');
events = [];
for (let i = 1; i < lines.length; i++) {
if (!lines[i].trim()) continue;
const row = parseCSVLine(lines[i]);
if (row.length < 3) continue;
const datum = row[0];
const rubrik = row[1];
const ingress = row[2];
const bildUrl = row[3] || '';
// Extract year from date
const yearMatch = datum.match(/\d{4}/);
if (!yearMatch) continue;
const year = parseInt(yearMatch[0]);
if (year >= START_YEAR && year <= END_YEAR) {
events.push({
year: year,
date: datum,
parsedDate: parseEventDate(datum),
title: rubrik,
description: ingress,
image: bildUrl,
coordinates: getEventCoordinates(rubrik, ingress)
});
}
}
console.log(`Loaded ${events.length} events`);
updateEventMarkers();
} catch (error) {
console.error('Error loading events:', error);
}
}
// Initialize timeline
function initTimeline() {
timelineMarkers.innerHTML = '';
// Create year markers
for (let i = 0; i < TOTAL_YEARS; i++) {
const year = START_YEAR + i;
const position = (i / (TOTAL_YEARS - 1)) * 100;
const yearMarker = document.createElement('div');
yearMarker.className = 'year-marker';
yearMarker.style.left = `${position}%`;
yearMarker.innerHTML = `
<div class="year-label">${year}</div>
<div class="year-line"></div>
`;
timelineMarkers.appendChild(yearMarker);
// Create event markers for this year
// Only create 1 dot for the last year (1946), 5 dots for all other years
const dotsForThisYear = (year === END_YEAR) ? 1 : EVENTS_PER_YEAR;
for (let j = 0; j < dotsForThisYear; j++) {
const eventPosition = position + (j / (TOTAL_YEARS - 1) / EVENTS_PER_YEAR) * 100;
const globalIndex = i * EVENTS_PER_YEAR + j;
const eventMarker = document.createElement('div');
eventMarker.className = 'event-marker';
eventMarker.style.left = `${eventPosition}%`;
eventMarker.dataset.index = globalIndex;
eventMarker.dataset.year = year;
eventMarker.addEventListener('click', () => {
goToEvent(globalIndex);
});
timelineMarkers.appendChild(eventMarker);
}
}
updateTimeline();
}
// Update timeline display
function updateTimeline() {
currentYear = START_YEAR + Math.floor(currentEventIndex / EVENTS_PER_YEAR);
yearDisplay.textContent = currentYear;
const progress = (currentEventIndex / (TOTAL_EVENTS - 1)) * 100;
timelineProgress.style.width = `${progress}%`;
// Update active marker and past markers
document.querySelectorAll('.event-marker').forEach((marker, index) => {
marker.classList.toggle('active', index === currentEventIndex);
marker.classList.toggle('past', index < currentEventIndex);
});
// Update navigation buttons
prevBtn.disabled = currentEventIndex === 0;
nextBtn.disabled = currentEventIndex === TOTAL_EVENTS - 1;
// Update info box with year context
updateYearInfo();
// Update historical borders overlay
updateHistoricalBorders();
// Update event markers on map
updateEventMarkers();
}
// Year descriptions for years without events
const yearDescriptions = {
1933: "Adolf Hitler utsågs till Tysklands rikskansler den 30 januari 1933. Nazistpartiet började omedelbart förfölja sina politiska motståndare och tyska judar. De första koncentrationslägren öppnades detta år.",
1934: "Under 1934 konsoliderade Hitler sin makt i Tyskland. Natten mellan 30 juni och 1 juli, känd som 'de långa knivarna natt', lät Hitler mörda sina politiska rivaler inom SA-ledningen.",
1935: "Nürnberglagarna antogs i september 1935, vilket officiellt gjorde tyska judar till andraklassmedborgare. Lagarna förbjöd äktenskap och sexuella relationer mellan judar och 'arier'.",
1936: "De olympiska spelen hölls i Berlin 1936. Nazisterna använde evenemanget som propaganda för att visa upp ett 'modernt' Tyskland, medan förföljelsen av judar tillfälligt doldes.",
1937: "Under 1937 intensifierades nazisternas förberedelser för krig. Den tyska ekonomin ställdes om för krigsproduktion och militären byggdes ut i strid mot Versaillesfördraget."
};
// Update year info box
function updateYearInfo() {
// Get first event of the current year to show as year context
const yearEvents = events.filter(e => e.year === currentYear);
let text = '';
if (yearEvents.length > 0) {
// Sort by date and take the first event
yearEvents.sort((a, b) => a.parsedDate - b.parsedDate);
text = yearEvents[0].description;
} else if (yearDescriptions[currentYear]) {
// Use predefined description for years without events
text = yearDescriptions[currentYear];
}
if (text) {
// Limit to 350 characters
if (text.length > 350) {
text = text.substring(0, 350) + '...';
}
infoText.textContent = text;
infoBox.classList.remove('hidden');
} else {
infoBox.classList.add('hidden');
}
}
// Load and update historical borders based on current year
async function updateHistoricalBorders() {
// Only load borders for years we have data (1938-1944)
if (currentYear < 1938 || currentYear > 1944) {
// Remove borders layer if outside range
if (map.getLayer('borders-fill')) {
map.removeLayer('borders-fill');
map.removeLayer('borders-outline');
map.removeSource('borders');
currentBordersYear = null;
}
return;
}
// Don't reload if already showing this year
if (currentBordersYear === currentYear) {
return;
}
try {
const geojsonFile = `geojson/December_31_${currentYear}.geojson`;
console.log(`Loading borders for ${currentYear}...`);
const response = await fetch(geojsonFile);
const data = await response.json();
// Remove existing layers if they exist
if (map.getLayer('borders-fill')) {
map.removeLayer('borders-fill');
map.removeLayer('borders-outline');
map.removeSource('borders');
}
// Add source
map.addSource('borders', {
type: 'geojson',
data: data
});
// Add fill layer with opacity based on occupation status
map.addLayer({
id: 'borders-fill',
type: 'fill',
source: 'borders',
paint: {
'fill-color': '#8B0000', // Dark red
'fill-opacity': [
'case',
// Check if Foreign_Po contains "German" or "occupied"
[
'any',
['==', ['get', 'Foreign_Po'], 'German-occupied'],
['==', ['get', 'Foreign_Po'], 'German Protectorate'],
['==', ['get', 'Foreign_Po'], 'German, Italian-occupied'],
['==', ['get', 'Foreign_Po'], 'German, Italian, Bulgarian-occupied'],
['==', ['get', 'Foreign_Po'], 'Axis and German-occupied'],
['==', ['get', 'Foreign_Po'], 'Axis and German, Italian-occupied']
], 0.4,
['==', ['get', 'Foreign_Po'], 'Italian-occupied'], 0.3,
['==', ['get', 'Foreign_Po'], 'Italian Protectorate'], 0.25,
['==', ['get', 'Foreign_Po'], 'Axis'], 0.25,
['==', ['get', 'Foreign_Po'], 'Romanian-occupied'], 0.25,
0 // Transparent for non-occupied
]
}
}, 'waterway-label'); // Insert below labels
// Add outline layer
map.addLayer({
id: 'borders-outline',
type: 'line',
source: 'borders',
paint: {
'line-color': '#444',
'line-width': 0.5,
'line-opacity': 0.3
}
}, 'waterway-label');
currentBordersYear = currentYear;
console.log(`Loaded borders for ${currentYear}`);
} catch (error) {
console.error(`Error loading borders for ${currentYear}:`, error);
}
}
// Collision detection: find non-overlapping positions for markers
function resolveCollisions(filteredEvents) {
const positions = [];
const MIN_PIXEL_DISTANCE = 100; // Minimum distance between markers in pixels
const zoom = map.getZoom();
// Helper function to calculate pixel distance at current zoom
function getPixelDistance(coord1, coord2) {
const point1 = map.project(coord1);
const point2 = map.project(coord2);
return Math.sqrt(
Math.pow(point1.x - point2.x, 2) +
Math.pow(point1.y - point2.y, 2)
);
}
// Helper function to offset coordinates in pixel space and convert back to geo
function offsetCoordinates(coords, pixelOffsetX, pixelOffsetY) {
const point = map.project(coords);
point.x += pixelOffsetX;
point.y += pixelOffsetY;
const newCoords = map.unproject(point);
return [newCoords.lng, newCoords.lat];
}
filteredEvents.forEach((event, index) => {
const originalCoords = event.coordinates;
let adjustedCoords = [...originalCoords];
let attempts = 0;
const maxAttempts = 50;
// Check collision with existing positions
while (attempts < maxAttempts) {
let hasCollision = false;
for (const pos of positions) {
const distance = getPixelDistance(adjustedCoords, pos.displayCoords);
if (distance < MIN_PIXEL_DISTANCE) {
hasCollision = true;
break;
}
}
if (!hasCollision) {
break;
}
// Spiral out from original position in pixel space
const angle = (attempts * 137.5) * (Math.PI / 180); // Golden angle
const radius = MIN_PIXEL_DISTANCE * 0.7 * Math.sqrt(attempts + 1); // Increasing radius in pixels
const offsetX = radius * Math.cos(angle);
const offsetY = radius * Math.sin(angle);
adjustedCoords = offsetCoordinates(originalCoords, offsetX, offsetY);
attempts++;
}
positions.push({
originalCoords: originalCoords,
displayCoords: adjustedCoords,
event: event
});
});
return positions;
}
// Set hover state for connection lines and circles
function setConnectionHoverState(eventId, isHovered) {
if (!map.getSource('connection-lines')) {
return;
}
// Update global hover state
hoveredEventId = isHovered ? eventId : null;
// Re-render layers by updating the data
const currentData = map.getSource('connection-lines')._data;
// Update hover property in all features
currentData.features.forEach(feature => {
if (feature.properties.eventId === eventId) {
feature.properties.hover = isHovered;
feature.properties.faded = false;
} else {
feature.properties.hover = false;
feature.properties.faded = isHovered; // Fade out other lines/circles when something is hovered
}
});
// Update the source data to trigger re-render
map.getSource('connection-lines').setData(currentData);
// Fade out/in other markers
mapMarkers.forEach((marker, markerEventId) => {
const el = marker.getElement();
if (markerEventId === eventId) {
// Keep hovered marker fully visible
el.style.transition = 'opacity 1s ease';
el.style.opacity = '1';
} else {
// Fade out other markers
el.style.transition = 'opacity 1s ease';
el.style.opacity = isHovered ? '0.15' : '1';
}
});
}
// Draw connection lines on map
function drawConnectionLines(positions) {
// Remove existing connection layer if present
if (map.getSource('connection-lines')) {
map.removeLayer('connection-circles');
map.removeLayer('connection-lines');
map.removeSource('connection-lines');
}
// Create GeoJSON for lines and circles with event IDs
const features = [];
const circleFeatures = [];
let featureId = 0;
positions.forEach(pos => {
// Only draw line if position was adjusted
const [origLng, origLat] = pos.originalCoords;
const [dispLng, dispLat] = pos.displayCoords;
if (origLng !== dispLng || origLat !== dispLat) {
const eventId = `${pos.event.year}-${pos.event.date}-${pos.event.title}`;
// Line from display position to original position
// Note: We'll use CSS to visually position the line under the marker
features.push({
type: 'Feature',
id: featureId++,
geometry: {
type: 'LineString',
coordinates: [pos.displayCoords, pos.originalCoords]
},
properties: {
eventId: eventId,
hover: false,
faded: false
}
});
// Circle at original position
circleFeatures.push({
type: 'Feature',
id: featureId++,
geometry: {
type: 'Point',
coordinates: pos.originalCoords
},
properties: {
eventId: eventId,
hover: false,
faded: false
}
});
}
});
if (features.length > 0) {
// Add source with promoteId to use feature id for feature state
map.addSource('connection-lines', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: [...features, ...circleFeatures]
},
promoteId: 'id',
lineMetrics: true // Enable line-gradient support
});
// Add line layer (render below markers)
map.addLayer({
id: 'connection-lines',
type: 'line',
source: 'connection-lines',
filter: ['==', ['geometry-type'], 'LineString'],
paint: {
'line-color': [
'case',
['get', 'hover'],
'#213159',
'#ffffff'
],
'line-width': [
'case',
['get', 'hover'],
3,
2
],
'line-offset': 0,
'line-opacity': [
'case',
['get', 'faded'],
0.1,
1
]
}
}, 'waterway-label');
// Add circle layer (render below markers)
map.addLayer({
id: 'connection-circles',
type: 'circle',
source: 'connection-lines',
filter: ['==', ['geometry-type'], 'Point'],
paint: {
'circle-radius': [
'case',
['get', 'hover'],
5, // 10px diameter
4 // 8px diameter
],
'circle-color': [
'case',
['get', 'hover'],
'#213159', // Match label background color on hover
'#ffffff'
],
'circle-opacity': [
'case',
['get', 'faded'],
0.1, // Fade out non-hovered circles
0.8 // Normal opacity
],
'circle-stroke-width': 1,
'circle-stroke-color': [
'case',
['get', 'hover'],
'#213159',
'#ffffff'
],
'circle-stroke-opacity': [
'case',
['get', 'faded'],
0.1, // Fade out stroke too
1 // Normal opacity
]
}
}, 'waterway-label');
// Add transitions for smooth hover effect with delay
// Delay of 450ms allows event marker to complete its animation first
map.setPaintProperty('connection-lines', 'line-width-transition', {
duration: 300,
delay: 450
});
map.setPaintProperty('connection-lines', 'line-color-transition', {
duration: 300,
delay: 450
});
map.setPaintProperty('connection-lines', 'line-opacity-transition', {
duration: 1000,
delay: 450
});
map.setPaintProperty('connection-circles', 'circle-radius-transition', {
duration: 300,
delay: 450
});
map.setPaintProperty('connection-circles', 'circle-color-transition', {
duration: 300,
delay: 450
});
map.setPaintProperty('connection-circles', 'circle-stroke-color-transition', {
duration: 300,
delay: 450
});
map.setPaintProperty('connection-circles', 'circle-opacity-transition', {
duration: 1000,
delay: 450
});
map.setPaintProperty('connection-circles', 'circle-stroke-opacity-transition', {
duration: 1000,
delay: 450
});
}
}
// Update event markers on map based on current year with animation
function updateEventMarkers() {
// Get events for current year and sort by date
const yearEvents = events
.filter(e => e.year === currentYear)
.sort((a, b) => a.parsedDate - b.parsedDate);
// Determine which time period we're in (based on timeline position)
const yearProgress = currentEventIndex % EVENTS_PER_YEAR;
// Filter events based on timeline position - show events progressively
// Split events into 5 groups based on their chronological order
const eventsPerStep = Math.ceil(yearEvents.length / EVENTS_PER_YEAR);
const startIndex = 0;
const endIndex = (yearProgress + 1) * eventsPerStep;
const filteredEvents = yearEvents.slice(startIndex, endIndex);
console.log(`Showing ${filteredEvents.length} events for year ${currentYear}, period ${yearProgress}`);
// Create set of event IDs that should be visible
const visibleEventIds = new Set(filteredEvents.map(e => `${e.year}-${e.date}-${e.title}`));
// Remove markers that are no longer visible
for (const [eventId, marker] of mapMarkers.entries()) {
if (!visibleEventIds.has(eventId)) {
marker.remove();
mapMarkers.delete(eventId);
}
}
// If no events, reset to default Europe view
if (filteredEvents.length === 0) {
// Remove connection lines
if (map.getSource('connection-lines')) {
map.removeLayer('connection-circles');
map.removeLayer('connection-lines');
map.removeSource('connection-lines');
}
map.flyTo({
center: [15, 52],
zoom: 4,
duration: 800,
padding: {top: 5, bottom: 5, left: 5, right: 5},
essential: true
});
return;
}
// Calculate bounds to fit all events with padding
if (filteredEvents.length > 0) {
const bounds = new mapboxgl.LngLatBounds();
filteredEvents.forEach(event => {
bounds.extend(event.coordinates);
});
// Fit map to bounds with padding
map.fitBounds(bounds, {
padding: {top: 150, bottom: 200, left: 450, right: 450},
duration: 800,
maxZoom: 6,
essential: true,
linear: false
});
// Wait for map movement to finish, then resolve collisions
map.once('moveend', () => {
// Resolve collisions and get adjusted positions at final zoom level
const positions = resolveCollisions(filteredEvents);
// Draw connection lines
drawConnectionLines(positions);
// Create or update markers for each event
positions.forEach((pos, index) => {
const event = pos.event;
const eventId = `${event.year}-${event.date}-${event.title}`;
// Check if marker already exists
if (mapMarkers.has(eventId)) {
// Update existing marker position
const existingMarker = mapMarkers.get(eventId);
existingMarker.setLngLat(pos.displayCoords);
// Note: Event listeners remain attached to existing markers
} else {
// Create new marker
const el = document.createElement('div');
el.className = 'map-event-marker';
// Check if this event has been animated before
const hasBeenAnimated = animatedEvents.has(eventId);
// Set initial opacity based on animation state
el.style.opacity = hasBeenAnimated ? '1' : '0';
// Add image if available
if (event.image) {
const img = document.createElement('img');
img.src = event.image;
img.alt = event.title;
img.onerror = () => {
img.remove();
};
el.appendChild(img);
}
// Add label
const label = document.createElement('div');
label.className = 'event-label';
label.textContent = event.title;
el.appendChild(label);
// Click handler
el.addEventListener('click', (e) => {
e.stopPropagation();
showEventPanel(event);
});
// Hover handlers to highlight connection lines/circles
el.addEventListener('mouseenter', () => {
setConnectionHoverState(eventId, true);
});
el.addEventListener('mouseleave', () => {
setConnectionHoverState(eventId, false);
});
const marker = new mapboxgl.Marker(el)
.setLngLat(pos.displayCoords)
.addTo(map);
mapMarkers.set(eventId, marker);
// Only animate if this is the first time showing this event
if (!hasBeenAnimated) {
const delay = index * 250;
setTimeout(() => {
el.style.transition = 'opacity 600ms cubic-bezier(0.4, 0, 0.2, 1)';
el.style.opacity = '1';
}, delay);
// Mark this event as animated
animatedEvents.add(eventId);
}
}
});
});
}
}
// Show event panel
function showEventPanel(event) {
const eventImage = document.getElementById('event-image');
if (event.image) {
eventImage.src = event.image;
eventImage.style.display = 'block';
} else {
eventImage.style.display = 'none';
}
document.getElementById('event-title').textContent = event.title;
document.getElementById('event-date').textContent = event.date;
document.getElementById('event-description').textContent = event.description;
eventPanel.classList.remove('hidden');
}
// Hide event panel
function hideEventPanel() {
eventPanel.classList.add('hidden');
}
// Navigate to specific event
function goToEvent(index) {
currentEventIndex = index;
updateTimeline();
resetIdleAnimation();
}
// Idle animation for navigation buttons
let idleTimer = null;
function startIdleAnimation() {
if (!prevBtn.disabled) prevBtn.classList.add('idle');
if (!nextBtn.disabled) nextBtn.classList.add('idle');
}
function resetIdleAnimation() {
prevBtn.classList.remove('idle');
nextBtn.classList.remove('idle');
clearTimeout(idleTimer);
idleTimer = setTimeout(startIdleAnimation, 2000);
}
// Navigation
prevBtn.addEventListener('click', () => {
if (currentEventIndex > 0) {
goToEvent(currentEventIndex - 1);
resetIdleAnimation();
}
});
nextBtn.addEventListener('click', () => {
if (currentEventIndex < TOTAL_EVENTS - 1) {
goToEvent(currentEventIndex + 1);
resetIdleAnimation();
}
});
closePanel.addEventListener('click', hideEventPanel);
// Keyboard navigation
document.addEventListener('keydown', (e) => {
if (e.key === 'ArrowLeft') {
prevBtn.click();
resetIdleAnimation();
} else if (e.key === 'ArrowRight') {
nextBtn.click();
resetIdleAnimation();
} else if (e.key === 'Escape') {
hideEventPanel();
}
});
// Initialize when map is loaded
map.on('load', () => {
console.log('Map loaded');
// Check what language fields are available in the map style
checkAvailableLanguages();
// Set initial language to Swedish
setMapLanguage('sv');
initTimeline();
loadEvents();
// Show help overlay on first load
helpOverlay.classList.remove('hidden');
// Start idle animation after 2 seconds
idleTimer = setTimeout(startIdleAnimation, 2000);
});
// Error handling
map.on('error', (e) => {
console.error('Map error:', e);
});
// Function to check what language fields are available in the map style
function checkAvailableLanguages() {