Merge pull request #4016 from Leaflet/toggle-popup

Fix popup toggle on marker click (fix #3992)
This commit is contained in:
Vladimir Agafonkin 2015-11-15 10:44:39 +02:00
commit c55f89972b
3 changed files with 12 additions and 9 deletions

View File

@ -6,10 +6,15 @@ describe('Popup', function () {
c = document.createElement('div'); c = document.createElement('div');
c.style.width = '400px'; c.style.width = '400px';
c.style.height = '400px'; c.style.height = '400px';
document.body.appendChild(c);
map = new L.Map(c); map = new L.Map(c);
map.setView(new L.LatLng(55.8, 37.6), 6); map.setView(new L.LatLng(55.8, 37.6), 6);
}); });
afterEach(function () {
document.body.removeChild(c);
});
it("closes on map click when map has closePopupOnClick option", function () { it("closes on map click when map has closePopupOnClick option", function () {
map.options.closePopupOnClick = true; map.options.closePopupOnClick = true;
@ -51,22 +56,18 @@ describe('Popup', function () {
map.addLayer(marker); map.addLayer(marker);
marker.bindPopup('Popup1'); marker.bindPopup('Popup1');
map.options.closePopupOnClick = true; expect(map.hasLayer(marker._popup)).to.be(false);
// toggle open popup // toggle open popup
marker.fire('click', { happen.click(marker._icon);
latlng: new L.LatLng(55.8, 37.6)
});
expect(map.hasLayer(marker._popup)).to.be(true); expect(map.hasLayer(marker._popup)).to.be(true);
// toggle close popup // toggle close popup
marker.fire('click', { happen.click(marker._icon);
latlng: new L.LatLng(55.8, 37.6)
});
expect(map.hasLayer(marker._popup)).to.be(false); expect(map.hasLayer(marker._popup)).to.be(false);
}); });
it("it should use a popup with a fuction as content with a FeatureGroup", function () { it("it should use a popup with a function as content with a FeatureGroup", function () {
var marker1 = new L.Marker(new L.LatLng(55.8, 37.6)); var marker1 = new L.Marker(new L.LatLng(55.8, 37.6));
var marker2 = new L.Marker(new L.LatLng(54.6, 38.2)); var marker2 = new L.Marker(new L.LatLng(54.6, 38.2));
var group = new L.FeatureGroup([marker1, marker2]).addTo(map); var group = new L.FeatureGroup([marker1, marker2]).addTo(map);

View File

@ -57,6 +57,7 @@ L.Popup = L.Layer.extend({
if (this._source) { if (this._source) {
this._source.fire('popupopen', {popup: this}, true); this._source.fire('popupopen', {popup: this}, true);
this._source.on('preclick', L.DomEvent.stopPropagation);
} }
}, },
@ -77,6 +78,7 @@ L.Popup = L.Layer.extend({
if (this._source) { if (this._source) {
this._source.fire('popupclose', {popup: this}, true); this._source.fire('popupclose', {popup: this}, true);
this._source.off('preclick', L.DomEvent.stopPropagation);
} }
}, },

View File

@ -668,7 +668,6 @@ L.Map = L.Evented.extend({
_handleDOMEvent: function (e) { _handleDOMEvent: function (e) {
if (!this._loaded || L.DomEvent._skipped(e)) { return; } if (!this._loaded || L.DomEvent._skipped(e)) { return; }
// find the layer the event is propagating from and its parents
var type = e.type === 'keypress' && e.keyCode === 13 ? 'click' : e.type; var type = e.type === 'keypress' && e.keyCode === 13 ? 'click' : e.type;
if (e.type === 'click') { if (e.type === 'click') {
@ -690,6 +689,7 @@ L.Map = L.Evented.extend({
if (e._stopped) { return; } if (e._stopped) { return; }
// Find the layer the event is propagating from and its parents.
targets = (targets || []).concat(this._findEventTargets(e, type)); targets = (targets || []).concat(this._findEventTargets(e, type));
if (!targets.length) { return; } if (!targets.length) { return; }