mobility-toolbox-js 2.0.1-beta.13 → 2.2.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/api/RealtimeAPI.d.ts +281 -0
  2. package/api/RealtimeAPI.d.ts.map +1 -0
  3. package/api/RealtimeAPI.js +490 -0
  4. package/api/RoutingAPI.d.ts +37 -0
  5. package/api/RoutingAPI.d.ts.map +1 -0
  6. package/api/RoutingAPI.js +32 -12
  7. package/api/StopsAPI.d.ts +38 -0
  8. package/api/StopsAPI.d.ts.map +1 -0
  9. package/api/StopsAPI.js +33 -9
  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/typedefs.js +75 -0
  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 +54 -27
  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 +330 -164
  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 -4
  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 +779 -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/realtimeDelayStyle.d.ts +12 -0
  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 +52 -0
  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 +26 -15
  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 +67 -37
  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 +17 -5
  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 +12 -5
  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 -15
  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 +34 -15
  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 +8 -6
  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 +44 -25
  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 -1
  130. package/mapbox/index.d.ts +6 -0
  131. package/mapbox/index.d.ts.map +1 -0
  132. package/mapbox/index.js +5 -4
  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 +99 -55
  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 -2
  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 +26074 -16501
  152. package/mbt.js.map +4 -4
  153. package/mbt.min.js +205 -126
  154. package/mbt.min.js.map +4 -4
  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 +62 -36
  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 +601 -357
  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 +30 -8
  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 -3
  167. package/ol/index.d.ts +6 -0
  168. package/ol/index.d.ts.map +1 -0
  169. package/ol/index.js +5 -5
  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 +163 -77
  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 +99 -193
  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 +362 -171
  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 +30 -135
  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 +72 -48
  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 +34 -18
  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 +84 -34
  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 -8
  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 +26 -24
  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 +40 -39
  206. package/ol/styles/index.d.ts +3 -0
  207. package/ol/styles/index.d.ts.map +1 -0
  208. package/ol/styles/index.js +2 -2
  209. package/package.json +81 -133
  210. package/setupTests.d.ts +2 -0
  211. package/setupTests.d.ts.map +1 -0
  212. package/setupTests.js +26 -0
  213. package/types/common.d.ts +122 -0
  214. package/types/index.d.ts +13 -0
  215. package/types/realtime.d.ts +320 -0
  216. package/types/routing.d.ts +206 -0
  217. package/types/stops.d.ts +143 -0
  218. package/README.md +0 -23
  219. package/api/RoutingAPI.test.js +0 -25
  220. package/api/StopsAPI.test.js +0 -22
  221. package/api/TralisAPI.js +0 -359
  222. package/api/TralisAPI.test.js +0 -67
  223. package/api/TralisAPIUtils.js +0 -43
  224. package/common/Tracker.js +0 -93
  225. package/common/api/HttpAPI.test.js +0 -50
  226. package/common/api/WebSocketAPI.test.js +0 -311
  227. package/common/controls/Control.js +0 -81
  228. package/common/controls/Control.test.js +0 -87
  229. package/common/layers/Layer.js +0 -213
  230. package/common/layers/Layer.test.js +0 -526
  231. package/common/mixins/CopyrightMixin.js +0 -24
  232. package/common/mixins/SearchMixin.js +0 -110
  233. package/common/mixins/TralisLayerMixin.js +0 -479
  234. package/common/styles/trackerDefaultStyle.js +0 -197
  235. package/common/styles/trackerDelayStyle.js +0 -8
  236. package/common/styles/trackerSimpleStyle.js +0 -18
  237. package/common/trackerConfig.js +0 -152
  238. package/common/trackerConfig.test.js +0 -23
  239. package/common/utils/createTrackerFilters.js +0 -56
  240. package/common/utils/createTrackerFilters.test.js +0 -79
  241. package/common/utils/getMapboxMapCopyrights.test.js +0 -40
  242. package/common/utils/getMapboxStyleUrl.js +0 -22
  243. package/common/utils/removeDuplicate.test.js +0 -19
  244. package/common/utils/timeUtils.test.js +0 -10
  245. package/mapbox/layers/Layer.test.js +0 -182
  246. package/mapbox/layers/TralisLayer.js +0 -182
  247. package/mapbox/layers/TralisLayer.test.js +0 -38
  248. package/mapbox/utils.js +0 -32
  249. package/ol/controls/CopyrightControl.test.js +0 -165
  250. package/ol/controls/RoutingControl.test.js +0 -151
  251. package/ol/controls/StopFinderControl.test.js +0 -48
  252. package/ol/layers/Layer.test.js +0 -174
  253. package/ol/layers/MapboxLayer.test.js +0 -160
  254. package/ol/layers/MapboxStyleLayer.test.js +0 -231
  255. package/ol/layers/RoutingLayer.test.js +0 -40
  256. package/ol/layers/TralisLayer.js +0 -185
  257. package/ol/layers/TralisLayer.test.js +0 -79
  258. package/ol/layers/VectorLayer.test.js +0 -87
  259. package/ol/layers/WMSLayer.test.js +0 -76
@@ -1,187 +1,378 @@
1
- import Layer from "./Layer";
1
+ import Layer from './Layer';
2
+ /**
3
+ * Layer for visualizing a specific set of layer from a MapboxLayer.
4
+ *
5
+ * @example
6
+ * import { MapboxLayer, MapboxStyleLayer } from 'mobility-toolbox-js/ol';
7
+ *
8
+ * const mapboxLayer = new MapboxLayer({
9
+ * url: 'https://maps.geops.io/styles/travic_v2/style.json?key=[yourApiKey]',
10
+ * });
11
+ *
12
+ * const layer = new MapboxStyleLayer({
13
+ * mapboxLayer: mapboxLayer,
14
+ * styleLayersFilter: () => {},
15
+ * });
16
+ *
17
+ * @classproperty {ol/Map~Map} map - The map where the layer is displayed.
18
+ * @extends {Layer}
19
+ */
2
20
  class MapboxStyleLayer extends Layer {
3
- constructor(options = {}) {
4
- super(options);
5
- this.mapboxLayer = options.mapboxLayer;
6
- this.disabled = false;
7
- this.styleLayersFilter = options.styleLayersFilter;
8
- this.beforeId = options.beforeId;
9
- this.featureInfoFilter = options.featureInfoFilter || ((obj) => obj);
10
- this.queryRenderedLayersFilter = options.queryRenderedLayersFilter;
11
- this.highlightedFeatures = [];
12
- this.selectedFeatures = [];
13
- this.styleLayers = (options.styleLayer ? [options.styleLayer] : options.styleLayers) || [];
14
- this.addStyleLayers = this.addStyleLayers.bind(this);
15
- this.onLoad = this.onLoad.bind(this);
16
- if (options.filters) {
17
- this.addDynamicFilters = () => {
18
- this.setFilter(typeof options.filters === "function" ? options.filters(this) : options.filters);
19
- };
20
- }
21
- if (!this.styleLayersFilter && this.styleLayers) {
22
- const ids = this.styleLayers.map((s) => s.id);
23
- this.styleLayersFilter = (styleLayer) => ids.includes(styleLayer.id);
24
- }
25
- }
26
- attachToMap(map) {
27
- if (!this.mapboxLayer.map) {
28
- this.mapboxLayer.attachToMap(map);
29
- }
30
- super.attachToMap(map);
31
- if (!this.map) {
32
- return;
33
- }
34
- const { mbMap } = this.mapboxLayer;
35
- if (!mbMap) {
36
- this.olListenersKeys.push(this.map.on("change:target", () => {
37
- this.attachToMap(map);
38
- }));
39
- return;
40
- }
41
- if (this.mapboxLayer.loaded || mbMap.isStyleLoaded() || mbMap.loaded()) {
42
- this.onLoad();
43
- } else {
44
- mbMap.once("load", this.onLoad);
45
- }
46
- this.olListenersKeys.push(this.on("change:visible", (evt) => {
47
- this.applyLayoutVisibility(evt);
48
- }));
49
- this.olListenersKeys.push(this.mapboxLayer.on("load", () => {
50
- this.onLoad();
51
- }));
52
- }
53
- detachFromMap(map) {
54
- const { mbMap } = this.mapboxLayer;
55
- if (mbMap) {
56
- mbMap.off("load", this.onLoad);
57
- this.removeStyleLayers();
21
+ /**
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.
27
+ */
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
+ }
58
106
  }
59
- super.detachFromMap(map);
60
- }
61
- addStyleLayers() {
62
- const { mbMap } = this.mapboxLayer;
63
- if (!mbMap) {
64
- return;
107
+ /**
108
+ * Initialize the layer.
109
+ * @param {ol/Map~Map} map the mapbox map.
110
+ * @override
111
+ */
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
+ }));
65
154
  }
66
- this.styleLayers.forEach((styleLayer) => {
67
- const { id, source } = styleLayer;
68
- if (mbMap.getSource(source) && !mbMap.getLayer(id)) {
69
- mbMap.addLayer(styleLayer, this.beforeId);
70
- }
71
- });
72
- this.applyLayoutVisibility();
73
- }
74
- removeStyleLayers() {
75
- const { mbMap } = this.mapboxLayer;
76
- if (!mbMap) {
77
- return;
155
+ /**
156
+ * Terminate the layer.
157
+ * @override
158
+ */
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();
78
167
  }
79
- this.styleLayers.forEach((styleLayer) => {
80
- if (mbMap.getLayer(styleLayer.id)) {
81
- mbMap.removeLayer(styleLayer.id);
82
- }
83
- });
84
- }
85
- onLoad() {
86
- this.addStyleLayers();
87
- if (this.addDynamicFilters) {
88
- this.addDynamicFilters();
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();
89
183
  }
90
- const { mbMap } = this.mapboxLayer;
91
- const style = mbMap.getStyle();
92
- if (style && this.styleLayersFilter) {
93
- const styles = style.layers.filter(this.styleLayersFilter);
94
- this.disabled = !styles.length;
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
+ });
95
197
  }
96
- }
97
- getFeatureInfoAtCoordinate(coordinate) {
98
- const { mbMap } = this.mapboxLayer;
99
- if (!mbMap || !mbMap.isStyleLoaded()) {
100
- return Promise.resolve({ coordinate, features: [], layer: this });
198
+ /**
199
+ * On Mapbox map load callback function. Add style layers and dynaimc filters.
200
+ * @ignore
201
+ */
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
+ }
101
218
  }
102
- let layers = this.styleLayers || [];
103
- if (this.styleLayersFilter) {
104
- layers = mbMap.getStyle().layers.filter(this.styleLayersFilter);
219
+ /**
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.
223
+ */
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
+ });
105
258
  }
106
- if (this.queryRenderedLayersFilter) {
107
- layers = mbMap.getStyle().layers.filter(this.queryRenderedLayersFilter);
259
+ /**
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.
262
+ */
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
+ });
108
275
  }
109
- return this.mapboxLayer.getFeatureInfoAtCoordinate(coordinate, {
110
- layers: layers.map((layer) => layer && layer.id),
111
- validate: false
112
- }).then((featureInfo) => {
113
- const features = featureInfo.features.filter((feature) => this.featureInfoFilter(feature, this.map.getView().getResolution()));
114
- this.highlight(features);
115
- return { ...featureInfo, features, layer: this };
116
- });
117
- }
118
- setFilter(filter) {
119
- const { mbMap } = this.mapboxLayer;
120
- if (!mbMap) {
121
- return;
276
+ /**
277
+ * Set if features are hovered or not.
278
+ * @param {Array<ol/Feature~Feature>} features
279
+ * @param {boolean} state Is the feature hovered
280
+ * @private
281
+ */
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
+ });
122
306
  }
123
- this.styleLayers.forEach(({ id }) => {
124
- if (mbMap.getLayer(id)) {
125
- mbMap.setFilter(id, filter);
126
- }
127
- });
128
- }
129
- setHoverState(features, state) {
130
- const { mbMap } = this.mapboxLayer;
131
- if (!features || !mbMap) {
132
- return;
307
+ /**
308
+ * Select a list of features.
309
+ * @param {Array<ol/Feature~Feature>} [features=[]] Features to select.
310
+ * @private
311
+ */
312
+ select(features = []) {
313
+ this.setHoverState(this.selectedFeatures || [], false);
314
+ this.selectedFeatures = features;
315
+ this.setHoverState(this.selectedFeatures || [], true);
133
316
  }
134
- features.forEach((feature) => {
135
- const { source, sourceLayer } = feature.get("mapboxFeature") || {};
136
- if (!source && !sourceLayer || !feature.getId()) {
137
- if (!feature.getId()) {
138
- 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());
139
- }
140
- return;
141
- }
142
- mbMap.setFeatureState({
143
- id: feature.getId(),
144
- source,
145
- sourceLayer
146
- }, { hover: state });
147
- });
148
- }
149
- select(features = []) {
150
- this.setHoverState(this.selectedFeatures, false);
151
- this.selectedFeatures = features;
152
- this.setHoverState(this.selectedFeatures, true);
153
- }
154
- highlight(features = []) {
155
- const filtered = this.highlightedFeatures.filter((feature) => !this.selectedFeatures.map((feat) => feat.getId()).includes(feature.getId()));
156
- this.setHoverState(filtered, false);
157
- this.highlightedFeatures = features;
158
- this.setHoverState(this.highlightedFeatures, true);
159
- }
160
- applyLayoutVisibility(evt) {
161
- const { visible } = this;
162
- const { mbMap } = this.mapboxLayer;
163
- const filterFunc = this.styleLayersFilter;
164
- if (!mbMap) {
165
- return;
317
+ /**
318
+ * Highlight a list of features.
319
+ * @param {Array<ol/Feature~Feature>} [features=[]] Features to highlight.
320
+ * @private
321
+ */
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);
166
333
  }
167
- const style = mbMap.getStyle();
168
- if (!style) {
169
- return;
334
+ /**
335
+ * Apply visibility to style layers that fits the styleLayersFilter function.
336
+ * @param {Event} evt Layer's event that has called the function.
337
+ * @private
338
+ */
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;
346
+ }
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
+ }
367
+ }
170
368
  }
171
- if (filterFunc) {
172
- const visibilityValue = visible ? "visible" : "none";
173
- for (let i = 0; i < style.layers.length; i += 1) {
174
- const styleLayer = style.layers[i];
175
- if (filterFunc(styleLayer)) {
176
- if (mbMap.getLayer(styleLayer.id)) {
177
- mbMap.setLayoutProperty(styleLayer.id, "visibility", visibilityValue);
178
- }
179
- }
180
- }
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));
181
376
  }
182
- }
183
- clone(newOptions) {
184
- return new MapboxStyleLayer({ ...this.options, ...newOptions });
185
- }
186
377
  }
187
378
  export default MapboxStyleLayer;
@@ -0,0 +1,28 @@
1
+ import { Map } from 'maplibre-gl';
2
+ import MapGlLayer, { MapGlLayerOptions } from './MapGlLayer';
3
+ /**
4
+ * A class representing MaplibreLayer to display on BasicMap
5
+ *
6
+ * @example
7
+ * import { MaplibreLayer } from 'mobility-toolbox-js/ol';
8
+ *
9
+ * const layer = new MaplibreLayer({
10
+ * url: 'https://maps.geops.io/styles/travic_v2/style.json',
11
+ * apikey: 'yourApiKey',
12
+ * });
13
+ *
14
+ * @classproperty {ol/Map~Map} map - The map where the layer is displayed.
15
+ * @extends {Layer}
16
+ */
17
+ export default class MaplibreLayer extends MapGlLayer {
18
+ mbMap?: maplibregl.Map;
19
+ getOlLayerRender(): import("ol/layer/Layer").RenderFunction;
20
+ getMapboxMapClass(): typeof Map;
21
+ /**
22
+ * Create a copy of the MapboxLayer.
23
+ * @param {Object} newOptions Options to override
24
+ * @return {MapboxLayer} A MapboxLayer
25
+ */
26
+ clone(newOptions: MapGlLayerOptions): MaplibreLayer;
27
+ }
28
+ //# sourceMappingURL=MaplibreLayer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MaplibreLayer.d.ts","sourceRoot":"","sources":["../../../src/ol/layers/MaplibreLayer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAElC,OAAO,UAAU,EAAE,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAE7D;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,UAAU;IACnD,KAAK,CAAC,EAAE,UAAU,CAAC,GAAG,CAAC;IAEvB,gBAAgB;IAKhB,iBAAiB;IAIjB;;;;OAIG;IACH,KAAK,CAAC,UAAU,EAAE,iBAAiB;CAGpC"}