maplair 4.3.0 → 4.5.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 (191) hide show
  1. package/CHANGELOG.md +151 -8
  2. package/dist/esm/control/AttributionControl.js +30 -13
  3. package/dist/esm/control/AttributionControl.js.map +1 -1
  4. package/dist/esm/control/ButtonControl.js +0 -1
  5. package/dist/esm/control/ButtonControl.js.map +1 -1
  6. package/dist/esm/control/ContextMenu.js +1 -1
  7. package/dist/esm/control/ContextMenu.js.map +1 -1
  8. package/dist/esm/control/Control.js +16 -11
  9. package/dist/esm/control/Control.js.map +1 -1
  10. package/dist/esm/control/FullscreenControl.js +0 -1
  11. package/dist/esm/control/FullscreenControl.js.map +1 -1
  12. package/dist/esm/control/LayersControl.js +46 -27
  13. package/dist/esm/control/LayersControl.js.map +1 -1
  14. package/dist/esm/control/RotateControl.js +0 -1
  15. package/dist/esm/control/RotateControl.js.map +1 -1
  16. package/dist/esm/control/ScaleControl.js +7 -8
  17. package/dist/esm/control/ScaleControl.js.map +1 -1
  18. package/dist/esm/control/SideBySideControl.js +3 -2
  19. package/dist/esm/control/SideBySideControl.js.map +1 -1
  20. package/dist/esm/control/ZoomControl.js +14 -13
  21. package/dist/esm/control/ZoomControl.js.map +1 -1
  22. package/dist/esm/core/Events.js +3 -2
  23. package/dist/esm/core/Events.js.map +1 -1
  24. package/dist/esm/core/Util.js +17 -5
  25. package/dist/esm/core/Util.js.map +1 -1
  26. package/dist/esm/dom/DomEvent.PointerEvents.js +72 -2
  27. package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -1
  28. package/dist/esm/dom/DomEvent.js +4 -7
  29. package/dist/esm/dom/DomEvent.js.map +1 -1
  30. package/dist/esm/dom/DomUtil.js +5 -5
  31. package/dist/esm/dom/DomUtil.js.map +1 -1
  32. package/dist/esm/dom/Draggable.js +10 -8
  33. package/dist/esm/dom/Draggable.js.map +1 -1
  34. package/dist/esm/geo/LatLng.js +24 -35
  35. package/dist/esm/geo/LatLng.js.map +1 -1
  36. package/dist/esm/geo/LatLngBounds.js +6 -3
  37. package/dist/esm/geo/LatLngBounds.js.map +1 -1
  38. package/dist/esm/geometry/Bounds.js +2 -2
  39. package/dist/esm/geometry/Bounds.js.map +1 -1
  40. package/dist/esm/geometry/Point.js +16 -19
  41. package/dist/esm/geometry/Point.js.map +1 -1
  42. package/dist/esm/geometry/PolyUtil.js +10 -3
  43. package/dist/esm/geometry/PolyUtil.js.map +1 -1
  44. package/dist/esm/geometry/Transformation.js +1 -7
  45. package/dist/esm/geometry/Transformation.js.map +1 -1
  46. package/dist/esm/layer/BlanketOverlay.js +3 -2
  47. package/dist/esm/layer/BlanketOverlay.js.map +1 -1
  48. package/dist/esm/layer/DivOverlay.js +25 -25
  49. package/dist/esm/layer/DivOverlay.js.map +1 -1
  50. package/dist/esm/layer/ImageOverlay.js +8 -5
  51. package/dist/esm/layer/ImageOverlay.js.map +1 -1
  52. package/dist/esm/layer/Layer.js +12 -7
  53. package/dist/esm/layer/Layer.js.map +1 -1
  54. package/dist/esm/layer/Popup.js +33 -15
  55. package/dist/esm/layer/Popup.js.map +1 -1
  56. package/dist/esm/layer/Tooltip.js +48 -56
  57. package/dist/esm/layer/Tooltip.js.map +1 -1
  58. package/dist/esm/layer/VideoOverlay.js +2 -2
  59. package/dist/esm/layer/VideoOverlay.js.map +1 -1
  60. package/dist/esm/layer/marker/DivIcon.js +4 -8
  61. package/dist/esm/layer/marker/DivIcon.js.map +1 -1
  62. package/dist/esm/layer/marker/Icon.js +37 -17
  63. package/dist/esm/layer/marker/Icon.js.map +1 -1
  64. package/dist/esm/layer/marker/Marker.Drag.js +11 -12
  65. package/dist/esm/layer/marker/Marker.Drag.js.map +1 -1
  66. package/dist/esm/layer/marker/Marker.js +54 -53
  67. package/dist/esm/layer/marker/Marker.js.map +1 -1
  68. package/dist/esm/layer/marker/SvgIcon.js +1 -1
  69. package/dist/esm/layer/marker/SvgIcon.js.map +1 -1
  70. package/dist/esm/layer/tile/GridLayer.js +29 -21
  71. package/dist/esm/layer/tile/GridLayer.js.map +1 -1
  72. package/dist/esm/layer/tile/TileLayer.js +19 -15
  73. package/dist/esm/layer/tile/TileLayer.js.map +1 -1
  74. package/dist/esm/layer/tile/WMSTileLayer.js +7 -3
  75. package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -1
  76. package/dist/esm/layer/vector/Canvas.js +45 -94
  77. package/dist/esm/layer/vector/Canvas.js.map +1 -1
  78. package/dist/esm/layer/vector/Circle.js +8 -9
  79. package/dist/esm/layer/vector/Circle.js.map +1 -1
  80. package/dist/esm/layer/vector/CircleMarker.js +15 -4
  81. package/dist/esm/layer/vector/CircleMarker.js.map +1 -1
  82. package/dist/esm/layer/vector/Path.ZoomStyles.js +4 -1
  83. package/dist/esm/layer/vector/Path.ZoomStyles.js.map +1 -1
  84. package/dist/esm/layer/vector/Path.js +10 -5
  85. package/dist/esm/layer/vector/Path.js.map +1 -1
  86. package/dist/esm/layer/vector/Polygon.js +10 -11
  87. package/dist/esm/layer/vector/Polygon.js.map +1 -1
  88. package/dist/esm/layer/vector/Polyline.js +19 -26
  89. package/dist/esm/layer/vector/Polyline.js.map +1 -1
  90. package/dist/esm/layer/vector/Renderer.getRenderer.js +2 -1
  91. package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -1
  92. package/dist/esm/layer/vector/Renderer.js +16 -0
  93. package/dist/esm/layer/vector/Renderer.js.map +1 -1
  94. package/dist/esm/layer/vector/SVG.js +3 -3
  95. package/dist/esm/layer/vector/SVG.js.map +1 -1
  96. package/dist/esm/map/Map.Rotation.js +9 -11
  97. package/dist/esm/map/Map.Rotation.js.map +1 -1
  98. package/dist/esm/map/Map.js +158 -71
  99. package/dist/esm/map/Map.js.map +1 -1
  100. package/dist/esm/map/MapSync.js +2 -5
  101. package/dist/esm/map/MapSync.js.map +1 -1
  102. package/dist/esm/map/handler/BoxZoomHandler.js +4 -0
  103. package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -1
  104. package/dist/esm/map/handler/DisplaceHandler.js +68 -30
  105. package/dist/esm/map/handler/DisplaceHandler.js.map +1 -1
  106. package/dist/esm/map/handler/DragHandler.js +7 -2
  107. package/dist/esm/map/handler/DragHandler.js.map +1 -1
  108. package/dist/esm/map/handler/KeyboardHandler.js +3 -5
  109. package/dist/esm/map/handler/KeyboardHandler.js.map +1 -1
  110. package/dist/esm/map/handler/PinchZoomHandler.js +4 -7
  111. package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -1
  112. package/dist/esm/map/handler/ScrollWheelZoomHandler.js +4 -7
  113. package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -1
  114. package/dist/esm/map/handler/TapDragZoomHandler.js +2 -5
  115. package/dist/esm/map/handler/TapDragZoomHandler.js.map +1 -1
  116. package/dist/esm/map/handler/TapHoldHandler.js +8 -20
  117. package/dist/esm/map/handler/TapHoldHandler.js.map +1 -1
  118. package/dist/esm/package.js +1 -1
  119. package/dist/maplair-global-src.js +970 -810
  120. package/dist/maplair-global-src.js.map +1 -1
  121. package/dist/maplair-global.js +2 -2
  122. package/dist/maplair-global.js.map +1 -1
  123. package/dist/maplair-src.js +970 -810
  124. package/dist/maplair-src.js.map +1 -1
  125. package/dist/maplair.css +1 -1
  126. package/dist/maplair.js +2 -2
  127. package/dist/maplair.js.map +1 -1
  128. package/i18n/en.json +1 -0
  129. package/package.json +1 -1
  130. package/src/control/AttributionControl.js +37 -11
  131. package/src/control/ButtonControl.js +0 -1
  132. package/src/control/ContextMenu.js +1 -1
  133. package/src/control/Control.js +13 -16
  134. package/src/control/FullscreenControl.js +0 -1
  135. package/src/control/LayersControl.js +61 -37
  136. package/src/control/RotateControl.js +0 -1
  137. package/src/control/ScaleControl.js +10 -8
  138. package/src/control/SideBySideControl.js +4 -2
  139. package/src/control/ZoomControl.js +11 -13
  140. package/src/core/Events.js +6 -2
  141. package/src/core/Util.js +17 -4
  142. package/src/dom/DomEvent.PointerEvents.js +68 -2
  143. package/src/dom/DomEvent.js +5 -17
  144. package/src/dom/DomUtil.js +5 -5
  145. package/src/dom/Draggable.js +11 -8
  146. package/src/geo/LatLng.js +24 -37
  147. package/src/geo/LatLngBounds.js +10 -4
  148. package/src/geometry/Bounds.js +3 -3
  149. package/src/geometry/Point.js +22 -28
  150. package/src/geometry/PolyUtil.js +12 -3
  151. package/src/geometry/Transformation.js +1 -8
  152. package/src/layer/BlanketOverlay.js +6 -3
  153. package/src/layer/DivOverlay.js +30 -32
  154. package/src/layer/ImageOverlay.js +12 -9
  155. package/src/layer/Layer.js +19 -7
  156. package/src/layer/Popup.js +41 -21
  157. package/src/layer/Tooltip.js +50 -70
  158. package/src/layer/VideoOverlay.js +2 -2
  159. package/src/layer/marker/DivIcon.js +3 -6
  160. package/src/layer/marker/Icon.js +41 -20
  161. package/src/layer/marker/Marker.Drag.js +22 -24
  162. package/src/layer/marker/Marker.js +71 -64
  163. package/src/layer/marker/SvgIcon.js +1 -1
  164. package/src/layer/tile/GridLayer.js +41 -30
  165. package/src/layer/tile/TileLayer.js +31 -21
  166. package/src/layer/tile/WMSTileLayer.js +4 -2
  167. package/src/layer/vector/Canvas.js +53 -149
  168. package/src/layer/vector/Circle.js +13 -13
  169. package/src/layer/vector/CircleMarker.js +16 -3
  170. package/src/layer/vector/Path.ZoomStyles.js +5 -3
  171. package/src/layer/vector/Path.js +11 -5
  172. package/src/layer/vector/Polygon.js +11 -10
  173. package/src/layer/vector/Polyline.js +20 -21
  174. package/src/layer/vector/Renderer.getRenderer.js +2 -1
  175. package/src/layer/vector/Renderer.js +21 -3
  176. package/src/layer/vector/SVG.js +3 -3
  177. package/src/map/Map.Rotation.js +13 -9
  178. package/src/map/Map.js +195 -83
  179. package/src/map/MapSync.js +2 -2
  180. package/src/map/handler/BoxZoomHandler.js +5 -0
  181. package/src/map/handler/DisplaceHandler.js +53 -7
  182. package/src/map/handler/DragHandler.js +5 -5
  183. package/src/map/handler/KeyboardHandler.js +5 -10
  184. package/src/map/handler/PinchZoomHandler.js +4 -4
  185. package/src/map/handler/ScrollWheelZoomHandler.js +4 -4
  186. package/src/map/handler/TapDragZoomHandler.js +2 -2
  187. package/src/map/handler/TapHoldHandler.js +13 -25
  188. package/src/maplair.css +50 -10
  189. package/dist/esm/dom/DomEvent.DoubleTap.js +0 -90
  190. package/dist/esm/dom/DomEvent.DoubleTap.js.map +0 -1
  191. package/src/dom/DomEvent.DoubleTap.js +0 -143
@@ -1 +1 @@
1
- {"version":3,"file":"MapSync.js","names":[],"sources":["../../../src/map/MapSync.js"],"sourcesContent":["/** @import {MaplairMap} from './Map.js' */\n\n/**\n * Options of `MapSync`.\n * @typedef {object} MapSyncOptions\n * @property {boolean} [bearing=true] Whether to synchronize the bearing of rotated maps\n * (with the map rotation, see `MaplairMap.setBearing()`).\n * @property {boolean} [oneWay=false] Whether only the first map leads and the other\n * maps follow it, without moving the first map when they are moved.\n * @property {'center'|'bounds'} [fit='center'] How the maps follow: `'center'` with the\n * same center and zoom, or `'bounds'` with the same center, zoomed so that the bounds\n * of the leading map fit (like `fitBounds()`), for maps of different sizes.\n */\n\n/**\n * Synchronizes the views (center, zoom and bearing) of several maps, e.g. to\n * compare different layers side by side: whenever one of the maps moves (by the\n * user or by code, including animations), the other maps follow.\n *\n * With the options `oneWay` and `fit: 'bounds'`, smaller maps can follow a main\n * map showing its bounds, e.g. thumbnails next to it.\n *\n * Inspired by the [Leaflet.Sync](https://github.com/jieter/Leaflet.Sync) plugin.\n *\n * @example\n * ```js\n * const sync = new MapSync([map1, map2]);\n * // later\n * sync.remove();\n *\n * // a thumbnail showing the bounds of the main map\n * new MapSync([map, thumbnail], {oneWay: true, fit: 'bounds'});\n * ```\n */\nexport class MapSync {\n\t/**\n\t * Synchronizes the views of the given maps, starting with the view of the first map.\n\t * @param {MaplairMap[]} maps\n\t * @param {MapSyncOptions} [options]\n\t */\n\tconstructor(maps, options) {\n\t\t/** @type {MapSyncOptions} */\n\t\tthis.options = {\n\t\t\tbearing: true,\n\t\t\toneWay: false,\n\t\t\tfit: 'center',\n\t\t\t...options\n\t\t};\n\t\tthis._maps = [...maps];\n\t\tthis._listeners = new Map();\n\t\tthis._animating = new Set();\n\t\t// following maps which ended their zoom animation already\n\t\tthis._ended = new Set();\n\n\t\tfor (const map of this._maps) {\n\t\t\tconst listeners = {\n\t\t\t\tmovestart: () => this._onMoveStart(map),\n\t\t\t\tzoomstart: () => this._onZoomStart(map),\n\t\t\t\tmove: () => this._onMove(map),\n\t\t\t\tzoomanim: e => this._onZoomAnim(map, e),\n\t\t\t\tmoveend: () => this._onMoveEnd(map),\n\t\t\t\trotate: () => this._onRotate(map)\n\t\t\t};\n\t\t\tmap.on(listeners);\n\t\t\tthis._listeners.set(map, listeners);\n\t\t}\n\n\t\tif (this._maps[0]?._loaded) {\n\t\t\tthis._syncAll(this._maps[0]);\n\t\t}\n\t}\n\n\t/**\n\t * Stops synchronizing the maps.\n\t * @returns {this}\n\t */\n\tremove() {\n\t\tfor (const [map, listeners] of this._listeners) {\n\t\t\tmap.off(listeners);\n\t\t}\n\t\tthis._listeners.clear();\n\t\tthis._maps = [];\n\t\treturn this;\n\t}\n\n\t// The other maps, which follow `map`\n\t_followers(map) {\n\t\treturn this._maps.filter(m => m !== map && m._loaded);\n\t}\n\n\t// Whether `map` may lead, see the `oneWay` option\n\t_canLead(map) {\n\t\treturn !this.options.oneWay || map === this._maps[0];\n\t}\n\n\t// The zoom of `other` following `map` at `zoom`, see the `fit` option; snapped to\n\t// the `zoomSnap` of `other` at the `end` of a movement\n\t_zoomFor(other, map, zoom = map.getZoom(), end = true) {\n\t\tconst size = other.getSize(),\n\t\tleaderSize = map.getSize();\n\t\tif (this.options.fit !== 'bounds' || !size.x || !size.y || !leaderSize.x || !leaderSize.y) { return zoom; }\n\t\tlet z = zoom + Math.log2(Math.min(size.x / leaderSize.x, size.y / leaderSize.y));\n\t\tconst snap = other.options.zoomSnap;\n\t\tif (end && snap) {\n\t\t\t// down, so that the bounds fit\n\t\t\tz = Math.floor(z / snap + 1e-6) * snap;\n\t\t}\n\t\treturn Math.max(other.getMinZoom(), Math.min(other.getMaxZoom(), z));\n\t}\n\n\t// A map starting to move leads until it (and all following animations) ended,\n\t// the events of the following maps are ignored meanwhile.\n\t_onMoveStart(map) {\n\t\tif (this._leader || !this._canLead(map)) { return; }\n\t\tthis._leader = map;\n\t\tthis._leaderEnded = false;\n\t\tthis._ended.clear();\n\t\tfor (const other of this._followers(map)) {\n\t\t\tother._stop();\n\t\t\tother._syncStartZoom = other.getZoom();\n\t\t\tother.fire('movestart');\n\t\t}\n\t}\n\n\t_onZoomStart(map) {\n\t\t// `_resetView()` fires zoomstart before movestart\n\t\tthis._onMoveStart(map);\n\t\tif (this._leader !== map) { return; }\n\t\tfor (const other of this._followers(map)) {\n\t\t\tother.fire('zoomstart');\n\t\t}\n\t}\n\n\t_onMove(map) {\n\t\tif (!this._leader) {\n\t\t\t// e.g. invalidateSize(), which fires move without movestart\n\t\t\tif (this._canLead(map)) {\n\t\t\t\tthis._syncAll(map);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (this._leader !== map) { return; }\n\t\tfor (const other of this._followers(map)) {\n\t\t\tif (!this._animating.has(other)) {\n\t\t\t\tthis._follow(other, map, true);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Mirrors the CSS zoom animation, the following map ends it itself\n\t_onZoomAnim(map, e) {\n\t\tif (this._leader !== map) { return; }\n\t\tfor (const other of this._followers(map)) {\n\t\t\tif (other._zoomAnimated) {\n\t\t\t\tthis._animating.add(other);\n\t\t\t\tother._animateZoom(e.center, this._zoomFor(other, map, e.zoom), true, e.noUpdate);\n\t\t\t}\n\t\t}\n\t}\n\n\t_onMoveEnd(map) {\n\t\tif (this._leader !== map) {\n\t\t\t// the end of a following zoom animation\n\t\t\tif (this._animating.delete(map)) {\n\t\t\t\tthis._ended.add(map);\n\t\t\t\tif (this._leaderEnded && !this._animating.size) {\n\t\t\t\t\tthis._leader = null;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tfor (const other of this._followers(map)) {\n\t\t\tif (!this._animating.has(other) && !this._ended.has(other)) {\n\t\t\t\tthis._follow(other, map, false);\n\t\t\t\tother._moveEnd(other.getZoom() !== other._syncStartZoom);\n\t\t\t}\n\t\t}\n\t\tthis._leaderEnded = true;\n\t\tif (!this._animating.size) {\n\t\t\tthis._leader = null;\n\t\t}\n\t}\n\n\t_onRotate(map) {\n\t\tif (!this.options.bearing || !this._canLead(map)) { return; }\n\t\tfor (const other of this._followers(map)) {\n\t\t\tif (other._rotate && other.getBearing() !== map.getBearing()) {\n\t\t\t\tother._rotate(map.getBearing());\n\t\t\t}\n\t\t}\n\t}\n\n\t// Moves `other` to the view of `map`, during or at the end of a movement\n\t_follow(other, map, during) {\n\t\tconst center = map.getCenter(),\n\t\tzoom = this._zoomFor(other, map, map.getZoom(), !during);\n\t\tif (zoom === other.getZoom()) {\n\t\t\t// panning, by whole pixels like dragging\n\t\t\tconst offset = other.project(center, zoom).subtract(other.project(other.getCenter(), zoom)).round();\n\t\t\tif (offset.x || offset.y) {\n\t\t\t\tother._rawPanBy(offset);\n\t\t\t\tother.fire('move');\n\t\t\t}\n\t\t} else {\n\t\t\t// zooming frame by frame (pinch, flyTo, continuousZoom) or at the end\n\t\t\tother._move(center, zoom, during ? {pinch: true, round: false} : undefined);\n\t\t}\n\t}\n\n\t// Sets the view of all other maps to the one of `map`, without animation\n\t_syncAll(map) {\n\t\tfor (const other of this._maps.filter(m => m !== map)) {\n\t\t\tif (this.options.bearing && other._rotate && other.getBearing() !== map.getBearing()) {\n\t\t\t\tother._rotate(map.getBearing());\n\t\t\t}\n\t\t\tconst zoom = this._zoomFor(other, map);\n\t\t\tif (!other._loaded || other.getZoom() !== zoom ||\n\t\t\t\tother.latLngToContainerPoint(map.getCenter()).distanceTo(other.getSize()._divideBy(2)) >= 1) {\n\t\t\t\tthis._leader = map;\n\t\t\t\tother.setView(map.getCenter(), zoom, {anim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SAAS;GAAC,OAAO;GAAM,OAAO;EAAK,IAAI,KAAA,CAAS;CAE5E;CAGA,SAAS,KAAK;EACb,KAAK,MAAM,SAAS,KAAK,MAAM,QAAO,MAAK,MAAM,GAAG,GAAG;GACtD,IAAI,KAAK,QAAQ,WAAW,MAAM,WAAW,MAAM,WAAW,MAAM,IAAI,WAAW,GAClF,MAAM,QAAQ,IAAI,WAAW,CAAC;GAE/B,MAAM,OAAO,KAAK,SAAS,OAAO,GAAG;GACrC,IAAI,CAAC,MAAM,WAAW,MAAM,QAAQ,MAAM,QACzC,MAAM,uBAAuB,IAAI,UAAU,CAAC,CAAC,CAAC,WAAW,MAAM,QAAQ,CAAC,CAAC,UAAU,CAAC,CAAC,KAAK,GAAG;IAC7F,KAAK,UAAU;IACf,MAAM,QAAQ,IAAI,UAAU,GAAG,MAAM,EAAC,SAAS,MAAK,CAAC;IACrD,KAAK,UAAU;GAChB;EACD;CACD;AACD"}
1
+ {"version":3,"file":"MapSync.js","names":[],"sources":["../../../src/map/MapSync.js"],"sourcesContent":["/** @import {MaplairMap} from './Map.js' */\n\n/**\n * Options of `MapSync`.\n * @typedef {object} MapSyncOptions\n * @property {boolean} [bearing=true] Whether to synchronize the bearing of rotated maps\n * (with the map rotation, see `MaplairMap.setBearing()`).\n * @property {boolean} [oneWay=false] Whether only the first map leads and the other\n * maps follow it, without moving the first map when they are moved.\n * @property {'center'|'bounds'} [fit='center'] How the maps follow: `'center'` with the\n * same center and zoom, or `'bounds'` with the same center, zoomed so that the bounds\n * of the leading map fit (like `fitBounds()`), for maps of different sizes.\n */\n\n/**\n * Synchronizes the views (center, zoom and bearing) of several maps, e.g. to\n * compare different layers side by side: whenever one of the maps moves (by the\n * user or by code, including animations), the other maps follow.\n *\n * With the options `oneWay` and `fit: 'bounds'`, smaller maps can follow a main\n * map showing its bounds, e.g. thumbnails next to it.\n *\n * Inspired by the [Leaflet.Sync](https://github.com/jieter/Leaflet.Sync) plugin.\n *\n * @example\n * ```js\n * const sync = new MapSync([map1, map2]);\n * // later\n * sync.remove();\n *\n * // a thumbnail showing the bounds of the main map\n * new MapSync([map, thumbnail], {oneWay: true, fit: 'bounds'});\n * ```\n */\nexport class MapSync {\n\t/**\n\t * Synchronizes the views of the given maps, starting with the view of the first map.\n\t * @param {MaplairMap[]} maps\n\t * @param {MapSyncOptions} [options]\n\t */\n\tconstructor(maps, options) {\n\t\t/** @type {MapSyncOptions} */\n\t\tthis.options = {\n\t\t\tbearing: true,\n\t\t\toneWay: false,\n\t\t\tfit: 'center',\n\t\t\t...options\n\t\t};\n\t\tthis._maps = [...maps];\n\t\tthis._listeners = new Map();\n\t\tthis._animating = new Set();\n\t\t// following maps which ended their zoom animation already\n\t\tthis._ended = new Set();\n\n\t\tfor (const map of this._maps) {\n\t\t\tconst listeners = {\n\t\t\t\tmovestart: () => this._onMoveStart(map),\n\t\t\t\tzoomstart: () => this._onZoomStart(map),\n\t\t\t\tmove: () => this._onMove(map),\n\t\t\t\tzoomanim: e => this._onZoomAnim(map, e),\n\t\t\t\tmoveend: () => this._onMoveEnd(map),\n\t\t\t\trotate: () => this._onRotate(map)\n\t\t\t};\n\t\t\tmap.on(listeners);\n\t\t\tthis._listeners.set(map, listeners);\n\t\t}\n\n\t\tif (this._maps[0]?._loaded) {\n\t\t\tthis._syncAll(this._maps[0]);\n\t\t}\n\t}\n\n\t/**\n\t * Stops synchronizing the maps.\n\t * @returns {this}\n\t */\n\tremove() {\n\t\tfor (const [map, listeners] of this._listeners) {\n\t\t\tmap.off(listeners);\n\t\t}\n\t\tthis._listeners.clear();\n\t\tthis._maps = [];\n\t\treturn this;\n\t}\n\n\t// The other maps, which follow `map`\n\t_followers(map) {\n\t\treturn this._maps.filter(m => m !== map && m._loaded);\n\t}\n\n\t// Whether `map` may lead, see the `oneWay` option\n\t_canLead(map) {\n\t\treturn !this.options.oneWay || map === this._maps[0];\n\t}\n\n\t// The zoom of `other` following `map` at `zoom`, see the `fit` option; snapped to\n\t// the `zoomSnap` of `other` at the `end` of a movement\n\t_zoomFor(other, map, zoom = map.getZoom(), end = true) {\n\t\tconst size = other.getSize(),\n\t\tleaderSize = map.getSize();\n\t\tif (this.options.fit !== 'bounds' || !size.x || !size.y || !leaderSize.x || !leaderSize.y) { return zoom; }\n\t\tlet z = zoom + Math.log2(Math.min(size.x / leaderSize.x, size.y / leaderSize.y));\n\t\tconst snap = other.options.zoomSnap;\n\t\tif (end && snap) {\n\t\t\t// down, so that the bounds fit\n\t\t\tz = Math.floor(z / snap + 1e-6) * snap;\n\t\t}\n\t\treturn Math.max(other.getMinZoom(), Math.min(other.getMaxZoom(), z));\n\t}\n\n\t// A map starting to move leads until it (and all following animations) ended,\n\t// the events of the following maps are ignored meanwhile.\n\t_onMoveStart(map) {\n\t\tif (this._leader || !this._canLead(map)) { return; }\n\t\tthis._leader = map;\n\t\tthis._leaderEnded = false;\n\t\tthis._ended.clear();\n\t\tfor (const other of this._followers(map)) {\n\t\t\tother._stop();\n\t\t\tother._syncStartZoom = other.getZoom();\n\t\t\tother.fire('movestart');\n\t\t}\n\t}\n\n\t_onZoomStart(map) {\n\t\t// `_resetView()` fires zoomstart before movestart\n\t\tthis._onMoveStart(map);\n\t\tif (this._leader !== map) { return; }\n\t\tfor (const other of this._followers(map)) {\n\t\t\tother.fire('zoomstart');\n\t\t}\n\t}\n\n\t_onMove(map) {\n\t\tif (!this._leader) {\n\t\t\t// e.g. invalidateSize(), which fires move without movestart\n\t\t\tif (this._canLead(map)) {\n\t\t\t\tthis._syncAll(map);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (this._leader !== map) { return; }\n\t\tfor (const other of this._followers(map)) {\n\t\t\tif (!this._animating.has(other)) {\n\t\t\t\tthis._follow(other, map, true);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Mirrors the CSS zoom animation, the following map ends it itself\n\t_onZoomAnim(map, e) {\n\t\tif (this._leader !== map) { return; }\n\t\tfor (const other of this._followers(map)) {\n\t\t\tif (other._zoomAnimated) {\n\t\t\t\tthis._animating.add(other);\n\t\t\t\tother._animateZoom(e.center, this._zoomFor(other, map, e.zoom), e.noUpdate);\n\t\t\t}\n\t\t}\n\t}\n\n\t_onMoveEnd(map) {\n\t\tif (this._leader !== map) {\n\t\t\t// the end of a following zoom animation\n\t\t\tif (this._animating.delete(map)) {\n\t\t\t\tthis._ended.add(map);\n\t\t\t\tif (this._leaderEnded && !this._animating.size) {\n\t\t\t\t\tthis._leader = null;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tfor (const other of this._followers(map)) {\n\t\t\tif (!this._animating.has(other) && !this._ended.has(other)) {\n\t\t\t\tthis._follow(other, map, false);\n\t\t\t\tother._moveEnd(other.getZoom() !== other._syncStartZoom);\n\t\t\t}\n\t\t}\n\t\tthis._leaderEnded = true;\n\t\tif (!this._animating.size) {\n\t\t\tthis._leader = null;\n\t\t}\n\t}\n\n\t_onRotate(map) {\n\t\tif (!this.options.bearing || !this._canLead(map)) { return; }\n\t\tfor (const other of this._followers(map)) {\n\t\t\tif (other._rotate && other.getBearing() !== map.getBearing()) {\n\t\t\t\tother._rotate(map.getBearing());\n\t\t\t}\n\t\t}\n\t}\n\n\t// Moves `other` to the view of `map`, during or at the end of a movement\n\t_follow(other, map, during) {\n\t\tconst center = map.getCenter(),\n\t\tzoom = this._zoomFor(other, map, map.getZoom(), !during);\n\t\tif (zoom === other.getZoom()) {\n\t\t\t// panning, by whole pixels like dragging\n\t\t\tconst offset = other.project(center, zoom).subtract(other.project(other.getCenter(), zoom)).round();\n\t\t\tif (offset.x || offset.y) {\n\t\t\t\tother._rawPanBy(offset);\n\t\t\t\tother.fire('move');\n\t\t\t}\n\t\t} else {\n\t\t\t// zooming frame by frame (pinch, flyTo, continuousZoom) or at the end\n\t\t\tother._move(center, zoom, during ? {pinch: true} : undefined);\n\t\t}\n\t}\n\n\t// Sets the view of all other maps to the one of `map`, without animation\n\t_syncAll(map) {\n\t\tfor (const other of this._maps.filter(m => m !== map)) {\n\t\t\tif (this.options.bearing && other._rotate && other.getBearing() !== map.getBearing()) {\n\t\t\t\tother._rotate(map.getBearing());\n\t\t\t}\n\t\t\tconst zoom = this._zoomFor(other, map);\n\t\t\tif (!other._loaded || other.getZoom() !== zoom ||\n\t\t\t\tother.latLngToContainerPoint(map.getCenter()).distanceTo(other.getSize()._divideBy(2)) >= 1) {\n\t\t\t\tthis._leader = map;\n\t\t\t\tother.setView(map.getCenter(), zoom, {animate: false});\n\t\t\t\tthis._leader = null;\n\t\t\t}\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,UAAb,MAAqB;;;;;;CAMpB,YAAY,MAAM,SAAS;;EAE1B,KAAK,UAAU;GACd,SAAS;GACT,QAAQ;GACR,KAAK;GACL,GAAG;EACJ;EACA,KAAK,QAAQ,CAAC,GAAG,IAAI;EACrB,KAAK,6BAAa,IAAI,IAAI;EAC1B,KAAK,6BAAa,IAAI,IAAI;EAE1B,KAAK,yBAAS,IAAI,IAAI;EAEtB,KAAK,MAAM,OAAO,KAAK,OAAO;GAC7B,MAAM,YAAY;IACjB,iBAAiB,KAAK,aAAa,GAAG;IACtC,iBAAiB,KAAK,aAAa,GAAG;IACtC,YAAY,KAAK,QAAQ,GAAG;IAC5B,WAAU,MAAK,KAAK,YAAY,KAAK,CAAC;IACtC,eAAe,KAAK,WAAW,GAAG;IAClC,cAAc,KAAK,UAAU,GAAG;GACjC;GACA,IAAI,GAAG,SAAS;GAChB,KAAK,WAAW,IAAI,KAAK,SAAS;EACnC;EAEA,IAAI,KAAK,MAAM,EAAE,EAAE,SAClB,KAAK,SAAS,KAAK,MAAM,EAAE;CAE7B;;;;;CAMA,SAAS;EACR,KAAK,MAAM,CAAC,KAAK,cAAc,KAAK,YACnC,IAAI,IAAI,SAAS;EAElB,KAAK,WAAW,MAAM;EACtB,KAAK,QAAQ,CAAC;EACd,OAAO;CACR;CAGA,WAAW,KAAK;EACf,OAA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AI,SAAS,MAAM,QAAQ,GAAG;GAE7B,MAAM,SAAS,MAAM,QAAQ,QAAQ,IAAI,CAAC,CAAC,SAAS,MAAM,QAAQ,MAAM,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC,MAAM;GAClG,IAAI,OAAO,KAAK,OAAO,GAAG;IACzB,MAAM,UAAU,MAAM;IACtB,MAAM,KAAK,MAAM;GAClB;EACD,OAEC,MAAM,MAAM,QAAQ,MAAM,SAAS,EAAC,OAAO,KAAI,IAAI,KAAA,CAAS;CAE9D;CAGA,SAAS,KAAK;EACb,KAAK,MAAM,SAAS,KAAK,MAAM,QAAO,MAAK,MAAM,GAAG,GAAG;GACtD,IAAI,KAAK,QAAQ,WAAW,MAAM,WAAW,MAAM,WAAW,MAAM,IAAI,WAAW,GAClF,MAAM,QAAQ,IAAI,WAAW,CAAC;GAE/B,MAAM,OAAO,KAAK,SAAS,OAAO,GAAG;GACrC,IAAI,CAAC,MAAM,WAAW,MAAM,QAAQ,MAAM,QACzC,MAAM,uBAAuB,IAAI,UAAU,CAAC,CAAC,CAAC,WAAW,MAAM,QAAQ,CAAC,CAAC,UAAU,CAAC,CAAC,KAAK,GAAG;IAC7F,KAAK,UAAU;IACf,MAAM,QAAQ,IAAI,UAAU,GAAG,MAAM,EAAC,SAAS,MAAK,CAAC;IACrD,KAAK,UAAU;GAChB;EACD;CACD;AACD"}
@@ -47,12 +47,16 @@ var BoxZoomHandler = class BoxZoomHandler extends Handler {
47
47
  */
48
48
  addHooks() {
49
49
  on(this._map._container, "pointerdown", this._onPointerDown, this);
50
+ this._map._container.classList.add("maplair-box-zoom");
50
51
  }
51
52
  /**
52
53
  * Called when the handler is disabled, removes the event hooks.
53
54
  */
54
55
  removeHooks() {
55
56
  off(this._map._container, "pointerdown", this._onPointerDown, this);
57
+ this._map._container.classList.remove("maplair-box-zoom");
58
+ this._finish();
59
+ this._resetState();
56
60
  }
57
61
  /**
58
62
  * Returns `true` if a box has been drawn by the current or last interaction.
@@ -1 +1 @@
1
- {"version":3,"file":"BoxZoomHandler.js","names":["DomEvent.stop","DomEvent.on","DomUtil.create","DomEvent.off"],"sources":["../../../../src/map/handler/BoxZoomHandler.js"],"sourcesContent":["import {Handler} from '../../core/Handler.js';\nimport * as DomUtil from '../../dom/DomUtil.js';\nimport * as DomEvent from '../../dom/DomEvent.js';\nimport {LatLngBounds} from '../../geo/LatLngBounds.js';\nimport {Bounds} from '../../geometry/Bounds.js';\n/** @import {MaplairMap} from '../Map.js' */\n/** @import {MaplairEvent} from '../../core/Events.js' */\n\n/**\n * The event object of `boxzoomend`.\n * @typedef {object} BoxZoomEndEvent\n * @property {LatLngBounds} boxZoomBounds The bounds of the box the map was zoomed to.\n * @extends MaplairEvent\n */\n\n/**\n * Map options of `BoxZoomHandler`.\n * @typedef {object} BoxZoomHandlerMapOptions\n * @lends MaplairMapOptions\n * @property {boolean} [boxZoom=true] Whether the map can be zoomed to a rectangular\n * area specified by dragging the pointer while pressing the shift key.\n */\n\n/**\n * Handler of `MaplairMap` for box zoom.\n * @typedef {object} BoxZoomHandlerMapProperties\n * @lends MaplairMap\n * @property {BoxZoomHandler} boxZoom Box (shift-drag with pointer) zoom handler.\n */\n\n/**\n * Adds shift-drag zoom interaction to the map (zoom to a selected bounding box),\n * enabled by default, see the map option `boxZoom`.\n *\n * @fires MaplairMap#boxzoomstart {MaplairEvent} Fired when the user starts drawing\n * a box to zoom to.\n * @fires MaplairMap#boxzoomend {BoxZoomEndEvent} Fired when the user has drawn a box\n * and the map is zoomed to it.\n */\nexport class BoxZoomHandler extends Handler {\n\t/**\n\t * @param {MaplairMap} map\n\t */\n\tconstructor(map) {\n\t\tsuper(map);\n\t\tthis.callInitHooks(BoxZoomHandler);\n\t}\n\n\t/**\n\t * Called when the handler is enabled, adds the event hooks.\n\t */\n\taddHooks() {\n\t\tDomEvent.on(this._map._container, 'pointerdown', this._onPointerDown, this);\n\t}\n\n\t/**\n\t * Called when the handler is disabled, removes the event hooks.\n\t */\n\tremoveHooks() {\n\t\tDomEvent.off(this._map._container, 'pointerdown', this._onPointerDown, this);\n\t}\n\n\t/**\n\t * Returns `true` if a box has been drawn by the current or last interaction.\n\t * @returns {boolean}\n\t */\n\tmoved() {\n\t\treturn this._moved;\n\t}\n\n\t_resetState() {\n\t\tclearTimeout(this._resetStateTimeout);\n\t\tthis._moved = false;\n\t}\n\n\t_toggleListeners(onOff) {\n\t\tonOff(this._map._container, {\n\t\t\tcontextmenu: DomEvent.stop,\n\t\t\tpointermove: this._onPointerMove,\n\t\t\tpointerup: this._onPointerUp\n\t\t}, this);\n\t\tonOff(document, 'keydown', this._onKeyDown, this);\n\t}\n\n\t_onPointerDown(e) {\n\t\tif (!e.shiftKey || (e.button !== 0)) { return false; }\n\n\t\t// also cancels a deferred reset that hasn't run yet, which would interrupt the interaction and orphan a box\n\t\tthis._resetState();\n\n\t\t// stops both text selection and native image drag in every engine without restyling the page; it also\n\t\t// suppresses compatibility mouse events, which is fine for a press that is already a box zoom gesture\n\t\tDomEvent.preventDefault(e);\n\n\t\tthis._startPoint = this._map.pointerEventToContainerPoint(e);\n\n\t\tthis._toggleListeners(DomEvent.on);\n\t}\n\n\t_onPointerMove(e) {\n\t\tconst map = this._map;\n\n\t\tif (!this._moved) {\n\t\t\tthis._moved = true;\n\n\t\t\tthis._box = DomUtil.create('div', 'maplair-zoom-box', map._container);\n\t\t\tmap._container.classList.add('maplair-crosshair');\n\n\t\t\tmap.fire('boxzoomstart');\n\t\t}\n\n\t\tthis._point = map.pointerEventToContainerPoint(e);\n\n\t\tconst bounds = new Bounds(this._point, this._startPoint),\n\t\tsize = bounds.getSize();\n\n\t\tDomUtil.setPosition(this._box, bounds.min);\n\n\t\tthis._box.style.width = `${size.x}px`;\n\t\tthis._box.style.height = `${size.y}px`;\n\t}\n\n\t_finish() {\n\t\tif (this._moved) {\n\t\t\tthis._box.remove();\n\t\t\tthis._map._container.classList.remove('maplair-crosshair');\n\t\t}\n\n\t\tthis._toggleListeners(DomEvent.off);\n\t}\n\n\t_onPointerUp(e) {\n\t\tif (e.button !== 0) { return; }\n\n\t\tthis._finish();\n\n\t\tif (!this._moved) { return; }\n\t\t// Postpone to next JS tick so internal click event handling\n\t\t// still see it as \"moved\".\n\t\tthis._resetStateTimeout = setTimeout(() => this._resetState(), 0);\n\n\t\tconst bounds = new LatLngBounds(\n\t\t\tthis._map.containerPointToLatLng(this._startPoint),\n\t\t\tthis._map.containerPointToLatLng(this._point));\n\n\t\tthis._map._setUserEvent(e);\n\t\t// a tiny box can collapse to a single LatLng at very high zoom, which fits at an Infinity zoom when no layer\n\t\t// limits it; unlike fitBounds before a tile layer is added, no layer will come along to clamp it (#6709)\n\t\tif (this._map.getBoundsZoom(bounds) !== Infinity) {\n\t\t\tthis._map.fitBounds(bounds);\n\t\t}\n\t\tthis._map.fire('boxzoomend', {boxZoomBounds: bounds});\n\t}\n\n\t_onKeyDown(e) {\n\t\tif (e.code === 'Escape') {\n\t\t\tthis._finish();\n\t\t\tthis._resetState();\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.mergeOptions({\n\t\t\tboxZoom: true\n\t\t});\n\n\t\tMaplairMap.addInitHook('addHandler', 'boxZoom', BoxZoomHandler);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,IAAa,iBAAb,MAAa,uBAAuB,QAAQ;;;;CAI3C,YAAY,KAAK;EAChB,MAAM,GAAG;EACT,KAAK,cAAc,cAAc;CAClC;;;;CAKA,WAAW;EACV,GAAY,KAAK,KAAK,YAAY,eAAe,KAAK,gBAAgB,IAAI;CAC3E;;;;CAKA,cAAc;EACb,IAAa,KAAK,KAAK,YAAY,eAAe,KAAK,gBAAgB,IAAI;CAC5E;;;;;CAMA,QAAQ;EACP,OAAO,KAAK;CACb;CAEA,cAAc;EACb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,WAAW,GACjD,KAAK,KAAK,uBAAuB,KAAK,MAAM,CAAC;EAE9C,KAAK,KAAK,cAAc,CAAC;EAGzB,IAAI,KAAK,KAAK,cAAc,MAAM,MAAM,UACvC,KAAK,KAAK,UAAU,MAAM;EAE3B,KAAK,KAAK,KAAK,cAAc,EAAC,eAAe,OAAM,CAAC;CACrD;CAEA,WAAW,GAAG;EACb,IAAI,EAAE,SAAS,UAAU;GACxB,KAAK,QAAQ;GACb,KAAK,YAAY;EAClB;CACD;;;;;;CAOA,OAAO,SAAS,YAAY;EAC3B,WAAW,aAAa,EACvB,SAAS,KACV,CAAC;EAED,WAAW,YAAY,cAAc,WAAW,cAAc;CAC/D;AACD"}
1
+ {"version":3,"file":"BoxZoomHandler.js","names":["DomEvent.stop","DomEvent.on","DomUtil.create","DomEvent.off"],"sources":["../../../../src/map/handler/BoxZoomHandler.js"],"sourcesContent":["import {Handler} from '../../core/Handler.js';\nimport * as DomUtil from '../../dom/DomUtil.js';\nimport * as DomEvent from '../../dom/DomEvent.js';\nimport {LatLngBounds} from '../../geo/LatLngBounds.js';\nimport {Bounds} from '../../geometry/Bounds.js';\n/** @import {MaplairMap} from '../Map.js' */\n/** @import {MaplairEvent} from '../../core/Events.js' */\n\n/**\n * The event object of `boxzoomend`.\n * @typedef {object} BoxZoomEndEvent\n * @property {LatLngBounds} boxZoomBounds The bounds of the box the map was zoomed to.\n * @extends MaplairEvent\n */\n\n/**\n * Map options of `BoxZoomHandler`.\n * @typedef {object} BoxZoomHandlerMapOptions\n * @lends MaplairMapOptions\n * @property {boolean} [boxZoom=true] Whether the map can be zoomed to a rectangular\n * area specified by dragging the pointer while pressing the shift key.\n */\n\n/**\n * Handler of `MaplairMap` for box zoom.\n * @typedef {object} BoxZoomHandlerMapProperties\n * @lends MaplairMap\n * @property {BoxZoomHandler} boxZoom Box (shift-drag with pointer) zoom handler.\n */\n\n/**\n * Adds shift-drag zoom interaction to the map (zoom to a selected bounding box),\n * enabled by default, see the map option `boxZoom`.\n *\n * @fires MaplairMap#boxzoomstart {MaplairEvent} Fired when the user starts drawing\n * a box to zoom to.\n * @fires MaplairMap#boxzoomend {BoxZoomEndEvent} Fired when the user has drawn a box\n * and the map is zoomed to it.\n */\nexport class BoxZoomHandler extends Handler {\n\t/**\n\t * @param {MaplairMap} map\n\t */\n\tconstructor(map) {\n\t\tsuper(map);\n\t\tthis.callInitHooks(BoxZoomHandler);\n\t}\n\n\t/**\n\t * Called when the handler is enabled, adds the event hooks.\n\t */\n\taddHooks() {\n\t\tDomEvent.on(this._map._container, 'pointerdown', this._onPointerDown, this);\n\t\tthis._map._container.classList.add('maplair-box-zoom');\n\t}\n\n\t/**\n\t * Called when the handler is disabled, removes the event hooks.\n\t */\n\tremoveHooks() {\n\t\tDomEvent.off(this._map._container, 'pointerdown', this._onPointerDown, this);\n\t\tthis._map._container.classList.remove('maplair-box-zoom');\n\t\t// cancel a box being drawn, as Escape does\n\t\tthis._finish();\n\t\tthis._resetState();\n\t}\n\n\t/**\n\t * Returns `true` if a box has been drawn by the current or last interaction.\n\t * @returns {boolean}\n\t */\n\tmoved() {\n\t\treturn this._moved;\n\t}\n\n\t_resetState() {\n\t\tclearTimeout(this._resetStateTimeout);\n\t\tthis._moved = false;\n\t}\n\n\t_toggleListeners(onOff) {\n\t\tonOff(this._map._container, {\n\t\t\tcontextmenu: DomEvent.stop,\n\t\t\tpointermove: this._onPointerMove,\n\t\t\tpointerup: this._onPointerUp\n\t\t}, this);\n\t\tonOff(document, 'keydown', this._onKeyDown, this);\n\t}\n\n\t_onPointerDown(e) {\n\t\tif (!e.shiftKey || (e.button !== 0)) { return false; }\n\n\t\t// also cancels a deferred reset that hasn't run yet, which would interrupt the interaction and orphan a box\n\t\tthis._resetState();\n\n\t\t// stops both text selection and native image drag in every engine without restyling the page; it also\n\t\t// suppresses compatibility mouse events, which is fine for a press that is already a box zoom gesture\n\t\tDomEvent.preventDefault(e);\n\n\t\tthis._startPoint = this._map.pointerEventToContainerPoint(e);\n\n\t\tthis._toggleListeners(DomEvent.on);\n\t}\n\n\t_onPointerMove(e) {\n\t\tconst map = this._map;\n\n\t\tif (!this._moved) {\n\t\t\tthis._moved = true;\n\n\t\t\tthis._box = DomUtil.create('div', 'maplair-zoom-box', map._container);\n\t\t\tmap._container.classList.add('maplair-crosshair');\n\n\t\t\tmap.fire('boxzoomstart');\n\t\t}\n\n\t\tthis._point = map.pointerEventToContainerPoint(e);\n\n\t\tconst bounds = new Bounds(this._point, this._startPoint),\n\t\tsize = bounds.getSize();\n\n\t\tDomUtil.setPosition(this._box, bounds.min);\n\n\t\tthis._box.style.width = `${size.x}px`;\n\t\tthis._box.style.height = `${size.y}px`;\n\t}\n\n\t_finish() {\n\t\tif (this._moved) {\n\t\t\tthis._box.remove();\n\t\t\tthis._map._container.classList.remove('maplair-crosshair');\n\t\t}\n\n\t\tthis._toggleListeners(DomEvent.off);\n\t}\n\n\t_onPointerUp(e) {\n\t\tif (e.button !== 0) { return; }\n\n\t\tthis._finish();\n\n\t\tif (!this._moved) { return; }\n\t\t// Postpone to next JS tick so internal click event handling\n\t\t// still see it as \"moved\".\n\t\tthis._resetStateTimeout = setTimeout(() => this._resetState(), 0);\n\n\t\tconst bounds = new LatLngBounds(\n\t\t\tthis._map.containerPointToLatLng(this._startPoint),\n\t\t\tthis._map.containerPointToLatLng(this._point));\n\n\t\tthis._map._setUserEvent(e);\n\t\t// a tiny box can collapse to a single LatLng at very high zoom, which fits at an Infinity zoom when no layer\n\t\t// limits it; unlike fitBounds before a tile layer is added, no layer will come along to clamp it (#6709)\n\t\tif (this._map.getBoundsZoom(bounds) !== Infinity) {\n\t\t\tthis._map.fitBounds(bounds);\n\t\t}\n\t\tthis._map.fire('boxzoomend', {boxZoomBounds: bounds});\n\t}\n\n\t_onKeyDown(e) {\n\t\tif (e.code === 'Escape') {\n\t\t\tthis._finish();\n\t\t\tthis._resetState();\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.mergeOptions({\n\t\t\tboxZoom: true\n\t\t});\n\n\t\tMaplairMap.addInitHook('addHandler', 'boxZoom', BoxZoomHandler);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,IAAa,iBAAb,MAAa,uBAAuB,QAAQ;;;;CAI3C,YAAY,KAAK;EAChB,MAAM,GAAG;EACT,KAAK,cAAc,cAAc;CAClC;;;;CAKA,WAAW;EACV,GAAY,KAAK,KAAK,YAAY,eAAe,KAAK,gBAAgB,IAAI;EAC1E,KAAK,KAAK,WAAW,UAAU,IAAI,kBAAkB;CACtD;;;;CAKA,cAAc;EACb,IAAa,KAAK,KAAK,YAAY,eAAe,KAAK,gBAAgB,IAAI;EAC3E,KAAK,KAAK,WAAW,UAAU,OAAO,kBAAkB;EAExD,KAAK,QAAQ;EACb,KAAK,YAAY;CAClB;;;;;CAMA,QAAQ;EACP,OAAO,KAAK;CACb;CAEA,cAAc;EACb,aAAa,KAAK,kBAAkB;EACpC,KAAK,SAAS;CACf;CAEA,iBAAiB,OAAO;EACvB,MAAM,KAAK,KAAK,YAAY;GAC3B,aAAaA;GACb,aAAa,KAAK;GAClB,WAAW,KAAK;EACjB,GAAG,IAAI;EACP,MAAM,UAAU,WAAW,KAAK,YAAY,IAAI;CACjD;CAEA,eAAe,GAAG;EACjB,IAAI,CAAC,EAAE,YAAa,EAAE,WAAW,GAAM,OAAO;EAG9C,KAAK,YAAY;EAIjB,eAAwB,CAAC;EAEzB,KAAK,cAAc,KAAK,KAAK,6BAA6B,CAAC;EAE3D,KAAK,iBAAiBC,EAAW;CAClC;CAEA,eAAe,GAAG;EACjB,MAAM,MAAM,KAAK;EAEjB,IAAI,CAAC,KAAK,QAAQ;GACjB,KAAK,SAAS;GAEd,KAAK,OAAOC,OAAe,OAAO,oBAAoB,IAAI,UAAU;GACpE,IAAI,WAAW,UAAU,IAAI,mBAAmB;GAEhD,IAAI,KAAK,cAAc;EACxB;EAEA,KAAK,SAAS,IAAI,6BAA6B,CAAC;EAEhD,MAAM,SAAS,IAAI,OAAO,KAAK,QAAQ,KAAK,WAAW,GACvD,OAAO,OAAO,QAAQ;EAEtB,YAAoB,KAAK,MAAM,OAAO,GAAG;EAEzC,KAAK,KAAK,MAAM,QAAS,GAAG,KAAK,EAAE;EACnC,KAAK,KAAK,MAAM,SAAS,GAAG,KAAK,EAAE;CACpC;CAEA,UAAU;EACT,IAAI,KAAK,QAAQ;GAChB,KAAK,KAAK,OAAO;GACjB,KAAK,KAAK,WAAW,UAAU,OAAO,mBAAmB;EAC1D;EAEA,KAAK,iBAAiBC,GAAY;CACnC;CAEA,aAAa,GAAG;EACf,IAAI,EAAE,WAAW,GAAK;EAEtB,KAAK,QAAQ;EAEb,IAAI,CAAC,KAAK,QAAU;EAGpB,KAAK,qBAAqB,iBAAiB,KAAK,YAAY,GAAG,CAAC;EAEhE,MAAM,SAAS,IAAI,aAClB,KAAK,KAAK,uBAAuB,KAAK,WAAW,GACjD,KAAK,KAAK,uBAAuB,KAAK,MAAM,CAAC;EAE9C,KAAK,KAAK,cAAc,CAAC;EAGzB,IAAI,KAAK,KAAK,cAAc,MAAM,MAAM,UACvC,KAAK,KAAK,UAAU,MAAM;EAE3B,KAAK,KAAK,KAAK,cAAc,EAAC,eAAe,OAAM,CAAC;CACrD;CAEA,WAAW,GAAG;EACb,IAAI,EAAE,SAAS,UAAU;GACxB,KAAK,QAAQ;GACb,KAAK,YAAY;EAClB;CACD;;;;;;CAOA,OAAO,SAAS,YAAY;EAC3B,WAAW,aAAa,EACvB,SAAS,KACV,CAAC;EAED,WAAW,YAAY,cAAc,WAAW,cAAc;CAC/D;AACD"}
@@ -120,6 +120,7 @@ var DisplaceHandler = class DisplaceHandler extends Handler {
120
120
  * Called when the handler is enabled, adds the event hooks.
121
121
  */
122
122
  addHooks() {
123
+ this._map._displaceHandler = this;
123
124
  this._map.on("zoomend", this.schedule, this);
124
125
  this._map.on("layeradd", this._onLayerAdd, this);
125
126
  this._map.on("layerremove", this._onLayerRemove, this);
@@ -131,6 +132,7 @@ var DisplaceHandler = class DisplaceHandler extends Handler {
131
132
  * true positions of the markers.
132
133
  */
133
134
  removeHooks() {
135
+ if (this._map._displaceHandler === this) delete this._map._displaceHandler;
134
136
  this._map.off("zoomend", this.schedule, this);
135
137
  this._map.off("layeradd", this._onLayerAdd, this);
136
138
  this._map.off("layerremove", this._onLayerRemove, this);
@@ -202,6 +204,7 @@ var DisplaceHandler = class DisplaceHandler extends Handler {
202
204
  * @param {LatLng} latLng
203
205
  */
204
206
  _moveTo(marker, latLng) {
207
+ if (marker.getLatLng().equals(latLng)) return;
205
208
  this._moving = true;
206
209
  try {
207
210
  marker.setLatLng(latLng);
@@ -221,6 +224,19 @@ var DisplaceHandler = class DisplaceHandler extends Handler {
221
224
  if (this._enabled && this._map.hasLayer(event.target)) this.schedule();
222
225
  }
223
226
  /**
227
+ * Called by `CircleMarker#beforeAdd`: puts a marker whose `displaceId` already
228
+ * has a displaced position at the current zoom (e.g. the same station on
229
+ * another layer) there before it is drawn, instead of drawing it twice.
230
+ * @param {CircleMarker} marker
231
+ */
232
+ _beforeMarkerAdd(marker) {
233
+ if (!displaceable(marker)) return;
234
+ this._track(marker);
235
+ const { displaceId } = marker.options;
236
+ const latLng = displaceId && this._displacedPositions.get(this._map.getZoom())?.get(displaceId);
237
+ if (latLng) this._moveTo(marker, latLng);
238
+ }
239
+ /**
224
240
  * @param {LayerEvent} event
225
241
  */
226
242
  _onLayerAdd(event) {
@@ -257,6 +273,10 @@ var DisplaceHandler = class DisplaceHandler extends Handler {
257
273
  }
258
274
  };
259
275
  /**
276
+ * Movement in pixels per pass below which `displace` stops early
277
+ */
278
+ const MIN_MOVE = .01;
279
+ /**
260
280
  * Displaces overlapping points in pixel space until they are at least
261
281
  * `minSeparation` apart. Iterative relaxation: every overlapping pair is pushed
262
282
  * apart by half of its overlap per pass. Neighbour lookup uses a grid (cell
@@ -264,7 +284,8 @@ var DisplaceHandler = class DisplaceHandler extends Handler {
264
284
  *
265
285
  * No point ends up further than `maxDisplacement` away from where it started.
266
286
  * Points which hit that limit stay overlapping – the limit wins over the
267
- * separation.
287
+ * separation. The relaxation stops early once the points are separated or
288
+ * no point moves by more than a hundredth of a pixel per pass.
268
289
  *
269
290
  * @param {Point[]} points Pixel coordinates (modified in place)
270
291
  * @param {number} minSeparation Minimum distance between centers, in pixels
@@ -284,49 +305,62 @@ function displace(points, minSeparation, { iterations = DisplaceHandler.defaultO
284
305
  for (let iter = 0; iter < iterations; iter++) {
285
306
  dx.fill(0);
286
307
  dy.fill(0);
287
- /** @type {Map<string, number[]>} */
308
+ /** @type {Map<number, Map<number, number[]>>} */
288
309
  const grid = /* @__PURE__ */ new Map();
289
310
  for (let i = 0; i < n; i++) {
290
- const key = `${Math.floor(points[i].x / cell)},${Math.floor(points[i].y / cell)}`;
291
- const bucket = grid.get(key);
311
+ const cx = Math.floor(points[i].x / cell);
312
+ const cy = Math.floor(points[i].y / cell);
313
+ let column = grid.get(cx);
314
+ if (!column) {
315
+ column = /* @__PURE__ */ new Map();
316
+ grid.set(cx, column);
317
+ }
318
+ const bucket = column.get(cy);
292
319
  if (bucket) bucket.push(i);
293
- else grid.set(key, [i]);
320
+ else column.set(cy, [i]);
294
321
  }
295
322
  let overlaps = 0;
296
323
  for (let i = 0; i < n; i++) {
297
324
  const cx = Math.floor(points[i].x / cell);
298
325
  const cy = Math.floor(points[i].y / cell);
299
- for (let gx = cx - 1; gx <= cx + 1; gx++) for (let gy = cy - 1; gy <= cy + 1; gy++) {
300
- const bucket = grid.get(`${gx},${gy}`);
301
- if (!bucket) continue;
302
- for (const j of bucket) {
303
- if (j <= i) continue;
304
- let vx = points[i].x - points[j].x;
305
- let vy = points[i].y - points[j].y;
306
- const distSq = vx * vx + vy * vy;
307
- if (distSq >= sepSq) continue;
308
- overlaps++;
309
- let dist;
310
- if (distSq === 0) {
311
- const angle = i * 2.399963229728653;
312
- vx = Math.cos(angle);
313
- vy = Math.sin(angle);
314
- dist = 1;
315
- } else {
316
- dist = Math.sqrt(distSq);
317
- vx /= dist;
318
- vy /= dist;
326
+ for (let gx = cx - 1; gx <= cx + 1; gx++) {
327
+ const column = grid.get(gx);
328
+ if (!column) continue;
329
+ for (let gy = cy - 1; gy <= cy + 1; gy++) {
330
+ const bucket = column.get(gy);
331
+ if (!bucket) continue;
332
+ for (const j of bucket) {
333
+ if (j <= i) continue;
334
+ let vx = points[i].x - points[j].x;
335
+ let vy = points[i].y - points[j].y;
336
+ const distSq = vx * vx + vy * vy;
337
+ if (distSq >= sepSq) continue;
338
+ overlaps++;
339
+ let dist;
340
+ if (distSq === 0) {
341
+ const angle = i * 2.399963229728653;
342
+ vx = Math.cos(angle);
343
+ vy = Math.sin(angle);
344
+ dist = 1;
345
+ } else {
346
+ dist = Math.sqrt(distSq);
347
+ vx /= dist;
348
+ vy /= dist;
349
+ }
350
+ const push = (sep - dist) / 2;
351
+ dx[i] += vx * push;
352
+ dy[i] += vy * push;
353
+ dx[j] -= vx * push;
354
+ dy[j] -= vy * push;
319
355
  }
320
- const push = (sep - dist) / 2;
321
- dx[i] += vx * push;
322
- dy[i] += vy * push;
323
- dx[j] -= vx * push;
324
- dy[j] -= vy * push;
325
356
  }
326
357
  }
327
358
  }
328
359
  if (overlaps === 0) break;
360
+ let maxMoveSq = 0;
329
361
  for (let i = 0; i < n; i++) {
362
+ const x = points[i].x;
363
+ const y = points[i].y;
330
364
  points[i].x += dx[i];
331
365
  points[i].y += dy[i];
332
366
  const ox = points[i].x - ax[i];
@@ -337,7 +371,11 @@ function displace(points, minSeparation, { iterations = DisplaceHandler.defaultO
337
371
  points[i].x = ax[i] + ox * scale;
338
372
  points[i].y = ay[i] + oy * scale;
339
373
  }
374
+ const mx = points[i].x - x;
375
+ const my = points[i].y - y;
376
+ maxMoveSq = Math.max(maxMoveSq, mx * mx + my * my);
340
377
  }
378
+ if (maxMoveSq < MIN_MOVE * MIN_MOVE) break;
341
379
  }
342
380
  return points;
343
381
  }
@@ -1 +1 @@
1
- {"version":3,"file":"DisplaceHandler.js","names":[],"sources":["../../../../src/map/handler/DisplaceHandler.js"],"sourcesContent":["import {Handler} from '../../core/Handler.js';\nimport * as Util from '../../core/Util.js';\nimport {Point} from '../../geometry/Point.js';\nimport {CircleMarker} from '../../layer/vector/CircleMarker.js';\n/** @import {MaplairMap} from '../Map.js' */\n/** @import {Layer} from '../../layer/Layer.js' */\n/** @import {LayerEvent} from '../../core/Events.js' */\n/** @import {LatLng} from '../../geo/LatLng.js' */\n\n/**\n * Options of `DisplaceHandler`.\n * @typedef {object} DisplaceHandlerOptions\n * @property {number} [gap=2] Additional pixel distance between the outlines of two\n * markers.\n * @property {number} [iterations=80] Maximum number of relaxation passes per relayout.\n * @property {number} [maxDistance=Infinity] Maximum distance in meters a marker may be\n * moved away from its true position. Markers which would have to move further stay\n * overlapping. Meters rather than pixels, since the point of the limit is to bound the\n * geographic error. A limit in meters also scales itself: it only bites at the zoom\n * levels where a pixel is worth a lot of ground.\n */\n\n/**\n * Map options of `DisplaceHandler`, the interaction options for displacement.\n * @typedef {object} DisplaceHandlerMapOptions\n * @lends MaplairMapOptions\n * @property {boolean|DisplaceHandlerOptions} [displace] Whether to displace\n * overlapping `CircleMarker`s, either `true` or an object overriding the options of\n * `DisplaceHandler`. Needs the `DisplaceHandler` to be added to the map, see there.\n */\n\n/**\n * Handler of `MaplairMap` for displacement, if added to the map (see `DisplaceHandler`).\n * @typedef {object} DisplaceHandlerMapProperties\n * @lends MaplairMap\n * @property {DisplaceHandler} [displace] The displacement handler, if added with the\n * name `displace`.\n */\n\n/**\n * Options of `CircleMarker` for the displacement by `DisplaceHandler`.\n * @typedef {object} DisplaceCircleMarkerOptions\n * @lends CircleMarkerOptions\n * @property {boolean} [displace] Whether to include this marker in the displacement of\n * the map, see `DisplaceHandler`.\n * @property {string} [displaceId] Stable identifier shared across marker instances that\n * represent the same thing on different layers, so that `DisplaceHandler` puts them at\n * the same displaced position.\n */\n\n/**\n * Whether a layer takes part in the displacement. Only these change the layout, so layers\n * that merely come and go on top of the markers — the highlight circle around a tapped or\n * searched bobble, for one — must not trigger a relayout of all of them.\n * @param {Layer} layer\n * @returns {boolean}\n */\nfunction displaceable(layer) {\n\treturn layer instanceof CircleMarker && !!layer.options.displace;\n}\n\n/**\n * Cartographic displacement of overlapping `CircleMarker`s: keeps all circle\n * markers created with `displace: true` free of overlap.\n *\n * Overlap is a screen space phenomenon: whether two markers cover each other\n * depends on their pixel radius and the current zoom level. The displacement is\n * therefore computed in projected pixel space and recomputed on every zoom. The\n * true position of each marker is kept by the handler, the displaced position is\n * applied via `setLatLng`. Since every run starts over from the true position,\n * markers never drift.\n *\n * `maxDistance` bounds how far a marker may be moved away from the truth, in\n * meters. It is worth noting what such a limit costs at low zoom levels: if a\n * marker is 11 km wide on the ground, separating two coincident ones moves each\n * of them by some 6 km, so a limit below that leaves them overlapping.\n *\n * Unlike the default handlers, it isn't added to every map, so that bundlers can\n * drop it from applications that don't use it. Add it to the map class (or to a\n * single map with `DisplaceHandler.addTo(map, 'displace')`), then enable it with\n * the map option `displace`, which is either `true` or an object overriding the\n * handler's options. It can be toggled at runtime via `map.displace.enable()` /\n * `map.displace.disable()`; disabling restores the true positions.\n *\n * @example\n * ```js\n * import { CircleMarker, DisplaceHandler, MaplairMap } from 'maplair';\n *\n * MaplairMap.addInitHook('addHandler', 'displace', DisplaceHandler);\n *\n * const map = new MaplairMap('map', {displace: { maxDistance: 10_000} });\n * new CircleMarker(latlng, { displace: true, radius: 6 }).addTo(map);\n * ```\n */\nexport class DisplaceHandler extends Handler {\n\tstatic defaultOptions = {\n\t\tgap: 2,\n\t\titerations: 80,\n\t\tmaxDistance: Infinity\n\t};\n\n\t/**\n\t * @param {...MaplairMap} args The map.\n\t */\n\tconstructor(...args) {\n\t\tsuper(...args);\n\t\t// the map option `displace` is either `true` or an object overriding the defaults\n\t\tUtil.setOptions(this, this._map.options.displace);\n\n\t\t/**\n\t\t * True (undisplaced) position per marker, kept outside of the marker so that\n\t\t * the displacement cannot clobber it and re-added markers keep their anchor.\n\t\t * @type {WeakMap<CircleMarker, LatLng>}\n\t\t */\n\t\tthis._trueLatLngs = new WeakMap();\n\n\t\t/**\n\t\t * Displaced position per `marker.options.displaceId`, per zoom level. Different\n\t\t * layers (e.g. one per classification) may create separate `CircleMarker`\n\t\t * instances for the same thing; keying by id instead of by marker instance\n\t\t * keeps it at the same displaced position regardless of which layer triggered\n\t\t * the relayout. Cleared whenever a true position changes.\n\t\t * @type {Map<number, Map<string, LatLng>>}\n\t\t */\n\t\tthis._displacedPositions = new Map();\n\n\t\tthis.callInitHooks(DisplaceHandler);\n\t}\n\n\t/**\n\t * Called when the handler is enabled, adds the event hooks.\n\t */\n\taddHooks() {\n\t\tthis._map.on('zoomend', this.schedule, this);\n\t\tthis._map.on('layeradd', this._onLayerAdd, this);\n\t\tthis._map.on('layerremove', this._onLayerRemove, this);\n\t\tthis._map.eachLayer(layer => this._track(layer));\n\t\tthis.schedule();\n\t}\n\n\t/**\n\t * Called when the handler is disabled, removes the event hooks and restores the\n\t * true positions of the markers.\n\t */\n\tremoveHooks() {\n\t\tthis._map.off('zoomend', this.schedule, this);\n\t\tthis._map.off('layeradd', this._onLayerAdd, this);\n\t\tthis._map.off('layerremove', this._onLayerRemove, this);\n\t\tif (this._frame) { cancelAnimationFrame(this._frame); }\n\t\tthis._frame = undefined;\n\t\tthis._restore();\n\t}\n\n\t/**\n\t * Requests a relayout in the next animation frame, at most one per frame.\n\t */\n\tschedule() {\n\t\tif (this._frame) {\n\t\t\treturn;\n\t\t}\n\t\tthis._frame = requestAnimationFrame(() => {\n\t\t\tthis._frame = undefined;\n\t\t\tthis.relayout();\n\t\t});\n\t}\n\n\t/**\n\t * Computes the displacement for all tracked markers in the pixel space of\n\t * the current zoom level and writes the displaced positions back.\n\t */\n\trelayout() {\n\t\tconst map = this._map;\n\t\t/** @type {CircleMarker[]} */\n\t\tconst markers = [];\n\t\tmap.eachLayer((layer) => {\n\t\t\tif (this._trueLatLngs.has(layer)) {\n\t\t\t\tmarkers.push(layer);\n\t\t\t}\n\t\t});\n\t\tif (markers.length < 2) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst zoom = map.getZoom();\n\t\tlet cache = this._displacedPositions.get(zoom);\n\t\tif (!cache) {\n\t\t\tcache = new Map();\n\t\t\tthis._displacedPositions.set(zoom, cache);\n\t\t}\n\n\t\t// If every currently visible marker already has a cached position for\n\t\t// this zoom (e.g. the same stations shown on a different layer), reuse\n\t\t// it as-is instead of recomputing the relaxation.\n\t\tif (markers.every(marker => marker.options.displaceId && cache.has(marker.options.displaceId))) {\n\t\t\tfor (const marker of markers) {\n\t\t\t\tthis._moveTo(marker, cache.get(marker.options.displaceId));\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tconst {gap, iterations, maxDistance} = this.options;\n\t\tconst points = markers.map(marker => map.project(this._trueLatLngs.get(marker), zoom));\n\n\t\t// Usually a uniform radius per zoom level; max() as a safeguard.\n\t\tconst radius = markers.reduce((r, marker) => Math.max(r, marker.getRadius()), 0);\n\t\tdisplace(points, 2 * radius + gap, {iterations, maxDisplacement: maxDistance * this._pixelsPerMeter(zoom)});\n\n\t\tfor (let i = 0; i < markers.length; i++) {\n\t\t\tconst latLng = map.unproject(points[i], zoom);\n\t\t\tthis._moveTo(markers[i], latLng);\n\t\t\tif (markers[i].options.displaceId) {\n\t\t\t\tcache.set(markers[i].options.displaceId, latLng);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Scale of the map at `zoom`, measured with the CRS of the map at its\n\t * center. Since a relayout only ever covers one screen, the scale variation\n\t * across the view is ignored.\n\t * @param {number} zoom\n\t * @returns {number}\n\t */\n\t_pixelsPerMeter(zoom) {\n\t\tconst map = this._map;\n\t\tconst center = map.getCenter();\n\t\tconst east = map.unproject(map.project(center, zoom).add(new Point(100, 0)), zoom);\n\t\treturn 100 / map.distance(center, east);\n\t}\n\n\t/**\n\t * Moves a marker to its displaced (or true) position, without taking that\n\t * for a new true position, see `_onMarkerMove`.\n\t * @param {CircleMarker} marker\n\t * @param {LatLng} latLng\n\t */\n\t_moveTo(marker, latLng) {\n\t\tthis._moving = true;\n\t\ttry {\n\t\t\tmarker.setLatLng(latLng);\n\t\t} finally {\n\t\t\tthis._moving = false;\n\t\t}\n\t}\n\n\t/**\n\t * A tracked marker was moved by the application: that is its new true position,\n\t * which invalidates the cached displaced positions.\n\t * @param {Event} event\n\t */\n\t_onMarkerMove(event) {\n\t\tif (this._moving) { return; }\n\t\tthis._trueLatLngs.set(event.target, event.latlng);\n\t\tthis._displacedPositions.clear();\n\t\tif (this._enabled && this._map.hasLayer(event.target)) { this.schedule(); }\n\t}\n\n\t/**\n\t * @param {LayerEvent} event\n\t */\n\t_onLayerAdd(event) {\n\t\tthis._track(event.layer);\n\t\tif (displaceable(event.layer)) { this.schedule(); }\n\t}\n\n\t/**\n\t * @param {LayerEvent} event\n\t */\n\t_onLayerRemove(event) {\n\t\tif (displaceable(event.layer)) { this.schedule(); }\n\t}\n\n\t/**\n\t * Remembers the true position of a marker opted in via `displace: true`.\n\t * Markers seen before keep their previously stored position, so re-adding a\n\t * displaced marker does not anchor it to its displaced position.\n\t * @param {Layer} layer\n\t */\n\t_track(layer) {\n\t\tif (!displaceable(layer)) {\n\t\t\treturn;\n\t\t}\n\t\tif (!this._trueLatLngs.has(layer)) {\n\t\t\tthis._trueLatLngs.set(layer, layer.getLatLng());\n\t\t\tlayer.on('move', this._onMarkerMove, this);\n\t\t}\n\t}\n\n\t/**\n\t * Moves all tracked markers back to their true position.\n\t */\n\t_restore() {\n\t\tthis._map.eachLayer((layer) => {\n\t\t\tconst latLng = this._trueLatLngs.get(layer);\n\t\t\tif (latLng) { this._moveTo(layer, latLng); }\n\t\t});\n\t}\n}\n\n/**\n * Displaces overlapping points in pixel space until they are at least\n * `minSeparation` apart. Iterative relaxation: every overlapping pair is pushed\n * apart by half of its overlap per pass. Neighbour lookup uses a grid (cell\n * size = separation) ⇒ O(n) per iteration.\n *\n * No point ends up further than `maxDisplacement` away from where it started.\n * Points which hit that limit stay overlapping – the limit wins over the\n * separation.\n *\n * @param {Point[]} points Pixel coordinates (modified in place)\n * @param {number} minSeparation Minimum distance between centers, in pixels\n * @param {{ iterations?: number, maxDisplacement?: number }} [options]\n * @returns {Point[]} the same (displaced) points\n */\nexport function displace(\n\tpoints,\n\tminSeparation,\n\t{iterations = DisplaceHandler.defaultOptions.iterations, maxDisplacement = Infinity} = {},\n) {\n\tconst n = points.length;\n\tif (n < 2) { return points; }\n\n\tconst sep = minSeparation;\n\tconst sepSq = sep * sep;\n\tconst cell = sep;\n\tconst dx = new Float64Array(n);\n\tconst dy = new Float64Array(n);\n\t// Anchors for `maxDisplacement`, the true positions of the points.\n\tconst ax = Float64Array.from(points, point => point.x);\n\tconst ay = Float64Array.from(points, point => point.y);\n\n\tfor (let iter = 0; iter < iterations; iter++) {\n\t\tdx.fill(0);\n\t\tdy.fill(0);\n\n\t\t// Sort the points into a grid (cell size = separation).\n\t\t/** @type {Map<string, number[]>} */\n\t\tconst grid = new Map();\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst key = `${Math.floor(points[i].x / cell)},${Math.floor(points[i].y / cell)}`;\n\t\t\tconst bucket = grid.get(key);\n\t\t\tif (bucket) {\n\t\t\t\tbucket.push(i);\n\t\t\t} else {\n\t\t\t\tgrid.set(key, [i]);\n\t\t\t}\n\t\t}\n\n\t\tlet overlaps = 0;\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst cx = Math.floor(points[i].x / cell);\n\t\t\tconst cy = Math.floor(points[i].y / cell);\n\t\t\t// Only the 3×3 neighbouring cells can be closer than `sep`.\n\t\t\tfor (let gx = cx - 1; gx <= cx + 1; gx++) {\n\t\t\t\tfor (let gy = cy - 1; gy <= cy + 1; gy++) {\n\t\t\t\t\tconst bucket = grid.get(`${gx},${gy}`);\n\t\t\t\t\tif (!bucket) { continue; }\n\t\t\t\t\tfor (const j of bucket) {\n\t\t\t\t\t\tif (j <= i) { continue; } // visit every pair once\n\t\t\t\t\t\tlet vx = points[i].x - points[j].x;\n\t\t\t\t\t\tlet vy = points[i].y - points[j].y;\n\t\t\t\t\t\tconst distSq = vx * vx + vy * vy;\n\t\t\t\t\t\tif (distSq >= sepSq) { continue; }\n\t\t\t\t\t\toverlaps++;\n\n\t\t\t\t\t\tlet dist;\n\t\t\t\t\t\tif (distSq === 0) {\n\t\t\t\t\t\t\t// Coincident points: deterministic direction derived\n\t\t\t\t\t\t\t// from the index (golden angle) instead of random.\n\t\t\t\t\t\t\tconst angle = i * 2.399963229728653;\n\t\t\t\t\t\t\tvx = Math.cos(angle);\n\t\t\t\t\t\t\tvy = Math.sin(angle);\n\t\t\t\t\t\t\tdist = 1;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tdist = Math.sqrt(distSq);\n\t\t\t\t\t\t\tvx /= dist;\n\t\t\t\t\t\t\tvy /= dist;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst push = (sep - dist) / 2;\n\t\t\t\t\t\tdx[i] += vx * push;\n\t\t\t\t\t\tdy[i] += vy * push;\n\t\t\t\t\t\tdx[j] -= vx * push;\n\t\t\t\t\t\tdy[j] -= vy * push;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (overlaps === 0) {\n\t\t\tbreak; // converged – nothing left to do\n\t\t}\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tpoints[i].x += dx[i];\n\t\t\tpoints[i].y += dy[i];\n\n\t\t\t// Pull the point back onto the circle of radius `maxDisplacement`\n\t\t\t// around its anchor. Clamping inside the loop (instead of once at\n\t\t\t// the end) lets the following iterations redistribute along that\n\t\t\t// circle rather than piling up against it.\n\t\t\tconst ox = points[i].x - ax[i];\n\t\t\tconst oy = points[i].y - ay[i];\n\t\t\tconst offsetSq = ox * ox + oy * oy;\n\t\t\tif (offsetSq > maxDisplacement * maxDisplacement) {\n\t\t\t\tconst scale = maxDisplacement / Math.sqrt(offsetSq);\n\t\t\t\tpoints[i].x = ax[i] + ox * scale;\n\t\t\t\tpoints[i].y = ay[i] + oy * scale;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn points;\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,SAAS,aAAa,OAAO;CAC5B,OAAO,iBAAiB,gBAAgB,CAAC,CAAC,MAAM,QAAQ;AACzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,IAAa,kBAAb,MAAa,wBAAwB,QAAQ;CAC5C,OAAO,iBAAiB;EACvB,KAAK;EACL,YAAY;EACZ,aAAa;CACd;;;;CAKA,YAAY,GAAG,MAAM;EACpB,MAAM,GAAG,IAAI;EAEb,WAAgB,MAAM,KAAK,KAAK,QAAQ,QAAQ;;;;;;EAOhD,KAAK,+BAAe,IAAI,QAAQ;;;;;;;;;EAUhC,KAAK,sCAAsB,IAAI,IAAI;EAEnC,KAAK,cAAc,eAAe;CACnC;;;;CAKA,WAAW;EACV,KAAK,KAAK,GAAG,WAAW,KAAK,UAAU,IAAI;EAC3C,KAAK,KAAK,GAAG,YAAY,KAAK,aAAa,IAAI;EAC/C,KAAK,KAAK,GAAG,eAAe,KAAK,gBAAgB,IAAI;EACrD,KAAK,KAAK,WAAU,UAAS,KAAK,OAAO,KAAK,CAAC;EAC/C,KAAK,SAAS;CACf;;;;;CAMA,cAAc;EACb,KAAK,KAAK,IAAI,WAAW,KAAK,UAAU,IAAI;EAC5C,KAAK,KAAK,IAAI,YAAY,KAAK,aAAa,IAAI;EAChD,KAAK,KAAK,IAAI,eAAe,KAAK,gBAAgB,IAAI;EACtD,IAAI,KAAK,QAAU,qBAAqB,KAAK,MAAM;EACnD,KAAK,SAAS,KAAA;EACd,KAAK,SAAS;CACf;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IAAI,KAAK,OAAO,EAAE,CAAC,IAAI,OAAO,EAAE,CAAC;KACjC,MAAM,SAAS,KAAK,KAAK,KAAK;KAC9B,IAAI,UAAU,OAAS;KACvB;KAEA,IAAI;KACJ,IAAI,WAAW,GAAG;MAGjB,MAAM,QAAQ,IAAI;MAClB,KAAK,KAAK,IAAI,KAAK;MACnB,KAAK,KAAK,IAAI,KAAK;MACnB,OAAO;KACR,OAAO;MACN,OAAO,KAAK,KAAK,MAAM;MACvB,MAAM;MACN,MAAM;KACP;KAEA,MAAM,QAAQ,MAAM,QAAQ;KAC5B,GAAG,MAAM,KAAK;KACd,GAAG,MAAM,KAAK;KACd,GAAG,MAAM,KAAK;KACd,GAAG,MAAM,KAAK;IACf;GACD;EAEF;EAEA,IAAI,aAAa,GAChB;EAED,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;GAC3B,OAAO,EAAE,CAAC,KAAK,GAAG;GAClB,OAAO,EAAE,CAAC,KAAK,GAAG;GAMlB,MAAM,KAAK,OAAO,EAAE,CAAC,IAAI,GAAG;GAC5B,MAAM,KAAK,OAAO,EAAE,CAAC,IAAI,GAAG;GAC5B,MAAM,WAAW,KAAK,KAAK,KAAK;GAChC,IAAI,WAAW,kBAAkB,iBAAiB;IACjD,MAAM,QAAQ,kBAAkB,KAAK,KAAK,QAAQ;IAClD,OAAO,EAAE,CAAC,IAAI,GAAG,KAAK,KAAK;IAC3B,OAAO,EAAE,CAAC,IAAI,GAAG,KAAK,KAAK;GAC5B;EACD;CACD;CAEA,OAAO;AACR"}
1
+ {"version":3,"file":"DisplaceHandler.js","names":[],"sources":["../../../../src/map/handler/DisplaceHandler.js"],"sourcesContent":["import {Handler} from '../../core/Handler.js';\nimport * as Util from '../../core/Util.js';\nimport {Point} from '../../geometry/Point.js';\nimport {CircleMarker} from '../../layer/vector/CircleMarker.js';\n/** @import {MaplairMap} from '../Map.js' */\n/** @import {Layer} from '../../layer/Layer.js' */\n/** @import {LayerEvent} from '../../core/Events.js' */\n/** @import {LatLng} from '../../geo/LatLng.js' */\n\n/**\n * Options of `DisplaceHandler`.\n * @typedef {object} DisplaceHandlerOptions\n * @property {number} [gap=2] Additional pixel distance between the outlines of two\n * markers.\n * @property {number} [iterations=80] Maximum number of relaxation passes per relayout.\n * @property {number} [maxDistance=Infinity] Maximum distance in meters a marker may be\n * moved away from its true position. Markers which would have to move further stay\n * overlapping. Meters rather than pixels, since the point of the limit is to bound the\n * geographic error. A limit in meters also scales itself: it only bites at the zoom\n * levels where a pixel is worth a lot of ground.\n */\n\n/**\n * Map options of `DisplaceHandler`, the interaction options for displacement.\n * @typedef {object} DisplaceHandlerMapOptions\n * @lends MaplairMapOptions\n * @property {boolean|DisplaceHandlerOptions} [displace] Whether to displace\n * overlapping `CircleMarker`s, either `true` or an object overriding the options of\n * `DisplaceHandler`. Needs the `DisplaceHandler` to be added to the map, see there.\n */\n\n/**\n * Handler of `MaplairMap` for displacement, if added to the map (see `DisplaceHandler`).\n * @typedef {object} DisplaceHandlerMapProperties\n * @lends MaplairMap\n * @property {DisplaceHandler} [displace] The displacement handler, if added with the\n * name `displace`.\n */\n\n/**\n * Options of `CircleMarker` for the displacement by `DisplaceHandler`.\n * @typedef {object} DisplaceCircleMarkerOptions\n * @lends CircleMarkerOptions\n * @property {boolean} [displace] Whether to include this marker in the displacement of\n * the map, see `DisplaceHandler`.\n * @property {string} [displaceId] Stable identifier shared across marker instances that\n * represent the same thing on different layers, so that `DisplaceHandler` puts them at\n * the same displaced position.\n */\n\n/**\n * Whether a layer takes part in the displacement. Only these change the layout, so layers\n * that merely come and go on top of the markers — the highlight circle around a tapped or\n * searched bobble, for one — must not trigger a relayout of all of them.\n * @param {Layer} layer\n * @returns {boolean}\n */\nfunction displaceable(layer) {\n\treturn layer instanceof CircleMarker && !!layer.options.displace;\n}\n\n/**\n * Cartographic displacement of overlapping `CircleMarker`s: keeps all circle\n * markers created with `displace: true` free of overlap.\n *\n * Overlap is a screen space phenomenon: whether two markers cover each other\n * depends on their pixel radius and the current zoom level. The displacement is\n * therefore computed in projected pixel space and recomputed on every zoom. The\n * true position of each marker is kept by the handler, the displaced position is\n * applied via `setLatLng`. Since every run starts over from the true position,\n * markers never drift.\n *\n * `maxDistance` bounds how far a marker may be moved away from the truth, in\n * meters. It is worth noting what such a limit costs at low zoom levels: if a\n * marker is 11 km wide on the ground, separating two coincident ones moves each\n * of them by some 6 km, so a limit below that leaves them overlapping.\n *\n * Unlike the default handlers, it isn't added to every map, so that bundlers can\n * drop it from applications that don't use it. Add it to the map class (or to a\n * single map with `DisplaceHandler.addTo(map, 'displace')`), then enable it with\n * the map option `displace`, which is either `true` or an object overriding the\n * handler's options. It can be toggled at runtime via `map.displace.enable()` /\n * `map.displace.disable()`; disabling restores the true positions.\n *\n * @example\n * ```js\n * import { CircleMarker, DisplaceHandler, MaplairMap } from 'maplair';\n *\n * MaplairMap.addInitHook('addHandler', 'displace', DisplaceHandler);\n *\n * const map = new MaplairMap('map', {displace: { maxDistance: 10_000} });\n * new CircleMarker(latlng, { displace: true, radius: 6 }).addTo(map);\n * ```\n */\nexport class DisplaceHandler extends Handler {\n\tstatic defaultOptions = {\n\t\tgap: 2,\n\t\titerations: 80,\n\t\tmaxDistance: Infinity\n\t};\n\n\t/**\n\t * @param {...MaplairMap} args The map.\n\t */\n\tconstructor(...args) {\n\t\tsuper(...args);\n\t\t// the map option `displace` is either `true` or an object overriding the defaults\n\t\tUtil.setOptions(this, this._map.options.displace);\n\n\t\t/**\n\t\t * True (undisplaced) position per marker, kept outside of the marker so that\n\t\t * the displacement cannot clobber it and re-added markers keep their anchor.\n\t\t * @type {WeakMap<CircleMarker, LatLng>}\n\t\t */\n\t\tthis._trueLatLngs = new WeakMap();\n\n\t\t/**\n\t\t * Displaced position per `marker.options.displaceId`, per zoom level. Different\n\t\t * layers (e.g. one per classification) may create separate `CircleMarker`\n\t\t * instances for the same thing; keying by id instead of by marker instance\n\t\t * keeps it at the same displaced position regardless of which layer triggered\n\t\t * the relayout. Cleared whenever a true position changes.\n\t\t * @type {Map<number, Map<string, LatLng>>}\n\t\t */\n\t\tthis._displacedPositions = new Map();\n\n\t\tthis.callInitHooks(DisplaceHandler);\n\t}\n\n\t/**\n\t * Called when the handler is enabled, adds the event hooks.\n\t */\n\taddHooks() {\n\t\t// see `CircleMarker#beforeAdd`\n\t\tthis._map._displaceHandler = this;\n\t\tthis._map.on('zoomend', this.schedule, this);\n\t\tthis._map.on('layeradd', this._onLayerAdd, this);\n\t\tthis._map.on('layerremove', this._onLayerRemove, this);\n\t\tthis._map.eachLayer(layer => this._track(layer));\n\t\tthis.schedule();\n\t}\n\n\t/**\n\t * Called when the handler is disabled, removes the event hooks and restores the\n\t * true positions of the markers.\n\t */\n\tremoveHooks() {\n\t\tif (this._map._displaceHandler === this) { delete this._map._displaceHandler; }\n\t\tthis._map.off('zoomend', this.schedule, this);\n\t\tthis._map.off('layeradd', this._onLayerAdd, this);\n\t\tthis._map.off('layerremove', this._onLayerRemove, this);\n\t\tif (this._frame) { cancelAnimationFrame(this._frame); }\n\t\tthis._frame = undefined;\n\t\tthis._restore();\n\t}\n\n\t/**\n\t * Requests a relayout in the next animation frame, at most one per frame.\n\t */\n\tschedule() {\n\t\tif (this._frame) {\n\t\t\treturn;\n\t\t}\n\t\tthis._frame = requestAnimationFrame(() => {\n\t\t\tthis._frame = undefined;\n\t\t\tthis.relayout();\n\t\t});\n\t}\n\n\t/**\n\t * Computes the displacement for all tracked markers in the pixel space of\n\t * the current zoom level and writes the displaced positions back.\n\t */\n\trelayout() {\n\t\tconst map = this._map;\n\t\t/** @type {CircleMarker[]} */\n\t\tconst markers = [];\n\t\tmap.eachLayer((layer) => {\n\t\t\tif (this._trueLatLngs.has(layer)) {\n\t\t\t\tmarkers.push(layer);\n\t\t\t}\n\t\t});\n\t\tif (markers.length < 2) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst zoom = map.getZoom();\n\t\tlet cache = this._displacedPositions.get(zoom);\n\t\tif (!cache) {\n\t\t\tcache = new Map();\n\t\t\tthis._displacedPositions.set(zoom, cache);\n\t\t}\n\n\t\t// If every currently visible marker already has a cached position for\n\t\t// this zoom (e.g. the same stations shown on a different layer), reuse\n\t\t// it as-is instead of recomputing the relaxation.\n\t\tif (markers.every(marker => marker.options.displaceId && cache.has(marker.options.displaceId))) {\n\t\t\tfor (const marker of markers) {\n\t\t\t\tthis._moveTo(marker, cache.get(marker.options.displaceId));\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tconst {gap, iterations, maxDistance} = this.options;\n\t\tconst points = markers.map(marker => map.project(this._trueLatLngs.get(marker), zoom));\n\n\t\t// Usually a uniform radius per zoom level; max() as a safeguard.\n\t\tconst radius = markers.reduce((r, marker) => Math.max(r, marker.getRadius()), 0);\n\t\tdisplace(points, 2 * radius + gap, {iterations, maxDisplacement: maxDistance * this._pixelsPerMeter(zoom)});\n\n\t\tfor (let i = 0; i < markers.length; i++) {\n\t\t\tconst latLng = map.unproject(points[i], zoom);\n\t\t\tthis._moveTo(markers[i], latLng);\n\t\t\tif (markers[i].options.displaceId) {\n\t\t\t\tcache.set(markers[i].options.displaceId, latLng);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Scale of the map at `zoom`, measured with the CRS of the map at its\n\t * center. Since a relayout only ever covers one screen, the scale variation\n\t * across the view is ignored.\n\t * @param {number} zoom\n\t * @returns {number}\n\t */\n\t_pixelsPerMeter(zoom) {\n\t\tconst map = this._map;\n\t\tconst center = map.getCenter();\n\t\tconst east = map.unproject(map.project(center, zoom).add(new Point(100, 0)), zoom);\n\t\treturn 100 / map.distance(center, east);\n\t}\n\n\t/**\n\t * Moves a marker to its displaced (or true) position, without taking that\n\t * for a new true position, see `_onMarkerMove`.\n\t * @param {CircleMarker} marker\n\t * @param {LatLng} latLng\n\t */\n\t_moveTo(marker, latLng) {\n\t\t// e.g. already placed by `_beforeMarkerAdd`: spares redrawing every marker\n\t\tif (marker.getLatLng().equals(latLng)) { return; }\n\t\tthis._moving = true;\n\t\ttry {\n\t\t\tmarker.setLatLng(latLng);\n\t\t} finally {\n\t\t\tthis._moving = false;\n\t\t}\n\t}\n\n\t/**\n\t * A tracked marker was moved by the application: that is its new true position,\n\t * which invalidates the cached displaced positions.\n\t * @param {Event} event\n\t */\n\t_onMarkerMove(event) {\n\t\tif (this._moving) { return; }\n\t\tthis._trueLatLngs.set(event.target, event.latlng);\n\t\tthis._displacedPositions.clear();\n\t\tif (this._enabled && this._map.hasLayer(event.target)) { this.schedule(); }\n\t}\n\n\t/**\n\t * Called by `CircleMarker#beforeAdd`: puts a marker whose `displaceId` already\n\t * has a displaced position at the current zoom (e.g. the same station on\n\t * another layer) there before it is drawn, instead of drawing it twice.\n\t * @param {CircleMarker} marker\n\t */\n\t_beforeMarkerAdd(marker) {\n\t\tif (!displaceable(marker)) { return; }\n\t\tthis._track(marker);\n\t\tconst {displaceId} = marker.options;\n\t\tconst latLng = displaceId && this._displacedPositions.get(this._map.getZoom())?.get(displaceId);\n\t\tif (latLng) { this._moveTo(marker, latLng); }\n\t}\n\n\t/**\n\t * @param {LayerEvent} event\n\t */\n\t_onLayerAdd(event) {\n\t\tthis._track(event.layer);\n\t\tif (displaceable(event.layer)) { this.schedule(); }\n\t}\n\n\t/**\n\t * @param {LayerEvent} event\n\t */\n\t_onLayerRemove(event) {\n\t\tif (displaceable(event.layer)) { this.schedule(); }\n\t}\n\n\t/**\n\t * Remembers the true position of a marker opted in via `displace: true`.\n\t * Markers seen before keep their previously stored position, so re-adding a\n\t * displaced marker does not anchor it to its displaced position.\n\t * @param {Layer} layer\n\t */\n\t_track(layer) {\n\t\tif (!displaceable(layer)) {\n\t\t\treturn;\n\t\t}\n\t\tif (!this._trueLatLngs.has(layer)) {\n\t\t\tthis._trueLatLngs.set(layer, layer.getLatLng());\n\t\t\tlayer.on('move', this._onMarkerMove, this);\n\t\t}\n\t}\n\n\t/**\n\t * Moves all tracked markers back to their true position.\n\t */\n\t_restore() {\n\t\tthis._map.eachLayer((layer) => {\n\t\t\tconst latLng = this._trueLatLngs.get(layer);\n\t\t\tif (latLng) { this._moveTo(layer, latLng); }\n\t\t});\n\t}\n}\n\n/**\n * Movement in pixels per pass below which `displace` stops early\n */\nconst MIN_MOVE = 0.01;\n\n/**\n * Displaces overlapping points in pixel space until they are at least\n * `minSeparation` apart. Iterative relaxation: every overlapping pair is pushed\n * apart by half of its overlap per pass. Neighbour lookup uses a grid (cell\n * size = separation) ⇒ O(n) per iteration.\n *\n * No point ends up further than `maxDisplacement` away from where it started.\n * Points which hit that limit stay overlapping – the limit wins over the\n * separation. The relaxation stops early once the points are separated or\n * no point moves by more than a hundredth of a pixel per pass.\n *\n * @param {Point[]} points Pixel coordinates (modified in place)\n * @param {number} minSeparation Minimum distance between centers, in pixels\n * @param {{ iterations?: number, maxDisplacement?: number }} [options]\n * @returns {Point[]} the same (displaced) points\n */\nexport function displace(\n\tpoints,\n\tminSeparation,\n\t{iterations = DisplaceHandler.defaultOptions.iterations, maxDisplacement = Infinity} = {},\n) {\n\tconst n = points.length;\n\tif (n < 2) { return points; }\n\n\tconst sep = minSeparation;\n\tconst sepSq = sep * sep;\n\tconst cell = sep;\n\tconst dx = new Float64Array(n);\n\tconst dy = new Float64Array(n);\n\t// Anchors for `maxDisplacement`, the true positions of the points.\n\tconst ax = Float64Array.from(points, point => point.x);\n\tconst ay = Float64Array.from(points, point => point.y);\n\n\tfor (let iter = 0; iter < iterations; iter++) {\n\t\tdx.fill(0);\n\t\tdy.fill(0);\n\n\t\t// Sort the points into a grid (cell size = separation), keyed by column and\n\t\t// then row. Numeric keys instead of a string \"x,y\" per lookup: the relaxation\n\t\t// runs up to `iterations` times over all points and 9 cells each.\n\t\t/** @type {Map<number, Map<number, number[]>>} */\n\t\tconst grid = new Map();\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst cx = Math.floor(points[i].x / cell);\n\t\t\tconst cy = Math.floor(points[i].y / cell);\n\t\t\tlet column = grid.get(cx);\n\t\t\tif (!column) {\n\t\t\t\tcolumn = new Map();\n\t\t\t\tgrid.set(cx, column);\n\t\t\t}\n\t\t\tconst bucket = column.get(cy);\n\t\t\tif (bucket) {\n\t\t\t\tbucket.push(i);\n\t\t\t} else {\n\t\t\t\tcolumn.set(cy, [i]);\n\t\t\t}\n\t\t}\n\n\t\tlet overlaps = 0;\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst cx = Math.floor(points[i].x / cell);\n\t\t\tconst cy = Math.floor(points[i].y / cell);\n\t\t\t// Only the 3×3 neighbouring cells can be closer than `sep`.\n\t\t\tfor (let gx = cx - 1; gx <= cx + 1; gx++) {\n\t\t\t\tconst column = grid.get(gx);\n\t\t\t\tif (!column) { continue; }\n\t\t\t\tfor (let gy = cy - 1; gy <= cy + 1; gy++) {\n\t\t\t\t\tconst bucket = column.get(gy);\n\t\t\t\t\tif (!bucket) { continue; }\n\t\t\t\t\tfor (const j of bucket) {\n\t\t\t\t\t\tif (j <= i) { continue; } // visit every pair once\n\t\t\t\t\t\tlet vx = points[i].x - points[j].x;\n\t\t\t\t\t\tlet vy = points[i].y - points[j].y;\n\t\t\t\t\t\tconst distSq = vx * vx + vy * vy;\n\t\t\t\t\t\tif (distSq >= sepSq) { continue; }\n\t\t\t\t\t\toverlaps++;\n\n\t\t\t\t\t\tlet dist;\n\t\t\t\t\t\tif (distSq === 0) {\n\t\t\t\t\t\t\t// Coincident points: deterministic direction derived\n\t\t\t\t\t\t\t// from the index (golden angle) instead of random.\n\t\t\t\t\t\t\tconst angle = i * 2.399963229728653;\n\t\t\t\t\t\t\tvx = Math.cos(angle);\n\t\t\t\t\t\t\tvy = Math.sin(angle);\n\t\t\t\t\t\t\tdist = 1;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tdist = Math.sqrt(distSq);\n\t\t\t\t\t\t\tvx /= dist;\n\t\t\t\t\t\t\tvy /= dist;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst push = (sep - dist) / 2;\n\t\t\t\t\t\tdx[i] += vx * push;\n\t\t\t\t\t\tdy[i] += vy * push;\n\t\t\t\t\t\tdx[j] -= vx * push;\n\t\t\t\t\t\tdy[j] -= vy * push;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (overlaps === 0) {\n\t\t\tbreak; // converged – nothing left to do\n\t\t}\n\t\tlet maxMoveSq = 0;\n\t\tfor (let i = 0; i < n; i++) {\n\t\t\tconst x = points[i].x;\n\t\t\tconst y = points[i].y;\n\t\t\tpoints[i].x += dx[i];\n\t\t\tpoints[i].y += dy[i];\n\n\t\t\t// Pull the point back onto the circle of radius `maxDisplacement`\n\t\t\t// around its anchor. Clamping inside the loop (instead of once at\n\t\t\t// the end) lets the following iterations redistribute along that\n\t\t\t// circle rather than piling up against it.\n\t\t\tconst ox = points[i].x - ax[i];\n\t\t\tconst oy = points[i].y - ay[i];\n\t\t\tconst offsetSq = ox * ox + oy * oy;\n\t\t\tif (offsetSq > maxDisplacement * maxDisplacement) {\n\t\t\t\tconst scale = maxDisplacement / Math.sqrt(offsetSq);\n\t\t\t\tpoints[i].x = ax[i] + ox * scale;\n\t\t\t\tpoints[i].y = ay[i] + oy * scale;\n\t\t\t}\n\t\t\tconst mx = points[i].x - x;\n\t\t\tconst my = points[i].y - y;\n\t\t\tmaxMoveSq = Math.max(maxMoveSq, mx * mx + my * my);\n\t\t}\n\t\t// Points held back by `maxDisplacement` keep overlapping, so `overlaps`\n\t\t// never drops to 0: stop once no point moves noticeably any more.\n\t\tif (maxMoveSq < MIN_MOVE * MIN_MOVE) {\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn points;\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyDA,SAAS,aAAa,OAAO;CAC5B,OAAO,iBAAiB,gBAAgB,CAAC,CAAC,MAAM,QAAQ;AACzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,IAAa,kB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AI;GACzB,MAAM,KAAK,OAAO,EAAE,CAAC,IAAI;GACzB,YAAY,KAAK,IAAI,WAAW,KAAK,KAAK,KAAK,EAAE;EAClD;EAGA,IAAI,YAAY,WAAW,UAC1B;CAEF;CAEA,OAAO;AACR"}
@@ -101,8 +101,13 @@ var DragHandler = class DragHandler extends Handler {
101
101
  map._stop();
102
102
  map._setUserEvent(e.originalEvent);
103
103
  if (this._map.options.maxBounds && this._map.options.maxBoundsViscosity) {
104
- const bounds = LatLngBounds.from(this._map.options.maxBounds);
105
- this._offsetLimit = new Bounds(this._map.latLngToContainerPoint(bounds.getNorthWest()).multiplyBy(-1), this._map.latLngToContainerPoint(bounds.getSouthEast()).multiplyBy(-1).add(this._map.getSize()));
104
+ const bounds = LatLngBounds.from(this._map.options.maxBounds), corners = [
105
+ bounds.getNorthWest(),
106
+ bounds.getNorthEast(),
107
+ bounds.getSouthEast(),
108
+ bounds.getSouthWest()
109
+ ], { min, max } = new Bounds(corners.map((c) => this._map.latLngToContainerPoint(c)));
110
+ this._offsetLimit = new Bounds(max.multiplyBy(-1), min.multiplyBy(-1).add(this._map.getSize()));
106
111
  this._viscosity = Math.min(1, Math.max(0, this._map.options.maxBoundsViscosity));
107
112
  } else this._offsetLimit = null;
108
113
  map.fire("movestart").fire("dragstart");
@@ -1 +1 @@
1
- {"version":3,"file":"DragHandler.js","names":[],"sources":["../../../../src/map/handler/DragHandler.js"],"sourcesContent":["import {Handler} from '../../core/Handler.js';\nimport {Draggable} from '../../dom/Draggable.js';\nimport {LatLngBounds} from '../../geo/LatLngBounds.js';\nimport {Bounds} from '../../geometry/Bounds.js';\n/** @import {MaplairMap} from '../Map.js' */\n/** @import {MaplairEvent, DragEndEvent} from '../../core/Events.js' */\n\n/**\n * Map options of `DragHandler`, the interaction options for dragging.\n * @typedef {object} DragHandlerMapOptions\n * @lends MaplairMapOptions\n * @property {boolean} [dragging=true] Whether the map is draggable with pointer or not.\n * @property {boolean} [inertia=true] If enabled, panning of the map will have an\n * inertia effect where the map builds momentum while dragging and continues moving in\n * the same direction for some time. Feels especially nice on touch devices.\n * @property {number} [inertiaDeceleration=3400] The rate with which the inertial\n * movement slows down, in pixels/second².\n * @property {number} [inertiaMaxSpeed=1400] Max speed of the inertial movement,\n * in pixels/second.\n * @property {number} [easeLinearity=0.2] The linearity of the easing of the inertial\n * movement.\n * @property {boolean} [worldCopyJump=false] With this option enabled, the map tracks\n * when you pan to another \"copy\" of the world and seamlessly jumps to the original one\n * so that all overlays like markers and vector layers are still visible. A map with an\n * infinite CRS, such as `SimpleCRS`, does not jump, as it has no copies of the world.\n * @property {number} [maxBoundsViscosity=0.0] If `maxBounds` is set, this option will\n * control how solid the bounds are when dragging the map around. The default value of\n * `0.0` allows the user to drag outside the bounds at normal speed, higher values will\n * slow down map dragging outside bounds, and `1.0` makes the bounds fully solid,\n * preventing the user from dragging outside the bounds.\n */\n\n/**\n * Handler of `MaplairMap` for dragging.\n * @typedef {object} DragHandlerMapProperties\n * @lends MaplairMap\n * @property {DragHandler} dragging Map dragging handler.\n */\n\n/**\n * Makes the map draggable (with panning inertia), enabled by default, see the map\n * option `dragging`.\n *\n * @fires MaplairMap#dragstart {MaplairEvent} Fired when the user starts dragging the map.\n * @fires MaplairMap#drag {MaplairEvent} Fired repeatedly while the user drags the map.\n * @fires MaplairMap#dragend {DragEndEvent} Fired when the user stops dragging the map.\n */\nexport class DragHandler extends Handler {\n\t/**\n\t * @param {...MaplairMap} args The map.\n\t */\n\tconstructor(...args) {\n\t\tsuper(...args);\n\t\tthis.callInitHooks(DragHandler);\n\t}\n\n\t/**\n\t * Called when the handler is enabled, adds the event hooks.\n\t */\n\taddHooks() {\n\t\tif (!this._draggable) {\n\t\t\tconst map = this._map;\n\n\t\t\tthis._draggable = new Draggable(map._mapPane, map._container);\n\n\t\t\tthis._draggable.on({\n\t\t\t\tdown: this._onDown,\n\t\t\t\tdragstart: this._onDragStart,\n\t\t\t\tdrag: this._onDrag,\n\t\t\t\tdragend: this._onDragEnd\n\t\t\t}, this);\n\n\t\t\tthis._draggable.on('predrag', this._onPreDragLimit, this);\n\t\t\tif (map.options.worldCopyJump) {\n\t\t\t\tthis._draggable.on('predrag', this._onPreDragWrap, this);\n\t\t\t}\n\t\t}\n\t\tthis._map._container.classList.add('maplair-grab', 'maplair-touch-drag');\n\t\tthis._draggable.enable();\n\t\tthis._positions = [];\n\t\tthis._times = [];\n\t}\n\n\t/**\n\t * Called when the handler is disabled, removes the event hooks.\n\t */\n\tremoveHooks() {\n\t\tthis._map._container.classList.remove('maplair-grab', 'maplair-touch-drag');\n\t\tthis._draggable.disable();\n\t}\n\n\t/**\n\t * Returns `true` if the map has been moved by the current or last drag.\n\t * @returns {boolean}\n\t */\n\tmoved() {\n\t\treturn this._draggable?._moved;\n\t}\n\n\t/**\n\t * Returns `true` while the map is being dragged.\n\t * @returns {boolean}\n\t */\n\tmoving() {\n\t\treturn this._draggable?._moving;\n\t}\n\n\t_onDown() {\n\t\t// Stop inertia before Draggable captures _startPos (#10376).\n\t\tthis._map._stop();\n\t}\n\n\t_onDragStart(e) {\n\t\tconst map = this._map;\n\n\t\tmap._stop();\n\t\tmap._setUserEvent(e.originalEvent);\n\t\tif (this._map.options.maxBounds && this._map.options.maxBoundsViscosity) {\n\t\t\tconst bounds = LatLngBounds.from(this._map.options.maxBounds);\n\n\t\t\tthis._offsetLimit = new Bounds(\n\t\t\t\tthis._map.latLngToContainerPoint(bounds.getNorthWest()).multiplyBy(-1),\n\t\t\t\tthis._map.latLngToContainerPoint(bounds.getSouthEast()).multiplyBy(-1)\n\t\t\t\t\t.add(this._map.getSize()));\n\n\t\t\tthis._viscosity = Math.min(1.0, Math.max(0.0, this._map.options.maxBoundsViscosity));\n\t\t} else {\n\t\t\tthis._offsetLimit = null;\n\t\t}\n\n\t\tmap\n\t\t\t.fire('movestart')\n\t\t\t.fire('dragstart');\n\n\t\tif (map.options.inertia) {\n\t\t\tthis._positions = [];\n\t\t\tthis._times = [];\n\t\t}\n\t}\n\n\t_onDrag(e) {\n\t\tif (this._map.options.inertia) {\n\t\t\tconst time = this._lastTime = Date.now(),\n\t\t\tpos = this._lastPos = this._draggable._absPos || this._draggable._newPos;\n\n\t\t\tthis._positions.push(pos);\n\t\t\tthis._times.push(time);\n\n\t\t\tthis._prunePositions(time);\n\t\t}\n\n\t\tthis._map\n\t\t\t.fire('move', e)\n\t\t\t.fire('drag', e);\n\t}\n\n\t_prunePositions(time) {\n\t\twhile (this._positions.length > 1 && time - this._times[0] > 50) {\n\t\t\tthis._positions.shift();\n\t\t\tthis._times.shift();\n\t\t}\n\t}\n\n\t_viscousLimit(value, threshold) {\n\t\treturn value - (value - threshold) * this._viscosity;\n\t}\n\n\t_onPreDragLimit() {\n\t\tif (!this._viscosity || !this._offsetLimit) { return; }\n\n\t\tconst offset = this._draggable._newPos.subtract(this._draggable._startPos);\n\n\t\tconst limit = this._offsetLimit;\n\t\tif (offset.x < limit.min.x) { offset.x = this._viscousLimit(offset.x, limit.min.x); }\n\t\tif (offset.y < limit.min.y) { offset.y = this._viscousLimit(offset.y, limit.min.y); }\n\t\tif (offset.x > limit.max.x) { offset.x = this._viscousLimit(offset.x, limit.max.x); }\n\t\tif (offset.y > limit.max.y) { offset.y = this._viscousLimit(offset.y, limit.max.y); }\n\n\t\tthis._draggable._newPos = this._draggable._startPos.add(offset);\n\t}\n\n\t_onPreDragWrap() {\n\t\tconst draggable = this._draggable;\n\t\tdraggable._absPos = draggable._newPos;\n\t\tdraggable._newPos = this._map._wrapWorldCopy(draggable._newPos);\n\t}\n\n\t_onDragEnd(e) {\n\t\tconst map = this._map,\n\t\toptions = map.options,\n\n\t\tnoInertia = !options.inertia || e.noInertia || this._times.length < 2;\n\n\t\tmap.fire('dragend', e);\n\n\t\tif (noInertia) {\n\t\t\tmap.fire('moveend');\n\n\t\t} else {\n\t\t\tthis._prunePositions(Date.now());\n\n\t\t\tconst direction = this._lastPos.subtract(this._positions[0]),\n\t\t\tduration = (this._lastTime - this._times[0]) / 1000,\n\t\t\tease = options.easeLinearity,\n\n\t\t\tspeedVector = direction.multiplyBy(ease / duration),\n\t\t\tspeed = speedVector.distanceTo([0, 0]),\n\n\t\t\tlimitedSpeed = Math.min(options.inertiaMaxSpeed, speed),\n\t\t\tlimitedSpeedVector = speedVector.multiplyBy(limitedSpeed / speed),\n\n\t\t\tmaxOffset = map.getSize().divideBy(2);\n\n\t\t\tlet decelerationDuration = limitedSpeed / (options.inertiaDeceleration * ease),\n\t\t\toffset = limitedSpeedVector.multiplyBy(-decelerationDuration / 2).round();\n\n\t\t\t// with reduced motion, shorten the coast to fit within half the map size; distance grows with the\n\t\t\t// square of duration, so scaling the duration by the square root keeps the deceleration the same\n\t\t\tif (map._prefersReducedMotion() && !maxOffset.contains(offset)) {\n\t\t\t\tconst k = Math.min(maxOffset.x / Math.abs(offset.x), maxOffset.y / Math.abs(offset.y));\n\t\t\t\toffset = offset.multiplyBy(k).round();\n\t\t\t\tdecelerationDuration *= Math.sqrt(k);\n\t\t\t}\n\n\t\t\tif (!offset.x && !offset.y) {\n\t\t\t\tmap.fire('moveend');\n\n\t\t\t} else {\n\t\t\t\toffset = map._limitOffset(offset, map.options.maxBounds);\n\n\t\t\t\trequestAnimationFrame(() => {\n\t\t\t\t\tmap.panBy(offset, {\n\t\t\t\t\t\tduration: decelerationDuration,\n\t\t\t\t\t\teaseLinearity: ease,\n\t\t\t\t\t\tnoMoveStart: true,\n\t\t\t\t\t\tanimate: true\n\t\t\t\t\t});\n\t\t\t\t});\n\t\t\t}\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.mergeOptions({\n\t\t\tdragging: true,\n\t\t\tinertia: true,\n\t\t\tinertiaDeceleration: 3400, // px/s^2\n\t\t\tinertiaMaxSpeed: 1400, // px/s\n\t\t\teaseLinearity: 0.2,\n\t\t\t// TODO refact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,SAAS,MAAM,GAAG;IAC/D,MAAM,IAAI,KAAK,IAAI,UAAU,IAAI,KAAK,IAAI,OAAO,CAAC,GAAG,UAAU,IAAI,KAAK,IAAI,OAAO,CAAC,CAAC;IACrF,SAAS,OAAO,WAAW,CAAC,CAAC,CAAC,MAAM;IACpC,wBAAwB,KAAK,KAAK,CAAC;GACpC;GAEA,IAAI,CAAC,OAAO,KAAK,CAAC,OAAO,GACxB,IAAI,KAAK,SAAS;QAEZ;IACN,SAAS,IAAI,aAAa,QAAQ,IAAI,QAAQ,SAAS;IAEvD,4BAA4B;KAC3B,IAAI,MAAM,QAAQ;MACjB,UAAU;MACV,eAAe;MACf,aAAa;MACb,SAAS;KACV,CAAC;IACF,CAAC;GACF;EACD;CACD;;;;;;CAOA,OAAO,SAAS,YAAY;EAC3B,WAAW,aAAa;GACvB,UAAU;GACV,SAAS;GACT,qBAAqB;GACrB,iBAAiB;GACjB,eAAe;GAEf,eAAe;GACf,oBAAoB;EACrB,CAAC;EAED,WAAW,YAAY,cAAc,YAAY,WAAW;CAC7D;AACD"}
1
+ {"version":3,"file":"DragHandler.js","names":[],"sources":["../../../../src/map/handler/DragHandler.js"],"sourcesContent":["import {Handler} from '../../core/Handler.js';\nimport {Draggable} from '../../dom/Draggable.js';\nimport {LatLngBounds} from '../../geo/LatLngBounds.js';\nimport {Bounds} from '../../geometry/Bounds.js';\n/** @import {MaplairMap} from '../Map.js' */\n/** @import {MaplairEvent, DragEndEvent} from '../../core/Events.js' */\n\n/**\n * Map options of `DragHandler`, the interaction options for dragging.\n * @typedef {object} DragHandlerMapOptions\n * @lends MaplairMapOptions\n * @property {boolean} [dragging=true] Whether the map is draggable with pointer or not.\n * @property {boolean} [inertia=true] If enabled, panning of the map will have an\n * inertia effect where the map builds momentum while dragging and continues moving in\n * the same direction for some time. Feels especially nice on touch devices.\n * @property {number} [inertiaDeceleration=3400] The rate with which the inertial\n * movement slows down, in pixels/second².\n * @property {number} [inertiaMaxSpeed=1400] Max speed of the inertial movement,\n * in pixels/second.\n * @property {number} [easeLinearity=0.2] The linearity of the easing of the inertial\n * movement.\n * @property {boolean} [worldCopyJump=false] With this option enabled, the map tracks\n * when you pan to another \"copy\" of the world and seamlessly jumps to the original one\n * so that all overlays like markers and vector layers are still visible. A map with an\n * infinite CRS, such as `SimpleCRS`, does not jump, as it has no copies of the world.\n * @property {number} [maxBoundsViscosity=0.0] If `maxBounds` is set, this option will\n * control how solid the bounds are when dragging the map around. The default value of\n * `0.0` allows the user to drag outside the bounds at normal speed, higher values will\n * slow down map dragging outside bounds, and `1.0` makes the bounds fully solid,\n * preventing the user from dragging outside the bounds.\n */\n\n/**\n * Handler of `MaplairMap` for dragging.\n * @typedef {object} DragHandlerMapProperties\n * @lends MaplairMap\n * @property {DragHandler} dragging Map dragging handler.\n */\n\n/**\n * Makes the map draggable (with panning inertia), enabled by default, see the map\n * option `dragging`.\n *\n * @fires MaplairMap#dragstart {MaplairEvent} Fired when the user starts dragging the map.\n * @fires MaplairMap#drag {MaplairEvent} Fired repeatedly while the user drags the map.\n * @fires MaplairMap#dragend {DragEndEvent} Fired when the user stops dragging the map.\n */\nexport class DragHandler extends Handler {\n\t/**\n\t * @param {...MaplairMap} args The map.\n\t */\n\tconstructor(...args) {\n\t\tsuper(...args);\n\t\tthis.callInitHooks(DragHandler);\n\t}\n\n\t/**\n\t * Called when the handler is enabled, adds the event hooks.\n\t */\n\taddHooks() {\n\t\tif (!this._draggable) {\n\t\t\tconst map = this._map;\n\n\t\t\tthis._draggable = new Draggable(map._mapPane, map._container);\n\n\t\t\tthis._draggable.on({\n\t\t\t\tdown: this._onDown,\n\t\t\t\tdragstart: this._onDragStart,\n\t\t\t\tdrag: this._onDrag,\n\t\t\t\tdragend: this._onDragEnd\n\t\t\t}, this);\n\n\t\t\tthis._draggable.on('predrag', this._onPreDragLimit, this);\n\t\t\tif (map.options.worldCopyJump) {\n\t\t\t\tthis._draggable.on('predrag', this._onPreDragWrap, this);\n\t\t\t}\n\t\t}\n\t\tthis._map._container.classList.add('maplair-grab', 'maplair-touch-drag');\n\t\tthis._draggable.enable();\n\t\tthis._positions = [];\n\t\tthis._times = [];\n\t}\n\n\t/**\n\t * Called when the handler is disabled, removes the event hooks.\n\t */\n\tremoveHooks() {\n\t\tthis._map._container.classList.remove('maplair-grab', 'maplair-touch-drag');\n\t\tthis._draggable.disable();\n\t}\n\n\t/**\n\t * Returns `true` if the map has been moved by the current or last drag.\n\t * @returns {boolean}\n\t */\n\tmoved() {\n\t\treturn this._draggable?._moved;\n\t}\n\n\t/**\n\t * Returns `true` while the map is being dragged.\n\t * @returns {boolean}\n\t */\n\tmoving() {\n\t\treturn this._draggable?._moving;\n\t}\n\n\t_onDown() {\n\t\t// Stop inertia before Draggable captures _startPos (#10376).\n\t\tthis._map._stop();\n\t}\n\n\t_onDragStart(e) {\n\t\tconst map = this._map;\n\n\t\tmap._stop();\n\t\tmap._setUserEvent(e.originalEvent);\n\t\tif (this._map.options.maxBounds && this._map.options.maxBoundsViscosity) {\n\t\t\tconst bounds = LatLngBounds.from(this._map.options.maxBounds),\n\t\t\t// any corner can be an extreme when the projection rotates the axes\n\t\t\tcorners = [bounds.getNorthWest(), bounds.getNorthEast(), bounds.getSouthEast(), bounds.getSouthWest()],\n\t\t\t{min, max} = new Bounds(corners.map(c => this._map.latLngToContainerPoint(c)));\n\n\t\t\tthis._offsetLimit = new Bounds(max.multiplyBy(-1), min.multiplyBy(-1).add(this._map.getSize()));\n\n\t\t\tthis._viscosity = Math.min(1.0, Math.max(0.0, this._map.options.maxBoundsViscosity));\n\t\t} else {\n\t\t\tthis._offsetLimit = null;\n\t\t}\n\n\t\tmap\n\t\t\t.fire('movestart')\n\t\t\t.fire('dragstart');\n\n\t\tif (map.options.inertia) {\n\t\t\tthis._positions = [];\n\t\t\tthis._times = [];\n\t\t}\n\t}\n\n\t_onDrag(e) {\n\t\tif (this._map.options.inertia) {\n\t\t\tconst time = this._lastTime = Date.now(),\n\t\t\tpos = this._lastPos = this._draggable._absPos || this._draggable._newPos;\n\n\t\t\tthis._positions.push(pos);\n\t\t\tthis._times.push(time);\n\n\t\t\tthis._prunePositions(time);\n\t\t}\n\n\t\tthis._map\n\t\t\t.fire('move', e)\n\t\t\t.fire('drag', e);\n\t}\n\n\t_prunePositions(time) {\n\t\twhile (this._positions.length > 1 && time - this._times[0] > 50) {\n\t\t\tthis._positions.shift();\n\t\t\tthis._times.shift();\n\t\t}\n\t}\n\n\t_viscousLimit(value, threshold) {\n\t\treturn value - (value - threshold) * this._viscosity;\n\t}\n\n\t_onPreDragLimit() {\n\t\tif (!this._viscosity || !this._offsetLimit) { return; }\n\n\t\tconst offset = this._draggable._newPos.subtract(this._draggable._startPos);\n\n\t\tconst limit = this._offsetLimit;\n\t\tif (offset.x < limit.min.x) { offset.x = this._viscousLimit(offset.x, limit.min.x); }\n\t\tif (offset.y < limit.min.y) { offset.y = this._viscousLimit(offset.y, limit.min.y); }\n\t\tif (offset.x > limit.max.x) { offset.x = this._viscousLimit(offset.x, limit.max.x); }\n\t\tif (offset.y > limit.max.y) { offset.y = this._viscousLimit(offset.y, limit.max.y); }\n\n\t\tthis._draggable._newPos = this._draggable._startPos.add(offset);\n\t}\n\n\t_onPreDragWrap() {\n\t\tconst draggable = this._draggable;\n\t\tdraggable._absPos = draggable._newPos;\n\t\tdraggable._newPos = this._map._wrapWorldCopy(draggable._newPos);\n\t}\n\n\t_onDragEnd(e) {\n\t\tconst map = this._map,\n\t\toptions = map.options,\n\n\t\tnoInertia = !options.inertia || e.noInertia || this._times.length < 2;\n\n\t\tmap.fire('dragend', e);\n\n\t\tif (noInertia) {\n\t\t\tmap.fire('moveend');\n\n\t\t} else {\n\t\t\tthis._prunePositions(Date.now());\n\n\t\t\tconst direction = this._lastPos.subtract(this._positions[0]),\n\t\t\tduration = (this._lastTime - this._times[0]) / 1000,\n\t\t\tease = options.easeLinearity,\n\n\t\t\tspeedVector = direction.multiplyBy(ease / duration),\n\t\t\tspeed = speedVector.distanceTo([0, 0]),\n\n\t\t\tlimitedSpeed = Math.min(options.inertiaMaxSpeed, speed),\n\t\t\tlimitedSpeedVector = speedVector.multiplyBy(limitedSpeed / speed),\n\n\t\t\tmaxOffset = map.getSize().divideBy(2);\n\n\t\t\tlet decelerationDuration = limitedSpeed / (options.inertiaDeceleration * ease),\n\t\t\toffset = limitedSpeedVector.multiplyBy(-decelerationDuration / 2).round();\n\n\t\t\t// with reduced motion, shorten the coast to fit within half the map size; distance grows with the\n\t\t\t// square of duration, so scaling the duration by the square root keeps the deceleration the same\n\t\t\tif (map._prefersReducedMotion() && !maxOffset.contains(offset)) {\n\t\t\t\tconst k = Math.min(maxOffset.x / Math.abs(offset.x), maxOffset.y / Math.abs(offset.y));\n\t\t\t\toffset = offset.multiplyBy(k).round();\n\t\t\t\tdecelerationDuration *= Math.sqrt(k);\n\t\t\t}\n\n\t\t\tif (!offset.x && !offset.y) {\n\t\t\t\tmap.fire('moveend');\n\n\t\t\t} else {\n\t\t\t\toffset = map._limitOffset(offset, map.options.maxBounds);\n\n\t\t\t\trequestAnimationFrame(() => {\n\t\t\t\t\tmap.panBy(offset, {\n\t\t\t\t\t\tduration: decelerationDuration,\n\t\t\t\t\t\teaseLinearity: ease,\n\t\t\t\t\t\tnoMoveStart: true,\n\t\t\t\t\t\tanimate: true\n\t\t\t\t\t});\n\t\t\t\t});\n\t\t\t}\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.mergeOptions({\n\t\t\tdragging: true,\n\t\t\tinertia: true,\n\t\t\tinertiaDeceleration: 3400, // px/s^2\n\t\t\tinertiaMaxSpeed: 1400, // px/s\n\t\t\teaseLinearity: 0.2,\n\t\t\t// TODO refactor, move to CRS\n\t\t\tworldCopyJump: false,\n\t\t\tmaxBoundsViscosity: 0.0\n\t\t});\n\n\t\tMaplairMap.addInitHook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UAAU,SAAS,MAAM,GAAG;IAC/D,MAAM,IAAI,KAAK,IAAI,UAAU,IAAI,KAAK,IAAI,OAAO,CAAC,GAAG,UAAU,IAAI,KAAK,IAAI,OAAO,CAAC,CAAC;IACrF,SAAS,OAAO,WAAW,CAAC,CAAC,CAAC,MAAM;IACpC,wBAAwB,KAAK,KAAK,CAAC;GACpC;GAEA,IAAI,CAAC,OAAO,KAAK,CAAC,OAAO,GACxB,IAAI,KAAK,SAAS;QAEZ;IACN,SAAS,IAAI,aAAa,QAAQ,IAAI,QAAQ,SAAS;IAEvD,4BAA4B;KAC3B,IAAI,MAAM,QAAQ;MACjB,UAAU;MACV,eAAe;MACf,aAAa;MACb,SAAS;KACV,CAAC;IACF,CAAC;GACF;EACD;CACD;;;;;;CAOA,OAAO,SAAS,YAAY;EAC3B,WAAW,aAAa;GACvB,UAAU;GACV,SAAS;GACT,qBAAqB;GACrB,iBAAiB;GACjB,eAAe;GAEf,eAAe;GACf,oBAAoB;EACrB,CAAC;EAED,WAAW,YAAY,cAAc,YAAY,WAAW;CAC7D;AACD"}
@@ -62,7 +62,7 @@ var KeyboardHandler = class KeyboardHandler extends Handler {
62
62
  const container = this._map._container;
63
63
  if (container.tabIndex <= 0) container.tabIndex = 0;
64
64
  const shortcuts = this._map.options.ariaKeyShortcuts;
65
- if (shortcuts) container.ariaKeyShortcuts = shortcuts === true ? "ArrowLeft ArrowRight ArrowUp ArrowDown = Plus -" : shortcuts;
65
+ if (shortcuts) container.setAttribute("aria-keyshortcuts", shortcuts === true ? "ArrowLeft ArrowRight ArrowUp ArrowDown = Plus -" : shortcuts);
66
66
  on(container, {
67
67
  focus: this._onFocus,
68
68
  blur: this._onBlur,
@@ -118,7 +118,7 @@ var KeyboardHandler = class KeyboardHandler extends Handler {
118
118
  return help;
119
119
  }
120
120
  _onPointerDown() {
121
- if (this._focused) return;
121
+ if (this._focused || this._map._container.contains(document.activeElement)) return;
122
122
  this._map._container.focus({
123
123
  preventScroll: true,
124
124
  focusVisible: false
@@ -136,11 +136,9 @@ var KeyboardHandler = class KeyboardHandler extends Handler {
136
136
  }
137
137
  _onContainerKeyDown(e) {
138
138
  const map = this._map, popup = map._popup;
139
- if (e.key !== "Escape" || e.defaultPrevented || !popup?.options.closeOnEscapeKey || map._controlContainer?.contains(e.target)) return;
140
- const inPopup = popup._container.contains(e.target), icon = popup._source?._icon;
139
+ if (e.key !== "Escape" || e.defaultPrevented || !popup?.isOpen() || !popup.options.closeOnEscapeKey || map._controlContainer?.contains(e.target)) return;
141
140
  map.closePopup();
142
141
  e.preventDefault();
143
- if (inPopup) (icon?.tabIndex >= 0 ? icon : map._container).focus();
144
142
  }
145
143
  _onKeyDown(e) {
146
144
  if (e.altKey || e.ctrlKey || e.metaKey) return;
@@ -1 +1 @@
1
- {"version":3,"file":"KeyboardHandler.js","names":["DomUtil.create"],"sources":["../../../../src/map/handler/KeyboardHandler.js"],"sourcesContent":["import {Handler} from '../../core/Handler.js';\nimport {on, off, stop, disableClickPropagation, disableScrollPropagation} from '../../dom/DomEvent.js';\nimport * as DomUtil from '../../dom/DomUtil.js';\nimport {Point} from '../../geometry/Point.js';\nimport {I18n} from '../../core/I18n.js';\n/** @import {MaplairMap} from '../Map.js' */\n/** @import {MaplairEvent} from '../../core/Events.js' */\n\n/**\n * Map options of `KeyboardHandler`, the keyboard navigation options.\n * @typedef {object} KeyboardHandlerMapOptions\n * @lends MaplairMapOptions\n * @property {boolean} [keyboard=true] Makes the map focusable and allows users to\n * navigate the map with keyboard arrows and `+`/`-` keys.\n * @property {number} [keyboardPanDelta=80] Amount of pixels to pan when pressing an\n * arrow key.\n * @property {boolean|string} [ariaKeyShortcuts=false] Sets the `aria-keyshortcuts`\n * attribute of the map container. ARIA names keys by what they type on a given layout,\n * so `true` uses the names for a US English layout, which can be wrong for pages in\n * other languages; pass a string to set the value for your audience's layout instead.\n */\n\n/**\n * Handler of `MaplairMap` for keyboard navigation.\n * @typedef {object} KeyboardHandlerMapProperties\n * @lends MaplairMap\n * @property {KeyboardHandler} keyboard Keyboard navigation handler.\n */\n\n/**\n * Handles keyboard interaction with the map, enabled by default, see the map option\n * `keyboard`. The `?` key shows a list of the keyboard shortcuts.\n *\n * @fires MaplairMap#focus {MaplairEvent} Fired when the map gains focus.\n * @fires MaplairMap#blur {MaplairEvent} Fired when the map loses focus.\n */\nexport class KeyboardHandler extends Handler {\n\n\t/**\n\t * The [values](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)\n\t * of the keys for panning and zooming the map, so zoom keys follow the keyboard\n\t * layout and cover the numpad too; `=` and `_` keep the US `+` and `-` keys working\n\t * both with and without Shift.\n\t * @type {Record<'left'|'right'|'down'|'up'|'zoomIn'|'zoomOut', string[]>}\n\t */\n\tstatic keys = {\n\t\tleft: ['ArrowLeft'],\n\t\tright: ['ArrowRight'],\n\t\tdown: ['ArrowDown'],\n\t\tup: ['ArrowUp'],\n\t\tzoomIn: ['+', '='],\n\t\tzoomOut: ['-', '_']\n\t};\n\n\t/**\n\t * @param {MaplairMap} map\n\t */\n\tconstructor(map) {\n\t\tsuper(map);\n\t\tthis.callInitHooks(KeyboardHandler);\n\t}\n\n\t/**\n\t * Called when the handler is enabled, adds the event hooks.\n\t */\n\taddHooks() {\n\t\tconst container = this._map._container;\n\n\t\t// make the container focusable by tabbing\n\t\tif (container.tabIndex <= 0) {\n\t\t\tcontainer.tabIndex = 0;\n\t\t}\n\n\t\tconst shortcuts = this._map.options.ariaKeyShortcuts;\n\t\tif (shortcuts) {\n\t\t\t// ARIA names physical keys by their unshifted character, so the default is for a US layout\n\t\t\tcontainer.ariaKeyShortcuts = shortcuts === true ?\n\t\t\t\t'ArrowLeft ArrowRight ArrowUp ArrowDown = Plus -' : shortcuts;\n\t\t}\n\n\t\ton(container, {\n\t\t\tfocus: this._onFocus,\n\t\t\tblur: this._onBlur,\n\t\t\tpointerdown: this._onPointerDown,\n\t\t\tkeydown: this._onContainerKeyDown\n\t\t}, this);\n\t}\n\n\t/**\n\t * Called when the handler is disabled, removes the event hooks.\n\t */\n\tremoveHooks() {\n\t\toff(document, 'keydown', this._onKeyDown, this);\n\n\t\tif (this._map.options.ariaKeyShortcuts) {\n\t\t\tthis._map._container.removeAttribute('aria-keyshortcuts');\n\t\t}\n\n\t\toff(this._map._container, {\n\t\t\tfocus: this._onFocus,\n\t\t\tblur: this._onBlur,\n\t\t\tpointerdown: this._onPointerDown,\n\t\t\tkeydown: this._onContainerKeyDown\n\t\t}, this);\n\n\t\tthis._help?.remove();\n\t\tthis._help = null;\n\t}\n\n\t// Shows or hides a list of the keyboard shortcuts (on the `?` key), as popover\n\t// closed by pressing `Escape` or clicking outside of it.\n\t_toggleHelp() {\n\t\tthis._help ??= this._createHelp();\n\t\tthis._help.togglePopover();\n\t}\n\n\t_createHelp() {\n\t\tconst help = DomUtil.create('div', 'maplair-keyboard-help', this._map._container),\n\t\ttitle = I18n.translate('Keyboard shortcuts');\n\t\thelp.popover = 'auto';\n\t\thelp.setAttribute('role', 'dialog');\n\t\thelp.setAttribute('aria-label', title);\n\n\t\tDomUtil.create('h2', '', help).textContent = title;\n\t\tconst list = DomUtil.create('dl', '', help);\n\t\tfor (const [keys, description] of [\n\t\t\t[['←', '→', '↑', '↓'], I18n.translate('Pan the map')],\n\t\t\t[['+'], I18n.translate('Zoom in')],\n\t\t\t[['−'], I18n.translate('Zoom out')],\n\t\t\t[['Shift'], I18n.translate('Pan or zoom further, together with the keys above')],\n\t\t\t[['Esc'], I18n.translate('Close popup')],\n\t\t\t[['?'], I18n.translate('Show or hide the keyboard shortcuts')]\n\t\t]) {\n\t\t\tconst row = DomUtil.create('div', '', list),\n\t\t\tdt = DomUtil.create('dt', '', row);\n\t\t\tfor (const key of keys) {\n\t\t\t\tDomUtil.create('kbd', '', dt).textContent = key;\n\t\t\t}\n\t\t\tDomUtil.create('dd', '', row).textContent = description;\n\t\t}\n\n\t\tdisableClickPropagation(help);\n\t\tdisableScrollPropagation(help);\n\t\treturn help;\n\t}\n\n\t// acquire/lose focus #594, #1228, #1540\n\t_onPointerDown() {\n\t\tif (this._focused) { return; }\n\n\t\t// script focus matches :focus-visible by default, which would show a focus ring on every click\n\t\tthis._map._container.focus({preventScroll: true, focusVisible: false});\n\t}\n\n\t_onFocus() {\n\t\tthis._focused = true;\n\t\ton(document, 'keydown', this._onKeyDown, this);\n\t\tthis._map.fire('focus');\n\t}\n\n\t_onBlur() {\n\t\tthis._focused = false;\n\t\toff(document, 'keydown', this._onKeyDown, this);\n\t\tthis._map.fire('blur');\n\t}\n\n\t// on the container rather than the document, so that Escape also works when focus is on a marker or\n\t// inside the popup, which blurs the container and removes the document listener; controls are left\n\t// out so that e.g. Escape in a geocoder input doesn't close an unrelated popup\n\t_onContainerKeyDown(e) {\n\t\tconst map = this._map,\n\t\tpopup = map._popup;\n\n\t\tif (e.key !== 'Escape' || e.defaultPrevented || !popup?.options.closeOnEscapeKey ||\n\t\t\tmap._controlContainer?.contains(e.target)) { return; }\n\n\t\tconst inPopup = popup._container.contains(e.target),\n\t\ticon = popup._source?._icon;\n\n\t\tmap.closePopup();\n\t\te.preventDefault();\n\n\t\t// closing removes the focused element, which would send focus back to the start of the page\n\t\tif (inPopup) { (icon?.tabIndex >= 0 ? icon : map._container).focus(); }\n\t}\n\n\t_onKeyDown(e) {\n\t\tif (e.altKey || e.ctrlKey || e.metaKey) { return; }\n\n\t\tif (e.key === '?') {\n\t\t\tthis._toggleHelp();\n\t\t\tstop(e);\n\t\t\treturn;\n\t\t}\n\n\t\tconst key = e.key,\n\t\tmap = this._map,\n\t\t{left, right, down, up, zoomIn, zoomOut} = KeyboardHandler.keys,\n\t\tx = +right.includes(key) - +left.includes(key),\n\t\ty = +down.includes(key) - +up.includes(key),\n\t\tdz = +zoomIn.includes(key) - +zoomOut.includes(key),\n\t\tk = e.shiftKey ? 3 : 1;\n\n\t\tif (x || y) {\n\t\t\tif (!map._panAnim?._inProgress) {\n\t\t\t\tlet offset = new Point(x, y).multiplyBy(k * map.options.keyboardPanDelta);\n\t\t\t\tif (map.options.maxBounds) {\n\t\t\t\t\toffset = map._limitOffset(offset, map.options.maxBounds);\n\t\t\t\t}\n\n\t\t\t\tmap._setUserEvent(e);\n\t\t\t\tmap.panBy(offset);\n\t\t\t}\n\t\t} else if (dz) {\n\t\t\tif (!map._zoomInputBlocked()) {\n\t\t\t\tmap._setUserEvent(e);\n\t\t\t\tmap.setZoom(map.getZoom() + k * dz * map.options.zoomDelta);\n\t\t\t}\n\n\t\t} else {\n\t\t\treturn;\n\t\t}\n\n\t\tstop(e);\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.mergeOptions({\n\t\t\tkeyboard: true,\n\t\t\tkeyboardPanDelta: 80,\n\t\t\tariaKeyShortcuts: false\n\t\t});\n\n\t\tMaplairMap.addInitHook('addHandler', 'keyboard', KeyboardHandler);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,kBAAb,MAAa,wBAAwB,QAAQ;;;;;;;;CAS5C,OAAO,OAAO;EACb,MAAM,CAAC,WAAW;EAClB,OAAO,CAAC,YAAY;EACpB,MAAM,CAAC,WAAW;EAClB,IAAI,CAAC,SAAS;EACd,QAAQ,CAAC,KAAK,GAAG;EACjB,SAAS,CAAC,KAAK,GAAG;CACnB;;;;CAKA,YAAY,KAAK;EAChB,MAAM,GAAG;EACT,KAAK,cAAc,eAAe;CACnC;;;;CAKA,WAAW;EACV,MAAM,YAAY,KAAK,KAAK;EAG5B,IAAI,UAAU,YAAY,GACzB,UAAU,WAAW;EAGtB,MAAM,YAAY,KAAK,KAAK,QAAQ;EACpC,IAAI,WAEH,UAAU,mBAAmB,cAAc,OAC1C,oDAAoD;EAGtD,GAAG,WAAW;GACb,OAAO,KAAK;GACZ,MAAM,KAAK;GACX,aAAa,KAAK;GAClB,SAAS,K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AAQ,SAAS,GAAG,GAClD,IAAI,EAAE,WAAW,IAAI;EAErB,IAAI,KAAK,GACJ;OAAA,CAAC,IAAI,UAAU,aAAa;IAC/B,IAAI,SAAS,IAAI,MAAM,GAAG,CAAC,CAAC,CAAC,WAAW,IAAI,IAAI,QAAQ,gBAAgB;IACxE,IAAI,IAAI,QAAQ,WACf,SAAS,IAAI,aAAa,QAAQ,IAAI,QAAQ,SAAS;IAGxD,IAAI,cAAc,CAAC;IACnB,IAAI,MAAM,MAAM;GACjB;SACM,IAAI,IACN;OAAA,CAAC,IAAI,kBAAkB,GAAG;IAC7B,IAAI,cAAc,CAAC;IACnB,IAAI,QAAQ,IAAI,QAAQ,IAAI,IAAI,KAAK,IAAI,QAAQ,SAAS;GAC3D;SAGA;EAGD,KAAK,CAAC;CACP;;;;;;CAOA,OAAO,SAAS,YAAY;EAC3B,WAAW,aAAa;GACvB,UAAU;GACV,kBAAkB;GAClB,kBAAkB;EACnB,CAAC;EAED,WAAW,YAAY,cAAc,YAAY,eAAe;CACjE;AACD"}
1
+ {"version":3,"file":"KeyboardHandler.js","names":["DomUtil.create"],"sources":["../../../../src/map/handler/KeyboardHandler.js"],"sourcesContent":["import {Handler} from '../../core/Handler.js';\nimport {on, off, stop, disableClickPropagation, disableScrollPropagation} from '../../dom/DomEvent.js';\nimport * as DomUtil from '../../dom/DomUtil.js';\nimport {Point} from '../../geometry/Point.js';\nimport {I18n} from '../../core/I18n.js';\n/** @import {MaplairMap} from '../Map.js' */\n/** @import {MaplairEvent} from '../../core/Events.js' */\n\n/**\n * Map options of `KeyboardHandler`, the keyboard navigation options.\n * @typedef {object} KeyboardHandlerMapOptions\n * @lends MaplairMapOptions\n * @property {boolean} [keyboard=true] Makes the map focusable and allows users to\n * navigate the map with keyboard arrows and `+`/`-` keys.\n * @property {number} [keyboardPanDelta=80] Amount of pixels to pan when pressing an\n * arrow key.\n * @property {boolean|string} [ariaKeyShortcuts=false] Sets the `aria-keyshortcuts`\n * attribute of the map container. ARIA names keys by what they type on a given layout,\n * so `true` uses the names for a US English layout, which can be wrong for pages in\n * other languages; pass a string to set the value for your audience's layout instead.\n */\n\n/**\n * Handler of `MaplairMap` for keyboard navigation.\n * @typedef {object} KeyboardHandlerMapProperties\n * @lends MaplairMap\n * @property {KeyboardHandler} keyboard Keyboard navigation handler.\n */\n\n/**\n * Handles keyboard interaction with the map, enabled by default, see the map option\n * `keyboard`. The `?` key shows a list of the keyboard shortcuts.\n *\n * @fires MaplairMap#focus {MaplairEvent} Fired when the map gains focus.\n * @fires MaplairMap#blur {MaplairEvent} Fired when the map loses focus.\n */\nexport class KeyboardHandler extends Handler {\n\n\t/**\n\t * The [values](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)\n\t * of the keys for panning and zooming the map, so zoom keys follow the keyboard\n\t * layout and cover the numpad too; `=` and `_` keep the US `+` and `-` keys working\n\t * both with and without Shift.\n\t * @type {Record<'left'|'right'|'down'|'up'|'zoomIn'|'zoomOut', string[]>}\n\t */\n\tstatic keys = {\n\t\tleft: ['ArrowLeft'],\n\t\tright: ['ArrowRight'],\n\t\tdown: ['ArrowDown'],\n\t\tup: ['ArrowUp'],\n\t\tzoomIn: ['+', '='],\n\t\tzoomOut: ['-', '_']\n\t};\n\n\t/**\n\t * @param {MaplairMap} map\n\t */\n\tconstructor(map) {\n\t\tsuper(map);\n\t\tthis.callInitHooks(KeyboardHandler);\n\t}\n\n\t/**\n\t * Called when the handler is enabled, adds the event hooks.\n\t */\n\taddHooks() {\n\t\tconst container = this._map._container;\n\n\t\t// make the container focusable by tabbing\n\t\tif (container.tabIndex <= 0) {\n\t\t\tcontainer.tabIndex = 0;\n\t\t}\n\n\t\tconst shortcuts = this._map.options.ariaKeyShortcuts;\n\t\tif (shortcuts) {\n\t\t\t// ARIA names physical keys by their unshifted character, so the default is for a US layout\n\t\t\tcontainer.setAttribute('aria-keyshortcuts', shortcuts === true ?\n\t\t\t\t'ArrowLeft ArrowRight ArrowUp ArrowDown = Plus -' : shortcuts);\n\t\t}\n\n\t\ton(container, {\n\t\t\tfocus: this._onFocus,\n\t\t\tblur: this._onBlur,\n\t\t\tpointerdown: this._onPointerDown,\n\t\t\tkeydown: this._onContainerKeyDown\n\t\t}, this);\n\t}\n\n\t/**\n\t * Called when the handler is disabled, removes the event hooks.\n\t */\n\tremoveHooks() {\n\t\toff(document, 'keydown', this._onKeyDown, this);\n\n\t\tif (this._map.options.ariaKeyShortcuts) {\n\t\t\tthis._map._container.removeAttribute('aria-keyshortcuts');\n\t\t}\n\n\t\toff(this._map._container, {\n\t\t\tfocus: this._onFocus,\n\t\t\tblur: this._onBlur,\n\t\t\tpointerdown: this._onPointerDown,\n\t\t\tkeydown: this._onContainerKeyDown\n\t\t}, this);\n\n\t\tthis._help?.remove();\n\t\tthis._help = null;\n\t}\n\n\t// Shows or hides a list of the keyboard shortcuts (on the `?` key), as popover\n\t// closed by pressing `Escape` or clicking outside of it.\n\t_toggleHelp() {\n\t\tthis._help ??= this._createHelp();\n\t\tthis._help.togglePopover();\n\t}\n\n\t_createHelp() {\n\t\tconst help = DomUtil.create('div', 'maplair-keyboard-help', this._map._container),\n\t\ttitle = I18n.translate('Keyboard shortcuts');\n\t\thelp.popover = 'auto';\n\t\thelp.setAttribute('role', 'dialog');\n\t\thelp.setAttribute('aria-label', title);\n\n\t\tDomUtil.create('h2', '', help).textContent = title;\n\t\tconst list = DomUtil.create('dl', '', help);\n\t\tfor (const [keys, description] of [\n\t\t\t[['←', '→', '↑', '↓'], I18n.translate('Pan the map')],\n\t\t\t[['+'], I18n.translate('Zoom in')],\n\t\t\t[['−'], I18n.translate('Zoom out')],\n\t\t\t[['Shift'], I18n.translate('Pan or zoom further, together with the keys above')],\n\t\t\t[['Esc'], I18n.translate('Close popup')],\n\t\t\t[['?'], I18n.translate('Show or hide the keyboard shortcuts')]\n\t\t]) {\n\t\t\tconst row = DomUtil.create('div', '', list),\n\t\t\tdt = DomUtil.create('dt', '', row);\n\t\t\tfor (const key of keys) {\n\t\t\t\tDomUtil.create('kbd', '', dt).textContent = key;\n\t\t\t}\n\t\t\tDomUtil.create('dd', '', row).textContent = description;\n\t\t}\n\n\t\tdisableClickPropagation(help);\n\t\tdisableScrollPropagation(help);\n\t\treturn help;\n\t}\n\n\t// acquire/lose focus #594, #1228, #1540\n\t_onPointerDown() {\n\t\t// a screen reader's simulated click on a marker has no mousedown to refocus the marker after this\n\t\tif (this._focused || this._map._container.contains(document.activeElement)) { return; }\n\n\t\t// script focus matches :focus-visible by default, which would show a focus ring on every click\n\t\tthis._map._container.focus({preventScroll: true, focusVisible: false});\n\t}\n\n\t_onFocus() {\n\t\tthis._focused = true;\n\t\ton(document, 'keydown', this._onKeyDown, this);\n\t\tthis._map.fire('focus');\n\t}\n\n\t_onBlur() {\n\t\tthis._focused = false;\n\t\toff(document, 'keydown', this._onKeyDown, this);\n\t\tthis._map.fire('blur');\n\t}\n\n\t// on the container rather than the document, so that Escape also works when focus is on a marker or\n\t// inside the popup, which blurs the container and removes the document listener; controls are left\n\t// out so that e.g. Escape in a geocoder input doesn't close an unrelated popup\n\t_onContainerKeyDown(e) {\n\t\tconst map = this._map,\n\t\tpopup = map._popup;\n\n\t\tif (e.key !== 'Escape' || e.defaultPrevented || !popup?.isOpen() || !popup.options.closeOnEscapeKey ||\n\t\t\tmap._controlContainer?.contains(e.target)) { return; }\n\n\t\tmap.closePopup();\n\t\te.preventDefault();\n\t}\n\n\t_onKeyDown(e) {\n\t\tif (e.altKey || e.ctrlKey || e.metaKey) { return; }\n\n\t\tif (e.key === '?') {\n\t\t\tthis._toggleHelp();\n\t\t\tstop(e);\n\t\t\treturn;\n\t\t}\n\n\t\tconst key = e.key,\n\t\tmap = this._map,\n\t\t{left, right, down, up, zoomIn, zoomOut} = KeyboardHandler.keys,\n\t\tx = +right.includes(key) - +left.includes(key),\n\t\ty = +down.includes(key) - +up.includes(key),\n\t\tdz = +zoomIn.includes(key) - +zoomOut.includes(key),\n\t\tk = e.shiftKey ? 3 : 1;\n\n\t\tif (x || y) {\n\t\t\tif (!map._panAnim?._inProgress) {\n\t\t\t\tlet offset = new Point(x, y).multiplyBy(k * map.options.keyboardPanDelta);\n\t\t\t\tif (map.options.maxBounds) {\n\t\t\t\t\toffset = map._limitOffset(offset, map.options.maxBounds);\n\t\t\t\t}\n\n\t\t\t\tmap._setUserEvent(e);\n\t\t\t\tmap.panBy(offset);\n\t\t\t}\n\t\t} else if (dz) {\n\t\t\tif (!map._zoomInputBlocked()) {\n\t\t\t\tmap._setUserEvent(e);\n\t\t\t\tmap.setZoom(map.getZoom() + k * dz * map.options.zoomDelta);\n\t\t\t}\n\n\t\t} else {\n\t\t\treturn;\n\t\t}\n\n\t\tstop(e);\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.mergeOptions({\n\t\t\tkeyboard: true,\n\t\t\tkeyboardPanDelta: 80,\n\t\t\tariaKeyShortcuts: false\n\t\t});\n\n\t\tMaplairMap.addInitHook('addHandler', 'keyboard', KeyboardHandler);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCA,IAAa,kBAAb,MAAa,wBAAwB,QAAQ;;;;;;;;CAS5C,OAAO,OAAO;EACb,MAAM,CAAC,WAAW;EAClB,OAAO,CAAC,YAAY;EACpB,MAAM,CAAC,WAAW;EAClB,IAAI,CAAC,SAAS;EACd,QAAQ,CAAC,KAAK,GAAG;EACjB,SAAS,CAAC,KAAK,GAAG;CACnB;;;;CAKA,YAAY,KAAK;EAChB,MAAM,GAAG;EACT,KAAK,cAAc,eAAe;CACnC;;;;CAKA,WAAW;EACV,MAAM,YAAY,KAAK,KAAK;EAG5B,IAAI,UAAU,YAAY,GACzB,UAAU,WAAW;EAGtB,MAAM,YAAY,KAAK,KAAK,QAAQ;EACpC,IAAI,WAEH,UAAU,aAAa,qBA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AC,OAAO,SAAS,GAAG,IAAI,CAAC,QAAQ,SAAS,GAAG,GAClD,IAAI,EAAE,WAAW,IAAI;EAErB,IAAI,KAAK,GACJ;OAAA,CAAC,IAAI,UAAU,aAAa;IAC/B,IAAI,SAAS,IAAI,MAAM,GAAG,CAAC,CAAC,CAAC,WAAW,IAAI,IAAI,QAAQ,gBAAgB;IACxE,IAAI,IAAI,QAAQ,WACf,SAAS,IAAI,aAAa,QAAQ,IAAI,QAAQ,SAAS;IAGxD,IAAI,cAAc,CAAC;IACnB,IAAI,MAAM,MAAM;GACjB;SACM,IAAI,IACN;OAAA,CAAC,IAAI,kBAAkB,GAAG;IAC7B,IAAI,cAAc,CAAC;IACnB,IAAI,QAAQ,IAAI,QAAQ,IAAI,IAAI,KAAK,IAAI,QAAQ,SAAS;GAC3D;SAGA;EAGD,KAAK,CAAC;CACP;;;;;;CAOA,OAAO,SAAS,YAAY;EAC3B,WAAW,aAAa;GACvB,UAAU;GACV,kBAAkB;GAClB,kBAAkB;EACnB,CAAC;EAED,WAAW,YAAY,cAAc,YAAY,eAAe;CACjE;AACD"}
@@ -69,8 +69,8 @@ var PinchZoomHandler = class PinchZoomHandler extends Handler {
69
69
  this._moved = false;
70
70
  this._zooming = true;
71
71
  map._stop();
72
- on(this._map._container, "pointermove", this._onPointerMove, this);
73
- on(this._map._container, "pointerup pointercancel", this._onPointerEnd, this);
72
+ on(map._container, "pointermove", this._onPointerMove, this);
73
+ on(map._container, "pointerup pointercancel", this._onPointerEnd, this);
74
74
  e.preventDefault();
75
75
  }
76
76
  _onPointerMove(e) {
@@ -93,10 +93,7 @@ var PinchZoomHandler = class PinchZoomHandler extends Handler {
93
93
  this._moved = true;
94
94
  }
95
95
  cancelAnimationFrame(this._animRequest);
96
- const moveFn = map._move.bind(map, this._center, this._zoom, {
97
- pinch: true,
98
- round: false
99
- }, void 0);
96
+ const moveFn = map._move.bind(map, this._center, this._zoom, { pinch: true }, void 0);
100
97
  this._animRequest = requestAnimationFrame(moveFn.bind(this));
101
98
  e.preventDefault();
102
99
  }
@@ -112,7 +109,7 @@ var PinchZoomHandler = class PinchZoomHandler extends Handler {
112
109
  }
113
110
  this._zooming = false;
114
111
  cancelAnimationFrame(this._animRequest);
115
- if (this._map._zoomAnimated) this._map._animateZoom(this._center, this._map._limitZoom(this._zoom), true, this._map.options.zoomSnap);
112
+ if (this._map._zoomAnimated) this._map._animateZoom(this._center, this._map._limitZoom(this._zoom), this._map.options.zoomSnap);
116
113
  else this._map._resetView(this._center, this._map._limitZoom(this._zoom));
117
114
  }
118
115
  /**