UNPKG

leaflet

Version:

JavaScript library for mobile-friendly interactive maps

173 lines (147 loc) 4.11 kB
/* * @namespace Layer * @section Popup methods example * * All layers share a set of methods convenient for binding popups to it. * * ```js * var layer = L.Polygon(latlngs).bindPopup('Hi There!').addTo(map); * layer.openPopup(); * layer.closePopup(); * ``` * * Popups will also be automatically opened when the layer is clicked on and closed when the layer is removed from the map or another popup is opened. */ // @section Popup methods L.Layer.include({ // @method bindPopup(content: String|HTMLElement|Function|Popup, options?: Popup options): this // Binds a popup to the layer with the passed `content` and sets up the // neccessary event listeners. If a `Function` is passed it will receive // the layer as the first argument and should return a `String` or `HTMLElement`. bindPopup: function (content, options) { if (content instanceof L.Popup) { L.setOptions(content, options); this._popup = content; content._source = this; } else { if (!this._popup || options) { this._popup = new L.Popup(options, this); } this._popup.setContent(content); } if (!this._popupHandlersAdded) { this.on({ click: this._openPopup, remove: this.closePopup, move: this._movePopup }); this._popupHandlersAdded = true; } return this; }, // @method unbindPopup(): this // Removes the popup previously bound with `bindPopup`. unbindPopup: function () { if (this._popup) { this.off({ click: this._openPopup, remove: this.closePopup, move: this._movePopup }); this._popupHandlersAdded = false; this._popup = null; } return this; }, // @method openPopup(latlng?: LatLng): this // Opens the bound popup at the specificed `latlng` or at the default popup anchor if no `latlng` is passed. openPopup: function (layer, latlng) { if (!(layer instanceof L.Layer)) { latlng = layer; layer = this; } if (layer instanceof L.FeatureGroup) { for (var id in this._layers) { layer = this._layers[id]; break; } } if (!latlng) { latlng = layer.getCenter ? layer.getCenter() : layer.getLatLng(); } if (this._popup && this._map) { // set popup source to this layer this._popup._source = layer; // update the popup (content, layout, ect...) this._popup.update(); // open the popup on the map this._map.openPopup(this._popup, latlng); } return this; }, // @method closePopup(): this // Closes the popup bound to this layer if it is open. closePopup: function () { if (this._popup) { this._popup._close(); } return this; }, // @method togglePopup(): this // Opens or closes the popup bound to this layer depending on its current state. togglePopup: function (target) { if (this._popup) { if (this._popup._map) { this.closePopup(); } else { this.openPopup(target); } } return this; }, // @method isPopupOpen(): boolean // Returns `true` if the popup bound to this layer is currently open. isPopupOpen: function () { return this._popup.isOpen(); }, // @method setPopupContent(content: String|HTMLElement|Popup): this // Sets the content of the popup bound to this layer. setPopupContent: function (content) { if (this._popup) { this._popup.setContent(content); } return this; }, // @method getPopup(): Popup // Returns the popup bound to this layer. getPopup: function () { return this._popup; }, _openPopup: function (e) { var layer = e.layer || e.target; if (!this._popup) { return; } if (!this._map) { return; } // prevent map click L.DomEvent.stop(e); // if this inherits from Path its a vector and we can just // open the popup at the new location if (layer instanceof L.Path) { this.openPopup(e.layer || e.target, e.latlng); return; } // otherwise treat it like a marker and figure out // if we should toggle it open/closed if (this._map.hasLayer(this._popup) && this._popup._source === layer) { this.closePopup(); } else { this.openPopup(layer, e.latlng); } }, _movePopup: function (e) { this._popup.setLatLng(e.latlng); } });