Skip to content

Commit

Permalink
disableClickPropagation does not prevent events on elements' features…
Browse files Browse the repository at this point in the history
… (and child elements)
  • Loading branch information
johndoe committed Nov 7, 2021
1 parent 3ce36bc commit 7172b7c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
47 changes: 46 additions & 1 deletion spec/suites/dom/DomEventSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe('DomEvent', function () {
});

describe('#disableClickPropagation', function () {
it('stops click events from propagation to parent elements', function () { // except 'click'
it('does not stop click events from propagation to parent HTML elements', function () {
var child = document.createElement('div');
el.appendChild(child);
L.DomEvent.disableClickPropagation(child);
Expand All @@ -312,6 +312,51 @@ describe('DomEvent', function () {
happen.once(child, {type: 'mousedown'});
happen.once(child, {type: 'touchstart', touches: []});

expect(listener.callCount).to.be(L.Browser.touchNative ? 4 : 3);
});

it('does not stop click events from firing on feature itself', function () {
var map = L.map(el, {zoom:0, center:[0, 0]});
var layer = new L.Layer();
layer.onAdd = layer.onRemove = L.Util.falseFn;
layer.addTo(map);

var child = document.createElement('div');
el.appendChild(child);
layer.addInteractiveTarget(child);

L.DomEvent.disableClickPropagation(child);
layer.on('click dblclick contextmenu mousedown', listener);

happen.once(child, {type: 'click'});
happen.once(child, {type: 'dblclick'});
happen.once(child, {type: 'contextmenu'});
happen.once(child, {type: 'mousedown'});

map.remove(); // cleanup
expect(listener.callCount).to.be(4);
});

it('stops click events from propagation to parent features', function () {
var map = L.map(el, {zoom:0, center:[0, 0]});
var layer = new L.Layer();
layer.onAdd = layer.onRemove = L.Util.falseFn;
layer.addTo(map);

var child = document.createElement('div');
el.appendChild(child);
layer.addInteractiveTarget(child);

L.DomEvent.disableClickPropagation(child);
map.on('preclick click dblclick contextmenu mousedown', listener);

happen.once(child, {type: 'preclick'});
happen.once(child, {type: 'click'});
happen.once(child, {type: 'dblclick'});
happen.once(child, {type: 'contextmenu'});
happen.once(child, {type: 'mousedown'});

map.remove(); // cleanup
expect(listener.notCalled).to.be.ok();
});

Expand Down
6 changes: 4 additions & 2 deletions src/dom/DomEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ export function disableScrollPropagation(el) {
// Adds `stopPropagation` to the element's `'click'`, `'dblclick'`, `'contextmenu'`,
// `'mousedown'` and `'touchstart'` events (plus browser variants).
export function disableClickPropagation(el) {
on(el, 'mousedown touchstart dblclick contextmenu', stopPropagation);
el['_leaflet_disable_click'] = true;
if (!(el instanceof HTMLElement)) {
throw new Error('HTMLElement expected');
}
el['_leaflet_disable_click_propagation'] = true;
return this;
}

Expand Down
4 changes: 3 additions & 1 deletion src/map/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,8 @@ export var Map = Evented.extend({
}
},

_clickEvents: ['mousedown', 'touchstart', 'preclick', 'click', 'dblclick', 'contextmenu'],

_findEventTargets: function (e, type) {
var targets = [],
target,
Expand All @@ -1350,7 +1352,7 @@ export var Map = Evented.extend({
var disablePropagation = false;
while (src) {
if (disablePropagation) { return targets; }
if (src['_leaflet_disable_click'] && (type === 'click' || type === 'preclick')) {
if (src['_leaflet_disable_click_propagation'] && this._clickEvents.indexOf(type) !== -1) {
disablePropagation = true;
}
target = this._targets[Util.stamp(src)];
Expand Down

0 comments on commit 7172b7c

Please sign in to comment.