-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
191 lines (164 loc) · 5.07 KB
/
app.js
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
import { mapColor, borderColor } from './config'
import L from 'leaflet'
import { africa } from './data/africajson'
import html2canvas from 'html2canvas'
import { Canvas2Image } from './lib/canvas2image'
import pym from 'pym.js'
import { addCountryMap } from './country-maps'
let formatNumber = new Intl.NumberFormat()
let tooltip = document.querySelector('.tooltip')
let countryStatus
async function getCountryData() {
await fetch('https://api.mediahack.co.za/vaccines/africa-country-status.php')
.then((data) => data.json())
.then((data) => {
// countryStatus = data.filter((d) => d.iso !== 'CPV')
countryStatus = data
})
}
let countryData
fetch('https://api.mediahack.co.za/adh/countries-json.php')
.then((data) => data.json())
.then((data) => {
countryData = data
})
let owid
fetch('https://api.mediahack.co.za/vaccines/owid.php')
.then((data) => data.json())
.then((data) => {
owid = data
})
function inIframe() {
try {
return window.self !== window.top
} catch (e) {
return true
}
}
function mapStyle(feature) {
let fillColor = '#fff'
let country = countryStatus.filter(
(d) => d.iso === feature.properties.ADM0_A3
)
if (country.length > 0) {
fillColor = mapColor
}
return {
color: borderColor,
fillColor: fillColor,
weight: 1,
opacity: 1,
fillOpacity: 0.8,
apiUrl: '',
}
}
let mapAfrica
function addAfricaMap() {
mapAfrica = L.map('map-africa', {
renderer: L.canvas(),
zoomControl: false,
scrollWheelZoom: false,
dragging: false,
zoomSnap: 0.1,
}).setView([51.505, -0.09], 13)
function onEachFeature(feature, layer) {
layer.on({
mouseover: hover,
mousemove: move,
mouseout: out,
})
}
function hover(feature) {
tooltip.style.left = feature.originalEvent.clientX + 15 + 'px'
tooltip.style.top = feature.originalEvent.clientY + 5 + 'px'
let tt = `<div class="tt-inner"><div class="tt-country">${feature.target.feature.properties.NAME_LONG}</div>`
let iso = feature.target.feature.properties.ADM0_A3
let cd = countryData.filter(
(d) => d.iso_code === iso && d.vaccination_started === 'Yes'
)
if (cd.length > 0) {
tt += `<div class="tt-vaccines-label">Vaccines in Use</div>`
cd.forEach((d) => {
tt += `<div class="tt-vaccines">${d.common_name}</div>`
})
}
let tv = owid.filter((d) => d.iso_code === iso)
let ttv
if (tv.length > 0) {
tt += `<div class="tt-total-label">Total Vaccinations</div>`
tt += `<div class="tt-value">${formatNumber.format(
tv[0].total_vaccinations
)}</div>`
} else {
tt += `<div class="tt-total-label">Total Vaccinations</div>`
tt += `<div class="tt-value">No Data</div>`
}
tt += `</div>`
tooltip.innerHTML = tt
let country = countryStatus.filter(
(d) => d.iso === feature.target.feature.properties.ADM0_A3
)
if (country.length > 0) {
tooltip.style.display = 'block'
}
}
function move(feature) {
tooltip.style.left = feature.originalEvent.clientX + 15 + 'px'
tooltip.style.top = feature.originalEvent.clientY + 5 + 'px'
}
function out(feature) {
tooltip.style.display = 'none'
}
let africaMap = L.geoJSON(africa, {
onEachFeature: onEachFeature,
style: mapStyle,
}).addTo(mapAfrica)
mapAfrica.fitBounds(africaMap.getBounds(), { padding: [10, 10] })
}
// adds the placeholders for the maps
async function addCountryMaps(iso) {
let node = document.createElement('div')
node.classList.add('box')
node.id = iso
document.getElementById('countries').appendChild(node)
let inner = `<div class="map-holder" id="map-${iso.toLowerCase()}"></div><div class="details"><div class="details-title"></div><div class="details-vaccines"></div><div class="total-vaccines">Total vaccinations:<br/><span class="total-vaccines-number"></div></div></div>`
document.getElementById(iso).innerHTML = inner
}
getCountryData()
.then(() => {
countryStatus.forEach((c) => {
addCountryMaps(c.iso)
})
})
.then(() => {
addAfricaMap()
})
.then(() => {
console.log(countryStatus)
countryStatus.forEach((c) => {
addCountryMap(c.iso, `map-${c.iso.toLowerCase()}`, countryStatus)
})
})
function downloadImage() {
let w = document.querySelector('.container').offsetWidth
let h = document.querySelector('.container').offsetHeight
html2canvas(document.querySelector('.container'), {
width: w + 10,
}).then((canvas) => {
return Canvas2Image.saveAsPNG(canvas)
})
}
function showEmbed() {
document.querySelector('.iframe-wrap').style.visibility = 'visible'
}
function hideEmbed() {
document.querySelector('.iframe-wrap').style.visibility = 'hidden'
}
let close = document.querySelector('.close')
close.addEventListener('click', hideEmbed)
let button = document.querySelector('.download-img')
button.addEventListener('click', downloadImage)
let embedButton = document.querySelector('.embed')
embedButton.addEventListener('click', showEmbed)
document.querySelector('.information').style.display = 'block'
var pymChild = new pym.Child({ polling: 500 })