simplify popup methods, merge #738

This commit is contained in:
Vladimir Agafonkin 2013-04-20 16:58:42 +03:00
commit 7cb7993571
5 changed files with 92 additions and 32 deletions

View File

@ -67,8 +67,7 @@ var deps = {
Popup: {
src: ['layer/Popup.js',
'layer/marker/Marker.Popup.js',
'map/ext/Map.Popup.js'],
'layer/marker/Marker.Popup.js'],
deps: ['Marker'],
desc: 'Used to display the map popup (used mostly for binding HTML data to markers and paths on click).'
},

View File

@ -59,6 +59,7 @@
<script type="text/javascript" src="suites/layer/GeoJSONSpec.js"></script>
<script type="text/javascript" src="suites/layer/LayerGroupSpec.js"></script>
<script type="text/javascript" src="suites/layer/TileLayerSpec.js"></script>
<script type="text/javascript" src="suites/layer/PopupSpec.js"></script>
<!-- /layer/vector/ -->
<script type="text/javascript" src="suites/layer/vector/CircleSpec.js"></script>

View File

@ -0,0 +1,59 @@
describe('Popup', function() {
var map;
beforeEach(function () {
var c = document.createElement('div');
c.style.width = '400px';
c.style.height = '400px';
map = new L.Map(c);
map.setView(new L.LatLng(55.8, 37.6), 6);
});
it("should trigger popupopen on marker when popup opens", function() {
var marker1 = new L.Marker(new L.LatLng(55.8, 37.6));
var marker2 = new L.Marker(new L.LatLng(57.123076977278, 44.861962891635));
map.addLayer(marker1);
map.addLayer(marker2);
marker1.bindPopup('Popup1');
marker2.bindPopup('Popup2');
var spy = sinon.spy();
marker1.on('popupopen', spy);
expect(spy.called).to.be(false);
marker2.openPopup();
expect(spy.called).to.be(false);
marker1.openPopup();
expect(spy.called).to.be(true);
});
it("should trigger popupclose on marker when popup closes", function() {
var marker1 = new L.Marker(new L.LatLng(55.8, 37.6));
var marker2 = new L.Marker(new L.LatLng(57.123076977278, 44.861962891635));
map.addLayer(marker1);
map.addLayer(marker2);
marker1.bindPopup('Popup1');
marker2.bindPopup('Popup2');
var spy = sinon.spy();
marker1.on('popupclose', spy);
expect(spy.called).to.be(false);
marker2.openPopup();
expect(spy.called).to.be(false);
marker1.openPopup();
expect(spy.called).to.be(false);
marker2.openPopup();
expect(spy.called).to.be(true);
marker1.openPopup();
marker1.closePopup();
expect(spy.callCount).to.be(2);
});
});

View File

@ -51,6 +51,12 @@ L.Popup = L.Class.extend({
if (animFade) {
L.DomUtil.setOpacity(this._container, 1);
}
map.fire('popupopen', {popup: this});
if (this._source) {
this._source.fire('popupopen', {popup: this});
}
},
addTo: function (map) {
@ -75,6 +81,12 @@ L.Popup = L.Class.extend({
}
this._map = null;
map.fire('popupclose', {popup: this});
if (this._source) {
this._source.fire('popupclose', {popup: this});
}
},
setLatLng: function (latlng) {
@ -108,14 +120,8 @@ L.Popup = L.Class.extend({
},
_close: function () {
var map = this._map;
if (map) {
map._popup = null;
map
.removeLayer(this)
.fire('popupclose', {popup: this});
if (this._map) {
this._map.removeLayer(this);
}
},
@ -278,3 +284,20 @@ L.Popup = L.Class.extend({
L.popup = function (options, source) {
return new L.Popup(options, source);
};
L.Map.include({
openPopup: function (popup) {
this.closePopup();
this._popup = popup;
return this.addLayer(popup);
},
closePopup: function () {
if (this._popup) {
this.removeLayer(this._popup);
this._popup = null;
}
return this;
}
});

View File

@ -1,22 +0,0 @@
/*
* Adds popup-related methods to L.Map.
*/
L.Map.include({
openPopup: function (popup) {
this.closePopup();
this._popup = popup;
return this
.addLayer(popup)
.fire('popupopen', {popup: this._popup});
},
closePopup: function () {
if (this._popup) {
this._popup._close();
}
return this;
}
});