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,12 +1,7 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
- import { toLonLat } from 'ol/proj';
3
2
  import { Map } from 'mapbox-gl';
4
- import Source from 'ol/source/Source';
5
- import OLLayer from 'ol/layer/Layer';
6
- import GeoJSON from 'ol/format/GeoJSON';
7
- import Layer from './Layer';
8
- import { getMapboxMapCopyrights, getMapboxStyleUrl } from '../../common/utils';
9
-
3
+ import { getMapboxMapCopyrights, getMapboxRender } from '../../common/utils';
4
+ import MapGlLayer from './MapGlLayer';
10
5
  /**
11
6
  * A class representing Mapboxlayer to display on BasicMap
12
7
  *
@@ -21,358 +16,94 @@ import { getMapboxMapCopyrights, getMapboxStyleUrl } from '../../common/utils';
21
16
  * @classproperty {ol/Map~Map} map - The map where the layer is displayed.
22
17
  * @extends {Layer}
23
18
  */
24
- export default class MapboxLayer extends Layer {
25
- /**
26
- * Constructor.
27
- *
28
- * @param {Object} options
29
- * @param {boolean} [options.preserveDrawingBuffer=false] If true able to export the canvas.
30
- * @param {number} [options.fadeDuration=300] Duration of the fade effect in ms.
31
- */
32
- constructor(options = {}) {
33
- const mbLayer = new OLLayer({
34
- source: new Source({}),
35
- render: (frameState) => {
36
- if (!this.mbMap) {
37
- // eslint-disable-next-line no-console
38
- console.warn("Mapbox map doesn't exist.");
39
- return null;
40
- }
41
- let changed = false;
42
- const canvas = this.mbMap.getCanvas();
43
- const { viewState } = frameState;
44
-
45
- const visible = this.olLayer.getVisible();
46
- if (this.renderState.visible !== visible) {
47
- canvas.style.display = visible ? 'block' : 'none';
48
- this.renderState.visible = visible;
49
- // Needed since mapbox-gl 1.9.0.
50
- // Without you don't see others ol layers on top.
51
- canvas.style.position = 'absolute';
52
- }
53
-
54
- const opacity = this.olLayer.getOpacity();
55
- if (this.renderState.opacity !== opacity) {
56
- canvas.style.opacity = opacity;
57
- this.renderState.opacity = opacity;
58
- }
59
-
60
- // adjust view parameters in mapbox
61
- const { rotation } = viewState;
62
- if (this.renderState.rotation !== rotation) {
63
- this.mbMap.rotateTo((-(rotation || 0) * 180) / Math.PI, {
64
- animate: false,
65
- });
66
- changed = true;
67
- this.renderState.rotation = rotation;
68
- }
69
-
70
- if (
71
- this.renderState.zoom !== viewState.zoom ||
72
- this.renderState.center[0] !== viewState.center[0] ||
73
- this.renderState.center[1] !== viewState.center[1]
74
- ) {
75
- this.mbMap.jumpTo({
76
- center: toLonLat(viewState.center),
77
- zoom: viewState.zoom - 1,
78
- animate: false,
79
- });
80
- changed = true;
81
- this.renderState.zoom = viewState.zoom;
82
- this.renderState.center = viewState.center;
83
- }
84
-
85
- const size = this.map.getSize();
86
- if (
87
- this.renderState.size[0] !== size[0] ||
88
- this.renderState.size[1] !== size[1]
89
- ) {
90
- changed = true;
91
- this.renderState.size = size;
92
- }
93
-
94
- // cancel the scheduled update & trigger synchronous redraw
95
- // see https://github.com/mapbox/mapbox-gl-js/issues/7893#issue-408992184
96
- // NOTE: THIS MIGHT BREAK WHEN UPDATING MAPBOX
97
- if (
98
- this.mbMap &&
99
- this.mbMap.style &&
100
- this.mbMap.isStyleLoaded() &&
101
- changed
102
- ) {
103
- try {
104
- if (this.mbMap._frame) {
105
- this.mbMap._frame.cancel();
106
- this.mbMap._frame = null;
107
- }
108
- this.mbMap._render();
109
- } catch (err) {
110
- // ignore render errors because it's probably related to
111
- // a render during an update of the style.
112
- // eslint-disable-next-line no-console
113
- console.warn(err);
114
- }
115
- }
116
-
117
- return this.mbMap.getContainer();
118
- },
119
- });
120
-
121
- super({
122
- ...options,
123
- olLayer: mbLayer,
124
- });
125
-
126
- /**
127
- * Url of the mapbox style.
128
- * @type {string}
129
- * @private
130
- */
131
- this.styleUrl = options.url;
132
-
19
+ export default class MapboxLayer extends MapGlLayer {
133
20
  /**
134
- * Api key for the url of the mapbox style.
135
- * If set to false, the apiKey is not required.
136
- * @type {string}
137
- * @private
138
- */
139
- this.apiKey = options.apiKey;
140
-
141
- /**
142
- * Name of the apiKey to set in the url request.
143
- * Default is 'key'.
144
- * @type {string}
145
- * @private
21
+ * Initialize the layer and listen to feature clicks.
22
+ * @param {ol/Map~Map} map
146
23
  */
147
- this.apiKeyName = options.apiKeyName || 'key';
148
-
149
- /** @ignore */
150
- this.updateAttribution = this.updateAttribution.bind(this);
151
- }
152
-
153
- /**
154
- * Initialize the layer and listen to feature clicks.
155
- * @param {ol/Map~Map} map
156
- */
157
- init(map) {
158
- super.init(map);
159
-
160
- if (!this.map || this.mbMap) {
161
- return;
24
+ attachToMap(map) {
25
+ super.attachToMap(map);
26
+ if (!this.map) {
27
+ return;
28
+ }
29
+ this.olListenersKeys.push(this.map.on('change:size', () => {
30
+ try {
31
+ if (this.mbMap) {
32
+ this.mbMap.resize();
33
+ }
34
+ }
35
+ catch (err) {
36
+ // ignore render errors
37
+ // eslint-disable-next-line no-console
38
+ console.warn(err);
39
+ }
40
+ }));
162
41
  }
163
-
164
42
  /**
165
- * The feature format.
166
- * @type {ol/format/GeoJSON}
43
+ * Create the mapbox map.
44
+ * @private
167
45
  */
168
- this.format = new GeoJSON({
169
- featureProjection: this.map.getView().getProjection(),
170
- });
171
-
172
- this.loadMbMap();
173
-
174
- this.olListenersKeys.push(
175
- this.map.on('change:size', () => {
176
- try {
177
- if (this.mbMap) {
46
+ loadMbMap() {
47
+ var _a;
48
+ // If the map hasn't been resized, the center could be [NaN,NaN].
49
+ // We set default good value for the mapbox map, to avoid the app crashes.
50
+ let [x, y] = ((_a = this.map) === null || _a === void 0 ? void 0 : _a.getView().getCenter()) || [];
51
+ if (!x || !y) {
52
+ x = 0;
53
+ y = 0;
54
+ }
55
+ // Options the last render run did happen. If something changes
56
+ // we have to render again
57
+ /** @ignore */
58
+ this.renderState = {
59
+ center: [x, y],
60
+ zoom: undefined,
61
+ rotation: undefined,
62
+ visible: undefined,
63
+ opacity: undefined,
64
+ size: [0, 0],
65
+ };
66
+ super.loadMbMap();
67
+ if (!this.mbMap) {
68
+ // mbMap could ne bull if the map is not in the dom yet.
69
+ return;
70
+ }
71
+ this.mbMap.once('load', () => {
72
+ var _a, _b;
73
+ if (!this.mbMap) {
74
+ return;
75
+ }
178
76
  this.mbMap.resize();
179
- }
180
- } catch (err) {
181
- // ignore render errors
182
- // eslint-disable-next-line no-console
183
- console.warn(err);
77
+ /** @ignore */
78
+ this.copyrights = getMapboxMapCopyrights(this.mbMap) || [];
79
+ // @ts-ignore
80
+ (_b = (_a = this.olLayer) === null || _a === void 0 ? void 0 : _a.getSource()) === null || _b === void 0 ? void 0 : _b.setAttributions(this.copyrights);
81
+ });
82
+ const mapboxCanvas = this.mbMap.getCanvas();
83
+ if (mapboxCanvas) {
84
+ if (this.options.tabIndex) {
85
+ mapboxCanvas.setAttribute('tabindex', `${this.options.tabIndex}`);
86
+ }
87
+ else {
88
+ // With a tabIndex='-1' the mouse events works but the map is not focused when we click on it
89
+ // so we remove completely the tabIndex attribute.
90
+ mapboxCanvas.removeAttribute('tabindex');
91
+ }
184
92
  }
185
- }),
186
- );
187
- }
188
-
189
- /**
190
- * Terminate what was initialized in init function. Remove layer, events...
191
- */
192
- terminate() {
193
- if (this.mbMap) {
194
- this.mbMap.off('idle', this.updateAttribution);
195
- // Some asynchrone repaints are triggered even if the mbMap has been removed,
196
- // to avoid display of errors we set an empty function.
197
- this.mbMap.triggerRepaint = () => {};
198
- this.mbMap.remove();
199
- this.mbMap = null;
200
93
  }
201
- this.loaded = false;
202
- super.terminate();
203
- }
204
-
205
- /**
206
- * Returns a style URL with apiKey & apiKeyName infos.
207
- * @private
208
- */
209
- createStyleUrl() {
210
- return getMapboxStyleUrl(this.apiKey, this.apiKeyName, this.styleUrl);
211
- }
212
-
213
- /**
214
- * Create the mapbox map.
215
- * @private
216
- */
217
- loadMbMap() {
218
- this.olListenersKeys.push(
219
- this.map.on('change:target', () => {
220
- this.loadMbMap();
221
- }),
222
- );
223
-
224
- if (!this.map.getTargetElement()) {
225
- return;
94
+ getOlLayerRender() {
95
+ return getMapboxRender(this);
226
96
  }
227
-
228
- if (!this.visible) {
229
- // On next change of visibility we load the map
230
- this.olListenersKeys.push(
231
- this.once('change:visible', () => {
232
- this.loadMbMap();
233
- }),
234
- );
235
- return;
97
+ // eslint-disable-next-line class-methods-use-this
98
+ getMapboxMapClass() {
99
+ return Map;
236
100
  }
237
-
238
- // If the map hasn't been resized, the center could be [NaN,NaN].
239
- // We set default good value for the mapbox map, to avoid the app crashes.
240
- let [x, y] = this.map.getView().getCenter();
241
- if (!x || !y) {
242
- x = 0;
243
- y = 0;
244
- }
245
-
246
- const container = document.createElement('div');
247
- container.style.position = 'absolute';
248
- container.style.width = '100%';
249
- container.style.height = '100%';
250
-
251
101
  /**
252
- * A mapbox map
253
- * @type {mapboxgl.Map}
102
+ * Create a copy of the MapboxLayer.
103
+ * @param {Object} newOptions Options to override
104
+ * @return {MapboxLayer} A MapboxLayer
254
105
  */
255
- this.mbMap = new Map({
256
- style: this.createStyleUrl(),
257
- container,
258
- interactive: false,
259
- trackResize: false,
260
- attributionControl: false,
261
- ...(this.options.mapOptions || {}),
262
- });
263
-
264
- // Options the last render run did happen. If something changes
265
- // we have to render again
266
- /** @ignore */
267
- this.renderState = {
268
- center: [x, y],
269
- zoom: null,
270
- rotation: null,
271
- visible: null,
272
- opacity: null,
273
- size: [0, 0],
274
- };
275
-
276
- this.mbMap.once('load', () => {
277
- this.mbMap.resize();
278
- /**
279
- * Is the map loaded.
280
- * @type {boolean}
281
- */
282
- this.loaded = true;
283
-
284
- /** @ignore */
285
- this.copyrights = getMapboxMapCopyrights(this.mbMap) || [];
286
-
287
- this.olLayer.getSource().setAttributions(this.copyrights);
288
-
289
- this.dispatchEvent({
290
- type: 'load',
291
- target: this,
292
- });
293
- });
294
-
295
- const mapboxCanvas = this.mbMap.getCanvas();
296
- if (mapboxCanvas) {
297
- if (this.options.tabIndex) {
298
- mapboxCanvas.setAttribute('tabindex', this.options.tabIndex);
299
- } else {
300
- // With a tabIndex='-1' the mouse events works but the map is not focused when we click on it
301
- // so we remove completely the tabIndex attribute.
302
- mapboxCanvas.removeAttribute('tabindex');
303
- }
304
- }
305
-
306
- this.mbMap.on('idle', this.updateAttribution);
307
- }
308
-
309
- /**
310
- * Update attributions of the source.
311
- * @private
312
- */
313
- updateAttribution(evt) {
314
- const newAttributions = getMapboxMapCopyrights(evt.target) || [];
315
- if (this.copyrights.toString() !== newAttributions.toString()) {
316
- this.copyrights = newAttributions;
317
- this.olLayer.getSource().setAttributions(newAttributions);
106
+ clone(newOptions) {
107
+ return new MapboxLayer(Object.assign(Object.assign({}, this.options), newOptions));
318
108
  }
319
- }
320
-
321
- /**
322
- * Request feature information for a given coordinate.
323
- * @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
324
- * @param {Object} options A [mapboxgl.Map#queryrenderedfeatures](https://docs.mapbox.com/mapbox-gl-js/api/map/#map#queryrenderedfeatures) options parameter.
325
- * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate. The original Mapbox feature is available as a property named 'mapboxFeature'.
326
- */
327
- getFeatureInfoAtCoordinate(coordinate, options) {
328
- // Ignore the getFeatureInfo until the mapbox map is loaded
329
- if (
330
- !options ||
331
- !this.format ||
332
- !this.mbMap ||
333
- !this.mbMap.isStyleLoaded()
334
- ) {
335
- return Promise.resolve({ coordinate, features: [], layer: this });
336
- }
337
-
338
- let pixel = coordinate && this.mbMap.project(toLonLat(coordinate));
339
-
340
- if (this.hitTolerance) {
341
- const { x, y } = pixel;
342
- pixel = [
343
- { x: x - this.hitTolerance, y: y - this.hitTolerance },
344
- { x: x + this.hitTolerance, y: y + this.hitTolerance },
345
- ];
346
- }
347
-
348
- // At this point we get GeoJSON Mapbox feature, we transform it to an OpenLayers
349
- // feature to be consistent with other layers.
350
- const features = this.mbMap
351
- .queryRenderedFeatures(pixel, options)
352
- .map((feature) => {
353
- const olFeature = this.format.readFeature(feature);
354
- if (olFeature) {
355
- // We save the original mapbox feature to avoid losing informations
356
- // potentially needed for other functionnality like highlighting
357
- // (id, layer id, source, sourceLayer ...)
358
- olFeature.set('mapboxFeature', feature);
359
- }
360
- return olFeature;
361
- });
362
-
363
- return Promise.resolve({
364
- layer: this,
365
- features,
366
- coordinate,
367
- });
368
- }
369
-
370
- /**
371
- * Create a copy of the MapboxLayer.
372
- * @param {Object} newOptions Options to override
373
- * @return {MapboxLayer} A MapboxLayer
374
- */
375
- clone(newOptions) {
376
- return new MapboxLayer({ ...this.options, ...newOptions });
377
- }
378
109
  }
@@ -0,0 +1,129 @@
1
+ import { Feature, Map } from 'ol';
2
+ import { Coordinate } from 'ol/coordinate';
3
+ import { ObjectEvent } from 'ol/Object';
4
+ import { AnyMapboxLayer, LayerGetFeatureInfoResponse } from '../../types';
5
+ import Layer, { OlLayerOptions } from './Layer';
6
+ export type MapboxStyleLayerOptions = OlLayerOptions & {
7
+ beforeId?: string;
8
+ mapboxLayer?: AnyMapboxLayer;
9
+ styleLayer?: {
10
+ [key: string]: any;
11
+ };
12
+ styleLayers?: {
13
+ [key: string]: any;
14
+ }[];
15
+ styleLayersFilter?: FilterFunction;
16
+ filters?: FilterFunction | {
17
+ [key: string]: any;
18
+ }[];
19
+ featureInfoFilter?: FilterFunction;
20
+ queryRenderedLayersFilter?: FilterFunction;
21
+ };
22
+ export type StyleLayer = {
23
+ id?: string;
24
+ [key: string]: any;
25
+ };
26
+ /**
27
+ * Layer for visualizing a specific set of layer from a MapboxLayer.
28
+ *
29
+ * @example
30
+ * import { MapboxLayer, MapboxStyleLayer } from 'mobility-toolbox-js/ol';
31
+ *
32
+ * const mapboxLayer = new MapboxLayer({
33
+ * url: 'https://maps.geops.io/styles/travic_v2/style.json?key=[yourApiKey]',
34
+ * });
35
+ *
36
+ * const layer = new MapboxStyleLayer({
37
+ * mapboxLayer: mapboxLayer,
38
+ * styleLayersFilter: () => {},
39
+ * });
40
+ *
41
+ * @classproperty {ol/Map~Map} map - The map where the layer is displayed.
42
+ * @extends {Layer}
43
+ */
44
+ declare class MapboxStyleLayer extends Layer {
45
+ beforeId?: string;
46
+ mapboxLayer?: AnyMapboxLayer;
47
+ styleLayersFilter?: FilterFunction;
48
+ featureInfoFilter?: FilterFunction;
49
+ queryRenderedLayersFilter?: FilterFunction;
50
+ selectedFeatures?: Feature[];
51
+ highlightedFeatures?: Feature[];
52
+ styleLayer?: StyleLayer;
53
+ styleLayers: StyleLayer[];
54
+ addDynamicFilters?: () => void;
55
+ /**
56
+ * Constructor.
57
+ *
58
+ * @param {Object} options
59
+ * @param {MapboxLayer} [options.mapboxLayer] The MapboxLayer to use.
60
+ * @param {Function} [options.styleLayersFilter] Filter function to decide which style layer to display.
61
+ */
62
+ constructor(options: MapboxStyleLayerOptions);
63
+ /**
64
+ * Initialize the layer.
65
+ * @param {ol/Map~Map} map the mapbox map.
66
+ * @override
67
+ */
68
+ attachToMap(map: Map): void;
69
+ /**
70
+ * Terminate the layer.
71
+ * @override
72
+ */
73
+ detachFromMap(): void;
74
+ /** @ignore */
75
+ addStyleLayers(): void;
76
+ /** @ignore */
77
+ removeStyleLayers(): void;
78
+ /**
79
+ * On Mapbox map load callback function. Add style layers and dynaimc filters.
80
+ * @ignore
81
+ */
82
+ onLoad(): void;
83
+ /**
84
+ * Request feature information for a given coordinate.
85
+ * @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
86
+ * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate.
87
+ */
88
+ getFeatureInfoAtCoordinate(coordinate: Coordinate): Promise<LayerGetFeatureInfoResponse>;
89
+ /**
90
+ * Set filter that determines which features should be rendered in a style layer.
91
+ * @param {mapboxgl.filter} filter Determines which features should be rendered in a style layer.
92
+ */
93
+ setFilter(filter: {
94
+ [key: string]: any;
95
+ }): void;
96
+ /**
97
+ * Set if features are hovered or not.
98
+ * @param {Array<ol/Feature~Feature>} features
99
+ * @param {boolean} state Is the feature hovered
100
+ * @private
101
+ */
102
+ setHoverState(features: Feature[], state: boolean): void;
103
+ /**
104
+ * Select a list of features.
105
+ * @param {Array<ol/Feature~Feature>} [features=[]] Features to select.
106
+ * @private
107
+ */
108
+ select(features?: Feature[]): void;
109
+ /**
110
+ * Highlight a list of features.
111
+ * @param {Array<ol/Feature~Feature>} [features=[]] Features to highlight.
112
+ * @private
113
+ */
114
+ highlight(features?: Feature[]): void;
115
+ /**
116
+ * Apply visibility to style layers that fits the styleLayersFilter function.
117
+ * @param {Event} evt Layer's event that has called the function.
118
+ * @private
119
+ */
120
+ applyLayoutVisibility(evt?: ObjectEvent): void;
121
+ /**
122
+ * Create a copy of the MapboxStyleLayer.
123
+ * @param {Object} newOptions Options to override.
124
+ * @return {MapboxStyleLayer} A MapboxStyleLayer.
125
+ */
126
+ clone(newOptions: MapboxStyleLayerOptions): MapboxStyleLayer;
127
+ }
128
+ export default MapboxStyleLayer;
129
+ //# sourceMappingURL=MapboxStyleLayer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MapboxStyleLayer.d.ts","sourceRoot":"","sources":["../../../src/ol/layers/MapboxStyleLayer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,IAAI,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,KAAK,EAAE,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEhD,MAAM,MAAM,uBAAuB,GAAG,cAAc,GAAG;IACrD,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,cAAc,CAAC;IAC7B,UAAU,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,CAAC;IACpC,WAAW,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,EAAE,CAAC;IACvC,iBAAiB,CAAC,EAAE,cAAc,CAAC;IACnC,OAAO,CAAC,EAAE,cAAc,GAAG;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,EAAE,CAAC;IACpD,iBAAiB,CAAC,EAAE,cAAc,CAAC;IACnC,yBAAyB,CAAC,EAAE,cAAc,CAAC;CAC5C,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,cAAM,gBAAiB,SAAQ,KAAK;IAClC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB,WAAW,CAAC,EAAE,cAAc,CAAC;IAE7B,iBAAiB,CAAC,EAAE,cAAc,CAAC;IAEnC,iBAAiB,CAAC,EAAE,cAAc,CAAC;IAEnC,yBAAyB,CAAC,EAAE,cAAc,CAAC;IAE3C,gBAAgB,CAAC,EAAE,OAAO,EAAE,CAAC;IAE7B,mBAAmB,CAAC,EAAE,OAAO,EAAE,CAAC;IAEhC,UAAU,CAAC,EAAE,UAAU,CAAC;IAExB,WAAW,EAAE,UAAU,EAAE,CAAC;IAE1B,iBAAiB,CAAC,EAAE,MAAM,IAAI,CAAC;IAE/B;;;;;;OAMG;gBACS,OAAO,EAAE,uBAAuB;IA6F5C;;;;OAIG;IACH,WAAW,CAAC,GAAG,EAAE,GAAG;IAoDpB;;;OAGG;IACH,aAAa;IASb,cAAc;IACd,cAAc;IAgBd,cAAc;IACd,iBAAiB;IAcjB;;;OAGG;IACH,MAAM;IAmBN;;;;OAIG;IACH,0BAA0B,CACxB,UAAU,EAAE,UAAU,GACrB,OAAO,CAAC,2BAA2B,CAAC;IA4CvC;;;OAGG;IACH,SAAS,CAAC,MAAM,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE;IAcxC;;;;;OAKG;IACH,aAAa,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,OAAO;IAmCjD;;;;OAIG;IACH,MAAM,CAAC,QAAQ,GAAE,OAAO,EAAO;IAM/B;;;;OAIG;IACH,SAAS,CAAC,QAAQ,GAAE,OAAO,EAAO;IAkBlC;;;;OAIG;IAEH,qBAAqB,CAAC,GAAG,CAAC,EAAE,WAAW;IAwCvC;;;;OAIG;IACH,KAAK,CAAC,UAAU,EAAE,uBAAuB;CAG1C;AAED,eAAe,gBAAgB,CAAC"}