Skip to content

Commit

Permalink
add Path Popup implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Dec 18, 2013
1 parent 919b862 commit 48a3c34
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
1 change: 1 addition & 0 deletions build/deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ var deps = {
'layer/vector2/SVG.js',
'layer/vector2/Canvas.js',
'layer/vector2/Path.js',
'layer/vector2/Path.Popup.js',
'geometry/LineUtil.js',
'layer/vector2/Polyline.js',
'geometry/PolyUtil.js',
Expand Down
6 changes: 3 additions & 3 deletions debug/vector/vector2.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
map.addLayer(L.marker(latlngs[0]));
map.addLayer(L.marker(latlngs[len - 1]));

var path = L.polygon([[latlngs, [[50.5, 30.5], [50.5, 40], [40, 40]]], [[20, 0], [20, 50], [0, 50]]]).addTo(map);
var poly = L.polyline([[60, 30], [60, 50], [40, 50]], {color: 'red'}).addTo(map);
var path = L.polygon([[latlngs, [[50.5, 30.5], [50.5, 40], [40, 40]]], [[20, 0], [20, 40], [0, 40]]]).addTo(map);
var poly = L.polyline([[[60, 30], [60, 50], [40, 50]], [[20, 50], [20, 70], [0, 70]]], {color: 'red'}).addTo(map);

map.fitBounds(path);

// path.bindPopup("Hello world");
path.bindPopup("Hello world");
</script>
</body>
</html>
13 changes: 8 additions & 5 deletions src/layer/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,19 @@ L.popup = function (options, source) {

L.Map.include({
openPopup: function (popup, latlng, options) { // (Popup) or (String || HTMLElement, LatLng[, Object])
this.closePopup();

if (!(popup instanceof L.Popup)) {
var content = popup;

popup = new L.Popup(options)
.setLatLng(latlng)
.setContent(content);
popup = new L.Popup(options).setContent(content);
}

popup.setLatLng(latlng);

if (this.hasLayer(popup)) {
return this;
}

this.closePopup();
this._popup = popup;
return this.addLayer(popup);
},
Expand Down
64 changes: 64 additions & 0 deletions src/layer/vector2/Path.Popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Popup extension to L.Path (polylines, polygons, circles), adding popup-related methods.
*/

L.Path.include({

bindPopup: function (content, options) {

if (content instanceof L.Popup) {
this._popup = content;
} else {
if (!this._popup || options) {
this._popup = new L.Popup(options, this);
}
this._popup.setContent(content);
}

if (!this._popupHandlersAdded) {
this
.on('click', this._openPopup, this)
.on('remove', this.closePopup, this);

this._popupHandlersAdded = true;
}

return this;
},

unbindPopup: function () {
if (this._popup) {
this._popup = null;
this
.off('click', this._openPopup)
.off('remove', this.closePopup);

this._popupHandlersAdded = false;
}
return this;
},

openPopup: function (latlng) {

if (this._popup) {
// open the popup from one of the path's points if not specified
latlng = latlng || this._latlng ||
this._latlngs[Math.floor(this._latlngs.length / 2)];

this._openPopup({latlng: latlng});
}

return this;
},

closePopup: function () {
if (this._popup) {
this._popup._close();
}
return this;
},

_openPopup: function (e) {
this._map.openPopup(this._popup, e.latlng);
}
});

0 comments on commit 48a3c34

Please sign in to comment.