add Path Popup implementation

This commit is contained in:
Vladimir Agafonkin 2013-12-12 19:04:51 -05:00
parent 919b862c15
commit 48a3c34702
4 changed files with 76 additions and 8 deletions

View File

@ -156,6 +156,7 @@ var deps = {
'layer/vector2/SVG.js', 'layer/vector2/SVG.js',
'layer/vector2/Canvas.js', 'layer/vector2/Canvas.js',
'layer/vector2/Path.js', 'layer/vector2/Path.js',
'layer/vector2/Path.Popup.js',
'geometry/LineUtil.js', 'geometry/LineUtil.js',
'layer/vector2/Polyline.js', 'layer/vector2/Polyline.js',
'geometry/PolyUtil.js', 'geometry/PolyUtil.js',

View File

@ -26,12 +26,12 @@
map.addLayer(L.marker(latlngs[0])); map.addLayer(L.marker(latlngs[0]));
map.addLayer(L.marker(latlngs[len - 1])); 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 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]], {color: 'red'}).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); map.fitBounds(path);
// path.bindPopup("Hello world"); path.bindPopup("Hello world");
</script> </script>
</body> </body>
</html> </html>

View File

@ -289,16 +289,19 @@ L.popup = function (options, source) {
L.Map.include({ L.Map.include({
openPopup: function (popup, latlng, options) { // (Popup) or (String || HTMLElement, LatLng[, Object]) openPopup: function (popup, latlng, options) { // (Popup) or (String || HTMLElement, LatLng[, Object])
this.closePopup();
if (!(popup instanceof L.Popup)) { if (!(popup instanceof L.Popup)) {
var content = popup; var content = popup;
popup = new L.Popup(options) popup = new L.Popup(options).setContent(content);
.setLatLng(latlng)
.setContent(content);
} }
popup.setLatLng(latlng);
if (this.hasLayer(popup)) {
return this;
}
this.closePopup();
this._popup = popup; this._popup = popup;
return this.addLayer(popup); return this.addLayer(popup);
}, },

View File

@ -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);
}
});