@@ -2,14 +2,12 @@ import { fetchData } from "./explore.js";
22
33export  class  Map  { 
44  // Initialize map. 
5-   constructor ( entity )  { 
5+   constructor ( entity ,   data )  { 
66    // Check if map is already initialized 
77    if  ( Map . _instance )  { 
8-       // Invalidate map size to fix the map container 
9-       Map . _instance . map . invalidateSize ( ) ; 
10- 
118      Map . _instance . entity  =  entity ; 
12-       Map . _instance . setup ( ) ; 
9+       Map . _instance . enabledMoveEnd  =  false ; 
10+       Map . _instance . setup ( data ) ; 
1311      return  Map . _instance ; 
1412    } 
1513
@@ -20,18 +18,19 @@ export class Map {
2018    document . getElementsByTagName ( "head" ) [ 0 ] . appendChild ( script ) ; 
2119
2220    this . entity  =  entity ; 
21+     this . enabledMoveEnd  =  false ; 
2322
2423    // Setup map after script is loaded 
2524    script . onload  =  ( )  =>  { 
26-       this . setup ( ) ; 
25+       this . setup ( data ) ; 
2726    } ; 
2827
2928    // Save map instance 
3029    Map . _instance  =  this ; 
3130  } 
3231
3332  // Setup map instance. 
34-   setup ( )  { 
33+   setup ( data )  { 
3534    this . map  =  L . map ( "map-box" ,  { 
3635      maxZoom : 20 , 
3736      minZoom : 3 , 
@@ -50,11 +49,14 @@ export class Map {
5049
5150    // Load events after the map is loaded 
5251    this . map . on ( "load" ,  ( )  =>  { 
53-       this . refresh ( true ) ; 
52+       this . refresh ( true ,   data ) ; 
5453    } ) ; 
5554
55+     // Remove map on unload, invalidating the size and removing event listeners 
5656    this . map . on ( "unload" ,  ( )  =>  { 
57-       this . layerGroup . clearLayers ( ) ; 
57+       this . map . invalidateSize ( ) ; 
58+       this . map . off ( ) ; 
59+       this . map . remove ( ) ; 
5860    } ) ; 
5961
6062    // Setting the position of the map: lat/long and zoom level 
@@ -78,15 +80,30 @@ export class Map {
7880    // Adding a listener to the map after setting the position to get the bounds 
7981    // when the map is moved (zoom or pan) 
8082    this . map . on ( "moveend" ,  ( )  =>  { 
81-       this . refresh ( ) ; 
83+       if  ( this . enabledMoveEnd )  { 
84+         this . refresh ( ) ; 
85+       } 
86+       this . enabledMoveEnd  =  true ; 
87+     } ) ; 
88+ 
89+     this . map . on ( "viewreset" ,  ( )  =>  { 
90+       console . log ( "View reset" ) ; 
8291    } ) ; 
8392  } 
8493
8594  // Refresh map, updating the markers. 
86-   async  refresh ( overwriteBounds )  { 
87-     const  data  =  await  this . fetchData ( overwriteBounds ) ; 
95+   async  refresh ( overwriteBounds  =  false ,  currentData  =  null )  { 
96+     let  data ; 
97+     if  ( currentData )  { 
98+       data  =  currentData ; 
99+     }  else  { 
100+       data  =  await  this . fetchData ( overwriteBounds ) ; 
101+     } 
88102
89-     fixMapBoxClasses ( ) ; 
103+     // Clear previous markers for the layer group 
104+     if  ( this . map . hasLayer ( this . layerGroup ) )  { 
105+       this . layerGroup . clearLayers ( ) ; 
106+     } 
90107
91108    if  ( data )  { 
92109      // Get items from data 
@@ -117,10 +134,6 @@ export class Map {
117134    params . delete ( "view_mode" ) ; 
118135    params . delete ( "kind" ,  "virtual" ) ; 
119136
120-     // Add limit and offset 
121-     params . append ( "limit" ,  100 ) ; 
122-     params . append ( "offset" ,  0 ) ; 
123- 
124137    if  ( overwriteBounds )  { 
125138      // Get bbox to overwrite bounds on first load 
126139      params . append ( "include_bbox" ,  true ) ; 
@@ -158,11 +171,6 @@ export class Map {
158171      popupAnchor : [ 0 ,  - 25 ] , 
159172    } ; 
160173
161-     // Clear previous markers for the layer group 
162-     if  ( this . map . hasLayer ( this . layerGroup ) )  { 
163-       this . layerGroup . clearLayers ( ) ; 
164-     } 
165- 
166174    // Add markers 
167175    items . forEach ( ( item )  =>  { 
168176      // Skip items without coordinates 
@@ -200,15 +208,3 @@ export class Map {
200208    } ) ; 
201209  } 
202210} 
203- 
204- // Fix map box classes (issue to refresh map on filters changes). 
205- const  fixMapBoxClasses  =  ( )  =>  { 
206-   const  mapBox  =  document . getElementById ( "map-box" ) ; 
207-   if  ( mapBox  &&  ! mapBox . classList . contains ( "leaflet-container" ) )  { 
208-     const  classes  = 
209-       "leaflet-container leaflet-touch leaflet-retina leaflet-fade-anim leaflet-grab leaflet-touch-drag leaflet-touch-zoom" . split ( 
210-         " " , 
211-       ) ; 
212-     mapBox . classList . add ( ...classes ) ; 
213-   } 
214- } ; 
0 commit comments