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