Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop propagation of 'click' event in Leaflet

In one of our projects we're using Leaflet along with Leaflet.markercluster plugin. Looking through the Leaflet's sources I found that it appends _collapse() function to the map's click event, so whenever I click on the map it contracts previously expanded cluster.
Now, I want to disable this behavior. If the cluster is expanded, then I just want to deselect all of its markers on click event (and don't contract the cluster itself). Here is the piece of my code:

map.on('click', function(e) {
    scope.deselectAllMarkers();
});

I tried to add the following lines in the end of this one-line callback in order to stop propagation of click event:

scope.L.DomEvent.stopPropagation(e);
scope.L.DomEvent.preventDefault(e);
scope.L.DomEvent.stop(e);
scope.L.DomEvent.stopPropagation(e.originalEvent);
scope.L.DomEvent.preventDefault(e.originalEvent);
scope.L.DomEvent.stop(e.originalEvent);

And none of them works. Default listener which is hidden inside of the Leaflet sources keeps its invocation whenever I click on the map. Am I missing something?

like image 685
aga Avatar asked Sep 04 '13 08:09

aga


People also ask

How do I stop click event propagation?

To stop the click event from propagating to the <div> element, you have to call the stopPropagation() method in the event handler of the button: btn. addEventListener('click', (e) => { e. stopPropagation(); alert('The button was clicked!

Which method can be used to stop propagation of an event?

stopPropagation() The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases.

What is event preventDefault () and event stopPropagation ()?

The event. preventDefault() prevents the browsers default behaviour, but does not stop the event from bubbling up the DOM. The event. stopPropagation() prevents the event from bubbling up the DOM, but does not stop the browsers default behaviour.


Video Answer


3 Answers

I know that this answer is quite late, but if someone is interested in a solution, here is how i have solved it.

This snippet here below is an example of binding a function to the click event.

map.on('click', doSomething);

Actually, after checking leaflet's API and some geekish debugging, it seems that the event returns an object, not the event itself. The event itself is wrapped into a field within the returned object.

var doSomething = function(map) {
    // stop propagation
    map.originalEvent.preventDefault();
};

When using the above snippet, the event bubbling has stopped, something which i wanted, and probably what you wanted.

like image 162
KarelG Avatar answered Oct 13 '22 16:10

KarelG


This one worked for me...

var div = L.DomUtil.get('div_id');
if (!L.Browser.touch) {
    L.DomEvent.disableClickPropagation(div);
    L.DomEvent.on(div, 'mousewheel', L.DomEvent.stopPropagation);
} else {
    L.DomEvent.on(div, 'click', L.DomEvent.stopPropagation);
}

Thanks to https://gis.stackexchange.com/questions/104507/disable-panning-dragging-on-leaflet-map-for-div-within-map

like image 23
Dhrumil Bhankhar Avatar answered Oct 13 '22 16:10

Dhrumil Bhankhar


I know that this answer is event more quite late, but as in jquery you can use .off

map.on('click', doSomething);
map.off('click');

It works fine for any leaflet events.

I use it for 'zoomend' event to be triggered one time only.

map.on('moveend', function(e){
    console.log("any code");
    map.off('moveend');
});
like image 1
Noma Avatar answered Oct 13 '22 14:10

Noma