Leaflet/src/layer/marker/Marker.Popup.js
Vladimir Agafonkin 3cef077128 Merge pull request #1385 from snkashis/bind_popup_option
Allow previously created popups to be used with bindPopup
2013-02-15 07:35:03 -08:00

73 lines
1.5 KiB
JavaScript

/*
* Popup extension to L.Marker, adding popup-related methods.
*/
L.Marker.include({
openPopup: function () {
if (this._popup && this._map && !this._map.hasLayer(this._popup)) {
this._popup.setLatLng(this._latlng);
this._map.openPopup(this._popup);
}
return this;
},
closePopup: function () {
if (this._popup) {
this._popup._close();
}
return this;
},
bindPopup: function (content, options) {
var anchor = L.point(this.options.icon.options.popupAnchor) || new L.Point(0, 0);
anchor = anchor.add(L.Popup.prototype.options.offset);
if (options && options.offset) {
anchor = anchor.add(options.offset);
}
options = L.extend({offset: anchor}, options);
if (!this._popup) {
this
.on('click', this.openPopup, this)
.on('remove', this.closePopup, this)
.on('move', this._movePopup, this);
}
if (content instanceof L.Popup) {
L.setOptions(content, options);
this._popup = content;
} else {
this._popup = new L.Popup(options, this)
.setContent(content);
}
return this;
},
setPopupContent: function (content) {
if (this._popup) {
this._popup.setContent(content);
}
return this;
},
unbindPopup: function () {
if (this._popup) {
this._popup = null;
this
.off('click', this.openPopup)
.off('remove', this.closePopup)
.off('move', this._movePopup);
}
return this;
},
_movePopup: function (e) {
this._popup.setLatLng(e.latlng);
}
});