mobility-toolbox-js 2.0.1-beta.13 → 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 (259) 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 +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 +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/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 +26061 -16500
  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 +11 -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
@@ -0,0 +1,776 @@
1
+ /* eslint-disable no-empty-function,@typescript-eslint/no-empty-function */
2
+ /* eslint-disable no-useless-constructor,@typescript-eslint/no-useless-constructor */
3
+ /* eslint-disable no-unused-vars,@typescript-eslint/no-unused-vars */
4
+ /* eslint-disable class-methods-use-this */
5
+ /* eslint-disable max-classes-per-file */
6
+ import { buffer, containsCoordinate, intersects } from 'ol/extent';
7
+ import { unByKey } from 'ol/Observable';
8
+ import GeoJSON from 'ol/format/GeoJSON';
9
+ import debounce from 'lodash.debounce';
10
+ import throttle from 'lodash.throttle';
11
+ import { fromLonLat } from 'ol/proj';
12
+ import realtimeDefaultStyle from '../styles/realtimeDefaultStyle';
13
+ import { RealtimeAPI, RealtimeModes } from '../../api';
14
+ import renderTrajectories from '../utils/renderTrajectories';
15
+ import * as realtimeConfig from '../utils/realtimeConfig';
16
+ /**
17
+ * RealtimeLayerInterface.
18
+ */
19
+ export class RealtimeLayerInterface {
20
+ /**
21
+ * Start the clock.
22
+ */
23
+ start() { }
24
+ /**
25
+ * Stop the clock.
26
+ */
27
+ stop() { }
28
+ /**
29
+ * Set the Realtime api's bbox.
30
+ *
31
+ * @param {Array<number>} extent Extent to request, [minX, minY, maxX, maxY, zoom].
32
+ * @param {number} zoom Zoom level to request. Must be an integer.
33
+ */
34
+ setBbox(extent, zoom) { }
35
+ /**
36
+ * Render the trajectories
37
+ */
38
+ renderTrajectories() { }
39
+ /**
40
+ * Request the stopSequence and the fullTrajectory informations for a vehicle.
41
+ *
42
+ * @param {string} id The vehicle identifier (the train_id property).
43
+ * @param {RealtimeMode} mode The mode to request. If not defined, the layer´s mode propetrty will be used.
44
+ * @return {Promise<{stopSequence: StopSequence, fullTrajectory: FullTrajectory>} A promise that will be resolved with the trajectory informations.
45
+ */
46
+ getTrajectoryInfos(id, mode) { }
47
+ }
48
+ /**
49
+ * Mixin for RealtimeLayerInterface.
50
+ *
51
+ * @param {Class} Base A class to extend with {RealtimeLayerInterface} functionnalities.
52
+ * @return {Class} A class that implements {RealtimeLayerInterface} class and extends Base;
53
+ * @private
54
+ */
55
+ function RealtimeLayerMixin(Base) {
56
+ // @ts-ignore
57
+ return class Mixin extends Base {
58
+ constructor(options) {
59
+ super(Object.assign({ hitTolerance: 10 }, options));
60
+ this.debug = options.debug || false;
61
+ this.mode = options.mode || RealtimeModes.TOPOGRAPHIC;
62
+ this.api = options.api || new RealtimeAPI(options);
63
+ this.tenant = options.tenant || ''; // sbb,sbh or sbm
64
+ this.minZoomInterpolation = options.minZoomInterpolation || 8; // Min zoom level from which trains positions are not interpolated.
65
+ this.format = new GeoJSON();
66
+ this.onStart = options.onStart;
67
+ this.onStop = options.onStop;
68
+ // MOTs by zoom
69
+ const allMots = [
70
+ 'tram',
71
+ 'subway',
72
+ 'rail',
73
+ 'bus',
74
+ 'ferry',
75
+ 'cablecar',
76
+ 'gondola',
77
+ 'funicular',
78
+ 'coach',
79
+ ];
80
+ const onlyRail = ['rail'];
81
+ const withoutCable = ['tram', 'subway', 'rail', 'bus'];
82
+ // Server will block non train before zoom 9
83
+ this.motsByZoom = options.motsByZoom || [
84
+ onlyRail,
85
+ onlyRail,
86
+ onlyRail,
87
+ onlyRail,
88
+ onlyRail,
89
+ onlyRail,
90
+ onlyRail,
91
+ onlyRail,
92
+ onlyRail,
93
+ withoutCable,
94
+ withoutCable,
95
+ allMots,
96
+ allMots,
97
+ allMots,
98
+ allMots,
99
+ ];
100
+ this.getMotsByZoom = (zoom) => {
101
+ return ((options.getMotsByZoom &&
102
+ options.getMotsByZoom(zoom, this.motsByZoom)) ||
103
+ this.motsByZoom[zoom] ||
104
+ this.motsByZoom[this.motsByZoom.length - 1]);
105
+ };
106
+ // Generalization levels by zoom
107
+ this.generalizationLevelByZoom = options.generalizationLevelByZoom || [
108
+ 5, 5, 5, 5, 5, 5, 5, 5, 10, 30, 30, 100, 100, 100,
109
+ ];
110
+ this.getGeneralizationLevelByZoom = (zoom) => {
111
+ return ((options.getGeneralizationLevelByZoom &&
112
+ options.getGeneralizationLevelByZoom(zoom, this.generalizationLevelByZoom)) ||
113
+ this.generalizationLevelByZoom[zoom]);
114
+ };
115
+ // Render time interval by zoom
116
+ this.renderTimeIntervalByZoom = options.renderTimeIntervalByZoom || [
117
+ 100000, 50000, 40000, 30000, 20000, 15000, 10000, 5000, 2000, 1000, 400,
118
+ 300, 250, 180, 90, 60, 50, 50, 50, 50, 50,
119
+ ];
120
+ this.getRenderTimeIntervalByZoom = (zoom) => {
121
+ return ((options.getRenderTimeIntervalByZoom &&
122
+ options.getRenderTimeIntervalByZoom(zoom, this.renderTimeIntervalByZoom)) ||
123
+ this.renderTimeIntervalByZoom[zoom]);
124
+ };
125
+ // This property will call api.setBbox on each movend event
126
+ this.isUpdateBboxOnMoveEnd = options.isUpdateBboxOnMoveEnd !== false;
127
+ // Define throttling and debounce render function
128
+ this.throttleRenderTrajectories = throttle(this.renderTrajectoriesInternal, 50, { leading: false, trailing: true });
129
+ this.debounceRenderTrajectories = debounce(this.renderTrajectoriesInternal, 50, { leading: true, trailing: true, maxWait: 5000 });
130
+ // Bind callbacks
131
+ this.onFeatureHover = this.onFeatureHover.bind(this);
132
+ this.onFeatureClick = this.onFeatureClick.bind(this);
133
+ this.renderTrajectoriesInternal =
134
+ this.renderTrajectoriesInternal.bind(this);
135
+ this.onTrajectoryMessage = this.onTrajectoryMessage.bind(this);
136
+ this.onDeleteTrajectoryMessage =
137
+ this.onDeleteTrajectoryMessage.bind(this);
138
+ this.onDocumentVisibilityChange =
139
+ this.onDocumentVisibilityChange.bind(this);
140
+ }
141
+ /**
142
+ * Define layer's properties.
143
+ *
144
+ * @ignore
145
+ */
146
+ defineProperties(options) {
147
+ const { style, speed, pixelRatio, hoverVehicleId, selectedVehicleId, filter, sort, time, live, canvas, styleOptions, mode, } = options;
148
+ let currCanvas = canvas;
149
+ let currSpeed = speed || 1;
150
+ let currTime = time || new Date();
151
+ let currMode = mode || RealtimeModes.TOPOGRAPHIC;
152
+ let currStyle = style || realtimeDefaultStyle;
153
+ super.defineProperties(options);
154
+ Object.defineProperties(this, {
155
+ isTrackerLayer: { value: true },
156
+ canvas: {
157
+ get: () => {
158
+ if (!currCanvas) {
159
+ currCanvas = document.createElement('canvas');
160
+ }
161
+ return currCanvas;
162
+ },
163
+ set: (cnvas) => {
164
+ currCanvas = cnvas;
165
+ },
166
+ },
167
+ /**
168
+ * Style function used to render a vehicle.
169
+ */
170
+ mode: {
171
+ get: () => currMode,
172
+ set: (newMode) => {
173
+ var _a, _b;
174
+ if (newMode === currMode) {
175
+ return;
176
+ }
177
+ currMode = newMode;
178
+ if ((_b = (_a = this.api) === null || _a === void 0 ? void 0 : _a.wsApi) === null || _b === void 0 ? void 0 : _b.open) {
179
+ this.stop();
180
+ this.start();
181
+ }
182
+ },
183
+ },
184
+ /**
185
+ * Style function used to render a vehicle.
186
+ */
187
+ style: {
188
+ get: () => currStyle,
189
+ set: (newStyle) => {
190
+ currStyle = newStyle;
191
+ // @ts-ignore function without parameters is defined in subclasses
192
+ this.renderTrajectories();
193
+ },
194
+ },
195
+ /**
196
+ * Custom options to pass as last parameter of the style function.
197
+ */
198
+ styleOptions: {
199
+ value: Object.assign(Object.assign({}, realtimeConfig), (styleOptions || {})),
200
+ },
201
+ /**
202
+ * Speed of the wheel of time.
203
+ * If live property is true. The speed is ignored.
204
+ */
205
+ speed: {
206
+ get: () => currSpeed,
207
+ set: (newSpeed) => {
208
+ currSpeed = newSpeed;
209
+ this.start();
210
+ },
211
+ },
212
+ /**
213
+ * Function to filter which vehicles to display.
214
+ */
215
+ filter: {
216
+ value: filter,
217
+ writable: true,
218
+ },
219
+ /**
220
+ * Function to sort the vehicles to display.
221
+ */
222
+ sort: {
223
+ value: sort,
224
+ writable: true,
225
+ },
226
+ /**
227
+ * If true. The layer will always use Date.now() on the next tick to render the trajectories.
228
+ * When true, setting the time property has no effect.
229
+ */
230
+ live: {
231
+ value: live === false ? live : true,
232
+ writable: true,
233
+ },
234
+ /**
235
+ * Time used to display the trajectories. Can be a Date or a number in ms representing a Date.
236
+ * If live property is true. The setter does nothing.
237
+ */
238
+ time: {
239
+ get: () => currTime,
240
+ set: (newTime) => {
241
+ currTime = newTime && newTime.getTime ? newTime : new Date(newTime);
242
+ // @ts-ignore function without parameters is defined in subclasses
243
+ this.renderTrajectories();
244
+ },
245
+ },
246
+ /**
247
+ * Keep track of which trajectories are stored.
248
+ */
249
+ trajectories: {
250
+ value: {},
251
+ writable: true,
252
+ },
253
+ /**
254
+ * Id of the hovered vehicle.
255
+ */
256
+ hoverVehicleId: {
257
+ value: hoverVehicleId,
258
+ writable: true,
259
+ },
260
+ /**
261
+ * Id of the selected vehicle.
262
+ */
263
+ selectedVehicleId: {
264
+ value: selectedVehicleId,
265
+ writable: true,
266
+ },
267
+ /**
268
+ * Id of the selected vehicle.
269
+ */
270
+ pixelRatio: {
271
+ value: pixelRatio ||
272
+ (typeof window !== 'undefined' ? window.devicePixelRatio : 1),
273
+ writable: true,
274
+ },
275
+ /**
276
+ * If true, encapsulates the renderTrajectories calls in a requestAnimationFrame.
277
+ */
278
+ useRequestAnimationFrame: {
279
+ value: options.useRequestAnimationFrame || false,
280
+ writable: true,
281
+ },
282
+ /**
283
+ * If true, encapsulates the renderTrajectories calls in a throttle function. Default to true.
284
+ */
285
+ useThrottle: {
286
+ value: options.useThrottle !== false,
287
+ writable: true,
288
+ },
289
+ /**
290
+ * If true, encapsulates the renderTrajectories calls in a debounce function.
291
+ */
292
+ useDebounce: {
293
+ value: options.useDebounce || false,
294
+ writable: true,
295
+ },
296
+ /**
297
+ * Debug properties.
298
+ */
299
+ // Not used anymore, but could be useful for debugging.
300
+ // showVehicleTraj: {
301
+ // value:
302
+ // options.showVehicleTraj !== undefined
303
+ // ? options.showVehicleTraj
304
+ // : true,
305
+ // writable: true,
306
+ // },
307
+ });
308
+ }
309
+ attachToMap(map) {
310
+ super.attachToMap(map);
311
+ // If the layer is visible we start the rendering clock
312
+ if (this.visible) {
313
+ this.start();
314
+ }
315
+ // On change of visibility we start/stop the rendering clock
316
+ this.visibilityRef = this.on('change:visible', (evt) => {
317
+ if (evt.target.visible) {
318
+ this.start();
319
+ }
320
+ else {
321
+ this.stop();
322
+ }
323
+ });
324
+ // To avoid browser hanging when the tab is not visible for a certain amount of time,
325
+ // We stop the rendering and the websocket when hide and start again when show.
326
+ document.addEventListener('visibilitychange', this.onDocumentVisibilityChange);
327
+ }
328
+ detachFromMap() {
329
+ document.removeEventListener('visibilitychange', this.onDocumentVisibilityChange);
330
+ this.stop();
331
+ unByKey(this.visibilityRef);
332
+ if (this.canvas) {
333
+ const context = this.canvas.getContext('2d');
334
+ if (context) {
335
+ context.clearRect(0, 0, this.canvas.width, this.canvas.height);
336
+ }
337
+ super.detachFromMap();
338
+ }
339
+ }
340
+ start() {
341
+ this.stop();
342
+ // Before starting to update trajectories, we remove trajectories that have
343
+ // a time_intervals in the past, it will
344
+ // avoid phantom train that are at the end of their route because we never
345
+ // received the deleted_vehicle event because we have changed the browser tab.
346
+ this.purgeOutOfDateTrajectories();
347
+ // @ts-ignore function without parameters must be define in subclasses
348
+ this.renderTrajectories();
349
+ this.startUpdateTime();
350
+ this.api.open();
351
+ this.api.subscribeTrajectory(this.mode, this.onTrajectoryMessage, undefined, this.isUpdateBboxOnMoveEnd);
352
+ this.api.subscribeDeletedVehicles(this.mode, this.onDeleteTrajectoryMessage, undefined, this.isUpdateBboxOnMoveEnd);
353
+ if (this.isUpdateBboxOnMoveEnd) {
354
+ // Update the bbox on each move end
355
+ // @ts-ignore function without parameters defined by subclasses
356
+ this.setBbox();
357
+ }
358
+ if (this.onStart) {
359
+ this.onStart(this);
360
+ }
361
+ }
362
+ /**
363
+ * Start the clock.
364
+ * @private
365
+ */
366
+ startUpdateTime() {
367
+ this.stopUpdateTime();
368
+ this.updateTimeDelay = this.getRefreshTimeInMs() || 0;
369
+ this.updateTimeInterval = window.setInterval(() => {
370
+ // When live=true, we update the time with new Date();
371
+ if (this.live) {
372
+ this.time = new Date();
373
+ }
374
+ else if (this.time && this.updateTimeDelay && this.speed) {
375
+ this.time = new Date(this.time.getTime() + this.updateTimeDelay * this.speed);
376
+ }
377
+ }, this.updateTimeDelay);
378
+ }
379
+ stop() {
380
+ this.api.unsubscribeTrajectory(this.onTrajectoryMessage);
381
+ this.api.unsubscribeDeletedVehicles(this.onDeleteTrajectoryMessage);
382
+ this.api.close();
383
+ if (this.onStop) {
384
+ this.onStop(this);
385
+ }
386
+ }
387
+ /**
388
+ * Stop the clock.
389
+ * @private
390
+ */
391
+ stopUpdateTime() {
392
+ if (this.updateTimeInterval) {
393
+ clearInterval(this.updateTimeInterval);
394
+ this.updateTimeInterval = undefined;
395
+ }
396
+ }
397
+ /**
398
+ * Launch renderTrajectories. it avoids duplicating code in renderTrajectories method.
399
+ *
400
+ * @param {object} viewState The view state of the map.
401
+ * @param {number[2]} viewState.center Center coordinate of the map in mercator coordinate.
402
+ * @param {number[4]} viewState.extent Extent of the map in mercator coordinates.
403
+ * @param {number[2]} viewState.size Size ([width, height]) of the canvas to render.
404
+ * @param {number} [viewState.rotation = 0] Rotation of the map to render.
405
+ * @param {number} viewState.resolution Resolution of the map to render.
406
+ * @param {boolean} noInterpolate If true trajectories are not interpolated but
407
+ * drawn at the last known coordinate. Use this for performance optimization
408
+ * during map navigation.
409
+ * @private
410
+ */
411
+ renderTrajectoriesInternal(viewState, noInterpolate = false) {
412
+ var _a;
413
+ if (!this.map || !this.trajectories) {
414
+ return false;
415
+ }
416
+ const time = this.live ? Date.now() : (_a = this.time) === null || _a === void 0 ? void 0 : _a.getTime();
417
+ const trajectories = Object.values(this.trajectories);
418
+ // console.time('sort');
419
+ if (this.sort) {
420
+ // @ts-ignore
421
+ trajectories.sort(this.sort);
422
+ }
423
+ // console.timeEnd('sort');
424
+ if (!this.canvas || !this.style) {
425
+ return true;
426
+ }
427
+ // console.time('render');
428
+ this.renderState = renderTrajectories(this.canvas, trajectories, this.style, Object.assign(Object.assign({}, viewState), { pixelRatio: this.pixelRatio || 1, time }), Object.assign({ filter: this.filter, noInterpolate: (viewState.zoom || 0) < this.minZoomInterpolation
429
+ ? true
430
+ : noInterpolate, hoverVehicleId: this.hoverVehicleId, selectedVehicleId: this.selectedVehicleId }, this.styleOptions));
431
+ // console.timeEnd('render');
432
+ return true;
433
+ }
434
+ /**
435
+ * Render the trajectories requesting an animation frame and cancelling the previous one.
436
+ * This function must be overrided by children to provide the correct parameters.
437
+ *
438
+ * @param {object} viewState The view state of the map.
439
+ * @param {number[2]} viewState.center Center coordinate of the map in mercator coordinate.
440
+ * @param {number[4]} viewState.extent Extent of the map in mercator coordinates.
441
+ * @param {number[2]} viewState.size Size ([width, height]) of the canvas to render.
442
+ * @param {number} [viewState.rotation = 0] Rotation of the map to render.
443
+ * @param {number} viewState.resolution Resolution of the map to render.
444
+ * @param {boolean} noInterpolate If true trajectories are not interpolated but
445
+ * drawn at the last known coordinate. Use this for performance optimization
446
+ * during map navigation.
447
+ * @private
448
+ */
449
+ renderTrajectories(viewState, noInterpolate) {
450
+ if (this.requestId) {
451
+ cancelAnimationFrame(this.requestId);
452
+ this.requestId = undefined;
453
+ }
454
+ if (!viewState) {
455
+ return;
456
+ }
457
+ if (!noInterpolate && this.useRequestAnimationFrame) {
458
+ this.requestId = requestAnimationFrame(() => {
459
+ this.renderTrajectoriesInternal(viewState, noInterpolate);
460
+ });
461
+ }
462
+ else if (!noInterpolate && this.useDebounce) {
463
+ this.debounceRenderTrajectories(viewState, noInterpolate);
464
+ }
465
+ else if (!noInterpolate && this.useThrottle) {
466
+ this.throttleRenderTrajectories(viewState, noInterpolate);
467
+ }
468
+ else {
469
+ this.renderTrajectoriesInternal(viewState, noInterpolate);
470
+ }
471
+ }
472
+ setBbox(extent, zoom) {
473
+ // Clean trajectories before sending the new bbox
474
+ // Purge trajectories:
475
+ // - which are outside the extent
476
+ // - when it's bus and zoom level is too low for them
477
+ if (this.trajectories && extent && zoom) {
478
+ const keys = Object.keys(this.trajectories);
479
+ for (let i = keys.length - 1; i >= 0; i -= 1) {
480
+ this.purgeTrajectory(this.trajectories[keys[i]], extent, zoom);
481
+ }
482
+ }
483
+ if (!extent) {
484
+ return;
485
+ }
486
+ const bbox = [...extent];
487
+ if (this.isUpdateBboxOnMoveEnd && zoom) {
488
+ bbox.push(zoom);
489
+ /* @ignore */
490
+ this.generalizationLevel = this.getGeneralizationLevelByZoom(zoom);
491
+ if (this.generalizationLevel) {
492
+ bbox.push(`gen=${this.generalizationLevel}`);
493
+ }
494
+ /* @ignore */
495
+ this.mots = this.getMotsByZoom(zoom);
496
+ if (this.mots) {
497
+ bbox.push(`mots=${this.mots}`);
498
+ }
499
+ }
500
+ if (this.tenant) {
501
+ bbox.push(`tenant=${this.tenant}`);
502
+ }
503
+ this.api.bbox = bbox;
504
+ }
505
+ /**
506
+ * Get the duration before the next update depending on zoom level.
507
+ *
508
+ * @private
509
+ * @param {number} zoom
510
+ */
511
+ getRefreshTimeInMs(zoom = 0) {
512
+ var _a;
513
+ const roundedZoom = zoom !== undefined ? Math.round(zoom) : -1;
514
+ const timeStep = this.getRenderTimeIntervalByZoom(roundedZoom) || 25;
515
+ const nextTick = Math.max(25, timeStep / (this.speed || 1));
516
+ const nextThrottleTick = Math.min(nextTick, 500);
517
+ // TODO: see if this should go elsewhere.
518
+ if (this.useThrottle) {
519
+ this.throttleRenderTrajectories = throttle(this.renderTrajectoriesInternal, nextThrottleTick, { leading: true, trailing: true });
520
+ }
521
+ else if (this.useDebounce) {
522
+ this.debounceRenderTrajectories = debounce(this.renderTrajectoriesInternal, nextThrottleTick, { leading: true, trailing: true, maxWait: 5000 });
523
+ }
524
+ if ((_a = this.api) === null || _a === void 0 ? void 0 : _a.buffer) {
525
+ const [, size] = this.api.buffer;
526
+ this.api.buffer = [nextThrottleTick, size];
527
+ }
528
+ return nextTick;
529
+ }
530
+ /**
531
+ * Get vehicle.
532
+ * @param {function} filterFc A function use to filter results.
533
+ * @return {Array<Object>} Array of vehicle.
534
+ */
535
+ getVehicle(filterFc) {
536
+ return ((this.trajectories &&
537
+ // @ts-ignore
538
+ Object.values(this.trajectories).filter(filterFc)) ||
539
+ []);
540
+ }
541
+ /**
542
+ * Request feature information for a given coordinate.
543
+ *
544
+ * @param {ol/coordinate~Coordinate} coordinate Coordinate.
545
+ * @param {Object} options Options See child classes to see which options are supported.
546
+ * @param {number} [options.resolution=1] The resolution of the map.
547
+ * @param {number} [options.nb=Infinity] The max number of vehicles to return.
548
+ * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate.
549
+ */
550
+ getFeatureInfoAtCoordinate(coordinate, options) {
551
+ const { resolution, nb } = options;
552
+ const ext = buffer([...coordinate, ...coordinate], this.hitTolerance * resolution);
553
+ let trajectories = Object.values(this.trajectories || {});
554
+ if (this.sort) {
555
+ // @ts-ignore
556
+ trajectories = trajectories.sort(this.sort);
557
+ }
558
+ const vehicles = [];
559
+ for (let i = 0; i < trajectories.length; i += 1) {
560
+ if (trajectories[i].properties.coordinate &&
561
+ containsCoordinate(ext, trajectories[i].properties.coordinate)) {
562
+ vehicles.push(trajectories[i]);
563
+ }
564
+ if (vehicles.length === nb) {
565
+ break;
566
+ }
567
+ }
568
+ return Promise.resolve({
569
+ layer: this,
570
+ features: vehicles.map((vehicle) => this.format.readFeature(vehicle)),
571
+ coordinate,
572
+ });
573
+ }
574
+ /**
575
+ * Request the stopSequence and the fullTrajectory informations for a vehicle.
576
+ *
577
+ * @param {string} id The vehicle identifier (the train_id property).
578
+ * @return {Promise<{stopSequence: StopSequence, fullTrajectory: FullTrajectory>} A promise that will be resolved with the trajectory informations.
579
+ */
580
+ getTrajectoryInfos(id) {
581
+ // When a vehicle is selected, we request the complete stop sequence and the complete full trajectory.
582
+ // Then we combine them in one response and send them to inherited layers.
583
+ const promises = [
584
+ this.api.getStopSequence(id),
585
+ this.api.getFullTrajectory(id, this.mode, this.generalizationLevel),
586
+ ];
587
+ return Promise.all(promises).then(([stopSequence, fullTrajectory]) => {
588
+ const response = {
589
+ stopSequence,
590
+ fullTrajectory,
591
+ };
592
+ return response;
593
+ });
594
+ }
595
+ /**
596
+ * Remove all trajectories that are in the past.
597
+ */
598
+ purgeOutOfDateTrajectories() {
599
+ Object.entries(this.trajectories || {}).forEach(([key, trajectory]) => {
600
+ var _a;
601
+ const timeIntervals = (_a = trajectory === null || trajectory === void 0 ? void 0 : trajectory.properties) === null || _a === void 0 ? void 0 : _a.time_intervals;
602
+ if (this.time && timeIntervals.length) {
603
+ const lastTimeInterval = timeIntervals[timeIntervals.length - 1][0];
604
+ if (lastTimeInterval < this.time) {
605
+ this.removeTrajectory(key);
606
+ }
607
+ }
608
+ });
609
+ }
610
+ /**
611
+ * Determine if the trajectory is useless and should be removed from the list or not.
612
+ * By default, this function exclude vehicles:
613
+ * - that have their trajectory outside the current extent and
614
+ * - that aren't in the MOT list.
615
+ *
616
+ * @param {RealtimeTrajectory} trajectory
617
+ * @param {Array<number>} extent
618
+ * @param {number} zoom
619
+ * @return {boolean} if the trajectory must be displayed or not.
620
+ * @ignore
621
+ */
622
+ purgeTrajectory(trajectory, extent, zoom) {
623
+ const { type, bounds } = trajectory.properties;
624
+ if ((this.isUpdateBboxOnMoveEnd && !intersects(extent, bounds)) ||
625
+ (this.mots && !this.mots.includes(type))) {
626
+ this.removeTrajectory(trajectory);
627
+ return true;
628
+ }
629
+ return false;
630
+ }
631
+ /**
632
+ * Add a trajectory.
633
+ * @param {RealtimeTrajectory} trajectory The trajectory to add.
634
+ * @private
635
+ */
636
+ addTrajectory(trajectory) {
637
+ if (this.filter && !this.filter(trajectory)) {
638
+ return;
639
+ }
640
+ if (!this.trajectories) {
641
+ this.trajectories = {};
642
+ }
643
+ this.trajectories[trajectory.properties.train_id] = trajectory;
644
+ // @ts-ignore the parameter are set by subclasses
645
+ this.renderTrajectories();
646
+ }
647
+ removeTrajectory(trajectoryOrId) {
648
+ var _a;
649
+ let id;
650
+ if (typeof trajectoryOrId !== 'string') {
651
+ id = (_a = trajectoryOrId === null || trajectoryOrId === void 0 ? void 0 : trajectoryOrId.properties) === null || _a === void 0 ? void 0 : _a.train_id;
652
+ }
653
+ else {
654
+ id = trajectoryOrId;
655
+ }
656
+ if (this.trajectories) {
657
+ delete this.trajectories[id];
658
+ }
659
+ }
660
+ /**
661
+ * On zoomend we adjust the time interval of the update of vehicles positions.
662
+ *
663
+ * @param evt Event that triggered the function.
664
+ * @private
665
+ */
666
+ onZoomEnd() {
667
+ this.startUpdateTime();
668
+ }
669
+ onDocumentVisibilityChange() {
670
+ if (!this.visible) {
671
+ return;
672
+ }
673
+ if (document.hidden) {
674
+ this.stop();
675
+ // Since we don't receive deleted_vehicles event when docuement
676
+ // is hidden. We have to clean all the trajectories for a fresh
677
+ // start when the document is visible again.
678
+ this.trajectories = {};
679
+ }
680
+ else {
681
+ this.start();
682
+ }
683
+ }
684
+ /**
685
+ * Callback on websocket's trajectory channel events.
686
+ * It adds a trajectory to the list.
687
+ *
688
+ * @private
689
+ */
690
+ onTrajectoryMessage(data) {
691
+ if (!data.content) {
692
+ return;
693
+ }
694
+ const trajectory = data.content;
695
+ const { geometry, properties: { train_id: id, time_since_update: timeSinceUpdate, raw_coordinates: rawCoordinates, }, } = trajectory;
696
+ // ignore old events [SBAHNM-97]
697
+ if (timeSinceUpdate < 0) {
698
+ return;
699
+ }
700
+ // console.time(`onTrajectoryMessage${data.content.properties.train_id}`);
701
+ // @ts-ignore default value for extentand zoom are provided by subclasses
702
+ if (this.purgeTrajectory(trajectory)) {
703
+ return;
704
+ }
705
+ if (this.debug &&
706
+ this.mode === RealtimeModes.TOPOGRAPHIC &&
707
+ rawCoordinates) {
708
+ trajectory.properties.olGeometry = this.format.readGeometry({
709
+ type: 'Point',
710
+ coordinates: fromLonLat(rawCoordinates, this.map.getView().getProjection()),
711
+ });
712
+ }
713
+ else {
714
+ trajectory.properties.olGeometry = this.format.readGeometry(geometry);
715
+ }
716
+ // TODO Make sure the timeOffset is useful. May be we can remove it.
717
+ trajectory.properties.timeOffset = Date.now() - data.timestamp;
718
+ this.addTrajectory(trajectory);
719
+ }
720
+ /**
721
+ * Callback on websocket's deleted_vehicles channel events.
722
+ * It removes the trajectory from the list.
723
+ *
724
+ * @private
725
+ * @override
726
+ */
727
+ onDeleteTrajectoryMessage(data) {
728
+ if (!data.content) {
729
+ return;
730
+ }
731
+ this.removeTrajectory(data.content);
732
+ }
733
+ /**
734
+ * Callback when user moves the mouse/pointer over the map.
735
+ * It sets the layer's hoverVehicleId property with the current hovered vehicle's id.
736
+ *
737
+ * @private
738
+ * @override
739
+ */
740
+ onFeatureHover(features, layer, coordinate) {
741
+ const [feature] = features;
742
+ let id = null;
743
+ if (feature) {
744
+ id = feature.get('train_id');
745
+ }
746
+ if (this.hoverVehicleId !== id) {
747
+ /** @ignore */
748
+ this.hoverVehicleId = id;
749
+ // @ts-ignore
750
+ this.renderTrajectories(true);
751
+ }
752
+ }
753
+ /**
754
+ * Callback when user clicks on the map.
755
+ * It sets the layer's selectedVehicleId property with the current selected vehicle's id.
756
+ *
757
+ * @private
758
+ * @override
759
+ */
760
+ onFeatureClick(features, layer, coordinate) {
761
+ const [feature] = features;
762
+ let id = null;
763
+ if (feature) {
764
+ id = feature.get('train_id');
765
+ }
766
+ if (this.selectedVehicleId !== id) {
767
+ /** @ignore */
768
+ this.selectedVehicleId = id;
769
+ this.selectedVehicle = feature;
770
+ // @ts-ignore parameters are provided by subclasses
771
+ this.renderTrajectories(true);
772
+ }
773
+ }
774
+ };
775
+ }
776
+ export default RealtimeLayerMixin;