maplair 4.0.0-alpha.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +47 -3
- package/LICENSE +1 -1
- package/README.md +10 -7
- package/dist/esm/Maplair.js +52 -33
- package/dist/esm/Maplair.js.map +1 -1
- package/dist/esm/control/AttributionControl.js +76 -4
- package/dist/esm/control/AttributionControl.js.map +1 -1
- package/dist/esm/control/ButtonControl.js +221 -0
- package/dist/esm/control/ButtonControl.js.map +1 -0
- package/dist/esm/control/ContextMenu.js +173 -0
- package/dist/esm/control/ContextMenu.js.map +1 -0
- package/dist/esm/control/Control.js +118 -30
- package/dist/esm/control/Control.js.map +1 -1
- package/dist/esm/control/FullscreenControl.js +186 -0
- package/dist/esm/control/FullscreenControl.js.map +1 -0
- package/dist/esm/control/LayersControl.js +122 -3
- package/dist/esm/control/LayersControl.js.map +1 -1
- package/dist/esm/control/LegendControl.js +230 -0
- package/dist/esm/control/LegendControl.js.map +1 -0
- package/dist/esm/control/MiniMapControl.js +182 -0
- package/dist/esm/control/MiniMapControl.js.map +1 -0
- package/dist/esm/control/RotateControl.js +104 -0
- package/dist/esm/control/RotateControl.js.map +1 -0
- package/dist/esm/control/ScaleControl.js +40 -0
- package/dist/esm/control/ScaleControl.js.map +1 -1
- package/dist/esm/control/SearchControl.js +442 -0
- package/dist/esm/control/SearchControl.js.map +1 -0
- package/dist/esm/control/SideBySideControl.js +208 -0
- package/dist/esm/control/SideBySideControl.js.map +1 -0
- package/dist/esm/control/TextControl.js +83 -0
- package/dist/esm/control/TextControl.js.map +1 -0
- package/dist/esm/control/ZoomControl.js +68 -5
- package/dist/esm/control/ZoomControl.js.map +1 -1
- package/dist/esm/core/Browser.js +71 -2
- package/dist/esm/core/Browser.js.map +1 -1
- package/dist/esm/core/Class.js +193 -0
- package/dist/esm/core/Class.js.map +1 -1
- package/dist/esm/core/Events.js +231 -0
- package/dist/esm/core/Events.js.map +1 -1
- package/dist/esm/core/Handler.js +34 -0
- package/dist/esm/core/Handler.js.map +1 -1
- package/dist/esm/core/I18n.js +38 -0
- package/dist/esm/core/I18n.js.map +1 -1
- package/dist/esm/core/Util.js +62 -0
- package/dist/esm/core/Util.js.map +1 -1
- package/dist/esm/dom/DomEvent.DoubleTap.js +14 -0
- package/dist/esm/dom/DomEvent.DoubleTap.js.map +1 -1
- package/dist/esm/dom/DomEvent.PointerEvents.js +25 -1
- package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -1
- package/dist/esm/dom/DomEvent.js +95 -0
- package/dist/esm/dom/DomEvent.js.map +1 -1
- package/dist/esm/dom/DomUtil.js +94 -0
- package/dist/esm/dom/DomUtil.js.map +1 -1
- package/dist/esm/dom/Draggable.js +55 -1
- package/dist/esm/dom/Draggable.js.map +1 -1
- package/dist/esm/dom/PosAnimation.js +48 -0
- package/dist/esm/dom/PosAnimation.js.map +1 -1
- package/dist/esm/geo/LatLng.js +154 -3
- package/dist/esm/geo/LatLng.js.map +1 -1
- package/dist/esm/geo/LatLngBounds.js +148 -7
- package/dist/esm/geo/LatLngBounds.js.map +1 -1
- package/dist/esm/geo/crs/CRS.js +102 -1
- package/dist/esm/geo/crs/CRS.js.map +1 -1
- package/dist/esm/geo/crs/EPSG3395.js +8 -1
- package/dist/esm/geo/crs/EPSG3395.js.map +1 -1
- package/dist/esm/geo/crs/EPSG3857.js +14 -1
- package/dist/esm/geo/crs/EPSG3857.js.map +1 -1
- package/dist/esm/geo/crs/EPSG4326.js +15 -1
- package/dist/esm/geo/crs/EPSG4326.js.map +1 -1
- package/dist/esm/geo/crs/EarthCRS.js +24 -0
- package/dist/esm/geo/crs/EarthCRS.js.map +1 -1
- package/dist/esm/geo/crs/SimpleCRS.js +28 -1
- package/dist/esm/geo/crs/SimpleCRS.js.map +1 -1
- package/dist/esm/geo/projection/Projection.LonLat.js +10 -2
- package/dist/esm/geo/projection/Projection.LonLat.js.map +1 -1
- package/dist/esm/geo/projection/Projection.Mercator.js +7 -2
- package/dist/esm/geo/projection/Projection.Mercator.js.map +1 -1
- package/dist/esm/geo/projection/Projection.SphericalMercator.js +9 -2
- package/dist/esm/geo/projection/Projection.SphericalMercator.js.map +1 -1
- package/dist/esm/geo/projection/index.js +1 -1
- package/dist/esm/geo/projection/index.js.map +1 -1
- package/dist/esm/geometry/Bounds.js +125 -7
- package/dist/esm/geometry/Bounds.js.map +1 -1
- package/dist/esm/geometry/LineUtil.js +55 -0
- package/dist/esm/geometry/LineUtil.js.map +1 -1
- package/dist/esm/geometry/Point.js +144 -5
- package/dist/esm/geometry/Point.js.map +1 -1
- package/dist/esm/geometry/PolyUtil.js +27 -1
- package/dist/esm/geometry/PolyUtil.js.map +1 -1
- package/dist/esm/geometry/Transformation.js +35 -0
- package/dist/esm/geometry/Transformation.js.map +1 -1
- package/dist/esm/layer/BlanketOverlay.js +54 -1
- package/dist/esm/layer/BlanketOverlay.js.map +1 -1
- package/dist/esm/layer/Classification.js +273 -0
- package/dist/esm/layer/Classification.js.map +1 -0
- package/dist/esm/layer/DivOverlay.js +219 -24
- package/dist/esm/layer/DivOverlay.js.map +1 -1
- package/dist/esm/layer/FeatureGroup.js +71 -0
- package/dist/esm/layer/FeatureGroup.js.map +1 -1
- package/dist/esm/layer/GeoJSON.Load.js +61 -0
- package/dist/esm/layer/GeoJSON.Load.js.map +1 -0
- package/dist/esm/layer/GeoJSON.js +219 -8
- package/dist/esm/layer/GeoJSON.js.map +1 -1
- package/dist/esm/layer/HeatLayer.js +198 -0
- package/dist/esm/layer/HeatLayer.js.map +1 -0
- package/dist/esm/layer/ImageOverlay.js +126 -1
- package/dist/esm/layer/ImageOverlay.js.map +1 -1
- package/dist/esm/layer/Layer.js +273 -63
- package/dist/esm/layer/Layer.js.map +1 -1
- package/dist/esm/layer/LayerGroup.js +77 -0
- package/dist/esm/layer/LayerGroup.js.map +1 -1
- package/dist/esm/layer/Popup.js +333 -90
- package/dist/esm/layer/Popup.js.map +1 -1
- package/dist/esm/layer/SVGOverlay.js +33 -0
- package/dist/esm/layer/SVGOverlay.js.map +1 -1
- package/dist/esm/layer/Tooltip.js +405 -149
- package/dist/esm/layer/Tooltip.js.map +1 -1
- package/dist/esm/layer/VideoOverlay.js +49 -0
- package/dist/esm/layer/VideoOverlay.js.map +1 -1
- package/dist/esm/layer/marker/DefaultIcon.js +33 -0
- package/dist/esm/layer/marker/DefaultIcon.js.map +1 -1
- package/dist/esm/layer/marker/DivIcon.js +45 -1
- package/dist/esm/layer/marker/DivIcon.js.map +1 -1
- package/dist/esm/layer/marker/Icon.js +73 -2
- package/dist/esm/layer/marker/Icon.js.map +1 -1
- package/dist/esm/layer/marker/Marker.Drag.js +29 -1
- package/dist/esm/layer/marker/Marker.Drag.js.map +1 -1
- package/dist/esm/layer/marker/Marker.Move.js +107 -0
- package/dist/esm/layer/marker/Marker.Move.js.map +1 -0
- package/dist/esm/layer/marker/Marker.Rotation.js +70 -0
- package/dist/esm/layer/marker/Marker.Rotation.js.map +1 -0
- package/dist/esm/layer/marker/Marker.js +157 -3
- package/dist/esm/layer/marker/Marker.js.map +1 -1
- package/dist/esm/layer/marker/SvgIcon.js +116 -0
- package/dist/esm/layer/marker/SvgIcon.js.map +1 -0
- package/dist/esm/layer/tile/GridLayer.js +254 -5
- package/dist/esm/layer/tile/GridLayer.js.map +1 -1
- package/dist/esm/layer/tile/TileLayer.js +190 -8
- package/dist/esm/layer/tile/TileLayer.js.map +1 -1
- package/dist/esm/layer/tile/WMSTileLayer.js +114 -6
- package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -1
- package/dist/esm/layer/vector/Canvas.js +58 -0
- package/dist/esm/layer/vector/Canvas.js.map +1 -1
- package/dist/esm/layer/vector/Circle.js +30 -0
- package/dist/esm/layer/vector/Circle.js.map +1 -1
- package/dist/esm/layer/vector/CircleMarker.js +54 -1
- package/dist/esm/layer/vector/CircleMarker.js.map +1 -1
- package/dist/esm/layer/vector/Path.DashAnimation.js +78 -0
- package/dist/esm/layer/vector/Path.DashAnimation.js.map +1 -0
- package/dist/esm/layer/vector/Path.ZoomStyles.js +77 -0
- package/dist/esm/layer/vector/Path.ZoomStyles.js.map +1 -0
- package/dist/esm/layer/vector/Path.js +74 -0
- package/dist/esm/layer/vector/Path.js.map +1 -1
- package/dist/esm/layer/vector/Polygon.js +67 -0
- package/dist/esm/layer/vector/Polygon.js.map +1 -1
- package/dist/esm/layer/vector/Polyline.Decorations.js +244 -0
- package/dist/esm/layer/vector/Polyline.Decorations.js.map +1 -0
- package/dist/esm/layer/vector/Polyline.js +91 -2
- package/dist/esm/layer/vector/Polyline.js.map +1 -1
- package/dist/esm/layer/vector/Rectangle.js +28 -0
- package/dist/esm/layer/vector/Rectangle.js.map +1 -1
- package/dist/esm/layer/vector/Renderer.getRenderer.js +52 -20
- package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -1
- package/dist/esm/layer/vector/Renderer.js +36 -0
- package/dist/esm/layer/vector/Renderer.js.map +1 -1
- package/dist/esm/layer/vector/SVG.js +49 -0
- package/dist/esm/layer/vector/SVG.js.map +1 -1
- package/dist/esm/map/Hash.js +237 -0
- package/dist/esm/map/Hash.js.map +1 -0
- package/dist/esm/map/Map.Rotation.js +500 -0
- package/dist/esm/map/Map.Rotation.js.map +1 -0
- package/dist/esm/map/Map.js +817 -33
- package/dist/esm/map/Map.js.map +1 -1
- package/dist/esm/map/MapSync.js +168 -0
- package/dist/esm/map/MapSync.js.map +1 -0
- package/dist/esm/map/handler/BoxZoomHandler.js +50 -1
- package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -1
- package/dist/esm/map/handler/DisplaceHandler.js +349 -0
- package/dist/esm/map/handler/DisplaceHandler.js.map +1 -0
- package/dist/esm/map/handler/DoubleClickZoomHandler.js +44 -0
- package/dist/esm/map/handler/DoubleClickZoomHandler.js.map +1 -1
- package/dist/esm/map/handler/DragHandler.js +67 -5
- package/dist/esm/map/handler/DragHandler.js.map +1 -1
- package/dist/esm/map/handler/KeyboardHandler.js +92 -5
- package/dist/esm/map/handler/KeyboardHandler.js.map +1 -1
- package/dist/esm/map/handler/PinchZoomHandler.js +39 -0
- package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -1
- package/dist/esm/map/handler/ScrollWheelZoomHandler.js +161 -0
- package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -1
- package/dist/esm/map/handler/TapDragZoomHandler.js +150 -0
- package/dist/esm/map/handler/TapDragZoomHandler.js.map +1 -0
- package/dist/esm/map/handler/TapHoldHandler.js +35 -0
- package/dist/esm/map/handler/TapHoldHandler.js.map +1 -1
- package/dist/esm/package.js +1 -1
- package/dist/maplair-global-src.js +15438 -4749
- package/dist/maplair-global-src.js.map +1 -1
- package/dist/maplair-global.js +3 -3
- package/dist/maplair-global.js.map +1 -1
- package/dist/maplair-src.js +15421 -4750
- package/dist/maplair-src.js.map +1 -1
- package/dist/maplair.css +629 -146
- package/dist/maplair.js +3 -3
- package/dist/maplair.js.map +1 -1
- package/i18n/cs.json +21 -1
- package/i18n/de.json +21 -1
- package/i18n/en.json +21 -1
- package/i18n/es.json +21 -1
- package/i18n/fr.json +21 -1
- package/i18n/hu.json +21 -1
- package/i18n/it.json +21 -1
- package/i18n/rm.json +21 -1
- package/i18n/sk.json +21 -1
- package/i18n/sl.json +21 -1
- package/package.json +84 -34
- package/src/Maplair.all.js +10 -0
- package/src/Maplair.js +4 -0
- package/src/control/AttributionControl.js +78 -31
- package/src/control/ButtonControl.js +248 -0
- package/src/control/ContextMenu.js +201 -0
- package/src/control/Control.js +132 -87
- package/src/control/FullscreenControl.js +227 -0
- package/src/control/LayersControl.js +92 -56
- package/src/control/LegendControl.js +273 -0
- package/src/control/MiniMapControl.js +198 -0
- package/src/control/RotateControl.js +122 -0
- package/src/control/ScaleControl.js +34 -25
- package/src/control/SearchControl.js +530 -0
- package/src/control/SideBySideControl.js +236 -0
- package/src/control/TextControl.js +91 -0
- package/src/control/ZoomControl.js +63 -35
- package/src/control/index.js +8 -0
- package/src/core/Browser.js +71 -36
- package/src/core/Class.js +193 -18
- package/src/core/Events.js +227 -35
- package/src/core/Handler.js +34 -24
- package/src/core/I18n.js +26 -19
- package/src/core/Util.js +63 -34
- package/src/dom/DomEvent.DoubleTap.js +15 -0
- package/src/dom/DomEvent.PointerEvents.js +30 -15
- package/src/dom/DomEvent.js +98 -62
- package/src/dom/DomUtil.js +102 -52
- package/src/dom/Draggable.js +49 -29
- package/src/dom/PosAnimation.js +27 -22
- package/src/geo/LatLng.js +150 -54
- package/src/geo/LatLngBounds.js +129 -69
- package/src/geo/crs/CRS.js +95 -56
- package/src/geo/crs/EPSG3395.js +5 -5
- package/src/geo/crs/EPSG3857.js +9 -6
- package/src/geo/crs/EPSG4326.js +11 -9
- package/src/geo/crs/EarthCRS.js +19 -9
- package/src/geo/crs/SimpleCRS.js +22 -5
- package/src/geo/projection/Projection.LonLat.js +7 -11
- package/src/geo/projection/Projection.Mercator.js +8 -8
- package/src/geo/projection/Projection.SphericalMercator.js +8 -9
- package/src/geo/projection/index.js +17 -15
- package/src/geometry/Bounds.js +108 -63
- package/src/geometry/LineUtil.js +56 -27
- package/src/geometry/Point.js +128 -69
- package/src/geometry/PolyUtil.js +22 -10
- package/src/geometry/Transformation.js +23 -18
- package/src/layer/BlanketOverlay.js +50 -51
- package/src/layer/Classification.js +302 -0
- package/src/layer/DivOverlay.js +247 -92
- package/src/layer/FeatureGroup.js +62 -26
- package/src/layer/GeoJSON.Load.js +109 -0
- package/src/layer/GeoJSON.js +220 -124
- package/src/layer/HeatLayer.js +231 -0
- package/src/layer/ImageOverlay.js +122 -62
- package/src/layer/Layer.js +311 -192
- package/src/layer/LayerGroup.js +66 -35
- package/src/layer/Popup.js +343 -270
- package/src/layer/SVGOverlay.js +22 -15
- package/src/layer/Tooltip.js +453 -298
- package/src/layer/VideoOverlay.js +41 -37
- package/src/layer/index.js +2 -0
- package/src/layer/marker/DefaultIcon.js +27 -11
- package/src/layer/marker/DivIcon.js +36 -20
- package/src/layer/marker/Icon.js +56 -61
- package/src/layer/marker/Marker.Drag.js +26 -29
- package/src/layer/marker/Marker.Move.js +153 -0
- package/src/layer/marker/Marker.Rotation.js +103 -0
- package/src/layer/marker/Marker.js +153 -94
- package/src/layer/marker/SvgIcon.js +122 -0
- package/src/layer/marker/index.js +1 -0
- package/src/layer/tile/GridLayer.js +224 -112
- package/src/layer/tile/TileLayer.js +204 -80
- package/src/layer/tile/WMSTileLayer.js +118 -44
- package/src/layer/vector/Canvas.js +44 -12
- package/src/layer/vector/Circle.js +24 -22
- package/src/layer/vector/CircleMarker.js +53 -22
- package/src/layer/vector/Path.DashAnimation.js +108 -0
- package/src/layer/vector/Path.ZoomStyles.js +103 -0
- package/src/layer/vector/Path.js +73 -57
- package/src/layer/vector/Polygon.js +32 -12
- package/src/layer/vector/Polyline.Decorations.js +299 -0
- package/src/layer/vector/Polyline.js +60 -37
- package/src/layer/vector/Rectangle.js +14 -14
- package/src/layer/vector/Renderer.getRenderer.js +65 -43
- package/src/layer/vector/Renderer.js +24 -9
- package/src/layer/vector/SVG.js +29 -19
- package/src/map/Hash.js +255 -0
- package/src/map/Map.Rotation.js +927 -0
- package/src/map/Map.js +866 -421
- package/src/map/MapSync.js +225 -0
- package/src/map/handler/BoxZoomHandler.js +50 -12
- package/src/map/handler/DisplaceHandler.js +416 -0
- package/src/map/handler/DoubleClickZoomHandler.js +43 -22
- package/src/map/handler/DragHandler.js +65 -40
- package/src/map/handler/KeyboardHandler.js +96 -18
- package/src/map/handler/PinchZoomHandler.js +39 -18
- package/src/map/handler/ScrollWheelZoomHandler.js +204 -21
- package/src/map/handler/TapDragZoomHandler.js +209 -0
- package/src/map/handler/TapHoldHandler.js +36 -17
- package/src/map/index.js +4 -0
- package/src/maplair.css +629 -146
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { disableClickPropagation, on, stop } from "../dom/DomEvent.js";
|
|
2
|
+
import { create } from "../dom/DomUtil.js";
|
|
3
|
+
import { Control } from "./Control.js";
|
|
4
|
+
//#region src/control/ButtonControl.js
|
|
5
|
+
/** @import {ControlOptions} from './Control.js' */
|
|
6
|
+
/** @import {MaplairMap} from '../map/Map.js' */
|
|
7
|
+
/**
|
|
8
|
+
* A state of a `ButtonControl`, see its `states` option.
|
|
9
|
+
* @typedef {object} ButtonControlState
|
|
10
|
+
* @property {string} name The name of the state, see `setState()`.
|
|
11
|
+
* @property {string|HTMLElement} [content] The content of the button in this state.
|
|
12
|
+
* @property {string} [title] The title of the button in this state.
|
|
13
|
+
* @property {(this: ButtonControl, e: MouseEvent, button: ButtonControl, map: MaplairMap) => void} [onClick]
|
|
14
|
+
* Called when clicking the button in this state.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Options of `ButtonControl`.
|
|
18
|
+
* @typedef {object} ButtonControlOptions
|
|
19
|
+
* @property {string} [position='topleft'] The position of the control (one of the map
|
|
20
|
+
* corners). Possible values are `'topleft'`, `'topright'`, `'bottomleft'` or
|
|
21
|
+
* `'bottomright'`.
|
|
22
|
+
* @property {string|HTMLElement} [content=''] The content of the button: HTML (e.g. an
|
|
23
|
+
* icon or a character) or an element. String content is rendered as HTML; do not pass
|
|
24
|
+
* untrusted input.
|
|
25
|
+
* @property {string} [title=''] The title (tooltip) and accessible name of the button.
|
|
26
|
+
* @property {(this: ButtonControl, e: MouseEvent, button: ButtonControl, map: MaplairMap) => void} [onClick]
|
|
27
|
+
* Called with the click event, the `ButtonControl` and the map when clicking the button.
|
|
28
|
+
* @property {ButtonControlState[]} [states] Alternative states of the button, each an
|
|
29
|
+
* object with `name` and with `content`, `title` and `onClick` like the options. The
|
|
30
|
+
* first state is the initial one, `setState()` switches them.
|
|
31
|
+
* @property {string} [className=''] A custom CSS class name to assign to the button.
|
|
32
|
+
* @property {string} [accessKey=''] A key to activate the button with the browser's
|
|
33
|
+
* [access key](https://developer.mozilla.org/docs/Web/HTML/Reference/Global_attributes/accesskey)
|
|
34
|
+
* modifiers (e.g. Alt+Shift+K), shown in the title.
|
|
35
|
+
* @extends ControlOptions
|
|
36
|
+
*/
|
|
37
|
+
/**
|
|
38
|
+
* A control with a button, styled like the zoom control, calling a function when
|
|
39
|
+
* clicked. It can have several states with their own content, title and function,
|
|
40
|
+
* e.g. to toggle something. Several buttons can be grouped with `ButtonBarControl`.
|
|
41
|
+
*
|
|
42
|
+
* Inspired by the [Leaflet.EasyButton](https://github.com/CliffCloud/Leaflet.EasyButton) plugin.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```js
|
|
46
|
+
* new ButtonControl({
|
|
47
|
+
* content: '⌂',
|
|
48
|
+
* title: 'Home',
|
|
49
|
+
* onClick: (e, button, map) => map.setView([47.27, 11.40], 13)
|
|
50
|
+
* }).addTo(map);
|
|
51
|
+
*
|
|
52
|
+
* new ButtonControl({
|
|
53
|
+
* states: [{
|
|
54
|
+
* name: 'show',
|
|
55
|
+
* content: '👁',
|
|
56
|
+
* title: 'Show the markers',
|
|
57
|
+
* onClick(e, button, map) { markers.addTo(map); button.setState('hide'); }
|
|
58
|
+
* }, {
|
|
59
|
+
* name: 'hide',
|
|
60
|
+
* content: '🙈',
|
|
61
|
+
* title: 'Hide the markers',
|
|
62
|
+
* onClick(e, button) { markers.remove(); button.setState('show'); }
|
|
63
|
+
* }]
|
|
64
|
+
* }).addTo(map);
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
var ButtonControl = class ButtonControl extends Control {
|
|
68
|
+
static {
|
|
69
|
+
this.setDefaultOptions({
|
|
70
|
+
position: "topleft",
|
|
71
|
+
content: "",
|
|
72
|
+
title: "",
|
|
73
|
+
onClick: void 0,
|
|
74
|
+
states: void 0,
|
|
75
|
+
className: "",
|
|
76
|
+
accessKey: ""
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Creates a button control.
|
|
81
|
+
* @param {...ButtonControlOptions} args The options.
|
|
82
|
+
*/
|
|
83
|
+
constructor(...args) {
|
|
84
|
+
super(...args);
|
|
85
|
+
this._state = this.options.states?.[0];
|
|
86
|
+
this.callInitHooks(ButtonControl);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* @internal
|
|
90
|
+
* @returns {HTMLElement}
|
|
91
|
+
*/
|
|
92
|
+
onAdd() {
|
|
93
|
+
const container = create("div", "maplair-bar maplair-control-button");
|
|
94
|
+
this._createButton(container);
|
|
95
|
+
return container;
|
|
96
|
+
}
|
|
97
|
+
_createButton(container) {
|
|
98
|
+
const button = this._button = create("button", `maplair-control-button-button ${this.options.className}`, container);
|
|
99
|
+
button.type = "button";
|
|
100
|
+
button.accessKey = this.options.accessKey;
|
|
101
|
+
disableClickPropagation(button);
|
|
102
|
+
on(button, "click", stop);
|
|
103
|
+
on(button, "click", this._onClick, this);
|
|
104
|
+
on(button, "click", this._refocusOnMap, this);
|
|
105
|
+
this._update();
|
|
106
|
+
return button;
|
|
107
|
+
}
|
|
108
|
+
_onClick(e) {
|
|
109
|
+
if (this._disabled) return;
|
|
110
|
+
(this._state ?? this.options).onClick?.call(this, e, this, this._map);
|
|
111
|
+
}
|
|
112
|
+
_update() {
|
|
113
|
+
if (!this._button) return;
|
|
114
|
+
const { content, title } = this._state ?? this.options, button = this._button;
|
|
115
|
+
if (typeof content === "string") button.innerHTML = content;
|
|
116
|
+
else button.replaceChildren(content);
|
|
117
|
+
const key = button.accessKeyLabel || this.options.accessKey.toUpperCase();
|
|
118
|
+
button.title = [title, key && `[${key}]`].filter(Boolean).join(" ");
|
|
119
|
+
button.setAttribute("aria-label", title ?? "");
|
|
120
|
+
if (this._state) button.dataset.state = this._state.name;
|
|
121
|
+
button.classList.toggle("maplair-disabled", !!this._disabled);
|
|
122
|
+
button.setAttribute("aria-disabled", !!this._disabled);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Switches to the state with the given `name`, see the `states` option.
|
|
126
|
+
* @param {string} name
|
|
127
|
+
* @returns {this}
|
|
128
|
+
*/
|
|
129
|
+
setState(name) {
|
|
130
|
+
const state = this.options.states?.find((s) => s.name === name);
|
|
131
|
+
if (!state) throw new Error(`Unknown button state "${name}".`);
|
|
132
|
+
this._state = state;
|
|
133
|
+
this._update();
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Returns the name of the current state, see the `states` option.
|
|
138
|
+
* @returns {string|undefined}
|
|
139
|
+
*/
|
|
140
|
+
getState() {
|
|
141
|
+
return this._state?.name;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Disables the button, so that clicking it does nothing.
|
|
145
|
+
* @returns {this}
|
|
146
|
+
*/
|
|
147
|
+
disable() {
|
|
148
|
+
this._disabled = true;
|
|
149
|
+
this._update();
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Enables the button again.
|
|
154
|
+
* @returns {this}
|
|
155
|
+
*/
|
|
156
|
+
enable() {
|
|
157
|
+
this._disabled = false;
|
|
158
|
+
this._update();
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Returns the button element.
|
|
163
|
+
* @returns {HTMLElement}
|
|
164
|
+
*/
|
|
165
|
+
getButton() {
|
|
166
|
+
return this._button;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Groups several `ButtonControl`s in one bar, like the buttons of the zoom control.
|
|
171
|
+
*
|
|
172
|
+
* Inspired by the [Leaflet.EasyButton](https://github.com/CliffCloud/Leaflet.EasyButton) plugin.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```js
|
|
176
|
+
* new ButtonBarControl([
|
|
177
|
+
* new ButtonControl({content: 'A', title: 'First', onClick: () => {}}),
|
|
178
|
+
* new ButtonControl({content: 'B', title: 'Second', onClick: () => {}})
|
|
179
|
+
* ]).addTo(map);
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
var ButtonBarControl = class ButtonBarControl extends Control {
|
|
183
|
+
/**
|
|
184
|
+
* Creates a bar with the given buttons.
|
|
185
|
+
* @param {ButtonControl[]} buttons
|
|
186
|
+
* @param {ControlOptions} [options]
|
|
187
|
+
*/
|
|
188
|
+
constructor(buttons, options) {
|
|
189
|
+
super(options);
|
|
190
|
+
this._buttons = buttons;
|
|
191
|
+
this.callInitHooks(ButtonBarControl);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* @internal
|
|
195
|
+
* @param {MaplairMap} map
|
|
196
|
+
* @returns {HTMLElement}
|
|
197
|
+
*/
|
|
198
|
+
onAdd(map) {
|
|
199
|
+
const container = create("div", "maplair-bar maplair-control-button-bar");
|
|
200
|
+
for (const button of this._buttons) {
|
|
201
|
+
button._map = map;
|
|
202
|
+
button._createButton(container);
|
|
203
|
+
}
|
|
204
|
+
return container;
|
|
205
|
+
}
|
|
206
|
+
/** @internal */
|
|
207
|
+
onRemove() {
|
|
208
|
+
for (const button of this._buttons) button._map = null;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Returns the buttons of the bar.
|
|
212
|
+
* @returns {ButtonControl[]}
|
|
213
|
+
*/
|
|
214
|
+
getButtons() {
|
|
215
|
+
return this._buttons;
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
//#endregion
|
|
219
|
+
export { ButtonBarControl, ButtonControl };
|
|
220
|
+
|
|
221
|
+
//# sourceMappingURL=ButtonControl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ButtonControl.js","names":["DomUtil.create","DomEvent.stop"],"sources":["../../../src/control/ButtonControl.js"],"sourcesContent":["import {Control} from './Control.js';\nimport * as DomUtil from '../dom/DomUtil.js';\nimport * as DomEvent from '../dom/DomEvent.js';\n/** @import {ControlOptions} from './Control.js' */\n/** @import {MaplairMap} from '../map/Map.js' */\n\n/**\n * A state of a `ButtonControl`, see its `states` option.\n * @typedef {object} ButtonControlState\n * @property {string} name The name of the state, see `setState()`.\n * @property {string|HTMLElement} [content] The content of the button in this state.\n * @property {string} [title] The title of the button in this state.\n * @property {(this: ButtonControl, e: MouseEvent, button: ButtonControl, map: MaplairMap) => void} [onClick]\n * Called when clicking the button in this state.\n */\n\n/**\n * Options of `ButtonControl`.\n * @typedef {object} ButtonControlOptions\n * @property {string} [position='topleft'] The position of the control (one of the map\n * corners). Possible values are `'topleft'`, `'topright'`, `'bottomleft'` or\n * `'bottomright'`.\n * @property {string|HTMLElement} [content=''] The content of the button: HTML (e.g. an\n * icon or a character) or an element. String content is rendered as HTML; do not pass\n * untrusted input.\n * @property {string} [title=''] The title (tooltip) and accessible name of the button.\n * @property {(this: ButtonControl, e: MouseEvent, button: ButtonControl, map: MaplairMap) => void} [onClick]\n * Called with the click event, the `ButtonControl` and the map when clicking the button.\n * @property {ButtonControlState[]} [states] Alternative states of the button, each an\n * object with `name` and with `content`, `title` and `onClick` like the options. The\n * first state is the initial one, `setState()` switches them.\n * @property {string} [className=''] A custom CSS class name to assign to the button.\n * @property {string} [accessKey=''] A key to activate the button with the browser's\n * [access key](https://developer.mozilla.org/docs/Web/HTML/Reference/Global_attributes/accesskey)\n * modifiers (e.g. Alt+Shift+K), shown in the title.\n * @extends ControlOptions\n */\n\n/**\n * A control with a button, styled like the zoom control, calling a function when\n * clicked. It can have several states with their own content, title and function,\n * e.g. to toggle something. Several buttons can be grouped with `ButtonBarControl`.\n *\n * Inspired by the [Leaflet.EasyButton](https://github.com/CliffCloud/Leaflet.EasyButton) plugin.\n *\n * @example\n * ```js\n * new ButtonControl({\n * \tcontent: '⌂',\n * \ttitle: 'Home',\n * \tonClick: (e, button, map) => map.setView([47.27, 11.40], 13)\n * }).addTo(map);\n *\n * new ButtonControl({\n * \tstates: [{\n * \t\tname: 'show',\n * \t\tcontent: '👁',\n * \t\ttitle: 'Show the markers',\n * \t\tonClick(e, button, map) { markers.addTo(map); button.setState('hide'); }\n * \t}, {\n * \t\tname: 'hide',\n * \t\tcontent: '🙈',\n * \t\ttitle: 'Hide the markers',\n * \t\tonClick(e, button) { markers.remove(); button.setState('show'); }\n * \t}]\n * }).addTo(map);\n * ```\n */\nexport class ButtonControl extends Control {\n\n\tstatic {\n\t\tthis.setDefaultOptions({\n\t\t\tposition: 'topleft',\n\t\t\tcontent: '',\n\t\t\ttitle: '',\n\t\t\tonClick: undefined,\n\t\t\tstates: undefined,\n\t\t\tclassName: '',\n\t\t\taccessKey: ''\n\t\t});\n\t}\n\n\t/**\n\t * Creates a button control.\n\t * @param {...ButtonControlOptions} args The options.\n\t */\n\tconstructor(...args) {\n\t\tsuper(...args);\n\t\tthis._state = this.options.states?.[0];\n\t\tthis.callInitHooks(ButtonControl);\n\t}\n\n\t/**\n\t * @internal\n\t * @returns {HTMLElement}\n\t */\n\tonAdd() {\n\t\tconst container = DomUtil.create('div', 'maplair-bar maplair-control-button');\n\t\tthis._createButton(container);\n\t\treturn container;\n\t}\n\n\t// Creates the button element in `container`, see `ButtonBarControl`\n\t_createButton(container) {\n\t\tconst button = this._button = DomUtil.create('button', `maplair-control-button-button ${this.options.className}`, container);\n\t\tbutton.type = 'button';\n\t\tbutton.accessKey = this.options.accessKey;\n\n\t\tDomEvent.disableClickPropagation(button);\n\t\tDomEvent.on(button, 'click', DomEvent.stop);\n\t\tDomEvent.on(button, 'click', this._onClick, this);\n\t\tDomEvent.on(button, 'click', this._refocusOnMap, this);\n\n\t\tthis._update();\n\t\treturn button;\n\t}\n\n\t_onClick(e) {\n\t\tif (this._disabled) { return; }\n\t\t(this._state ?? this.options).onClick?.call(this, e, this, this._map);\n\t}\n\n\t_update() {\n\t\tif (!this._button) { return; }\n\t\tconst {content, title} = this._state ?? this.options,\n\t\tbutton = this._button;\n\n\t\tif (typeof content === 'string') {\n\t\t\tbutton.innerHTML = content;\n\t\t} else {\n\t\t\tbutton.replaceChildren(content);\n\t\t}\n\t\tconst key = button.accessKeyLabel || this.options.accessKey.toUpperCase();\n\t\tbutton.title = [title, key && `[${key}]`].filter(Boolean).join(' ');\n\t\tbutton.setAttribute('aria-label', title ?? '');\n\t\tif (this._state) {\n\t\t\tbutton.dataset.state = this._state.name;\n\t\t}\n\t\tbutton.classList.toggle('maplair-disabled', !!this._disabled);\n\t\tbutton.setAttribute('aria-disabled', !!this._disabled);\n\t}\n\n\t/**\n\t * Switches to the state with the given `name`, see the `states` option.\n\t * @param {string} name\n\t * @returns {this}\n\t */\n\tsetState(name) {\n\t\tconst state = this.options.states?.find(s => s.name === name);\n\t\tif (!state) {\n\t\t\tthrow new Error(`Unknown button state \"${name}\".`);\n\t\t}\n\t\tthis._state = state;\n\t\tthis._update();\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns the name of the current state, see the `states` option.\n\t * @returns {string|undefined}\n\t */\n\tgetState() {\n\t\treturn this._state?.name;\n\t}\n\n\t/**\n\t * Disables the button, so that clicking it does nothing.\n\t * @returns {this}\n\t */\n\tdisable() {\n\t\tthis._disabled = true;\n\t\tthis._update();\n\t\treturn this;\n\t}\n\n\t/**\n\t * Enables the button again.\n\t * @returns {this}\n\t */\n\tenable() {\n\t\tthis._disabled = false;\n\t\tthis._update();\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns the button element.\n\t * @returns {HTMLElement}\n\t */\n\tgetButton() {\n\t\treturn this._button;\n\t}\n}\n\n/**\n * Groups several `ButtonControl`s in one bar, like the buttons of the zoom control.\n *\n * Inspired by the [Leaflet.EasyButton](https://github.com/CliffCloud/Leaflet.EasyButton) plugin.\n *\n * @example\n * ```js\n * new ButtonBarControl([\n * \tnew ButtonControl({content: 'A', title: 'First', onClick: () => {}}),\n * \tnew ButtonControl({content: 'B', title: 'Second', onClick: () => {}})\n * ]).addTo(map);\n * ```\n */\nexport class ButtonBarControl extends Control {\n\t/**\n\t * Creates a bar with the given buttons.\n\t * @param {ButtonControl[]} buttons\n\t * @param {ControlOptions} [options]\n\t */\n\tconstructor(buttons, options) {\n\t\tsuper(options);\n\t\tthis._buttons = buttons;\n\t\tthis.callInitHooks(ButtonBarControl);\n\t}\n\n\t/**\n\t * @internal\n\t * @param {MaplairMap} map\n\t * @returns {HTMLElement}\n\t */\n\tonAdd(map) {\n\t\tconst container = DomUtil.create('div', 'maplair-bar maplair-control-button-bar');\n\t\tfor (const button of this._buttons) {\n\t\t\tbutton._map = map;\n\t\t\tbutton._createButton(container);\n\t\t}\n\t\treturn container;\n\t}\n\n\t/** @internal */\n\tonRemove() {\n\t\tfor (const button of this._buttons) {\n\t\t\tbutton._map = null;\n\t\t}\n\t}\n\n\t/**\n\t * Returns the buttons of the bar.\n\t * @returns {ButtonControl[]}\n\t */\n\tgetButtons() {\n\t\treturn this._buttons;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEA,IAAa,gBAAb,MAAa,sBAAsB,QAAQ;CAE1C;EACC,KAAK,kBAAkB;GACtB,UAAU;GACV,SAAS;GACT,OAAO;GACP,SAAS,KAAA;GACT,QAAQ,KAAA;GACR,WAAW;GACX,WAAW;EACZ,CAAC;CACF;;;;;CAMA,YAAY,GAAG,MAAM;EACpB,MAAM,GAAG,IAAI;EACb,KAAK,SAAS,KAAK,QAAQ,SAAS;EACpC,KAAK,cAAc,aAAa;CACjC;;;;;CAMA,QAAQ;EACP,MAAM,YAAYA,OAAe,OAAO,oCAAoC;EAC5E,KAAK,cAAc,SAAS;EAC5B,OAAO;CACR;CAGA,cAAc,WAAW;EACxB,MAAM,SAAS,KAAK,UAAUA,OAAe,UAAU,iCAAiC,KAAK,QAAQ,aAAa,SAAS;EAC3H,OAAO,OAAO;EACd,OAAO,YAAY,KAAK,QAAQ;EAEhC,wBAAiC,MAAM;EACvC,GAAY,QAAQ,SAASC,IAAa;EAC1C,GAAY,QAAQ,SAAS,KAAK,UAAU,IAAI;EAChD,GAAY,QAAQ,SAAS,KAAK,eAAe,IAAI;EAErD,KAAK,QAAQ;EACb,OAAO;CACR;CAEA,SAAS,GAAG;EACX,IAAI,KAAK,WAAa;EACtB,CAAC,KAAK,UAAU,KAAK,QAAA,CAAS,SAAS,KAAK,MAAM,GAAG,MAAM,KAAK,IAAI;CACrE;CAEA,UAAU;EACT,IAAI,CAAC,KAAK,SAAW;EACrB,MAAM,EAAC,SAAS,UAAS,KAAK,UAAU,KAAK,SAC7C,SAAS,KAAK;EAEd,IAAI,OAAO,YAAY,UACtB,OAAO,YAAY;OAEnB,OAAO,gBAAgB,OAAO;EAE/B,MAAM,MAAM,OAAO,kBAAkB,KAAK,QAAQ,UAAU,YAAY;EACxE,OAAO,QAAQ,CAAC,OAAO,OAAO,IAAI,IAAI,EAAE,CAAC,CAAC,OAAO,OAAO,CAAC,CAAC,KAAK,GAAG;EAClE,OAAO,aAAa,cAAc,SAAS,EAAE;EAC7C,IAAI,KAAK,QACR,OAAO,QAAQ,QAAQ,KAAK,OAAO;EAEpC,OAAO,UAAU,OAAO,oBAAoB,CAAC,CAAC,KAAK,SAAS;EAC5D,OAAO,aAAa,iBAAiB,CAAC,CAAC,KAAK,SAAS;CACtD;;;;;;CAOA,SAAS,MAAM;EACd,MAAM,QAAQ,KAAK,QAAQ,QAAQ,MAAK,MAAK,EAAE,SAAS,IAAI;EAC5D,IAAI,CAAC,OACJ,MAAM,IAAI,MAAM,yBAAyB,KAAK,GAAG;EAElD,KAAK,SAAS;EACd,KAAK,QAAQ;EACb,OAAO;CACR;;;;;CAMA,WAAW;EACV,OAAO,KAAK,QAAQ;CACrB;;;;;CAMA,UAAU;EACT,KAAK,YAAY;EACjB,KAAK,QAAQ;EACb,OAAO;CACR;;;;;CAMA,SAAS;EACR,KAAK,YAAY;EACjB,KAAK,QAAQ;EACb,OAAO;CACR;;;;;CAMA,YAAY;EACX,OAAO,KAAK;CACb;AACD;;;;;;;;;;;;;;AAeA,IAAa,mBAAb,MAAa,yBAAyB,QAAQ;;;;;;CAM7C,YAAY,SAAS,SAAS;EAC7B,MAAM,OAAO;EACb,KAAK,WAAW;EAChB,KAAK,cAAc,gBAAgB;CACpC;;;;;;CAOA,MAAM,KAAK;EACV,MAAM,YAAYD,OAAe,OAAO,wCAAwC;EAChF,KAAK,MAAM,UAAU,KAAK,UAAU;GACnC,OAAO,OAAO;GACd,OAAO,cAAc,SAAS;EAC/B;EACA,OAAO;CACR;;CAGA,WAAW;EACV,KAAK,MAAM,UAAU,KAAK,UACzB,OAAO,OAAO;CAEhB;;;;;CAMA,aAAa;EACZ,OAAO,KAAK;CACb;AACD"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { setOptions } from "../core/Util.js";
|
|
2
|
+
import { Class } from "../core/Class.js";
|
|
3
|
+
import { disableClickPropagation, disableScrollPropagation, on, stop } from "../dom/DomEvent.js";
|
|
4
|
+
import { create } from "../dom/DomUtil.js";
|
|
5
|
+
import { Layer } from "../layer/Layer.js";
|
|
6
|
+
//#region src/control/ContextMenu.js
|
|
7
|
+
/** @import {MaplairMap} from '../map/Map.js' */
|
|
8
|
+
/** @import {LatLng} from '../geo/LatLng.js' */
|
|
9
|
+
/** @import {Point} from '../geometry/Point.js' */
|
|
10
|
+
/**
|
|
11
|
+
* Options of `ContextMenu`.
|
|
12
|
+
* @typedef {object} ContextMenuOptions
|
|
13
|
+
* @property {string} [className=''] A custom CSS class name to assign to the menu.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* The argument of the `callback` of a `ContextMenu` item, with the location of the
|
|
17
|
+
* `contextmenu` event.
|
|
18
|
+
* @typedef {object} ContextMenuEvent
|
|
19
|
+
* @property {LatLng} latlng The geographical point of the event.
|
|
20
|
+
* @property {Point} containerPoint Pixel coordinates of the event relative to the map
|
|
21
|
+
* container.
|
|
22
|
+
* @property {Point} layerPoint Pixel coordinates of the event relative to the map layer.
|
|
23
|
+
* @property {MouseEvent|PointerEvent} originalEvent The original DOM event.
|
|
24
|
+
* @property {Layer} [relatedTarget] The layer, for menus of layers.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* An item of a `ContextMenu`.
|
|
28
|
+
* @typedef {object} ContextMenuItem
|
|
29
|
+
* @property {string} text The text of the item.
|
|
30
|
+
* @property {(this: ContextMenu, event: ContextMenuEvent) => void} [callback] Called
|
|
31
|
+
* when the item is chosen.
|
|
32
|
+
* @property {boolean} [disabled] Whether the item is disabled.
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* A menu opened with the `contextmenu` event (right click, or long tap on touch
|
|
36
|
+
* screens) on the map or on a layer. Each item calls a function with the location
|
|
37
|
+
* of the event. The menu is a [popover](https://developer.mozilla.org/docs/Web/API/Popover_API),
|
|
38
|
+
* closed by clicking outside of it or pressing Escape, and can be used with the
|
|
39
|
+
* keyboard (arrow keys, Enter).
|
|
40
|
+
*
|
|
41
|
+
* Inspired by the [Leaflet.contextmenu](https://github.com/aratcliffe/Leaflet.contextmenu) plugin.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```js
|
|
45
|
+
* new ContextMenu([
|
|
46
|
+
* {text: 'Center the map here', callback: e => map.panTo(e.latlng)},
|
|
47
|
+
* '-',
|
|
48
|
+
* {text: 'Zoom in', callback: () => map.zoomIn()}
|
|
49
|
+
* ]).addTo(map);
|
|
50
|
+
*
|
|
51
|
+
* new ContextMenu([
|
|
52
|
+
* {text: 'Remove', callback: e => e.relatedTarget.remove()}
|
|
53
|
+
* ]).addTo(marker);
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
var ContextMenu = class ContextMenu extends Class {
|
|
57
|
+
static {
|
|
58
|
+
this.setDefaultOptions({ className: "" });
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates a context menu. Each item is an object with `text`, `callback` and the
|
|
62
|
+
* optional `disabled`, or `'-'` for a separator. The `callback` gets an event with
|
|
63
|
+
* `latlng`, `containerPoint`, `layerPoint`, `originalEvent` and `relatedTarget`
|
|
64
|
+
* (the layer, for menus of layers).
|
|
65
|
+
* @param {(ContextMenuItem|'-')[]} items
|
|
66
|
+
* @param {ContextMenuOptions} [options]
|
|
67
|
+
*/
|
|
68
|
+
constructor(items, options) {
|
|
69
|
+
super();
|
|
70
|
+
setOptions(this, options);
|
|
71
|
+
this._items = items;
|
|
72
|
+
this.callInitHooks(ContextMenu);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Opens the menu on `contextmenu` events of the map or the layer.
|
|
76
|
+
* @param {MaplairMap|Layer} target
|
|
77
|
+
* @returns {this}
|
|
78
|
+
*/
|
|
79
|
+
addTo(target) {
|
|
80
|
+
this.remove();
|
|
81
|
+
this._target = target;
|
|
82
|
+
target.on("contextmenu", this._open, this);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Removes the menu from its map or layer.
|
|
87
|
+
* @returns {this}
|
|
88
|
+
*/
|
|
89
|
+
remove() {
|
|
90
|
+
this.close();
|
|
91
|
+
this._target?.off("contextmenu", this._open, this);
|
|
92
|
+
this._target = null;
|
|
93
|
+
this._container?.remove();
|
|
94
|
+
this._container = null;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Closes the menu.
|
|
99
|
+
* @returns {this}
|
|
100
|
+
*/
|
|
101
|
+
close() {
|
|
102
|
+
if (this._container?.matches(":popover-open")) this._container.hidePopover();
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Returns `true` if the menu is open.
|
|
107
|
+
* @returns {boolean}
|
|
108
|
+
*/
|
|
109
|
+
isOpen() {
|
|
110
|
+
return !!this._container?.matches(":popover-open");
|
|
111
|
+
}
|
|
112
|
+
_open(e) {
|
|
113
|
+
stop(e);
|
|
114
|
+
const map = this._target instanceof Layer ? this._target._map : this._target;
|
|
115
|
+
if (!map) return;
|
|
116
|
+
this._map = map;
|
|
117
|
+
this._event = {
|
|
118
|
+
latlng: e.latlng,
|
|
119
|
+
containerPoint: e.containerPoint,
|
|
120
|
+
layerPoint: e.layerPoint,
|
|
121
|
+
originalEvent: e.originalEvent,
|
|
122
|
+
relatedTarget: this._target instanceof Layer ? this._target : void 0
|
|
123
|
+
};
|
|
124
|
+
const container = this._createMenu(map._container);
|
|
125
|
+
if (!container.matches(":popover-open")) container.showPopover();
|
|
126
|
+
this._position(e.originalEvent);
|
|
127
|
+
container.querySelector("button:not([disabled])")?.focus({ preventScroll: true });
|
|
128
|
+
map.once("movestart", this.close, this);
|
|
129
|
+
}
|
|
130
|
+
_createMenu(parent) {
|
|
131
|
+
const container = this._container ??= create("div", `maplair-contextmenu ${this.options.className}`);
|
|
132
|
+
if (container.parentNode !== parent) parent.appendChild(container);
|
|
133
|
+
container.popover = "auto";
|
|
134
|
+
container.setAttribute("role", "menu");
|
|
135
|
+
container.replaceChildren();
|
|
136
|
+
for (const item of this._items) {
|
|
137
|
+
if (item === "-") {
|
|
138
|
+
create("div", "maplair-contextmenu-separator", container).setAttribute("role", "separator");
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
const button = create("button", "maplair-contextmenu-item", container);
|
|
142
|
+
button.type = "button";
|
|
143
|
+
button.setAttribute("role", "menuitem");
|
|
144
|
+
button.textContent = item.text;
|
|
145
|
+
button.disabled = !!item.disabled;
|
|
146
|
+
on(button, "click", (ev) => {
|
|
147
|
+
stop(ev);
|
|
148
|
+
this.close();
|
|
149
|
+
item.callback?.call(this, this._event);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
on(container, "keydown", this._onKeyDown, this);
|
|
153
|
+
disableClickPropagation(container);
|
|
154
|
+
disableScrollPropagation(container);
|
|
155
|
+
on(container, "contextmenu", stop);
|
|
156
|
+
return container;
|
|
157
|
+
}
|
|
158
|
+
_position(e) {
|
|
159
|
+
const container = this._container, viewport = document.documentElement, x = Math.min(e.clientX, viewport.clientWidth - container.offsetWidth), y = Math.min(e.clientY, viewport.clientHeight - container.offsetHeight);
|
|
160
|
+
container.style.left = `${Math.max(0, x)}px`;
|
|
161
|
+
container.style.top = `${Math.max(0, y)}px`;
|
|
162
|
+
}
|
|
163
|
+
_onKeyDown(e) {
|
|
164
|
+
if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;
|
|
165
|
+
stop(e);
|
|
166
|
+
const buttons = [...this._container.querySelectorAll("button:not([disabled])")], index = buttons.indexOf(document.activeElement), next = e.key === "ArrowDown" ? index + 1 : index - 1;
|
|
167
|
+
buttons.at(next % buttons.length)?.focus();
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
//#endregion
|
|
171
|
+
export { ContextMenu };
|
|
172
|
+
|
|
173
|
+
//# sourceMappingURL=ContextMenu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContextMenu.js","names":["DomUtil.create","DomEvent.stop"],"sources":["../../../src/control/ContextMenu.js"],"sourcesContent":["import {Class} from '../core/Class.js';\nimport * as Util from '../core/Util.js';\nimport * as DomUtil from '../dom/DomUtil.js';\nimport * as DomEvent from '../dom/DomEvent.js';\nimport {Layer} from '../layer/Layer.js';\n/** @import {MaplairMap} from '../map/Map.js' */\n/** @import {LatLng} from '../geo/LatLng.js' */\n/** @import {Point} from '../geometry/Point.js' */\n\n/**\n * Options of `ContextMenu`.\n * @typedef {object} ContextMenuOptions\n * @property {string} [className=''] A custom CSS class name to assign to the menu.\n */\n\n/**\n * The argument of the `callback` of a `ContextMenu` item, with the location of the\n * `contextmenu` event.\n * @typedef {object} ContextMenuEvent\n * @property {LatLng} latlng The geographical point of the event.\n * @property {Point} containerPoint Pixel coordinates of the event relative to the map\n * container.\n * @property {Point} layerPoint Pixel coordinates of the event relative to the map layer.\n * @property {MouseEvent|PointerEvent} originalEvent The original DOM event.\n * @property {Layer} [relatedTarget] The layer, for menus of layers.\n */\n\n/**\n * An item of a `ContextMenu`.\n * @typedef {object} ContextMenuItem\n * @property {string} text The text of the item.\n * @property {(this: ContextMenu, event: ContextMenuEvent) => void} [callback] Called\n * when the item is chosen.\n * @property {boolean} [disabled] Whether the item is disabled.\n */\n\n/**\n * A menu opened with the `contextmenu` event (right click, or long tap on touch\n * screens) on the map or on a layer. Each item calls a function with the location\n * of the event. The menu is a [popover](https://developer.mozilla.org/docs/Web/API/Popover_API),\n * closed by clicking outside of it or pressing Escape, and can be used with the\n * keyboard (arrow keys, Enter).\n *\n * Inspired by the [Leaflet.contextmenu](https://github.com/aratcliffe/Leaflet.contextmenu) plugin.\n *\n * @example\n * ```js\n * new ContextMenu([\n * \t{text: 'Center the map here', callback: e => map.panTo(e.latlng)},\n * \t'-',\n * \t{text: 'Zoom in', callback: () => map.zoomIn()}\n * ]).addTo(map);\n *\n * new ContextMenu([\n * \t{text: 'Remove', callback: e => e.relatedTarget.remove()}\n * ]).addTo(marker);\n * ```\n */\nexport class ContextMenu extends Class {\n\n\tstatic {\n\t\tthis.setDefaultOptions({\n\t\t\tclassName: ''\n\t\t});\n\t}\n\n\t/**\n\t * Creates a context menu. Each item is an object with `text`, `callback` and the\n\t * optional `disabled`, or `'-'` for a separator. The `callback` gets an event with\n\t * `latlng`, `containerPoint`, `layerPoint`, `originalEvent` and `relatedTarget`\n\t * (the layer, for menus of layers).\n\t * @param {(ContextMenuItem|'-')[]} items\n\t * @param {ContextMenuOptions} [options]\n\t */\n\tconstructor(items, options) {\n\t\tsuper();\n\t\tUtil.setOptions(this, options);\n\t\tthis._items = items;\n\t\tthis.callInitHooks(ContextMenu);\n\t}\n\n\t/**\n\t * Opens the menu on `contextmenu` events of the map or the layer.\n\t * @param {MaplairMap|Layer} target\n\t * @returns {this}\n\t */\n\taddTo(target) {\n\t\tthis.remove();\n\t\tthis._target = target;\n\t\ttarget.on('contextmenu', this._open, this);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Removes the menu from its map or layer.\n\t * @returns {this}\n\t */\n\tremove() {\n\t\tthis.close();\n\t\tthis._target?.off('contextmenu', this._open, this);\n\t\tthis._target = null;\n\t\tthis._container?.remove();\n\t\tthis._container = null;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Closes the menu.\n\t * @returns {this}\n\t */\n\tclose() {\n\t\tif (this._container?.matches(':popover-open')) {\n\t\t\tthis._container.hidePopover();\n\t\t}\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns `true` if the menu is open.\n\t * @returns {boolean}\n\t */\n\tisOpen() {\n\t\treturn !!this._container?.matches(':popover-open');\n\t}\n\n\t_open(e) {\n\t\t// a menu of a layer replaces the one of the map\n\t\tDomEvent.stop(e);\n\t\tconst map = this._target instanceof Layer ? this._target._map : this._target;\n\t\tif (!map) { return; }\n\n\t\tthis._map = map;\n\t\tthis._event = {\n\t\t\tlatlng: e.latlng,\n\t\t\tcontainerPoint: e.containerPoint,\n\t\t\tlayerPoint: e.layerPoint,\n\t\t\toriginalEvent: e.originalEvent,\n\t\t\trelatedTarget: this._target instanceof Layer ? this._target : undefined\n\t\t};\n\n\t\tconst container = this._createMenu(map._container);\n\t\tif (!container.matches(':popover-open')) {\n\t\t\tcontainer.showPopover();\n\t\t}\n\t\tthis._position(e.originalEvent);\n\t\tcontainer.querySelector('button:not([disabled])')?.focus({preventScroll: true});\n\t\tmap.once('movestart', this.close, this);\n\t}\n\n\t_createMenu(parent) {\n\t\tconst container = this._container ??= DomUtil.create('div', `maplair-contextmenu ${this.options.className}`);\n\t\tif (container.parentNode !== parent) {\n\t\t\tparent.appendChild(container);\n\t\t}\n\t\tcontainer.popover = 'auto';\n\t\tcontainer.setAttribute('role', 'menu');\n\t\tcontainer.replaceChildren();\n\n\t\tfor (const item of this._items) {\n\t\t\tif (item === '-') {\n\t\t\t\tDomUtil.create('div', 'maplair-contextmenu-separator', container).setAttribute('role', 'separator');\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst button = DomUtil.create('button', 'maplair-contextmenu-item', container);\n\t\t\tbutton.type = 'button';\n\t\t\tbutton.setAttribute('role', 'menuitem');\n\t\t\tbutton.textContent = item.text;\n\t\t\tbutton.disabled = !!item.disabled;\n\t\t\tDomEvent.on(button, 'click', (ev) => {\n\t\t\t\tDomEvent.stop(ev);\n\t\t\t\tthis.close();\n\t\t\t\titem.callback?.call(this, this._event);\n\t\t\t});\n\t\t}\n\n\t\tDomEvent.on(container, 'keydown', this._onKeyDown, this);\n\t\tDomEvent.disableClickPropagation(container);\n\t\tDomEvent.disableScrollPropagation(container);\n\t\tDomEvent.on(container, 'contextmenu', DomEvent.stop);\n\t\treturn container;\n\t}\n\n\t// At the pointer, within the viewport\n\t_position(e) {\n\t\tconst container = this._container,\n\t\tviewport = document.documentElement,\n\t\tx = Math.min(e.clientX, viewport.clientWidth - container.offsetWidth),\n\t\ty = Math.min(e.clientY, viewport.clientHeight - container.offsetHeight);\n\t\tcontainer.style.left = `${Math.max(0, x)}px`;\n\t\tcontainer.style.top = `${Math.max(0, y)}px`;\n\t}\n\n\t_onKeyDown(e) {\n\t\tif (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') { return; }\n\t\tDomEvent.stop(e);\n\t\tconst buttons = [...this._container.querySelectorAll('button:not([disabled])')],\n\t\tindex = buttons.indexOf(document.activeElement),\n\t\tnext = e.key === 'ArrowDown' ? index + 1 : index - 1;\n\t\tbuttons.at(next % buttons.length)?.focus();\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0DA,IAAa,cAAb,MAAa,oBAAoB,MAAM;CAEtC;EACC,KAAK,kBAAkB,EACtB,WAAW,GACZ,CAAC;CACF;;;;;;;;;CAUA,YAAY,OAAO,SAAS;EAC3B,MAAM;EACN,WAAgB,MAAM,OAAO;EAC7B,KAAK,SAAS;EACd,KAAK,cAAc,WAAW;CAC/B;;;;;;CAOA,MAAM,QAAQ;EACb,KAAK,OAAO;EACZ,KAAK,UAAU;EACf,OAAO,GAAG,eAAe,KAAK,OAAO,IAAI;EACzC,OAAO;CACR;;;;;CAMA,SAAS;EACR,KAAK,MAAM;EACX,KAAK,SAAS,IAAI,eAAe,KAAK,OAAO,IAAI;EACjD,KAAK,UAAU;EACf,KAAK,YAAY,OAAO;EACxB,KAAK,aAAa;EAClB,OAAO;CACR;;;;;CAMA,QAAQ;EACP,IAAI,KAAK,YAAY,QAAQ,eAAe,GAC3C,KAAK,WAAW,YAAY;EAE7B,OAAO;CACR;;;;;CAMA,SAAS;EACR,OAAO,CAAC,CAAC,KAAK,YAAY,QAAQ,eAAe;CAClD;CAEA,MAAM,GAAG;EAER,KAAc,CAAC;EACf,MAAM,MAAM,KAAK,mBAAmB,QAAQ,KAAK,QAAQ,OAAO,KAAK;EACrE,IAAI,CAAC,KAAO;EAEZ,KAAK,OAAO;EACZ,KAAK,SAAS;GACb,QAAQ,EAAE;GACV,gBAAgB,EAAE;GAClB,YAAY,EAAE;GACd,eAAe,EAAE;GACjB,eAAe,KAAK,mBAAmB,QAAQ,KAAK,UAAU,KAAA;EAC/D;EAEA,MAAM,YAAY,KAAK,YAAY,IAAI,UAAU;EACjD,IAAI,CAAC,UAAU,QAAQ,eAAe,GACrC,UAAU,YAAY;EAEvB,KAAK,UAAU,EAAE,aAAa;EAC9B,UAAU,cAAc,wBAAwB,CAAC,EAAE,MAAM,EAAC,eAAe,KAAI,CAAC;EAC9E,IAAI,KAAK,aAAa,KAAK,OAAO,IAAI;CACvC;CAEA,YAAY,QAAQ;EACnB,MAAM,YAAY,KAAK,eAAeA,OAAe,OAAO,uBAAuB,KAAK,QAAQ,WAAW;EAC3G,IAAI,UAAU,eAAe,QAC5B,OAAO,YAAY,SAAS;EAE7B,UAAU,UAAU;EACpB,UAAU,aAAa,QAAQ,MAAM;EACrC,UAAU,gBAAgB;EAE1B,KAAK,MAAM,QAAQ,KAAK,QAAQ;GAC/B,IAAI,SAAS,KAAK;IACjB,OAAe,OAAO,iCAAiC,SAAS,CAAC,CAAC,aAAa,QAAQ,WAAW;IAClG;GACD;GACA,MAAM,SAASA,OAAe,UAAU,4BAA4B,SAAS;GAC7E,OAAO,OAAO;GACd,OAAO,aAAa,QAAQ,UAAU;GACtC,OAAO,cAAc,KAAK;GAC1B,OAAO,WAAW,CAAC,CAAC,KAAK;GACzB,GAAY,QAAQ,UAAU,OAAO;IACpC,KAAc,EAAE;IAChB,KAAK,MAAM;IACX,KAAK,UAAU,KAAK,MAAM,KAAK,MAAM;GACtC,CAAC;EACF;EAEA,GAAY,WAAW,WAAW,KAAK,YAAY,IAAI;EACvD,wBAAiC,SAAS;EAC1C,yBAAkC,SAAS;EAC3C,GAAY,WAAW,eAAeC,IAAa;EACnD,OAAO;CACR;CAGA,UAAU,GAAG;EACZ,MAAM,YAAY,KAAK,YACvB,WAAW,SAAS,iBACpB,IAAI,KAAK,IAAI,EAAE,SAAS,SAAS,cAAc,UAAU,WAAW,GACpE,IAAI,KAAK,IAAI,EAAE,SAAS,SAAS,eAAe,UAAU,YAAY;EACtE,UAAU,MAAM,OAAO,GAAG,KAAK,IAAI,GAAG,CAAC,EAAE;EACzC,UAAU,MAAM,MAAM,GAAG,KAAK,IAAI,GAAG,CAAC,EAAE;CACzC;CAEA,WAAW,GAAG;EACb,IAAI,EAAE,QAAQ,eAAe,EAAE,QAAQ,WAAa;EACpD,KAAc,CAAC;EACf,MAAM,UAAU,CAAC,GAAG,KAAK,WAAW,iBAAiB,wBAAwB,CAAC,GAC9E,QAAQ,QAAQ,QAAQ,SAAS,aAAa,GAC9C,OAAO,EAAE,QAAQ,cAAc,QAAQ,IAAI,QAAQ;EACnD,QAAQ,GAAG,OAAO,QAAQ,MAAM,CAAC,EAAE,MAAM;CAC1C;AACD"}
|
|
@@ -2,18 +2,113 @@ import { setOptions } from "../core/Util.js";
|
|
|
2
2
|
import { Class } from "../core/Class.js";
|
|
3
3
|
import { create } from "../dom/DomUtil.js";
|
|
4
4
|
//#region src/control/Control.js
|
|
5
|
+
/** @import {MaplairMap} from '../map/Map.js' */
|
|
6
|
+
/**
|
|
7
|
+
* Options of `Control`, inherited by all controls.
|
|
8
|
+
* @typedef {object} ControlOptions
|
|
9
|
+
* @property {string} [position='topright'] The position of the control (one of the map
|
|
10
|
+
* corners). Possible values are `'topleft'`, `'topright'`, `'bottomleft'` or
|
|
11
|
+
* `'bottomright'`.
|
|
12
|
+
* @property {number} [order=0] The position of the control within its corner: controls
|
|
13
|
+
* with a lower order are closer to the corner of the map, those with the same order in
|
|
14
|
+
* the order of adding.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Methods of `MaplairMap` for controls.
|
|
18
|
+
* @lends MaplairMap.prototype
|
|
19
|
+
*/
|
|
20
|
+
const mapControlMethods = {
|
|
21
|
+
/**
|
|
22
|
+
* Adds the given control to the map.
|
|
23
|
+
* @group Methods for layers and controls
|
|
24
|
+
* @param {Control} control
|
|
25
|
+
* @returns {this}
|
|
26
|
+
*/
|
|
27
|
+
addControl(control) {
|
|
28
|
+
control.addTo(this);
|
|
29
|
+
return this;
|
|
30
|
+
},
|
|
31
|
+
/**
|
|
32
|
+
* Removes the given control from the map.
|
|
33
|
+
* @group Methods for layers and controls
|
|
34
|
+
* @param {Control} control
|
|
35
|
+
* @returns {this}
|
|
36
|
+
*/
|
|
37
|
+
removeControl(control) {
|
|
38
|
+
control.remove();
|
|
39
|
+
return this;
|
|
40
|
+
},
|
|
41
|
+
_initControlPos() {
|
|
42
|
+
const corners = this._controlCorners = {}, l = "maplair-", container = this._controlContainer = create("div", `${l}control-container`, this._container);
|
|
43
|
+
function createCorner(vSide, hSide) {
|
|
44
|
+
const className = `${l + vSide} ${l}${hSide}`;
|
|
45
|
+
corners[vSide + hSide] = create("div", className, container);
|
|
46
|
+
}
|
|
47
|
+
createCorner("top", "left");
|
|
48
|
+
createCorner("top", "right");
|
|
49
|
+
createCorner("bottom", "left");
|
|
50
|
+
createCorner("bottom", "right");
|
|
51
|
+
},
|
|
52
|
+
_clearControlPos() {
|
|
53
|
+
for (const c of Object.values(this._controlCorners)) c.remove();
|
|
54
|
+
this._controlContainer.remove();
|
|
55
|
+
delete this._controlCorners;
|
|
56
|
+
delete this._controlContainer;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
/** @typedef {typeof mapControlMethods} MapControlMethods */
|
|
60
|
+
/**
|
|
61
|
+
* The methods which controls implement, see `Control`.
|
|
62
|
+
* @typedef {object} ControlExtensionMethods
|
|
63
|
+
* @property {(map: MaplairMap) => HTMLElement} [onAdd] Should return the container DOM
|
|
64
|
+
* element for the control and add listeners on relevant map events. Called on
|
|
65
|
+
* [`control.addTo(map)`](#control-addto).
|
|
66
|
+
* @property {(map: MaplairMap) => void} [onRemove] Should contain all clean up code that
|
|
67
|
+
* removes the listeners previously added in `onAdd`. Called on
|
|
68
|
+
* [`control.remove()`](#control-remove).
|
|
69
|
+
* @lends Control
|
|
70
|
+
*/
|
|
71
|
+
const controlOrders = /* @__PURE__ */ new WeakMap();
|
|
72
|
+
/**
|
|
73
|
+
* Control is a base class for implementing map controls. Handles positioning.
|
|
74
|
+
* All other controls extend from this class.
|
|
75
|
+
*
|
|
76
|
+
* Every control should extend from `Control` and (re-)implement the following methods:
|
|
77
|
+
*
|
|
78
|
+
* * `onAdd(map: MaplairMap): HTMLElement` should return the container DOM element for
|
|
79
|
+
* the control and add listeners on relevant map events. Called on
|
|
80
|
+
* [`control.addTo(map)`](#control-addTo).
|
|
81
|
+
* * `onRemove(map: MaplairMap)` (optional) should contain all clean up code that removes
|
|
82
|
+
* the listeners previously added in [`onAdd`](#control-onadd). Called on
|
|
83
|
+
* [`control.remove()`](#control-remove).
|
|
84
|
+
*/
|
|
5
85
|
var Control = class Control extends Class {
|
|
6
86
|
static {
|
|
7
|
-
this.setDefaultOptions({
|
|
87
|
+
this.setDefaultOptions({
|
|
88
|
+
position: "topright",
|
|
89
|
+
order: 0
|
|
90
|
+
});
|
|
8
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* @param {ControlOptions} [options]
|
|
94
|
+
*/
|
|
9
95
|
constructor(options) {
|
|
10
96
|
super();
|
|
11
97
|
setOptions(this, options);
|
|
12
98
|
this.callInitHooks(Control);
|
|
13
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Returns the position of the control.
|
|
102
|
+
* @returns {string}
|
|
103
|
+
*/
|
|
14
104
|
getPosition() {
|
|
15
105
|
return this.options.position;
|
|
16
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Sets the position of the control.
|
|
109
|
+
* @param {string} position
|
|
110
|
+
* @returns {this}
|
|
111
|
+
*/
|
|
17
112
|
setPosition(position) {
|
|
18
113
|
const map = this._map;
|
|
19
114
|
map?.removeControl(this);
|
|
@@ -21,19 +116,33 @@ var Control = class Control extends Class {
|
|
|
21
116
|
map?.addControl(this);
|
|
22
117
|
return this;
|
|
23
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Returns the HTMLElement that contains the control.
|
|
121
|
+
* @returns {HTMLElement}
|
|
122
|
+
*/
|
|
24
123
|
getContainer() {
|
|
25
124
|
return this._container;
|
|
26
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Adds the control to the given map.
|
|
128
|
+
* @param {MaplairMap} map
|
|
129
|
+
* @returns {this}
|
|
130
|
+
*/
|
|
27
131
|
addTo(map) {
|
|
28
132
|
this.remove();
|
|
29
133
|
this._map = map;
|
|
30
134
|
const container = this._container = this.onAdd(map), pos = this.getPosition(), corner = map._controlCorners[pos];
|
|
31
135
|
container.classList.add("maplair-control");
|
|
32
|
-
|
|
33
|
-
|
|
136
|
+
const order = this.options.order ?? 0, bottom = pos.includes("bottom"), children = [...corner.children];
|
|
137
|
+
controlOrders.set(container, order);
|
|
138
|
+
corner.insertBefore(container, children.find((el) => bottom ? (controlOrders.get(el) ?? 0) <= order : (controlOrders.get(el) ?? 0) > order) ?? null);
|
|
34
139
|
this._map.on("unload", this.remove, this);
|
|
35
140
|
return this;
|
|
36
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Removes the control from the map it is currently active on.
|
|
144
|
+
* @returns {this}
|
|
145
|
+
*/
|
|
37
146
|
remove() {
|
|
38
147
|
if (!this._map) return this;
|
|
39
148
|
this._container.remove();
|
|
@@ -45,34 +154,13 @@ var Control = class Control extends Class {
|
|
|
45
154
|
_refocusOnMap(e) {
|
|
46
155
|
if (this._map && e && !(e.screenX === 0 && e.screenY === 0)) this._map.getContainer().focus();
|
|
47
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Registers this default feature on the map class, called by `MaplairMap` itself.
|
|
159
|
+
* @internal
|
|
160
|
+
* @param {typeof MaplairMap} MaplairMap
|
|
161
|
+
*/
|
|
48
162
|
static register(MaplairMap) {
|
|
49
|
-
MaplairMap.include(
|
|
50
|
-
addControl(control) {
|
|
51
|
-
control.addTo(this);
|
|
52
|
-
return this;
|
|
53
|
-
},
|
|
54
|
-
removeControl(control) {
|
|
55
|
-
control.remove();
|
|
56
|
-
return this;
|
|
57
|
-
},
|
|
58
|
-
_initControlPos() {
|
|
59
|
-
const corners = this._controlCorners = {}, l = "maplair-", container = this._controlContainer = create("div", `${l}control-container`, this._container);
|
|
60
|
-
function createCorner(vSide, hSide) {
|
|
61
|
-
const className = `${l + vSide} ${l}${hSide}`;
|
|
62
|
-
corners[vSide + hSide] = create("div", className, container);
|
|
63
|
-
}
|
|
64
|
-
createCorner("top", "left");
|
|
65
|
-
createCorner("top", "right");
|
|
66
|
-
createCorner("bottom", "left");
|
|
67
|
-
createCorner("bottom", "right");
|
|
68
|
-
},
|
|
69
|
-
_clearControlPos() {
|
|
70
|
-
for (const c of Object.values(this._controlCorners)) c.remove();
|
|
71
|
-
this._controlContainer.remove();
|
|
72
|
-
delete this._controlCorners;
|
|
73
|
-
delete this._controlContainer;
|
|
74
|
-
}
|
|
75
|
-
});
|
|
163
|
+
MaplairMap.include(mapControlMethods);
|
|
76
164
|
}
|
|
77
165
|
};
|
|
78
166
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Control.js","names":["DomUtil.create"],"sources":["../../../src/control/Control.js"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"Control.js","names":["DomUtil.create"],"sources":["../../../src/control/Control.js"],"sourcesContent":["import {Class} from '../core/Class.js';\nimport * as Util from '../core/Util.js';\nimport * as DomUtil from '../dom/DomUtil.js';\n/** @import {MaplairMap} from '../map/Map.js' */\n\n/**\n * Options of `Control`, inherited by all controls.\n * @typedef {object} ControlOptions\n * @property {string} [position='topright'] The position of the control (one of the map\n * corners). Possible values are `'topleft'`, `'topright'`, `'bottomleft'` or\n * `'bottomright'`.\n * @property {number} [order=0] The position of the control within its corner: controls\n * with a lower order are closer to the corner of the map, those with the same order in\n * the order of adding.\n */\n\n/**\n * Methods of `MaplairMap` for controls.\n * @lends MaplairMap.prototype\n */\nconst mapControlMethods = {\n\t/**\n\t * Adds the given control to the map.\n\t * @group Methods for layers and controls\n\t * @param {Control} control\n\t * @returns {this}\n\t */\n\taddControl(control) {\n\t\tcontrol.addTo(this);\n\t\treturn this;\n\t},\n\n\t/**\n\t * Removes the given control from the map.\n\t * @group Methods for layers and controls\n\t * @param {Control} control\n\t * @returns {this}\n\t */\n\tremoveControl(control) {\n\t\tcontrol.remove();\n\t\treturn this;\n\t},\n\n\t_initControlPos() {\n\t\tconst corners = this._controlCorners = {},\n\t\tl = 'maplair-',\n\t\tcontainer = this._controlContainer = DomUtil.create('div', `${l}control-container`, this._container);\n\n\t\tfunction createCorner(vSide, hSide) {\n\t\t\tconst className = `${l + vSide} ${l}${hSide}`;\n\n\t\t\tcorners[vSide + hSide] = DomUtil.create('div', className, container);\n\t\t}\n\n\t\tcreateCorner('top', 'left');\n\t\tcreateCorner('top', 'right');\n\t\tcreateCorner('bottom', 'left');\n\t\tcreateCorner('bottom', 'right');\n\t},\n\n\t_clearControlPos() {\n\t\tfor (const c of Object.values(this._controlCorners)) {\n\t\t\tc.remove();\n\t\t}\n\t\tthis._controlContainer.remove();\n\t\tdelete this._controlCorners;\n\t\tdelete this._controlContainer;\n\t}\n};\n/** @typedef {typeof mapControlMethods} MapControlMethods */\n\n/**\n * The methods which controls implement, see `Control`.\n * @typedef {object} ControlExtensionMethods\n * @property {(map: MaplairMap) => HTMLElement} [onAdd] Should return the container DOM\n * element for the control and add listeners on relevant map events. Called on\n * [`control.addTo(map)`](#control-addto).\n * @property {(map: MaplairMap) => void} [onRemove] Should contain all clean up code that\n * removes the listeners previously added in `onAdd`. Called on\n * [`control.remove()`](#control-remove).\n * @lends Control\n */\n\n// The `order` option of the containers of added controls\nconst controlOrders = new WeakMap();\n\n/**\n * Control is a base class for implementing map controls. Handles positioning.\n * All other controls extend from this class.\n *\n * Every control should extend from `Control` and (re-)implement the following methods:\n *\n * * `onAdd(map: MaplairMap): HTMLElement` should return the container DOM element for\n * the control and add listeners on relevant map events. Called on\n * [`control.addTo(map)`](#control-addTo).\n * * `onRemove(map: MaplairMap)` (optional) should contain all clean up code that removes\n * the listeners previously added in [`onAdd`](#control-onadd). Called on\n * [`control.remove()`](#control-remove).\n */\nexport class Control extends Class {\n\n\tstatic {\n\t\tthis.setDefaultOptions({\n\t\t\tposition: 'topright',\n\t\t\torder: 0\n\t\t});\n\t}\n\n\t/**\n\t * @param {ControlOptions} [options]\n\t */\n\tconstructor(options) {\n\t\tsuper();\n\t\tUtil.setOptions(this, options);\n\n\t\tthis.callInitHooks(Control);\n\t}\n\n\t/**\n\t * Returns the position of the control.\n\t * @returns {string}\n\t */\n\tgetPosition() {\n\t\treturn this.options.position;\n\t}\n\n\t/**\n\t * Sets the position of the control.\n\t * @param {string} position\n\t * @returns {this}\n\t */\n\tsetPosition(position) {\n\t\tconst map = this._map;\n\n\t\tmap?.removeControl(this);\n\n\t\tthis.options.position = position;\n\n\t\tmap?.addControl(this);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Returns the HTMLElement that contains the control.\n\t * @returns {HTMLElement}\n\t */\n\tgetContainer() {\n\t\treturn this._container;\n\t}\n\n\t/**\n\t * Adds the control to the given map.\n\t * @param {MaplairMap} map\n\t * @returns {this}\n\t */\n\taddTo(map) {\n\t\tthis.remove();\n\t\tthis._map = map;\n\n\t\tconst container = this._container = this.onAdd(map),\n\t\tpos = this.getPosition(),\n\t\tcorner = map._controlCorners[pos];\n\n\t\tcontainer.classList.add('maplair-control');\n\n\t\t// sorted by order from the corner of the map (bottom corners are filled upwards),\n\t\t// farther than the controls with the same order\n\t\tconst order = this.options.order ?? 0,\n\t\tbottom = pos.includes('bottom'),\n\t\tchildren = [...corner.children];\n\t\tcontrolOrders.set(container, order);\n\t\tcorner.insertBefore(container, children.find(el => bottom ?\n\t\t\t(controlOrders.get(el) ?? 0) <= order :\n\t\t\t(controlOrders.get(el) ?? 0) > order) ?? null);\n\n\t\tthis._map.on('unload', this.remove, this);\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Removes the control from the map it is currently active on.\n\t * @returns {this}\n\t */\n\tremove() {\n\t\tif (!this._map) {\n\t\t\treturn this;\n\t\t}\n\n\t\tthis._container.remove();\n\n\t\tif (this.onRemove) {\n\t\t\tthis.onRemove(this._map);\n\t\t}\n\n\t\tthis._map.off('unload', this.remove, this);\n\t\tthis._map = null;\n\n\t\treturn this;\n\t}\n\n\t_refocusOnMap(e) {\n\t\t// We exclude keyboard-click event to keep the focus on the control for accessibility.\n\t\t// The position of keyboard-click events are x=0 and y=0.\n\t\tif (this._map && e && !(e.screenX === 0 && e.screenY === 0)) {\n\t\t\tthis._map.getContainer().focus();\n\t\t}\n\t}\n\n\t/**\n\t * Registers this default feature on the map class, called by `MaplairMap` itself.\n\t * @internal\n\t * @param {typeof MaplairMap} MaplairMap\n\t */\n\tstatic register(MaplairMap) {\n\t\tMaplairMap.include(mapControlMethods);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAoBA,MAAM,oBAAoB;;;;;;;CAOzB,WAAW,SAAS;EACnB,QAAQ,MAAM,IAAI;EAClB,OAAO;CACR;;;;;;;CAQA,cAAc,SAAS;EACtB,QAAQ,OAAO;EACf,OAAO;CACR;CAEA,kBAAkB;EACjB,MAAM,UAAU,KAAK,kBAAkB,CAAC,GACxC,IAAI,YACJ,YAAY,KAAK,oBAAoBA,OAAe,OAAO,GAAG,EAAE,oBAAoB,KAAK,UAAU;EAEnG,SAAS,aAAa,OAAO,OAAO;GACnC,MAAM,YAAY,GAAG,IAAI,MAAM,GAAG,IAAI;GAEtC,QAAQ,QAAQ,SAASA,OAAe,OAAO,WAAW,SAAS;EACpE;EAEA,aAAa,OAAO,MAAM;EAC1B,aAAa,OAAO,OAAO;EAC3B,aAAa,UAAU,MAAM;EAC7B,aAAa,UAAU,OAAO;CAC/B;CAEA,mBAAmB;EAClB,KAAK,MAAM,KAAK,OAAO,OAAO,KAAK,eAAe,GACjD,EAAE,OAAO;EAEV,KAAK,kBAAkB,OAAO;EAC9B,OAAO,KAAK;EACZ,OAAO,KAAK;CACb;AACD;;;;;;;;;;;;;AAgBA,MAAM,gCAAgB,IAAI,QAAQ;;;;;;;;;;;;;;AAelC,IAAa,UAAb,MAAa,gBAAgB,MAAM;CAElC;EACC,KAAK,kBAAkB;GACtB,UAAU;GACV,OAAO;EACR,CAAC;CACF;;;;CAKA,YAAY,SAAS;EACpB,MAAM;EACN,WAAgB,MAAM,OAAO;EAE7B,KAAK,cAAc,OAAO;CAC3B;;;;;CAMA,cAAc;EACb,OAAO,KAAK,QAAQ;CACrB;;;;;;CAOA,YAAY,UAAU;EACrB,MAAM,MAAM,KAAK;EAEjB,KAAK,cAAc,IAAI;EAEvB,KAAK,QAAQ,WAAW;EAExB,KAAK,WAAW,IAAI;EAEpB,OAAO;CACR;;;;;CAMA,eAAe;EACd,OAAO,KAAK;CACb;;;;;;CAOA,MAAM,KAAK;EACV,KAAK,OAAO;EACZ,KAAK,OAAO;EAEZ,MAAM,YAAY,KAAK,aAAa,KAAK,MAAM,GAAG,GAClD,MAAM,KAAK,YAAY,GACvB,SAAS,IAAI,gBAAgB;EAE7B,UAAU,UAAU,IAAI,iBAAiB;EAIzC,MAAM,QAAQ,KAAK,QAAQ,SAAS,GACpC,SAAS,IAAI,SAAS,QAAQ,GAC9B,WAAW,CAAC,GAAG,OAAO,QAAQ;EAC9B,cAAc,IAAI,WAAW,KAAK;EAClC,OAAO,aAAa,WAAW,SAAS,MAAK,OAAM,UACjD,cAAc,IAAI,EAAE,KAAK,MAAM,SAC/B,cAAc,IAAI,EAAE,KAAK,KAAK,KAAK,KAAK,IAAI;EAE9C,KAAK,KAAK,GAAG,UAAU,KAAK,QAAQ,IAAI;EAExC,OAAO;CACR;;;;;CAMA,SAAS;EACR,IAAI,CAAC,KAAK,MACT,OAAO;EAGR,KAAK,WAAW,OAAO;EAEvB,IAAI,KAAK,UACR,KAAK,SAAS,KAAK,IAAI;EAGxB,KAAK,KAAK,IAAI,UAAU,KAAK,QAAQ,IAAI;EACzC,KAAK,OAAO;EAEZ,OAAO;CACR;CAEA,cAAc,GAAG;EAGhB,IAAI,KAAK,QAAQ,KAAK,EAAE,EAAE,YAAY,KAAK,EAAE,YAAY,IACxD,KAAK,KAAK,aAAa,CAAC,CAAC,MAAM;CAEjC;;;;;;CAOA,OAAO,SAAS,YAAY;EAC3B,WAAW,QAAQ,iBAAiB;CACrC;AACD"}
|