mobility-toolbox-js 3.0.0-beta.2 → 3.0.0-beta.21

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 (125) hide show
  1. package/api/HttpAPI.d.ts +20 -0
  2. package/api/HttpAPI.js +0 -11
  3. package/api/RealtimeAPI.d.ts +404 -0
  4. package/api/RealtimeAPI.js +342 -276
  5. package/api/RoutingAPI.d.ts +47 -0
  6. package/api/RoutingAPI.js +17 -7
  7. package/api/StopsAPI.d.ts +44 -0
  8. package/api/StopsAPI.js +16 -10
  9. package/api/WebSocketAPI.d.ts +147 -0
  10. package/api/WebSocketAPI.js +164 -164
  11. package/api/index.d.ts +3 -0
  12. package/api/index.js +1 -1
  13. package/api/typedefs.d.ts +76 -0
  14. package/api/typedefs.js +27 -42
  15. package/common/controls/StopFinderControlCommon.d.ts +53 -0
  16. package/common/controls/StopFinderControlCommon.js +31 -31
  17. package/common/index.d.ts +2 -0
  18. package/common/index.js +1 -1
  19. package/common/mixins/RealtimeLayerMixin.d.ts +267 -0
  20. package/common/mixins/RealtimeLayerMixin.js +401 -393
  21. package/common/styles/index.d.ts +4 -0
  22. package/common/styles/realtimeDefaultStyle.d.ts +36 -0
  23. package/common/styles/realtimeDefaultStyle.js +6 -6
  24. package/common/styles/realtimeDelayStyle.d.ts +12 -0
  25. package/common/styles/realtimeHeadingStyle.d.ts +12 -0
  26. package/common/styles/realtimeHeadingStyle.js +5 -5
  27. package/common/styles/realtimeSimpleStyle.d.ts +4 -0
  28. package/common/typedefs.d.ts +219 -0
  29. package/common/typedefs.js +7 -1
  30. package/common/utils/compareDepartures.d.ts +10 -0
  31. package/common/utils/compareDepartures.js +2 -2
  32. package/common/utils/constants.d.ts +5 -0
  33. package/common/utils/createCanvas.d.ts +10 -0
  34. package/common/utils/createDefaultCopyrightElt.d.ts +5 -0
  35. package/common/utils/createDefaultStopFinderElt.d.ts +5 -0
  36. package/common/utils/createRealtimeFilters.d.ts +12 -0
  37. package/common/utils/debounceDeparturesMessages.d.ts +12 -0
  38. package/common/utils/debounceWebsocketMessages.d.ts +11 -0
  39. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  40. package/common/utils/getLayersAsFlatArray.js +5 -1
  41. package/common/utils/getMapGlCopyrights.d.ts +17 -0
  42. package/common/utils/getMapGlCopyrights.js +3 -3
  43. package/common/utils/getRealtimeModeSuffix.d.ts +10 -0
  44. package/common/utils/getRealtimeModeSuffix.js +1 -0
  45. package/common/utils/getUrlWithParams.d.ts +8 -0
  46. package/common/utils/getVehiclePosition.d.ts +17 -0
  47. package/common/utils/getVehiclePosition.js +9 -3
  48. package/common/utils/index.d.ts +16 -0
  49. package/common/utils/realtimeConfig.d.ts +64 -0
  50. package/common/utils/removeDuplicate.d.ts +9 -0
  51. package/common/utils/renderTrajectories.d.ts +16 -0
  52. package/common/utils/renderTrajectories.js +6 -6
  53. package/common/utils/sortAndFilterDepartures.d.ts +15 -0
  54. package/common/utils/sortAndFilterDepartures.js +1 -1
  55. package/common/utils/sortByDelay.d.ts +3 -0
  56. package/common/utils/sortByDelay.js +5 -1
  57. package/common/utils/timeUtils.d.ts +23 -0
  58. package/common/utils/toMercatorExtent.d.ts +5 -0
  59. package/iife.d.ts +2 -0
  60. package/index.d.ts +9 -0
  61. package/maplibre/controls/CopyrightControl.d.ts +35 -0
  62. package/maplibre/controls/index.d.ts +1 -0
  63. package/maplibre/index.d.ts +5 -0
  64. package/maplibre/layers/Layer.d.ts +28 -0
  65. package/maplibre/layers/Layer.js +1 -1
  66. package/maplibre/layers/RealtimeLayer.d.ts +181 -0
  67. package/maplibre/layers/RealtimeLayer.js +29 -5
  68. package/maplibre/layers/index.d.ts +2 -0
  69. package/maplibre/utils/getMercatorResolution.d.ts +7 -0
  70. package/maplibre/utils/getSourceCoordinates.d.ts +7 -0
  71. package/maplibre/utils/getSourceCoordinates.js +5 -5
  72. package/maplibre/utils/index.d.ts +2 -0
  73. package/mbt.js +22103 -14430
  74. package/mbt.js.map +4 -4
  75. package/mbt.min.js +61 -58
  76. package/mbt.min.js.map +4 -4
  77. package/ol/controls/CopyrightControl.d.ts +31 -0
  78. package/ol/controls/CopyrightControl.js +18 -8
  79. package/ol/controls/RoutingControl.d.ts +202 -0
  80. package/ol/controls/RoutingControl.js +220 -219
  81. package/ol/controls/StopFinderControl.d.ts +37 -0
  82. package/ol/controls/StopFinderControl.js +4 -1
  83. package/ol/controls/index.d.ts +3 -0
  84. package/ol/index.d.ts +7 -0
  85. package/ol/index.js +1 -0
  86. package/ol/layers/Layer.d.ts +101 -0
  87. package/ol/layers/Layer.js +25 -0
  88. package/ol/layers/MaplibreLayer.d.ts +160 -0
  89. package/ol/layers/MaplibreLayer.js +97 -27
  90. package/ol/layers/MaplibreStyleLayer.d.ts +237 -0
  91. package/ol/layers/MaplibreStyleLayer.js +291 -267
  92. package/ol/layers/RealtimeLayer.d.ts +283 -0
  93. package/ol/layers/RealtimeLayer.js +143 -128
  94. package/ol/layers/VectorLayer.d.ts +18 -0
  95. package/ol/layers/VectorLayer.js +31 -0
  96. package/ol/layers/index.d.ts +5 -0
  97. package/ol/layers/index.js +3 -0
  98. package/ol/mixins/MobilityLayerMixin.d.ts +96 -0
  99. package/ol/mixins/MobilityLayerMixin.js +1 -4
  100. package/ol/mixins/PropertiesLayerMixin.d.ts +135 -0
  101. package/ol/mixins/PropertiesLayerMixin.js +112 -140
  102. package/ol/mixins/index.d.ts +1 -0
  103. package/ol/mixins/index.js +2 -0
  104. package/ol/renderers/MaplibreLayerRenderer.d.ts +0 -0
  105. package/ol/renderers/MaplibreLayerRenderer.js +142 -114
  106. package/ol/renderers/MaplibreStyleLayerRenderer.d.ts +20 -0
  107. package/ol/renderers/MaplibreStyleLayerRenderer.js +20 -23
  108. package/ol/renderers/RealtimeLayerRenderer.d.ts +22 -0
  109. package/ol/renderers/RealtimeLayerRenderer.js +58 -53
  110. package/ol/styles/fullTrajectoryDelayStyle.d.ts +6 -0
  111. package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
  112. package/ol/styles/index.d.ts +3 -0
  113. package/ol/styles/routingStyle.d.ts +4 -0
  114. package/ol/utils/getFeatureInfoAtCoordinate.d.ts +8 -0
  115. package/ol/utils/getFeatureInfoAtCoordinate.js +12 -18
  116. package/ol/utils/index.d.ts +1 -0
  117. package/package.json +31 -31
  118. package/setupTests.d.ts +1 -0
  119. package/setupTests.js +3 -4
  120. package/types/common.d.ts +55 -48
  121. package/types/index.d.ts +1 -1
  122. package/types/realtime.d.ts +91 -93
  123. package/types/routing.d.ts +60 -60
  124. package/types/stops.d.ts +62 -62
  125. package/ol/layers/MapGlLayer.js +0 -142
@@ -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.
@@ -135,139 +137,66 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
135
137
  if (!this.map || !this.maplibreLayer) {
136
138
  return;
137
139
  }
138
- // Apply the initial visibiltity.
139
- const { maplibreMap } = this.maplibreLayer;
140
- if (!maplibreMap) {
141
- // If the maplibreMap is not yet created because the map has no target yet, we
140
+ if (!this.map.getTargetElement()) {
141
+ // If ther e is no target element the mapLibreMap is not yet created, we
142
142
  // relaunch the initialisation when it's the case.
143
- this.olListenersKeys.push(this.map.on('change:target', () => {
143
+ this.olEventsKeys.push(this.map.on('change:target', () => {
144
144
  this.attachToMap(map);
145
145
  }));
146
146
  return;
147
147
  }
148
- // maplibreMap.loaded() and maplibreMap.isStyleLoaded() are reliable only on the first call of init.
149
- // On the next call (when a topic change for example), these functions returns false because
150
- // the style is being modified.
151
- // That's why we rely on a property instead for the next calls.
152
- if (maplibreMap.loaded()) {
153
- this.onLoad();
154
- }
155
- else {
156
- maplibreMap.once('load', this.onLoad);
148
+ // Apply the initial visibility if possible otherwise we wait for the load event of the layer
149
+ const { mapLibreMap } = this.maplibreLayer;
150
+ if (mapLibreMap) {
151
+ // mapLibreMap.loaded() and mapLibreMap.isStyleLoaded() are reliable only on the first call of init.
152
+ // On the next call (when a topic change for example), these functions returns false because
153
+ // the style is being modified.
154
+ // That's why we rely on a property instead for the next calls.
155
+ if (mapLibreMap.loaded()) {
156
+ this.onLoad();
157
+ }
158
+ else {
159
+ mapLibreMap.once('load', this.onLoad);
160
+ }
157
161
  }
158
162
  // Apply the visibiltity when layer's visibility change.
159
- this.olListenersKeys.push(
160
- // @ts-expect-error 'load' is a custom event form mobility-toolbox-js
163
+ this.olEventsKeys.push(
164
+ // @ts-expect-error 'load' is a custom event
161
165
  this.maplibreLayer.on('load', this.onLoad.bind(this)), this.on('change:visible', (evt) => {
162
166
  // Once the map is loaded we can apply visiblity without waiting
163
167
  // the style. Maplibre take care of the application of style changes.
164
168
  this.applyLayoutVisibility(evt);
165
169
  }), this.on('propertychange', (evt) => {
166
- if (/(sources|layers|layersFilter|maplibreLayer|beforeId|)/.test(evt.key)) {
170
+ if (/(sources|layers|layersFilter|maplibreLayer|beforeId)/.test(evt.key)) {
167
171
  this.detachFromMap();
168
172
  this.attachToMap(map);
169
173
  }
170
174
  }));
171
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
+ }
172
187
  /**
173
188
  * Terminate the layer.
174
189
  * @override
175
190
  */
176
191
  detachFromMap() {
177
192
  var _a;
178
- if ((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) {
179
- 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);
180
195
  this.removeLayers();
181
196
  this.removeSources();
182
197
  }
183
198
  super.detachFromMap();
184
199
  }
185
- /** @private */
186
- addSources() {
187
- var _a;
188
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) || !Array.isArray(this.sources)) {
189
- return;
190
- }
191
- const { maplibreMap } = this.maplibreLayer;
192
- if (maplibreMap) {
193
- this.sources.forEach((source) => {
194
- const { id } = source;
195
- if (!maplibreMap.getSource(id)) {
196
- maplibreMap.addSource(id, source);
197
- }
198
- });
199
- }
200
- }
201
- /** @private */
202
- removeSources() {
203
- var _a;
204
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) || !Array.isArray(this.layers)) {
205
- return;
206
- }
207
- const { maplibreMap } = this.maplibreLayer;
208
- if (maplibreMap) {
209
- this.sources.forEach((source) => {
210
- const { id } = source;
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() {
290
+ var _a;
291
+ if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !Array.isArray(this.layers)) {
292
+ return;
293
+ }
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() {
345
306
  var _a;
346
- if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.maplibreMap) || !features.length) {
307
+ if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !this.sources) {
347
308
  return;
348
309
  }
349
- const { maplibreMap } = this.maplibreLayer;
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;