-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts-local.html
891 lines (746 loc) · 33.8 KB
/
scripts-local.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
{% include_relative scripts-listeners.html %}
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""/>
<script>
let events = {% include metadata/Events.json %};
//////////////////////////////
//
// buildLookupTables –-
//
function buildLookupTables() {
let metadata = MAP.METADATA;
if (!metadata){
console.warn("No METADATA!");
return;
}
for (sheet in metadata) {
if (sheet === "Events"){
continue;
}
buildLookupTable(sheet);
}
}
//////////////////////////////
//
// buildLookupTable –-
//
function buildLookupTable(sheet) {
let sheetArray = MAP.METADATA[sheet];
if (!sheetArray || !Array.isArray(sheetArray)){
console.warn("No METADATA FOR", sheet);
return;
}
MAP.lookup[sheet] = {};
const lookup = MAP.lookup[sheet];
// Dynamically determine the ID key
let idKey = null;
if (sheetArray.length > 0) {
const firstEntry = sheetArray[0];
for (let key in firstEntry) {
if (key.toLowerCase().includes("id")) {
idKey = key;
break;
}
}
}
if (!idKey) {
console.warn(`No ID key found for sheet ${sheet}`);
return;
}
for (let entry of sheetArray) {
let id = entry[idKey];
if (!id){
console.warn("NO ID FOR ENTRY", entry);
continue;
}
lookup[id] = entry;
}
}
//////////////////////////////
//
// findInfo –- find info on other sheets
//
function findInfo(id, sheet) {
return MAP.lookup[sheet] ? MAP.lookup[sheet][id] : null;
}
//////////////////////////////
//
// doMapSetup –- Function to setup the map and add markers
//
function doMapSetup(opts) {
if (typeof opts === "undefined") {
console.error("doMapSetup input parameter is undefined");
return;
}
if (typeof opts.name === "undefined") {
console.error("doMapSetup name parameter is undefined");
return;
}
if (typeof opts.view === "undefined") {
console.error("view option is not specified");
return;
}
if (typeof opts.zoom === "undefined") {
console.warn("zoom option is not specified, setting to 4");
opts.zoom = 5;
}
if (typeof opts.zoomMin === "undefined") {
console.warn("zoomMin option is not specified, setting to zoom");
opts.zoomMin = opts.zoom;
}
if (typeof opts.zoomMax === "undefined") {
console.warn("zoomMax option is not specified, setting to 19");
opts.zoomMax = 19;
}
if (map) {
map.eachLayer(function(layer) {
map.removeLayer(layer);
});
map.remove();
}
map = L.map(opts.name).setView(opts.view, opts.zoom);
map.options.minZoom = opts.zoomMin;
let mapImage = "https://tile.openstreetmap.org/{z}/{x}/{y}.png";
let attribution = "© <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>";
L.tileLayer(mapImage, {
maxZoom: opts.zoomMax,
attribution: attribution
}).addTo(map);
// Get names of variables
let ID = MAP.cindex("EV", "EVID");
let LOC = MAP.cindex("EV", "LOCID");
let BIOID = MAP.cindex("EV", "BIOID");
let EINFO = MAP.cindex("EV", "EINFO");
let DATERANGE = MAP.cindex("EV", "DATERANGE");
let EYEAR = MAP.cindex("EV","EYEAR");
let EMONTH = MAP.cindex("EV","EMONTH");
let EDAY = MAP.cindex("EV","EDAY");
let LYEAR = MAP.cindex("EV","LYEAR");
let LMONTH = MAP.cindex("EV","LMONTH");
let LDAY = MAP.cindex("EV","LDAY");
let LOCID = MAP.cindex("LOC", "LOCID");
let LOCNAME = MAP.cindex("LOC", "LOCNAME");
let LOCCITY = MAP.cindex("LOC", "CITY");
let COORD = MAP.cindex("LOC", "COORD");
let BCONAME = MAP.cindex("BCO", "BCONAME");
let BMUNAME = MAP.cindex("BMU", "BMUNAME");
let BNONAME = MAP.cindex("BNO", "BNONAME");
// Get variable names for certainty markers
let CERTLOC = MAP.cindex("EV", "CERTLOC");
let CERTEDATE = MAP.cindex("EV", "CERTEDATE");
let CERTLDATE = MAP.cindex("EV", "CERTLDATE");
let CERTRANGE = MAP.cindex("EV", "CERTRANGE");
// Get variable names for document entries
let DOEID = MAP.cindex("EV", "DOEID");
let ARDID = MAP.cindex("DOE", "ARDID");
let ARDARC = MAP.cindex("ARD", "ARC");
let ARDARCFOND = MAP.cindex("ARD", "ARCFOND");
let ARDSIG = MAP.cindex("ARD", "SIG");
// Get variable names for bibliography entries
let BIBID = MAP.cindex("EV", "BIBID");
let BIBPAGES = MAP.cindex("EV", "BIBPAGES");
let AUTHOR = MAP.cindex("BIB", "AUTHOR");
let ARTNAME = MAP.cindex("BIB", "ARTNAME");
let VOLNAME = MAP.cindex("BIB", "VOLNAME");
let EDIT = MAP.cindex("BIB", "EDIT");
let VOLNO = MAP.cindex("BIB", "VOLNO");
let EDITION = MAP.cindex("BIB", "EDITION");
let SER = MAP.cindex("BIB", "SER");
let BIBLOC = MAP.cindex("BIB", "LOC");
let PUB = MAP.cindex("BIB", "PUB");
let YEAR = MAP.cindex("BIB", "YEAR");
let PAGES = MAP.cindex("BIB", "PAGES");
let URL = MAP.cindex("BIB", "URL");
// Establish variables
let eventid = "";
let person = "";
let personprocessed = "";
let locationid = "";
let locationprocessed = "";
let largerlocation = "";
let daterange = "";
let earliestyear = "";
let earliestmonth = "";
let earliestday = "";
let earliestdate = "";
let latestyear = "";
let latestmonth = "";
let latestday = "";
let latestdate = "";
let coordinates = "";
let coordinatesprocessed = "";
let description = "";
let docentry = "";
let bibliography = "";
let archiveprocessed = "";
let nameshort = "";
markers = []; // Clear the markers array
for (let i = 0; i < events.length; i++) {
// Only process events for map if there's a location and a date
if (events[i][LOC] && events[i][DATERANGE]) {
eventid = events[i][ID];
locationid = events[i][LOC];
let locationInfo = findInfo(locationid, "Locations");
locationprocessed = locationInfo[LOCNAME];
// Find larger location, if applicable
let largerlocation = locationInfo[LOCCITY];
if (largerlocation) {
locationprocessed += `, ${largerlocation}`;
}
// Get name of person
if (events[i][BIOID]) {
person = events[i][BIOID];
let composerInfo = findInfo(person, "Bio_Composers");
let musicianInfo = findInfo(person, "Bio_Musicians");
let nonmusicianInfo = findInfo(person, "Bio_Nonmusicians");
// Get first three letters to determine type of ID
nameshort = person.substring(0, 3);
// Get name from composer, musician, or non-musician biography sheets
if (nameshort === "BCO" && composerInfo) {
personprocessed = composerInfo[BCONAME];
} else if (nameshort === "BMU" && musicianInfo) {
personprocessed = musicianInfo[BMUNAME];
} else if (nameshort === "BNO" && nonmusicianInfo) {
personprocessed = nonmusicianInfo[BNONAME];
} else {
continue;
}
}
// Get dates
if (events[i][DATERANGE]) {
daterange = events[i][DATERANGE];
// Get earliest/latest years, months, dates
earliestyear = events[i][EYEAR];
earliestmonth = events[i][EMONTH];
earliestday = events[i][EDAY];
latestyear = events[i][LYEAR];
latestmonth = events[i][LMONTH];
latestday = events[i][LDAY];
// Put together earliest and latest dates in YEAR-MONTH-DAY format
earliestdate = `${earliestyear}-${earliestmonth}-${earliestday}`;
latestdate = `${latestyear}-${latestmonth}-${latestday}`;
}
// Get event description
if (events[i][EINFO]) {
description = events[i][EINFO];
}
// Get certainty information
let locationCertainty = events[i][CERTLOC];
let earliestDateCertainty = events[i][CERTEDATE];
let latestDateCertainty = events[i][CERTLDATE];
let rangeCertainty = events[i][CERTRANGE];
// Add information about location uncertainty to location
// Only consider location certainty if not certain
// Return a console message if the location certainty is not 1, 2, 3, or 4
if (locationCertainty && locationCertainty !== "1"){
if (locationCertainty === "2"){
locationprocessed += ` [probable]`;
} else if (locationCertainty === "3"){
locationprocessed += ` [possible]`;
} else if (locationCertainty === "4"){
locationprocessed += ` [doubtful]`;
} else {
console.warn("Forbidden location certainty for", eventid, locationCertainty);
continue;
}
}
// Add information about date uncertainty to date range
// Only consider date certainty if not certain
// Return a console message if the date certainty is not 1, 2, 3, or 4
if (earliestDateCertainty && earliestDateCertainty !== "1"){
if (earliestDateCertainty === "2"){
daterange += ` [earliest date probable]`;
} else if (earliestDateCertainty === "3"){
daterange += ` [earliest date possible]`;
} else if (earliestDateCertainty === "4"){
daterange += ` [earliest date doubtful]`;
} else {
console.warn("Forbidden earliestDateCertainty certainty for", eventid, earliestDateCertainty);
continue;
}
}
if (latestDateCertainty && latestDateCertainty !== "1"){
if (latestDateCertainty === "2"){
daterange += ` [latest date probable]`;
} else if (latestDateCertainty === "3"){
daterange += ` [latest date possible]`;
} else if (latestDateCertainty === "4"){
daterange += ` [latest date doubtful]`;
} else {
console.warn("Forbidden latestDateCertainty certainty for", eventid, latestDateCertainty);
continue;
}
}
// Add information about event uncertainty to description
// Only consider event certainty if not certain
// Return a console message if the event certainty is not 1, 2, 3, or 4
if (rangeCertainty && rangeCertainty !== "1"){
if (rangeCertainty === "2"){
description += ` [event probable]`;
} else if (rangeCertainty === "3"){
description += ` [event possible]`;
} else if (rangeCertainty === "4"){
description += ` [event doubtful]`;
} else {
console.warn("Forbidden rangeCertainty certainty for", eventid, rangeCertainty);
continue;
}
}
// Get archival signature for linked document entry, if applicable
if (events[i][DOEID]) {
docentry = events[i][DOEID];
let archivalid = "";
// Split docentries if multiple documents separated by a semicolon
if (docentry.match(";")){
docentry = docentry.trim().split(/\s*;\s*/);
} else {
docentry = [ docentry ];
}
for (let j=0; j<docentry.length; j++){
// Get archival document from document entries data
let documentInfo = findInfo(docentry[j], "Doc_Entries");
// Only process if there's an archive linked to the document
if (documentInfo) {
archivalid = documentInfo[ARDID];
}
// Get archival signature from archival documents data
if (archivalid) {
let archivalInfo = findInfo(archivalid, "Archival_Docs");
let archive = archivalInfo[ARDARC];
let fond = archivalInfo[ARDARCFOND];
let signature = archivalInfo[ARDSIG];
if (archive && j == 0){
archiveprocessed += `<br><b>Archival Document:</b> ${archive}`;
} else if (archive) {
archiveprocessed += `; ${archive}`;
}
if (archive && fond){
archiveprocessed += `, ${fond}`;
}
if (archive && fond && signature){
archiveprocessed += ` ${signature}`;
}
}
}
}
// Get bibliography, if applicable
if (events[i][BIBID]){
let bibliographyid = events[i][BIBID];
let bibliographypages = "";
if (events[i][BIBPAGES]){
bibliographypages = events[i][BIBPAGES];
}
// Split bibliography entries if multiple entries separated by a semicolon
if (bibliographyid.match(";")){
bibliographyid = bibliographyid.trim().split(/\s*;\s*/);
} else {
bibliographyid = [ bibliographyid ];
}
if (bibliographypages.match(";")){
bibliographypages = bibliographypages.trim().split(/\s*;\s*/);
} else {
bibliographypages = [ bibliographypages ];
}
for (let j=0; j<bibliographyid.length; j++){
let bibliographyInfo = findInfo(bibliographyid[j], "Bibliography");
let author = bibliographyInfo[AUTHOR];
let articlename = bibliographyInfo[ARTNAME];
let volumename = bibliographyInfo[VOLNAME];
let editor = bibliographyInfo[EDIT];
let volumenumber = bibliographyInfo[VOLNO];
let edition = bibliographyInfo[EDITION];
let series = bibliographyInfo[SER];
let bibliographylocation = bibliographyInfo[BIBLOC];
let publisher = bibliographyInfo[PUB];
let year = bibliographyInfo[YEAR];
let pages = bibliographyInfo[PAGES];
let bibliographyurl = bibliographyInfo[URL];
if (author){
bibliography += `${author}, `;
}
if (articlename) {
bibliography += `"${articlename}," `;
}
if (volumename){
bibliography += `<i>${volumename}</i>`;
}
if (editor) {
bibliography += `, ed. ${editor}`;
}
if (volumenumber) {
bibliography += ` ${volumenumber}`;
}
if (bibliographylocation && publisher && year){
bibliography += ` (${bibliographylocation}: ${publisher}, ${year})`;
} else if (bibliographylocation && year){
bibliography += ` (${bibliographylocation}: ${year})`;
} else if (year) {
bibliography += ` (${year})`;
}
if (pages){
bibliography += `: ${pages}`;
}
if (pages && bibliographypages) {
bibliography += `, at ${bibliographypages[j]}`;
} else if (bibliographypages > 0) {
bibliography += `, ${bibliographypages[j]}`;
}
if (j < bibliographyid.length - 1){
bibliography += `; and `
} else {
bibliography += `.`;
}
}
}
// Get coordinates
coordinates = locationInfo[COORD];
// Map points if there are coordinates and the earliest and latest years are defined
if (coordinates !== undefined && earliestyear !== undefined && latestyear !== undefined) {
// Convert the comma-separated string into an array
let coordsArray = coordinates.split(',').map(Number);
// Ensure coordinates are in the format [longitude, latitude]
if (coordsArray.length === 2 && !isNaN(coordsArray[0]) && !isNaN(coordsArray[1])) {
// Swap latitude and longitude
coordinatesprocessed = [coordsArray[1], coordsArray[0]];
const geoJsonFeature = {
type: "Feature",
geometry: {
type: "Point",
coordinates: coordinatesprocessed // Ensure this is [longitude, latitude]
},
properties: {
popupContent: description ?
`<b>Person:</b> ${personprocessed}<br>
<b>Date:</b> ${daterange}<br>
<b>Place:</b> ${locationprocessed} <br>
<b>Description:</b> ${description} ${archiveprocessed}
${bibliography ? `<b><br>Bibliography:</b> ${bibliography}` : ''}` :
`<b>Person:</b> ${personprocessed}<br>
<b>Date:</b> ${daterange}<br>
<b>Place:</b> ${locationprocessed} ${archiveprocessed}
${bibliography ? `<br><b>Bibliography:</b> ${bibliography}` : ''}`,
locationCertainty: locationCertainty,
earliestDateCertainty: earliestDateCertainty,
latestDateCertainty: latestDateCertainty,
rangeCertainty: rangeCertainty,
earliestyear: earliestyear,
latestyear: latestyear,
}
};
// Determine color based on nameshort
let fillColor;
if (nameshort === "BCO") {
fillColor = "#440154"; // Color for composers (BCO)
} else if (nameshort === "BMU") {
fillColor = "#23ed5c"; // Color for musicians (BMU)
} else if (nameshort === "BNO") {
fillColor = "#fde725"; // Color for non-musicians (BNO)
} else {
fillColor = "#0c8aff"; // Default color if no match
}
const geojsonMarkerOptions = {
radius: 5,
fillColor: fillColor, //black fill color
color: "#000000", //Black border color
weight: 1,
opacity: 1,
fillOpacity: 0.8
};
let marker = L.geoJson(geoJsonFeature, {
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, geojsonMarkerOptions);
}
}).bindPopup(geoJsonFeature.properties.popupContent);
markers.push({ marker, personID: person, text: `[${daterange}] ${personprocessed}: ${description}`, locationCertainty: locationCertainty, earliestDateCertainty: earliestDateCertainty, latestDateCertainty: latestDateCertainty, rangeCertainty: rangeCertainty, earliestyear: earliestyear, latestyear: latestyear });
} else {
console.warn('Invalid coordinates:', coordsArray, events[i]);
continue;
}
} else {
continue;
}
// Reset variables
person = "";
personprocessed = "";
locationid = "";
locationprocessed = "";
largerlocation = "";
daterange = "";
coordinates = "";
coordinatesprocessed = "";
description = "";
docentry = "";
bibliography = "";
bibliographypages = "";
archiveprocessed = "";
earliestyear = "";
earliestmonth = "";
earliestday = "";
earliestdate = "";
latestyear = "";
latestmonth = "";
latestday = "";
latestdate = "";
}
}
// Apply the initial filter
filterMarkers();
}
// Helper function to fetch checkbox states
function getCheckboxStates() {
return {
showComposers: document.querySelector("#composer-select").checked,
showMusicians: document.querySelector("#musician-select").checked,
showNonMusicians: document.querySelector("#non-musician-select").checked,
showCertainty: document.querySelector("#certainty-select").checked,
};
}
// Helper function to determine if a marker should be shown based on checkbox states and certainty
function shouldShowMarker(type, checkboxStates, certainties) {
let { showComposers, showMusicians, showNonMusicians, showCertainty } = checkboxStates;
let { locationCertainty, earliestDateCertainty, latestDateCertainty, rangeCertainty } = certainties;
let show = false;
if ((type === "BCO" && showComposers) ||
(type === "BMU" && showMusicians) ||
(type === "BNO" && showNonMusicians)) {
show = true;
}
if (showCertainty) {
if (locationCertainty && locationCertainty !== "1" && locationCertainty !== "2") show = false;
if (earliestDateCertainty && earliestDateCertainty !== "1" && earliestDateCertainty !== "2") show = false;
if (latestDateCertainty && latestDateCertainty !== "1" && latestDateCertainty !== "2") show = false;
if (rangeCertainty && rangeCertainty !== "1" && rangeCertainty !== "2") show = false;
}
return show;
}
// Helper function to add or remove marker from map
function updateMarkerOnMap(marker, show) {
if (show) {
if (!map.hasLayer(marker)) {
marker.addTo(map);
}
} else {
if (map.hasLayer(marker)) {
map.removeLayer(marker);
}
}
}
// Function to filter markers based on checkboxes and breakdown by decade (1400-1600), with optional searchRegex
function filterMarkers(searchRegex = null) {
const checkboxStates = getCheckboxStates();
const histogramCounts = initializeDecades(1400, 1590); // Initialize all decades between 1400-1590 with 0 count
const histogramRow = document.getElementById("histogramRow");
const minYear = document.getElementById('date-slider-min').value;
const maxYear = document.getElementById('date-slider-max').value;
// Clear the histogram before adding new values
histogramRow.innerHTML = '';
let searchCount = 0; // Track how many markers match the filters
markers.forEach(({ marker, personID, locationCertainty, earliestDateCertainty, latestDateCertainty, rangeCertainty, earliestyear, latestyear, text }) => {
const type = personID.substring(0, 3); // Example of how type could be extracted
const certainties = { locationCertainty, earliestDateCertainty, latestDateCertainty, rangeCertainty };
let show = shouldShowMarker(type, checkboxStates, certainties);
// Apply search filter if searchRegex is provided
if (show && searchRegex && !text.match(searchRegex)) {
show = false;
}
// If the marker is shown after all filtering, update histogram counts
if (show) {
searchCount++;
// Clamp earliestyear and latestyear to the 1400-1600 range
const clampedEarliest = Math.max(1400, earliestyear);
const clampedLatest = Math.min(1600, latestyear);
// Get all decades between the clamped earliest year and clamped latest year
const decades = getDecades(clampedEarliest, clampedLatest);
decades.forEach(decade => {
if (histogramCounts[decade] !== undefined) {
histogramCounts[decade] += 1; // Increment the count for this decade
}
});
}
// Filter out markers that fall outside of the date range (minYear to maxYear)
// Must be done after the histogram is created
if (earliestyear > maxYear || latestyear < minYear) {
show = false;
}
// Update marker visibility on the map
updateMarkerOnMap(marker, show);
});
// Update search count display
document.querySelector("#search-count").innerHTML = `[${searchCount} ${searchCount === 1 ? "event" : "events"}]`;
// Now that we have the counts, update the histogram
updateHistogram(histogramCounts);
// Update the map size
map.invalidateSize();
}
// Function to initialize all decades between a given start and end year with 0
function initializeDecades(startYear, endYear) {
const histogramCounts = {};
for (let year = startYear; year <= endYear; year += 10) {
histogramCounts[year] = 0; // Initialize each decade with 0 events
}
return histogramCounts;
}
// Function to get all decades between two years
function getDecades(earliestYear, latestYear) {
const decades = [];
let startDecade = getDecade(earliestYear); // Get the starting decade (e.g., 1990 for 1994)
const endDecade = getDecade(latestYear); // Get the ending decade (e.g., 2000 for 2002)
// Loop through each decade from the start to end
for (let decade = startDecade; decade <= endDecade; decade += 10) {
decades.push(decade);
}
return decades; // Return array of decades, e.g., [1990, 2000] if marker spans 1994 to 2002
}
// Function to calculate the decade from a given year
function getDecade(year) {
return Math.floor(year / 10) * 10; // Returns the decade, e.g., 1990 for years 1990-1999
}
// Function to update the histogram based on counts
function updateHistogram(histogramCounts) {
const histogramRow = document.getElementById("histogramRow");
histogramRow.innerHTML = ''; // Clear existing content
const decades = Object.keys(histogramCounts).sort((a, b) => a - b); // Sort decades in ascending order
// Find the maximum and minimum count to scale the height proportionally
const maxCount = Math.max(...Object.values(histogramCounts));
const minCount = Math.min(...Object.values(histogramCounts));
// Define a maximum height, e.g., 50px
const maxHeight = 50;
// Create a cell for both maxCount and minCount labels at the left of the histogram
const labelCell = document.createElement("td");
labelCell.style.verticalAlign = "top";
labelCell.style.position = "relative";
labelCell.style.width = "50px"; // Adjust width for both labels
// Create maxCount label
const maxCountLabel = document.createElement("div");
maxCountLabel.textContent = maxCount;
maxCountLabel.style.fontSize = "12px";
maxCountLabel.style.color = "black";
maxCountLabel.style.position = "relative";
maxCountLabel.style.left = "-25px"; // Move the label outside of the histogram
maxCountLabel.style.top = "-10px"; // Move it up slightly
// Create minCount label
const minCountLabel = document.createElement("div");
minCountLabel.textContent = minCount;
minCountLabel.style.fontSize = "12px";
minCountLabel.style.color = "black";
minCountLabel.style.position = "relative";
minCountLabel.style.left = "-15px"; // Align with maxCount label
minCountLabel.style.top = "30px"; // Position it below maxCount label
// Append both labels to the same labelCell
labelCell.appendChild(maxCountLabel);
labelCell.appendChild(minCountLabel);
histogramRow.appendChild(labelCell); // Add the label cell to the row
// Create the histogram bars
decades.forEach(decade => {
const count = histogramCounts[decade];
const cell = document.createElement("td");
const div = document.createElement("div");
div.className = "count";
// Scale the height proportionally to the maximum count
const height = (count / maxCount) * maxHeight; // Proportional height
div.style.height = height + "px"; // Set the height of the bar
div.title = `${count} events in the ${decade}s`;
cell.appendChild(div);
histogramRow.appendChild(cell); // Add the bar to the row
});
}
//////////////////////////////
//
// UserSearch –- freetext search through data
//
function UserSearch(skipFilter = false) {
const input = document.querySelector("#input");
if (!input) return;
const regex = new RegExp(input.value, 'i');
let searchCount = 0;
const checkboxStates = getCheckboxStates();
const minYear = document.getElementById('date-slider-min').value;
const maxYear = document.getElementById('date-slider-max').value;
markers.forEach(({ marker, text, personID, locationCertainty, earliestDateCertainty, latestDateCertainty, rangeCertainty, earliestyear, latestyear }) => {
const type = personID.substring(0, 3);
const certainties = { locationCertainty, earliestDateCertainty, latestDateCertainty, rangeCertainty };
let show = shouldShowMarker(type, checkboxStates, certainties) && text.match(regex);
// Apply chronology filter based on date slider
if (earliestyear > maxYear || latestyear < minYear) {
show = false;
}
updateMarkerOnMap(marker, show);
if (show) searchCount++;
});
// Update the search count display
document.querySelector("#search-count").innerHTML = `[${searchCount} ${searchCount === 1 ? "event" : "events"}]`;
// Trigger filterMarkers and pass the search regex to update the histogram as well
filterMarkers(regex);
map.invalidateSize();
}
// Function to update the active value labels and reapply filters
function updateDateRange() {
const sliderMin = document.getElementById('date-slider-min');
const sliderMax = document.getElementById('date-slider-max');
const startActiveLabel = document.getElementById('slider-start-active-label');
const endActiveLabel = document.getElementById('slider-end-active-label');
let minValue = parseInt(sliderMin.value, 10);
let maxValue = parseInt(sliderMax.value, 10);
// Ensure the min handle doesn't exceed the max handle
if (minValue > maxValue) {
minValue = maxValue;
sliderMin.value = minValue;
}
// Ensure the max handle doesn't fall below the min handle
if (maxValue < minValue) {
maxValue = minValue;
sliderMax.value = maxValue;
}
// Update the global date range with year values
currentDateRange[0] = minValue;
currentDateRange[1] = maxValue;
// Update the labels
startActiveLabel.textContent = currentDateRange[0];
endActiveLabel.textContent = currentDateRange[1];
// Position the labels dynamically
const sliderWidth = sliderMin.offsetWidth;
const minPercent = (minValue - sliderMin.min) / (sliderMin.max - sliderMin.min);
const maxPercent = (maxValue - sliderMin.min) / (sliderMin.max - sliderMin.min);
// Adjust the rate of movement by scaling down the percentage
const adjustmentFactor = 0.975; // Adjust this value to control the rate of movement
const adjustedMinPercent = minPercent * adjustmentFactor;
const adjustedMaxPercent = maxPercent * adjustmentFactor;
startActiveLabel.style.left = `${adjustedMinPercent * sliderWidth}px`;
endActiveLabel.style.left = `${adjustedMaxPercent * sliderWidth}px`;
// Call a function to reapply filters based on the selected range, or go to UserSearch depending on whether there is text.
const input = document.querySelector("#input");
if (input){
UserSearch();
} else {
filterMarkers();
}
}
function updateSliderBackground() {
const sliderMin = document.getElementById("date-slider-min");
const sliderMax = document.getElementById("date-slider-max");
const minValue = parseInt(sliderMin.value);
const maxValue = parseInt(sliderMax.value);
const totalRange = sliderMin.max - sliderMin.min;
const minPercent = ((minValue - sliderMin.min) / totalRange) * 100;
const maxPercent = ((maxValue - sliderMax.min) / totalRange) * 100;
// Define a constant width for the black portion (in percentage)
const blackWidth = 2; // For example, 1% width in front of the thumb
// The black portion should be directly in front of the min thumb, but never extend beyond maxPercent
const blackEnd = Math.min(minPercent + blackWidth, maxPercent);
// Apply gradient as background
sliderMin.style.background = `linear-gradient(to right,
#ddd 0%,
#ddd ${minPercent}%,
black ${minPercent}%,
black ${blackEnd}%,
#1E90FF ${blackEnd}%,
#1E90FF ${maxPercent}%,
#ddd ${maxPercent}%,
#ddd 100%)`;
sliderMax.style.background = sliderMin.style.background;
}
</script>