maplair 4.0.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +178 -0
- package/dist/esm/Maplair.js +18 -17
- package/dist/esm/Maplair.js.map +1 -1
- package/dist/esm/_virtual/_rolldown/runtime.js +1 -2
- package/dist/esm/control/AttributionControl.js +25 -16
- package/dist/esm/control/AttributionControl.js.map +1 -1
- package/dist/esm/control/ButtonControl.js +10 -13
- package/dist/esm/control/ButtonControl.js.map +1 -1
- package/dist/esm/control/ContextMenu.js +2 -5
- package/dist/esm/control/ContextMenu.js.map +1 -1
- package/dist/esm/control/Control.js +10 -18
- package/dist/esm/control/Control.js.map +1 -1
- package/dist/esm/control/FullscreenControl.js +6 -9
- package/dist/esm/control/FullscreenControl.js.map +1 -1
- package/dist/esm/control/LayersControl.js +170 -65
- package/dist/esm/control/LayersControl.js.map +1 -1
- package/dist/esm/control/LegendControl.js +8 -11
- package/dist/esm/control/LegendControl.js.map +1 -1
- package/dist/esm/control/MiniMapControl.js +15 -18
- package/dist/esm/control/MiniMapControl.js.map +1 -1
- package/dist/esm/control/RotateControl.js +2 -5
- package/dist/esm/control/RotateControl.js.map +1 -1
- package/dist/esm/control/ScaleControl.js +8 -11
- package/dist/esm/control/ScaleControl.js.map +1 -1
- package/dist/esm/control/SearchControl.js +100 -19
- package/dist/esm/control/SearchControl.js.map +1 -1
- package/dist/esm/control/SideBySideControl.js +2 -5
- package/dist/esm/control/SideBySideControl.js.map +1 -1
- package/dist/esm/control/TextControl.js +6 -9
- package/dist/esm/control/TextControl.js.map +1 -1
- package/dist/esm/control/TimeControl.js +407 -0
- package/dist/esm/control/TimeControl.js.map +1 -0
- package/dist/esm/control/ZoomControl.js +23 -36
- package/dist/esm/control/ZoomControl.js.map +1 -1
- package/dist/esm/core/Class.js +28 -44
- package/dist/esm/core/Class.js.map +1 -1
- package/dist/esm/core/Events.js +22 -10
- package/dist/esm/core/Events.js.map +1 -1
- package/dist/esm/core/Handler.js +23 -14
- package/dist/esm/core/Handler.js.map +1 -1
- package/dist/esm/core/I18n.js +1 -2
- package/dist/esm/core/Util.js +15 -4
- package/dist/esm/core/Util.js.map +1 -1
- package/dist/esm/dom/DomEvent.DoubleTap.js +26 -24
- package/dist/esm/dom/DomEvent.DoubleTap.js.map +1 -1
- package/dist/esm/dom/DomEvent.PointerEvents.js +3 -18
- package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -1
- package/dist/esm/dom/DomEvent.js +18 -60
- package/dist/esm/dom/DomEvent.js.map +1 -1
- package/dist/esm/dom/DomUtil.js +15 -70
- package/dist/esm/dom/DomUtil.js.map +1 -1
- package/dist/esm/dom/Draggable.js +21 -21
- package/dist/esm/dom/Draggable.js.map +1 -1
- package/dist/esm/dom/PosAnimation.js +4 -27
- package/dist/esm/dom/PosAnimation.js.map +1 -1
- package/dist/esm/geo/LatLng.js +16 -7
- package/dist/esm/geo/LatLng.js.map +1 -1
- package/dist/esm/geo/LatLngBounds.js +9 -15
- package/dist/esm/geo/LatLngBounds.js.map +1 -1
- package/dist/esm/geo/crs/CRS.js +1 -2
- package/dist/esm/geo/crs/EPSG3395.js +5 -6
- package/dist/esm/geo/crs/EPSG3395.js.map +1 -1
- package/dist/esm/geo/crs/EPSG3857.js +5 -6
- package/dist/esm/geo/crs/EPSG3857.js.map +1 -1
- package/dist/esm/geo/crs/EPSG4326.js +4 -5
- package/dist/esm/geo/crs/EPSG4326.js.map +1 -1
- package/dist/esm/geo/crs/EarthCRS.js +3 -4
- package/dist/esm/geo/crs/EarthCRS.js.map +1 -1
- package/dist/esm/geo/crs/SimpleCRS.js +4 -5
- package/dist/esm/geo/crs/SimpleCRS.js.map +1 -1
- package/dist/esm/geo/projection/{Projection.LonLat.js → LonLatProjection.js} +5 -6
- package/dist/esm/geo/projection/LonLatProjection.js.map +1 -0
- package/dist/esm/geo/projection/{Projection.Mercator.js → MercatorProjection.js} +4 -5
- package/dist/esm/geo/projection/MercatorProjection.js.map +1 -0
- package/dist/esm/geo/projection/{Projection.SphericalMercator.js → SphericalMercatorProjection.js} +5 -6
- package/dist/esm/geo/projection/SphericalMercatorProjection.js.map +1 -0
- package/dist/esm/geometry/Bounds.js +10 -14
- package/dist/esm/geometry/Bounds.js.map +1 -1
- package/dist/esm/geometry/LineUtil.js +37 -32
- package/dist/esm/geometry/LineUtil.js.map +1 -1
- package/dist/esm/geometry/Point.js +1 -2
- package/dist/esm/geometry/PolyUtil.js +24 -51
- package/dist/esm/geometry/PolyUtil.js.map +1 -1
- package/dist/esm/geometry/Transformation.js +1 -2
- package/dist/esm/layer/BlanketOverlay.js +7 -10
- package/dist/esm/layer/BlanketOverlay.js.map +1 -1
- package/dist/esm/layer/Classification.js +1 -2
- package/dist/esm/layer/DivOverlay.js +16 -22
- package/dist/esm/layer/DivOverlay.js.map +1 -1
- package/dist/esm/layer/FeatureGroup.js +5 -4
- package/dist/esm/layer/FeatureGroup.js.map +1 -1
- package/dist/esm/layer/GeoJSON.Load.js +1 -2
- package/dist/esm/layer/GeoJSON.js +17 -155
- package/dist/esm/layer/GeoJSON.js.map +1 -1
- package/dist/esm/layer/GeoJSONUtil.js +74 -0
- package/dist/esm/layer/GeoJSONUtil.js.map +1 -0
- package/dist/esm/layer/HeatLayer.js +17 -20
- package/dist/esm/layer/HeatLayer.js.map +1 -1
- package/dist/esm/layer/ImageOverlay.js +22 -29
- package/dist/esm/layer/ImageOverlay.js.map +1 -1
- package/dist/esm/layer/Layer.js +9 -12
- package/dist/esm/layer/Layer.js.map +1 -1
- package/dist/esm/layer/LayerGroup.js +53 -3
- package/dist/esm/layer/LayerGroup.js.map +1 -1
- package/dist/esm/layer/Popup.js +35 -58
- package/dist/esm/layer/Popup.js.map +1 -1
- package/dist/esm/layer/SVGOverlay.js +3 -7
- package/dist/esm/layer/SVGOverlay.js.map +1 -1
- package/dist/esm/layer/Tooltip.js +31 -34
- package/dist/esm/layer/Tooltip.js.map +1 -1
- package/dist/esm/layer/VideoOverlay.js +13 -17
- package/dist/esm/layer/VideoOverlay.js.map +1 -1
- package/dist/esm/layer/marker/DefaultIcon.js +38 -49
- package/dist/esm/layer/marker/DefaultIcon.js.map +1 -1
- package/dist/esm/layer/marker/DivIcon.js +8 -11
- package/dist/esm/layer/marker/DivIcon.js.map +1 -1
- package/dist/esm/layer/marker/Icon.js +9 -12
- package/dist/esm/layer/marker/Icon.js.map +1 -1
- package/dist/esm/layer/marker/Marker.Drag.js +44 -5
- package/dist/esm/layer/marker/Marker.Drag.js.map +1 -1
- package/dist/esm/layer/marker/Marker.Move.js +5 -8
- package/dist/esm/layer/marker/Marker.Move.js.map +1 -1
- package/dist/esm/layer/marker/Marker.js +115 -47
- package/dist/esm/layer/marker/Marker.js.map +1 -1
- package/dist/esm/layer/marker/MarkerClusterGroup.js +301 -0
- package/dist/esm/layer/marker/MarkerClusterGroup.js.map +1 -0
- package/dist/esm/layer/marker/SvgIcon.js +7 -10
- package/dist/esm/layer/marker/SvgIcon.js.map +1 -1
- package/dist/esm/layer/tile/GridLayer.js +89 -101
- package/dist/esm/layer/tile/GridLayer.js.map +1 -1
- package/dist/esm/layer/tile/TileLayer.js +36 -44
- package/dist/esm/layer/tile/TileLayer.js.map +1 -1
- package/dist/esm/layer/tile/WMSTileLayer.js +33 -27
- package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -1
- package/dist/esm/layer/vector/Canvas.js +8 -17
- package/dist/esm/layer/vector/Canvas.js.map +1 -1
- package/dist/esm/layer/vector/Circle.js +6 -5
- package/dist/esm/layer/vector/Circle.js.map +1 -1
- package/dist/esm/layer/vector/CircleMarker.js +19 -10
- package/dist/esm/layer/vector/CircleMarker.js.map +1 -1
- package/dist/esm/layer/vector/Path.DashAnimation.js +3 -5
- package/dist/esm/layer/vector/Path.DashAnimation.js.map +1 -1
- package/dist/esm/layer/vector/Path.ZoomStyles.js +1 -2
- package/dist/esm/layer/vector/Path.js +28 -22
- package/dist/esm/layer/vector/Path.js.map +1 -1
- package/dist/esm/layer/vector/Polygon.js +22 -7
- package/dist/esm/layer/vector/Polygon.js.map +1 -1
- package/dist/esm/layer/vector/Polyline.Decorations.js +3 -4
- package/dist/esm/layer/vector/Polyline.js +25 -10
- package/dist/esm/layer/vector/Polyline.js.map +1 -1
- package/dist/esm/layer/vector/Rectangle.js +1 -2
- package/dist/esm/layer/vector/Renderer.getRenderer.js +17 -17
- package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -1
- package/dist/esm/layer/vector/Renderer.js +1 -2
- package/dist/esm/layer/vector/SVG.js +12 -9
- package/dist/esm/layer/vector/SVG.js.map +1 -1
- package/dist/esm/map/Hash.js +1 -2
- package/dist/esm/map/Map.Rotation.js +41 -22
- package/dist/esm/map/Map.Rotation.js.map +1 -1
- package/dist/esm/map/Map.js +125 -126
- package/dist/esm/map/Map.js.map +1 -1
- package/dist/esm/map/MapSync.js +1 -2
- package/dist/esm/map/handler/BoxZoomHandler.js +25 -47
- package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -1
- package/dist/esm/map/handler/DisplaceHandler.js +7 -10
- package/dist/esm/map/handler/DisplaceHandler.js.map +1 -1
- package/dist/esm/map/handler/DoubleClickZoomHandler.js +1 -2
- package/dist/esm/map/handler/DragHandler.js +20 -21
- package/dist/esm/map/handler/DragHandler.js.map +1 -1
- package/dist/esm/map/handler/KeyboardHandler.js +48 -68
- package/dist/esm/map/handler/KeyboardHandler.js.map +1 -1
- package/dist/esm/map/handler/PinchZoomHandler.js +13 -5
- package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -1
- package/dist/esm/map/handler/ScrollWheelZoomHandler.js +4 -6
- package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -1
- package/dist/esm/map/handler/TapDragZoomHandler.js +1 -2
- package/dist/esm/map/handler/TapHoldHandler.js +13 -7
- package/dist/esm/map/handler/TapHoldHandler.js.map +1 -1
- package/dist/esm/package.js +2 -3
- package/dist/maplair-global-src.js +6495 -5981
- package/dist/maplair-global-src.js.map +1 -1
- package/dist/maplair-global.js +2 -2
- package/dist/maplair-global.js.map +1 -1
- package/dist/maplair-src.js +6206 -5673
- package/dist/maplair-src.js.map +1 -1
- package/dist/maplair.css +1 -1213
- package/dist/maplair.js +2 -2
- package/dist/maplair.js.map +1 -1
- package/i18n/cs.json +6 -0
- package/i18n/de.json +6 -0
- package/i18n/en.json +6 -0
- package/i18n/es.json +6 -0
- package/i18n/fr.json +6 -0
- package/i18n/hu.json +6 -0
- package/i18n/it.json +6 -0
- package/i18n/rm.json +6 -0
- package/i18n/sk.json +6 -0
- package/i18n/sl.json +6 -0
- package/package.json +11 -18
- package/src/Maplair.all.js +0 -1
- package/src/control/AttributionControl.js +25 -18
- package/src/control/ButtonControl.js +9 -11
- package/src/control/ContextMenu.js +3 -5
- package/src/control/Control.js +9 -19
- package/src/control/FullscreenControl.js +5 -7
- package/src/control/LayersControl.js +196 -86
- package/src/control/LegendControl.js +7 -9
- package/src/control/MiniMapControl.js +9 -11
- package/src/control/RotateControl.js +3 -5
- package/src/control/ScaleControl.js +7 -9
- package/src/control/SearchControl.js +102 -19
- package/src/control/SideBySideControl.js +3 -5
- package/src/control/TextControl.js +5 -7
- package/src/control/TimeControl.js +471 -0
- package/src/control/ZoomControl.js +22 -38
- package/src/control/index.js +1 -0
- package/src/core/Class.js +37 -45
- package/src/core/Events.js +22 -3
- package/src/core/Handler.js +25 -13
- package/src/core/Util.js +20 -2
- package/src/core/index.js +0 -1
- package/src/dom/DomEvent.DoubleTap.js +18 -10
- package/src/dom/DomEvent.PointerEvents.js +3 -26
- package/src/dom/DomEvent.js +17 -85
- package/src/dom/DomUtil.js +22 -78
- package/src/dom/Draggable.js +32 -23
- package/src/dom/PosAnimation.js +3 -25
- package/src/dom/index.js +0 -1
- package/src/geo/LatLng.js +20 -3
- package/src/geo/LatLngBounds.js +8 -15
- package/src/geo/crs/EPSG3395.js +4 -4
- package/src/geo/crs/EPSG3857.js +4 -4
- package/src/geo/crs/EPSG4326.js +3 -3
- package/src/geo/crs/EarthCRS.js +2 -8
- package/src/geo/crs/SimpleCRS.js +3 -3
- package/src/geo/index.js +1 -2
- package/src/geo/projection/{Projection.LonLat.js → LonLatProjection.js} +2 -2
- package/src/geo/projection/{Projection.Mercator.js → MercatorProjection.js} +2 -2
- package/src/geo/projection/{Projection.SphericalMercator.js → SphericalMercatorProjection.js} +2 -2
- package/src/geo/projection/index.js +5 -5
- package/src/geometry/Bounds.js +12 -17
- package/src/geometry/LineUtil.js +48 -49
- package/src/geometry/PolyUtil.js +39 -76
- package/src/layer/BlanketOverlay.js +6 -10
- package/src/layer/DivOverlay.js +16 -27
- package/src/layer/FeatureGroup.js +8 -2
- package/src/layer/GeoJSON.js +17 -218
- package/src/layer/GeoJSONUtil.js +85 -0
- package/src/layer/HeatLayer.js +10 -12
- package/src/layer/ImageOverlay.js +21 -32
- package/src/layer/Layer.js +8 -10
- package/src/layer/LayerGroup.js +72 -1
- package/src/layer/Popup.js +37 -62
- package/src/layer/SVGOverlay.js +1 -5
- package/src/layer/Tooltip.js +34 -36
- package/src/layer/VideoOverlay.js +12 -15
- package/src/layer/marker/DefaultIcon.js +40 -58
- package/src/layer/marker/DivIcon.js +9 -11
- package/src/layer/marker/Icon.js +9 -10
- package/src/layer/marker/Marker.Drag.js +54 -6
- package/src/layer/marker/Marker.Move.js +4 -6
- package/src/layer/marker/Marker.js +145 -52
- package/src/layer/marker/MarkerClusterGroup.js +370 -0
- package/src/layer/marker/SvgIcon.js +7 -9
- package/src/layer/marker/index.js +1 -0
- package/src/layer/tile/GridLayer.js +108 -135
- package/src/layer/tile/TileLayer.js +44 -49
- package/src/layer/tile/WMSTileLayer.js +31 -26
- package/src/layer/vector/Canvas.js +13 -19
- package/src/layer/vector/Circle.js +9 -3
- package/src/layer/vector/CircleMarker.js +19 -7
- package/src/layer/vector/Path.DashAnimation.js +1 -2
- package/src/layer/vector/Path.js +30 -21
- package/src/layer/vector/Polygon.js +29 -6
- package/src/layer/vector/Polyline.js +26 -7
- package/src/layer/vector/Renderer.getRenderer.js +20 -19
- package/src/layer/vector/SVG.js +17 -7
- package/src/map/Map.Rotation.js +40 -8
- package/src/map/Map.js +158 -153
- package/src/map/handler/BoxZoomHandler.js +30 -50
- package/src/map/handler/DisplaceHandler.js +6 -8
- package/src/map/handler/DragHandler.js +25 -28
- package/src/map/handler/KeyboardHandler.js +65 -90
- package/src/map/handler/PinchZoomHandler.js +16 -5
- package/src/map/handler/ScrollWheelZoomHandler.js +6 -4
- package/src/map/handler/TapHoldHandler.js +15 -4
- package/src/map/index.js +1 -1
- package/src/maplair.css +153 -98
- package/dist/esm/core/Browser.js +0 -100
- package/dist/esm/core/Browser.js.map +0 -1
- package/dist/esm/geo/projection/Projection.LonLat.js.map +0 -1
- package/dist/esm/geo/projection/Projection.Mercator.js.map +0 -1
- package/dist/esm/geo/projection/Projection.SphericalMercator.js.map +0 -1
- package/dist/esm/geo/projection/index.js +0 -14
- package/dist/esm/geo/projection/index.js.map +0 -1
- package/dist/esm/layer/marker/Marker.Rotation.js +0 -70
- package/dist/esm/layer/marker/Marker.Rotation.js.map +0 -1
- package/dist/images/layers.svg +0 -1
- package/dist/images/marker-icon.svg +0 -1
- package/dist/images/marker-shadow.svg +0 -1
- package/src/core/Browser.js +0 -110
- package/src/images/layers.svg +0 -1
- package/src/images/marker-icon.svg +0 -1
- package/src/images/marker-shadow.svg +0 -1
- package/src/layer/marker/Marker.Rotation.js +0 -103
|
@@ -11,6 +11,8 @@ import {Marker} from '../layer/marker/Marker.js';
|
|
|
11
11
|
/** @import {MaplairEvent} from '../core/Events.js' */
|
|
12
12
|
/** @import {LatLngExpression} from '../geo/LatLng.js' */
|
|
13
13
|
/** @import {LatLngBoundsExpression} from '../geo/LatLngBounds.js' */
|
|
14
|
+
/** @import {Layer} from '../layer/Layer.js' */
|
|
15
|
+
/** @import {LayerGroup} from '../layer/LayerGroup.js' */
|
|
14
16
|
|
|
15
17
|
const searchIcon = '<svg aria-hidden="true" width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2"><circle cx="6.5" cy="6.5" r="4.5"/><path d="M10 10l4.5 4.5"/></svg>';
|
|
16
18
|
|
|
@@ -36,6 +38,8 @@ function delay(ms, signal) {
|
|
|
36
38
|
* @property {LatLngBoundsExpression} [bounds] The extent of the result, e.g. of a city,
|
|
37
39
|
* which the map fits to instead of zooming to the position.
|
|
38
40
|
* @property {any} [raw] The original result of the service, for use in event listeners.
|
|
41
|
+
* @property {Layer} [layer] The found layer, by [`SearchControl.layers()`](#searchcontrol-layers),
|
|
42
|
+
* e.g. to open its tooltip in a `searchresult` listener.
|
|
39
43
|
*/
|
|
40
44
|
|
|
41
45
|
/**
|
|
@@ -69,9 +73,17 @@ function delay(ms, signal) {
|
|
|
69
73
|
* [`autoComplete`](#searchcontrol-autocomplete).
|
|
70
74
|
* @property {number} [zoom=16] The zoom level for results without bounds, and the
|
|
71
75
|
* maximum zoom level for fitting the bounds of results.
|
|
72
|
-
* @property {boolean} [marker=true] Whether
|
|
76
|
+
* @property {boolean|((result: SearchResult) => Layer|null|undefined)} [marker=true] Whether
|
|
77
|
+
* to show a marker at the selected result, or a function returning the layer to show
|
|
78
|
+
* instead, e.g. a `CircleMarker` highlighting the result (or nothing for none). The
|
|
79
|
+
* layer is removed when the search is cleared or another result is selected.
|
|
80
|
+
* @property {boolean} [clearOnMapClick=false] If `true`, a click on the map clears the
|
|
81
|
+
* search (the field, the results and the marker) and collapses the control. By
|
|
82
|
+
* default, the click only hides the results and the marker stays.
|
|
73
83
|
* @property {string} [placeholder] The placeholder of the search field, by default
|
|
74
84
|
* the translation of "Search".
|
|
85
|
+
* @property {string} [noResultsText] The message shown if nothing is found, by
|
|
86
|
+
* default the translation of "No results".
|
|
75
87
|
* @extends ControlOptions
|
|
76
88
|
*/
|
|
77
89
|
|
|
@@ -126,19 +138,19 @@ function delay(ms, signal) {
|
|
|
126
138
|
*/
|
|
127
139
|
export class SearchControl extends Control {
|
|
128
140
|
|
|
129
|
-
static {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
141
|
+
static defaultOptions = {
|
|
142
|
+
position: 'topleft',
|
|
143
|
+
geocoder: undefined,
|
|
144
|
+
collapsed: true,
|
|
145
|
+
autoComplete: false,
|
|
146
|
+
autoCompleteDelay: 250,
|
|
147
|
+
minLength: 3,
|
|
148
|
+
zoom: 16,
|
|
149
|
+
marker: true,
|
|
150
|
+
clearOnMapClick: false,
|
|
151
|
+
placeholder: undefined,
|
|
152
|
+
noResultsText: undefined
|
|
153
|
+
};
|
|
142
154
|
|
|
143
155
|
/**
|
|
144
156
|
* Returns a [geocoder](#searchcontrol-geocoder) searching with
|
|
@@ -218,6 +230,73 @@ export class SearchControl extends Control {
|
|
|
218
230
|
});
|
|
219
231
|
}
|
|
220
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Returns a [geocoder](#searchcontrol-geocoder) searching the layers of a layer group,
|
|
235
|
+
* e.g. the markers of a `FeatureGroup` or the features of a `GeoJSON` layer, by their
|
|
236
|
+
* label: a property of their GeoJSON feature (`layer.feature.properties`) or the
|
|
237
|
+
* result of a function. The group is searched at the time of the search, so layers
|
|
238
|
+
* added later are found as well, and nested groups without a label are searched
|
|
239
|
+
* recursively. The search ignores the case and finds the query anywhere in the
|
|
240
|
+
* label; labels starting with it come first, then in alphabetical order. The results
|
|
241
|
+
* contain the found `layer` and its GeoJSON feature as `raw`; layers without a
|
|
242
|
+
* single position (e.g. polygons) have the `bounds` and their center as `latlng`.
|
|
243
|
+
* Combine it with [`autoComplete`](#searchcontrol-autocomplete) to search while
|
|
244
|
+
* typing, and with [`marker`](#searchcontrol-marker) `false` or a function to not
|
|
245
|
+
* cover the found marker with another one.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```js
|
|
249
|
+
* const stations = new GeoJSON(data, {
|
|
250
|
+
* onEachFeature: (feature, layer) => layer.bindTooltip(feature.properties.name)
|
|
251
|
+
* }).addTo(map);
|
|
252
|
+
* new SearchControl({
|
|
253
|
+
* geocoder: SearchControl.layers(stations, {property: 'name'}),
|
|
254
|
+
* autoComplete: true,
|
|
255
|
+
* minLength: 1,
|
|
256
|
+
* marker: false
|
|
257
|
+
* }).addTo(map);
|
|
258
|
+
* map.on('searchresult', ({result}) => result.layer.openTooltip());
|
|
259
|
+
* ```
|
|
260
|
+
* @param {LayerGroup} group The layer group to search.
|
|
261
|
+
* @param {object} [options]
|
|
262
|
+
* @param {string} [options.property='name'] The property of the GeoJSON features to
|
|
263
|
+
* search.
|
|
264
|
+
* @param {(layer: Layer) => string|number|null|undefined} [options.label] A function
|
|
265
|
+
* returning the label of a layer instead of the `property`, nothing to skip the
|
|
266
|
+
* layer (or to search its layers, if it is a group).
|
|
267
|
+
* @param {number} [options.limit=10] The maximum number of results.
|
|
268
|
+
* @returns {Geocoder}
|
|
269
|
+
*/
|
|
270
|
+
static layers(group, {property = 'name', label = layer => layer.feature?.properties?.[property], limit = 10} = {}) {
|
|
271
|
+
return (query) => {
|
|
272
|
+
query = query.toLocaleLowerCase();
|
|
273
|
+
const found = [];
|
|
274
|
+
group.eachLayer(function search(layer) {
|
|
275
|
+
const text = label(layer);
|
|
276
|
+
if (text === undefined || text === null || text === '') {
|
|
277
|
+
layer.eachLayer?.(search);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const result = {label: String(text), layer, raw: layer.feature},
|
|
281
|
+
index = result.label.toLocaleLowerCase().indexOf(query);
|
|
282
|
+
if (index < 0) { return; }
|
|
283
|
+
if (layer.getLatLng) {
|
|
284
|
+
result.latlng = layer.getLatLng();
|
|
285
|
+
} else if (layer.getBounds?.().isValid()) {
|
|
286
|
+
result.bounds = layer.getBounds();
|
|
287
|
+
result.latlng = result.bounds.getCenter();
|
|
288
|
+
} else {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
found.push({result, prefix: index === 0});
|
|
292
|
+
});
|
|
293
|
+
return Promise.resolve(found
|
|
294
|
+
.sort((a, b) => b.prefix - a.prefix || a.result.label.localeCompare(b.result.label))
|
|
295
|
+
.slice(0, limit)
|
|
296
|
+
.map(({result}) => result));
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
221
300
|
/**
|
|
222
301
|
* Creates a search control.
|
|
223
302
|
* @param {...SearchControlOptions} args The options.
|
|
@@ -349,7 +428,7 @@ export class SearchControl extends Control {
|
|
|
349
428
|
try {
|
|
350
429
|
const results = await this.options.geocoder(query, {signal: controller.signal});
|
|
351
430
|
if (!controller.signal.aborted) {
|
|
352
|
-
this._showResults(results, I18n.translate('No results'));
|
|
431
|
+
this._showResults(results, this.options.noResultsText ?? I18n.translate('No results'));
|
|
353
432
|
}
|
|
354
433
|
return results;
|
|
355
434
|
} catch (error) {
|
|
@@ -385,9 +464,10 @@ export class SearchControl extends Control {
|
|
|
385
464
|
}
|
|
386
465
|
|
|
387
466
|
this._marker?.remove();
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
467
|
+
const {marker} = this.options;
|
|
468
|
+
this._marker = typeof marker === 'function' ?
|
|
469
|
+
marker(result)?.addTo(map) ?? null :
|
|
470
|
+
marker ? new Marker(latlng, {title: result.label}).addTo(map) : null;
|
|
391
471
|
|
|
392
472
|
map.fire('searchresult', {result, latlng, bounds});
|
|
393
473
|
return this;
|
|
@@ -521,7 +601,10 @@ export class SearchControl extends Control {
|
|
|
521
601
|
}
|
|
522
602
|
|
|
523
603
|
_onMapClick() {
|
|
524
|
-
if (this.
|
|
604
|
+
if (this.options.clearOnMapClick) {
|
|
605
|
+
this.clear();
|
|
606
|
+
this.collapse();
|
|
607
|
+
} else if (this._input.value) {
|
|
525
608
|
this._showResults([]);
|
|
526
609
|
} else {
|
|
527
610
|
this.collapse();
|
|
@@ -43,11 +43,9 @@ import {I18n} from '../core/I18n.js';
|
|
|
43
43
|
*/
|
|
44
44
|
export class SideBySideControl extends Control {
|
|
45
45
|
|
|
46
|
-
static {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
});
|
|
50
|
-
}
|
|
46
|
+
static defaultOptions = {
|
|
47
|
+
ratio: 0.5
|
|
48
|
+
};
|
|
51
49
|
|
|
52
50
|
/**
|
|
53
51
|
* Creates a side-by-side control for the given layers (which have to be added to the
|
|
@@ -27,13 +27,11 @@ import * as DomEvent from '../dom/DomEvent.js';
|
|
|
27
27
|
*/
|
|
28
28
|
export class TextControl extends Control {
|
|
29
29
|
|
|
30
|
-
static {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
});
|
|
36
|
-
}
|
|
30
|
+
static defaultOptions = {
|
|
31
|
+
position: 'topright',
|
|
32
|
+
content: '',
|
|
33
|
+
className: ''
|
|
34
|
+
};
|
|
37
35
|
|
|
38
36
|
/**
|
|
39
37
|
* Creates a text control.
|
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import {Control} from './Control.js';
|
|
2
|
+
import {ButtonControl} from './ButtonControl.js';
|
|
3
|
+
import * as DomUtil from '../dom/DomUtil.js';
|
|
4
|
+
import * as DomEvent from '../dom/DomEvent.js';
|
|
5
|
+
import {I18n} from '../core/I18n.js';
|
|
6
|
+
/** @import {ControlOptions} from './Control.js' */
|
|
7
|
+
/** @import {MaplairMap} from '../map/Map.js' */
|
|
8
|
+
/** @import {Layer} from '../layer/Layer.js' */
|
|
9
|
+
/** @import {MaplairEvent} from '../core/Events.js' */
|
|
10
|
+
|
|
11
|
+
const icon = path => `<svg aria-hidden="true" width="16" height="16" viewBox="0 0 16 16" fill="currentColor"><path d="${path}"/></svg>`;
|
|
12
|
+
const previousIcon = icon('M3 3h2v10H3zm10 0v10L6 8z'),
|
|
13
|
+
playIcon = icon('M4 2v12l10-6z'),
|
|
14
|
+
pauseIcon = icon('M4 3h3v10H4zm5 0h3v10H9z'),
|
|
15
|
+
nextIcon = icon('M11 3h2v10h-2zM3 3v10l7-5z');
|
|
16
|
+
|
|
17
|
+
const durationRe = /^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/;
|
|
18
|
+
|
|
19
|
+
// Parses an ISO 8601 duration like `'PT5M'` or `'P1Y2M'` into calendar years and months
|
|
20
|
+
// and the milliseconds of the units with a fixed length
|
|
21
|
+
function parseDuration(duration) {
|
|
22
|
+
const match = durationRe.exec(duration);
|
|
23
|
+
if (!match || duration === 'P' || duration.endsWith('T')) {
|
|
24
|
+
throw new Error(`Invalid or unsupported ISO 8601 duration "${duration}".`);
|
|
25
|
+
}
|
|
26
|
+
const [years, months, weeks, days, hours, minutes, seconds] = match.slice(1).map(n => Number(n ?? 0));
|
|
27
|
+
return {years, months, ms: ((((weeks * 7 + days) * 24 + hours) * 60 + minutes) * 60 + seconds) * 1000};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Returns the time `n` times the duration after the given time, adding years and months in UTC
|
|
31
|
+
function addDuration(time, duration, n) {
|
|
32
|
+
const date = new Date(time);
|
|
33
|
+
date.setUTCFullYear(date.getUTCFullYear() + n * duration.years, date.getUTCMonth() + n * duration.months);
|
|
34
|
+
return new Date(+date + n * duration.ms);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function parseDate(date) {
|
|
38
|
+
const time = Date.parse(date);
|
|
39
|
+
if (Number.isNaN(time)) {
|
|
40
|
+
throw new Error(`Invalid date "${date}".`);
|
|
41
|
+
}
|
|
42
|
+
return new Date(time);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Returns the times of an ISO 8601 interval (start/end, start/duration or duration/end,
|
|
46
|
+
// optionally with the period as third part), every period from the start
|
|
47
|
+
function intervalTimes(interval, period) {
|
|
48
|
+
const parts = interval.split('/');
|
|
49
|
+
if (parts.length === 3) {
|
|
50
|
+
period = parts.pop();
|
|
51
|
+
}
|
|
52
|
+
if (parts.length !== 2) {
|
|
53
|
+
throw new Error(`Invalid ISO 8601 interval "${interval}".`);
|
|
54
|
+
}
|
|
55
|
+
const [first, second] = parts,
|
|
56
|
+
step = parseDuration(period);
|
|
57
|
+
let start, end;
|
|
58
|
+
if (first.startsWith('P')) {
|
|
59
|
+
end = parseDate(second);
|
|
60
|
+
start = addDuration(end, parseDuration(first), -1);
|
|
61
|
+
} else {
|
|
62
|
+
start = parseDate(first);
|
|
63
|
+
end = second.startsWith('P') ? addDuration(start, parseDuration(second), 1) : parseDate(second);
|
|
64
|
+
}
|
|
65
|
+
if (!(addDuration(start, step, 1) > start)) {
|
|
66
|
+
throw new Error(`The period "${period}" has to be positive.`);
|
|
67
|
+
}
|
|
68
|
+
const times = [];
|
|
69
|
+
for (let i = 0, time = start; time <= end; time = addDuration(start, step, ++i)) {
|
|
70
|
+
times.push(time);
|
|
71
|
+
}
|
|
72
|
+
return times;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Options of `TimeControl`.
|
|
77
|
+
* @typedef {object} TimeControlOptions
|
|
78
|
+
* @property {string} [position='bottomleft'] The position of the control (one of the map
|
|
79
|
+
* corners). Possible values are `'topleft'`, `'topright'`, `'bottomleft'` or
|
|
80
|
+
* `'bottomright'`.
|
|
81
|
+
* @property {(Date|number|string)[]} [times] The times to step through in ascending
|
|
82
|
+
* order: dates, timestamps or date strings (parsed with `Date.parse()`).
|
|
83
|
+
* @property {string} [interval] Instead of `times`, an ISO 8601 time interval whose times
|
|
84
|
+
* every [`period`](#timecontrol-period) from the start up to the end are stepped
|
|
85
|
+
* through: `start/end` (e.g. `'2026-10-17T00:00Z/2026-10-18T00:00Z'`), `start/duration`
|
|
86
|
+
* (e.g. `'2026-10-17T00:00Z/P1D'`) or `duration/end` (e.g. `'PT24H/2026-10-18T00:00Z'`).
|
|
87
|
+
* A third part is the period, as in the time dimension of WMS capabilities (e.g.
|
|
88
|
+
* `'2026-10-17T00:00Z/2026-10-18T00:00Z/PT5M'`). Start and end are parsed with
|
|
89
|
+
* `Date.parse()`, e.g. `2026-10-17`, `2026-10-17T12:00Z` or
|
|
90
|
+
* `2026-10-17T12:00:00.000+02:00` (without a time zone, times are local times).
|
|
91
|
+
* @property {string} [period='P1D'] The ISO 8601 duration between the times of the
|
|
92
|
+
* `interval`: integer numbers of years, months, weeks, days, hours, minutes and seconds,
|
|
93
|
+
* e.g. `'PT1H'`, `'PT5M'`, `'P1DT12H'` or `'P1M'` (years and months are added in UTC).
|
|
94
|
+
* @property {Date|number|string} [time] The initial time (the nearest of the times), by
|
|
95
|
+
* default the current time.
|
|
96
|
+
* @property {(Layer & TimeLayer)[]} [layers=[]] The layers showing the current time, see
|
|
97
|
+
* `addLayer()`.
|
|
98
|
+
* @property {number} [transitionTime=1000] The minimum time in milliseconds a time is
|
|
99
|
+
* shown while playing. The next time is shown only after the layers have loaded, too.
|
|
100
|
+
* @property {boolean} [loop=false] Whether playing starts over after the last time
|
|
101
|
+
* instead of stopping, and the buttons step from the last time to the first one and
|
|
102
|
+
* back.
|
|
103
|
+
* @property {(date: Date) => string} [formatTime] Returns the text of the label for a
|
|
104
|
+
* time, by default the date and time by `Intl.DateTimeFormat` in the [locale](#i18n)
|
|
105
|
+
* (or the browser's language) and the browser's time zone.
|
|
106
|
+
* @extends ControlOptions
|
|
107
|
+
*/
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* A layer showing a given time, see `TimeControl`.
|
|
111
|
+
* @typedef {object} TimeLayer
|
|
112
|
+
* @property {(date: Date) => any} [setTime] Shows the given time.
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The event object of `timechange`.
|
|
117
|
+
* @typedef {object} TimeChangeEvent
|
|
118
|
+
* @property {Date} time The current time.
|
|
119
|
+
* @property {number} index The index of the current time in the times of the control.
|
|
120
|
+
* @extends MaplairEvent
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* A control to step through or play the times of time-dependent layers, e.g. an
|
|
125
|
+
* animated weather radar from a WMS with a `TIME` dimension. It has buttons for the
|
|
126
|
+
* previous and next time and to play or pause, a slider (usable with the arrow, Home
|
|
127
|
+
* and End keys) and a label with the current time.
|
|
128
|
+
*
|
|
129
|
+
* Each of the `layers` shows the current time: by its method `setTime(date)` if it has
|
|
130
|
+
* one, otherwise by the parameter `time` set to the time in ISO 8601 format
|
|
131
|
+
* (`date.toISOString()`): the WMS parameter of a `WMSTileLayer` (with `setParams()`) or
|
|
132
|
+
* the variable of the URL template of a `TileLayer` (e.g.
|
|
133
|
+
* `'https://…/{time}/{z}/{x}/{y}.png'`, with `setUrl()`). The tiles of the previous
|
|
134
|
+
* time stay visible until the new ones have loaded, and playing waits for them (the
|
|
135
|
+
* `load` event), so that no time is skipped with slow servers.
|
|
136
|
+
*
|
|
137
|
+
* Replaces the [Leaflet.TimeDimension](https://github.com/socib/Leaflet.TimeDimension)
|
|
138
|
+
* plugin for the common use cases.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```js
|
|
142
|
+
* const radar = new WMSTileLayer('https://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r-t.cgi', {
|
|
143
|
+
* layers: 'nexrad-n0r-wmst',
|
|
144
|
+
* format: 'image/png',
|
|
145
|
+
* transparent: true
|
|
146
|
+
* }).addTo(map);
|
|
147
|
+
*
|
|
148
|
+
* new TimeControl({
|
|
149
|
+
* interval: '2013-05-20T18:00Z/2013-05-20T22:00Z',
|
|
150
|
+
* period: 'PT5M',
|
|
151
|
+
* layers: [radar],
|
|
152
|
+
* loop: true
|
|
153
|
+
* }).addTo(map);
|
|
154
|
+
*
|
|
155
|
+
* map.on('timechange', e => console.log(e.time));
|
|
156
|
+
* ```
|
|
157
|
+
*
|
|
158
|
+
* @fires MaplairMap#timechange {TimeChangeEvent} Fired when the current time of a
|
|
159
|
+
* `TimeControl` changes.
|
|
160
|
+
*/
|
|
161
|
+
export class TimeControl extends Control {
|
|
162
|
+
|
|
163
|
+
static defaultOptions = {
|
|
164
|
+
position: 'bottomleft',
|
|
165
|
+
times: undefined,
|
|
166
|
+
interval: undefined,
|
|
167
|
+
period: 'P1D',
|
|
168
|
+
time: undefined,
|
|
169
|
+
layers: [],
|
|
170
|
+
transitionTime: 1000,
|
|
171
|
+
loop: false,
|
|
172
|
+
formatTime: undefined
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Creates a time control.
|
|
177
|
+
* @param {...TimeControlOptions} args The options.
|
|
178
|
+
*/
|
|
179
|
+
constructor(...args) {
|
|
180
|
+
super(...args);
|
|
181
|
+
const {times, interval, period, time, layers} = this.options;
|
|
182
|
+
this._layers = [];
|
|
183
|
+
this._playing = false;
|
|
184
|
+
this._times = [];
|
|
185
|
+
this._index = -1;
|
|
186
|
+
this.setTimes(interval ? intervalTimes(interval, period) : times ?? [], time);
|
|
187
|
+
for (const layer of layers) {
|
|
188
|
+
this.addLayer(layer);
|
|
189
|
+
}
|
|
190
|
+
this.callInitHooks(TimeControl);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @internal
|
|
195
|
+
* @param {MaplairMap} map
|
|
196
|
+
* @returns {HTMLElement}
|
|
197
|
+
*/
|
|
198
|
+
onAdd(map) {
|
|
199
|
+
const container = DomUtil.create('div', 'maplair-bar maplair-control-time');
|
|
200
|
+
DomEvent.disableClickPropagation(container);
|
|
201
|
+
DomEvent.disableScrollPropagation(container);
|
|
202
|
+
|
|
203
|
+
this._previousButton = new ButtonControl({
|
|
204
|
+
className: 'maplair-control-time-previous',
|
|
205
|
+
content: previousIcon,
|
|
206
|
+
title: I18n.translate('Step back'),
|
|
207
|
+
onClick: () => this.previous()
|
|
208
|
+
});
|
|
209
|
+
this._playButton = new ButtonControl({
|
|
210
|
+
className: 'maplair-control-time-play',
|
|
211
|
+
states: [
|
|
212
|
+
{name: 'play', content: playIcon, title: I18n.translate('Play'), onClick: () => this.play()},
|
|
213
|
+
{name: 'pause', content: pauseIcon, title: I18n.translate('Pause'), onClick: () => this.pause()}
|
|
214
|
+
]
|
|
215
|
+
});
|
|
216
|
+
this._nextButton = new ButtonControl({
|
|
217
|
+
className: 'maplair-control-time-next',
|
|
218
|
+
content: nextIcon,
|
|
219
|
+
title: I18n.translate('Step forward'),
|
|
220
|
+
onClick: () => this.next()
|
|
221
|
+
});
|
|
222
|
+
for (const button of [this._previousButton, this._playButton, this._nextButton]) {
|
|
223
|
+
button._map = map;
|
|
224
|
+
button._createButton(container);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const slider = this._slider = DomUtil.create('input', 'maplair-control-time-slider', container);
|
|
228
|
+
slider.type = 'range';
|
|
229
|
+
slider.min = '0';
|
|
230
|
+
slider.step = '1';
|
|
231
|
+
slider.setAttribute('aria-label', I18n.translate('Time'));
|
|
232
|
+
DomEvent.on(slider, 'input', this._onSliderInput, this);
|
|
233
|
+
|
|
234
|
+
this._label = DomUtil.create('span', 'maplair-control-time-label', container);
|
|
235
|
+
|
|
236
|
+
this._formatter = new Intl.DateTimeFormat(I18n.getLocale() ?? undefined, {dateStyle: 'short', timeStyle: 'short'});
|
|
237
|
+
this._updateControl();
|
|
238
|
+
return container;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @internal
|
|
243
|
+
*/
|
|
244
|
+
onRemove() {
|
|
245
|
+
this.pause();
|
|
246
|
+
this._slider = null;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
_onSliderInput() {
|
|
250
|
+
this.pause();
|
|
251
|
+
this._setIndex(Number(this._slider.value));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
_updateControl() {
|
|
255
|
+
if (!this._slider) { return; }
|
|
256
|
+
const index = this._index,
|
|
257
|
+
last = this._times.length - 1,
|
|
258
|
+
time = this.getTime(),
|
|
259
|
+
text = time ? (this.options.formatTime ?? this._formatter.format)(time) : '',
|
|
260
|
+
loop = this.options.loop;
|
|
261
|
+
|
|
262
|
+
this._slider.max = String(Math.max(last, 0));
|
|
263
|
+
this._slider.value = String(Math.max(index, 0));
|
|
264
|
+
this._slider.disabled = last < 1;
|
|
265
|
+
this._slider.setAttribute('aria-valuetext', text);
|
|
266
|
+
this._label.textContent = text;
|
|
267
|
+
|
|
268
|
+
this._playButton.setState(this._playing ? 'pause' : 'play');
|
|
269
|
+
this._setDisabled(this._playButton, last < 1);
|
|
270
|
+
this._setDisabled(this._previousButton, last < 1 || (index === 0 && !loop));
|
|
271
|
+
this._setDisabled(this._nextButton, last < 1 || (index === last && !loop));
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
_setDisabled(button, disabled) {
|
|
275
|
+
if (disabled) {
|
|
276
|
+
button.disable();
|
|
277
|
+
} else {
|
|
278
|
+
button.enable();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Sets the times to step through, see the [`times` option](#timecontrol-times), and
|
|
284
|
+
* shows the nearest of them to `time`, by default to the current time of the control.
|
|
285
|
+
* @param {(Date|number|string)[]} times
|
|
286
|
+
* @param {Date|number|string} [time]
|
|
287
|
+
* @returns {this}
|
|
288
|
+
*/
|
|
289
|
+
setTimes(times, time = this.getTime()) {
|
|
290
|
+
this._times = times.map(t => new Date(t));
|
|
291
|
+
this._index = -1;
|
|
292
|
+
if (this._times.length < 2) {
|
|
293
|
+
this.pause();
|
|
294
|
+
}
|
|
295
|
+
return this.setTime(time ?? Date.now());
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Returns the times to step through.
|
|
300
|
+
* @returns {Date[]}
|
|
301
|
+
*/
|
|
302
|
+
getTimes() {
|
|
303
|
+
return this._times;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Shows the nearest of the times to the given time.
|
|
308
|
+
* @param {Date|number|string} time
|
|
309
|
+
* @returns {this}
|
|
310
|
+
*/
|
|
311
|
+
setTime(time) {
|
|
312
|
+
const ms = +new Date(time);
|
|
313
|
+
let index = this._times.length ? 0 : -1;
|
|
314
|
+
this._times.forEach((t, i) => {
|
|
315
|
+
if (Math.abs(+t - ms) < Math.abs(+this._times[index] - ms)) {
|
|
316
|
+
index = i;
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
return this._setIndex(index);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Returns the current time.
|
|
324
|
+
* @returns {Date|undefined}
|
|
325
|
+
*/
|
|
326
|
+
getTime() {
|
|
327
|
+
return this._times[this._index];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
_setIndex(index) {
|
|
331
|
+
if (index !== this._index) {
|
|
332
|
+
this._index = index;
|
|
333
|
+
for (const layer of this._layers) {
|
|
334
|
+
this._updateLayer(layer);
|
|
335
|
+
}
|
|
336
|
+
if (index >= 0) {
|
|
337
|
+
this._map?.fire('timechange', {time: this.getTime(), index});
|
|
338
|
+
}
|
|
339
|
+
if (this._playing) {
|
|
340
|
+
this._schedule();
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
this._updateControl();
|
|
344
|
+
return this;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Shows the next time (or the first one after the last one if looping).
|
|
349
|
+
* @returns {this}
|
|
350
|
+
*/
|
|
351
|
+
next() {
|
|
352
|
+
if (this._index < this._times.length - 1) {
|
|
353
|
+
this._setIndex(this._index + 1);
|
|
354
|
+
} else if (this.options.loop && this._times.length > 1) {
|
|
355
|
+
this._setIndex(0);
|
|
356
|
+
}
|
|
357
|
+
return this;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Shows the previous time (or the last one before the first one if looping).
|
|
362
|
+
* @returns {this}
|
|
363
|
+
*/
|
|
364
|
+
previous() {
|
|
365
|
+
if (this._index > 0) {
|
|
366
|
+
this._setIndex(this._index - 1);
|
|
367
|
+
} else if (this.options.loop && this._times.length > 1) {
|
|
368
|
+
this._setIndex(this._times.length - 1);
|
|
369
|
+
}
|
|
370
|
+
return this;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Plays the times one after the other, each for at least the
|
|
375
|
+
* [`transitionTime`](#timecontrol-transitiontime) and until the layers have loaded.
|
|
376
|
+
* Starts at the first time if the last one is shown. Stops at the last time unless
|
|
377
|
+
* [looping](#timecontrol-loop).
|
|
378
|
+
* @returns {this}
|
|
379
|
+
*/
|
|
380
|
+
play() {
|
|
381
|
+
if (this._playing || this._times.length < 2) { return this; }
|
|
382
|
+
this._playing = true;
|
|
383
|
+
if (this._index === this._times.length - 1) {
|
|
384
|
+
// also schedules the next time
|
|
385
|
+
this._setIndex(0);
|
|
386
|
+
} else {
|
|
387
|
+
this._schedule();
|
|
388
|
+
this._updateControl();
|
|
389
|
+
}
|
|
390
|
+
return this;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Stops playing.
|
|
395
|
+
* @returns {this}
|
|
396
|
+
*/
|
|
397
|
+
pause() {
|
|
398
|
+
clearTimeout(this._timeout);
|
|
399
|
+
this._timeout = null;
|
|
400
|
+
if (this._playing) {
|
|
401
|
+
this._playing = false;
|
|
402
|
+
this._updateControl();
|
|
403
|
+
}
|
|
404
|
+
return this;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Returns `true` while playing.
|
|
409
|
+
* @returns {boolean}
|
|
410
|
+
*/
|
|
411
|
+
isPlaying() {
|
|
412
|
+
return this._playing;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Shows the next time after the transition time, once the layers have loaded
|
|
416
|
+
_schedule() {
|
|
417
|
+
clearTimeout(this._timeout);
|
|
418
|
+
this._timeout = setTimeout(() => {
|
|
419
|
+
this._timeout = null;
|
|
420
|
+
this._advance();
|
|
421
|
+
}, this.options.transitionTime);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
_advance() {
|
|
425
|
+
if (!this._playing || this._timeout || this._layers.some(layer => layer._map && layer.isLoading?.())) { return; }
|
|
426
|
+
if (this._index < this._times.length - 1 || this.options.loop) {
|
|
427
|
+
this.next();
|
|
428
|
+
} else {
|
|
429
|
+
this.pause();
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
_updateLayer(layer) {
|
|
434
|
+
const time = this.getTime();
|
|
435
|
+
if (!time) { return; }
|
|
436
|
+
if (typeof layer.setTime === 'function') {
|
|
437
|
+
layer.setTime(time);
|
|
438
|
+
} else if (typeof layer.setParams === 'function') {
|
|
439
|
+
layer.setParams({time: time.toISOString()});
|
|
440
|
+
} else if (typeof layer.setUrl === 'function') {
|
|
441
|
+
layer.options.time = time.toISOString();
|
|
442
|
+
layer.setUrl(layer._url, false);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Adds a layer showing the current time, see the [`layers` option](#timecontrol-layers).
|
|
448
|
+
* The layer still has to be added to the map.
|
|
449
|
+
* @param {Layer & TimeLayer} layer
|
|
450
|
+
* @returns {this}
|
|
451
|
+
*/
|
|
452
|
+
addLayer(layer) {
|
|
453
|
+
if (this._layers.includes(layer)) { return this; }
|
|
454
|
+
this._layers.push(layer);
|
|
455
|
+
layer.on('load remove', this._advance, this);
|
|
456
|
+
this._updateLayer(layer);
|
|
457
|
+
return this;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Removes a layer from the layers showing the current time.
|
|
462
|
+
* @param {Layer} layer
|
|
463
|
+
* @returns {this}
|
|
464
|
+
*/
|
|
465
|
+
removeLayer(layer) {
|
|
466
|
+
this._layers = this._layers.filter(l => l !== layer);
|
|
467
|
+
layer.off('load remove', this._advance, this);
|
|
468
|
+
this._advance();
|
|
469
|
+
return this;
|
|
470
|
+
}
|
|
471
|
+
}
|