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
@@ -12,10 +12,14 @@ import * as DomUtil from '../dom/DomUtil.js';
12
12
 
13
13
  /**
14
14
  * The content of a `DivOverlay` (`Popup`, `Tooltip`): a string rendered as HTML, an
15
- * `HTMLElement`, or a function receiving the source layer and returning one of them.
16
- * @typedef {string|HTMLElement|((layer: Layer) => string|HTMLElement)} DivOverlayContent
15
+ * `HTMLElement` or `DocumentFragment`, or a function receiving the source layer and
16
+ * returning one of them.
17
+ * @typedef {string|HTMLElement|DocumentFragment|
18
+ * ((layer: Layer) => string|HTMLElement|DocumentFragment)} DivOverlayContent
17
19
  */
18
20
 
21
+ const fragmentNodes = new WeakMap();
22
+
19
23
  /**
20
24
  * Options of `DivOverlay`.
21
25
  * @typedef {object} DivOverlayOptions
@@ -31,8 +35,8 @@ import * as DomUtil from '../dom/DomUtil.js';
31
35
  * in the DOM (for styling and events).
32
36
  * @property {DivOverlayContent} [content=''] Sets the HTML content of the overlay while
33
37
  * initializing. If a function is passed the source layer will be passed to the
34
- * function. The function should return a `String` or `HTMLElement` to be used in the
35
- * overlay. String content is rendered as HTML; sanitize untrusted input or pass an
38
+ * function. The function should return a `String`, `HTMLElement` or `DocumentFragment`
39
+ * to be used in the overlay. String content is rendered as HTML; sanitize untrusted input or pass an
36
40
  * `HTMLElement` with safe `textContent` instead.
37
41
  * @extends InteractiveLayerOptions
38
42
  */
@@ -75,7 +79,8 @@ const layerDivOverlayMethods = {
75
79
  /** @typedef {typeof layerDivOverlayMethods} LayerDivOverlayMethods */
76
80
 
77
81
  /**
78
- * Base model for Popup and Tooltip. Inherit from it for custom overlays like plugins.
82
+ * Base model for `Popup` and `Tooltip`; use those to show content on the map. Inherit
83
+ * from it for custom overlays like plugins.
79
84
  *
80
85
  * @fires contentupdate {MaplairEvent} Fired when the content of the overlay is updated.
81
86
  */
@@ -166,10 +171,10 @@ export class DivOverlay extends Layer {
166
171
  } else {
167
172
  layer = this._source;
168
173
  }
169
- this._prepareOpen();
170
-
171
- // open the overlay on the map
172
- this.openOn(layer._map);
174
+ if (this._prepareOpen()) {
175
+ // open the overlay on the map
176
+ this.openOn(layer._map);
177
+ }
173
178
  }
174
179
  return this;
175
180
  }
@@ -188,7 +193,7 @@ export class DivOverlay extends Layer {
188
193
  }
189
194
 
190
195
  clearTimeout(this._removeTimeout);
191
- this.getPane().appendChild(this._container);
196
+ this.getPane().append(this._container);
192
197
  if (this.options.overflow) {
193
198
  this._container.classList.add('maplair-overflow');
194
199
  this._container.popover = 'manual';
@@ -279,10 +284,11 @@ export class DivOverlay extends Layer {
279
284
 
280
285
  /**
281
286
  * Sets the HTML content of the overlay. A `String` argument is rendered as
282
- * HTML; if it may contain untrusted input, sanitize it (e.g. with DOMPurify)
283
- * or pass an `HTMLElement` with safe `textContent` instead. If a function is
284
- * passed the source layer will be passed to the function. The function should
285
- * return a `String` or `HTMLElement` to be used in the overlay.
287
+ * HTML; if it may contain untrusted input, sanitize it or pass an `HTMLElement`
288
+ * with safe `textContent` instead. A `DocumentFragment`
289
+ * can be passed as well. If a function is passed the source layer will be passed
290
+ * to the function. The function should return a `String`, `HTMLElement` or
291
+ * `DocumentFragment` to be used in the overlay.
286
292
  * @param {DivOverlayContent} content
287
293
  * @returns {this}
288
294
  */
@@ -341,7 +347,7 @@ export class DivOverlay extends Layer {
341
347
  * @returns {boolean}
342
348
  */
343
349
  isOpen() {
344
- return !!this._map && this._map.hasLayer(this);
350
+ return !!this._map?.hasLayer(this);
345
351
  }
346
352
 
347
353
  /**
@@ -401,17 +407,8 @@ export class DivOverlay extends Layer {
401
407
  this._source = source;
402
408
  }
403
409
 
404
- if (!latlng) {
405
- if (source.getCenter) {
406
- latlng = source.getCenter();
407
- } else if (source.getLatLng) {
408
- latlng = source.getLatLng();
409
- } else if (source.getBounds) {
410
- latlng = source.getBounds().getCenter();
411
- } else {
412
- throw new Error('Unable to get source layer LatLng.');
413
- }
414
- }
410
+ latlng ??= source.getCenter?.() ?? source.getLatLng?.() ?? source.getBounds?.().getCenter();
411
+ if (!latlng) { throw new Error('Unable to get source layer LatLng.'); }
415
412
  this.setLatLng(latlng);
416
413
 
417
414
  if (this._map) {
@@ -423,18 +420,19 @@ export class DivOverlay extends Layer {
423
420
  }
424
421
 
425
422
  _updateContent() {
426
- if (!this._content) { return; }
423
+ if (this._content == null) { return; }
427
424
 
428
425
  const node = this._contentNode;
429
426
  const content = (typeof this._content === 'function') ? this._content(this._source ?? this) : this._content;
430
427
 
431
428
  if (typeof content === 'string') {
432
429
  node.innerHTML = content;
430
+ } else if (content instanceof DocumentFragment) {
431
+ // inserting a fragment empties it, so keep its nodes to insert them again on later updates
432
+ if (content.hasChildNodes()) { fragmentNodes.set(content, [...content.childNodes]); }
433
+ node.replaceChildren(...fragmentNodes.get(content) ?? []);
433
434
  } else {
434
- while (node.hasChildNodes()) {
435
- node.removeChild(node.firstChild);
436
- }
437
- node.appendChild(content);
435
+ node.replaceChildren(content);
438
436
  }
439
437
 
440
438
  this.fire('contentupdate');
@@ -443,7 +441,7 @@ export class DivOverlay extends Layer {
443
441
  _updatePosition() {
444
442
  if (!this._map) { return; }
445
443
 
446
- const pos = this._map.latLngToLayerPoint(this._latlng)._round(),
444
+ const pos = this._map._snapLayerPoint(this._map.latLngToLayerPoint(this._latlng)),
447
445
  offset = Point.from(this.options.offset);
448
446
 
449
447
  this._setContainerPosition(pos.add(this._getAnchor()));
@@ -94,6 +94,7 @@ export class ImageOverlay extends Layer {
94
94
  onAdd() {
95
95
  if (!this._image) {
96
96
  this._initImage();
97
+ this._updateZIndex();
97
98
 
98
99
  if (this.options.opacity < 1) {
99
100
  this._updateOpacity();
@@ -106,7 +107,7 @@ export class ImageOverlay extends Layer {
106
107
  }
107
108
  this._image.classList.toggle('maplair-pixelated', !!this.options.pixelated);
108
109
 
109
- this.getPane().appendChild(this._image);
110
+ this.getPane().append(this._image);
110
111
  this._reset();
111
112
  }
112
113
 
@@ -151,7 +152,7 @@ export class ImageOverlay extends Layer {
151
152
  * @returns {this}
152
153
  */
153
154
  setStyle(styleOpts) {
154
- if (styleOpts.opacity) {
155
+ if (styleOpts.opacity != null) {
155
156
  this.setOpacity(styleOpts.opacity);
156
157
  }
157
158
  return this;
@@ -265,10 +266,6 @@ export class ImageOverlay extends Layer {
265
266
 
266
267
  img.decoding = this.options.decoding;
267
268
 
268
- if (this.options.zIndex) {
269
- this._updateZIndex();
270
- }
271
-
272
269
  if (wasElementSupplied) {
273
270
  this._url = img.src;
274
271
  return;
@@ -287,9 +284,10 @@ export class ImageOverlay extends Layer {
287
284
 
288
285
  _reset() {
289
286
  const image = this._image,
287
+ map = this._map,
290
288
  bounds = new Bounds(
291
- this._map.latLngToLayerPoint(this._bounds.getNorthWest())._round(),
292
- this._map.latLngToLayerPoint(this._bounds.getSouthEast())._round()),
289
+ map._snapLayerPoint(map.latLngToLayerPoint(this._bounds.getNorthWest())),
290
+ map._snapLayerPoint(map.latLngToLayerPoint(this._bounds.getSouthEast()))),
293
291
  size = bounds.getSize();
294
292
 
295
293
  DomUtil.setPosition(image, bounds.min);
@@ -303,7 +301,7 @@ export class ImageOverlay extends Layer {
303
301
  }
304
302
 
305
303
  _updateZIndex() {
306
- if (this._image && this.options.zIndex !== undefined && this.options.zIndex !== null) {
304
+ if (this._image && this.options.zIndex != null) {
307
305
  this._image.style.zIndex = `${this.options.zIndex}`;
308
306
  }
309
307
  }
@@ -325,4 +323,9 @@ export class ImageOverlay extends Layer {
325
323
  getCenter() {
326
324
  return this._bounds.getCenter();
327
325
  }
326
+
327
+ // like on paths, popups open where the overlay was clicked, so another click moves them
328
+ _togglesPopupOnClick() {
329
+ return false;
330
+ }
328
331
  }
@@ -170,8 +170,12 @@ const mapLayerMethods = {
170
170
 
171
171
  for (const l of Object.values(this._zoomBoundLayers)) {
172
172
  const options = l.options;
173
- minZoom = Math.min(minZoom, options.minZoom ?? Infinity);
174
- maxZoom = Math.max(maxZoom, options.maxZoom ?? -Infinity);
173
+ if (options.minZoom != null && !Number.isNaN(options.minZoom)) {
174
+ minZoom = Math.min(minZoom, options.minZoom);
175
+ }
176
+ if (options.maxZoom != null && !Number.isNaN(options.maxZoom)) {
177
+ maxZoom = Math.max(maxZoom, options.maxZoom);
178
+ }
175
179
  }
176
180
 
177
181
  this._layersMaxZoom = maxZoom === -Infinity ? undefined : maxZoom;
@@ -181,7 +185,7 @@ const mapLayerMethods = {
181
185
  this.fire('zoomlevelschange');
182
186
  }
183
187
 
184
- if (this.options.maxZoom === undefined && this._layersMaxZoom && this.getZoom() > this._layersMaxZoom) {
188
+ if (this.options.maxZoom === undefined && this._layersMaxZoom !== undefined && this.getZoom() > this._layersMaxZoom) {
185
189
  this.setZoom(this._layersMaxZoom);
186
190
  }
187
191
  if (this.options.minZoom === undefined && this._layersMinZoom && this.getZoom() < this._layersMinZoom) {
@@ -315,8 +319,10 @@ export class Layer extends Evented {
315
319
  _updateInteractive() {}
316
320
 
317
321
  /**
318
- * Makes `targetEl` a target of pointer events of this layer.
319
- * @internal
322
+ * Registers `targetEl`, one of the layer's DOM elements, so that pointer, click and
323
+ * keyboard events on it (or its descendants) fire on the layer and bubble through its
324
+ * parent groups to the map, the way they do for markers and paths. Custom layers call
325
+ * it in [`onAdd`](#layer-onadd), once the layer is on a map.
320
326
  * @param {HTMLElement} targetEl
321
327
  * @returns {this}
322
328
  */
@@ -326,8 +332,8 @@ export class Layer extends Evented {
326
332
  }
327
333
 
328
334
  /**
329
- * Stops `targetEl` from being a target of pointer events of this layer.
330
- * @internal
335
+ * Undoes [`addInteractiveTarget`](#layer-addinteractivetarget), typically in
336
+ * [`onRemove`](#layer-onremove).
331
337
  * @param {HTMLElement} targetEl
332
338
  * @returns {this}
333
339
  */
@@ -336,6 +342,12 @@ export class Layer extends Evented {
336
342
  return this;
337
343
  }
338
344
 
345
+ // Whether clicking the layer again closes its popup, rather than moving it to the
346
+ // clicked point like on paths, where the popup opens where they were clicked.
347
+ _togglesPopupOnClick() {
348
+ return true;
349
+ }
350
+
339
351
  /**
340
352
  * Used by the `attribution control`, returns the [attribution option](#layeroptions-attribution).
341
353
  * @returns {string|null}
@@ -1,9 +1,9 @@
1
1
  import {DivOverlay} from './DivOverlay.js';
2
2
  import * as DomEvent from '../dom/DomEvent.js';
3
3
  import * as DomUtil from '../dom/DomUtil.js';
4
+ import * as Util from '../core/Util.js';
4
5
  import {Point} from '../geometry/Point.js';
5
6
  import {Layer} from './Layer.js';
6
- import {Path} from './vector/Path.js';
7
7
  import {FeatureGroup} from './FeatureGroup.js';
8
8
  import {I18n} from '../core/I18n.js';
9
9
 
@@ -124,10 +124,10 @@ const layerPopupMethods = {
124
124
  /**
125
125
  * Binds a popup to the layer with the passed `content` and sets up the
126
126
  * necessary event listeners. A `String` argument is rendered as HTML; if it
127
- * may contain untrusted input, sanitize it (e.g. with DOMPurify) or pass an
128
- * `HTMLElement` with safe `textContent` instead. If a `Function` is passed
127
+ * may contain untrusted input, sanitize it or pass an `HTMLElement` with safe
128
+ * `textContent` instead. If a `Function` is passed
129
129
  * it will receive the layer as the first argument and should return a
130
- * `String` or `HTMLElement`.
130
+ * `String`, `HTMLElement` or `DocumentFragment`.
131
131
  * @group Popup methods
132
132
  * @param {DivOverlayContent|Popup} content
133
133
  * @param {PopupOptions} [options]
@@ -136,6 +136,7 @@ const layerPopupMethods = {
136
136
  bindPopup(content, options) {
137
137
  // eslint-disable-next-line no-use-before-define -- defined below, used when called
138
138
  this._popup = this._initOverlay(Popup, this._popup, content, options);
139
+ this._popup._updateSourceAria(this._popup.isOpen());
139
140
  this.on({
140
141
  click: this._openPopup,
141
142
  remove: this.closePopup,
@@ -157,6 +158,9 @@ const layerPopupMethods = {
157
158
  remove: this.closePopup,
158
159
  move: this._movePopup
159
160
  });
161
+ const el = this.getElement?.();
162
+ el?.removeAttribute('aria-expanded');
163
+ el?.removeAttribute('aria-controls');
160
164
  this._popup = null;
161
165
  }
162
166
  return this;
@@ -241,14 +245,9 @@ const layerPopupMethods = {
241
245
  DomEvent.stopPropagation(e);
242
246
 
243
247
  const target = e.propagatedFrom ?? e.target;
244
- if (this._popup._source === target && !(target instanceof Path)) {
245
- // treat it like a marker and figure out
246
- // if we should toggle it open/closed
247
- if (this._map.hasLayer(this._popup)) {
248
- this.closePopup();
249
- } else {
250
- this.openPopup(e.latlng);
251
- }
248
+ // clicking a marker-like source again toggles its popup closed
249
+ if (this._popup._source === target && target._togglesPopupOnClick() && this._map.hasLayer(this._popup)) {
250
+ this.closePopup();
252
251
  return;
253
252
  }
254
253
  this._popup._source = target;
@@ -290,8 +289,7 @@ const layerPopupMethods = {
290
289
  *
291
290
  * **Security note.** Popup string content is rendered as HTML. If your content
292
291
  * may include untrusted input (e.g. data from users or external APIs), sanitize
293
- * it with a library like [DOMPurify](https://github.com/cure53/DOMPurify), or
294
- * build a DOM element using `textContent` to render it as plain text:
292
+ * it first, or build a DOM element using `textContent` to render it as plain text:
295
293
  *
296
294
  * ```js
297
295
  * const el = document.createElement('div');
@@ -351,12 +349,14 @@ export class Popup extends DivOverlay {
351
349
  openOn(map) {
352
350
  map = arguments.length ? map : this._source._map; // experimental, not the part of public api
353
351
 
354
- if (!map.hasLayer(this) && map._popup && map._popup.options.autoClose) {
352
+ if (!map.hasLayer(this) && map._popup?.options.autoClose) {
355
353
  map.removeLayer(map._popup);
356
354
  }
357
355
  map._popup = this;
358
356
 
359
- return super.openOn(map);
357
+ super.openOn(map);
358
+ this._updateSourceAria(true); // the source may have changed while open, e.g. within a FeatureGroup
359
+ return this;
360
360
  }
361
361
 
362
362
  /**
@@ -379,12 +379,13 @@ export class Popup extends DivOverlay {
379
379
  }
380
380
 
381
381
  map.fire('popupopen', {popup: this});
382
+ this._updateSourceAria(true);
382
383
 
383
384
  if (this._source) {
384
385
  this._source.fire('popupopen', {popup: this}, true);
385
386
  // For non-path layers, we toggle the popup when clicking
386
387
  // again the layer, so prevent the map to reopen it.
387
- if (!(this._source instanceof Path)) {
388
+ if (this._source._togglesPopupOnClick()) {
388
389
  this._source.on('preclick', DomEvent.stopPropagation);
389
390
  }
390
391
  }
@@ -395,18 +396,23 @@ export class Popup extends DivOverlay {
395
396
  * @param {MaplairMap} map
396
397
  */
397
398
  onRemove(map) {
398
- if (this._container.contains(document.activeElement) && this._returnFocus?.isConnected) {
399
- this._returnFocus.focus({preventScroll: true});
399
+ // closing removes the focused element, which would send focus back to the start of the page
400
+ if (this._container.contains(document.activeElement)) {
401
+ const el = this._source?.getElement?.();
402
+ const target = this._returnFocus?.isConnected ? this._returnFocus :
403
+ el?.tabIndex >= 0 ? el : map._container;
404
+ target.focus({preventScroll: true});
400
405
  }
401
406
  this._returnFocus = null;
402
407
 
403
408
  super.onRemove(map);
404
409
 
405
410
  map.fire('popupclose', {popup: this});
411
+ this._updateSourceAria(false);
406
412
 
407
413
  if (this._source) {
408
414
  this._source.fire('popupclose', {popup: this}, true);
409
- if (!(this._source instanceof Path)) {
415
+ if (this._source._togglesPopupOnClick()) {
410
416
  this._source.off('preclick', DomEvent.stopPropagation);
411
417
  }
412
418
  }
@@ -430,10 +436,24 @@ export class Popup extends DivOverlay {
430
436
  return events;
431
437
  }
432
438
 
439
+ // Marks a source that acts as a button (e.g. a keyboard-accessible marker) as a
440
+ // disclosure for the popup.
441
+ _updateSourceAria(open) {
442
+ this._ariaSource?.setAttribute('aria-expanded', 'false');
443
+ this._ariaSource?.removeAttribute('aria-controls');
444
+
445
+ const el = this._source?.getElement?.();
446
+ this._ariaSource = el?.getAttribute('role') === 'button' ? el : null;
447
+ this._ariaSource?.setAttribute('aria-expanded', String(open));
448
+ if (open) { this._ariaSource?.setAttribute('aria-controls', this._container.id); }
449
+ }
450
+
433
451
  _initLayout() {
434
452
  const prefix = 'maplair-popup',
435
453
  container = this._container = DomUtil.create('div', `${prefix} ${this.options.className || ''} maplair-zoom-animated`);
436
454
 
455
+ container.id = `maplair-popup-${Util.stamp(this)}`;
456
+
437
457
  const wrapper = this._wrapper = DomUtil.create('div', `${prefix}-content-wrapper`, container);
438
458
  this._contentNode = DomUtil.create('div', `${prefix}-content`, wrapper);
439
459
 
@@ -449,7 +469,7 @@ export class Popup extends DivOverlay {
449
469
  closeButton.type = 'button';
450
470
  closeButton.setAttribute('aria-label', I18n.translate('Close popup'));
451
471
 
452
- closeButton.innerHTML = '<span aria-hidden="true">&#215;</span>';
472
+ closeButton.textContent = '\u00D7';
453
473
 
454
474
  DomEvent.on(closeButton, 'click', (ev) => {
455
475
  ev.preventDefault();
@@ -6,7 +6,8 @@ import * as DomEvent from '../dom/DomEvent.js';
6
6
  import * as Util from '../core/Util.js';
7
7
  import {FeatureGroup} from './FeatureGroup.js';
8
8
 
9
- const directions = ['top', 'bottom', 'center', 'right', 'left'];
9
+ // which point of the tooltip (as a fraction of its width and height) sits at the anchor, for each direction
10
+ const directions = {top: [0.5, 1], bottom: [0.5, 0], center: [0.5, 0.5], right: [0, 0.5], left: [1, 0.5]};
10
11
 
11
12
  // The size of the tooltip tip (see `.maplair-tooltip-top` etc. in maplair.css) and
12
13
  // the minimum distance of an `auto` tooltip to the edge of the map.
@@ -34,7 +35,8 @@ function clampTip(tipShift, size) {
34
35
  * Options of `Tooltip`.
35
36
  * @typedef {object} TooltipOptions
36
37
  * @property {string} [pane='tooltipPane'] `Map pane` where the tooltip will be added.
37
- * @property {PointExpression} [offset=[0, 0]] Optional offset of the tooltip position.
38
+ * @property {PointExpression} [offset=[0, 0]] Optional offset of the tooltip position, in
39
+ * pixels. Mirrored horizontally when an `auto` tooltip opens to the left.
38
40
  * @property {'right'|'left'|'top'|'bottom'|'center'|'auto'} [direction='auto']
39
41
  * Direction where to open the tooltip. Possible values are: `right`, `left`, `top`,
40
42
  * `bottom`, `center`, `auto`. `auto` chooses the direction with enough space when
@@ -111,10 +113,10 @@ const layerTooltipMethods = {
111
113
  /**
112
114
  * Binds a tooltip to the layer with the passed `content` and sets up the
113
115
  * necessary event listeners. A `String` argument is rendered as HTML; if it
114
- * may contain untrusted input, sanitize it (e.g. with DOMPurify) or pass an
115
- * `HTMLElement` with safe `textContent` instead. If a `Function` is passed
116
+ * may contain untrusted input, sanitize it or pass an `HTMLElement` with safe
117
+ * `textContent` instead. If a `Function` is passed
116
118
  * it will receive the layer as the first argument and should return a
117
- * `String` or `HTMLElement`.
119
+ * `String`, `HTMLElement` or `DocumentFragment`.
118
120
  * @group Tooltip methods
119
121
  * @param {DivOverlayContent|Tooltip} content
120
122
  * @param {TooltipOptions} [options]
@@ -133,7 +135,7 @@ const layerTooltipMethods = {
133
135
  this._tooltip = this._initOverlay(Tooltip, old, content, options);
134
136
  this._initTooltipInteractions();
135
137
 
136
- if (this._tooltip.options.permanent && this._map && this._map.hasLayer(this)) {
138
+ if (this._tooltip.options.permanent && this._map?.hasLayer(this)) {
137
139
  this.openTooltip();
138
140
  }
139
141
 
@@ -193,12 +195,6 @@ const layerTooltipMethods = {
193
195
  if (this._tooltip._prepareOpen(latlng)) {
194
196
  // open the tooltip on the map
195
197
  this._tooltip.openOn(this._map);
196
-
197
- if (this.getElement) {
198
- this._setAriaDescribedByOnLayer(this);
199
- } else if (this.eachLayer) {
200
- this.eachLayer(this._setAriaDescribedByOnLayer, this);
201
- }
202
198
  }
203
199
  }
204
200
  return this;
@@ -272,8 +268,10 @@ const layerTooltipMethods = {
272
268
  // Remove focus listener, if already existing
273
269
  el._maplair_focus_handler && DomEvent.off(el, 'focus', el._maplair_focus_handler, this);
274
270
 
271
+ // open only on keyboard focus: an element clicked with the mouse keeps focus and gets it again whenever
272
+ // the window is refocused or a popup closes, which would open the tooltip without the pointer over it
275
273
  el._maplair_focus_handler = () => {
276
- if (this._tooltip) {
274
+ if (this._tooltip && el.matches(':focus-visible')) {
277
275
  this._tooltip._source = layer;
278
276
  this.openTooltip();
279
277
  }
@@ -289,11 +287,6 @@ const layerTooltipMethods = {
289
287
  }
290
288
  },
291
289
 
292
- _setAriaDescribedByOnLayer(layer) {
293
- const el = typeof layer.getElement === 'function' && layer.getElement();
294
- el?.setAttribute?.('aria-describedby', this._tooltip._container.id);
295
- },
296
-
297
290
  _openTooltip(e) {
298
291
  if (!this._tooltip || !this._map) {
299
292
  return;
@@ -317,13 +310,8 @@ const layerTooltipMethods = {
317
310
  },
318
311
 
319
312
  _moveTooltip(e) {
320
- let latlng = e.latlng, containerPoint, layerPoint;
321
- if (this._tooltip.options.sticky && e.originalEvent) {
322
- containerPoint = this._map.pointerEventToContainerPoint(e.originalEvent);
323
- layerPoint = this._map.containerPointToLayerPoint(containerPoint);
324
- latlng = this._map.layerPointToLatLng(layerPoint);
325
- }
326
- this._tooltip.setLatLng(latlng);
313
+ this._tooltip.setLatLng(this._tooltip.options.sticky && e.originalEvent ?
314
+ this._map.pointerEventToLatLng(e.originalEvent) : e.latlng);
327
315
  }
328
316
  };
329
317
  /** @typedef {typeof layerTooltipMethods} LayerTooltipMethods */
@@ -355,8 +343,7 @@ const layerTooltipMethods = {
355
343
  *
356
344
  * **Security note.** Tooltip string content is rendered as HTML. If your content
357
345
  * may include untrusted input (e.g. data from users or external APIs), sanitize
358
- * it with a library like [DOMPurify](https://github.com/cure53/DOMPurify), or
359
- * build a DOM element using `textContent` to render it as plain text:
346
+ * it first, or build a DOM element using `textContent` to render it as plain text:
360
347
  *
361
348
  * ```js
362
349
  * const el = document.createElement('div');
@@ -418,6 +405,8 @@ export class Tooltip extends DivOverlay {
418
405
 
419
406
  if (this._source) {
420
407
  this.addEventParent(this._source);
408
+ this._ariaSource = this._source.getElement?.();
409
+ this._ariaSource?.setAttribute('aria-describedby', this._container.id);
421
410
 
422
411
  this._source.fire('tooltipopen', {tooltip: this}, true);
423
412
  }
@@ -435,6 +424,11 @@ export class Tooltip extends DivOverlay {
435
424
  if (this._source) {
436
425
  this.removeEventParent(this._source);
437
426
 
427
+ // drop the reference to the removed tooltip unless something else replaced it since
428
+ const el = this._ariaSource;
429
+ if (el?.getAttribute('aria-describedby') === this._container.id) { el.removeAttribute('aria-describedby'); }
430
+ this._ariaSource = null;
431
+
438
432
  this._source.fire('tooltipclose', {tooltip: this}, true);
439
433
  }
440
434
  }
@@ -460,7 +454,7 @@ export class Tooltip extends DivOverlay {
460
454
  this._contentNode = this._container = DomUtil.create('div', className);
461
455
 
462
456
  this._container.setAttribute('role', 'tooltip');
463
- this._container.setAttribute('id', `maplair-tooltip-${Util.stamp(this)}`);
457
+ this._container.id = `maplair-tooltip-${Util.stamp(this)}`;
464
458
  }
465
459
 
466
460
  _updateLayout() {}
@@ -484,39 +478,21 @@ export class Tooltip extends DivOverlay {
484
478
  const container = this._container,
485
479
  tooltipWidth = container.offsetWidth,
486
480
  tooltipHeight = container.offsetHeight,
487
- offset = new Point(this.options.offset),
488
- anchor = this._getAnchor();
489
-
490
- if (!directions.includes(direction)) {
491
- ({direction, shift} = this._getAutoPlacement(pos, offset, anchor, tooltipWidth, tooltipHeight));
492
- if (direction === 'left') {
493
- // mirror the offset and anchor, which are meant for the right side
494
- offset.x = -offset.x;
495
- anchor.x = -anchor.x;
496
- }
497
- }
481
+ offset = new Point(this.options.offset);
498
482
 
499
- let subX = tooltipWidth / 2,
500
- subY = tooltipHeight / 2;
501
- if (direction === 'top') {
502
- subY = tooltipHeight;
503
- } else if (direction === 'bottom') {
504
- subY = 0;
505
- } else if (direction === 'right') {
506
- subX = 0;
507
- } else if (direction === 'left') {
508
- subX = tooltipWidth;
483
+ if (!(direction in directions)) {
484
+ ({direction, shift} = this._getAutoPlacement(pos, offset, tooltipWidth, tooltipHeight));
485
+ // mirror the offset, which is meant for the right side
486
+ if (direction === 'left') { offset.x = -offset.x; }
509
487
  }
488
+ const anchor = this._getAnchor(direction);
510
489
 
511
- pos = pos.subtract(new Point(subX, subY, true)).add(offset).add(anchor).add(shift);
490
+ const [fx, fy] = directions[direction];
491
+ pos = pos.subtract(new Point(tooltipWidth * fx, tooltipHeight * fy, true)).add(offset).add(anchor).add(shift);
512
492
 
513
- container.classList.remove(
514
- 'maplair-tooltip-right',
515
- 'maplair-tooltip-left',
516
- 'maplair-tooltip-top',
517
- 'maplair-tooltip-bottom'
518
- );
519
- container.classList.add(`maplair-tooltip-${direction}`);
493
+ for (const d in directions) {
494
+ container.classList.toggle(`maplair-tooltip-${d}`, d === direction);
495
+ }
520
496
 
521
497
  // Let the tip point at the source despite the shift
522
498
  const tipShift = direction === 'top' || direction === 'bottom' ?
@@ -534,17 +510,20 @@ export class Tooltip extends DivOverlay {
534
510
  // Chooses the direction of an `auto` tooltip by the available space:
535
511
  // preferably the side towards the center, otherwise the opposite side,
536
512
  // above or below. The tooltip is shifted along that side to stay in view.
537
- _getAutoPlacement(pos, offset, anchor, width, height) {
513
+ _getAutoPlacement(pos, offset, width, height) {
538
514
  const area = this._getAutoArea(),
539
515
  point = this._map.layerPointToContainerPoint(pos),
540
- x = point.x + offset.x + anchor.x,
541
- y = point.y + offset.y + anchor.y,
542
- // the space for each direction, see `direction` for the mirrored left side
543
- space = {
544
- right: area.max.x - x - width - tipSize,
545
- left: point.x - offset.x - anchor.x - area.min.x - width - tipSize,
546
- top: y - area.min.y - height - tipSize,
547
- bottom: area.max.y - y - height - tipSize
516
+ // where the tooltip attaches for each direction, with the offset mirrored on the left side
517
+ at = {};
518
+ for (const d of ['right', 'left', 'top', 'bottom']) {
519
+ at[d] = point.add(this._getAnchor(d)).add(d === 'left' ? [-offset.x, offset.y] : offset);
520
+ }
521
+ // the space for each direction
522
+ const space = {
523
+ right: area.max.x - at.right.x - width - tipSize,
524
+ left: at.left.x - area.min.x - width - tipSize,
525
+ top: at.top.y - area.min.y - height - tipSize,
526
+ bottom: area.max.y - at.bottom.y - height - tipSize
548
527
  };
549
528
 
550
529
  if (!this._autoDirection) {
@@ -556,6 +535,7 @@ export class Tooltip extends DivOverlay {
556
535
  }
557
536
 
558
537
  const direction = this._autoDirection,
538
+ {x, y} = at[direction],
559
539
  shift = direction === 'top' || direction === 'bottom' ?
560
540
  new Point(shiftInto(x - width / 2, width, area.min.x, area.max.x), 0) :
561
541
  new Point(0, shiftInto(y - height / 2, height, area.min.y, area.max.y));
@@ -582,7 +562,7 @@ export class Tooltip extends DivOverlay {
582
562
  }
583
563
 
584
564
  _updatePosition() {
585
- const pos = this._map.latLngToLayerPoint(this._latlng)._round();
565
+ const pos = this._map._snapLayerPoint(this._map.latLngToLayerPoint(this._latlng));
586
566
  this._setPosition(pos);
587
567
  }
588
568
 
@@ -604,9 +584,9 @@ export class Tooltip extends DivOverlay {
604
584
  this._setPosition(pos);
605
585
  }
606
586
 
607
- _getAnchor() {
608
- // Where should we anchor the tooltip on the source layer?
609
- return new Point(this._source?._getTooltipAnchor && !this.options.sticky ? this._source._getTooltipAnchor() : [0, 0]);
587
+ // Where the tooltip opening in the given direction attaches on the source layer
588
+ _getAnchor(direction) {
589
+ return new Point(this._source?._getTooltipAnchor && !this.options.sticky ? this._source._getTooltipAnchor(direction) : [0, 0]);
610
590
  }
611
591
 
612
592
  /**