From 48a3c3470238c61b5b46b6d21e6b7d277c5cc8c1 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Thu, 12 Dec 2013 19:04:51 -0500 Subject: [PATCH] add Path Popup implementation --- build/deps.js | 1 + debug/vector/vector2.html | 6 ++-- src/layer/Popup.js | 13 ++++--- src/layer/vector2/Path.Popup.js | 64 +++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 src/layer/vector2/Path.Popup.js diff --git a/build/deps.js b/build/deps.js index c2115be6..87ee5330 100644 --- a/build/deps.js +++ b/build/deps.js @@ -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', diff --git a/debug/vector/vector2.html b/debug/vector/vector2.html index 09147986..5ef3b0cc 100644 --- a/debug/vector/vector2.html +++ b/debug/vector/vector2.html @@ -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"); diff --git a/src/layer/Popup.js b/src/layer/Popup.js index 8d524e93..cc7c0bfe 100644 --- a/src/layer/Popup.js +++ b/src/layer/Popup.js @@ -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); }, diff --git a/src/layer/vector2/Path.Popup.js b/src/layer/vector2/Path.Popup.js new file mode 100644 index 00000000..5b533d8f --- /dev/null +++ b/src/layer/vector2/Path.Popup.js @@ -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); + } +});