krz/world-incarceration

An interactive map of world incarceration statistics.

clone: git clone https://gitbay.org/krz/world-incarceration.git

main: assets/js/ui/modal.js · raw

 1export function showCountryModal(countryData) {
 2	document.getElementById("country-modal-title").textContent = countryData.country;
 3	document.getElementById("country-modal-body").innerHTML = `
 4		<p><b>Prison Population:</b> ${_fmt(countryData.pop, "pop")}</p>
 5		<p><b>Rate per 100,000 Citizens:</b> ${_fmt(countryData.rate, "rate")}</p>
 6		<p><b>Females:</b> ${_fmt(countryData.females, "pct")}</p>
 7		<p><b>Juveniles:</b> ${_fmt(countryData.juveniles, "pct")}</p>
 8		<p><b>Occupancy:</b> ${_fmt(countryData.occupancy, "pct")}</p>
 9		<p><b>Government:</b> ${_fmtGov(countryData.government)}</p>
10	`;
11	const el = document.getElementById("countryModal");
12	(bootstrap.Modal.getInstance(el) || new bootstrap.Modal(el)).show();
13}
14
15function _fmt(val, type) {
16	if (val === "--" || val === null || val === undefined || (typeof val === "number" && isNaN(val))) {
17		return "<span class='text-danger'>Information not available.</span>";
18	}
19	if (type === "pop") return Number(val).toLocaleString("en");
20	if (type === "rate") return String(val);
21	if (type === "pct") return (val * 100).toFixed(2) + " %";
22	return String(val);
23}
24
25function _fmtGov(val) {
26	return (!val || val === "--")
27		? "<span class='text-danger'>Information not available.</span>"
28		: val;
29}