mobility-toolbox-js 3.0.0-beta.8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/README.md +11 -3
  2. package/api/HttpAPI.d.ts +20 -0
  3. package/api/HttpAPI.js +1 -14
  4. package/api/RealtimeAPI.d.ts +404 -0
  5. package/api/RealtimeAPI.js +265 -206
  6. package/api/RoutingAPI.d.ts +47 -0
  7. package/api/RoutingAPI.js +17 -7
  8. package/api/StopsAPI.d.ts +44 -0
  9. package/api/StopsAPI.js +17 -11
  10. package/api/WebSocketAPI.d.ts +147 -0
  11. package/api/WebSocketAPI.js +164 -165
  12. package/api/index.d.ts +3 -0
  13. package/api/index.js +1 -1
  14. package/api/typedefs.d.ts +76 -0
  15. package/api/typedefs.js +27 -42
  16. package/common/controls/StopFinderControlCommon.d.ts +53 -0
  17. package/common/controls/StopFinderControlCommon.js +32 -32
  18. package/common/index.d.ts +2 -0
  19. package/common/index.js +1 -1
  20. package/common/styles/index.d.ts +4 -0
  21. package/common/styles/realtimeDefaultStyle.d.ts +36 -0
  22. package/common/styles/realtimeDefaultStyle.js +6 -11
  23. package/common/styles/realtimeDelayStyle.d.ts +12 -0
  24. package/common/styles/realtimeHeadingStyle.d.ts +12 -0
  25. package/common/styles/realtimeHeadingStyle.js +5 -10
  26. package/common/styles/realtimeSimpleStyle.d.ts +3 -0
  27. package/common/styles/realtimeSimpleStyle.js +0 -1
  28. package/common/typedefs.d.ts +102 -0
  29. package/common/typedefs.js +6 -31
  30. package/common/utils/RealtimeEngine.d.ts +214 -0
  31. package/common/utils/RealtimeEngine.js +554 -0
  32. package/common/utils/compareDepartures.d.ts +10 -0
  33. package/common/utils/compareDepartures.js +2 -2
  34. package/common/utils/constants.d.ts +5 -0
  35. package/common/utils/createCanvas.d.ts +10 -0
  36. package/common/utils/createDefaultCopyrightElt.d.ts +5 -0
  37. package/common/utils/createDefaultStopFinderElt.d.ts +5 -0
  38. package/common/utils/createRealtimeFilters.d.ts +12 -0
  39. package/common/utils/debounceDeparturesMessages.d.ts +12 -0
  40. package/common/utils/debounceWebsocketMessages.d.ts +11 -0
  41. package/common/utils/getLayersAsFlatArray.d.ts +2 -0
  42. package/common/utils/getLayersAsFlatArray.js +0 -1
  43. package/common/utils/getMapGlCopyrights.d.ts +17 -0
  44. package/common/utils/getMapGlCopyrights.js +3 -3
  45. package/common/utils/getRealtimeModeSuffix.d.ts +10 -0
  46. package/common/utils/getRealtimeModeSuffix.js +1 -0
  47. package/common/utils/getUrlWithParams.d.ts +8 -0
  48. package/common/utils/getVehiclePosition.d.ts +17 -0
  49. package/common/utils/getVehiclePosition.js +6 -3
  50. package/common/utils/index.d.ts +16 -0
  51. package/common/utils/realtimeConfig.d.ts +64 -0
  52. package/common/utils/realtimeConfig.js +0 -1
  53. package/common/utils/removeDuplicate.d.ts +9 -0
  54. package/common/utils/renderTrajectories.d.ts +17 -0
  55. package/common/utils/renderTrajectories.js +7 -6
  56. package/common/utils/sortAndFilterDepartures.d.ts +16 -0
  57. package/common/utils/sortAndFilterDepartures.js +2 -1
  58. package/common/utils/sortByDelay.d.ts +3 -0
  59. package/common/utils/sortByDelay.js +5 -1
  60. package/common/utils/timeUtils.d.ts +23 -0
  61. package/common/utils/toMercatorExtent.d.ts +5 -0
  62. package/iife.d.ts +2 -0
  63. package/index.d.ts +9 -0
  64. package/maplibre/controls/CopyrightControl.d.ts +38 -0
  65. package/maplibre/controls/CopyrightControl.js +11 -8
  66. package/maplibre/controls/index.d.ts +1 -0
  67. package/maplibre/index.d.ts +5 -0
  68. package/maplibre/layers/Layer.d.ts +29 -0
  69. package/maplibre/layers/Layer.js +2 -3
  70. package/maplibre/layers/RealtimeLayer.d.ts +124 -0
  71. package/maplibre/layers/RealtimeLayer.js +154 -118
  72. package/maplibre/layers/index.d.ts +2 -0
  73. package/maplibre/utils/getMercatorResolution.d.ts +7 -0
  74. package/maplibre/utils/getSourceCoordinates.d.ts +8 -0
  75. package/maplibre/utils/getSourceCoordinates.js +6 -5
  76. package/maplibre/utils/index.d.ts +2 -0
  77. package/mbt.js +14585 -14557
  78. package/mbt.js.map +4 -4
  79. package/mbt.min.js +75 -75
  80. package/mbt.min.js.map +4 -4
  81. package/ol/controls/CopyrightControl.d.ts +39 -0
  82. package/ol/controls/CopyrightControl.js +13 -5
  83. package/ol/controls/RoutingControl.d.ts +213 -0
  84. package/ol/controls/RoutingControl.js +250 -264
  85. package/ol/controls/StopFinderControl.d.ts +56 -0
  86. package/ol/controls/StopFinderControl.js +24 -5
  87. package/ol/controls/index.d.ts +3 -0
  88. package/ol/index.d.ts +6 -0
  89. package/ol/layers/Layer.d.ts +26 -0
  90. package/ol/layers/Layer.js +39 -0
  91. package/ol/layers/MaplibreLayer.d.ts +91 -0
  92. package/ol/layers/MaplibreLayer.js +154 -31
  93. package/ol/layers/MaplibreStyleLayer.d.ts +157 -0
  94. package/ol/layers/MaplibreStyleLayer.js +281 -210
  95. package/ol/layers/RealtimeLayer.d.ts +150 -0
  96. package/ol/layers/RealtimeLayer.js +210 -206
  97. package/ol/layers/VectorLayer.d.ts +17 -0
  98. package/ol/layers/VectorLayer.js +33 -0
  99. package/ol/layers/index.d.ts +5 -0
  100. package/ol/layers/index.js +3 -0
  101. package/ol/renderers/MaplibreLayerRenderer.d.ts +0 -0
  102. package/ol/renderers/MaplibreLayerRenderer.js +142 -114
  103. package/ol/renderers/MaplibreStyleLayerRenderer.d.ts +20 -0
  104. package/ol/renderers/MaplibreStyleLayerRenderer.js +20 -23
  105. package/ol/renderers/RealtimeLayerRenderer.d.ts +22 -0
  106. package/ol/renderers/RealtimeLayerRenderer.js +50 -70
  107. package/ol/styles/fullTrajectoryDelayStyle.d.ts +6 -0
  108. package/ol/styles/fullTrajectoryDelayStyle.js +5 -7
  109. package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
  110. package/ol/styles/fullTrajectoryStyle.js +5 -7
  111. package/ol/styles/index.d.ts +3 -0
  112. package/ol/styles/routingStyle.d.ts +3 -0
  113. package/ol/styles/routingStyle.js +13 -10
  114. package/ol/utils/defineDeprecatedProperties.d.ts +10 -0
  115. package/ol/utils/defineDeprecatedProperties.js +180 -0
  116. package/ol/utils/getFeatureInfoAtCoordinate.d.ts +8 -0
  117. package/ol/utils/getFeatureInfoAtCoordinate.js +11 -17
  118. package/ol/utils/index.d.ts +1 -0
  119. package/package.json +44 -44
  120. package/setupTests.d.ts +1 -0
  121. package/setupTests.js +17 -4
  122. package/types/common.d.ts +53 -69
  123. package/types/index.d.ts +1 -1
  124. package/types/realtime.d.ts +98 -95
  125. package/types/routing.d.ts +60 -60
  126. package/types/stops.d.ts +62 -62
  127. package/common/mixins/RealtimeLayerMixin.js +0 -743
  128. package/ol/layers/MapGlLayer.js +0 -142
  129. package/ol/mixins/MobilityLayerMixin.js +0 -9
  130. package/ol/mixins/PropertiesLayerMixin.js +0 -142
@@ -1,27 +1,41 @@
1
+ import debounce from 'lodash.debounce';
1
2
  import { Layer } from 'ol/layer';
3
+ import { unByKey } from 'ol/Observable';
2
4
  import { Source } from 'ol/source';
3
- import MobilityLayerMixin from '../mixins/MobilityLayerMixin';
4
- import MaplibreStyleLayerRenderer from '../renderers/MaplibreStyleLayerRenderer';
5
5
  import { VECTOR_TILE_FEATURE_PROPERTY } from '../../common';
6
+ import MaplibreStyleLayerRenderer from '../renderers/MaplibreStyleLayerRenderer';
7
+ import defineDeprecatedProperties from '../utils/defineDeprecatedProperties';
8
+ let deprecated = () => { };
9
+ if (typeof window !== 'undefined' &&
10
+ new URLSearchParams(window.location.search).get('deprecated')) {
11
+ deprecated = debounce((...messages) => {
12
+ // eslint-disable-next-line no-console
13
+ console.warn(...messages);
14
+ }, 1000);
15
+ }
6
16
  /**
7
- * Layer for visualizing a specific set of layer from a MapboxLayer.
17
+ * Layer that helps show/hide a specific subset of style layers of a [MaplibreLayer](./MaplibreLayer.js~MaplibreLayer.html).
8
18
  *
9
19
  * @example
10
- * import { MapboxLayer, MapboxStyleLayer } from 'mobility-toolbox-js/ol';
20
+ * import { MaplibreLayer, MaplibreStyleLayer } from 'mobility-toolbox-js/ol';
11
21
  *
12
- * const maplibreLayer = new MapboxLayer({
13
- * url: 'https://maps.geops.io/styles/travic_v2/style.json?key=[yourApiKey]',
22
+ * const maplibreLayer = new MaplibreLayer({
23
+ * apiKey: 'yourApiKey',
14
24
  * });
15
25
  *
16
- * const layer = new MapboxStyleLayer({
26
+ * const layer = new MaplibreStyleLayer({
17
27
  * maplibreLayer: maplibreLayer,
18
- * styleLayersFilter: () => {},
28
+ * layersFilter: (layer) => {
29
+ * // show/hide only style layers related to stations
30
+ * return /station/.test(layer.id);
31
+ * },
19
32
  * });
20
33
  *
21
- * @classproperty {ol/Map~Map} map - The map where the layer is displayed.
34
+ * @see <a href="/example/ol-maplibre-style-layer">OpenLayers MaplibreStyle layer example</a>
22
35
  * @extends {ol/layer/Layer~Layer}
36
+ * @public
23
37
  */
24
- class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
38
+ class MaplibreStyleLayer extends Layer {
25
39
  get beforeId() {
26
40
  return this.get('beforeId');
27
41
  }
@@ -40,9 +54,11 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
40
54
  set layersFilter(newValue) {
41
55
  this.set('layersFilter', newValue);
42
56
  }
57
+ /**
58
+ * @deprecated Use MaplibreStyleLayer.maplibreLayer instead.
59
+ */
43
60
  get mapboxLayer() {
44
- // eslint-disable-next-line no-console
45
- console.warn('Deprecated. Use maplibreLayer instead.');
61
+ deprecated('Deprecated. Use maplibreLayer instead.');
46
62
  return this.get('maplibreLayer');
47
63
  }
48
64
  get maplibreLayer() {
@@ -64,54 +80,82 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
64
80
  this.set('sources', newValue);
65
81
  }
66
82
  /**
67
- * @deprecated
83
+ * @deprecated Use MaplibreStyleLayer.layer instead.
68
84
  */
69
85
  get styleLayer() {
70
- // eslint-disable-next-line no-console
71
- console.warn('Deprecated. Use layers instead.');
86
+ deprecated('Deprecated. Use MaplibreStyleLayer.layer instead.');
72
87
  return this.layers[0];
73
88
  }
74
89
  /**
75
90
  * @deprecated
76
91
  */
77
92
  set styleLayer(newValue) {
78
- // eslint-disable-next-line no-console
79
- console.warn('Deprecated. Use layers instead.');
93
+ deprecated('MaplibreStyleLayer.styleLayer is deprecated. Use MaplibreStyleLayer.layer instead.');
80
94
  this.layers = [newValue];
81
95
  }
96
+ /**
97
+ * Apply visibility to style layers that fits the styleLayersFilter function.
98
+ */
82
99
  /**
83
100
  * @deprecated
84
101
  */
85
102
  get styleLayers() {
86
- // eslint-disable-next-line no-console
87
- console.warn('Deprecated. Use layers instead.');
103
+ deprecated('MaplibreStyleLayer.styleLayers is deprecated. Use MaplibreStyleLayer.layers instead.');
88
104
  return this.layers;
89
105
  }
90
106
  /**
91
107
  * @deprecated
92
108
  */
93
109
  set styleLayers(newValue) {
94
- // eslint-disable-next-line no-console
95
- console.warn('Deprecated. Use layers instead.');
110
+ deprecated('MaplibreStyleLayer.styleLayers is deprecated. Use MaplibreStyleLayer.layers instead.');
96
111
  this.layers = newValue;
97
112
  }
98
113
  /**
99
114
  * Constructor.
100
115
  *
101
116
  * @param {Object} options
102
- * @param {MapboxLayer} [options.maplibreLayer] The MaplibreLayer to use.
103
- * @param {maplibregl.SourceSpecification[]} [options.sources] The source to add to the style on load.
117
+ * @param {string} [options.beforeId] The style layer id to use when the options.layers property is defined, unsused otherwise.
104
118
  * @param {maplibregl.AddLayerObject[]} [options.layers] The layers to add to the style on load.
105
119
  * @param {FilterFunction} [options.layersFilter] Filter function to decide which style layer to apply visiblity on. If not provided, the 'layers' property is used.
120
+ * @param {MaplibreLayer} [options.maplibreLayer] The MaplibreLayer to use.
106
121
  * @param {FilterFunction} [options.queryRenderedLayersFilter] Filter function to decide which style layer are available for query.
122
+ * @param {{[id: string]:maplibregl.SourceSpecification}} [options.sources] The sources to add to the style on load.
123
+ * @public
107
124
  */
108
- constructor(options) {
109
- super(Object.assign({ source: new Source({}) }, (options || {})));
125
+ constructor(options = {
126
+ mapLibreOptions: { style: { layers: [], sources: {}, version: 8 } },
127
+ }) {
128
+ /** Manage renamed property for backward compatibility with v2 */
129
+ if (options.mapboxLayer) {
130
+ deprecated('options.mapboxLayer is deprecated. Use options.maplibreLayer instead.');
131
+ // eslint-disable-next-line no-param-reassign
132
+ options.maplibreLayer = options.mapboxLayer;
133
+ // eslint-disable-next-line no-param-reassign
134
+ delete options.mapboxLayer;
135
+ }
136
+ if (options.styleLayers) {
137
+ deprecated('options.styleLayers is deprecated. Use options.layers instead.');
138
+ // eslint-disable-next-line no-param-reassign
139
+ options.layers = options.styleLayers;
140
+ // eslint-disable-next-line no-param-reassign
141
+ delete options.styleLayers;
142
+ }
143
+ if (options.styleLayersFilter) {
144
+ deprecated('options.styleLayersFilter is deprecated. Use options.layersFilter instead.');
145
+ // eslint-disable-next-line no-param-reassign
146
+ options.layersFilter = options.styleLayersFilter;
147
+ // eslint-disable-next-line no-param-reassign
148
+ delete options.styleLayersFilter;
149
+ }
150
+ super(Object.assign({ source: new Source({}) }, options));
110
151
  this.highlightedFeatures = [];
152
+ this.olEventsKeys = [];
111
153
  this.selectedFeatures = [];
112
- /**
113
- * @private
114
- */
154
+ // For backward compatibility with v2
155
+ defineDeprecatedProperties(this, options);
156
+ // For cloning
157
+ this.set('options', options);
158
+ this.beforeId = options.beforeId;
115
159
  this.onLoad = this.onLoad.bind(this);
116
160
  if (!this.layersFilter && this.layers) {
117
161
  this.layersFilter = (layer) => {
@@ -119,8 +163,62 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
119
163
  };
120
164
  }
121
165
  }
122
- createRenderer() {
123
- return new MaplibreStyleLayerRenderer(this);
166
+ addLayers() {
167
+ var _a;
168
+ if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !Array.isArray(this.layers)) {
169
+ return;
170
+ }
171
+ const { mapLibreMap } = this.maplibreLayer;
172
+ if (mapLibreMap) {
173
+ this.layers.forEach((layer) => {
174
+ // @ts-expect-error source is optional but exists in TS definition
175
+ const { id, source } = layer;
176
+ if ((!source || (source && mapLibreMap.getSource(source))) &&
177
+ id &&
178
+ !mapLibreMap.getLayer(id)) {
179
+ mapLibreMap.addLayer(layer, this.beforeId);
180
+ }
181
+ });
182
+ this.applyLayoutVisibility();
183
+ }
184
+ }
185
+ addSources() {
186
+ var _a;
187
+ if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !this.sources) {
188
+ return;
189
+ }
190
+ const { mapLibreMap } = this.maplibreLayer;
191
+ if (mapLibreMap) {
192
+ Object.entries(this.sources).forEach(([id, source]) => {
193
+ if (!mapLibreMap.getSource(id)) {
194
+ mapLibreMap.addSource(id, source);
195
+ }
196
+ });
197
+ }
198
+ }
199
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
200
+ applyLayoutVisibility(evt) {
201
+ var _a, _b;
202
+ if (!((_b = (_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) === null || _b === void 0 ? void 0 : _b.getStyle()) || !this.layersFilter) {
203
+ return;
204
+ }
205
+ const { mapLibreMap } = this.maplibreLayer;
206
+ const style = mapLibreMap.getStyle();
207
+ const visibilityValue = this.getVisible() ? 'visible' : 'none';
208
+ const layers = style.layers || [];
209
+ for (let i = 0; i < layers.length; i += 1) {
210
+ const layer = layers[i];
211
+ if (this.layersFilter(layer)) {
212
+ const { id } = layer;
213
+ if (mapLibreMap.getLayer(id)) {
214
+ mapLibreMap.setLayoutProperty(id, 'visibility', visibilityValue);
215
+ if (this.getMinZoom() || this.getMaxZoom()) {
216
+ mapLibreMap.setLayerZoomRange(id, this.getMinZoom() ? this.getMinZoom() - 1 : 0, // Maplibre zoom = ol zoom - 1
217
+ this.getMaxZoom() ? this.getMaxZoom() - 1 : 24);
218
+ }
219
+ }
220
+ }
221
+ }
124
222
  }
125
223
  /**
126
224
  * Initialize the layer.
@@ -128,38 +226,38 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
128
226
  * @override
129
227
  */
130
228
  attachToMap(map) {
131
- if (this.maplibreLayer && !this.maplibreLayer.map) {
229
+ if (this.maplibreLayer && !this.maplibreLayer.getMapInternal()) {
132
230
  map.addLayer(this.maplibreLayer);
133
231
  }
134
- super.attachToMap(map);
135
- if (!this.map || !this.maplibreLayer) {
232
+ const mapInternal = this.getMapInternal();
233
+ if (!mapInternal || !this.maplibreLayer) {
136
234
  return;
137
235
  }
138
- if (!this.map.getTargetElement()) {
139
- // If ther e is no target element the maplibreMap is not yet created, we
236
+ if (!mapInternal.getTargetElement()) {
237
+ // If ther e is no target element the mapLibreMap is not yet created, we
140
238
  // relaunch the initialisation when it's the case.
141
- this.olListenersKeys.push(this.map.on('change:target', () => {
239
+ this.olEventsKeys.push(mapInternal.on('change:target', () => {
142
240
  this.attachToMap(map);
143
241
  }));
144
242
  return;
145
243
  }
146
244
  // Apply the initial visibility if possible otherwise we wait for the load event of the layer
147
- const { maplibreMap } = this.maplibreLayer;
148
- if (maplibreMap) {
149
- // maplibreMap.loaded() and maplibreMap.isStyleLoaded() are reliable only on the first call of init.
245
+ const { mapLibreMap } = this.maplibreLayer;
246
+ if (mapLibreMap) {
247
+ // mapLibreMap.loaded() and mapLibreMap.isStyleLoaded() are reliable only on the first call of init.
150
248
  // On the next call (when a topic change for example), these functions returns false because
151
249
  // the style is being modified.
152
250
  // That's why we rely on a property instead for the next calls.
153
- if (maplibreMap.loaded()) {
251
+ if (mapLibreMap.loaded()) {
154
252
  this.onLoad();
155
253
  }
156
254
  else {
157
- maplibreMap.once('load', this.onLoad);
255
+ mapLibreMap.once('load', this.onLoad);
158
256
  }
159
257
  }
160
258
  // Apply the visibiltity when layer's visibility change.
161
- this.olListenersKeys.push(
162
- // @ts-expect-error 'load' is a custom event form mobility-toolbox-js
259
+ this.olEventsKeys.push(
260
+ // @ts-expect-error 'load' is a custom event
163
261
  this.maplibreLayer.on('load', this.onLoad.bind(this)), this.on('change:visible', (evt) => {
164
262
  // Once the map is loaded we can apply visiblity without waiting
165
263
  // the style. Maplibre take care of the application of style changes.
@@ -169,136 +267,77 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
169
267
  this.detachFromMap();
170
268
  this.attachToMap(map);
171
269
  }
270
+ }),
271
+ // When the style changes we wait that it is loaded to relaunch the onLoad
272
+ this.maplibreLayer.on('propertychange', (evt) => {
273
+ if (evt.key === 'style') {
274
+ evt.target.maplibreMap.once('styledata', () => {
275
+ evt.target.maplibreMap.once('idle', () => {
276
+ this.onLoad();
277
+ });
278
+ });
279
+ }
172
280
  }));
173
281
  }
282
+ /**
283
+ * Create a copy of the MaplibreStyleLayer.
284
+ *
285
+ * @param {Object} newOptions Options to override. See constructor.
286
+ * @return {MapboxStyleLayer} A MaplibreStyleLayer.
287
+ * @public
288
+ */
289
+ clone(newOptions) {
290
+ return new MaplibreStyleLayer(Object.assign(Object.assign({}, this.get('options')), newOptions));
291
+ }
292
+ createRenderer() {
293
+ return new MaplibreStyleLayerRenderer(this);
294
+ }
174
295
  /**
175
296
  * Terminate the layer.
176
297
  * @override
177
298
  */
178
299
  detachFromMap() {
179
300
  var _a;
180
- if ((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) {
181
- this.maplibreLayer.maplibreMap.off('load', this.onLoad);
301
+ unByKey(this.olEventsKeys);
302
+ if ((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) {
303
+ this.maplibreLayer.mapLibreMap.off('load', this.onLoad);
182
304
  this.removeLayers();
183
305
  this.removeSources();
184
306
  }
185
- super.detachFromMap();
186
- }
187
- /** @private */
188
- addSources() {
189
- var _a;
190
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) || !this.sources) {
191
- return;
192
- }
193
- const { maplibreMap } = this.maplibreLayer;
194
- if (maplibreMap) {
195
- Object.entries(this.sources).forEach(([id, source]) => {
196
- if (!maplibreMap.getSource(id)) {
197
- maplibreMap.addSource(id, source);
198
- }
199
- });
200
- }
201
- }
202
- /** @private */
203
- removeSources() {
204
- var _a;
205
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) || !this.sources) {
206
- return;
207
- }
208
- const { maplibreMap } = this.maplibreLayer;
209
- if (maplibreMap) {
210
- Object.keys(this.sources).forEach((id) => {
211
- if (maplibreMap.getSource(id)) {
212
- maplibreMap.removeSource(id);
213
- }
214
- });
215
- }
216
- }
217
- /** @private */
218
- addLayers() {
219
- var _a;
220
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) || !Array.isArray(this.layers)) {
221
- return;
222
- }
223
- const { maplibreMap } = this.maplibreLayer;
224
- if (maplibreMap) {
225
- this.layers.forEach((layer) => {
226
- // @ts-expect-error source is optional but exists in TS definition
227
- const { id, source } = layer;
228
- if ((!source || (source && maplibreMap.getSource(source))) &&
229
- id &&
230
- !maplibreMap.getLayer(id)) {
231
- maplibreMap.addLayer(layer, this.beforeId);
232
- }
233
- });
234
- this.applyLayoutVisibility();
235
- }
236
- }
237
- /** @private */
238
- removeLayers() {
239
- var _a;
240
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) || !Array.isArray(this.layers)) {
241
- return;
242
- }
243
- const { maplibreMap } = this.maplibreLayer;
244
- if (maplibreMap) {
245
- this.layers.forEach((styleLayer) => {
246
- const { id } = styleLayer;
247
- if (id && maplibreMap.getLayer(id)) {
248
- maplibreMap.removeLayer(id);
249
- }
250
- });
251
- }
252
- }
253
- /**
254
- * On Maplibre map load callback function. Add style layers and dynaimc filters.
255
- * @private
256
- */
257
- onLoad() {
258
- var _a;
259
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap)) {
260
- return;
261
- }
262
- this.addSources();
263
- this.addLayers();
264
- const { maplibreMap } = this.maplibreLayer;
265
- const style = maplibreMap.getStyle();
266
- if ((style === null || style === void 0 ? void 0 : style.layers) && this.layersFilter) {
267
- const styles = style.layers.filter(this.layersFilter);
268
- this.disabled = !styles.length;
269
- }
270
307
  }
271
308
  /**
272
309
  * Request feature information for a given coordinate.
273
310
  * @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
274
311
  * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate.
312
+ * @deprecated Use getFeatureInfoAtCoordinate([layer], coordinate) from mobility-toolbox-ol package instead.
275
313
  */
276
314
  getFeatureInfoAtCoordinate(coordinate) {
277
315
  var _a;
278
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap)) {
316
+ deprecated(`Deprecated. getFeatureInfoAtCoordinate([layer], coordinate) from ol package instead.`);
317
+ if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap)) {
279
318
  return Promise.resolve({ coordinate, features: [], layer: this });
280
319
  }
281
- const { maplibreMap } = this.maplibreLayer;
320
+ const { mapLibreMap } = this.maplibreLayer;
282
321
  // Ignore the getFeatureInfo until the Maplibre map is loaded
283
- if (!maplibreMap.isStyleLoaded()) {
322
+ if (!mapLibreMap.isStyleLoaded()) {
284
323
  return Promise.resolve({ coordinate, features: [], layer: this });
285
324
  }
286
325
  // We query features only on style layers used by this layer.
287
326
  let layers = this.layers || [];
288
327
  if (this.layersFilter) {
289
- layers = maplibreMap.getStyle().layers.filter(this.layersFilter);
328
+ layers = mapLibreMap.getStyle().layers.filter(this.layersFilter);
329
+ console.log(layers);
290
330
  }
291
331
  if (this.queryRenderedLayersFilter) {
292
- // @ts-ignore
293
332
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
294
- layers = maplibreMap
333
+ layers = mapLibreMap
295
334
  .getStyle()
296
335
  .layers.filter(this.queryRenderedLayersFilter);
297
336
  }
298
337
  return Promise.resolve({
338
+ coordinate,
299
339
  features: [],
300
340
  layer: this,
301
- coordinate,
302
341
  });
303
342
  // this.maplibreLayer
304
343
  // .getFeatureInfoAtCoordinate(coordinate, {
@@ -308,7 +347,7 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
308
347
  // .then((featureInfo: LayerGetFeatureInfoResponse) => {
309
348
  // const features: Feature[] = featureInfo.features.filter(
310
349
  // (feature: Feature) => {
311
- // // @ts-ignore
350
+ // // @ts-expect-error
312
351
  // return this.featureInfoFilter(
313
352
  // feature,
314
353
  // this.map?.getView().getResolution(),
@@ -319,113 +358,145 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
319
358
  // return { ...featureInfo, features, layer: this };
320
359
  // });
321
360
  }
361
+ /**
362
+ * Highlight a list of features.
363
+ * @param {Array<ol/Feature~Feature>} [features=[]] Features to highlight.
364
+ * @deprecated Use layer.setFeatureState(features, {hover: true|false}) instead.
365
+ */
366
+ highlight(features = []) {
367
+ var _a;
368
+ deprecated(`Deprecated. Use layer.setFeatureState(features, {hover: true}) instead.`);
369
+ // Filter out selected features
370
+ const filtered = ((_a = this.highlightedFeatures) === null || _a === void 0 ? void 0 : _a.filter((feature) => !(this.selectedFeatures || [])
371
+ .map((feat) => feat.getId())
372
+ .includes(feature.getId()))) || [];
373
+ // Remove previous highlight
374
+ this.setHoverState(filtered, false);
375
+ this.highlightedFeatures = features;
376
+ // Add highlight
377
+ this.setHoverState(this.highlightedFeatures, true);
378
+ }
379
+ /**
380
+ * On Maplibre map load callback function. Add style layers and dynaimc filters.
381
+ */
382
+ onLoad() {
383
+ var _a;
384
+ if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap)) {
385
+ return;
386
+ }
387
+ this.addSources();
388
+ this.addLayers();
389
+ const { mapLibreMap } = this.maplibreLayer;
390
+ const style = mapLibreMap.getStyle();
391
+ if ((style === null || style === void 0 ? void 0 : style.layers) && this.layersFilter) {
392
+ const styles = style.layers.filter(this.layersFilter);
393
+ this.set('disabled', !styles.length);
394
+ }
395
+ this.applyLayoutVisibility();
396
+ }
397
+ removeLayers() {
398
+ var _a;
399
+ if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !Array.isArray(this.layers)) {
400
+ return;
401
+ }
402
+ const { mapLibreMap } = this.maplibreLayer;
403
+ if (mapLibreMap) {
404
+ this.layers.forEach((styleLayer) => {
405
+ const { id } = styleLayer;
406
+ if (id && mapLibreMap.getLayer(id)) {
407
+ mapLibreMap.removeLayer(id);
408
+ }
409
+ });
410
+ }
411
+ }
322
412
  // /**
323
413
  // * Set filter that determines which features should be rendered in a style layer.
324
414
  // * @param {maplibregl.filter} filter Determines which features should be rendered in a style layer.
325
415
  // */
326
416
  // setFilter(filter: { [key: string]: any }) {
327
- // if (!this.maplibreLayer?.maplibreMap) {
417
+ // if (!this.maplibreLayer?.mapLibreMap) {
328
418
  // return;
329
419
  // }
330
- // const { maplibreMap } = this.maplibreLayer;
420
+ // const { mapLibreMap } = this.maplibreLayer;
331
421
  // this.styleLayers.forEach(({ id }) => {
332
- // if (id && filter && maplibreMap.getLayer(id)) {
333
- // // @ts-ignore
334
- // maplibreMap.setFilter(id, filter);
422
+ // if (id && filter && mapLibreMap.getLayer(id)) {
423
+ // // @ts-expect-error
424
+ // mapLibreMap.setFilter(id, filter);
335
425
  // }
336
426
  // });
337
427
  // }
338
- /**
339
- * Set if features are hovered or not.
340
- * @param {Array<ol/Feature~Feature>} features
341
- * @param {boolean} state Is the feature hovered
342
- * @private
343
- */
344
- setHoverState(features, state) {
428
+ removeSources() {
345
429
  var _a;
346
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) || !features.length) {
430
+ if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !this.sources) {
347
431
  return;
348
432
  }
349
- const { maplibreMap } = this.maplibreLayer;
350
- features.forEach((feature) => {
351
- const { source, sourceLayer } = feature.get(VECTOR_TILE_FEATURE_PROPERTY) || {};
352
- if ((!source && !sourceLayer) || !feature.getId()) {
353
- if (!feature.getId()) {
354
- // eslint-disable-next-line no-console
355
- console.warn("No feature's id found. To use the feature state functionnality, tiles must be generated with --generate-ids. See https://github.com/Maplibre/tippecanoe#adding-calculated-attributes.", feature.getId(), feature.getProperties());
433
+ const { mapLibreMap } = this.maplibreLayer;
434
+ if (mapLibreMap) {
435
+ Object.keys(this.sources).forEach((id) => {
436
+ if (mapLibreMap.getSource(id)) {
437
+ mapLibreMap.removeSource(id);
356
438
  }
357
- return;
358
- }
359
- maplibreMap.setFeatureState({
360
- id: feature.getId(),
361
- source,
362
- sourceLayer,
363
- }, { hover: state });
364
- });
439
+ });
440
+ }
365
441
  }
366
442
  /**
367
443
  * Select a list of features.
368
444
  * @param {Array<ol/Feature~Feature>} [features=[]] Features to select.
369
- * @private
445
+ * @deprecated Use layer.setFeatureState(features, {selected: true|false}) instead.
370
446
  */
371
447
  select(features = []) {
448
+ deprecated(`Deprecated. Use layer.setFeatureState(features, {selected: true}) instead.`);
372
449
  this.setHoverState(this.selectedFeatures || [], false);
373
450
  this.selectedFeatures = features;
374
451
  this.setHoverState(this.selectedFeatures || [], true);
375
452
  }
376
453
  /**
377
- * Highlight a list of features.
378
- * @param {Array<ol/Feature~Feature>} [features=[]] Features to highlight.
379
- * @private
380
- */
381
- highlight(features = []) {
382
- var _a;
383
- // Filter out selected features
384
- const filtered = ((_a = this.highlightedFeatures) === null || _a === void 0 ? void 0 : _a.filter((feature) => !(this.selectedFeatures || [])
385
- .map((feat) => feat.getId())
386
- .includes(feature.getId()))) || [];
387
- // Remove previous highlight
388
- this.setHoverState(filtered, false);
389
- this.highlightedFeatures = features;
390
- // Add highlight
391
- this.setHoverState(this.highlightedFeatures, true);
392
- }
393
- /**
394
- * Apply visibility to style layers that fits the styleLayersFilter function.
454
+ * Set the [feature state](https://maplibre.org/maplibre-style-spec/expressions/#feature-state) of the features.
395
455
  *
396
- * @private
456
+ * @param {ol/Feature~Feature[]} features
457
+ * @param {{[key: string]: any}} state The feature state
458
+ * @public
397
459
  */
398
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
399
- applyLayoutVisibility(evt) {
400
- var _a, _b;
401
- if (!((_b = (_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) === null || _b === void 0 ? void 0 : _b.getStyle()) || !this.layersFilter) {
460
+ setFeatureState(features, state) {
461
+ var _a;
462
+ if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !features.length) {
402
463
  return;
403
464
  }
404
- const { maplibreMap } = this.maplibreLayer;
405
- const style = maplibreMap.getStyle();
406
- const visibilityValue = this.getVisible() ? 'visible' : 'none';
407
- const layers = style.layers || [];
408
- for (let i = 0; i < layers.length; i += 1) {
409
- const layer = layers[i];
410
- if (this.layersFilter(layer)) {
411
- const { id } = layer;
412
- if (maplibreMap.getLayer(id)) {
413
- maplibreMap.setLayoutProperty(id, 'visibility', visibilityValue);
414
- if (this.getMinZoom() || this.getMaxZoom()) {
415
- maplibreMap.setLayerZoomRange(id, this.getMinZoom() ? this.getMinZoom() - 1 : 0, // Maplibre zoom = ol zoom - 1
416
- this.getMaxZoom() ? this.getMaxZoom() - 1 : 24);
417
- }
465
+ const mapLibreMap = this.maplibreLayer.mapLibreMap;
466
+ features.forEach((feature) => {
467
+ const { source, sourceLayer } = feature.get(VECTOR_TILE_FEATURE_PROPERTY) || {};
468
+ if ((!source && !sourceLayer) || !feature.getId()) {
469
+ if (!feature.getId()) {
470
+ deprecated("No feature's id found. To use the feature state functionnality, tiles must be generated with --generate-ids. See https://github.com/Maplibre/tippecanoe#adding-calculated-attributes.", feature.getProperties());
418
471
  }
472
+ return;
419
473
  }
420
- }
474
+ mapLibreMap.setFeatureState({
475
+ id: feature.getId(),
476
+ source,
477
+ sourceLayer,
478
+ }, state);
479
+ });
421
480
  }
422
481
  /**
423
- * Create a copy of the MapboxStyleLayer.
424
- * @param {Object} newOptions Options to override.
425
- * @return {MapboxStyleLayer} A MapboxStyleLayer.
482
+ * Set if features are hovered or not.
483
+ * @param {Array<ol/Feature~Feature>} features
484
+ * @param {boolean} state Is the feature hovered
485
+ * @deprecated Use layer.setFeatureState(features, {hover: true|false}) instead.
426
486
  */
427
- clone(newOptions) {
428
- return new MaplibreStyleLayer(Object.assign(Object.assign({}, this.options), newOptions));
487
+ setHoverState(features, state) {
488
+ deprecated(`Deprecated. Use layer.setFeatureState(features, {hover: ${state}}) instead.`);
489
+ this.setFeatureState(features, { hover: state });
490
+ }
491
+ setMapInternal(map) {
492
+ if (map) {
493
+ super.setMapInternal(map);
494
+ this.attachToMap(map);
495
+ }
496
+ else {
497
+ this.detachFromMap();
498
+ super.setMapInternal(map);
499
+ }
429
500
  }
430
501
  }
431
502
  export default MaplibreStyleLayer;