mobility-toolbox-js 3.0.0-beta.32 → 3.0.0-beta.34

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 (56) hide show
  1. package/api/HttpAPI.js +1 -3
  2. package/api/RealtimeAPI.d.ts +5 -5
  3. package/api/RealtimeAPI.js +3 -3
  4. package/api/WebSocketAPI.js +0 -1
  5. package/common/controls/StopFinderControlCommon.d.ts +1 -1
  6. package/common/controls/StopFinderControlCommon.js +1 -1
  7. package/common/styles/realtimeDefaultStyle.js +0 -5
  8. package/common/styles/realtimeHeadingStyle.js +0 -5
  9. package/common/styles/realtimeSimpleStyle.d.ts +0 -1
  10. package/common/styles/realtimeSimpleStyle.js +0 -1
  11. package/common/utils/RealtimeEngine.d.ts +214 -0
  12. package/common/utils/RealtimeEngine.js +555 -0
  13. package/common/utils/getLayersAsFlatArray.d.ts +0 -1
  14. package/common/utils/getLayersAsFlatArray.js +0 -1
  15. package/common/utils/realtimeConfig.d.ts +1 -1
  16. package/common/utils/realtimeConfig.js +0 -1
  17. package/common/utils/renderTrajectories.d.ts +1 -0
  18. package/common/utils/renderTrajectories.js +1 -0
  19. package/common/utils/sortAndFilterDepartures.d.ts +1 -0
  20. package/common/utils/sortAndFilterDepartures.js +1 -0
  21. package/maplibre/controls/CopyrightControl.d.ts +9 -6
  22. package/maplibre/controls/CopyrightControl.js +11 -8
  23. package/maplibre/layers/Layer.d.ts +7 -6
  24. package/maplibre/layers/Layer.js +1 -2
  25. package/maplibre/layers/RealtimeLayer.d.ts +54 -111
  26. package/maplibre/layers/RealtimeLayer.js +126 -114
  27. package/maplibre/utils/getSourceCoordinates.d.ts +1 -0
  28. package/maplibre/utils/getSourceCoordinates.js +5 -4
  29. package/mbt.js +5329 -13530
  30. package/mbt.js.map +4 -4
  31. package/mbt.min.js +68 -71
  32. package/mbt.min.js.map +4 -4
  33. package/ol/controls/CopyrightControl.d.ts +13 -5
  34. package/ol/controls/CopyrightControl.js +13 -5
  35. package/ol/controls/RoutingControl.d.ts +30 -19
  36. package/ol/controls/RoutingControl.js +33 -48
  37. package/ol/controls/StopFinderControl.d.ts +23 -4
  38. package/ol/controls/StopFinderControl.js +22 -3
  39. package/ol/layers/MaplibreLayer.d.ts +22 -9
  40. package/ol/layers/MaplibreLayer.js +22 -9
  41. package/ol/layers/MaplibreStyleLayer.d.ts +35 -27
  42. package/ol/layers/MaplibreStyleLayer.js +36 -29
  43. package/ol/layers/RealtimeLayer.d.ts +76 -125
  44. package/ol/layers/RealtimeLayer.js +134 -169
  45. package/ol/mixins/PropertiesLayerMixin.d.ts +4 -6
  46. package/ol/mixins/PropertiesLayerMixin.js +0 -2
  47. package/ol/renderers/RealtimeLayerRenderer.js +6 -31
  48. package/ol/styles/fullTrajectoryDelayStyle.js +5 -7
  49. package/ol/styles/fullTrajectoryStyle.d.ts +1 -2
  50. package/ol/styles/fullTrajectoryStyle.js +5 -7
  51. package/ol/styles/routingStyle.d.ts +0 -1
  52. package/ol/styles/routingStyle.js +2 -7
  53. package/package.json +34 -32
  54. package/types/common.d.ts +2 -1
  55. package/common/mixins/RealtimeLayerMixin.d.ts +0 -267
  56. package/common/mixins/RealtimeLayerMixin.js +0 -751
package/api/HttpAPI.js CHANGED
@@ -14,9 +14,7 @@ import getUrlWithParams from '../common/utils/getUrlWithParams';
14
14
  */
15
15
  class HttpAPI {
16
16
  constructor(options) {
17
- /** @private */
18
17
  this.url = options.url;
19
- /** @private */
20
18
  this.apiKey = options.apiKey;
21
19
  }
22
20
  /**
@@ -29,7 +27,7 @@ class HttpAPI {
29
27
  if (!this.url) {
30
28
  throw new Error(`No url defined for request to ${this.url}/${path}`);
31
29
  }
32
- if (!this.url && !this.apiKey && !/key=/.test(this.url)) {
30
+ if (!this.url && !this.apiKey && !this.url.includes('key=')) {
33
31
  // eslint-disable-next-line no-console
34
32
  throw new Error(`No apiKey defined for request to ${this.url}`);
35
33
  }
@@ -55,18 +55,18 @@ export declare const RealtimeModes: {
55
55
  * });
56
56
  *
57
57
  * // Close the websocket connection
58
- * // api.close();
58
+ * api.close();
59
59
  *
60
60
  * @public
61
61
  */
62
62
  declare class RealtimeAPI {
63
- _bbox?: RealtimeBbox;
64
- _buffer?: number[];
65
- _url: string;
66
63
  private pingInterval;
67
64
  private pingIntervalMs;
68
65
  private reconnectTimeout?;
69
66
  private reconnectTimeoutMs?;
67
+ _bbox?: RealtimeBbox;
68
+ _buffer?: number[];
69
+ _url: string;
70
70
  version: RealtimeVersion;
71
71
  wsApi: WebSocketAPI;
72
72
  /**
@@ -350,7 +350,7 @@ declare class RealtimeAPI {
350
350
  * @param {function(data: { content: RealtimeDeparture[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
351
351
  * @public
352
352
  */
353
- unsubscribeTimetabe(stationId: RealtimeStationId, onMessage?: WebSocketAPIMessageCallback<RealtimeDeparture>): void;
353
+ unsubscribeTimetable(stationId: RealtimeStationId, onMessage?: WebSocketAPIMessageCallback<RealtimeDeparture>): void;
354
354
  /**
355
355
  * Unsubscribe to trajectory channels.
356
356
  * @param {function(data: { content: RealtimeTrajectory })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
@@ -39,7 +39,7 @@ export const RealtimeModes = {
39
39
  * });
40
40
  *
41
41
  * // Close the websocket connection
42
- * // api.close();
42
+ * api.close();
43
43
  *
44
44
  * @public
45
45
  */
@@ -422,7 +422,7 @@ class RealtimeAPI {
422
422
  * @deprecated Use RealtimeAPI.unsubscribeTimetabe instead.
423
423
  */
424
424
  unsubscribeDepartures(stationId, onMessage) {
425
- this.unsubscribeTimetabe(stationId, onMessage);
425
+ this.unsubscribeTimetable(stationId, onMessage);
426
426
  }
427
427
  /**
428
428
  * Unsubscribe disruptions.
@@ -490,7 +490,7 @@ class RealtimeAPI {
490
490
  * @param {function(data: { content: RealtimeDeparture[] })} onMessage Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
491
491
  * @public
492
492
  */
493
- unsubscribeTimetabe(stationId, onMessage) {
493
+ unsubscribeTimetable(stationId, onMessage) {
494
494
  this.unsubscribe(`timetable_${stationId}`, '', onMessage);
495
495
  }
496
496
  /**
@@ -71,7 +71,6 @@ class WebSocketAPI {
71
71
  this.websocket.close();
72
72
  }
73
73
  }
74
- /** @private */
75
74
  this.websocket = new WebSocket(url);
76
75
  if (!this.open) {
77
76
  this.websocket.addEventListener('open', () => {
@@ -30,7 +30,7 @@ declare class StopFinderControlCommon {
30
30
  * @param {Object} options Options
31
31
  * @param {HTMLElement} options.element HTML element where to attach input and suggestions.
32
32
  * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/). See StopsAPI.
33
- * @param {string} [options.url='https://api.geops.io/tracker/v1'] Stops service url. See StopsAPI.
33
+ * @param {string} [options.url='https://api.geops.io/stops/v1/'] Stops service url. See StopsAPI.
34
34
  * @param {string} [options.placeholder='Search for a stop...'] Input field placeholder.
35
35
  * @param {StopsSearchParams} [options.apiParams={ limit: 20 }] Request parameters. See [Stops service documentation](https://developer.geops.io/apis/5dcbd702a256d90001cf1361/).
36
36
  */
@@ -13,7 +13,7 @@ class StopFinderControlCommon {
13
13
  * @param {Object} options Options
14
14
  * @param {HTMLElement} options.element HTML element where to attach input and suggestions.
15
15
  * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/). See StopsAPI.
16
- * @param {string} [options.url='https://api.geops.io/tracker/v1'] Stops service url. See StopsAPI.
16
+ * @param {string} [options.url='https://api.geops.io/stops/v1/'] Stops service url. See StopsAPI.
17
17
  * @param {string} [options.placeholder='Search for a stop...'] Input field placeholder.
18
18
  * @param {StopsSearchParams} [options.apiParams={ limit: 20 }] Request parameters. See [Stops service documentation](https://developer.geops.io/apis/5dcbd702a256d90001cf1361/).
19
19
  */
@@ -1,5 +1,4 @@
1
1
  import createCanvas from '../utils/createCanvas';
2
- /** @private */
3
2
  const cacheDelayBg = {};
4
3
  /**
5
4
  * Draw circle delay background
@@ -25,7 +24,6 @@ export const getDelayBgCanvas = (origin, radius, color) => {
25
24
  }
26
25
  return cacheDelayBg[key];
27
26
  };
28
- /** @private */
29
27
  const cacheDelayText = {};
30
28
  /**
31
29
  * Draw delay text
@@ -55,7 +53,6 @@ export const getDelayTextCanvas = (text, fontSize, font, delayColor, delayOutlin
55
53
  }
56
54
  return cacheDelayText[key];
57
55
  };
58
- /** @private */
59
56
  const cacheCircle = {};
60
57
  /**
61
58
  * Draw colored circle with black border
@@ -90,7 +87,6 @@ export const getCircleCanvas = (origin, radius, color, hasStroke, hasDash, pixel
90
87
  }
91
88
  return cacheCircle[key];
92
89
  };
93
- /** @private */
94
90
  const cacheText = {};
95
91
  /**
96
92
  * Draw text in the circle
@@ -129,7 +125,6 @@ export const getTextCanvas = (text, origin, textSize, fillColor, strokeColor, ha
129
125
  }
130
126
  return cacheText[key];
131
127
  };
132
- /** @private */
133
128
  const cache = {};
134
129
  /**
135
130
  * A tracker style that take in account the delay.
@@ -1,16 +1,13 @@
1
1
  import createCanvas from '../utils/createCanvas';
2
2
  import { getBgColor } from '../utils/realtimeConfig';
3
3
  import realtimeDefaultStyle from './realtimeDefaultStyle';
4
- /** @private */
5
4
  const rotateCanvas = (canvas, rotation) => {
6
5
  const ctx = canvas.getContext('2d');
7
6
  ctx === null || ctx === void 0 ? void 0 : ctx.translate(canvas.width / 2, canvas.height / 2);
8
7
  ctx === null || ctx === void 0 ? void 0 : ctx.rotate(rotation);
9
8
  ctx === null || ctx === void 0 ? void 0 : ctx.translate(-canvas.width / 2, -canvas.height / 2);
10
9
  };
11
- /** @private */
12
10
  const arrowCache = {};
13
- /** @private */
14
11
  const getArrowCanvas = (fillColor) => {
15
12
  const key = `${fillColor}`;
16
13
  if (!arrowCache[key]) {
@@ -35,9 +32,7 @@ const getArrowCanvas = (fillColor) => {
35
32
  }
36
33
  return arrowCache[key];
37
34
  };
38
- /** @private */
39
35
  const bufferArrowCache = {};
40
- /** @private */
41
36
  const getBufferArrowCanvas = (canvas, fillColor, rotation) => {
42
37
  const margin = 20;
43
38
  const bufferKey = `${fillColor},${canvas.width},${canvas.height},${rotation}`;
@@ -1,4 +1,3 @@
1
1
  import type { RealtimeStyleFunction } from '../../types';
2
- /** @private */
3
2
  declare const realtimeSimpleStyle: RealtimeStyleFunction;
4
3
  export default realtimeSimpleStyle;
@@ -4,7 +4,6 @@ import createCanvas from '../utils/createCanvas';
4
4
  * Display blue point for each train.
5
5
  */
6
6
  let canvas;
7
- /** @private */
8
7
  const realtimeSimpleStyle = () => {
9
8
  if (!canvas) {
10
9
  canvas = createCanvas(15, 15);
@@ -0,0 +1,214 @@
1
+ import { FeatureCollection } from 'geojson';
2
+ import GeoJSON from 'ol/format/GeoJSON';
3
+ import { RealtimeAPI } from '../../api';
4
+ import { WebSocketAPIMessageEventData } from '../../api/WebSocketAPI';
5
+ import { AnyCanvas, AnyLayer, AnyRealtimeLayer, LayerGetFeatureInfoOptions, RealtimeGeneralizationLevel, RealtimeMode, RealtimeMot, RealtimeRenderState, RealtimeStyleFunction, RealtimeStyleOptions, RealtimeTenant, RealtimeTrainId, RealtimeTrajectory, ViewState } from '../../types';
6
+ import { FilterFunction, SortFunction } from '../typedefs';
7
+ import type { Coordinate } from 'ol/coordinate';
8
+ export interface RealtimeEngineOptions {
9
+ api?: RealtimeAPI;
10
+ apiKey?: string;
11
+ bbox?: (number | string)[];
12
+ bboxParameters?: Record<string, boolean | boolean[] | number | number[] | string | string[]>;
13
+ buffer?: number[];
14
+ canvas?: HTMLCanvasElement;
15
+ debug?: boolean;
16
+ filter?: FilterFunction;
17
+ generalizationLevelByZoom?: RealtimeGeneralizationLevel[];
18
+ getGeneralizationLevelByZoom?: (zoom: number, generalizationLevelByZoom: RealtimeGeneralizationLevel[]) => RealtimeGeneralizationLevel;
19
+ getMotsByZoom?: (zoom: number, motsByZoom: RealtimeMot[][]) => RealtimeMot[];
20
+ getRenderTimeIntervalByZoom?: (zoom: number, renderTimeIntervalByZoom: number[]) => number;
21
+ getViewState?: () => ViewState;
22
+ hoverVehicleId?: RealtimeTrainId;
23
+ isUpdateBboxOnMoveEnd?: boolean;
24
+ live?: boolean;
25
+ minZoomInterpolation?: number;
26
+ mode?: RealtimeMode;
27
+ motsByZoom?: RealtimeMot[][];
28
+ onRender?: (renderState: RealtimeRenderState, viewState: ViewState) => void;
29
+ onStart?: (realtimeLayer: AnyRealtimeLayer) => void;
30
+ onStop?: (realtimeLayer: AnyRealtimeLayer) => void;
31
+ pingIntervalMs?: number;
32
+ pixelRatio?: number;
33
+ prefix?: string;
34
+ renderTimeIntervalByZoom?: number[];
35
+ selectedVehicleId?: RealtimeTrainId;
36
+ shouldRender?: () => boolean;
37
+ sort?: SortFunction;
38
+ speed?: number;
39
+ style?: RealtimeStyleFunction;
40
+ styleOptions?: RealtimeStyleOptions;
41
+ tenant?: RealtimeTenant;
42
+ time?: Date;
43
+ url?: string;
44
+ useDebounce?: boolean;
45
+ useRequestAnimationFrame?: boolean;
46
+ useThrottle?: boolean;
47
+ }
48
+ /**
49
+ * This class is responsible for drawing trajectories from a realtime API in a canvas,
50
+ * depending on the map's view state and at a specific time.
51
+ *
52
+ * This class is totally agnostic from Maplibre or OpenLayers and must stay taht way.
53
+ */
54
+ declare class RealtimeEngine {
55
+ _mode: RealtimeMode;
56
+ _speed: number;
57
+ _style: RealtimeStyleFunction;
58
+ _time: Date;
59
+ api: RealtimeAPI;
60
+ bboxParameters?: Record<string, boolean | boolean[] | number | number[] | string | string[]>;
61
+ canvas?: AnyCanvas;
62
+ debounceRenderTrajectories: (viewState: ViewState, noInterpolate?: boolean) => void;
63
+ debug: boolean;
64
+ filter?: FilterFunction;
65
+ format: GeoJSON;
66
+ generalizationLevel?: RealtimeGeneralizationLevel;
67
+ generalizationLevelByZoom: RealtimeGeneralizationLevel[];
68
+ getGeneralizationLevelByZoom: (zoom: number) => RealtimeGeneralizationLevel;
69
+ getMotsByZoom: (zoom: number) => RealtimeMot[];
70
+ getRenderTimeIntervalByZoom: (zoom: number) => number;
71
+ getViewState: () => ViewState;
72
+ hoverVehicleId?: RealtimeTrainId;
73
+ isUpdateBboxOnMoveEnd: boolean;
74
+ live?: boolean;
75
+ minZoomInterpolation: number;
76
+ mots?: RealtimeMot[];
77
+ motsByZoom: RealtimeMot[][];
78
+ onRender?: (renderState: RealtimeRenderState, viewState: ViewState) => void;
79
+ onStart?: (realtimeLayer: AnyLayer) => void;
80
+ onStop?: (realtimeLayer: AnyLayer) => void;
81
+ pixelRatio?: number;
82
+ renderState?: RealtimeRenderState;
83
+ renderTimeIntervalByZoom: number[];
84
+ requestId?: number;
85
+ selectedVehicle: RealtimeTrajectory;
86
+ selectedVehicleId?: RealtimeTrainId;
87
+ shouldRender: () => boolean;
88
+ sort?: SortFunction;
89
+ styleOptions?: RealtimeStyleOptions;
90
+ tenant: RealtimeTenant;
91
+ throttleRenderTrajectories: (viewState: ViewState, noInterpolate?: boolean) => void;
92
+ trajectories?: Record<RealtimeTrainId, RealtimeTrajectory>;
93
+ updateTimeDelay?: number;
94
+ updateTimeInterval?: number;
95
+ useDebounce?: boolean;
96
+ useRequestAnimationFrame?: boolean;
97
+ useThrottle?: boolean;
98
+ constructor(options: RealtimeEngineOptions);
99
+ /**
100
+ * Add a trajectory.
101
+ * @param {RealtimeTrajectory} trajectory The trajectory to add.
102
+ * @private
103
+ */
104
+ addTrajectory(trajectory: RealtimeTrajectory): void;
105
+ attachToMap(): void;
106
+ detachFromMap(): void;
107
+ /**
108
+ * Get the duration before the next update depending on zoom level.
109
+ *
110
+ * @private
111
+ */
112
+ getRefreshTimeInMs(): number;
113
+ /**
114
+ * Get vehicle.
115
+ * @param {function} filterFc A function use to filter results.
116
+ * @return {Array<Object>} Array of vehicle.
117
+ */
118
+ getVehicle(filterFc: FilterFunction): RealtimeTrajectory[];
119
+ /**
120
+ * Request feature information for a given coordinate.
121
+ *
122
+ * @param {ol/coordinate~Coordinate} coordinate Coordinate.
123
+ * @param {Object} options Options See child classes to see which options are supported.
124
+ * @param {number} [options.resolution=1] The resolution of the map.
125
+ * @param {number} [options.nb=Infinity] The max number of vehicles to return.
126
+ * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate.
127
+ */
128
+ getVehiclesAtCoordinate(coordinate: Coordinate, options?: LayerGetFeatureInfoOptions): FeatureCollection;
129
+ /**
130
+ * Callback on websocket's deleted_vehicles channel events.
131
+ * It removes the trajectory from the list.
132
+ *
133
+ * @private
134
+ * @override
135
+ */
136
+ onDeleteTrajectoryMessage(data: WebSocketAPIMessageEventData<RealtimeTrainId>): void;
137
+ onDocumentVisibilityChange(): void;
138
+ /**
139
+ * Callback on websocket's trajectory channel events.
140
+ * It adds a trajectory to the list.
141
+ *
142
+ * @private
143
+ */
144
+ onTrajectoryMessage(data: WebSocketAPIMessageEventData<RealtimeTrajectory>): void;
145
+ /**
146
+ * On zoomend we adjust the time interval of the update of vehicles positions.
147
+ *
148
+ * @private
149
+ */
150
+ onZoomEnd(): void;
151
+ /**
152
+ * Remove all trajectories that are in the past.
153
+ */
154
+ purgeOutOfDateTrajectories(): void;
155
+ /**
156
+ * Determine if the trajectory is useless and should be removed from the list or not.
157
+ * By default, this function exclude vehicles:
158
+ * - that have their trajectory outside the current extent and
159
+ * - that aren't in the MOT list.
160
+ *
161
+ * @param {RealtimeTrajectory} trajectory
162
+ * @return {boolean} if the trajectory must be displayed or not.
163
+ * @private
164
+ */
165
+ purgeTrajectory(trajectory: RealtimeTrajectory): boolean;
166
+ removeTrajectory(trajectoryOrId: RealtimeTrainId | RealtimeTrajectory): void;
167
+ /**
168
+ * Render the trajectories requesting an animation frame and cancelling the previous one.
169
+ * This function must be overrided by children to provide the correct parameters.
170
+ *
171
+ * @param {boolean} noInterpolate If true trajectories are not interpolated but
172
+ * drawn at the last known coordinate. Use this for performance optimization
173
+ * during map navigation.
174
+ * @private
175
+ */
176
+ renderTrajectories(noInterpolate?: boolean): void;
177
+ /**
178
+ * Launch renderTrajectories. it avoids duplicating code in renderTrajectories method.
179
+ *
180
+ * @param {object} viewState The view state of the map.
181
+ * @param {number[2]} viewState.center Center coordinate of the map in mercator coordinate.
182
+ * @param {number[4]} viewState.extent Extent of the map in mercator coordinates.
183
+ * @param {number[2]} viewState.size Size ([width, height]) of the canvas to render.
184
+ * @param {number} [viewState.rotation = 0] Rotation of the map to render.
185
+ * @param {number} viewState.resolution Resolution of the map to render.
186
+ * @param {boolean} noInterpolate If true trajectories are not interpolated but
187
+ * drawn at the last known coordinate. Use this for performance optimization
188
+ * during map navigation.
189
+ * @private
190
+ */
191
+ renderTrajectoriesInternal(viewState: ViewState, noInterpolate?: boolean): boolean;
192
+ setBbox(): void;
193
+ start(): void;
194
+ /**
195
+ * Start the clock.
196
+ * @private
197
+ */
198
+ startUpdateTime(): void;
199
+ stop(): void;
200
+ /**
201
+ * Stop the clock.
202
+ * @private
203
+ */
204
+ stopUpdateTime(): void;
205
+ get mode(): RealtimeMode;
206
+ set mode(newMode: RealtimeMode);
207
+ get speed(): number;
208
+ set speed(newSpeed: number);
209
+ get style(): RealtimeStyleFunction;
210
+ set style(newStyle: RealtimeStyleFunction);
211
+ get time(): Date;
212
+ set time(newTime: Date | number);
213
+ }
214
+ export default RealtimeEngine;