Skip to content

Commit b0407d9

Browse files
committed
Extract data to json files
1 parent 5290302 commit b0407d9

File tree

5 files changed

+144
-123
lines changed

5 files changed

+144
-123
lines changed

index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
<div id="map"></div>
1616

17-
<script type="text/javascript" src="municipalities.js" defer></script>
18-
<script type="text/javascript" src="periods.js" defer></script>
1917
<script type="text/javascript" src="map.js" defer></script>
2018
<!-- Create your own key and white list your website: https://console.cloud.google.com/apis/credentials/ -->
2119
<!-- View usage: https://console.cloud.google.com/apis/dashboard -->

map.js

Lines changed: 86 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*jslint browser: true, for: true, long: true, unordered: true, nomen: true */
2-
/*global window console google municipalities periods */
2+
/*global window console google */
33

44
/**
55
* Op zoek naar de website?
@@ -12,6 +12,10 @@ function initMap() {
1212
const appState = {
1313
// The map itself:
1414
"map": null,
15+
// Periods
16+
"periods": null,
17+
// Municipalities
18+
"municipalities": null,
1519
// The selected (or initial) municipality:
1620
"activeMunicipality": "Hoorn",
1721
// The zoom level when starting the app:
@@ -49,19 +53,19 @@ function initMap() {
4953
*/
5054
function getInitialMapSettings() {
5155
let zoomLevel = appState.initialZoomLevel;
52-
let center = Object.assign({}, municipalities[appState.activeMunicipality].center); // Create new copy
56+
let center = Object.assign({}, appState.municipalities[appState.activeMunicipality].center); // Create new copy
5357
let lat;
5458
let lng;
5559
if (window.URLSearchParams) {
5660
const urlSearchParams = new window.URLSearchParams(window.location.search);
5761
let zoomParam = urlSearchParams.get("zoom");
5862
let centerParam = urlSearchParams.get("center");
5963
const municipalityParam = urlSearchParams.get("in");
60-
if (municipalityParam && municipalities[municipalityParam] !== undefined) {
64+
if (municipalityParam && appState.municipalities[municipalityParam] !== undefined) {
6165
appState.activeMunicipality = municipalityParam;
6266
console.log("Adjusted municipality from URL: " + municipalityParam);
6367
}
64-
center = Object.assign({}, municipalities[appState.activeMunicipality].center);
68+
center = Object.assign({}, appState.municipalities[appState.activeMunicipality].center);
6569
if (zoomParam && centerParam) {
6670
zoomParam = parseFloat(zoomParam);
6771
if (zoomParam > 14 && zoomParam < 20) {
@@ -473,7 +477,7 @@ function initMap() {
473477

474478
const controlDiv = document.createElement("div"); // Create a DIV to attach the control UI to the Map.
475479
const combobox = document.createElement("select");
476-
const municipalityNames = Object.keys(municipalities);
480+
const municipalityNames = Object.keys(appState.municipalities);
477481
combobox.id = "idCbxMunicipality";
478482
combobox.title = "Gemeente selecteren";
479483
municipalityNames.forEach(function (municipalityName) {
@@ -507,7 +511,7 @@ function initMap() {
507511
const combobox = document.createElement("select");
508512
combobox.id = "idCbxPeriod";
509513
combobox.title = "Periode van de bekendmaking selecteren";
510-
periods.forEach(function (period) {
514+
appState.periods.forEach(function (period) {
511515
combobox.add(createOptionEx(period.key, period.val));
512516
});
513517
combobox.addEventListener("change", updatePeriodFilter);
@@ -933,7 +937,7 @@ function initMap() {
933937
} else if (publication.location === undefined) {
934938
console.error("Publication without position: " + JSON.stringify(publication, null, 4));
935939
// Take the center of the municipality:
936-
position = findUniquePosition(municipalities[appState.activeMunicipality].center);
940+
position = findUniquePosition(appState.municipalities[appState.activeMunicipality].center);
937941
prepareToAddMarker(publication, periodFilter.periodToShow, position, bounds);
938942
} else {
939943
console.error("Unsupported publication location: " + JSON.stringify(publication, null, 4));
@@ -952,9 +956,9 @@ function initMap() {
952956
* @return {void}
953957
*/
954958
function addMunicipalitiyMarkers() {
955-
const municipalityNames = Object.keys(municipalities);
959+
const municipalityNames = Object.keys(appState.municipalities);
956960
municipalityNames.forEach(function (municipalityName) {
957-
const municipalityObject = municipalities[municipalityName];
961+
const municipalityObject = appState.municipalities[municipalityName];
958962
let marker = new google.maps.Marker({
959963
"map": appState.map,
960964
"position": municipalityObject.center,
@@ -1060,10 +1064,10 @@ function initMap() {
10601064
* @return {void}
10611065
*/
10621066
function activateClosestMunicipality(position) {
1063-
const municipalityNames = Object.keys(municipalities);
1067+
const municipalityNames = Object.keys(appState.municipalities);
10641068
let distance = 1000000;
10651069
municipalityNames.forEach(function (municipalityName) {
1066-
const municipalityObject = municipalities[municipalityName];
1070+
const municipalityObject = appState.municipalities[municipalityName];
10671071
const distanceBetweenMunicipalityAndViewer = computeDistanceBetween(position, municipalityObject.center) / 1000;
10681072
if (distanceBetweenMunicipalityAndViewer < distance) {
10691073
console.log("Found closer municipality: " + municipalityName);
@@ -1081,7 +1085,7 @@ function initMap() {
10811085
if (window.URLSearchParams) {
10821086
const urlSearchParams = new window.URLSearchParams(window.location.search);
10831087
const municipalityParam = urlSearchParams.get("in");
1084-
if (municipalityParam && municipalities[municipalityParam] !== undefined) {
1088+
if (municipalityParam && appState.municipalities[municipalityParam] !== undefined) {
10851089
return true;
10861090
}
10871091
}
@@ -1101,7 +1105,7 @@ function initMap() {
11011105
).then(function (response) {
11021106
if (response.ok) {
11031107
response.json().then(function (responseJson) {
1104-
if (municipalities[responseJson.city] !== undefined) {
1108+
if (appState.municipalities[responseJson.city] !== undefined) {
11051109
// Name of the city is the same as the municipality.
11061110
console.log("Client location in municipality " + responseJson.city);
11071111
appState.activeMunicipality = responseJson.city;
@@ -1188,7 +1192,7 @@ function initMap() {
11881192
const previousMonth = new Date();
11891193
previousMonth.setDate(0); // Set to last day of previous month
11901194
const previousMonthString = previousMonth.getFullYear() + "-" + addLeadingZero(previousMonth.getMonth() + 1);
1191-
const periodId = periods.findIndex(function (period) {
1195+
const periodId = appState.periods.findIndex(function (period) {
11921196
return period.key === previousMonthString;
11931197
});
11941198
appState.requestPeriod.startDate = new Date();
@@ -1290,7 +1294,7 @@ function initMap() {
12901294
* @return {void}
12911295
*/
12921296
function navigateTo(municipality) {
1293-
const center = municipalities[municipality].center;
1297+
const center = appState.municipalities[municipality].center;
12941298
appState.map.setZoom(appState.initialZoomLevel);
12951299
appState.map.setCenter(new google.maps.LatLng(center.lat, center.lng));
12961300
}
@@ -1403,68 +1407,76 @@ function initMap() {
14031407
}
14041408
}
14051409

1410+
/**
1411+
* Download json from the Github Live Pages.
1412+
* @param {string} path Path and file name to retrieve.
1413+
* @param {function} callback Function when request is successful.
1414+
* @return {void}
1415+
*/
1416+
function getData(path, callback) {
1417+
const host = "https://basgroot.github.io";
1418+
const url = host + path;
1419+
console.log("Loading file " + url + "..");
1420+
fetch(url, {"method": "GET"}).then(function (response) {
1421+
if (response.ok) {
1422+
response.json().then(callback);
1423+
} else {
1424+
console.error(response);
1425+
}
1426+
}).catch(function (error) {
1427+
console.error(error);
1428+
});
1429+
}
1430+
14061431
/**
14071432
* Open an historical file, where data is stored per month.
14081433
* @param {string} period Month to display.
14091434
* @return {void}
14101435
*/
14111436
function loadHistory(period, isNewRequest) {
14121437
const lookupMunicipality = (
1413-
municipalities[appState.activeMunicipality].hasOwnProperty("lookupName")
1414-
? municipalities[appState.activeMunicipality].lookupName
1438+
appState.municipalities[appState.activeMunicipality].hasOwnProperty("lookupName")
1439+
? appState.municipalities[appState.activeMunicipality].lookupName
14151440
: appState.activeMunicipality
14161441
);
1417-
const fileName = "https://basgroot.github.io/bekendmakingen/history/" + encodeURIComponent(lookupMunicipality.toLowerCase().replace(/\s/g, "-")) + "-" + period + ".json";
1442+
const url = "/bekendmakingen/history/" + encodeURIComponent(lookupMunicipality.toLowerCase().replace(/\s/g, "-")) + "-" + period + ".json";
14181443
if (isNewRequest) {
14191444
setLoadingIndicatorVisibility("show");
14201445
clearMarkers(appState.activeMunicipality);
14211446
}
14221447
console.log("Loading historical data of municipality " + appState.activeMunicipality);
1423-
fetch(
1424-
fileName,
1425-
{
1426-
"method": "GET"
1427-
}
1428-
).then(function (response) {
1429-
if (response.ok) {
1430-
response.json().then(function (responseJson) {
1431-
let startRecord = 1;
1432-
// Preprocess data:
1433-
responseJson.publications.forEach(function (publication) {
1434-
publication.date = new Date(publication.date);
1435-
});
1436-
if (isNewRequest) {
1437-
// This is a request for an historic month:
1438-
if (!appState.isHistoryActive) {
1439-
if (appState.isFullyLoaded) {
1440-
// Make a backup, for when the time filter is reset
1441-
appState.publicationsArrayBackup = [].concat(appState.publicationsArray);
1442-
console.log("Backup created");
1443-
}
1444-
appState.isHistoryActive = true;
1445-
}
1446-
appState.publicationsArray = responseJson.publications;
1447-
} else {
1448-
// This is a request to add some history to the current view:
1449-
startRecord = appState.publicationsArray.length + 1;
1450-
// Delete the publications older than 6 weeks:
1451-
const publicationIndex = responseJson.publications.findIndex(function (publication) {
1452-
return publication.date < appState.requestPeriod.startDate;
1453-
});
1454-
if (publicationIndex >= 0) {
1455-
console.log("Deleting " + (responseJson.publications.length - publicationIndex) + " historical items from before " + appState.requestPeriod.startDate.toDateString());
1456-
responseJson.publications = responseJson.publications.slice(0, publicationIndex - 1);
1457-
}
1458-
appState.publicationsArray = appState.publicationsArray.concat(responseJson.publications);
1459-
appState.isFullyLoaded = true;
1448+
getData(url, function (responseJson) {
1449+
let startRecord = 1;
1450+
// Preprocess data:
1451+
responseJson.publications.forEach(function (publication) {
1452+
publication.date = new Date(publication.date);
1453+
});
1454+
if (isNewRequest) {
1455+
// This is a request for an historic month:
1456+
if (!appState.isHistoryActive) {
1457+
if (appState.isFullyLoaded) {
1458+
// Make a backup, for when the time filter is reset
1459+
appState.publicationsArrayBackup = [].concat(appState.publicationsArray);
1460+
console.log("Backup created");
14601461
}
1461-
addMarkers(startRecord, false);
1462-
});
1462+
appState.isHistoryActive = true;
1463+
}
1464+
appState.publicationsArray = responseJson.publications;
14631465
} else {
1464-
console.error(response);
1466+
// This is a request to add some history to the current view:
1467+
startRecord = appState.publicationsArray.length + 1;
1468+
// Delete the publications older than 6 weeks:
1469+
const publicationIndex = responseJson.publications.findIndex(function (publication) {
1470+
return publication.date < appState.requestPeriod.startDate;
1471+
});
1472+
if (publicationIndex >= 0) {
1473+
console.log("Deleting " + (responseJson.publications.length - publicationIndex) + " historical items from before " + appState.requestPeriod.startDate.toDateString());
1474+
responseJson.publications = responseJson.publications.slice(0, publicationIndex - 1);
1475+
}
1476+
appState.publicationsArray = appState.publicationsArray.concat(responseJson.publications);
1477+
appState.isFullyLoaded = true;
14651478
}
1466-
}).catch(function (error) {
1467-
console.error(error);
1479+
addMarkers(startRecord, false);
14681480
});
14691481
}
14701482

@@ -1476,8 +1488,8 @@ function initMap() {
14761488
*/
14771489
function loadDataForMunicipality(municipality, startRecord) {
14781490
const lookupMunicipality = (
1479-
municipalities[municipality].hasOwnProperty("lookupName")
1480-
? municipalities[municipality].lookupName
1491+
appState.municipalities[municipality].hasOwnProperty("lookupName")
1492+
? appState.municipalities[municipality].lookupName
14811493
: municipality
14821494
);
14831495
setLoadingIndicatorVisibility("show");
@@ -1569,5 +1581,16 @@ function initMap() {
15691581
loadDataForMunicipality(appState.activeMunicipality, 1);
15701582
}
15711583

1572-
getLocationAndLoadData();
1584+
function init() {
1585+
getData("/bekendmakingen/periods.json", function (periodsJson) {
1586+
appState.periods = periodsJson;
1587+
getData("/bekendmakingen/municipalities.json", function (municipalitiesJson) {
1588+
// Source: https://organisaties.overheid.nl/Gemeenten/
1589+
appState.municipalities = municipalitiesJson;
1590+
getLocationAndLoadData();
1591+
});
1592+
});
1593+
}
1594+
1595+
init();
15731596
}

municipalities.js renamed to municipalities.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// Source: https://organisaties.overheid.nl/Gemeenten/
2-
3-
const municipalities = {
1+
{
42
"Aa en Hunze": {
53
"center": {
64
"lat": 52.93517488,
@@ -2057,4 +2055,4 @@ const municipalities = {
20572055
"lng": 6.09229013
20582056
}
20592057
}
2060-
};
2058+
}

periods.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)