Skip to content

Commit 1aa5408

Browse files
committed
Avoid double api call on load map
Signed-off-by: Cintia Sánchez García <[email protected]>
1 parent ed2d5db commit 1aa5408

File tree

4 files changed

+46
-41
lines changed

4 files changed

+46
-41
lines changed

ocg-server/static/js/community/explore/map.js

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import { fetchData } from "./explore.js";
22

33
export 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-
};

ocg-server/templates/community/base.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq"
2929
crossorigin="anonymous"></script>
3030

31+
{# djlint:off #}
3132
<script type="text/javascript">
3233
// Tailwind CSS configuration
33-
const palette = JSON.parse('{{ community.theme.palette|json|safe }}');
3434
tailwind.config = {
3535
theme: {
3636
extend: {
3737
colors: {
38-
primary: palette,
38+
primary: {{ community.theme.palette|json|safe }},
3939
},
4040
fontSize: {
4141
filterTitle: '0.8rem',
@@ -48,7 +48,8 @@
4848
},
4949
},
5050
};
51-
</script>
51+
</script>
52+
{# djlint:on #}
5253
</head>
5354
<body class="h-full {% block bg_color %} bg-white {% endblock bg_color %}">
5455
<div class="min-h-full flex flex-col">

ocg-server/templates/community/explore/events/results.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,21 @@
8383
{# Map view -#}
8484
<div class="mt-5 border-t border-gray-100 md:mt-7 p-5 md:p-7">
8585
<div class="relative w-full border h-[600px] md:h-[820px] overflow-hidden">
86-
<div id="map-box" class="w-full h-full"></div>
86+
<div id="map-box"
87+
class="relative w-full h-full leaflet-container leaflet-touch leaflet-retina leaflet-fade-anim leaflet-grab leaflet-touch-drag leaflet-touch-zoom">
88+
</div>
8789
</div>
8890
</div>
8991

92+
{# djlint:off #}
9093
<script type="module">
9194
import {
9295
Map
9396
} from '/static/js/community/explore/map.js';
9497

95-
const map = new Map('events');
98+
const map = new Map('events', { events: {{ events|json|safe }}, bbox: {{ bbox|json|safe }} });
9699
</script>
100+
{# djlint:on #}
97101
{# End map view -#}
98102

99103
{% endmatch %}

ocg-server/templates/community/explore/groups/results.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,21 @@
3535
{# Map view -#}
3636
<div class="mt-5 border-t border-gray-100 md:mt-7 p-5 md:p-7">
3737
<div class="relative w-full border h-[600px] md:h-[820px] overflow-hidden">
38-
<div id="map-box" class="relative w-full h-full"></div>
38+
<div id="map-box"
39+
class="relative w-full h-full leaflet-container leaflet-touch leaflet-retina leaflet-fade-anim leaflet-grab leaflet-touch-drag leaflet-touch-zoom">
40+
</div>
3941
</div>
4042
</div>
4143

44+
{# djlint:off #}
4245
<script type="module">
4346
import {
4447
Map
4548
} from '/static/js/community/explore/map.js';
4649

47-
const map = new Map('groups');
50+
const map = new Map('groups', { groups: {{ groups|json|safe }}, bbox: {{ bbox|json|safe }} });
4851
</script>
52+
{# djlint:on #}
4953
{# End map view -#}
5054

5155
{% when _ %}

0 commit comments

Comments
 (0)