mobility-toolbox-js 2.0.0 → 2.0.1-beta.13

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 (102) hide show
  1. package/api/RoutingAPI.js +15 -0
  2. package/api/RoutingAPI.test.js +25 -0
  3. package/api/StopsAPI.js +12 -0
  4. package/api/StopsAPI.test.js +22 -0
  5. package/api/TralisAPI.js +359 -0
  6. package/api/TralisAPI.test.js +67 -0
  7. package/api/{tralis/TralisAPIUtils.js → TralisAPIUtils.js} +2 -32
  8. package/api/index.js +3 -3
  9. package/{ol/README.md → api/typedefs.js} +0 -0
  10. package/common/Tracker.js +14 -118
  11. package/common/api/HttpAPI.js +30 -0
  12. package/common/api/HttpAPI.test.js +50 -0
  13. package/common/api/WebSocketAPI.js +175 -0
  14. package/{api/tralis/WebSocketConnector.test.js → common/api/WebSocketAPI.test.js} +100 -145
  15. package/common/controls/Control.js +26 -91
  16. package/common/controls/Control.test.js +32 -43
  17. package/common/index.js +4 -0
  18. package/common/layers/Layer.js +53 -244
  19. package/common/layers/Layer.test.js +185 -244
  20. package/common/mixins/CopyrightMixin.js +20 -44
  21. package/common/mixins/SearchMixin.js +100 -166
  22. package/common/mixins/TralisLayerMixin.js +443 -894
  23. package/common/styles/index.js +4 -4
  24. package/common/styles/trackerDefaultStyle.js +39 -175
  25. package/common/styles/trackerDelayStyle.js +2 -11
  26. package/common/styles/trackerSimpleStyle.js +4 -8
  27. package/common/trackerConfig.js +61 -99
  28. package/common/trackerConfig.test.js +15 -17
  29. package/common/typedefs.js +0 -23
  30. package/common/utils/createTrackerFilters.js +10 -41
  31. package/common/utils/createTrackerFilters.test.js +40 -56
  32. package/common/utils/getMapboxMapCopyrights.js +3 -16
  33. package/common/utils/getMapboxMapCopyrights.test.js +32 -39
  34. package/common/utils/getMapboxStyleUrl.js +3 -13
  35. package/common/utils/getVehiclePosition.js +3 -33
  36. package/common/utils/index.js +5 -6
  37. package/common/utils/removeDuplicate.js +3 -17
  38. package/common/utils/removeDuplicate.test.js +17 -20
  39. package/common/utils/sortByDelay.js +2 -7
  40. package/common/utils/timeUtils.js +8 -32
  41. package/common/utils/timeUtils.test.js +7 -13
  42. package/index.js +8 -2
  43. package/mapbox/controls/CopyrightControl.js +9 -38
  44. package/mapbox/controls/index.js +1 -0
  45. package/mapbox/index.js +4 -3
  46. package/mapbox/layers/Layer.js +15 -76
  47. package/mapbox/layers/Layer.test.js +81 -101
  48. package/mapbox/layers/TralisLayer.js +46 -193
  49. package/mapbox/layers/TralisLayer.test.js +12 -14
  50. package/mapbox/layers/index.js +2 -0
  51. package/mapbox/utils.js +7 -21
  52. package/mbt.js +50444 -0
  53. package/mbt.js.map +7 -0
  54. package/mbt.min.js +1005 -0
  55. package/mbt.min.js.map +7 -0
  56. package/ol/controls/CopyrightControl.js +8 -46
  57. package/ol/controls/CopyrightControl.test.js +75 -121
  58. package/ol/controls/RoutingControl.js +167 -532
  59. package/ol/controls/RoutingControl.test.js +99 -164
  60. package/ol/controls/StopFinderControl.js +3 -31
  61. package/ol/controls/StopFinderControl.test.js +18 -29
  62. package/ol/controls/index.js +3 -0
  63. package/ol/index.js +5 -13
  64. package/ol/layers/Layer.js +23 -128
  65. package/ol/layers/Layer.test.js +79 -102
  66. package/ol/layers/MapboxLayer.js +62 -237
  67. package/ol/layers/MapboxLayer.test.js +58 -84
  68. package/ol/layers/MapboxStyleLayer.js +38 -268
  69. package/ol/layers/MapboxStyleLayer.test.js +97 -128
  70. package/ol/layers/MaplibreLayer.js +46 -187
  71. package/ol/layers/RoutingLayer.js +21 -51
  72. package/ol/layers/RoutingLayer.test.js +15 -24
  73. package/ol/layers/TralisLayer.js +102 -276
  74. package/ol/layers/TralisLayer.test.js +32 -50
  75. package/ol/layers/VectorLayer.js +3 -24
  76. package/ol/layers/VectorLayer.test.js +34 -45
  77. package/ol/layers/WMSLayer.js +15 -57
  78. package/ol/layers/WMSLayer.test.js +35 -43
  79. package/ol/layers/index.js +8 -0
  80. package/ol/styles/fullTrajectoryDelayStyle.js +11 -15
  81. package/ol/styles/fullTrajectoryStyle.js +17 -25
  82. package/ol/styles/index.js +2 -2
  83. package/package.json +35 -62
  84. package/api/routing/RoutingAPI.js +0 -44
  85. package/api/routing/RoutingAPI.test.js +0 -41
  86. package/api/stops/StopsAPI.js +0 -41
  87. package/api/stops/StopsAPI.test.js +0 -34
  88. package/api/tralis/TralisAPI.js +0 -731
  89. package/api/tralis/TralisAPI.test.js +0 -75
  90. package/api/tralis/WebSocketConnector.js +0 -338
  91. package/api/tralis/typedefs.js +0 -81
  92. package/common/api/api.js +0 -64
  93. package/common/api/api.test.js +0 -68
  94. package/index.js.map +0 -1
  95. package/module.js +0 -23
  96. package/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
  97. package/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
  98. package/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
  99. package/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
  100. package/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
  101. package/ol/controls/snapshots/RoutingControlStation1.json +0 -60
  102. package/ol/controls/snapshots/RoutingControlStation2.json +0 -49
@@ -1,930 +1,479 @@
1
- /* eslint-disable no-empty-function */
2
- /* eslint-disable no-useless-constructor */
3
- /* eslint-disable no-unused-vars */
4
- /* eslint-disable class-methods-use-this */
5
- /* eslint-disable max-classes-per-file */
6
- import qs from 'query-string';
7
- import { buffer, containsCoordinate, intersects } from 'ol/extent';
8
- import { unByKey } from 'ol/Observable';
9
- import GeoJSON from 'ol/format/GeoJSON';
10
- import Point from 'ol/geom/Point';
11
- import debounce from 'lodash.debounce';
12
- import throttle from 'lodash.throttle';
13
- import { fromLonLat } from 'ol/proj';
14
- import Tracker from '../Tracker';
15
- import { timeSteps } from '../trackerConfig';
16
- import createFilters from '../utils/createTrackerFilters';
17
- import trackerDefaultStyle from '../styles/trackerDefaultStyle';
18
- import { TralisAPI, TralisModes } from '../../api';
19
-
20
- /* Permalink parameter used to filters vehicles */
21
- const LINE_FILTER = 'publishedlinename';
22
- const ROUTE_FILTER = 'tripnumber';
23
- const OPERATOR_FILTER = 'operator';
24
-
25
- /**
26
- * TralisLayerInterface.
27
- */
1
+ import qs from "query-string";
2
+ import { buffer, containsCoordinate, intersects } from "ol/extent";
3
+ import { unByKey } from "ol/Observable";
4
+ import GeoJSON from "ol/format/GeoJSON";
5
+ import Point from "ol/geom/Point";
6
+ import debounce from "lodash.debounce";
7
+ import throttle from "lodash.throttle";
8
+ import { fromLonLat } from "ol/proj";
9
+ import Tracker from "../Tracker";
10
+ import { timeSteps } from "../trackerConfig";
11
+ import createFilters from "../utils/createTrackerFilters";
12
+ import trackerDefaultStyle from "../styles/trackerDefaultStyle";
13
+ import { TralisAPI, TralisModes } from "../../api";
14
+ const LINE_FILTER = "publishedlinename";
15
+ const ROUTE_FILTER = "tripnumber";
16
+ const OPERATOR_FILTER = "operator";
28
17
  export class TralisLayerInterface {
29
- /*
30
- * Constructor
31
-
32
- * @param {Object} options Layer options.
33
- * @param {string} options.url Tralis service url.
34
- * @param {string} options.apiKey Access key for [geOps services](https://developer.geops.io/).
35
- * @param {boolean} [options.debug=false] Display additional debug informations.
36
- * @param {TralisMode} [options.mode=TralisMode.TOPOGRAPHIC] Tralis's Mode.
37
- * @param {number} [options.minZoomInterpolation=8] Minimal zoom when trains positions start to be interpolated.
38
- * @param {number} [options.minZoomNonTrain=9] Minimal zoom when non trains vehicles are allowed to be displayed.
39
- */
40
- constructor(options = {}) {}
41
-
42
- /**
43
- * Initialize the layer subscribing to the Tralis api.
44
- *
45
- * @param {ol/Map~Map} map
46
- */
47
- init(map) {}
48
-
49
- /**
50
- * Terminate the layer unsubscribing to the Tralis api.
51
- */
52
- terminate() {}
53
-
54
- /**
55
- * Start the clock.
56
- */
57
- start() {}
58
-
59
- /**
60
- * Stop the clock.
61
- */
62
- stop() {}
63
-
64
- /**
65
- * Set the Tralis api's bbox.
66
- *
67
- * @param {Array<number>} extent Extent to request, [minX, minY, maxX, maxY, zoom].
68
- * @param {number} zoom Zoom level to request. Must be an integer.
69
- */
70
- setBbox(extent, zoom) {}
71
-
72
- /**
73
- * Set the Tralis api's mode.
74
- *
75
- * @param {TralisMode} mode Tralis mode
76
- */
77
- setMode(mode) {}
78
-
79
- /**
80
- * Request the stopSequence and the fullTrajectory informations for a vehicle.
81
- *
82
- * @param {string} id The vehicle identifier (the train_id property).
83
- * @param {TralisMode} mode The mode to request. If not defined, the layer´s mode propetrty will be used.
84
- * @return {Promise<{stopSequence: StopSequence, fullTrajectory: FullTrajectory>} A promise that will be resolved with the trajectory informations.
85
- */
86
- getTrajectoryInfos(id, mode) {}
18
+ constructor(options = {}) {
19
+ }
20
+ attachToMap(map) {
21
+ }
22
+ detachFromMap() {
23
+ }
24
+ start() {
25
+ }
26
+ stop() {
27
+ }
28
+ setBbox(extent, zoom) {
29
+ }
30
+ setMode(mode) {
31
+ }
32
+ getTrajectoryInfos(id, mode) {
33
+ }
87
34
  }
88
-
89
- /**
90
- * Mixin for TralisLayerInterface.
91
- *
92
- * @param {Class} Base A class to extend with {TralisLayerInterface} functionnalities.
93
- * @return {Class} A class that implements {TralisLayerInterface} class and extends Base;
94
- * @private
95
- */
96
- const TralisLayerMixin = (Base) =>
97
- class extends Base {
98
- constructor(options = {}) {
99
- super({ hitTolerance: 10, ...options });
100
-
101
- this.debug = options.debug;
102
- this.mode = options.mode || TralisModes.TOPOGRAPHIC;
103
- this.api = options.api || new TralisAPI(options);
104
- this.tenant = options.tenant || ''; // sbb,sbh or sbm
105
- this.minZoomNonTrain = options.minZoomNonTrain || 9; // Min zoom level from which non trains are allowed to be displayed. Min value is 9 (as configured by the server
106
- this.minZoomInterpolation = options.minZoomInterpolation || 8; // Min zoom level from which trains positions are not interpolated.
107
- this.format = new GeoJSON();
108
- this.generalizationLevelByZoom = options.generalizationLevelByZoom || {
109
- 0: 5,
110
- 1: 5,
111
- 2: 5,
112
- 3: 5,
113
- 4: 5,
114
- 5: 5,
115
- 6: 5,
116
- 7: 5,
117
- 8: 10,
118
- 9: 30,
119
- 10: 30,
120
- 11: 100,
121
- 12: 100,
122
- 13: 100,
123
- };
124
-
125
- // This property will call api.setBbox on each movend event
126
- this.isUpdateBboxOnMoveEnd = options.isUpdateBboxOnMoveEnd !== false;
127
-
128
- // Define throttling nad debounce render function
129
- this.throttleRenderTrajectories = throttle(
130
- this.renderTrajectoriesInternal,
131
- 50,
132
- { leading: false, trailing: true },
133
- );
134
-
135
- this.debounceRenderTrajectories = debounce(
136
- this.renderTrajectoriesInternal,
137
- 50,
138
- { leading: true, trailing: true, maxWait: 5000 },
139
- );
140
-
141
- // Bind callbacks
142
- this.onFeatureHover = this.onFeatureHover.bind(this);
143
- this.onFeatureClick = this.onFeatureClick.bind(this);
144
- this.renderTrajectoriesInternal =
145
- this.renderTrajectoriesInternal.bind(this);
146
- this.onTrajectoryMessage = this.onTrajectoryMessage.bind(this);
147
- this.onDeleteTrajectoryMessage =
148
- this.onDeleteTrajectoryMessage.bind(this);
149
- this.onDocumentVisibilityChange =
150
- this.onDocumentVisibilityChange.bind(this);
151
- }
152
-
153
- /**
154
- * Define layer's properties.
155
- *
156
- * @ignore
157
- */
158
- defineProperties(options) {
159
- // Tracker options use to build the tracker.
160
- let { regexPublishedLineName, publishedLineName, tripNumber, operator } =
161
- options;
162
- const {
163
- style,
164
- speed,
165
- pixelRatio,
166
- hoverVehicleId,
167
- selectedVehicleId,
168
- filter,
169
- sort,
170
- time,
171
- live,
172
- } = options;
173
-
174
- const initTrackerOptions = {
175
- style,
176
- };
177
-
178
- Object.keys(initTrackerOptions).forEach(
179
- (key) =>
180
- initTrackerOptions[key] === undefined &&
181
- delete initTrackerOptions[key],
182
- );
183
-
184
- let currSpeed = speed || 1;
185
- let currTime = time || new Date();
186
-
187
- super.defineProperties(options);
188
-
189
- Object.defineProperties(this, {
190
- isTrackerLayer: { value: true },
191
-
192
- /**
193
- * Style function used to render a vehicle.
194
- */
195
- style: {
196
- value: (trajectory, viewState) =>
197
- (style || trackerDefaultStyle)(trajectory, viewState, this),
198
- },
199
-
200
- /**
201
- * Speed of the wheel of time.
202
- * If live property is true. The speed is ignored.
203
- */
204
- speed: {
205
- get: () => currSpeed,
206
- set: (newSpeed) => {
207
- currSpeed = newSpeed;
208
- this.start();
209
- },
210
- },
211
-
212
- /**
213
- * Function to filter which vehicles to display.
214
- */
215
- filter: {
216
- value: filter,
217
- writable: true,
218
- },
219
-
220
- /**
221
- * Function to sort the vehicles to display.
222
- */
223
- sort: {
224
- value: sort,
225
- writable: true,
226
- },
227
-
228
- /**
229
- * The tracker that renders the trajectories.
230
- */
231
- tracker: { value: null, writable: true },
232
-
233
- /**
234
- * Canvas cache object for trajectories drawn.
235
- */
236
- styleCache: { value: {} },
237
-
238
- /**
239
- * If true. The layer will always use Date.now() on the next tick to render the trajectories.
240
- * When true, setting the time property has no effect.
241
- */
242
- live: {
243
- value: live === false ? live : true,
244
- writable: true,
245
- },
246
-
247
- /**
248
- * Time used to display the trajectories. Can be a Date or a number in ms representing a Date.
249
- * If live property is true. The setter does nothing.
250
- */
251
- time: {
252
- get: () => currTime,
253
- set: (newTime) => {
254
- currTime = newTime && newTime.getTime ? newTime : new Date(newTime);
255
- this.renderTrajectories();
256
- },
257
- },
258
-
259
- /**
260
- * Keep track of which trajectories are stored.
261
- */
262
- trajectories: {
263
- value: {},
264
- writable: true,
265
- },
266
-
267
- /**
268
- * Keep track of which trajectories are currently drawn.
269
- */
270
- renderedTrajectories: {
271
- get: () => (this.tracker && this.tracker.renderedTrajectories) || [],
272
- },
273
-
274
- /**
275
- * Id of the hovered vehicle.
276
- */
277
- hoverVehicleId: {
278
- value: hoverVehicleId,
279
- writable: true,
280
- },
281
-
282
- /**
283
- * Id of the selected vehicle.
284
- */
285
- selectedVehicleId: {
286
- value: selectedVehicleId,
287
- writable: true,
288
- },
289
-
290
- /**
291
- * Id of the selected vehicle.
292
- */
293
- pixelRatio: {
294
- value:
295
- pixelRatio ||
296
- (typeof window !== 'undefined' ? window.devicePixelRatio : 1),
297
- writable: true,
298
- },
299
-
300
- /**
301
- * Options used by the constructor of the Tracker class.
302
- */
303
- initTrackerOptions: {
304
- value: initTrackerOptions,
305
- writable: false,
306
- },
307
-
308
- /**
309
- * If true, encapsulates the renderTrajectories calls in a requestAnimationFrame.
310
- */
311
- useRequestAnimationFrame: {
312
- value: options.useRequestAnimationFrame || false,
313
- writable: true,
314
- },
315
-
316
- /**
317
- * If true, encapsulates the renderTrajectories calls in a throttle function. Default to true.
318
- */
319
- useThrottle: {
320
- value: options.useThrottle || true,
321
- writable: true,
322
- },
323
-
324
- /**
325
- * If true, encapsulates the renderTrajectories calls in a debounce function.
326
- */
327
- useDebounce: {
328
- value: options.useDebounce || false,
329
- writable: true,
330
- },
331
-
332
- /**
333
- * Filter properties used in combination with permalink parameters.
334
- */
335
- publishedLineName: {
336
- get: () => publishedLineName,
337
- set: (newPublishedLineName) => {
338
- publishedLineName = newPublishedLineName;
339
- this.updateFilters();
340
- },
341
- },
342
- tripNumber: {
343
- get: () => tripNumber,
344
- set: (newTripNumber) => {
345
- tripNumber = newTripNumber;
346
- this.updateFilters();
347
- },
348
- },
349
- operator: {
350
- get: () => operator,
351
- set: (newOperator) => {
352
- operator = newOperator;
353
- this.updateFilters();
354
- },
355
- },
356
- regexPublishedLineName: {
357
- get: () => regexPublishedLineName,
358
- set: (newRegex) => {
359
- regexPublishedLineName = newRegex;
360
- this.updateFilters();
361
- },
362
- },
363
-
364
- /**
365
- * Style properties.
366
- */
367
- delayDisplay: {
368
- value: options.delayDisplay || 300000,
369
- writable: true,
370
- },
371
- delayOutlineColor: {
372
- value: options.delayOutlineColor || '#000000',
373
- writable: true,
374
- },
375
-
376
- /**
377
- * Debug properties.
378
- */
379
- // Not used anymore, but could be useful for debugging.
380
- // showVehicleTraj: {
381
- // value:
382
- // options.showVehicleTraj !== undefined
383
- // ? options.showVehicleTraj
384
- // : true,
385
- // writable: true,
386
- // },
387
- });
388
-
389
- // Update filter function based on convenient properties
390
- this.updateFilters();
391
- }
392
-
393
- init(map) {
394
- super.init(map);
395
-
396
- this.tracker = new Tracker({
397
- style: (...args) => this.style(...args),
398
- ...this.initTrackerOptions,
399
- });
400
-
401
- // If the layer is visible we start the rendering clock
402
- if (this.visible) {
403
- this.start();
404
- }
405
-
406
- // On change of visibility we start/stop the rendering clock
407
- this.visibilityRef = this.on('change:visible', (evt) => {
408
- if (evt.target.visible) {
35
+ const TralisLayerMixin = (Base) => class extends Base {
36
+ constructor(options = {}) {
37
+ super({ hitTolerance: 10, ...options });
38
+ this.debug = options.debug;
39
+ this.mode = options.mode || TralisModes.TOPOGRAPHIC;
40
+ this.api = options.api || new TralisAPI(options);
41
+ this.tenant = options.tenant || "";
42
+ this.minZoomNonTrain = options.minZoomNonTrain || 9;
43
+ this.minZoomInterpolation = options.minZoomInterpolation || 8;
44
+ this.format = new GeoJSON();
45
+ this.generalizationLevelByZoom = options.generalizationLevelByZoom || {
46
+ 0: 5,
47
+ 1: 5,
48
+ 2: 5,
49
+ 3: 5,
50
+ 4: 5,
51
+ 5: 5,
52
+ 6: 5,
53
+ 7: 5,
54
+ 8: 10,
55
+ 9: 30,
56
+ 10: 30,
57
+ 11: 100,
58
+ 12: 100,
59
+ 13: 100
60
+ };
61
+ this.isUpdateBboxOnMoveEnd = options.isUpdateBboxOnMoveEnd !== false;
62
+ this.throttleRenderTrajectories = throttle(this.renderTrajectoriesInternal, 50, { leading: false, trailing: true });
63
+ this.debounceRenderTrajectories = debounce(this.renderTrajectoriesInternal, 50, { leading: true, trailing: true, maxWait: 5e3 });
64
+ this.onFeatureHover = this.onFeatureHover.bind(this);
65
+ this.onFeatureClick = this.onFeatureClick.bind(this);
66
+ this.renderTrajectoriesInternal = this.renderTrajectoriesInternal.bind(this);
67
+ this.onTrajectoryMessage = this.onTrajectoryMessage.bind(this);
68
+ this.onDeleteTrajectoryMessage = this.onDeleteTrajectoryMessage.bind(this);
69
+ this.onDocumentVisibilityChange = this.onDocumentVisibilityChange.bind(this);
70
+ }
71
+ defineProperties(options) {
72
+ let { regexPublishedLineName, publishedLineName, tripNumber, operator } = options;
73
+ const {
74
+ style,
75
+ speed,
76
+ pixelRatio,
77
+ hoverVehicleId,
78
+ selectedVehicleId,
79
+ filter,
80
+ sort,
81
+ time,
82
+ live
83
+ } = options;
84
+ const initTrackerOptions = {
85
+ style
86
+ };
87
+ Object.keys(initTrackerOptions).forEach((key) => initTrackerOptions[key] === void 0 && delete initTrackerOptions[key]);
88
+ let currSpeed = speed || 1;
89
+ let currTime = time || new Date();
90
+ super.defineProperties(options);
91
+ Object.defineProperties(this, {
92
+ isTrackerLayer: { value: true },
93
+ style: {
94
+ value: (trajectory, viewState) => (style || trackerDefaultStyle)(trajectory, viewState, this)
95
+ },
96
+ speed: {
97
+ get: () => currSpeed,
98
+ set: (newSpeed) => {
99
+ currSpeed = newSpeed;
409
100
  this.start();
410
- } else {
411
- this.stop();
412
101
  }
413
- });
414
-
415
- // To avoid browser hanging when the tab is not visible for a certain amount of time,
416
- // We stop the rendering and the websocket when hide and start again when show.
417
- document.addEventListener(
418
- 'visibilitychange',
419
- this.onDocumentVisibilityChange,
420
- );
421
- }
422
-
423
- terminate() {
424
- document.removeEventListener(
425
- 'visibilitychange',
426
- this.onDocumentVisibilityChange,
427
- );
428
-
429
- this.stop();
430
- unByKey(this.visibilityRef);
431
- if (this.tracker) {
432
- const { canvas } = this.tracker;
433
- const context = canvas.getContext('2d');
434
- context.clearRect(0, 0, canvas.width, canvas.height);
435
- this.tracker = null;
102
+ },
103
+ filter: {
104
+ value: filter,
105
+ writable: true
106
+ },
107
+ sort: {
108
+ value: sort,
109
+ writable: true
110
+ },
111
+ tracker: { value: null, writable: true },
112
+ styleCache: { value: {} },
113
+ live: {
114
+ value: live === false ? live : true,
115
+ writable: true
116
+ },
117
+ time: {
118
+ get: () => currTime,
119
+ set: (newTime) => {
120
+ currTime = newTime && newTime.getTime ? newTime : new Date(newTime);
121
+ this.renderTrajectories();
122
+ }
123
+ },
124
+ trajectories: {
125
+ value: {},
126
+ writable: true
127
+ },
128
+ renderedTrajectories: {
129
+ get: () => this.tracker && this.tracker.renderedTrajectories || []
130
+ },
131
+ hoverVehicleId: {
132
+ value: hoverVehicleId,
133
+ writable: true
134
+ },
135
+ selectedVehicleId: {
136
+ value: selectedVehicleId,
137
+ writable: true
138
+ },
139
+ pixelRatio: {
140
+ value: pixelRatio || (typeof window !== "undefined" ? window.devicePixelRatio : 1),
141
+ writable: true
142
+ },
143
+ initTrackerOptions: {
144
+ value: initTrackerOptions,
145
+ writable: false
146
+ },
147
+ useRequestAnimationFrame: {
148
+ value: options.useRequestAnimationFrame || false,
149
+ writable: true
150
+ },
151
+ useThrottle: {
152
+ value: options.useThrottle || true,
153
+ writable: true
154
+ },
155
+ useDebounce: {
156
+ value: options.useDebounce || false,
157
+ writable: true
158
+ },
159
+ publishedLineName: {
160
+ get: () => publishedLineName,
161
+ set: (newPublishedLineName) => {
162
+ publishedLineName = newPublishedLineName;
163
+ this.updateFilters();
164
+ }
165
+ },
166
+ tripNumber: {
167
+ get: () => tripNumber,
168
+ set: (newTripNumber) => {
169
+ tripNumber = newTripNumber;
170
+ this.updateFilters();
171
+ }
172
+ },
173
+ operator: {
174
+ get: () => operator,
175
+ set: (newOperator) => {
176
+ operator = newOperator;
177
+ this.updateFilters();
178
+ }
179
+ },
180
+ regexPublishedLineName: {
181
+ get: () => regexPublishedLineName,
182
+ set: (newRegex) => {
183
+ regexPublishedLineName = newRegex;
184
+ this.updateFilters();
185
+ }
186
+ },
187
+ delayDisplay: {
188
+ value: options.delayDisplay || 3e5,
189
+ writable: true
190
+ },
191
+ delayOutlineColor: {
192
+ value: options.delayOutlineColor || "#000000",
193
+ writable: true
436
194
  }
437
- super.terminate();
195
+ });
196
+ this.updateFilters();
197
+ }
198
+ attachToMap(map) {
199
+ super.attachToMap(map);
200
+ this.tracker = new Tracker({
201
+ style: (...args) => this.style(...args),
202
+ ...this.initTrackerOptions
203
+ });
204
+ if (this.visible) {
205
+ this.start();
438
206
  }
439
-
440
- start() {
441
- this.stop();
442
- this.renderTrajectories();
443
- this.startUpdateTime();
444
-
445
- if (this.isClickActive) {
446
- this.onClick(this.onFeatureClick);
447
- }
448
-
449
- if (this.isHoverActive) {
450
- this.onHover(this.onFeatureHover);
451
- }
452
-
453
- this.api.open();
454
- this.api.subscribeTrajectory(
455
- this.mode,
456
- this.onTrajectoryMessage,
457
- this.isUpdateBboxOnMoveEnd,
458
- );
459
- this.api.subscribeDeletedVehicles(
460
- this.mode,
461
- this.onDeleteTrajectoryMessage,
462
- this.isUpdateBboxOnMoveEnd,
463
- );
464
-
465
- if (this.isUpdateBboxOnMoveEnd) {
466
- // Update the bbox on each move end
467
- this.setBbox();
207
+ this.visibilityRef = this.on("change:visible", (evt) => {
208
+ if (evt.target.visible) {
209
+ this.start();
210
+ } else {
211
+ this.stop();
468
212
  }
213
+ });
214
+ document.addEventListener("visibilitychange", this.onDocumentVisibilityChange);
215
+ }
216
+ detachFromMap() {
217
+ document.removeEventListener("visibilitychange", this.onDocumentVisibilityChange);
218
+ this.stop();
219
+ unByKey(this.visibilityRef);
220
+ if (this.tracker) {
221
+ const { canvas } = this.tracker;
222
+ const context = canvas.getContext("2d");
223
+ context.clearRect(0, 0, canvas.width, canvas.height);
224
+ this.tracker = null;
469
225
  }
470
-
471
- /**
472
- * Start the clock.
473
- * @private
474
- */
475
- startUpdateTime() {
476
- this.stopUpdateTime();
477
- this.updateTimeDelay = this.getRefreshTimeInMs();
478
- this.updateTimeInterval = setInterval(() => {
479
- // When live=true, we update the time with new Date();
480
- this.time = this.live
481
- ? new Date()
482
- : this.time.getTime() + this.updateTimeDelay * this.speed;
483
- }, this.updateTimeDelay);
226
+ super.detachFromMap();
227
+ }
228
+ start() {
229
+ this.stop();
230
+ this.renderTrajectories();
231
+ this.startUpdateTime();
232
+ if (this.isClickActive) {
233
+ this.onClick(this.onFeatureClick);
484
234
  }
485
-
486
- stop() {
487
- this.api.unsubscribeTrajectory(this.onTrajectoryMessage);
488
- this.api.unsubscribeDeletedVehicles(this.onDeleteTrajectoryMessage);
489
- this.api.close();
235
+ if (this.isHoverActive) {
236
+ this.onHover(this.onFeatureHover);
490
237
  }
491
-
492
- /**
493
- * Stop the clock.
494
- * @private
495
- */
496
- stopUpdateTime() {
497
- if (this.updateTimeInterval) {
498
- clearInterval(this.updateTimeInterval);
499
- }
238
+ this.api.open();
239
+ this.api.subscribeTrajectory(this.mode, this.onTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
240
+ this.api.subscribeDeletedVehicles(this.mode, this.onDeleteTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
241
+ if (this.isUpdateBboxOnMoveEnd) {
242
+ this.setBbox();
500
243
  }
501
-
502
- /**
503
- * Launch renderTrajectories. it avoids duplicating code in renderTrajectories method.
504
- *
505
- * @param {object} viewState The view state of the map.
506
- * @param {number[2]} viewState.center Center coordinate of the map in mercator coordinate.
507
- * @param {number[4]} viewState.extent Extent of the map in mercator coordinates.
508
- * @param {number[2]} viewState.size Size ([width, height]) of the canvas to render.
509
- * @param {number} [viewState.rotation = 0] Rotation of the map to render.
510
- * @param {number} viewState.resolution Resolution of the map to render.
511
- * @param {boolean} noInterpolate If true trajectories are not interpolated but
512
- * drawn at the last known coordinate. Use this for performance optimization
513
- * during map navigation.
514
- * @private
515
- */
516
- renderTrajectoriesInternal(viewState, noInterpolate) {
517
- if (!this.tracker) {
518
- return false;
519
- }
520
-
521
- const time = this.live ? Date.now() : this.time;
522
-
523
- const trajectories = Object.values(this.trajectories);
524
-
525
- // console.time('sort');
526
- if (this.sort) {
527
- trajectories.sort(this.sort);
528
- }
529
- // console.timeEnd('sort');
530
-
531
- // console.time('render');
532
- this.renderState = this.tracker.renderTrajectories(
533
- trajectories,
534
- { ...viewState, pixelRatio: this.pixelRatio, time },
535
- {
536
- noInterpolate:
537
- viewState.zoom < this.minZoomInterpolation ? true : noInterpolate,
538
- hoverVehicleId: this.hoverVehicleId,
539
- selectedVehicleId: this.selectedVehicleId,
540
- iconScale: this.iconScale,
541
- delayDisplay: this.delayDisplay,
542
- delayOutlineColor: this.delayOutlineColor,
543
- },
544
- );
545
-
546
- // console.timeEnd('render');
547
- return true;
244
+ }
245
+ startUpdateTime() {
246
+ this.stopUpdateTime();
247
+ this.updateTimeDelay = this.getRefreshTimeInMs();
248
+ this.updateTimeInterval = setInterval(() => {
249
+ this.time = this.live ? new Date() : this.time.getTime() + this.updateTimeDelay * this.speed;
250
+ }, this.updateTimeDelay);
251
+ }
252
+ stop() {
253
+ this.api.unsubscribeTrajectory(this.onTrajectoryMessage);
254
+ this.api.unsubscribeDeletedVehicles(this.onDeleteTrajectoryMessage);
255
+ this.api.close();
256
+ }
257
+ stopUpdateTime() {
258
+ if (this.updateTimeInterval) {
259
+ clearInterval(this.updateTimeInterval);
548
260
  }
549
-
550
- /**
551
- * Render the trajectories requesting an animation frame and cancelling the previous one.
552
- * This function must be overrided by children to provide the correct parameters.
553
- *
554
- * @param {object} viewState The view state of the map.
555
- * @param {number[2]} viewState.center Center coordinate of the map in mercator coordinate.
556
- * @param {number[4]} viewState.extent Extent of the map in mercator coordinates.
557
- * @param {number[2]} viewState.size Size ([width, height]) of the canvas to render.
558
- * @param {number} [viewState.rotation = 0] Rotation of the map to render.
559
- * @param {number} viewState.resolution Resolution of the map to render.
560
- * @param {boolean} noInterpolate If true trajectories are not interpolated but
561
- * drawn at the last known coordinate. Use this for performance optimization
562
- * during map navigation.
563
- * @private
564
- */
565
- renderTrajectories(viewState, noInterpolate) {
566
- if (this.requestId) {
567
- cancelAnimationFrame(this.requestId);
568
- this.requestId = null;
569
- }
570
-
571
- if (!noInterpolate && this.useRequestAnimationFrame) {
572
- this.requestId = requestAnimationFrame(() => {
573
- this.renderTrajectoriesInternal(viewState, noInterpolate);
574
- });
575
- } else if (!noInterpolate && this.useDebounce) {
576
- this.debounceRenderTrajectories(viewState, noInterpolate);
577
- } else if (!noInterpolate && this.useThrottle) {
578
- this.throttleRenderTrajectories(viewState, noInterpolate);
579
- } else {
261
+ }
262
+ renderTrajectoriesInternal(viewState, noInterpolate) {
263
+ if (!this.tracker) {
264
+ return false;
265
+ }
266
+ const time = this.live ? Date.now() : this.time;
267
+ const trajectories = Object.values(this.trajectories);
268
+ if (this.sort) {
269
+ trajectories.sort(this.sort);
270
+ }
271
+ this.renderState = this.tracker.renderTrajectories(trajectories, { ...viewState, pixelRatio: this.pixelRatio, time }, {
272
+ noInterpolate: viewState.zoom < this.minZoomInterpolation ? true : noInterpolate,
273
+ hoverVehicleId: this.hoverVehicleId,
274
+ selectedVehicleId: this.selectedVehicleId,
275
+ iconScale: this.iconScale,
276
+ delayDisplay: this.delayDisplay,
277
+ delayOutlineColor: this.delayOutlineColor
278
+ });
279
+ return true;
280
+ }
281
+ renderTrajectories(viewState, noInterpolate) {
282
+ if (this.requestId) {
283
+ cancelAnimationFrame(this.requestId);
284
+ this.requestId = null;
285
+ }
286
+ if (!noInterpolate && this.useRequestAnimationFrame) {
287
+ this.requestId = requestAnimationFrame(() => {
580
288
  this.renderTrajectoriesInternal(viewState, noInterpolate);
581
- }
289
+ });
290
+ } else if (!noInterpolate && this.useDebounce) {
291
+ this.debounceRenderTrajectories(viewState, noInterpolate);
292
+ } else if (!noInterpolate && this.useThrottle) {
293
+ this.throttleRenderTrajectories(viewState, noInterpolate);
294
+ } else {
295
+ this.renderTrajectoriesInternal(viewState, noInterpolate);
296
+ }
297
+ }
298
+ setBbox(extent, zoom) {
299
+ const keys = Object.keys(this.trajectories);
300
+ for (let i = keys.length - 1; i >= 0; i -= 1) {
301
+ this.purgeTrajectory(this.trajectories[keys[i]], extent, zoom);
582
302
  }
583
-
584
- setBbox(extent, zoom) {
585
- // Clean trajectories before sending the new bbox
586
- // Purge trajectories:
587
- // - which are outside the extent
588
- // - when it's bus and zoom level is too low for them
589
- const keys = Object.keys(this.trajectories);
590
- for (let i = keys.length - 1; i >= 0; i -= 1) {
591
- this.purgeTrajectory(this.trajectories[keys[i]], extent, zoom);
303
+ const bbox = [...extent];
304
+ if (this.isUpdateBboxOnMoveEnd) {
305
+ bbox.push(zoom);
306
+ if (this.tenant) {
307
+ bbox.push(`tenant=${this.tenant}`);
592
308
  }
593
-
594
- const bbox = [...extent];
595
-
596
- if (this.isUpdateBboxOnMoveEnd) {
597
- bbox.push(zoom);
598
-
599
- if (this.tenant) {
600
- bbox.push(`tenant=${this.tenant}`);
601
- }
602
-
603
- /* @ignore */
604
- this.generalizationLevel = this.generalizationLevelByZoom[zoom];
605
- if (this.generalizationLevel) {
606
- bbox.push(`gen=${this.generalizationLevel}`);
607
- }
309
+ this.generalizationLevel = this.generalizationLevelByZoom[zoom];
310
+ if (this.generalizationLevel) {
311
+ bbox.push(`gen=${this.generalizationLevel}`);
608
312
  }
609
-
610
- this.api.bbox = bbox;
611
313
  }
612
-
613
- setMode(mode) {
614
- if (this.mode === mode) {
615
- return;
616
- }
617
- this.mode = mode;
618
- this.api.subscribeTrajectory(
619
- this.mode,
620
- this.onTrajectoryMessage,
621
- this.isUpdateBboxOnMoveEnd,
622
- );
623
- this.api.subscribeDeletedVehicles(
624
- this.mode,
625
- this.onDeleteTrajectoryMessage,
626
- this.isUpdateBboxOnMoveEnd,
627
- );
314
+ this.api.bbox = bbox;
315
+ }
316
+ setMode(mode) {
317
+ if (this.mode === mode) {
318
+ return;
628
319
  }
629
-
630
- /**
631
- * Get the duration before the next update depending on zoom level.
632
- *
633
- * @private
634
- * @param {number} zoom
635
- */
636
- getRefreshTimeInMs(zoom) {
637
- const roundedZoom = Math.round(zoom);
638
- const timeStep = timeSteps[roundedZoom] || 25;
639
- const nextTick = Math.max(25, timeStep / this.speed);
640
- const nextThrottleTick = Math.min(nextTick, 500);
641
- // TODO: see if this should go elsewhere.
642
- if (this.useThrottle) {
643
- this.throttleRenderTrajectories = throttle(
644
- this.renderTrajectoriesInternal,
645
- nextThrottleTick,
646
- { leading: true, trailing: true },
647
- );
648
- } else if (this.useDebounce) {
649
- this.debounceRenderTrajectories = debounce(
650
- this.renderTrajectoriesInternal,
651
- nextThrottleTick,
652
- { leading: true, trailing: true, maxWait: 5000 },
653
- );
654
- }
655
- if (this.api?.buffer) {
656
- const [, size] = this.api.buffer;
657
- this.api.buffer = [nextThrottleTick, size];
658
- }
659
- return nextTick;
320
+ this.mode = mode;
321
+ this.api.subscribeTrajectory(this.mode, this.onTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
322
+ this.api.subscribeDeletedVehicles(this.mode, this.onDeleteTrajectoryMessage, this.isUpdateBboxOnMoveEnd);
323
+ }
324
+ getRefreshTimeInMs(zoom) {
325
+ const roundedZoom = Math.round(zoom);
326
+ const timeStep = timeSteps[roundedZoom] || 25;
327
+ const nextTick = Math.max(25, timeStep / this.speed);
328
+ const nextThrottleTick = Math.min(nextTick, 500);
329
+ if (this.useThrottle) {
330
+ this.throttleRenderTrajectories = throttle(this.renderTrajectoriesInternal, nextThrottleTick, { leading: true, trailing: true });
331
+ } else if (this.useDebounce) {
332
+ this.debounceRenderTrajectories = debounce(this.renderTrajectoriesInternal, nextThrottleTick, { leading: true, trailing: true, maxWait: 5e3 });
660
333
  }
661
-
662
- /**
663
- * Get vehicle.
664
- * @param {function} filterFc A function use to filter results.
665
- * @return {Array<Object>} Array of vehicle.
666
- */
667
- getVehicle(filterFc) {
668
- return Object.values(this.trajectories).filter(filterFc);
334
+ if (this.api?.buffer) {
335
+ const [, size] = this.api.buffer;
336
+ this.api.buffer = [nextThrottleTick, size];
669
337
  }
670
-
671
- /**
672
- * Request feature information for a given coordinate.
673
- *
674
- * @param {ol/coordinate~Coordinate} coordinate Coordinate.
675
- * @param {Object} options Options See child classes to see which options are supported.
676
- * @param {number} [options.resolution=1] The resolution of the map.
677
- * @param {number} [options.nb=Infinity] The max number of vehicles to return.
678
- * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate.
679
- */
680
- getFeatureInfoAtCoordinate(coordinate, options = {}) {
681
- const { resolution, nb } = options;
682
- const ext = buffer(
683
- [...coordinate, ...coordinate],
684
- this.hitTolerance * resolution,
685
- );
686
- let trajectories = Object.values(this.trajectories);
687
-
688
- if (this.sort) {
689
- trajectories = trajectories.sort(this.sort);
338
+ return nextTick;
339
+ }
340
+ getVehicle(filterFc) {
341
+ return Object.values(this.trajectories).filter(filterFc);
342
+ }
343
+ getFeatureInfoAtCoordinate(coordinate, options = {}) {
344
+ const { resolution, nb } = options;
345
+ const ext = buffer([...coordinate, ...coordinate], this.hitTolerance * resolution);
346
+ let trajectories = Object.values(this.trajectories);
347
+ if (this.sort) {
348
+ trajectories = trajectories.sort(this.sort);
349
+ }
350
+ const vehicles = [];
351
+ for (let i = 0; i < trajectories.length; i += 1) {
352
+ if (trajectories[i].properties.coordinate && containsCoordinate(ext, trajectories[i].properties.coordinate)) {
353
+ vehicles.push(trajectories[i]);
690
354
  }
691
-
692
- const vehicles = [];
693
- for (let i = 0; i < trajectories.length; i += 1) {
694
- if (
695
- trajectories[i].properties.coordinate &&
696
- containsCoordinate(ext, trajectories[i].properties.coordinate)
697
- ) {
698
- vehicles.push(trajectories[i]);
699
- }
700
- if (vehicles.length === nb) {
701
- break;
702
- }
355
+ if (vehicles.length === nb) {
356
+ break;
703
357
  }
704
-
705
- return Promise.resolve({
706
- layer: this,
707
- features: vehicles.map((vehicle) => this.format.readFeature(vehicle)),
708
- coordinate,
709
- });
710
358
  }
711
-
712
- /**
713
- * Request the stopSequence and the fullTrajectory informations for a vehicle.
714
- *
715
- * @param {string} id The vehicle identifier (the train_id property).
716
- * @return {Promise<{stopSequence: StopSequence, fullTrajectory: FullTrajectory>} A promise that will be resolved with the trajectory informations.
717
- */
718
- getTrajectoryInfos(id) {
719
- // When a vehicle is selected, we request the complete stop sequence and the complete full trajectory.
720
- // Then we combine them in one response and send them to inherited layers.
721
- const promises = [
722
- this.api.getStopSequence(id, this.mode),
723
- this.api.getFullTrajectory(id, this.mode, this.generalizationLevel),
724
- ];
725
-
726
- return Promise.all(promises).then(([stopSequence, fullTrajectory]) => {
727
- const response = {
728
- stopSequence,
729
- fullTrajectory,
730
- };
731
- return response;
732
- });
359
+ return Promise.resolve({
360
+ layer: this,
361
+ features: vehicles.map((vehicle) => this.format.readFeature(vehicle)),
362
+ coordinate
363
+ });
364
+ }
365
+ getTrajectoryInfos(id) {
366
+ const promises = [
367
+ this.api.getStopSequence(id, this.mode),
368
+ this.api.getFullTrajectory(id, this.mode, this.generalizationLevel)
369
+ ];
370
+ return Promise.all(promises).then(([stopSequence, fullTrajectory]) => {
371
+ const response = {
372
+ stopSequence,
373
+ fullTrajectory
374
+ };
375
+ return response;
376
+ });
377
+ }
378
+ purgeTrajectory(trajectory, extent, zoom) {
379
+ const { type, bounds, train_id: id } = trajectory.properties;
380
+ if (!intersects(extent, bounds) || type !== "rail" && zoom < (this.minZoomNonTrain || 9)) {
381
+ this.removeTrajectory(id);
382
+ return true;
733
383
  }
734
-
735
- /**
736
- * Determine if the trajectory is useless and should be removed from the list or not.
737
- * By default, this function exclude vehicles:
738
- * - that have their trajectory outside the current extent and
739
- * - that are not a train and zoom level is lower than layer's minZoomNonTrain property.
740
- *
741
- * @param {TralisTrajectory} trajectory
742
- * @param {Array<number>} extent
743
- * @param {number} zoom
744
- * @return {boolean} if the trajectory must be displayed or not.
745
- * @ignore
746
- */
747
- purgeTrajectory(trajectory, extent, zoom) {
748
- const { type, bounds, train_id: id } = trajectory.properties;
749
- if (
750
- !intersects(extent, bounds) ||
751
- (type !== 'rail' && zoom < (this.minZoomNonTrain || 9))
752
- ) {
753
- this.removeTrajectory(id);
754
- return true;
755
- }
756
- return false;
384
+ return false;
385
+ }
386
+ addTrajectory(trajectory) {
387
+ if (this.filter && !this.filter(trajectory)) {
388
+ return;
757
389
  }
758
-
759
- /**
760
- * Add a trajectory to the tracker.
761
- * @param {TralisTrajectory} trajectory The trajectory to add.
762
- * @private
763
- */
764
- addTrajectory(trajectory) {
765
- if (this.filter && !this.filter(trajectory)) {
766
- return;
767
- }
768
- this.trajectories[trajectory.properties.train_id] = trajectory;
769
- this.renderTrajectories();
390
+ this.trajectories[trajectory.properties.train_id] = trajectory;
391
+ this.renderTrajectories();
392
+ }
393
+ removeTrajectory(id) {
394
+ delete this.trajectories[id];
395
+ }
396
+ onZoomEnd(evt) {
397
+ this.startUpdateTime();
398
+ }
399
+ onDocumentVisibilityChange() {
400
+ if (!this.visible) {
401
+ return;
770
402
  }
771
-
772
- removeTrajectory(id) {
773
- delete this.trajectories[id];
403
+ if (document.hidden) {
404
+ this.stop();
405
+ } else {
406
+ this.start();
774
407
  }
775
-
776
- /**
777
- * On zoomend we adjust the time interval of the update of vehicles positions.
778
- *
779
- * @param evt Event that triggered the function.
780
- * @private
781
- */
782
- // eslint-disable-next-line no-unused-vars
783
- onZoomEnd(evt) {
784
- this.startUpdateTime();
408
+ }
409
+ onTrajectoryMessage(data) {
410
+ if (!data.content) {
411
+ return;
785
412
  }
786
-
787
- onDocumentVisibilityChange() {
788
- if (!this.visible) {
789
- return;
790
- }
791
- if (document.hidden) {
792
- this.stop();
793
- } else {
794
- this.start();
413
+ const trajectory = data.content;
414
+ const {
415
+ geometry,
416
+ properties: {
417
+ train_id: id,
418
+ time_since_update: timeSinceUpdate,
419
+ raw_coordinates: rawCoordinates
795
420
  }
421
+ } = trajectory;
422
+ if (timeSinceUpdate < 0) {
423
+ return;
796
424
  }
797
-
798
- /**
799
- * Callback on websocket's trajectory channel events.
800
- * It adds a trajectory to the list.
801
- *
802
- * @private
803
- */
804
- onTrajectoryMessage(data) {
805
- if (!data.content) {
806
- return;
807
- }
808
- const trajectory = data.content;
809
-
810
- const {
811
- geometry,
812
- properties: {
813
- train_id: id,
814
- time_since_update: timeSinceUpdate,
815
- raw_coordinates: rawCoordinates,
816
- },
817
- } = trajectory;
818
-
819
- // ignore old events [SBAHNM-97]
820
- if (timeSinceUpdate < 0) {
821
- return;
822
- }
823
-
824
- // console.time(`onTrajectoryMessage${data.content.properties.train_id}`);
825
- if (this.purgeTrajectory(trajectory)) {
826
- return;
827
- }
828
-
829
- if (
830
- this.debug &&
831
- this.mode === TralisModes.TOPOGRAPHIC &&
832
- rawCoordinates
833
- ) {
834
- trajectory.properties.olGeometry = {
835
- type: 'Point',
836
- coordinates: fromLonLat(
837
- rawCoordinates,
838
- this.map.getView().getProjection(),
839
- ),
840
- };
841
- } else {
842
- trajectory.properties.olGeometry = this.format.readGeometry(geometry);
843
- }
844
-
845
- // TODO Make sure the timeOffset is useful. May be we can remove it.
846
- trajectory.properties.timeOffset = Date.now() - data.timestamp;
847
- this.addTrajectory(trajectory);
425
+ if (this.purgeTrajectory(trajectory)) {
426
+ return;
848
427
  }
849
-
850
- /**
851
- * Callback on websocket's deleted_vehicles channel events.
852
- * It removes the trajectory from the list.
853
- *
854
- * @private
855
- * @override
856
- */
857
- onDeleteTrajectoryMessage(data) {
858
- if (!data.content) {
859
- return;
860
- }
861
-
862
- this.removeTrajectory(data.content);
428
+ if (this.debug && this.mode === TralisModes.TOPOGRAPHIC && rawCoordinates) {
429
+ trajectory.properties.olGeometry = {
430
+ type: "Point",
431
+ coordinates: fromLonLat(rawCoordinates, this.map.getView().getProjection())
432
+ };
433
+ } else {
434
+ trajectory.properties.olGeometry = this.format.readGeometry(geometry);
863
435
  }
864
-
865
- /**
866
- * Callback when user moves the mouse/pointer over the map.
867
- * It sets the layer's hoverVehicleId property with the current hovered vehicle's id.
868
- *
869
- * @private
870
- * @override
871
- */
872
- onFeatureHover(features, layer, coordinate) {
873
- const [feature] = features;
874
- let id = null;
875
- if (feature) {
876
- id = feature.get('train_id');
877
- }
878
- if (this.hoverVehicleId !== id) {
879
- /** @ignore */
880
- this.hoverVehicleId = id;
881
- this.renderTrajectories(true);
882
- }
436
+ trajectory.properties.timeOffset = Date.now() - data.timestamp;
437
+ this.addTrajectory(trajectory);
438
+ }
439
+ onDeleteTrajectoryMessage(data) {
440
+ if (!data.content) {
441
+ return;
883
442
  }
884
-
885
- /**
886
- * Callback when user clicks on the map.
887
- * It sets the layer's selectedVehicleId property with the current selected vehicle's id.
888
- *
889
- * @private
890
- * @override
891
- */
892
- onFeatureClick(features, layer, coordinate) {
893
- const [feature] = features;
894
- let id = null;
895
- if (feature) {
896
- id = feature.get('train_id');
897
- }
898
- if (this.selectedVehicleId !== id) {
899
- /** @ignore */
900
- this.selectedVehicleId = id;
901
- this.selectedVehicle = feature;
902
- this.renderTrajectories(true);
903
- }
443
+ this.removeTrajectory(data.content);
444
+ }
445
+ onFeatureHover(features, layer, coordinate) {
446
+ const [feature] = features;
447
+ let id = null;
448
+ if (feature) {
449
+ id = feature.get("train_id");
904
450
  }
905
-
906
- /**
907
- * Update filter provided by properties or permalink.
908
- */
909
- updateFilters() {
910
- // Setting filters from the permalink if no values defined by the layer.
911
- const parameters = qs.parse(window.location.search.toLowerCase());
912
- const publishedName = this.publishedLineName || parameters[LINE_FILTER];
913
- const tripNumber = this.tripNumber || parameters[ROUTE_FILTER];
914
- const operator = this.operator || parameters[OPERATOR_FILTER];
915
- const { regexPublishedLineName } = this;
916
-
917
- // Only overrides filter function if one of this property exists.
918
- if (publishedName || tripNumber || operator || regexPublishedLineName) {
919
- // filter is the property in TrackerLayerMixin.
920
- this.filter = createFilters(
921
- publishedName,
922
- tripNumber,
923
- operator,
924
- regexPublishedLineName,
925
- );
926
- }
451
+ if (this.hoverVehicleId !== id) {
452
+ this.hoverVehicleId = id;
453
+ this.renderTrajectories(true);
454
+ }
455
+ }
456
+ onFeatureClick(features, layer, coordinate) {
457
+ const [feature] = features;
458
+ let id = null;
459
+ if (feature) {
460
+ id = feature.get("train_id");
461
+ }
462
+ if (this.selectedVehicleId !== id) {
463
+ this.selectedVehicleId = id;
464
+ this.selectedVehicle = feature;
465
+ this.renderTrajectories(true);
466
+ }
467
+ }
468
+ updateFilters() {
469
+ const parameters = qs.parse(window.location.search.toLowerCase());
470
+ const publishedName = this.publishedLineName || parameters[LINE_FILTER];
471
+ const tripNumber = this.tripNumber || parameters[ROUTE_FILTER];
472
+ const operator = this.operator || parameters[OPERATOR_FILTER];
473
+ const { regexPublishedLineName } = this;
474
+ if (publishedName || tripNumber || operator || regexPublishedLineName) {
475
+ this.filter = createFilters(publishedName, tripNumber, operator, regexPublishedLineName);
927
476
  }
928
- };
929
-
477
+ }
478
+ };
930
479
  export default TralisLayerMixin;