mobility-toolbox-js 2.0.0-beta.3 → 2.0.0-beta.33

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 (220) hide show
  1. package/README.md +4 -1
  2. package/api/RealtimeAPI.d.ts +268 -0
  3. package/api/RealtimeAPI.d.ts.map +1 -0
  4. package/api/RoutingAPI.d.ts +33 -0
  5. package/api/RoutingAPI.d.ts.map +1 -0
  6. package/api/StopsAPI.d.ts +36 -0
  7. package/api/StopsAPI.d.ts.map +1 -0
  8. package/api/index.d.ts +5 -0
  9. package/api/index.d.ts.map +1 -0
  10. package/api/typedefs.d.ts +105 -0
  11. package/api/typedefs.d.ts.map +1 -0
  12. package/common/api/HttpAPI.d.ts +31 -0
  13. package/common/api/HttpAPI.d.ts.map +1 -0
  14. package/common/api/WebSocketAPI.d.ts +95 -0
  15. package/common/api/WebSocketAPI.d.ts.map +1 -0
  16. package/common/controls/Control.d.ts +74 -0
  17. package/common/controls/Control.d.ts.map +1 -0
  18. package/common/index.d.ts +3 -0
  19. package/common/index.d.ts.map +1 -0
  20. package/common/layers/Layer.d.ts +80 -0
  21. package/common/layers/Layer.d.ts.map +1 -0
  22. package/common/mixins/CopyrightMixin.d.ts +22 -0
  23. package/common/mixins/CopyrightMixin.d.ts.map +1 -0
  24. package/common/mixins/MapboxLayerMixin.d.ts +27 -0
  25. package/common/mixins/MapboxLayerMixin.d.ts.map +1 -0
  26. package/common/mixins/RealtimeLayerMixin.d.ts +58 -0
  27. package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
  28. package/common/mixins/StopFinderMixin.d.ts +40 -0
  29. package/common/mixins/StopFinderMixin.d.ts.map +1 -0
  30. package/common/mixins/UserInteractionsLayerMixin.d.ts +42 -0
  31. package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
  32. package/common/styles/index.d.ts +5 -0
  33. package/common/styles/index.d.ts.map +1 -0
  34. package/common/styles/realtimeDefaultStyle.d.ts +15 -0
  35. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
  36. package/common/styles/{trackerDelayStyle.js → realtimeDelayStyle.d.ts} +3 -9
  37. package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
  38. package/common/styles/realtimeSimpleStyle.d.ts +3 -0
  39. package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
  40. package/common/typedefs.d.ts +50 -0
  41. package/common/typedefs.d.ts.map +1 -0
  42. package/common/utils/cleanStopTime.d.ts +7 -0
  43. package/common/utils/cleanStopTime.d.ts.map +1 -0
  44. package/common/utils/compareDepartures.d.ts +9 -0
  45. package/common/utils/compareDepartures.d.ts.map +1 -0
  46. package/common/utils/createCanvas.d.ts +9 -0
  47. package/common/utils/createCanvas.d.ts.map +1 -0
  48. package/common/utils/createTrackerFilters.d.ts +12 -0
  49. package/common/utils/createTrackerFilters.d.ts.map +1 -0
  50. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  51. package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
  52. package/common/utils/getMapboxMapCopyrights.d.ts +8 -0
  53. package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
  54. package/common/utils/getMapboxRender.d.ts +5 -0
  55. package/common/utils/getMapboxRender.d.ts.map +1 -0
  56. package/common/utils/getMaplibreRender.d.ts +5 -0
  57. package/common/utils/getMaplibreRender.d.ts.map +1 -0
  58. package/common/utils/getRealtimeModeSuffix.d.ts +8 -0
  59. package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
  60. package/common/utils/getUrlWithParams.d.ts +9 -0
  61. package/common/utils/getUrlWithParams.d.ts.map +1 -0
  62. package/common/utils/getVehiclePosition.d.ts +14 -0
  63. package/common/utils/getVehiclePosition.d.ts.map +1 -0
  64. package/common/utils/index.d.ts +12 -0
  65. package/common/utils/index.d.ts.map +1 -0
  66. package/common/utils/removeDuplicate.d.ts +10 -0
  67. package/common/utils/removeDuplicate.d.ts.map +1 -0
  68. package/common/utils/renderTrajectories.d.ts +20 -0
  69. package/common/utils/renderTrajectories.d.ts.map +1 -0
  70. package/common/utils/sortByDelay.d.ts +3 -0
  71. package/common/utils/sortByDelay.d.ts.map +1 -0
  72. package/common/utils/timeUtils.d.ts +5 -0
  73. package/common/utils/timeUtils.d.ts.map +1 -0
  74. package/common/utils/trackerConfig.d.ts +24 -0
  75. package/common/utils/trackerConfig.d.ts.map +1 -0
  76. package/iife.d.ts +3 -0
  77. package/iife.d.ts.map +1 -0
  78. package/index.d.ts +6 -0
  79. package/index.d.ts.map +1 -0
  80. package/mapbox/controls/CopyrightControl.d.ts +32 -0
  81. package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
  82. package/mapbox/controls/index.d.ts +2 -0
  83. package/mapbox/controls/index.d.ts.map +1 -0
  84. package/mapbox/index.d.ts +5 -0
  85. package/mapbox/index.d.ts.map +1 -0
  86. package/mapbox/layers/Layer.d.ts +47 -0
  87. package/mapbox/layers/Layer.d.ts.map +1 -0
  88. package/mapbox/layers/RealtimeLayer.d.ts +118 -0
  89. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
  90. package/mapbox/layers/index.d.ts +3 -0
  91. package/mapbox/layers/index.d.ts.map +1 -0
  92. package/mapbox/utils.d.ts +8 -0
  93. package/mapbox/utils.d.ts.map +1 -0
  94. package/mbt.js +50042 -0
  95. package/mbt.js.map +7 -0
  96. package/mbt.min.js +1008 -0
  97. package/mbt.min.js.map +7 -0
  98. package/ol/controls/CopyrightControl.d.ts +31 -0
  99. package/ol/controls/CopyrightControl.d.ts.map +1 -0
  100. package/ol/controls/RoutingControl.d.ts +180 -0
  101. package/ol/controls/RoutingControl.d.ts.map +1 -0
  102. package/ol/controls/StopFinderControl.d.ts +32 -0
  103. package/ol/controls/StopFinderControl.d.ts.map +1 -0
  104. package/ol/controls/index.d.ts +4 -0
  105. package/ol/controls/index.d.ts.map +1 -0
  106. package/ol/index.d.ts +6 -0
  107. package/ol/index.d.ts.map +1 -0
  108. package/ol/layers/Layer.d.ts +49 -0
  109. package/ol/layers/Layer.d.ts.map +1 -0
  110. package/ol/layers/MapboxLayer.d.ts +42 -0
  111. package/ol/layers/MapboxLayer.d.ts.map +1 -0
  112. package/ol/layers/MapboxStyleLayer.d.ts +146 -0
  113. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
  114. package/ol/layers/MaplibreLayer.d.ts +27 -0
  115. package/ol/layers/MaplibreLayer.d.ts.map +1 -0
  116. package/ol/layers/RealtimeLayer.d.ts +119 -0
  117. package/ol/layers/RealtimeLayer.d.ts.map +1 -0
  118. package/ol/layers/RoutingLayer.d.ts +24 -0
  119. package/ol/layers/RoutingLayer.d.ts.map +1 -0
  120. package/ol/layers/VectorLayer.d.ts +23 -0
  121. package/ol/layers/VectorLayer.d.ts.map +1 -0
  122. package/ol/layers/WMSLayer.d.ts +38 -0
  123. package/ol/layers/WMSLayer.d.ts.map +1 -0
  124. package/ol/layers/index.d.ts +9 -0
  125. package/ol/layers/index.d.ts.map +1 -0
  126. package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
  127. package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
  128. package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
  129. package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
  130. package/ol/styles/index.d.ts +3 -0
  131. package/ol/styles/index.d.ts.map +1 -0
  132. package/package.json +56 -95
  133. package/setupTests.d.ts +2 -0
  134. package/setupTests.d.ts.map +1 -0
  135. package/types/index.d.ts +10 -0
  136. package/types/realtime.d.ts +24 -0
  137. package/types/routing.d.ts +206 -0
  138. package/types/stops.d.ts +143 -0
  139. package/api/index.js +0 -3
  140. package/api/routing/RoutingAPI.js +0 -44
  141. package/api/routing/RoutingAPI.test.js +0 -41
  142. package/api/stops/StopsAPI.js +0 -41
  143. package/api/stops/StopsAPI.test.js +0 -34
  144. package/api/tralis/TralisAPI.js +0 -731
  145. package/api/tralis/TralisAPI.test.js +0 -75
  146. package/api/tralis/TralisAPIUtils.js +0 -73
  147. package/api/tralis/WebSocketConnector.js +0 -338
  148. package/api/tralis/WebSocketConnector.test.js +0 -356
  149. package/api/tralis/typedefs.js +0 -81
  150. package/common/Tracker.js +0 -197
  151. package/common/api/api.js +0 -64
  152. package/common/api/api.test.js +0 -68
  153. package/common/controls/Control.js +0 -146
  154. package/common/controls/Control.test.js +0 -98
  155. package/common/layers/Layer.js +0 -404
  156. package/common/layers/Layer.test.js +0 -585
  157. package/common/mixins/CopyrightMixin.js +0 -48
  158. package/common/mixins/SearchMixin.js +0 -176
  159. package/common/mixins/TralisLayerMixin.js +0 -930
  160. package/common/styles/index.js +0 -4
  161. package/common/styles/trackerDefaultStyle.js +0 -333
  162. package/common/styles/trackerSimpleStyle.js +0 -22
  163. package/common/trackerConfig.js +0 -190
  164. package/common/trackerConfig.test.js +0 -25
  165. package/common/typedefs.js +0 -23
  166. package/common/utils/createTrackerFilters.js +0 -87
  167. package/common/utils/createTrackerFilters.test.js +0 -95
  168. package/common/utils/getMapboxMapCopyrights.js +0 -32
  169. package/common/utils/getMapboxMapCopyrights.test.js +0 -47
  170. package/common/utils/getMapboxStyleUrl.js +0 -32
  171. package/common/utils/getVehiclePosition.js +0 -72
  172. package/common/utils/index.js +0 -6
  173. package/common/utils/removeDuplicate.js +0 -22
  174. package/common/utils/removeDuplicate.test.js +0 -22
  175. package/common/utils/sortByDelay.js +0 -23
  176. package/common/utils/timeUtils.js +0 -44
  177. package/common/utils/timeUtils.test.js +0 -16
  178. package/index.js +0 -2
  179. package/index.js.map +0 -1
  180. package/mapbox/controls/CopyrightControl.js +0 -58
  181. package/mapbox/index.js +0 -3
  182. package/mapbox/layers/Layer.js +0 -118
  183. package/mapbox/layers/Layer.test.js +0 -202
  184. package/mapbox/layers/TralisLayer.js +0 -329
  185. package/mapbox/layers/TralisLayer.test.js +0 -40
  186. package/mapbox/utils.js +0 -46
  187. package/module.js +0 -23
  188. package/ol/README.md +0 -0
  189. package/ol/controls/CopyrightControl.js +0 -80
  190. package/ol/controls/CopyrightControl.test.js +0 -211
  191. package/ol/controls/RoutingControl.js +0 -752
  192. package/ol/controls/RoutingControl.test.js +0 -216
  193. package/ol/controls/StopFinderControl.js +0 -38
  194. package/ol/controls/StopFinderControl.test.js +0 -59
  195. package/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
  196. package/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
  197. package/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
  198. package/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
  199. package/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
  200. package/ol/controls/snapshots/RoutingControlStation1.json +0 -60
  201. package/ol/controls/snapshots/RoutingControlStation2.json +0 -49
  202. package/ol/index.js +0 -13
  203. package/ol/layers/Layer.js +0 -193
  204. package/ol/layers/Layer.test.js +0 -197
  205. package/ol/layers/MapboxLayer.js +0 -378
  206. package/ol/layers/MapboxLayer.test.js +0 -186
  207. package/ol/layers/MapboxStyleLayer.js +0 -417
  208. package/ol/layers/MapboxStyleLayer.test.js +0 -262
  209. package/ol/layers/MaplibreLayer.js +0 -280
  210. package/ol/layers/RoutingLayer.js +0 -91
  211. package/ol/layers/RoutingLayer.test.js +0 -49
  212. package/ol/layers/TralisLayer.js +0 -359
  213. package/ol/layers/TralisLayer.test.js +0 -97
  214. package/ol/layers/VectorLayer.js +0 -43
  215. package/ol/layers/VectorLayer.test.js +0 -98
  216. package/ol/layers/WMSLayer.js +0 -80
  217. package/ol/layers/WMSLayer.test.js +0 -84
  218. package/ol/styles/fullTrajectoryDelayStyle.js +0 -35
  219. package/ol/styles/fullTrajectoryStyle.js +0 -51
  220. package/ol/styles/index.js +0 -2
@@ -1,417 +0,0 @@
1
- /* eslint-disable no-param-reassign */
2
- import Layer from './Layer';
3
-
4
- /**
5
- * Layer for visualizing a specific set of layer from a MapboxLayer.
6
- *
7
- * @example
8
- * import { MapboxLayer, MapboxStyleLayer } from 'mobility-toolbox-js/ol';
9
- *
10
- * const mapboxLayer = new MapboxLayer({
11
- * url: 'https://maps.geops.io/styles/travic_v2/style.json?key=[yourApiKey]',
12
- * });
13
- *
14
- * const layer = new MapboxStyleLayer({
15
- * mapboxLayer: mapboxLayer,
16
- * styleLayersFilter: () => {},
17
- * });
18
- *
19
- * @classproperty {ol/Map~Map} map - The map where the layer is displayed.
20
- * @extends {Layer}
21
- */
22
- class MapboxStyleLayer extends Layer {
23
- /**
24
- * Constructor.
25
- *
26
- * @param {Object} options
27
- * @param {MapboxLayer} [options.mapboxLayer] The MapboxLayer to use.
28
- * @param {Function} [options.styleLayersFilter] Filter function to decide which style layer to display.
29
- */
30
- constructor(options = {}) {
31
- super(options);
32
-
33
- /**
34
- * MapboxLayer provided for the style Layer.
35
- * @type {MapboxLayer}
36
- * @private
37
- */
38
- this.mapboxLayer = options.mapboxLayer;
39
-
40
- /**
41
- * Define if the layer has data to display in the current mapbox layer.
42
- */
43
- this.disabled = false;
44
-
45
- /**
46
- * Function to filter features to be displayed.
47
- * @type {function}
48
- * @private
49
- */
50
- this.styleLayersFilter = options.styleLayersFilter;
51
-
52
- /**
53
- * Mapbox style layer id where to add the style layers.
54
- * See [mapbox.map.addLayer](https://docs.mapbox.com/mapbox-gl-js/api/map/#map#addlayer) documentation.
55
- * @type {String}
56
- * @private
57
- */
58
- this.beforeId = options.beforeId;
59
-
60
- /**
61
- * Function to filter features for getFeatureInfoAtCoordinate method.
62
- * @type {function}
63
- * @private
64
- */
65
- this.featureInfoFilter = options.featureInfoFilter || ((obj) => obj);
66
-
67
- /**
68
- * Function to query the rendered features.
69
- * @type {function}
70
- * @private
71
- */
72
- this.queryRenderedLayersFilter = options.queryRenderedLayersFilter;
73
-
74
- /**
75
- * Array of features to highlight.
76
- * @type {Array<ol/Feature~Feature>}
77
- * @private
78
- */
79
- this.highlightedFeatures = [];
80
-
81
- /**
82
- * Array of selected features.
83
- * @type {Array<ol/Feature~Feature>}
84
- * @private
85
- */
86
- this.selectedFeatures = [];
87
-
88
- /**
89
- * Array of mapbox style layers to add.
90
- * @type {Array<mapboxgl.styleLayer>}
91
- * @private
92
- */
93
- this.styleLayers =
94
- (options.styleLayer ? [options.styleLayer] : options.styleLayers) || [];
95
-
96
- /**
97
- * @private
98
- */
99
- this.addStyleLayers = this.addStyleLayers.bind(this);
100
-
101
- /**
102
- * @private
103
- */
104
- this.onLoad = this.onLoad.bind(this);
105
- if (options.filters) {
106
- /** @private */
107
- this.addDynamicFilters = () => {
108
- this.setFilter(
109
- typeof options.filters === 'function'
110
- ? options.filters(this)
111
- : options.filters,
112
- );
113
- };
114
- }
115
-
116
- if (!this.styleLayersFilter && this.styleLayers) {
117
- const ids = this.styleLayers.map((s) => s.id);
118
- this.styleLayersFilter = (styleLayer) => ids.includes(styleLayer.id);
119
- }
120
- }
121
-
122
- /**
123
- * Initialize the layer.
124
- * @param {mapboxgl.Map} map the mapbox map.
125
- * @override
126
- */
127
- init(map) {
128
- if (!this.mapboxLayer.map) {
129
- this.mapboxLayer.init(map);
130
- }
131
- super.init(map);
132
-
133
- if (!this.map) {
134
- return;
135
- }
136
-
137
- // Apply the initial visibiltity.
138
- const { mbMap } = this.mapboxLayer;
139
- if (!mbMap) {
140
- // If the mbMap is not yet created because the map has no target yet, we
141
- // relaunch the initialisation when it's the case.
142
- this.olListenersKeys.push(
143
- this.map.on('change:target', () => {
144
- this.init(map);
145
- }),
146
- );
147
-
148
- return;
149
- }
150
-
151
- // mbMap.loaded() and mbMap.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 (this.mapboxLayer.loaded || mbMap.isStyleLoaded() || mbMap.loaded()) {
156
- this.onLoad();
157
- } else {
158
- mbMap.once('load', this.onLoad);
159
- }
160
-
161
- // Apply the visibiltity when layer's visibility change.
162
- this.olListenersKeys.push(
163
- this.on('change:visible', (evt) => {
164
- // Once the map is loaded we can apply vsiiblity without waiting
165
- // the style. Mapbox take care of the application of style changes.
166
- this.applyLayoutVisibility(evt);
167
- }),
168
- );
169
-
170
- this.olListenersKeys.push(
171
- this.mapboxLayer.on('load', () => {
172
- this.onLoad();
173
- }),
174
- );
175
- }
176
-
177
- /**
178
- * Terminate the layer.
179
- * @param {mapboxgl.Map} map the mapbox map.
180
- * @override
181
- */
182
- terminate(map) {
183
- const { mbMap } = this.mapboxLayer;
184
- if (mbMap) {
185
- mbMap.off('load', this.onLoad);
186
- this.removeStyleLayers();
187
- }
188
- super.terminate(map);
189
- }
190
-
191
- /** @ignore */
192
- addStyleLayers() {
193
- const { mbMap } = this.mapboxLayer;
194
-
195
- if (!mbMap) {
196
- return;
197
- }
198
-
199
- this.styleLayers.forEach((styleLayer) => {
200
- const { id, source } = styleLayer;
201
- if (mbMap.getSource(source) && !mbMap.getLayer(id)) {
202
- mbMap.addLayer(styleLayer, this.beforeId);
203
- }
204
- });
205
- this.applyLayoutVisibility();
206
- }
207
-
208
- /** @ignore */
209
- removeStyleLayers() {
210
- const { mbMap } = this.mapboxLayer;
211
-
212
- if (!mbMap) {
213
- return;
214
- }
215
-
216
- this.styleLayers.forEach((styleLayer) => {
217
- if (mbMap.getLayer(styleLayer.id)) {
218
- mbMap.removeLayer(styleLayer.id);
219
- }
220
- });
221
- }
222
-
223
- /**
224
- * On Mapbox map load callback function. Add style layers and dynaimc filters.
225
- * @ignore
226
- */
227
- onLoad() {
228
- this.addStyleLayers();
229
-
230
- if (this.addDynamicFilters) {
231
- this.addDynamicFilters();
232
- }
233
-
234
- const { mbMap } = this.mapboxLayer;
235
- const style = mbMap.getStyle();
236
- if (style && this.styleLayersFilter) {
237
- const styles = style.layers.filter(this.styleLayersFilter);
238
- this.disabled = !styles.length;
239
- }
240
- }
241
-
242
- /**
243
- * Request feature information for a given coordinate.
244
- * @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
245
- * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate.
246
- */
247
- getFeatureInfoAtCoordinate(coordinate) {
248
- const { mbMap } = this.mapboxLayer;
249
-
250
- // Ignore the getFeatureInfo until the mapbox map is loaded
251
- if (!mbMap || !mbMap.isStyleLoaded()) {
252
- return Promise.resolve({ coordinate, features: [], layer: this });
253
- }
254
-
255
- // We query features only on style layers used by this layer.
256
- let layers = this.styleLayers || [];
257
-
258
- if (this.styleLayersFilter) {
259
- layers = mbMap.getStyle().layers.filter(this.styleLayersFilter);
260
- }
261
-
262
- if (this.queryRenderedLayersFilter) {
263
- layers = mbMap.getStyle().layers.filter(this.queryRenderedLayersFilter);
264
- }
265
-
266
- return this.mapboxLayer
267
- .getFeatureInfoAtCoordinate(coordinate, {
268
- layers: layers.map((layer) => layer && layer.id),
269
- validate: false,
270
- })
271
- .then((featureInfo) => {
272
- const features = featureInfo.features.filter((feature) =>
273
- this.featureInfoFilter(feature, this.map.getView().getResolution()),
274
- );
275
- this.highlight(features);
276
- return { ...featureInfo, features, layer: this };
277
- });
278
- }
279
-
280
- /**
281
- * Set filter that determines which features should be rendered in a style layer.
282
- * @param {mapboxgl.filter} filter Determines which features should be rendered in a style layer.
283
- */
284
- setFilter(filter) {
285
- const { mbMap } = this.mapboxLayer;
286
-
287
- if (!mbMap) {
288
- return;
289
- }
290
-
291
- this.styleLayers.forEach(({ id }) => {
292
- if (mbMap.getLayer(id)) {
293
- mbMap.setFilter(id, filter);
294
- }
295
- });
296
- }
297
-
298
- /**
299
- * Set if features are hovered or not.
300
- * @param {Array<ol/Feature~Feature>} features
301
- * @param {boolean} state Is the feature hovered
302
- * @private
303
- */
304
- setHoverState(features, state) {
305
- const { mbMap } = this.mapboxLayer;
306
-
307
- if (!features || !mbMap) {
308
- return;
309
- }
310
-
311
- features.forEach((feature) => {
312
- const { source, sourceLayer } = feature.get('mapboxFeature') || {};
313
- if ((!source && !sourceLayer) || !feature.getId()) {
314
- if (!feature.getId()) {
315
- // eslint-disable-next-line no-console
316
- console.warn(
317
- "No feature's id found. To use the feature state functionnality, tiles must be generated with --generate-ids. See https://github.com/mapbox/tippecanoe#adding-calculated-attributes.",
318
- feature.getId(),
319
- feature.getProperties(),
320
- );
321
- }
322
- return;
323
- }
324
-
325
- mbMap.setFeatureState(
326
- {
327
- id: feature.getId(),
328
- source,
329
- sourceLayer,
330
- },
331
- { hover: state },
332
- );
333
- });
334
- }
335
-
336
- /**
337
- * Select a list of features.
338
- * @param {Array<ol/Feature~Feature>} [features=[]] Features to select.
339
- * @private
340
- */
341
- select(features = []) {
342
- this.setHoverState(this.selectedFeatures, false);
343
- this.selectedFeatures = features;
344
- this.setHoverState(this.selectedFeatures, true);
345
- }
346
-
347
- /**
348
- * Highlight a list of features.
349
- * @param {Array<ol/Feature~Feature>} [features=[]] Features to highlight.
350
- * @private
351
- */
352
- highlight(features = []) {
353
- // Filter out selected features
354
- const filtered = this.highlightedFeatures.filter(
355
- (feature) =>
356
- !this.selectedFeatures
357
- .map((feat) => feat.getId())
358
- .includes(feature.getId()),
359
- );
360
-
361
- // Remove previous highlight
362
- this.setHoverState(filtered, false);
363
- this.highlightedFeatures = features;
364
-
365
- // Add highlight
366
- this.setHoverState(this.highlightedFeatures, true);
367
- }
368
-
369
- /**
370
- * Apply visibility to style layers that fits the styleLayersFilter function.
371
- * @param {Event} evt Layer's event that has called the function.
372
- * @private
373
- */
374
- // eslint-disable-next-line no-unused-vars
375
- applyLayoutVisibility(evt) {
376
- const { visible } = this;
377
- const { mbMap } = this.mapboxLayer;
378
- const filterFunc = this.styleLayersFilter;
379
-
380
- if (!mbMap) {
381
- return;
382
- }
383
-
384
- const style = mbMap.getStyle();
385
-
386
- if (!style) {
387
- return;
388
- }
389
-
390
- if (filterFunc) {
391
- const visibilityValue = visible ? 'visible' : 'none';
392
- for (let i = 0; i < style.layers.length; i += 1) {
393
- const styleLayer = style.layers[i];
394
- if (filterFunc(styleLayer)) {
395
- if (mbMap.getLayer(styleLayer.id)) {
396
- mbMap.setLayoutProperty(
397
- styleLayer.id,
398
- 'visibility',
399
- visibilityValue,
400
- );
401
- }
402
- }
403
- }
404
- }
405
- }
406
-
407
- /**
408
- * Create a copy of the MapboxStyleLayer.
409
- * @param {Object} newOptions Options to override.
410
- * @return {MapboxStyleLayer} A MapboxStyleLayer.
411
- */
412
- clone(newOptions) {
413
- return new MapboxStyleLayer({ ...this.options, ...newOptions });
414
- }
415
- }
416
-
417
- export default MapboxStyleLayer;
@@ -1,262 +0,0 @@
1
- import OlMap from 'ol/Map';
2
- import View from 'ol/View';
3
- import gllib from 'maplibre-gl';
4
- import Layer from './Layer';
5
- import MaplibreLayer from './MaplibreLayer';
6
- import MapboxStyleLayer from './MapboxStyleLayer';
7
-
8
- let source;
9
- let layer;
10
- let map;
11
- let onClick;
12
-
13
- const styleLayer = {
14
- id: 'layer',
15
- };
16
-
17
- describe('MapboxStyleLayer', () => {
18
- beforeEach(() => {
19
- onClick = jest.fn();
20
- source = new MaplibreLayer({
21
- name: 'Layer',
22
- apiKey: false,
23
- });
24
- layer = new MapboxStyleLayer({
25
- name: 'mapbox layer',
26
- visible: true,
27
- mapboxLayer: source,
28
- styleLayer,
29
- onClick,
30
- });
31
- map = new OlMap({
32
- target: document.createElement('div'),
33
- view: new View({ center: [0, 0] }),
34
- });
35
- });
36
-
37
- afterEach(() => {
38
- if (layer.map) {
39
- layer.terminate(map);
40
- }
41
- if (source.map) {
42
- source.terminate(map);
43
- }
44
- });
45
-
46
- test('should be instanced.', () => {
47
- expect(layer).toBeInstanceOf(MapboxStyleLayer);
48
- expect(layer.styleLayers[0]).toBe(styleLayer);
49
- expect(layer.clickCallbacks[0]).toBe(onClick);
50
- });
51
-
52
- test('should not initalized mapbox map.', () => {
53
- layer.init();
54
- expect(layer.mbMap).toBe();
55
- layer.terminate();
56
- });
57
-
58
- test('should initalized mapbox map.', () => {
59
- source.init(map);
60
- layer.init(map);
61
- expect(layer.mapboxLayer.mbMap).toBeInstanceOf(gllib.Map);
62
- layer.terminate();
63
- source.terminate();
64
- });
65
-
66
- test('should add onClick callback.', () => {
67
- const onClick2 = jest.fn();
68
- layer.onClick(onClick2);
69
- expect(layer.clickCallbacks[1]).toBe(onClick2);
70
- });
71
-
72
- test('should called terminate on initalization.', () => {
73
- const spy = jest.spyOn(layer, 'terminate');
74
- layer.init();
75
- expect(spy).toHaveBeenCalledTimes(1);
76
- layer.terminate(map);
77
- });
78
-
79
- test('should return coordinates, features and a layer instance.', async () => {
80
- source.init(map);
81
- layer.init(map);
82
- const data = await layer.getFeatureInfoAtCoordinate([50, 50]);
83
- expect(data.coordinate).toEqual([50, 50]);
84
- expect(data.features).toEqual([]);
85
- expect(data.layer).toBeInstanceOf(MapboxStyleLayer);
86
- layer.terminate(map);
87
- source.terminate(map);
88
- });
89
-
90
- test('should call onClick callback', async () => {
91
- const coordinate = [500, 500];
92
- const features = [];
93
- const evt = { type: 'singleclick', map, coordinate };
94
- layer.init(map);
95
- expect(onClick).toHaveBeenCalledTimes(0);
96
- await map.dispatchEvent(evt);
97
- expect(onClick).toHaveBeenCalledTimes(1);
98
- expect(onClick).toHaveBeenCalledWith(features, layer, coordinate);
99
- layer.terminate();
100
- });
101
-
102
- test('should call super class terminate function.', () => {
103
- layer.init(map);
104
- const spy = jest.spyOn(Layer.prototype, 'terminate');
105
- layer.terminate(map);
106
- expect(spy).toHaveBeenCalledTimes(1);
107
- spy.mockRestore();
108
- });
109
-
110
- test('should call super class terminate if the mapboxLayer associated has been terminated before.', () => {
111
- layer.init(map);
112
- source.terminate(map);
113
- const spy = jest.spyOn(Layer.prototype, 'terminate');
114
- layer.terminate(map);
115
- expect(spy).toHaveBeenCalledTimes(1);
116
- spy.mockRestore();
117
- });
118
-
119
- test('should clone', () => {
120
- const clone = layer.clone({ name: 'clone' });
121
- expect(clone).not.toBe(layer);
122
- expect(clone.name).toBe('clone');
123
- expect(clone).toBeInstanceOf(MapboxStyleLayer);
124
- });
125
-
126
- test('should add layer on load', () => {
127
- const style = { layers: [] };
128
- layer.mapboxLayer.mbMap = {
129
- getStyle: () => style,
130
- getSource: () => ({}),
131
- getLayer: () => null,
132
- setLayoutProperty: () => null,
133
- addLayer: (styleLayerr) => style.layers.push(styleLayerr),
134
- };
135
- layer.onLoad();
136
- expect(style.layers[0]).toBe(styleLayer);
137
- });
138
-
139
- describe('should set disabled property to false on load', () => {
140
- test('when layer uses styleLayer property', () => {
141
- const styles = { layers: [] };
142
- layer.mapboxLayer.mbMap = {
143
- getStyle: () => styles,
144
- getSource: () => ({}),
145
- getLayer: () => null,
146
- setLayoutProperty: () => null,
147
- addLayer: (styleLayerr) => styles.layers.push(styleLayerr),
148
- };
149
- expect(layer).toBeInstanceOf(MapboxStyleLayer);
150
- layer.onLoad();
151
- expect(layer.disabled).toBe(false);
152
- });
153
- });
154
-
155
- describe('should set disabled property to true on load', () => {
156
- test('when layer uses styleLayersFilter property', () => {
157
- const styles = { layers: [styleLayer] };
158
- const layer2 = new MapboxStyleLayer({
159
- name: 'mapbox layer',
160
- mapboxLayer: source,
161
- styleLayersFilter: () => false,
162
- });
163
- layer2.mapboxLayer.mbMap = {
164
- getStyle: () => styles,
165
- getSource: () => ({}),
166
- getLayer: () => null,
167
- setLayoutProperty: () => null,
168
- addLayer: () => ({}),
169
- };
170
- layer2.onLoad();
171
- expect(layer2.disabled).toBe(true);
172
- });
173
- });
174
-
175
- describe('#getFeatureInfoAtCoordinate()', () => {
176
- beforeEach(() => {
177
- source.init(map);
178
- source.mbMap.isStyleLoaded = jest.fn(() => true);
179
- source.mbMap.getSource = jest.fn(() => true);
180
- });
181
- afterEach(() => {
182
- source.mbMap.getSource.mockRestore();
183
- source.mbMap.isStyleLoaded.mockRestore();
184
- });
185
-
186
- test('should request features on layers ids from styleLayers property', () => {
187
- source.mbMap.getStyle = jest.fn(() => ({
188
- layers: [{ id: 'foo' }, { id: 'layer' }, { id: 'bar' }],
189
- }));
190
- layer.init(map);
191
- layer.mapboxLayer.getFeatureInfoAtCoordinate = jest.fn(() =>
192
- Promise.resolve({ features: [] }),
193
- );
194
- layer.getFeatureInfoAtCoordinate([0, 0]).then(() => {});
195
- expect(layer.mapboxLayer.getFeatureInfoAtCoordinate).toHaveBeenCalledWith(
196
- [0, 0],
197
- { layers: ['layer'], validate: false },
198
- );
199
- layer.mapboxLayer.getFeatureInfoAtCoordinate.mockRestore();
200
- source.mbMap.getStyle.mockRestore();
201
- });
202
-
203
- test('should request features on layers ids from styleLayersFilter property', () => {
204
- source.mbMap.getStyle = jest.fn(() => ({
205
- layers: [{ id: 'foo' }, { id: 'layer' }, { id: 'bar' }, { id: 'foo2' }],
206
- }));
207
- const layer2 = new MapboxStyleLayer({
208
- name: 'mapbox layer',
209
- visible: true,
210
- mapboxLayer: source,
211
- styleLayer,
212
- styleLayersFilter: ({ id }) => /foo/.test(id),
213
- });
214
- layer2.init(map);
215
- layer2.mapboxLayer.getFeatureInfoAtCoordinate = jest.fn(() =>
216
- Promise.resolve({ features: [] }),
217
- );
218
- layer2.getFeatureInfoAtCoordinate([0, 0]).then(() => {});
219
- expect(
220
- layer2.mapboxLayer.getFeatureInfoAtCoordinate,
221
- ).toHaveBeenCalledWith([0, 0], {
222
- layers: ['foo', 'foo2'],
223
- validate: false,
224
- });
225
- layer2.mapboxLayer.getFeatureInfoAtCoordinate.mockRestore();
226
- source.mbMap.getStyle.mockRestore();
227
- });
228
-
229
- test('should request features on layers ids from queryRenderedLayersFilter property', () => {
230
- source.mbMap.getStyle = jest.fn(() => ({
231
- layers: [
232
- { id: 'foo' },
233
- { id: 'bar2' },
234
- { id: 'layer' },
235
- { id: 'bar' },
236
- { id: 'foo2' },
237
- ],
238
- }));
239
- const layer2 = new MapboxStyleLayer({
240
- name: 'mapbox layer',
241
- visible: true,
242
- mapboxLayer: source,
243
- styleLayer,
244
- styleLayersFilter: ({ id }) => /foo/.test(id),
245
- queryRenderedLayersFilter: ({ id }) => /bar/.test(id),
246
- });
247
- layer2.init(map);
248
- layer2.mapboxLayer.getFeatureInfoAtCoordinate = jest.fn(() =>
249
- Promise.resolve({ features: [] }),
250
- );
251
- layer2.getFeatureInfoAtCoordinate([0, 0]).then(() => {});
252
- expect(
253
- layer2.mapboxLayer.getFeatureInfoAtCoordinate,
254
- ).toHaveBeenCalledWith([0, 0], {
255
- layers: ['bar2', 'bar'],
256
- validate: false,
257
- });
258
- layer2.mapboxLayer.getFeatureInfoAtCoordinate.mockRestore();
259
- source.mbMap.getStyle.mockRestore();
260
- });
261
- });
262
- });