1
1
/*jslint browser: true, for: true, long: true, unordered: true, nomen: true */
2
- /*global window console google municipalities periods */
2
+ /*global window console google */
3
3
4
4
/**
5
5
* Op zoek naar de website?
@@ -12,6 +12,10 @@ function initMap() {
12
12
const appState = {
13
13
// The map itself:
14
14
"map" : null ,
15
+ // Periods
16
+ "periods" : null ,
17
+ // Municipalities
18
+ "municipalities" : null ,
15
19
// The selected (or initial) municipality:
16
20
"activeMunicipality" : "Hoorn" ,
17
21
// The zoom level when starting the app:
@@ -49,19 +53,19 @@ function initMap() {
49
53
*/
50
54
function getInitialMapSettings ( ) {
51
55
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
53
57
let lat ;
54
58
let lng ;
55
59
if ( window . URLSearchParams ) {
56
60
const urlSearchParams = new window . URLSearchParams ( window . location . search ) ;
57
61
let zoomParam = urlSearchParams . get ( "zoom" ) ;
58
62
let centerParam = urlSearchParams . get ( "center" ) ;
59
63
const municipalityParam = urlSearchParams . get ( "in" ) ;
60
- if ( municipalityParam && municipalities [ municipalityParam ] !== undefined ) {
64
+ if ( municipalityParam && appState . municipalities [ municipalityParam ] !== undefined ) {
61
65
appState . activeMunicipality = municipalityParam ;
62
66
console . log ( "Adjusted municipality from URL: " + municipalityParam ) ;
63
67
}
64
- center = Object . assign ( { } , municipalities [ appState . activeMunicipality ] . center ) ;
68
+ center = Object . assign ( { } , appState . municipalities [ appState . activeMunicipality ] . center ) ;
65
69
if ( zoomParam && centerParam ) {
66
70
zoomParam = parseFloat ( zoomParam ) ;
67
71
if ( zoomParam > 14 && zoomParam < 20 ) {
@@ -473,7 +477,7 @@ function initMap() {
473
477
474
478
const controlDiv = document . createElement ( "div" ) ; // Create a DIV to attach the control UI to the Map.
475
479
const combobox = document . createElement ( "select" ) ;
476
- const municipalityNames = Object . keys ( municipalities ) ;
480
+ const municipalityNames = Object . keys ( appState . municipalities ) ;
477
481
combobox . id = "idCbxMunicipality" ;
478
482
combobox . title = "Gemeente selecteren" ;
479
483
municipalityNames . forEach ( function ( municipalityName ) {
@@ -507,7 +511,7 @@ function initMap() {
507
511
const combobox = document . createElement ( "select" ) ;
508
512
combobox . id = "idCbxPeriod" ;
509
513
combobox . title = "Periode van de bekendmaking selecteren" ;
510
- periods . forEach ( function ( period ) {
514
+ appState . periods . forEach ( function ( period ) {
511
515
combobox . add ( createOptionEx ( period . key , period . val ) ) ;
512
516
} ) ;
513
517
combobox . addEventListener ( "change" , updatePeriodFilter ) ;
@@ -933,7 +937,7 @@ function initMap() {
933
937
} else if ( publication . location === undefined ) {
934
938
console . error ( "Publication without position: " + JSON . stringify ( publication , null , 4 ) ) ;
935
939
// Take the center of the municipality:
936
- position = findUniquePosition ( municipalities [ appState . activeMunicipality ] . center ) ;
940
+ position = findUniquePosition ( appState . municipalities [ appState . activeMunicipality ] . center ) ;
937
941
prepareToAddMarker ( publication , periodFilter . periodToShow , position , bounds ) ;
938
942
} else {
939
943
console . error ( "Unsupported publication location: " + JSON . stringify ( publication , null , 4 ) ) ;
@@ -952,9 +956,9 @@ function initMap() {
952
956
* @return {void }
953
957
*/
954
958
function addMunicipalitiyMarkers ( ) {
955
- const municipalityNames = Object . keys ( municipalities ) ;
959
+ const municipalityNames = Object . keys ( appState . municipalities ) ;
956
960
municipalityNames . forEach ( function ( municipalityName ) {
957
- const municipalityObject = municipalities [ municipalityName ] ;
961
+ const municipalityObject = appState . municipalities [ municipalityName ] ;
958
962
let marker = new google . maps . Marker ( {
959
963
"map" : appState . map ,
960
964
"position" : municipalityObject . center ,
@@ -1060,10 +1064,10 @@ function initMap() {
1060
1064
* @return {void }
1061
1065
*/
1062
1066
function activateClosestMunicipality ( position ) {
1063
- const municipalityNames = Object . keys ( municipalities ) ;
1067
+ const municipalityNames = Object . keys ( appState . municipalities ) ;
1064
1068
let distance = 1000000 ;
1065
1069
municipalityNames . forEach ( function ( municipalityName ) {
1066
- const municipalityObject = municipalities [ municipalityName ] ;
1070
+ const municipalityObject = appState . municipalities [ municipalityName ] ;
1067
1071
const distanceBetweenMunicipalityAndViewer = computeDistanceBetween ( position , municipalityObject . center ) / 1000 ;
1068
1072
if ( distanceBetweenMunicipalityAndViewer < distance ) {
1069
1073
console . log ( "Found closer municipality: " + municipalityName ) ;
@@ -1081,7 +1085,7 @@ function initMap() {
1081
1085
if ( window . URLSearchParams ) {
1082
1086
const urlSearchParams = new window . URLSearchParams ( window . location . search ) ;
1083
1087
const municipalityParam = urlSearchParams . get ( "in" ) ;
1084
- if ( municipalityParam && municipalities [ municipalityParam ] !== undefined ) {
1088
+ if ( municipalityParam && appState . municipalities [ municipalityParam ] !== undefined ) {
1085
1089
return true ;
1086
1090
}
1087
1091
}
@@ -1101,7 +1105,7 @@ function initMap() {
1101
1105
) . then ( function ( response ) {
1102
1106
if ( response . ok ) {
1103
1107
response . json ( ) . then ( function ( responseJson ) {
1104
- if ( municipalities [ responseJson . city ] !== undefined ) {
1108
+ if ( appState . municipalities [ responseJson . city ] !== undefined ) {
1105
1109
// Name of the city is the same as the municipality.
1106
1110
console . log ( "Client location in municipality " + responseJson . city ) ;
1107
1111
appState . activeMunicipality = responseJson . city ;
@@ -1188,7 +1192,7 @@ function initMap() {
1188
1192
const previousMonth = new Date ( ) ;
1189
1193
previousMonth . setDate ( 0 ) ; // Set to last day of previous month
1190
1194
const previousMonthString = previousMonth . getFullYear ( ) + "-" + addLeadingZero ( previousMonth . getMonth ( ) + 1 ) ;
1191
- const periodId = periods . findIndex ( function ( period ) {
1195
+ const periodId = appState . periods . findIndex ( function ( period ) {
1192
1196
return period . key === previousMonthString ;
1193
1197
} ) ;
1194
1198
appState . requestPeriod . startDate = new Date ( ) ;
@@ -1290,7 +1294,7 @@ function initMap() {
1290
1294
* @return {void }
1291
1295
*/
1292
1296
function navigateTo ( municipality ) {
1293
- const center = municipalities [ municipality ] . center ;
1297
+ const center = appState . municipalities [ municipality ] . center ;
1294
1298
appState . map . setZoom ( appState . initialZoomLevel ) ;
1295
1299
appState . map . setCenter ( new google . maps . LatLng ( center . lat , center . lng ) ) ;
1296
1300
}
@@ -1403,68 +1407,76 @@ function initMap() {
1403
1407
}
1404
1408
}
1405
1409
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
+
1406
1431
/**
1407
1432
* Open an historical file, where data is stored per month.
1408
1433
* @param {string } period Month to display.
1409
1434
* @return {void }
1410
1435
*/
1411
1436
function loadHistory ( period , isNewRequest ) {
1412
1437
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
1415
1440
: appState . activeMunicipality
1416
1441
) ;
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" ;
1418
1443
if ( isNewRequest ) {
1419
1444
setLoadingIndicatorVisibility ( "show" ) ;
1420
1445
clearMarkers ( appState . activeMunicipality ) ;
1421
1446
}
1422
1447
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" ) ;
1460
1461
}
1461
- addMarkers ( startRecord , false ) ;
1462
- } ) ;
1462
+ appState . isHistoryActive = true ;
1463
+ }
1464
+ appState . publicationsArray = responseJson . publications ;
1463
1465
} 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 ;
1465
1478
}
1466
- } ) . catch ( function ( error ) {
1467
- console . error ( error ) ;
1479
+ addMarkers ( startRecord , false ) ;
1468
1480
} ) ;
1469
1481
}
1470
1482
@@ -1476,8 +1488,8 @@ function initMap() {
1476
1488
*/
1477
1489
function loadDataForMunicipality ( municipality , startRecord ) {
1478
1490
const lookupMunicipality = (
1479
- municipalities [ municipality ] . hasOwnProperty ( "lookupName" )
1480
- ? municipalities [ municipality ] . lookupName
1491
+ appState . municipalities [ municipality ] . hasOwnProperty ( "lookupName" )
1492
+ ? appState . municipalities [ municipality ] . lookupName
1481
1493
: municipality
1482
1494
) ;
1483
1495
setLoadingIndicatorVisibility ( "show" ) ;
@@ -1569,5 +1581,16 @@ function initMap() {
1569
1581
loadDataForMunicipality ( appState . activeMunicipality , 1 ) ;
1570
1582
}
1571
1583
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 ( ) ;
1573
1596
}
0 commit comments