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