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.
Files changed (315) hide show
  1. package/CHANGELOG.md +47 -3
  2. package/LICENSE +1 -1
  3. package/README.md +10 -7
  4. package/dist/esm/Maplair.js +52 -33
  5. package/dist/esm/Maplair.js.map +1 -1
  6. package/dist/esm/control/AttributionControl.js +76 -4
  7. package/dist/esm/control/AttributionControl.js.map +1 -1
  8. package/dist/esm/control/ButtonControl.js +221 -0
  9. package/dist/esm/control/ButtonControl.js.map +1 -0
  10. package/dist/esm/control/ContextMenu.js +173 -0
  11. package/dist/esm/control/ContextMenu.js.map +1 -0
  12. package/dist/esm/control/Control.js +118 -30
  13. package/dist/esm/control/Control.js.map +1 -1
  14. package/dist/esm/control/FullscreenControl.js +186 -0
  15. package/dist/esm/control/FullscreenControl.js.map +1 -0
  16. package/dist/esm/control/LayersControl.js +122 -3
  17. package/dist/esm/control/LayersControl.js.map +1 -1
  18. package/dist/esm/control/LegendControl.js +230 -0
  19. package/dist/esm/control/LegendControl.js.map +1 -0
  20. package/dist/esm/control/MiniMapControl.js +182 -0
  21. package/dist/esm/control/MiniMapControl.js.map +1 -0
  22. package/dist/esm/control/RotateControl.js +104 -0
  23. package/dist/esm/control/RotateControl.js.map +1 -0
  24. package/dist/esm/control/ScaleControl.js +40 -0
  25. package/dist/esm/control/ScaleControl.js.map +1 -1
  26. package/dist/esm/control/SearchControl.js +442 -0
  27. package/dist/esm/control/SearchControl.js.map +1 -0
  28. package/dist/esm/control/SideBySideControl.js +208 -0
  29. package/dist/esm/control/SideBySideControl.js.map +1 -0
  30. package/dist/esm/control/TextControl.js +83 -0
  31. package/dist/esm/control/TextControl.js.map +1 -0
  32. package/dist/esm/control/ZoomControl.js +68 -5
  33. package/dist/esm/control/ZoomControl.js.map +1 -1
  34. package/dist/esm/core/Browser.js +71 -2
  35. package/dist/esm/core/Browser.js.map +1 -1
  36. package/dist/esm/core/Class.js +193 -0
  37. package/dist/esm/core/Class.js.map +1 -1
  38. package/dist/esm/core/Events.js +231 -0
  39. package/dist/esm/core/Events.js.map +1 -1
  40. package/dist/esm/core/Handler.js +34 -0
  41. package/dist/esm/core/Handler.js.map +1 -1
  42. package/dist/esm/core/I18n.js +38 -0
  43. package/dist/esm/core/I18n.js.map +1 -1
  44. package/dist/esm/core/Util.js +62 -0
  45. package/dist/esm/core/Util.js.map +1 -1
  46. package/dist/esm/dom/DomEvent.DoubleTap.js +14 -0
  47. package/dist/esm/dom/DomEvent.DoubleTap.js.map +1 -1
  48. package/dist/esm/dom/DomEvent.PointerEvents.js +25 -1
  49. package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -1
  50. package/dist/esm/dom/DomEvent.js +95 -0
  51. package/dist/esm/dom/DomEvent.js.map +1 -1
  52. package/dist/esm/dom/DomUtil.js +94 -0
  53. package/dist/esm/dom/DomUtil.js.map +1 -1
  54. package/dist/esm/dom/Draggable.js +55 -1
  55. package/dist/esm/dom/Draggable.js.map +1 -1
  56. package/dist/esm/dom/PosAnimation.js +48 -0
  57. package/dist/esm/dom/PosAnimation.js.map +1 -1
  58. package/dist/esm/geo/LatLng.js +154 -3
  59. package/dist/esm/geo/LatLng.js.map +1 -1
  60. package/dist/esm/geo/LatLngBounds.js +148 -7
  61. package/dist/esm/geo/LatLngBounds.js.map +1 -1
  62. package/dist/esm/geo/crs/CRS.js +102 -1
  63. package/dist/esm/geo/crs/CRS.js.map +1 -1
  64. package/dist/esm/geo/crs/EPSG3395.js +8 -1
  65. package/dist/esm/geo/crs/EPSG3395.js.map +1 -1
  66. package/dist/esm/geo/crs/EPSG3857.js +14 -1
  67. package/dist/esm/geo/crs/EPSG3857.js.map +1 -1
  68. package/dist/esm/geo/crs/EPSG4326.js +15 -1
  69. package/dist/esm/geo/crs/EPSG4326.js.map +1 -1
  70. package/dist/esm/geo/crs/EarthCRS.js +24 -0
  71. package/dist/esm/geo/crs/EarthCRS.js.map +1 -1
  72. package/dist/esm/geo/crs/SimpleCRS.js +28 -1
  73. package/dist/esm/geo/crs/SimpleCRS.js.map +1 -1
  74. package/dist/esm/geo/projection/Projection.LonLat.js +10 -2
  75. package/dist/esm/geo/projection/Projection.LonLat.js.map +1 -1
  76. package/dist/esm/geo/projection/Projection.Mercator.js +7 -2
  77. package/dist/esm/geo/projection/Projection.Mercator.js.map +1 -1
  78. package/dist/esm/geo/projection/Projection.SphericalMercator.js +9 -2
  79. package/dist/esm/geo/projection/Projection.SphericalMercator.js.map +1 -1
  80. package/dist/esm/geo/projection/index.js +1 -1
  81. package/dist/esm/geo/projection/index.js.map +1 -1
  82. package/dist/esm/geometry/Bounds.js +125 -7
  83. package/dist/esm/geometry/Bounds.js.map +1 -1
  84. package/dist/esm/geometry/LineUtil.js +55 -0
  85. package/dist/esm/geometry/LineUtil.js.map +1 -1
  86. package/dist/esm/geometry/Point.js +144 -5
  87. package/dist/esm/geometry/Point.js.map +1 -1
  88. package/dist/esm/geometry/PolyUtil.js +27 -1
  89. package/dist/esm/geometry/PolyUtil.js.map +1 -1
  90. package/dist/esm/geometry/Transformation.js +35 -0
  91. package/dist/esm/geometry/Transformation.js.map +1 -1
  92. package/dist/esm/layer/BlanketOverlay.js +54 -1
  93. package/dist/esm/layer/BlanketOverlay.js.map +1 -1
  94. package/dist/esm/layer/Classification.js +273 -0
  95. package/dist/esm/layer/Classification.js.map +1 -0
  96. package/dist/esm/layer/DivOverlay.js +219 -24
  97. package/dist/esm/layer/DivOverlay.js.map +1 -1
  98. package/dist/esm/layer/FeatureGroup.js +71 -0
  99. package/dist/esm/layer/FeatureGroup.js.map +1 -1
  100. package/dist/esm/layer/GeoJSON.Load.js +61 -0
  101. package/dist/esm/layer/GeoJSON.Load.js.map +1 -0
  102. package/dist/esm/layer/GeoJSON.js +219 -8
  103. package/dist/esm/layer/GeoJSON.js.map +1 -1
  104. package/dist/esm/layer/HeatLayer.js +198 -0
  105. package/dist/esm/layer/HeatLayer.js.map +1 -0
  106. package/dist/esm/layer/ImageOverlay.js +126 -1
  107. package/dist/esm/layer/ImageOverlay.js.map +1 -1
  108. package/dist/esm/layer/Layer.js +273 -63
  109. package/dist/esm/layer/Layer.js.map +1 -1
  110. package/dist/esm/layer/LayerGroup.js +77 -0
  111. package/dist/esm/layer/LayerGroup.js.map +1 -1
  112. package/dist/esm/layer/Popup.js +333 -90
  113. package/dist/esm/layer/Popup.js.map +1 -1
  114. package/dist/esm/layer/SVGOverlay.js +33 -0
  115. package/dist/esm/layer/SVGOverlay.js.map +1 -1
  116. package/dist/esm/layer/Tooltip.js +405 -149
  117. package/dist/esm/layer/Tooltip.js.map +1 -1
  118. package/dist/esm/layer/VideoOverlay.js +49 -0
  119. package/dist/esm/layer/VideoOverlay.js.map +1 -1
  120. package/dist/esm/layer/marker/DefaultIcon.js +33 -0
  121. package/dist/esm/layer/marker/DefaultIcon.js.map +1 -1
  122. package/dist/esm/layer/marker/DivIcon.js +45 -1
  123. package/dist/esm/layer/marker/DivIcon.js.map +1 -1
  124. package/dist/esm/layer/marker/Icon.js +73 -2
  125. package/dist/esm/layer/marker/Icon.js.map +1 -1
  126. package/dist/esm/layer/marker/Marker.Drag.js +29 -1
  127. package/dist/esm/layer/marker/Marker.Drag.js.map +1 -1
  128. package/dist/esm/layer/marker/Marker.Move.js +107 -0
  129. package/dist/esm/layer/marker/Marker.Move.js.map +1 -0
  130. package/dist/esm/layer/marker/Marker.Rotation.js +70 -0
  131. package/dist/esm/layer/marker/Marker.Rotation.js.map +1 -0
  132. package/dist/esm/layer/marker/Marker.js +157 -3
  133. package/dist/esm/layer/marker/Marker.js.map +1 -1
  134. package/dist/esm/layer/marker/SvgIcon.js +116 -0
  135. package/dist/esm/layer/marker/SvgIcon.js.map +1 -0
  136. package/dist/esm/layer/tile/GridLayer.js +254 -5
  137. package/dist/esm/layer/tile/GridLayer.js.map +1 -1
  138. package/dist/esm/layer/tile/TileLayer.js +190 -8
  139. package/dist/esm/layer/tile/TileLayer.js.map +1 -1
  140. package/dist/esm/layer/tile/WMSTileLayer.js +114 -6
  141. package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -1
  142. package/dist/esm/layer/vector/Canvas.js +58 -0
  143. package/dist/esm/layer/vector/Canvas.js.map +1 -1
  144. package/dist/esm/layer/vector/Circle.js +30 -0
  145. package/dist/esm/layer/vector/Circle.js.map +1 -1
  146. package/dist/esm/layer/vector/CircleMarker.js +54 -1
  147. package/dist/esm/layer/vector/CircleMarker.js.map +1 -1
  148. package/dist/esm/layer/vector/Path.DashAnimation.js +78 -0
  149. package/dist/esm/layer/vector/Path.DashAnimation.js.map +1 -0
  150. package/dist/esm/layer/vector/Path.ZoomStyles.js +77 -0
  151. package/dist/esm/layer/vector/Path.ZoomStyles.js.map +1 -0
  152. package/dist/esm/layer/vector/Path.js +74 -0
  153. package/dist/esm/layer/vector/Path.js.map +1 -1
  154. package/dist/esm/layer/vector/Polygon.js +67 -0
  155. package/dist/esm/layer/vector/Polygon.js.map +1 -1
  156. package/dist/esm/layer/vector/Polyline.Decorations.js +244 -0
  157. package/dist/esm/layer/vector/Polyline.Decorations.js.map +1 -0
  158. package/dist/esm/layer/vector/Polyline.js +91 -2
  159. package/dist/esm/layer/vector/Polyline.js.map +1 -1
  160. package/dist/esm/layer/vector/Rectangle.js +28 -0
  161. package/dist/esm/layer/vector/Rectangle.js.map +1 -1
  162. package/dist/esm/layer/vector/Renderer.getRenderer.js +52 -20
  163. package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -1
  164. package/dist/esm/layer/vector/Renderer.js +36 -0
  165. package/dist/esm/layer/vector/Renderer.js.map +1 -1
  166. package/dist/esm/layer/vector/SVG.js +49 -0
  167. package/dist/esm/layer/vector/SVG.js.map +1 -1
  168. package/dist/esm/map/Hash.js +237 -0
  169. package/dist/esm/map/Hash.js.map +1 -0
  170. package/dist/esm/map/Map.Rotation.js +500 -0
  171. package/dist/esm/map/Map.Rotation.js.map +1 -0
  172. package/dist/esm/map/Map.js +817 -33
  173. package/dist/esm/map/Map.js.map +1 -1
  174. package/dist/esm/map/MapSync.js +168 -0
  175. package/dist/esm/map/MapSync.js.map +1 -0
  176. package/dist/esm/map/handler/BoxZoomHandler.js +50 -1
  177. package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -1
  178. package/dist/esm/map/handler/DisplaceHandler.js +349 -0
  179. package/dist/esm/map/handler/DisplaceHandler.js.map +1 -0
  180. package/dist/esm/map/handler/DoubleClickZoomHandler.js +44 -0
  181. package/dist/esm/map/handler/DoubleClickZoomHandler.js.map +1 -1
  182. package/dist/esm/map/handler/DragHandler.js +67 -5
  183. package/dist/esm/map/handler/DragHandler.js.map +1 -1
  184. package/dist/esm/map/handler/KeyboardHandler.js +92 -5
  185. package/dist/esm/map/handler/KeyboardHandler.js.map +1 -1
  186. package/dist/esm/map/handler/PinchZoomHandler.js +39 -0
  187. package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -1
  188. package/dist/esm/map/handler/ScrollWheelZoomHandler.js +161 -0
  189. package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -1
  190. package/dist/esm/map/handler/TapDragZoomHandler.js +150 -0
  191. package/dist/esm/map/handler/TapDragZoomHandler.js.map +1 -0
  192. package/dist/esm/map/handler/TapHoldHandler.js +35 -0
  193. package/dist/esm/map/handler/TapHoldHandler.js.map +1 -1
  194. package/dist/esm/package.js +1 -1
  195. package/dist/maplair-global-src.js +15438 -4749
  196. package/dist/maplair-global-src.js.map +1 -1
  197. package/dist/maplair-global.js +3 -3
  198. package/dist/maplair-global.js.map +1 -1
  199. package/dist/maplair-src.js +15421 -4750
  200. package/dist/maplair-src.js.map +1 -1
  201. package/dist/maplair.css +629 -146
  202. package/dist/maplair.js +3 -3
  203. package/dist/maplair.js.map +1 -1
  204. package/i18n/cs.json +21 -1
  205. package/i18n/de.json +21 -1
  206. package/i18n/en.json +21 -1
  207. package/i18n/es.json +21 -1
  208. package/i18n/fr.json +21 -1
  209. package/i18n/hu.json +21 -1
  210. package/i18n/it.json +21 -1
  211. package/i18n/rm.json +21 -1
  212. package/i18n/sk.json +21 -1
  213. package/i18n/sl.json +21 -1
  214. package/package.json +84 -34
  215. package/src/Maplair.all.js +10 -0
  216. package/src/Maplair.js +4 -0
  217. package/src/control/AttributionControl.js +78 -31
  218. package/src/control/ButtonControl.js +248 -0
  219. package/src/control/ContextMenu.js +201 -0
  220. package/src/control/Control.js +132 -87
  221. package/src/control/FullscreenControl.js +227 -0
  222. package/src/control/LayersControl.js +92 -56
  223. package/src/control/LegendControl.js +273 -0
  224. package/src/control/MiniMapControl.js +198 -0
  225. package/src/control/RotateControl.js +122 -0
  226. package/src/control/ScaleControl.js +34 -25
  227. package/src/control/SearchControl.js +530 -0
  228. package/src/control/SideBySideControl.js +236 -0
  229. package/src/control/TextControl.js +91 -0
  230. package/src/control/ZoomControl.js +63 -35
  231. package/src/control/index.js +8 -0
  232. package/src/core/Browser.js +71 -36
  233. package/src/core/Class.js +193 -18
  234. package/src/core/Events.js +227 -35
  235. package/src/core/Handler.js +34 -24
  236. package/src/core/I18n.js +26 -19
  237. package/src/core/Util.js +63 -34
  238. package/src/dom/DomEvent.DoubleTap.js +15 -0
  239. package/src/dom/DomEvent.PointerEvents.js +30 -15
  240. package/src/dom/DomEvent.js +98 -62
  241. package/src/dom/DomUtil.js +102 -52
  242. package/src/dom/Draggable.js +49 -29
  243. package/src/dom/PosAnimation.js +27 -22
  244. package/src/geo/LatLng.js +150 -54
  245. package/src/geo/LatLngBounds.js +129 -69
  246. package/src/geo/crs/CRS.js +95 -56
  247. package/src/geo/crs/EPSG3395.js +5 -5
  248. package/src/geo/crs/EPSG3857.js +9 -6
  249. package/src/geo/crs/EPSG4326.js +11 -9
  250. package/src/geo/crs/EarthCRS.js +19 -9
  251. package/src/geo/crs/SimpleCRS.js +22 -5
  252. package/src/geo/projection/Projection.LonLat.js +7 -11
  253. package/src/geo/projection/Projection.Mercator.js +8 -8
  254. package/src/geo/projection/Projection.SphericalMercator.js +8 -9
  255. package/src/geo/projection/index.js +17 -15
  256. package/src/geometry/Bounds.js +108 -63
  257. package/src/geometry/LineUtil.js +56 -27
  258. package/src/geometry/Point.js +128 -69
  259. package/src/geometry/PolyUtil.js +22 -10
  260. package/src/geometry/Transformation.js +23 -18
  261. package/src/layer/BlanketOverlay.js +50 -51
  262. package/src/layer/Classification.js +302 -0
  263. package/src/layer/DivOverlay.js +247 -92
  264. package/src/layer/FeatureGroup.js +62 -26
  265. package/src/layer/GeoJSON.Load.js +109 -0
  266. package/src/layer/GeoJSON.js +220 -124
  267. package/src/layer/HeatLayer.js +231 -0
  268. package/src/layer/ImageOverlay.js +122 -62
  269. package/src/layer/Layer.js +311 -192
  270. package/src/layer/LayerGroup.js +66 -35
  271. package/src/layer/Popup.js +343 -270
  272. package/src/layer/SVGOverlay.js +22 -15
  273. package/src/layer/Tooltip.js +453 -298
  274. package/src/layer/VideoOverlay.js +41 -37
  275. package/src/layer/index.js +2 -0
  276. package/src/layer/marker/DefaultIcon.js +27 -11
  277. package/src/layer/marker/DivIcon.js +36 -20
  278. package/src/layer/marker/Icon.js +56 -61
  279. package/src/layer/marker/Marker.Drag.js +26 -29
  280. package/src/layer/marker/Marker.Move.js +153 -0
  281. package/src/layer/marker/Marker.Rotation.js +103 -0
  282. package/src/layer/marker/Marker.js +153 -94
  283. package/src/layer/marker/SvgIcon.js +122 -0
  284. package/src/layer/marker/index.js +1 -0
  285. package/src/layer/tile/GridLayer.js +224 -112
  286. package/src/layer/tile/TileLayer.js +204 -80
  287. package/src/layer/tile/WMSTileLayer.js +118 -44
  288. package/src/layer/vector/Canvas.js +44 -12
  289. package/src/layer/vector/Circle.js +24 -22
  290. package/src/layer/vector/CircleMarker.js +53 -22
  291. package/src/layer/vector/Path.DashAnimation.js +108 -0
  292. package/src/layer/vector/Path.ZoomStyles.js +103 -0
  293. package/src/layer/vector/Path.js +73 -57
  294. package/src/layer/vector/Polygon.js +32 -12
  295. package/src/layer/vector/Polyline.Decorations.js +299 -0
  296. package/src/layer/vector/Polyline.js +60 -37
  297. package/src/layer/vector/Rectangle.js +14 -14
  298. package/src/layer/vector/Renderer.getRenderer.js +65 -43
  299. package/src/layer/vector/Renderer.js +24 -9
  300. package/src/layer/vector/SVG.js +29 -19
  301. package/src/map/Hash.js +255 -0
  302. package/src/map/Map.Rotation.js +927 -0
  303. package/src/map/Map.js +866 -421
  304. package/src/map/MapSync.js +225 -0
  305. package/src/map/handler/BoxZoomHandler.js +50 -12
  306. package/src/map/handler/DisplaceHandler.js +416 -0
  307. package/src/map/handler/DoubleClickZoomHandler.js +43 -22
  308. package/src/map/handler/DragHandler.js +65 -40
  309. package/src/map/handler/KeyboardHandler.js +96 -18
  310. package/src/map/handler/PinchZoomHandler.js +39 -18
  311. package/src/map/handler/ScrollWheelZoomHandler.js +204 -21
  312. package/src/map/handler/TapDragZoomHandler.js +209 -0
  313. package/src/map/handler/TapHoldHandler.js +36 -17
  314. package/src/map/index.js +4 -0
  315. 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;C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,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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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({ position: "topright" });
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
- if (pos.includes("bottom")) corner.insertBefore(container, corner.firstChild);
33
- else corner.appendChild(container);
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":["\nimport {Class} from '../core/Class.js';\nimport * as Util from '../core/Util.js';\nimport * as DomUtil from '../dom/DomUtil.js';\n\n/*\n * @class Control\n * @inherits Class\n *\n * Control is a base class for implementing map controls. Handles positioning.\n * All other controls extend from this class.\n */\n\nexport class Control extends Class {\n\n\tstatic {\n\t\t// @section\n\t\t// @aka Control Options\n\t\tthis.setDefaultOptions({\n\t\t\t// @option position: String = 'topright'\n\t\t\t// The position of the control (one of the map corners). Possible values are `'topleft'`,\n\t\t\t// `'topright'`, `'bottomleft'` or `'bottomright'`\n\t\t\tposition: 'topright'\n\t\t});\n\t}\n\n\tconstructor(options) {\n\t\tsuper();\n\t\tUtil.setOptions(this, options);\n\n\t\tthis.callInitHooks(Control);\n\t}\n\n\t/* @section\n\t * Classes extending Control will inherit the following methods:\n\t *\n\t * @method getPosition: string\n\t * Returns the position of the control.\n\t */\n\tgetPosition() {\n\t\treturn this.options.position;\n\t}\n\n\t// @method setPosition(position: string): this\n\t// Sets the position of the control.\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// @method getContainer: HTMLElement\n\t// Returns the HTMLElement that contains the control.\n\tgetContainer() {\n\t\treturn this._container;\n\t}\n\n\t// @method addTo(map: MaplairMap): this\n\t// Adds the control to the given map.\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\tif (pos.includes('bottom')) {\n\t\t\tcorner.insertBefore(container, corner.firstChild);\n\t\t} else {\n\t\t\tcorner.appendChild(container);\n\t\t}\n\n\t\tthis._map.on('unload', this.remove, this);\n\n\t\treturn this;\n\t}\n\n\t// @method remove: this\n\t// Removes the control from the map it is currently active on.\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// Registers this default feature on the map class, called by `MaplairMap` itself.\n\tstatic register(MaplairMap) {\n\t\t/* @namespace MaplairMap\n\t\t * @section Methods for Layers and Controls\n\t\t */\n\t\tMaplairMap.include({\n\t\t\t// @method addControl(control: Control): this\n\t\t\t// Adds the given control to the map\n\t\t\taddControl(control) {\n\t\t\t\tcontrol.addTo(this);\n\t\t\t\treturn this;\n\t\t\t},\n\n\t\t\t// @method removeControl(control: Control): this\n\t\t\t// Removes the given control from the map\n\t\t\tremoveControl(control) {\n\t\t\t\tcontrol.remove();\n\t\t\t\treturn this;\n\t\t\t},\n\n\t\t\t_initControlPos() {\n\t\t\t\tconst corners = this._controlCorners = {},\n\t\t\t\tl = 'maplair-',\n\t\t\t\tcontainer = this._controlContainer = DomUtil.create('div', `${l}control-container`, this._container);\n\n\t\t\t\tfunction createCorner(vSide, hSide) {\n\t\t\t\t\tconst className = `${l + vSide} ${l}${hSide}`;\n\n\t\t\t\t\tcorners[vSide + hSide] = DomUtil.create('div', className, container);\n\t\t\t\t}\n\n\t\t\t\tcreateCorner('top', 'left');\n\t\t\t\tcreateCorner('top', 'right');\n\t\t\t\tcreateCorner('bottom', 'left');\n\t\t\t\tcreateCorner('bottom', 'right');\n\t\t\t},\n\n\t\t\t_clearControlPos() {\n\t\t\t\tfor (const c of Object.values(this._controlCorners)) {\n\t\t\t\t\tc.remove();\n\t\t\t\t}\n\t\t\t\tthis._controlContainer.remove();\n\t\t\t\tdelete this._controlCorners;\n\t\t\t\tdelete this._controlContainer;\n\t\t\t}\n\t\t});\n\t}\n}\n\n/* @namespace Control\n * @section Extension methods\n * @uninheritable\n *\n * Every control should extend from `Control` and (re-)implement the following methods.\n *\n * @method onAdd(map: MaplairMap): HTMLElement\n * Should return the container DOM element for the control and add listeners on relevant map events. Called on [`control.addTo(map)`](#control-addTo).\n *\n * @method onRemove(map: MaplairMap)\n * Optional method. Should contain all clean up code that removes the listeners previously added in [`onAdd`](#control-onadd). Called on [`control.remove()`](#control-remove).\n */\n"],"mappings":";;;;AAaA,IAAa,UAAb,MAAa,gBAAgB,MAAM;CAElC;EAGC,KAAK,kBAAkB,EAItB,UAAU,WACX,CAAC;CACF;CAEA,YAAY,SAAS;EACpB,MAAM;EACN,WAAgB,MAAM,OAAO;EAE7B,KAAK,cAAc,OAAO;CAC3B;CAQA,cAAc;EACb,OAAO,KAAK,QAAQ;CACrB;CAIA,YAAY,UAAU;EACrB,MAAM,MAAM,KAAK;EAEjB,KAAK,cAAc,IAAI;EAEvB,KAAK,QAAQ,WAAW;EAExB,KAAK,WAAW,IAAI;EAEpB,OAAO;CACR;CAIA,eAAe;EACd,OAAO,KAAK;CACb;CAIA,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;EAEzC,IAAI,IAAI,SAAS,QAAQ,GACxB,OAAO,aAAa,WAAW,OAAO,UAAU;OAEhD,OAAO,YAAY,SAAS;EAG7B,KAAK,KAAK,GAAG,UAAU,KAAK,QAAQ,IAAI;EAExC,OAAO;CACR;CAIA,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;CAGA,OAAO,SAAS,YAAY;EAI3B,WAAW,QAAQ;GAGlB,WAAW,SAAS;IACnB,QAAQ,MAAM,IAAI;IAClB,OAAO;GACR;GAIA,cAAc,SAAS;IACtB,QAAQ,OAAO;IACf,OAAO;GACR;GAEA,kBAAkB;IACjB,MAAM,UAAU,KAAK,kBAAkB,CAAC,GACxC,IAAI,YACJ,YAAY,KAAK,oBAAoBA,OAAe,OAAO,GAAG,EAAE,oBAAoB,KAAK,UAAU;IAEnG,SAAS,aAAa,OAAO,OAAO;KACnC,MAAM,YAAY,GAAG,IAAI,MAAM,GAAG,IAAI;KAEtC,QAAQ,QAAQ,SAASA,OAAe,OAAO,WAAW,SAAS;IACpE;IAEA,aAAa,OAAO,MAAM;IAC1B,aAAa,OAAO,OAAO;IAC3B,aAAa,UAAU,MAAM;IAC7B,aAAa,UAAU,OAAO;GAC/B;GAEA,mBAAmB;IAClB,KAAK,MAAM,KAAK,OAAO,OAAO,KAAK,eAAe,GACjD,EAAE,OAAO;IAEV,KAAK,kBAAkB,OAAO;IAC9B,OAAO,KAAK;IACZ,OAAO,KAAK;GACb;EACD,CAAC;CACF;AACD"}
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,WAA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