UNPKG

leaflet

Version:

JavaScript library for mobile-friendly interactive maps

278 lines (277 loc) • 11.2 kB
import { LayerBase } from './LayerBase.js'; import { Popup } from './Popup.js'; import { Tooltip } from './Tooltip.js'; import type { LatLngExpression, LayerEventMap, LayerOptions, LeafletEvent, LeafletPointerEvent, MarkerMoveEvent, PopupOptions, TooltipOptions } from '../types.js'; import type { LatLng } from '../geo/LatLng.js'; import type { OverlayContent } from './DivOverlay.js'; export type OverlayTriggerEvents = { click?: LeafletPointerEvent; pointermove?: LeafletPointerEvent; move?: MarkerMoveEvent; preclick?: LeafletPointerEvent; }; export type InteractiveLayerProps = { /** * If `false`, the layer will not emit pointer events and will * act as a part of the underlying map. */ interactive: boolean; /** * When `true`, a pointer event on this layer will trigger the * same event on the map (unless [`DomEvent.stopPropagation`](#domevent-stoppropagation) is used). */ bubblingPointerEvents: boolean; }; export type InteractiveLayerEvents = { /** * Fired when the user clicks (or taps) the layer. */ click: LeafletPointerEvent; /** * Fired right before `click`, for something that * should happen before any `click` listener runs. */ preclick: LeafletPointerEvent; /** * Fired when the user double-clicks (or double-taps) the layer. */ dblclick: LeafletPointerEvent; /** * Fired when the user pushes the pointer on the layer. */ pointerdown: LeafletPointerEvent; /** * Fired when the user releases the pointer pushed on the layer. */ pointerup: LeafletPointerEvent; /** * Fired when the pointer enters the layer. */ pointerover: LeafletPointerEvent; /** * Fired when the pointer leaves the layer. */ pointerout: LeafletPointerEvent; /** * Fired when the user right-clicks on the layer, * prevents default browser context menu from showing if there are listeners on this event. Also * fired on mobile when the user holds a single touch for a second (also called long press). */ contextmenu: LeafletPointerEvent; }; /** * Events that bound popups and tooltips listen to, with the event objects they expect if a layer fires them. * @typedef {Object} OverlayTriggerEvents * @property {LeafletPointerEvent} [click] * @property {LeafletPointerEvent} [pointermove] * @property {MarkerMoveEvent} [move] * @property {LeafletPointerEvent} [preclick] */ /** * @typedef {Object} InteractiveLayerProps * @property {boolean} interactive If `false`, the layer will not emit pointer events and will * act as a part of the underlying map. * @property {boolean} bubblingPointerEvents When `true`, a pointer event on this layer will trigger the * same event on the map (unless [`DomEvent.stopPropagation`](#domevent-stoppropagation) is used). */ /** * @category Pointer events * @typedef {Object} InteractiveLayerEvents * @property {LeafletPointerEvent} click Fired when the user clicks (or taps) the layer. * @property {LeafletPointerEvent} preclick Fired right before `click`, for something that * should happen before any `click` listener runs. * @property {LeafletPointerEvent} dblclick Fired when the user double-clicks (or double-taps) the layer. * @property {LeafletPointerEvent} pointerdown Fired when the user pushes the pointer on the layer. * @property {LeafletPointerEvent} pointerup Fired when the user releases the pointer pushed on the layer. * @property {LeafletPointerEvent} pointerover Fired when the pointer enters the layer. * @property {LeafletPointerEvent} pointerout Fired when the pointer leaves the layer. * @property {LeafletPointerEvent} contextmenu Fired when the user right-clicks on the layer, * prevents default browser context menu from showing if there are listeners on this event. Also * fired on mobile when the user holds a single touch for a second (also called long press). */ /** * A set of methods from the Layer base class that all Leaflet layers use. Inherits all methods, options and events * from `Evented`. * * @example * ```js * const layer = new Marker(latlng).addTo(map); * layer.addTo(map); * layer.remove(); * ``` * * @categoryDescription Methods * Classes extending `Layer` will inherit the following methods: * * @categoryDescription Popup methods * All layers share a set of methods convenient for binding popups to it. * * ```js * const layer = new 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. * * @categoryDescription Tooltip methods * All layers share a set of methods convenient for binding tooltips to it. * * ```js * const layer = new Polygon(latlngs).bindTooltip('Hi There!').addTo(map); * layer.openTooltip(); * layer.closeTooltip(); * ``` * * @categoryDescription Extension methods * Every layer should extend from `Layer` and (re-)implement the following methods. * * @template {LayerOptions} [O=LayerOptions] * @template {LayerEventMap & OverlayTriggerEvents} [E=LayerEventMap] * @extends {LayerBase<O, E>} */ export declare class Layer<O extends LayerOptions = LayerOptions, E extends LayerEventMap & OverlayTriggerEvents = LayerEventMap> extends LayerBase<O, E> { /** @type {Popup | null | undefined} */ _popup: Popup | null | undefined; /** @type {Tooltip | null | undefined} */ _tooltip: Tooltip | null | undefined; _moveEndOpensTooltip: boolean | undefined; /** * Binds a popup to the layer with the passed `content` and sets up the necessary event listeners. A * `String` argument is rendered as HTML; if it may contain untrusted input, sanitize it (e.g. with * DOMPurify) or pass an `HTMLElement` with safe `textContent` instead. If a `Function` is passed it * will receive the layer as the first argument and should return a `String` or `HTMLElement`. * @category Popup methods * @param {OverlayContent | Popup} content * @param {Partial<PopupOptions>} [options] */ bindPopup(content: OverlayContent | Popup, options?: Partial<PopupOptions>): this; /** * Removes the popup previously bound with `bindPopup`. * @category Popup methods */ unbindPopup(): this; /** * Opens the bound popup at the specified `latlng` or at the default popup anchor if no `latlng` is passed. * @category Popup methods * @param {LatLngExpression} [latlng] * @returns {this} */ openPopup(latlng?: LatLngExpression): this; /** * Closes the popup bound to this layer if it is open. * @category Popup methods */ closePopup(): this; /** * Opens or closes the popup bound to this layer depending on its current state. * @category Popup methods */ togglePopup(): this; /** * Returns `true` if the popup bound to this layer is currently open. * @category Popup methods */ isPopupOpen(): boolean; /** * Sets the content of the popup bound to this layer. String content is rendered as HTML; * sanitize untrusted input or pass an `HTMLElement` with safe `textContent` instead. * @category Popup methods * @param {OverlayContent} content */ setPopupContent(content: OverlayContent): this; /** * Returns the popup bound to this layer. * @category Popup methods * @returns {Popup | null | undefined} */ getPopup(): Popup | null | undefined; /** @param {LeafletPointerEvent} e */ _openPopup(e: LeafletPointerEvent): void; /** @param {{latlng: LatLng}} e */ _movePopup(e: { latlng: LatLng; }): void; /** * Binds a tooltip to the layer with the passed `content` and sets up the necessary event listeners. * A `String` argument is rendered as HTML; if it may contain untrusted input, sanitize it (e.g. with * DOMPurify) or pass an `HTMLElement` with safe `textContent` instead. If a `Function` is passed it * will receive the layer as the first argument and should return a `String` or `HTMLElement`. * @category Tooltip methods * @param {OverlayContent | Tooltip} content * @param {Partial<TooltipOptions>} [options] */ bindTooltip(content: OverlayContent | Tooltip, options?: Partial<TooltipOptions>): this; /** * Removes the tooltip previously bound with `bindTooltip`. * @category Tooltip methods */ unbindTooltip(): this; /** @param {boolean} [remove] */ _initTooltipInteractions(remove?: boolean): void; /** * Opens the bound tooltip at the specified `latlng` or at the default tooltip anchor if no `latlng` is passed. * @category Tooltip methods * @param {LatLngExpression} [latlng] * @returns {this} */ openTooltip(latlng?: LatLngExpression): this; /** * Closes the tooltip bound to this layer if it is open. * @category Tooltip methods */ closeTooltip(): this; /** * Opens or closes the tooltip bound to this layer depending on its current state. * @category Tooltip methods */ toggleTooltip(): this; /** * Returns `true` if the tooltip bound to this layer is currently open. * @category Tooltip methods */ isTooltipOpen(): boolean; /** * Sets the content of the tooltip bound to this layer. String content is rendered as HTML; * sanitize untrusted input or pass an `HTMLElement` with safe `textContent` instead. * @category Tooltip methods * @param {OverlayContent} content */ setTooltipContent(content: OverlayContent): this; /** * Returns the tooltip bound to this layer. * @category Tooltip methods * @returns {Tooltip | null | undefined} */ getTooltip(): Tooltip | null | undefined; /** @param {unknown} add also the `add` listener, so any truthy argument adds */ _toggleFocusListeners(add: unknown): void; /** * @param {Layer} layer * @param {boolean} remove */ _addFocusListenersOnLayer(layer: Layer, remove: boolean): void; /** @param {Layer} layer */ _setAriaDescribedByOnLayer(layer: Layer): void; /** @param {LeafletEvent & {latlng?: LatLng}} e */ _openTooltip(e: LeafletEvent & { latlng?: LatLng; }): void; /** @param {{latlng: LatLng, originalEvent?: PointerEvent | MouseEvent}} e */ _moveTooltip(e: { latlng: LatLng; originalEvent?: PointerEvent | MouseEvent; }): void; /** * @template {Popup | Tooltip} T * @param {new (options?: any, source?: Layer) => T} OverlayClass * @param {T | null | undefined} old * @param {OverlayContent | T} content * @param {object} [options] */ _initOverlay<T extends Popup | Tooltip>(OverlayClass: new (options?: any, source?: Layer) => T, old: T | null | undefined, content: OverlayContent | T, options?: object): T; /** @returns {Layer | undefined} */ _getOverlaySource(): Layer | undefined; _togglesPopupOnClick(): boolean; }