Skip to content

Commit

Permalink
Clos popups on double click and cluster click
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Feb 13, 2024
1 parent 9d67405 commit 6a49644
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions freesound/static/bw-frontend/src/components/mapsMapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ function clipLatLonRanges(lat, lon){
return [lat, lon];
}

function closeAllMapPopups(map){
stopAllPlayers();
Object.keys(map.popups).forEach(function(key) {
if (map.popups[key] !== undefined){
map.popups[key].remove();
}
delete map.popups[key];
})
}

function ajaxLoad(url, callback, postData, plain) {
var http_request = false;

Expand Down Expand Up @@ -235,17 +245,9 @@ function makeSoundsMap(geotags_url, map_element_id, on_built_callback, on_bounds
map.on('click', 'sounds-unclustered', function (e) {
var sound_id = e.features[0].properties.id;
if (map.popups.hasOwnProperty(sound_id) === false){
// Close all other popups
Object.keys(map.popups).forEach(function(key) {
if (map.popups[key] !== undefined){
map.popups[key].remove();
}
delete map.popups[key];
})
// Set new popup as loading
// Close all other popups and set new popup as loading
closeAllMapPopups(map);
map.popups[sound_id] = undefined;

stopAllPlayers();

var coordinates = e.features[0].geometry.coordinates.slice();
let url = '/geotags/infowindow/' + sound_id;
Expand Down Expand Up @@ -291,6 +293,11 @@ function makeSoundsMap(geotags_url, map_element_id, on_built_callback, on_bounds
}
});

map.on("dblclick", function(e) {
// On double click, close popups and zoom (zoom will be done by mapbox by default)
closeAllMapPopups(map);
});

map.on("dblclick", "sounds-unclustered", function(e) {
e.preventDefault(); // Don't zoom in when double clicking on a sound
});
Expand All @@ -307,9 +314,20 @@ function makeSoundsMap(geotags_url, map_element_id, on_built_callback, on_bounds

// Zoom-in when clicking on clusters
map.on('click', 'sounds-clusters', function (e) {
closeAllMapPopups(map);
map.flyTo({'center': e.lngLat, 'zoom': map.getZoom() + 4});
});

// Change the cursor to a pointer when the mouse is over clusters.
map.on('mouseenter', 'sounds-clusters', function () {
map.getCanvas().style.cursor = 'pointer';
});

// Change it back to a pointer when it leaves.
map.on('mouseleave', 'sounds-clusters', function () {
map.getCanvas().style.cursor = '';
});

// Adjust map boundaries
if (center_lat === undefined){
// If initital center and zoom were not given, adjust map boundaries now based on the sounds
Expand Down

0 comments on commit 6a49644

Please sign in to comment.