This repository has been archived by the owner on Dec 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplasma.html
859 lines (765 loc) · 26.9 KB
/
plasma.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ImmunHelden Map</title>
<link rel="stylesheet" href="assets/css/leaflet.css">
<script src="assets/js/leaflet.min.js"></script>
<script src="assets/js/jquery.min.js"></script>
<style>
@import url("https://fonts.googleapis.com/css?family=Raleway:200,300,400,500,600");
body {
font-family: "Raleway", Arial, Helvetica, sans-serif;
margin: 0;
}
a:link, a:visited, a:hover, a:active {
text-decoration: none;
color: #393939;
}
path {
stroke: rgb(136, 136, 136);
stroke-opacity: 0.2;
stroke-width: 1;
stroke-linecap: round;
stroke-linejoin: round;
fill: #008000;
fill-opacity: 0.025;
}
div#osm-map {
position: absolute;
left: 0px;
top: 44px;
width: 100%;
height: calc(100% - 44px);
box-shadow: 3px 2px 15px #bbb;
z-index: 10010;
}
#osm-map-lock {
position: absolute;
z-index: 10013;
left: 0px;
top: 0px;
width: 100%;
height: 0px;
}
a#osm-map-lock-link {
display: block;
width: 100%;
height:100%;
}
a#osm-map-lock-link:hover {
background: #0001;
}
#osm-map-pane {
position: absolute;
z-index: 10003;
top: 44px;
right: 0px;
width: 400px;
bottom: 0px;
display: none;
}
@media only screen and (max-width: 600px) {
#osm-map-pane {
width: 100%;
z-index: 10011;
background: #fff;
}
}
#osm-map-details {
margin: 0;
border: 0;
width: 100%;
height: calc(100% - 2rem);
overflow: scroll;
}
#osm-map-pane-close {
height: 1.75rem;
background-color: #eee;
display: flex;
justify-content: center;
align-items: center;
}
img.spin-loading {
width: 100%;
height: auto;
}
div.popup-pin p {
margin-top: 0;
margin-bottom: 0.75em;
}
div.popup-pin td {
vertical-align: top;
}
div.popup-pin img.expander {
width: auto;
height: 1.2rem;
}
div.popup-immuneheroes b {
font-size: 1.5rem;
color: #d3303b;
}
div.popup-immuneheroes img {
height: 1.2rem;
width: auto;
}
div.popup-clinic img {
height: 2rem;
float: left;
}
div.popup-clinic div {
padding-left: 2.5rem;
padding-bottom: 0.5rem;
}
.nuts-polygon {
fill: #479325;
stroke: #479325;
stroke-width: 1.5px;
stroke-opacity: 0.75;
fill-opacity: 0.05;
stroke-linecap: round;
}
header {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 44px;
font-family: "Raleway", Arial, Helvetica, sans-serif;
display: flex;
align-items:center;
font-weight: 600;
font-size: 1rem;
overflow: hidden;
}
header a:link, header a:visited, header a:hover, header a:active {
color: #393939;
text-decoration: none;
padding: 0 10px;
}
#platforms table {
margin-top: 4px;
}
/*
#platforms img {
height: 1rem;
padding: 0 0.25rem;
}
#platforms td:first-child {
text-align: center;
vertical-align: middle;
}
*/
.beta-ribbon-wrapper {
width: 130px;
height: 130px;
overflow: hidden;
position: fixed;
top: 0;
right: 0;
z-index: 10012;
}
.beta-ribbon {
font-weight: bold;
text-align: center;
transform: rotate(45deg);
position: relative;
padding: 7px 0;
left: 0px;
top: 11px;
width: 200px;
background-color: #ffea94;
background-image: linear-gradient(to bottom, #ffea94, #ffea5E);
}
.beta-ribbon a:link, .beta-ribbon a:visited, .beta-ribbon a:hover, .beta-ribbon a:active {
font-size: 18px;
text-decoration: none;
color: #393939;
}
</style>
</head>
<body>
<div class="beta-ribbon-wrapper" id="beta-ribbon">
<div class="beta-ribbon">
<a target="_blank" href="https://github.com/weliveindetail/WirVsVirusMap">Beta</a>
</div>
</div>
<header id="header">
<a href="https://twitter.com/WirvsVirusHack" target="_blank">
<img src="images/wirvsvirusSE.png" alt="wirvsvirus" style="height: 40px; margin-left: 10px; margin-top: 2px;">
</a>
<a href="https://twitter.com/search?q=%23SpendeCOVIDPlasma" target="_blank" style="padding-bottom: 4px;">
#SpendeCOVIDPlasma
</a>
</header>
<div id="osm-map"></div>
<div id="osm-map-lock">
<a href="plasma.html" target="_parent" id="osm-map-lock-link"></a>
</div>
<div id="osm-map-pane">
<!-- Allow scripts temporarily to initiate follow-up dialog -->
<iframe id="osm-map-details"></iframe>
<div id="osm-map-pane-close">
<a href="#" onclick="closeDetails();" style="display: block;">
Einklappen >>
</a>
</div>
</div>
<script>
if (!String.prototype.format) {
String.prototype.format = function() {
var args = Array.prototype.slice.call(arguments);
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
function parseBool(string) {
if (string) {
switch (string.toLowerCase().trim()) {
case "true": case "yes": case "1": case "on": return true;
case "false": case "no": case "0": case "off": case null: return false;
default: return Boolean(string);
}
}
return false;
}
function parseBaseUrl(url) {
const regex = /[^?#]+/g;
const match = regex.exec(url);
return match.hasOwnProperty('0') ? match[0] : null;
}
function parseAnchor(url) {
const apos = url.lastIndexOf('#');
return (apos > 0 && apos < url.length - 1) ? url.substring(apos + 1) : null;
}
function parseUrlParams(url) {
const regex = /[?&]([^=#]+)=([^&#]*)/g;
let params = {};
let match;
while(match = regex.exec(url)) {
params[match[1]] = match[2];
}
return params;
}
// TODO: Only for demo purpose
//const demoPlatformsStr = 'Demo ImmunHelden.de|https://immunhelden.de/map,Demo Static|https://weliveindetail.github.io/WirVsVirusMap/demo_static'
const baseUrl = parseBaseUrl(window.location.href);
const baseUrlDir = baseUrl.substring(0, baseUrl.length - 12);
console.log(baseUrlDir);
const plasmaPlatfromStr = '#SpendeCOVIDPlasma|' + baseUrlDir + '/plasma_beta';
const defaultPlatformsStr = plasmaPlatfromStr;
function parsePlatforms(params) {
const platformsStr = decodeURIComponent(params.platforms || defaultPlatformsStr);
return platformsStr.split(',').map(function(pair) {
const nameUrlArray = pair.split('|');
return { "name": nameUrlArray[0], "url": nameUrlArray[1] };
});
}
function createBaseLayer() {
return L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager_labels_under/{z}/{x}/{y}.png',
{ attribution:
'<a href="https://carto.com/help/building-maps/basemap-list/">Map tiles by Carto under CC BY 3.0</a> | ' +
'<a href="http://osm.org/copyright">Data by OpenStreetMap under ODbL</a>'
});
}
L.Control.Platforms = L.Control.extend({
onAdd: function(map) {
var div = L.DomUtil.create('div', 'leaflet-bar');
div.style.backgroundColor = "#fff";
div.style.padding = "3px 8px";
div.id = "platforms-pane";
var html = '';
html += '<table>';
html += '<tr><th>';
html += ' Anzeigen und Gesuche';
html += '</th></tr>';
for (var i = 0; i < platforms.length; i++) {
html += '<tr><td>';
html += '<input type="checkbox" id="platform{0}" checked onchange="togglePlatform(this, {0});">'.format(i);
html += '<label for="platform{0}" title="Anzeigen und Gesuche von {1}">'.format(i, platforms[i].name);
html += platforms[i].name;
html += '</label>';
html += '</td></tr>';
}
html += '</table>';
div.innerHTML = html;
return div;
}
});
function loadJson(fileUrl) {
return new Promise(function(resolve, reject) {
$.ajax({ url: fileUrl })
.fail(() => reject("Error querying " + fileUrl))
.done(content => {
resolve((typeof content === 'string') ? JSON.parse(content) : content);
});
});
}
function loadPlain(fileUrl) {
return new Promise(function(resolve, reject) {
$.ajax({ url: fileUrl })
.fail(() => reject("Error querying " + fileUrl))
.done(content => resolve(content));
});
}
var params = parseUrlParams(window.location.href);
var initialZoom = params.zoom || 6;
var interactive = params.hasOwnProperty("interactive") ? parseBool(params.interactive) : true;
var showHeader = params.hasOwnProperty("header") ? parseBool(params.header) : true;
var overlayHref = params.hasOwnProperty("href") ? decodeURIComponent(params.href) : null;
var platforms = parsePlatforms(params);
if (!interactive) {
$("#osm-map-lock").css("height", "100%");
}
if (!showHeader) {
$("#header").css("height", "0");
$("#osm-map").css("top", "0");
$("#osm-map").css("height", "100%");
$("#beta-ribbon").hide();
}
if (overlayHref) {
$("#osm-map-lock-link").attr("href", overlayHref);
}
var map = new L.map('osm-map', {
center: [51.5, 10],
zoom: initialZoom,
zoomControl: interactive,
dragging: interactive,
doubleClickZoom: interactive,
scrollWheelZoom: interactive
});
createBaseLayer().addTo(map);
var platformsView = new L.Control.Platforms({ position: 'bottomleft' });
//platformsView.addTo(map);
const defaultPinIcons = [
new L.Icon({
iconUrl: 'images/marker-icon-blood-sharp.png',
iconSize: [30, 38],
iconAnchor: [15, 37],
popupAnchor: [1, -34]
}),
new L.Icon({
iconUrl: 'images/marker-icon-2x-orange.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34]
})
];
function forEachPoint(geometry, predicate) {
if (geometry.length == 2 && !geometry[0].hasOwnProperty("length")) {
predicate(geometry);
}
else {
for (var component of geometry) {
forEachPoint(component, predicate);
}
}
}
function renderPreviewHtml(title, clickHandler) {
var html = '';
html += '<div class="popup-pin">';
html += '<h2><a href="#" onclick="' + clickHandler + '">';
html += title;
html += '</a></h2>';
html += '</div>';
return html;
}
function invalidInfo(info, message) {
console.warn("Encountered invalid item in response:", message, info);
return false;
}
function isInteger(N) {
return !isNaN(N) && parseInt(Number(N)) == N && !isNaN(parseInt(N, 10));
}
function isValidPinInfo(I) {
if (!I.hasOwnProperty("latlng") || !I.latlng.hasOwnProperty("length") || I.latlng.length != 2)
return invalidInfo(I, 'Expected pin property "latlng" to be an array with 2 elements');
if (!parseFloat(I.latlng[0]) || !parseFloat(I.latlng[1]))
return invalidInfo(I, 'Expected elements of pin property "latlng" to be floating point numbers');
if (!I.hasOwnProperty("type") || !isInteger(I.type))
return invalidInfo(I, 'Expected integer value in pin property "type"');
const typeMax = defaultPinIcons.length - 1; // TODO: customizable icons
if (I.type < 0 || I.type > typeMax)
return invalidInfo(I, 'Expected "type" property of pin in range [0..' + typeMax + ']');
if (!I.hasOwnProperty("title"))
return invalidInfo(I, 'Expected string value in pin property "title"');
return true;
}
var allPinsById = {};
var allRegionsByZip = {};
// function addPin(id, info, icon) {
// var clickHandler = "viewDetails('" + id + "');";
// var content = renderPreviewHtml(info.title, clickHandler);
// allPinsById[id].popup = L.marker(L.latLng(info.latlng), { "icon": icon }).bindPopup(content);
// allPinsById[id].popup.addTo(map);
// }
// TODO: It needs a place to live
let followupHtml;
loadPlain(baseUrlDir + "/demo_plasma/followup.html").then(
html => { console.log(html); followupHtml = html; },
err => console.error(err)
);
function viewDetails(id) {
if (!allPinsById.hasOwnProperty(id)) {
console.error("Requested invalid ID");
return;
}
$("#osm-map-details").attr("srcdoc", '<img src="images/spin_loading.gif" style="width:100%;">');
$("#osm-map-pane").show();
$("#osm-map").css("width", "calc(100% - 400px)");
const pinInfo = allPinsById[id];
console.log(pinInfo);
// TODO: if there's space left for the map at all
map.invalidateSize();
map.panTo(L.latLng(pinInfo.latlng));
//$("#platforms-pane").hide();
if (pinInfo.hasOwnProperty("details")) {
$("#osm-map-details").attr("srcdoc", pinInfo.details.html());
}
else {
const contentBaseUrl = platforms[pinInfo.platformIdx].url + "/html/";
loadPlain(contentBaseUrl + id).then(
html => {
const annotations = '<base href="' + contentBaseUrl + '" target="_blank">';
pinInfo.details = $('<div>' + annotations + html + '</div>');
const followupHandler =
'setTimeout(function() { document.getElementById("followup").style.display="flex"; }, 500)';
pinInfo.details.find('a').each((i, elem) => {
const targetUrl = $(elem).attr('href');
if (targetUrl && targetUrl.hasOwnProperty('length') && targetUrl.length > 0) {
if (targetUrl.charAt(0) == '#') {
$(elem).attr('href', baseUrl + targetUrl);
}
else {
$(elem).attr('onclick', followupHandler);
}
}
});
pinInfo.details.children('div').each((i, elem) => $(elem).css('margin-left', '25px').css('margin-right', '10px'));
pinInfo.details.append(followupHtml);
$("#osm-map-details").attr("srcdoc", pinInfo.details.html());
},
err => {
console.error("Error repsonse from: ", contentBaseUrl + id);
closeDetails()
}
);
}
}
var allRegionsByAgs = {};
function viewDetailsR(ags) {
if (!allRegionsByAgs.hasOwnProperty(ags)) {
console.error("Requested invalid region", ags);
return;
}
$("#osm-map-details").html('<img src="images/spin_loading.gif" style="width:100%;">');
$("#osm-map-pane").show();
$("#osm-map").css("width", "calc(100% - 400px)");
const regionInfo = allRegionsByAgs[ags];
console.log(regionInfo);
// TODO: if there's space left for the map at all
map.invalidateSize();
map.panTo(regionInfo.center);
//$("#platforms-pane").hide();
if (regionInfo.idsByPlatform.length == 0) {
const text = "<b>Leider liegen für diese Postleitzahlen bis jetzt keine Angebote vor:</b><br>" +
regionInfo.zips.join(", ");
$("#osm-map-details").html('<div style="padding: 1rem;">' + text + '</div>');
}
else if (regionInfo.hasOwnProperty("cachedDetails")) {
// TODO: select the divs to show!
$("#osm-map-details").html(regionInfo.cachedDetails);
}
else {
// Select platforms that have IDs for the region and request details.
const contentReady = [];
const idxs = [];
const ids = regionInfo.idsByPlatform;
for (let i = 0; i < ids.length; i++) {
if (ids[i] && ids[i].hasOwnProperty('length') && ids[i].length > 0) {
contentReady.push(loadPlain(platforms[i].url + "/html/" + ids[i].join(',')));
//contentReady.push(loadPlain(platforms[i].url + "/details_html?ids=" + ids[i].join(',')));
idxs.push(i);
}
}
// We should have bailed out early in this case!
if (contentReady.length == 0) {
console.error("[Internal] IDs by platform cannot be empty at this point", regionInfo);
return;
}
// Once all platforms responded, display their content.
Promise.all(contentReady).then(
function(htmls) {
// TODO: Select the relevant DIVs (for static hosting, we always get all details)
//const snippets = [];
//for (let i = 0; i < idxs.length; i++) {
// const platformIdx = idxs[i];
// for (const id of regionInfo.idsByPlatform[platformIdx]) {
// const elem = $('#' + id, '<div>' + htmls[i] + '</div>');
// snippets.push(elem.html());
// }
//}
regionInfo.cachedDetails = htmls;
$("#osm-map-details").html(htmls);
},
err => closeDetails()
);
}
}
function closeDetails() {
$("#osm-map-pane").hide();
$("#osm-map").css("width", "100%");
map.invalidateSize();
//$("#platforms-pane").show();
}
function selectId(id) {
if (allPinsById.hasOwnProperty(id)) {
allPinsById[id].popup.openPopup();
viewDetails(id);
map.flyTo(allPinsById[id].latlng, 12, {
animate: true,
duration: 3
});
return;
}
// TODO: Should we have a ID-lookup for regions?
for (var ags in allRegionsByAgs) {
if (allRegionsByAgs[ags].ids &&
allRegionsByAgs[ags].ids.indexOf(preselectId) != -1) {
// TODO: User might need to scroll down.
viewDetailsR(ags);
return;
}
}
}
var pinsReady = [];
for (let i = 0; i < platforms.length; i++) {
pinsReady.push(loadJson(platforms[i].url + '/pins').then(pinsById => {
for (var id in pinsById) {
if (isValidPinInfo(pinsById[id])) {
const pin = {
latlng: L.latLng(pinsById[id].latlng),
type: pinsById[id].type,
title: pinsById[id].title,
platformIdx: i,
marker: null,
popup: null,
elem: null
};
const clickHandler = "viewDetails('" + id + "');";
const content = renderPreviewHtml(pin.title, clickHandler);
const icon = defaultPinIcons[pin.type]; // TODO: customizable icons
pin.marker = L.marker(pin.latlng, { "icon": icon });
pin.popup = pin.marker.bindPopup(content);
pin.elem = $(pin.popup.addTo(map).getElement());
pin.marker.on('click', function() { eval(clickHandler); });
allPinsById[id] = pin;
}
}
}));
}
// loadJson('assets/zipcodes.de.json').then(zipCodes => {
// const reg = {};
// for (const entry of zipCodes) {
// if (!reg.hasOwnProperty(entry.zipcode))
// reg[entry.zipcode] = { "ags": entry.community_code, "latlngs": [] };
// reg[entry.zipcode].latlngs.push([parseFloat(entry.latitude), parseFloat(entry.longitude)]);
// }
// console.log(JSON.stringify(reg));
// });
function isRegionVisible(ags, zoom) {
return false;
if (zoom < 6) {
// Bundesländer
return ags.length == 2;
}
else { //if (zoom < 8) {
// Landkreise
return ags.length == 4 || ags.length == 5;
}
//else {
// // Landkreise und Berliner Bezirke
// return ags.length > 2 && ags != "11000";
//}
//return false;
}
var regionsByAgsReady = loadJson('assets/Karte_de_geodata.geo.json').then(geoJson => {
var geoJsonLayer = L.geoJSON(geoJson).addTo(map);
geoJsonLayer.eachLayer(function (layer) {
var lnglatRange = [[90, -90], [180, -180]];
forEachPoint(layer.feature.geometry.coordinates, lnglat => {
for (var i = 0; i < lnglat.length; i++) {
if (lnglat[i] < lnglatRange[i][0]) // min
lnglatRange[i][0] = lnglat[i];
if (lnglat[i] > lnglatRange[i][1]) // max
lnglatRange[i][1] = lnglat[i];
}
});
var centerLatLng = L.latLng((lnglatRange[1][1] + lnglatRange[1][0]) / 2,
(lnglatRange[0][1] + lnglatRange[0][0]) / 2);
if (allRegionsByAgs.hasOwnProperty(layer.feature.properties.ags))
console.warn("Duplicate ags?", layer.feature.properties.ags);
var domElement = $(layer.getElement());
allRegionsByAgs[layer.feature.properties.ags] = {
elem: domElement,
center: centerLatLng,
name: layer.feature.properties.name,
fill: domElement.css("fill"),
opacity: domElement.css("fill-opacity"),
zips: [],
idsByPlatform: []
};
layer.on('mouseover', function (e) {
var region = allRegionsByAgs[layer.feature.properties.ags];
region.elem.css("fill", "#00C0C0");
region.elem.css("fill-opacity", "0.2");
console.log(layer.feature.properties.ags);
});
layer.on('mouseout', function (e) {
var region = allRegionsByAgs[layer.feature.properties.ags];
region.elem.css("fill", region.fill);
region.elem.css("fill-opacity", region.opacity);
});
layer.on('click', function (e) {
var region = allRegionsByAgs[layer.feature.properties.ags];
var clickHandler = "viewDetailsR('" + layer.feature.properties.ags + "');";
var popupContent = renderPreviewHtml(region.name, clickHandler);
L.popup().setLatLng(region.center).setContent(popupContent).openOn(map);
eval(clickHandler);
});
});
var updateVisibleRegions = function() {
var zoom = map.getZoom();
for (var ags in allRegionsByAgs) {
if (isRegionVisible(ags, zoom)) {
allRegionsByAgs[ags].elem.show();
}
else {
allRegionsByAgs[ags].elem.hide();
}
}
};
map.on('zoomend', updateVisibleRegions);
updateVisibleRegions();
});
var zip2argsReady = loadJson('assets/zip2ags.json');
let idsByZipCodeReady = [];
for (let i = 0; i < platforms.length; i++) {
idsByZipCodeReady.push(loadJson(platforms[i].url + "/regions"));
}
const maxIdsPerRegionSize = { "2": 1, "5": 1 };
function registerIdsInRegion(ags, ids, platformIdx) {
if (allRegionsByAgs.hasOwnProperty(ags)) {
const regionIds = allRegionsByAgs[ags].idsByPlatform;
// JS will extend array as necessary
const platformIds = regionIds[platformIdx] || [];
//const platformIds = existing ? regionIds[platformIdx] : [];
regionIds[platformIdx] = platformIds.concat(ids);
maxIdsPerRegionSize[ags.length] =
Math.max(maxIdsPerRegionSize[ags.length],
regionIds[platformIdx].length);
return true;
}
return false;
}
function registerZipInRegion(ags, zip) {
if (!allRegionsByAgs.hasOwnProperty(ags))
return false;
allRegionsByAgs[ags].zips.push(zip);
return true;
};
var regionsReady = Promise.all(idsByZipCodeReady.concat([zip2argsReady, regionsByAgsReady]))
.then(function (values) {
if (platforms.length != values.length - 2) {
console.error("[Internal] Invalid promise results order");
return;
}
// Result from zip2argsReady. The regionsByAgsReady promise has no return value.
// The first platforms.length elements are IDs by ZIP
const zip2ags = values[values.length - 2];
const idsByZipCode = values;
// TODO: Handle/detect overlap in IDs between different platforms!
for (let i = 0; i < platforms.length; i++) {
for (const zip in idsByZipCode[i]) {
const entry = idsByZipCode[i][zip];
// Determine AGS code for given ZIP and validate.
if (!zip2ags.hasOwnProperty(zip)) {
console.warn("Unknown ZIP code", zip, "Dropping IDs", entry);
continue;
}
const ags = zip2ags[zip].ags;
if (!allRegionsByAgs.hasOwnProperty(ags)) {
console.warn("Unknown AGS code", ags, "for zip", zip, "Dropping IDs", entry);
continue;
}
if (!registerIdsInRegion(ags.substr(0, 2), entry, i)) {
console.warn("Invalid AGS Bundesland prefix in", ags, "Dropping IDs", entry);
}
if (!registerIdsInRegion(ags.substr(0, 5), entry, i)) {
console.warn("Invalid AGS Landkreis prefix in", ags, "Dropping IDs", entry);
}
if (ags.length > 5) {
// TODO: We may have/use a more detailed encoding in the future.
console.log("AGS code provides further region encoding", ags);
}
}
}
for (const zip in zip2ags) {
var ags = zip2ags[zip].ags;
if (!allRegionsByAgs.hasOwnProperty(ags)) {
console.error("[Internal] Unknown AGS code", ags, "for zip", zip);
continue;
}
if (!registerZipInRegion(ags.substr(0, 2), zip)) {
console.error("[Internal] Invalid AGS Bundesland prefix in", ags);
}
if (!registerZipInRegion(ags.substr(0, 5), zip)) {
console.error("[Internal] Invalid AGS Landkreis prefix in", ags);
}
}
const numIds = function(region) {
let sum = 0;
for (const ids of region.idsByPlatform)
sum += ids ? ids.length : 0;
return sum;
}
for (var ags in allRegionsByAgs) {
const region = allRegionsByAgs[ags];
const strength = numIds(region) / maxIdsPerRegionSize[ags.length];
region.opacity = strength * 0.5;
region.elem.css("fill-opacity", region.opacity);
}
});
Promise.all(pinsReady.concat([regionsReady])).then(function() {
let preselectId = parseAnchor(window.location.href);
console.log(preselectId);
if (preselectId) {
selectId(preselectId);
}
});
$(window).on('hashchange', function(e) {
let preselectId = parseAnchor(window.location.href);
console.log(preselectId);
if (preselectId) {
selectId(preselectId);
}
});
function togglePlatform(checkbox, platformIdx) {
const show = $(checkbox).is(':checked');
console.log("Anzeigen von ", platforms[platformIdx].name, (show ? "ein" : "aus") + "blenden");
const toggle = (show ? function(elem) { elem.show(); }
: function(elem) { elem.hide(); });
for (const id in allPinsById) {
if (allPinsById[id].platformIdx == platformIdx) {
toggle(allPinsById[id].elem);
}
}
}
</script>
</body>
</html>