2019-01-17 13:56:25 +00:00
"use strict" ;
2019-01-17 14:07:31 +00:00
// Import leaflet, but some plugins require it to have the variable name 'L' :-/
import L from 'leaflet' ;
import 'leaflet-fullscreen' ;
2019-09-18 21:41:42 +00:00
import 'leaflet-easyprint' ;
2019-06-09 22:51:41 +00:00
// import '../../node_modules/leaflet-timedimension/dist/leaflet.timedimension.src.withlog.js';
2019-01-17 13:56:25 +00:00
import Config from './Config.mjs' ;
2019-01-17 16:13:27 +00:00
import LayerDeviceMarkers from './LayerDeviceMarkers.mjs' ;
2019-04-16 17:08:15 +00:00
import DeviceData from './DeviceData.mjs' ;
2021-01-30 21:10:53 +00:00
import ReadingsData from './ReadingsData.mjs' ;
2019-01-18 21:25:30 +00:00
import UI from './UI.mjs' ;
2019-01-17 13:56:25 +00:00
2019-01-19 22:04:51 +00:00
class MapManager {
2019-01-17 13:56:25 +00:00
constructor ( ) {
2019-01-26 22:00:37 +00:00
console . log ( Config ) ;
2021-01-30 21:10:53 +00:00
this . readings _data = new ReadingsData ( ) ;
2019-01-17 13:56:25 +00:00
}
2019-04-16 17:08:15 +00:00
async setup ( ) {
2019-01-17 16:13:27 +00:00
// Create the map
2019-01-17 14:07:31 +00:00
this . map = L . map ( "map" , {
2019-07-22 14:37:17 +00:00
fullscreenControl : true
2019-01-17 14:07:31 +00:00
} ) ;
2019-01-17 13:56:25 +00:00
this . map . setView ( Config . default _location , Config . default _zoom ) ;
2019-01-17 16:13:27 +00:00
// Add the OpenStreetMap tile layer
2019-01-20 23:41:45 +00:00
this . layer _openstreet = L . tileLayer ( "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" , {
2019-01-17 13:56:25 +00:00
id : "openstreetmap" ,
maxZoom : 19 ,
2019-01-17 13:58:56 +00:00
attribution : "© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap contributors</a>"
2019-01-17 13:56:25 +00:00
} ) . addTo ( this . map ) ;
2019-01-17 16:13:27 +00:00
2019-03-18 20:44:12 +00:00
// Add the attribution
2019-01-21 11:48:29 +00:00
this . map . attributionControl . addAttribution ( "Data: <a href='https://connectedhumber.org/'>Connected Humber</a>" ) ;
this . map . attributionControl . addAttribution ( "<a href='https://github.com/ConnectedHumber/Air-Quality-Web/'>Air Quality Web</a> by <a href='https://starbeamrainbowlabs.com/'>Starbeamrainbowlabs</a>" ) ;
2020-04-27 19:13:01 +00:00
this . map . attributionControl . addAttribution ( "<strong><a href='https://github.com/ConnectedHumber/Air-Quality-Web/tree/dev#disclaimer'>Sensor Data Disclaimer</a></strong>" ) ;
2019-01-21 11:48:29 +00:00
2019-06-30 17:40:26 +00:00
// Setup the UI
this . ui = new UI ( Config , this ) ;
this . ui . setup ( ) . then ( ( ) => console . log ( "[map] UI setup complete." ) ) ;
2019-03-18 20:44:12 +00:00
2019-09-18 21:41:42 +00:00
// Set the export to image button
this . setup _print _export ( ) ;
2019-04-16 17:08:15 +00:00
// Load the device information
this . device _data = new DeviceData ( ) ;
await this . device _data . setup ( ) ;
console . log ( "[map] Device data loaded" ) ;
2019-01-17 16:13:27 +00:00
// Add the device markers
console . info ( "[map] Loading device markers...." ) ;
2019-06-30 17:40:26 +00:00
Promise . all ( [
this . setup _device _markers . bind ( this ) ( )
. then ( ( ) => console . info ( "[map] Device markers loaded successfully." ) ) ,
] ) . then ( ( ) => document . querySelector ( "main" ) . classList . remove ( "working-visual" ) ) ;
2019-01-17 16:38:15 +00:00
2019-06-09 22:51:41 +00:00
}
2021-01-30 21:16:02 +00:00
// NOTE: We tried leaflet-time-dimension for changing the time displayed, but it didn't work out
2019-04-13 13:54:44 +00:00
2019-01-17 17:31:59 +00:00
async setup _device _markers ( ) {
2021-01-30 21:10:53 +00:00
this . device _markers = new LayerDeviceMarkers ( this , this . device _data ) ;
2019-01-17 17:31:59 +00:00
await this . device _markers . setup ( ) ;
}
2019-09-18 21:41:42 +00:00
setup _print _export ( ) {
L . easyPrint ( {
title : "Export as image" ,
position : "topleft" ,
exportOnly : true ,
sizeModes : [
"A4Portrait" ,
"A4Landscape" ,
"Current" ,
{
width : 3308 ,
height : 2339 ,
name : "HiRes Landscape" ,
className : 'HiResLandscape' ,
tooltip : 'HiRes Landscape'
}
] ,
defaultSizeTitles : {
Current : 'Current Size' ,
A4Landscape : 'A4 Landscape' ,
A4Portrait : 'A4 Portrait' ,
HiResLandscape : 'HiRes Landscape'
}
} ) . addTo ( this . map ) ;
}
2019-01-17 16:38:15 +00:00
setup _layer _control ( ) {
this . layer _control = L . control . layers ( {
// Base layer(s)
"OpenStreetMap" : this . layer _openstreet
} , { // Overlay(s)
2019-01-17 17:31:59 +00:00
"Devices" : this . device _markers . layer ,
2021-01-30 21:10:53 +00:00
// "Heatmap": this.overlay.layer
2019-01-17 16:38:15 +00:00
} , { // Options
2019-01-17 16:39:47 +00:00
2019-01-17 16:38:15 +00:00
} ) ;
this . layer _control . addTo ( this . map ) ;
2019-01-17 13:56:25 +00:00
}
}
2019-01-20 00:17:54 +00:00
export default MapManager ;