maplair 4.2.0 → 4.4.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 (179) hide show
  1. package/CHANGELOG.md +106 -8
  2. package/dist/esm/control/AttributionControl.js +10 -10
  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 +13 -9
  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 +4 -5
  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 +13 -13
  21. package/dist/esm/control/ZoomControl.js.map +1 -1
  22. package/dist/esm/core/Events.js +4 -2
  23. package/dist/esm/core/Events.js.map +1 -1
  24. package/dist/esm/core/Util.js +16 -4
  25. package/dist/esm/core/Util.js.map +1 -1
  26. package/dist/esm/dom/DomEvent.PointerEvents.js +55 -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 +4 -6
  33. package/dist/esm/dom/Draggable.js.map +1 -1
  34. package/dist/esm/geo/LatLng.js +23 -34
  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 +12 -17
  41. package/dist/esm/geometry/Point.js.map +1 -1
  42. package/dist/esm/geometry/Transformation.js +1 -7
  43. package/dist/esm/geometry/Transformation.js.map +1 -1
  44. package/dist/esm/layer/BlanketOverlay.js +1 -1
  45. package/dist/esm/layer/BlanketOverlay.js.map +1 -1
  46. package/dist/esm/layer/DivOverlay.js +23 -23
  47. package/dist/esm/layer/DivOverlay.js.map +1 -1
  48. package/dist/esm/layer/ImageOverlay.js +7 -4
  49. package/dist/esm/layer/ImageOverlay.js.map +1 -1
  50. package/dist/esm/layer/Layer.js +11 -6
  51. package/dist/esm/layer/Layer.js.map +1 -1
  52. package/dist/esm/layer/Popup.js +30 -11
  53. package/dist/esm/layer/Popup.js.map +1 -1
  54. package/dist/esm/layer/Tooltip.js +18 -35
  55. package/dist/esm/layer/Tooltip.js.map +1 -1
  56. package/dist/esm/layer/VideoOverlay.js +2 -2
  57. package/dist/esm/layer/VideoOverlay.js.map +1 -1
  58. package/dist/esm/layer/marker/DivIcon.js +4 -8
  59. package/dist/esm/layer/marker/DivIcon.js.map +1 -1
  60. package/dist/esm/layer/marker/Icon.js +16 -15
  61. package/dist/esm/layer/marker/Icon.js.map +1 -1
  62. package/dist/esm/layer/marker/Marker.Drag.js +6 -7
  63. package/dist/esm/layer/marker/Marker.Drag.js.map +1 -1
  64. package/dist/esm/layer/marker/Marker.js +49 -48
  65. package/dist/esm/layer/marker/Marker.js.map +1 -1
  66. package/dist/esm/layer/marker/SvgIcon.js +1 -1
  67. package/dist/esm/layer/marker/SvgIcon.js.map +1 -1
  68. package/dist/esm/layer/tile/GridLayer.js +18 -16
  69. package/dist/esm/layer/tile/GridLayer.js.map +1 -1
  70. package/dist/esm/layer/tile/TileLayer.js +9 -9
  71. package/dist/esm/layer/tile/TileLayer.js.map +1 -1
  72. package/dist/esm/layer/vector/Canvas.js +44 -93
  73. package/dist/esm/layer/vector/Canvas.js.map +1 -1
  74. package/dist/esm/layer/vector/Circle.js +8 -7
  75. package/dist/esm/layer/vector/Circle.js.map +1 -1
  76. package/dist/esm/layer/vector/CircleMarker.js +9 -1
  77. package/dist/esm/layer/vector/CircleMarker.js.map +1 -1
  78. package/dist/esm/layer/vector/Path.js +10 -5
  79. package/dist/esm/layer/vector/Path.js.map +1 -1
  80. package/dist/esm/layer/vector/Polygon.js +10 -11
  81. package/dist/esm/layer/vector/Polygon.js.map +1 -1
  82. package/dist/esm/layer/vector/Polyline.js +19 -26
  83. package/dist/esm/layer/vector/Polyline.js.map +1 -1
  84. package/dist/esm/layer/vector/Renderer.getRenderer.js +2 -1
  85. package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -1
  86. package/dist/esm/layer/vector/Renderer.js +16 -0
  87. package/dist/esm/layer/vector/Renderer.js.map +1 -1
  88. package/dist/esm/layer/vector/SVG.js +3 -3
  89. package/dist/esm/layer/vector/SVG.js.map +1 -1
  90. package/dist/esm/map/Map.Rotation.js +6 -9
  91. package/dist/esm/map/Map.Rotation.js.map +1 -1
  92. package/dist/esm/map/Map.js +104 -49
  93. package/dist/esm/map/Map.js.map +1 -1
  94. package/dist/esm/map/MapSync.js +1 -4
  95. package/dist/esm/map/MapSync.js.map +1 -1
  96. package/dist/esm/map/handler/BoxZoomHandler.js +2 -0
  97. package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -1
  98. package/dist/esm/map/handler/DisplaceHandler.js +68 -30
  99. package/dist/esm/map/handler/DisplaceHandler.js.map +1 -1
  100. package/dist/esm/map/handler/KeyboardHandler.js +3 -5
  101. package/dist/esm/map/handler/KeyboardHandler.js.map +1 -1
  102. package/dist/esm/map/handler/PinchZoomHandler.js +3 -6
  103. package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -1
  104. package/dist/esm/map/handler/ScrollWheelZoomHandler.js +3 -6
  105. package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -1
  106. package/dist/esm/map/handler/TapDragZoomHandler.js +1 -4
  107. package/dist/esm/map/handler/TapDragZoomHandler.js.map +1 -1
  108. package/dist/esm/map/handler/TapHoldHandler.js +8 -20
  109. package/dist/esm/map/handler/TapHoldHandler.js.map +1 -1
  110. package/dist/esm/package.js +1 -1
  111. package/dist/maplair-global-src.js +700 -687
  112. package/dist/maplair-global-src.js.map +1 -1
  113. package/dist/maplair-global.js +2 -2
  114. package/dist/maplair-global.js.map +1 -1
  115. package/dist/maplair-src.js +700 -687
  116. package/dist/maplair-src.js.map +1 -1
  117. package/dist/maplair.css +1 -1
  118. package/dist/maplair.js +2 -2
  119. package/dist/maplair.js.map +1 -1
  120. package/i18n/en.json +1 -0
  121. package/package.json +2 -1
  122. package/src/control/AttributionControl.js +9 -9
  123. package/src/control/ButtonControl.js +0 -1
  124. package/src/control/ContextMenu.js +1 -1
  125. package/src/control/Control.js +13 -16
  126. package/src/control/FullscreenControl.js +0 -1
  127. package/src/control/LayersControl.js +14 -10
  128. package/src/control/RotateControl.js +0 -1
  129. package/src/control/ScaleControl.js +4 -5
  130. package/src/control/SideBySideControl.js +4 -2
  131. package/src/control/ZoomControl.js +10 -13
  132. package/src/core/Events.js +8 -2
  133. package/src/core/Util.js +16 -3
  134. package/src/dom/DomEvent.PointerEvents.js +47 -1
  135. package/src/dom/DomEvent.js +5 -17
  136. package/src/dom/DomUtil.js +5 -5
  137. package/src/dom/Draggable.js +5 -6
  138. package/src/geo/LatLng.js +22 -36
  139. package/src/geo/LatLngBounds.js +10 -4
  140. package/src/geometry/Bounds.js +3 -3
  141. package/src/geometry/Point.js +18 -26
  142. package/src/geometry/Transformation.js +1 -8
  143. package/src/layer/BlanketOverlay.js +1 -1
  144. package/src/layer/DivOverlay.js +28 -30
  145. package/src/layer/ImageOverlay.js +9 -7
  146. package/src/layer/Layer.js +18 -6
  147. package/src/layer/Popup.js +38 -17
  148. package/src/layer/Tooltip.js +17 -44
  149. package/src/layer/VideoOverlay.js +2 -2
  150. package/src/layer/marker/DivIcon.js +3 -6
  151. package/src/layer/marker/Icon.js +19 -18
  152. package/src/layer/marker/Marker.Drag.js +16 -18
  153. package/src/layer/marker/Marker.js +66 -59
  154. package/src/layer/marker/SvgIcon.js +1 -1
  155. package/src/layer/tile/GridLayer.js +21 -23
  156. package/src/layer/tile/TileLayer.js +17 -14
  157. package/src/layer/vector/Canvas.js +52 -148
  158. package/src/layer/vector/Circle.js +14 -9
  159. package/src/layer/vector/CircleMarker.js +11 -1
  160. package/src/layer/vector/Path.js +11 -5
  161. package/src/layer/vector/Polygon.js +11 -10
  162. package/src/layer/vector/Polyline.js +20 -21
  163. package/src/layer/vector/Renderer.getRenderer.js +2 -1
  164. package/src/layer/vector/Renderer.js +21 -3
  165. package/src/layer/vector/SVG.js +3 -3
  166. package/src/map/Map.Rotation.js +7 -6
  167. package/src/map/Map.js +135 -54
  168. package/src/map/MapSync.js +1 -1
  169. package/src/map/handler/BoxZoomHandler.js +2 -0
  170. package/src/map/handler/DisplaceHandler.js +53 -7
  171. package/src/map/handler/KeyboardHandler.js +5 -10
  172. package/src/map/handler/PinchZoomHandler.js +3 -3
  173. package/src/map/handler/ScrollWheelZoomHandler.js +3 -3
  174. package/src/map/handler/TapDragZoomHandler.js +1 -1
  175. package/src/map/handler/TapHoldHandler.js +13 -25
  176. package/src/maplair.css +38 -9
  177. package/dist/esm/dom/DomEvent.DoubleTap.js +0 -90
  178. package/dist/esm/dom/DomEvent.DoubleTap.js.map +0 -1
  179. package/src/dom/DomEvent.DoubleTap.js +0 -143
package/src/geo/LatLng.js CHANGED
@@ -56,53 +56,45 @@ export class LatLng {
56
56
  * @param {number} [alt] Altitude in meters.
57
57
  */
58
58
  constructor(lat, lng, alt) {
59
- const valid = LatLng.validate(lat, lng, alt);
60
- if (!valid) {
61
- throw new Error(`Invalid LatLng object: (${lat}, ${lng})`);
62
- }
63
-
64
- let _lat, _lng, _alt;
59
+ let _lat = lat, _lng = lng, _alt = alt;
65
60
  if (lat instanceof LatLng) {
66
- _lat = lat.lat;
67
- _lng = lat.lng;
68
- _alt = lat.alt;
69
- } else if (Array.isArray(lat) && typeof lat[0] !== 'object') {
70
- if (lat.length === 3) {
71
- _lat = lat[0];
72
- _lng = lat[1];
73
- _alt = lat[2];
74
- } else if (lat.length === 2) {
75
- _lat = lat[0];
76
- _lng = lat[1];
77
- }
78
- } else if (typeof lat === 'object' && 'lat' in lat) {
61
+ ({lat: _lat, lng: _lng, alt: _alt} = lat);
62
+ } else if (Array.isArray(lat)) {
63
+ _lat = lat.length <= 3 ? lat[0] : NaN;
64
+ _lng = lat[1];
65
+ _alt = lat[2];
66
+ } else if (typeof lat === 'object' && lat !== null) {
79
67
  _lat = lat.lat;
80
68
  _lng = 'lng' in lat ? lat.lng : lat.lon;
81
69
  _alt = lat.alt;
82
- } else {
83
- _lat = lat;
84
- _lng = lng;
85
- _alt = alt;
86
70
  }
87
71
 
88
72
  /**
89
73
  * Latitude in degrees.
90
74
  * @type {number}
91
75
  */
92
- this.lat = +_lat;
76
+ this.lat = Util.toNumber(_lat);
93
77
 
94
78
  /**
95
79
  * Longitude in degrees.
96
80
  * @type {number}
97
81
  */
98
- this.lng = +_lng;
82
+ this.lng = Util.toNumber(_lng);
99
83
 
100
- if (_alt !== undefined) {
84
+ if (isNaN(this.lat) || isNaN(this.lng)) {
85
+ throw new Error(`Invalid LatLng object: (${_lat}, ${_lng})`);
86
+ }
87
+
88
+ if (_alt != null) {
101
89
  /**
102
90
  * Altitude in meters (optional).
103
91
  * @type {number|undefined}
104
92
  */
105
- this.alt = +_alt;
93
+ this.alt = Util.toNumber(_alt);
94
+
95
+ if (isNaN(this.alt)) {
96
+ throw new Error(`Invalid LatLng altitude: ${_alt}`);
97
+ }
106
98
  }
107
99
  }
108
100
 
@@ -135,17 +127,11 @@ export class LatLng {
135
127
  */
136
128
  // eslint-disable-next-line no-unused-vars
137
129
  static validate(lat, lng, alt) {
138
- if (lat instanceof LatLng || (typeof lat === 'object' && lat !== null && 'lat' in lat)) {
139
- return true;
140
- } else if (lat && Array.isArray(lat) && typeof lat[0] !== 'object') {
141
- if (lat.length === 3 || lat.length === 2) {
142
- return true;
143
- }
130
+ try {
131
+ return !!new LatLng(lat, lng, alt);
132
+ } catch {
144
133
  return false;
145
- } else if ((lat || lat === 0) && (lng || lng === 0)) {
146
- return true;
147
134
  }
148
- return false;
149
135
  }
150
136
 
151
137
  /**
@@ -98,7 +98,7 @@ export class LatLngBounds {
98
98
  if (!sw2) { return this; }
99
99
  } else if (!obj) {
100
100
  return this;
101
- } else if (LatLng.validate(obj)) {
101
+ } else if (isLatLngExpression(obj)) {
102
102
  sw2 = ne2 = LatLng.from(obj);
103
103
  } else {
104
104
  return this.extend(LatLngBounds.from(obj));
@@ -215,7 +215,7 @@ export class LatLngBounds {
215
215
  * @returns {boolean}
216
216
  */
217
217
  contains(obj) { // (LatLngBounds) or (LatLng) -> Boolean
218
- if (LatLng.validate(obj)) {
218
+ if (isLatLngExpression(obj)) {
219
219
  obj = LatLng.from(obj);
220
220
  } else {
221
221
  obj = LatLngBounds.from(obj);
@@ -270,8 +270,8 @@ export class LatLngBounds {
270
270
  sw2 = bounds.getSouthWest(),
271
271
  ne2 = bounds.getNorthEast(),
272
272
 
273
- latOverlaps = (ne2.lat > sw.lat) && (sw2.lat < ne.lat),
274
- lngOverlaps = (ne2.lng > sw.lng) && (sw2.lng < ne.lng);
273
+ latOverlaps = Math.max(sw.lat, sw2.lat) < Math.min(ne.lat, ne2.lat),
274
+ lngOverlaps = Math.max(sw.lng, sw2.lng) < Math.min(ne.lng, ne2.lng);
275
275
 
276
276
  return latOverlaps && lngOverlaps;
277
277
  }
@@ -311,3 +311,9 @@ export class LatLngBounds {
311
311
  return !!(this._southWest && this._northEast);
312
312
  }
313
313
  }
314
+
315
+ // Whether `obj` has the shape of a `LatLngExpression` rather than of bounds.
316
+ function isLatLngExpression(obj) {
317
+ return obj instanceof LatLng || (Array.isArray(obj) ? typeof obj[0] !== 'object' :
318
+ typeof obj === 'object' && obj !== null && 'lat' in obj);
319
+ }
@@ -206,8 +206,8 @@ export class Bounds {
206
206
  max = this.max,
207
207
  min2 = bounds.min,
208
208
  max2 = bounds.max,
209
- xOverlaps = (max2.x > min.x) && (min2.x < max.x),
210
- yOverlaps = (max2.y > min.y) && (min2.y < max.y);
209
+ xOverlaps = Math.max(min.x, min2.x) < Math.min(max.x, max2.x),
210
+ yOverlaps = Math.max(min.y, min2.y) < Math.min(max.y, max2.y);
211
211
 
212
212
  return xOverlaps && yOverlaps;
213
213
  }
@@ -258,5 +258,5 @@ export class Bounds {
258
258
 
259
259
  // Whether `obj` is a point (or an array or object literal of one) rather than bounds
260
260
  function isPointExpression(obj) {
261
- return obj instanceof Point || (Array.isArray(obj) ? typeof obj[0] === 'number' : 'x' in obj);
261
+ return obj instanceof Point || (Array.isArray(obj) ? typeof obj[0] !== 'object' : 'x' in obj);
262
262
  }
@@ -1,4 +1,5 @@
1
- import {formatNum} from '../core/Util.js';
1
+ // a named import of toNumber makes Point#add ~10% slower in V8
2
+ import * as Util from '../core/Util.js';
2
3
 
3
4
  /**
4
5
  * A point in pixels: a `Point`, an array of the form `[x, y]`, or a plain object of
@@ -38,37 +39,31 @@ export class Point {
38
39
  * @param {boolean} [round]
39
40
  */
40
41
  constructor(x, y, round) {
41
-
42
- const valid = Point.validate(x, y);
43
- if (!valid) {
44
- throw new Error(`Invalid Point object: (${x}, ${y})`);
45
- }
46
-
47
- let _x, _y;
48
- if (x instanceof Point) {
49
- _x = x.x;
50
- _y = x.y;
51
- } else if (Array.isArray(x)) {
42
+ let _x = x, _y = y;
43
+ if (Array.isArray(x)) {
52
44
  _x = x[0];
53
45
  _y = x[1];
54
- } else if (typeof x === 'object' && 'x' in x && 'y' in x) {
46
+ } else if (typeof x === 'object' && x !== null) {
55
47
  _x = x.x;
56
48
  _y = x.y;
57
- } else {
58
- _x = x;
59
- _y = y;
60
49
  }
61
50
 
62
51
  /**
63
52
  * The `x` coordinate of the point.
64
53
  * @type {number}
65
54
  */
66
- this.x = (round ? Math.round(_x) : _x);
55
+ this.x = round ? Math.round(Util.toNumber(_x)) : Util.toNumber(_x);
67
56
  /**
68
57
  * The `y` coordinate of the point.
69
58
  * @type {number}
70
59
  */
71
- this.y = (round ? Math.round(_y) : _y);
60
+ this.y = round ? Math.round(Util.toNumber(_y)) : Util.toNumber(_y);
61
+
62
+ // NaN check; isNaN() here makes Point#add ~10% slower in V8
63
+ // eslint-disable-next-line no-self-compare
64
+ if (this.x !== this.x || this.y !== this.y) {
65
+ throw new Error(`Invalid Point object: (${_x}, ${_y})`);
66
+ }
72
67
  }
73
68
 
74
69
  /**
@@ -98,14 +93,11 @@ export class Point {
98
93
  * @returns {boolean}
99
94
  */
100
95
  static validate(x, y) {
101
- if (x instanceof Point || Array.isArray(x)) {
102
- return true;
103
- } else if (x && typeof x === 'object' && 'x' in x && 'y' in x) {
104
- return true;
105
- } else if ((x || x === 0) && (y || y === 0)) {
106
- return true;
96
+ try {
97
+ return !!new Point(x, y);
98
+ } catch {
99
+ return false;
107
100
  }
108
- return false;
109
101
  }
110
102
 
111
103
  /**
@@ -293,6 +285,6 @@ export class Point {
293
285
  * @returns {string}
294
286
  */
295
287
  toString() {
296
- return `Point(${formatNum(this.x)}, ${formatNum(this.y)})`;
288
+ return `Point(${Util.formatNum(this.x)}, ${Util.formatNum(this.y)})`;
297
289
  }
298
290
  }
@@ -23,14 +23,7 @@ export class Transformation {
23
23
  * @param {number} [d]
24
24
  */
25
25
  constructor(a, b, c, d) {
26
- if (Array.isArray(a)) {
27
- // use array properties
28
- this._a = a[0];
29
- this._b = a[1];
30
- this._c = a[2];
31
- this._d = a[3];
32
- return;
33
- }
26
+ if (Array.isArray(a)) { [a, b, c, d] = a; }
34
27
  this._a = a;
35
28
  this._b = b;
36
29
  this._c = c;
@@ -72,7 +72,7 @@ export class BlanketOverlay extends Layer {
72
72
  this._container.classList.add('maplair-zoom-animated');
73
73
  }
74
74
 
75
- this.getPane().appendChild(this._container);
75
+ this.getPane().append(this._container);
76
76
  this._resizeContainer();
77
77
  this._onMoveEnd();
78
78
  }
@@ -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';
@@ -280,9 +285,10 @@ export class DivOverlay extends Layer {
280
285
  /**
281
286
  * Sets the HTML content of the overlay. A `String` argument is rendered as
282
287
  * 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.
288
+ * or pass an `HTMLElement` 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');
@@ -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;
@@ -303,7 +300,7 @@ export class ImageOverlay extends Layer {
303
300
  }
304
301
 
305
302
  _updateZIndex() {
306
- if (this._image && this.options.zIndex !== undefined && this.options.zIndex !== null) {
303
+ if (this._image && this.options.zIndex != null) {
307
304
  this._image.style.zIndex = `${this.options.zIndex}`;
308
305
  }
309
306
  }
@@ -325,4 +322,9 @@ export class ImageOverlay extends Layer {
325
322
  getCenter() {
326
323
  return this._bounds.getCenter();
327
324
  }
325
+
326
+ // like on paths, popups open where the overlay was clicked, so another click moves them
327
+ _togglesPopupOnClick() {
328
+ return false;
329
+ }
328
330
  }
@@ -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;
@@ -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
 
@@ -127,7 +127,7 @@ const layerPopupMethods = {
127
127
  * may contain untrusted input, sanitize it (e.g. with DOMPurify) or pass an
128
128
  * `HTMLElement` with safe `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;
@@ -351,12 +350,14 @@ export class Popup extends DivOverlay {
351
350
  openOn(map) {
352
351
  map = arguments.length ? map : this._source._map; // experimental, not the part of public api
353
352
 
354
- if (!map.hasLayer(this) && map._popup && map._popup.options.autoClose) {
353
+ if (!map.hasLayer(this) && map._popup?.options.autoClose) {
355
354
  map.removeLayer(map._popup);
356
355
  }
357
356
  map._popup = this;
358
357
 
359
- return super.openOn(map);
358
+ super.openOn(map);
359
+ this._updateSourceAria(true); // the source may have changed while open, e.g. within a FeatureGroup
360
+ return this;
360
361
  }
361
362
 
362
363
  /**
@@ -379,12 +380,13 @@ export class Popup extends DivOverlay {
379
380
  }
380
381
 
381
382
  map.fire('popupopen', {popup: this});
383
+ this._updateSourceAria(true);
382
384
 
383
385
  if (this._source) {
384
386
  this._source.fire('popupopen', {popup: this}, true);
385
387
  // For non-path layers, we toggle the popup when clicking
386
388
  // again the layer, so prevent the map to reopen it.
387
- if (!(this._source instanceof Path)) {
389
+ if (this._source._togglesPopupOnClick()) {
388
390
  this._source.on('preclick', DomEvent.stopPropagation);
389
391
  }
390
392
  }
@@ -395,18 +397,23 @@ export class Popup extends DivOverlay {
395
397
  * @param {MaplairMap} map
396
398
  */
397
399
  onRemove(map) {
398
- if (this._container.contains(document.activeElement) && this._returnFocus?.isConnected) {
399
- this._returnFocus.focus({preventScroll: true});
400
+ // closing removes the focused element, which would send focus back to the start of the page
401
+ if (this._container.contains(document.activeElement)) {
402
+ const el = this._source?.getElement?.();
403
+ const target = this._returnFocus?.isConnected ? this._returnFocus :
404
+ el?.tabIndex >= 0 ? el : map._container;
405
+ target.focus({preventScroll: true});
400
406
  }
401
407
  this._returnFocus = null;
402
408
 
403
409
  super.onRemove(map);
404
410
 
405
411
  map.fire('popupclose', {popup: this});
412
+ this._updateSourceAria(false);
406
413
 
407
414
  if (this._source) {
408
415
  this._source.fire('popupclose', {popup: this}, true);
409
- if (!(this._source instanceof Path)) {
416
+ if (this._source._togglesPopupOnClick()) {
410
417
  this._source.off('preclick', DomEvent.stopPropagation);
411
418
  }
412
419
  }
@@ -430,10 +437,24 @@ export class Popup extends DivOverlay {
430
437
  return events;
431
438
  }
432
439
 
440
+ // Marks a source that acts as a button (e.g. a keyboard-accessible marker) as a
441
+ // disclosure for the popup.
442
+ _updateSourceAria(open) {
443
+ this._ariaSource?.setAttribute('aria-expanded', 'false');
444
+ this._ariaSource?.removeAttribute('aria-controls');
445
+
446
+ const el = this._source?.getElement?.();
447
+ this._ariaSource = el?.getAttribute('role') === 'button' ? el : null;
448
+ this._ariaSource?.setAttribute('aria-expanded', String(open));
449
+ if (open) { this._ariaSource?.setAttribute('aria-controls', this._container.id); }
450
+ }
451
+
433
452
  _initLayout() {
434
453
  const prefix = 'maplair-popup',
435
454
  container = this._container = DomUtil.create('div', `${prefix} ${this.options.className || ''} maplair-zoom-animated`);
436
455
 
456
+ container.id = `maplair-popup-${Util.stamp(this)}`;
457
+
437
458
  const wrapper = this._wrapper = DomUtil.create('div', `${prefix}-content-wrapper`, container);
438
459
  this._contentNode = DomUtil.create('div', `${prefix}-content`, wrapper);
439
460
 
@@ -449,7 +470,7 @@ export class Popup extends DivOverlay {
449
470
  closeButton.type = 'button';
450
471
  closeButton.setAttribute('aria-label', I18n.translate('Close popup'));
451
472
 
452
- closeButton.innerHTML = '<span aria-hidden="true">&#215;</span>';
473
+ closeButton.textContent = '\u00D7';
453
474
 
454
475
  DomEvent.on(closeButton, 'click', (ev) => {
455
476
  ev.preventDefault();