Skip to content

Commit

Permalink
Add nonBubblingEvents option (fix #3604)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Jul 9, 2015
1 parent d2513b2 commit 74018f2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
30 changes: 30 additions & 0 deletions spec/suites/layer/marker/MarkerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,35 @@ describe("Marker", function () {
expect(spy.calledTwice).to.be.ok();
});

it("do not propagate click event", function () {
var spy = sinon.spy();
var spy2 = sinon.spy();
var mapSpy = sinon.spy();
var marker = new L.Marker(new L.LatLng(55.8, 37.6));
map.addLayer(marker);
marker.on('click', spy);
marker.on('click', spy2);
map.on('click', mapSpy);
happen.click(marker._icon);
expect(spy.called).to.be.ok();
expect(spy2.called).to.be.ok();
expect(mapSpy.called).not.to.be.ok();
});

it("do not propagate dblclick event", function () {
var spy = sinon.spy();
var spy2 = sinon.spy();
var mapSpy = sinon.spy();
var marker = new L.Marker(new L.LatLng(55.8, 37.6));
map.addLayer(marker);
marker.on('dblclick', spy);
marker.on('dblclick', spy2);
map.on('dblclick', mapSpy);
happen.dblclick(marker._icon);
expect(spy.called).to.be.ok();
expect(spy2.called).to.be.ok();
expect(mapSpy.called).not.to.be.ok();
});

});
});
7 changes: 7 additions & 0 deletions src/core/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ L.Util = {
return (Object.prototype.toString.call(obj) === '[object Array]');
},

indexOf: function (array, el) {
for (var i = 0; i < array.length; i++) {
if (array[i] === el) { return i; }
}
return -1;
},

// minimal image URI, set to an image when disposing to flush memory
emptyImageUrl: 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
};
Expand Down
3 changes: 2 additions & 1 deletion src/layer/Layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
L.Layer = L.Evented.extend({

options: {
pane: 'overlayPane'
pane: 'overlayPane',
nonBubblingEvents: [] // Array of events that should not be bubbled to DOM parents (like the map)
},

addTo: function (map) {
Expand Down
1 change: 1 addition & 0 deletions src/layer/marker/Marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ L.Marker = L.Layer.extend({

options: {
pane: 'markerPane',
nonBubblingEvents: ['click', 'dblclick', 'mouseover', 'mouseout', 'contextmenu'],

icon: new L.Icon.Default(),
// title: '',
Expand Down
3 changes: 2 additions & 1 deletion src/map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,8 @@ L.Map = L.Evented.extend({
for (var i = 0; i < targets.length; i++) {
if (targets[i].listens(type, true)) {
targets[i].fire(type, data, true);
if (data.originalEvent._stopped) { return; }
if (data.originalEvent._stopped
|| (targets[i].options.nonBubblingEvents && L.Util.indexOf(targets[i].options.nonBubblingEvents, type) !== -1)) { return; }
}
}
},
Expand Down

3 comments on commit 74018f2

@iH8
Copy link
Contributor

@iH8 iH8 commented on 74018f2 Feb 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently still undocumented in the current Leaflet 1.0.0 docs. Should i drop an issue for that or is it already in the works or is this mention enough?

@yohanboniface
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd tempted to say that it's internal stuff for now, and should not be documented yet. Do you have a use case that could motivate to document it?

@iH8
Copy link
Contributor

@iH8 iH8 commented on 74018f2 Feb 17, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mentioned in the CHANGELOG.md which get read by endusers and i just ran into this question on Stackoverflow: http://stackoverflow.com/questions/35466139/map-on-click-fires-when-geojson-is-clicked-on-leaflet-1-0/35467201#35467201 Personally i think it should be there, it's a very elegant solution, way better than having to use stopPropagation on event objects. Especially when you have multiple events on a layer.

Please sign in to comment.