Prevent map click when layer has popup (#4603)

* prevent map click when layer has popup

* add test cases for click events on Paths

* update test for Path popups to check popup is open
This commit is contained in:
Jon Woyame 2016-05-29 16:01:16 -04:00 committed by Yohan Boniface
parent bd2093a3af
commit 878a022897
3 changed files with 63 additions and 0 deletions

View File

@ -229,6 +229,25 @@ describe('Popup', function () {
L.Icon.Default.prototype.options.popupAnchor = popupAnchorBefore;
});
it("prevents an underlying map click for Layer", function () {
var layer = new L.Polygon([[55.8, 37.6], [55.9, 37.7], [56.0, 37.8]]).addTo(map);
layer.bindPopup("layer popup");
var mapClicked = false;
map.on('click', function (e) {
mapClicked = true;
new L.Popup()
.setLatLng(e.latlng)
.setContent("map popup")
.openOn(map);
});
expect(map.hasLayer(layer._popup)).to.be(false);
happen.click(layer._path);
expect(mapClicked).to.be(false);
expect(map.hasLayer(layer._popup)).to.be(true);
});
});
describe("L.Map#openPopup", function () {

View File

@ -12,4 +12,45 @@ describe('Path', function () {
expect(path.bringToFront()).to.equal(path);
});
});
describe('#events', function () {
var c, map;
beforeEach(function () {
c = document.createElement('div');
c.style.width = '400px';
c.style.height = '400px';
map = new L.Map(c);
map.setView(new L.LatLng(0, 0), 0);
document.body.appendChild(c);
});
afterEach(function () {
document.body.removeChild(c);
});
it('fires click event', function () {
var spy = sinon.spy();
var layer = new L.Polygon([[1, 2], [3, 4], [5, 6]]).addTo(map);
layer.on('click', spy);
happen.click(layer._path);
expect(spy.called).to.be.ok();
});
it('propagates click event by default', function () {
var spy = sinon.spy();
var spy2 = sinon.spy();
var mapSpy = sinon.spy();
var layer = new L.Polygon([[1, 2], [3, 4], [5, 6]]).addTo(map);
layer.on('click', spy);
layer.on('click', spy2);
map.on('click', mapSpy);
happen.click(layer._path);
expect(spy.called).to.be.ok();
expect(spy2.called).to.be.ok();
expect(mapSpy.called).to.be.ok();
});
});
});

View File

@ -153,6 +153,9 @@ L.Layer.include({
return;
}
// prevent map click
L.DomEvent.stop(e);
// if this inherits from Path its a vector and we can just
// open the popup at the new location
if (layer instanceof L.Path) {