mobility-toolbox-js 1.6.4 → 1.7.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.
@@ -18,8 +18,7 @@ describe('RoutingAPI', () => {
18
18
  return api
19
19
  .route({
20
20
  mot: 'bus',
21
- via:
22
- '47.3739194713294,8.538274823394632|47.37595378493421,8.537490375951839',
21
+ via: '47.3739194713294,8.538274823394632|47.37595378493421,8.537490375951839',
23
22
  })
24
23
  .then((featureCollection) => {
25
24
  // Correct url
@@ -51,9 +51,7 @@ class TrajservAPI extends API {
51
51
  fetchTrajectories(params, abortController = {}) {
52
52
  return this.fetch(`/trajectory_collection`, params, {
53
53
  signal: abortController.signal,
54
- }).then((data) => {
55
- return translateTrajCollResponse(data.features);
56
- });
54
+ }).then((data) => translateTrajCollResponse(data.features));
57
55
  }
58
56
 
59
57
  /**
@@ -66,9 +64,7 @@ class TrajservAPI extends API {
66
64
  fetchTrajectoryStations(params, abortController = {}) {
67
65
  return this.fetch(`/trajstations`, params, {
68
66
  signal: abortController.signal,
69
- }).then((data) => {
70
- return translateTrajStationsResp(data);
71
- });
67
+ }).then((data) => translateTrajStationsResp(data));
72
68
  }
73
69
  }
74
70
 
@@ -20,8 +20,7 @@ describe('TrajservAPI', () => {
20
20
  return api
21
21
  .fetchTrajectoryById({
22
22
  a: '1',
23
- bbox:
24
- '917830.8141233932,5949421.787168904,926771.7516620635,5953300.375201735',
23
+ bbox: '917830.8141233932,5949421.787168904,926771.7516620635,5953300.375201735',
25
24
  btime: '5:54:7.880',
26
25
  cd: '1',
27
26
  date: '20200701',
@@ -57,8 +56,7 @@ describe('TrajservAPI', () => {
57
56
  return api
58
57
  .fetchTrajectoryById({
59
58
  attr_det: '1',
60
- bbox:
61
- '922972.5439121567,5951167.694705085,923812.5648796591,5951532.096677226',
59
+ bbox: '922972.5439121567,5951167.694705085,923812.5648796591,5951532.096677226',
62
60
  btime: '5:18:31.766',
63
61
  etime: '5:18:41.967',
64
62
  date: '20200701',
@@ -86,8 +84,7 @@ describe('TrajservAPI', () => {
86
84
  return api
87
85
  .fetchTrajectories({
88
86
  attr_det: '1',
89
- bbox:
90
- '922972.5439121567,5951167.694705085,923812.5648796591,5951532.096677226',
87
+ bbox: '922972.5439121567,5951167.694705085,923812.5648796591,5951532.096677226',
91
88
  btime: '5:18:31.766',
92
89
  etime: '5:18:41.967',
93
90
  date: '20200701',
@@ -116,8 +113,7 @@ describe('TrajservAPI', () => {
116
113
  return api
117
114
  .fetchTrajectories({
118
115
  attr_det: '1',
119
- bbox:
120
- '922972.5439121567,5951167.694705085,923812.5648796591,5951532.096677226',
116
+ bbox: '922972.5439121567,5951167.694705085,923812.5648796591,5951532.096677226',
121
117
  btime: '5:18:31.766',
122
118
  etime: '5:18:41.967',
123
119
  date: '20200701',
@@ -147,8 +143,7 @@ describe('TrajservAPI', () => {
147
143
  return api
148
144
  .fetchTrajectoryStations({
149
145
  a: '1',
150
- bbox:
151
- '1059098.4179109985,5917420.543096403,1063534.5137495932,5919344.9260996',
146
+ bbox: '1059098.4179109985,5917420.543096403,1063534.5137495932,5919344.9260996',
152
147
  btime: '6:17:22.722',
153
148
  cd: '1',
154
149
  date: '20200701',
@@ -86,18 +86,14 @@ class TralisAPI {
86
86
 
87
87
  Object.defineProperties(this, {
88
88
  url: {
89
- get: () => {
90
- return url;
91
- },
89
+ get: () => url,
92
90
  set: (newUrl) => {
93
91
  url = newUrl;
94
92
  this.open();
95
93
  },
96
94
  },
97
95
  projection: {
98
- get: () => {
99
- return projection;
100
- },
96
+ get: () => projection,
101
97
  set: (newProjection) => {
102
98
  if (newProjection !== projection) {
103
99
  projection = newProjection;
@@ -108,9 +104,7 @@ class TralisAPI {
108
104
  },
109
105
  },
110
106
  bbox: {
111
- get: () => {
112
- return bbox;
113
- },
107
+ get: () => bbox,
114
108
  set: (newBbox) => {
115
109
  if (JSON.stringify(newBbox) !== JSON.stringify(bbox)) {
116
110
  bbox = newBbox;
@@ -234,20 +228,19 @@ class TralisAPI {
234
228
  * Unsubscribe both modes of a channel.
235
229
  *
236
230
  * @param {string} channel Name of the websocket channel to unsubscribe.
237
- * @param {string} [suffix=''] Suffix to add to the channel name.
231
+ * @param {string} suffix Suffix to add to the channel name.
238
232
  * @param {function} cb Callback function to unsubscribe. If null all subscriptions for the channel will be unsubscribed.
239
233
  * @private
240
234
  */
241
- unsubscribe(channel, suffix = '', cb) {
235
+ unsubscribe(channel, suffix, cb) {
242
236
  this.conn.unsubscribe(
243
237
  `${channel}${getModeSuffix(TralisModes.SCHEMATIC, TralisModes)}${suffix}`,
244
238
  cb,
245
239
  );
246
240
  this.conn.unsubscribe(
247
- `${channel}${getModeSuffix(
248
- TralisModes.TOPOGRAPHIC,
249
- TralisModes,
250
- )}${suffix}`,
241
+ `${channel}${getModeSuffix(TralisModes.TOPOGRAPHIC, TralisModes)}${
242
+ suffix || ''
243
+ }`,
251
244
  cb,
252
245
  );
253
246
  }
@@ -323,7 +316,7 @@ class TralisAPI {
323
316
  * @param {Boolean} sortByMinArrivalTime Sort by minimum arrival time
324
317
  * @param {function(departures:Departure[])} onMessage Function called on each message of the channel.
325
318
  */
326
- subscribeDepartures(stationId, sortByMinArrivalTime = false, onMessage) {
319
+ subscribeDepartures(stationId, sortByMinArrivalTime, onMessage) {
327
320
  window.clearTimeout(this.departureUpdateTimeout);
328
321
  this.unsubscribeDepartures();
329
322
  this.subscribedStationUic = stationId;
@@ -342,7 +335,7 @@ class TralisAPI {
342
335
  this.departureUpdateTimeout = window.setTimeout(() => {
343
336
  const departures = this.filterDepartures(
344
337
  departureObject,
345
- sortByMinArrivalTime,
338
+ sortByMinArrivalTime || false,
346
339
  );
347
340
  onMessage(departures);
348
341
  }, 100);
@@ -584,9 +577,9 @@ class TralisAPI {
584
577
  * @return {Promise<Array<FullTrajectory>>} Return an array of full trajectories.
585
578
  */
586
579
  getFullTrajectories(ids, mode, generalizationLevel) {
587
- const promises = ids.map((id) => {
588
- return this.getFullTrajectory(id, mode, generalizationLevel);
589
- });
580
+ const promises = ids.map((id) =>
581
+ this.getFullTrajectory(id, mode, generalizationLevel),
582
+ );
590
583
  return Promise.all(promises);
591
584
  }
592
585
 
@@ -637,9 +630,9 @@ class TralisAPI {
637
630
  params,
638
631
  (data) => {
639
632
  if (data.content && data.content.length) {
640
- const content = data.content.map((stopSequence) => {
641
- return cleanStopTime(stopSequence);
642
- });
633
+ const content = data.content.map((stopSequence) =>
634
+ cleanStopTime(stopSequence),
635
+ );
643
636
 
644
637
  // Remove the delay from arrivalTime and departureTime
645
638
  resolve(content);
@@ -660,9 +653,7 @@ class TralisAPI {
660
653
  * @return {Promise<Array<StopSequence>>} Return an array of stop sequences.
661
654
  */
662
655
  getStopSequences(ids) {
663
- const promises = ids.map((id) => {
664
- return this.getStopSequence(id);
665
- });
656
+ const promises = ids.map((id) => this.getStopSequence(id));
666
657
  return Promise.all(promises);
667
658
  }
668
659
 
@@ -680,9 +671,9 @@ class TralisAPI {
680
671
  `stopsequence_${id}`,
681
672
  (data) => {
682
673
  if (data.content && data.content.length) {
683
- const content = data.content.map((stopSequence) => {
684
- return cleanStopTime(stopSequence);
685
- });
674
+ const content = data.content.map((stopSequence) =>
675
+ cleanStopTime(stopSequence),
676
+ );
686
677
 
687
678
  // Remove the delay from arrivalTime and departureTime
688
679
  onMessage(content);
@@ -13,35 +13,31 @@ class WebSocketConnector {
13
13
  defineProperties() {
14
14
  Object.defineProperties(this, {
15
15
  closed: {
16
- get: () => {
17
- return !!(
16
+ get: () =>
17
+ !!(
18
18
  this.websocket &&
19
19
  this.websocket.readyState === this.websocket.CLOSED
20
- );
21
- },
20
+ ),
22
21
  },
23
22
  closing: {
24
- get: () => {
25
- return !!(
23
+ get: () =>
24
+ !!(
26
25
  this.websocket &&
27
26
  this.websocket.readyState === this.websocket.CLOSING
28
- );
29
- },
27
+ ),
30
28
  },
31
29
  connecting: {
32
- get: () => {
33
- return !!(
30
+ get: () =>
31
+ !!(
34
32
  this.websocket &&
35
33
  this.websocket.readyState === this.websocket.CONNECTING
36
- );
37
- },
34
+ ),
38
35
  },
39
36
  open: {
40
- get: () => {
41
- return !!(
37
+ get: () =>
38
+ !!(
42
39
  this.websocket && this.websocket.readyState === this.websocket.OPEN
43
- );
44
- },
40
+ ),
45
41
  },
46
42
  /**
47
43
  * Array of message to send on open.
@@ -212,9 +208,9 @@ class WebSocketConnector {
212
208
  return;
213
209
  }
214
210
  this.subscriptions
215
- .filter((s) => {
216
- return s.params.channel === params.channel && (!cb || s.cb === cb);
217
- })
211
+ .filter(
212
+ (s) => s.params.channel === params.channel && (!cb || s.cb === cb),
213
+ )
218
214
  .forEach(({ onMessageCb, onErrorCb }) => {
219
215
  if (this.websocket) {
220
216
  this.websocket.removeEventListener('message', onMessageCb);
@@ -253,9 +249,9 @@ class WebSocketConnector {
253
249
  const { onMessageCb, onErrorCb } = this.listen(params, cb, errorCb);
254
250
  const reqStr = WebSocketConnector.getRequestString('', params);
255
251
 
256
- const index = this.subscriptions.findIndex((subcr) => {
257
- return params.channel === subcr.params.channel && cb === subcr.cb;
258
- });
252
+ const index = this.subscriptions.findIndex(
253
+ (subcr) => params.channel === subcr.params.channel && cb === subcr.cb,
254
+ );
259
255
  const newSubscr = { params, cb, errorCb, onMessageCb, onErrorCb, quiet };
260
256
  if (index > -1) {
261
257
  this.subscriptions[index] = newSubscr;
@@ -279,9 +275,9 @@ class WebSocketConnector {
279
275
  * @private
280
276
  */
281
277
  unsubscribe(source, cb) {
282
- const toRemove = this.subscriptions.filter((s) => {
283
- return s.params.channel === source && (!cb || s.cb === cb);
284
- });
278
+ const toRemove = this.subscriptions.filter(
279
+ (s) => s.params.channel === source && (!cb || s.cb === cb),
280
+ );
285
281
 
286
282
  toRemove.forEach(({ onMessageCb, onErrorCb }) => {
287
283
  if (this.websocket) {
package/common/Tracker.js CHANGED
@@ -291,9 +291,7 @@ export default class Tracker {
291
291
  continue;
292
292
  }
293
293
 
294
- px = px.map((p) => {
295
- return p * pixelRatio;
296
- });
294
+ px = px.map((p) => p * pixelRatio);
297
295
 
298
296
  if (
299
297
  px[0] < 0 ||
package/common/api/api.js CHANGED
@@ -29,9 +29,9 @@ class API extends BaseObject {
29
29
  * Append the apiKey before sending the request.
30
30
  * @ignore
31
31
  */
32
- fetch(path = '', params = {}, config) {
32
+ fetch(path, params, config) {
33
33
  // Clean requets parameters, removing undefined and null values.
34
- const urlParams = { ...params, key: this.apiKey };
34
+ const urlParams = { ...(params || {}), key: this.apiKey };
35
35
  const clone = { ...urlParams };
36
36
  Object.keys(urlParams).forEach(
37
37
  (key) =>
@@ -43,20 +43,21 @@ class API extends BaseObject {
43
43
  new Error(`No apiKey defined for request to ${this.url}`),
44
44
  );
45
45
  }
46
- return fetch(`${this.url}${path}?${qs.stringify(clone)}`, config).then(
47
- (response) => {
48
- try {
49
- return response.json().then((data) => {
50
- if (data.error) {
51
- throw new Error(data.error);
52
- }
53
- return data;
54
- });
55
- } catch (err) {
56
- return Promise.reject(new Error(err));
57
- }
58
- },
59
- );
46
+ return fetch(
47
+ `${this.url}${path || ''}?${qs.stringify(clone)}`,
48
+ config,
49
+ ).then((response) => {
50
+ try {
51
+ return response.json().then((data) => {
52
+ if (data.error) {
53
+ throw new Error(data.error);
54
+ }
55
+ return data;
56
+ });
57
+ } catch (err) {
58
+ return Promise.reject(new Error(err));
59
+ }
60
+ });
60
61
  }
61
62
  }
62
63
 
@@ -48,9 +48,7 @@ class Control extends BaseObject {
48
48
 
49
49
  Object.defineProperties(this, {
50
50
  active: {
51
- get: () => {
52
- return this.get('active');
53
- },
51
+ get: () => this.get('active'),
54
52
  set: (newActive) => {
55
53
  this.set('active', newActive);
56
54
  if (newActive) {
@@ -62,9 +60,7 @@ class Control extends BaseObject {
62
60
  },
63
61
  },
64
62
  map: {
65
- get: () => {
66
- return this.get('map');
67
- },
63
+ get: () => this.get('map'),
68
64
  set: (map) => {
69
65
  // Remove previous node.
70
66
  if (this.map && this.element && this.element.parentNode) {
@@ -130,14 +130,12 @@ export default class Layer extends Observable {
130
130
  writable: true,
131
131
  },
132
132
  copyrights: {
133
- get: () => {
134
- return this.get('copyrights');
135
- },
133
+ get: () => this.get('copyrights'),
136
134
  set: (newCopyrights) => {
137
135
  const arrValue = !Array.isArray(newCopyrights)
138
136
  ? [newCopyrights]
139
137
  : newCopyrights;
140
- return this.set('copyrights', arrValue);
138
+ this.set('copyrights', arrValue);
141
139
  },
142
140
  },
143
141
  visible: {
@@ -376,9 +374,7 @@ export default class Layer extends Observable {
376
374
  );
377
375
  return featureInfo;
378
376
  })
379
- .catch(() => {
380
- return emptyFeatureInfo;
381
- });
377
+ .catch(() => emptyFeatureInfo);
382
378
  }
383
379
 
384
380
  /**
@@ -434,8 +430,6 @@ export default class Layer extends Observable {
434
430
  );
435
431
  return featureInfo;
436
432
  })
437
- .catch(() => {
438
- return emptyFeatureInfo;
439
- });
433
+ .catch(() => emptyFeatureInfo);
440
434
  }
441
435
  }
@@ -456,14 +456,12 @@ describe('Layer', () => {
456
456
  describe('#onUserClickCallback()', () => {
457
457
  const evt = { type: 'signleclick', coordinate: [0, 0] };
458
458
 
459
- const getFeatureInfo = (layer, features = []) => {
460
- return {
461
- features,
462
- layer,
463
- coordinate: evt.coordinate,
464
- event: evt,
465
- };
466
- };
459
+ const getFeatureInfo = (layer, features = []) => ({
460
+ features,
461
+ layer,
462
+ coordinate: evt.coordinate,
463
+ event: evt,
464
+ });
467
465
 
468
466
  test('calls click callback functions', (done) => {
469
467
  const layer = new Layer({
@@ -599,14 +597,12 @@ describe('Layer', () => {
599
597
  describe('#onUserMoveCallback()', () => {
600
598
  const evt = { type: 'pointermove', coordinate: [0, 0] };
601
599
 
602
- const getFeatureInfo = (layer, features = []) => {
603
- return {
604
- features,
605
- layer,
606
- coordinate: evt.coordinate,
607
- event: evt,
608
- };
609
- };
600
+ const getFeatureInfo = (layer, features = []) => ({
601
+ features,
602
+ layer,
603
+ coordinate: evt.coordinate,
604
+ event: evt,
605
+ });
610
606
 
611
607
  test('calls hover callback functions', (done) => {
612
608
  const layer = new Layer({
@@ -137,12 +137,8 @@ const TrackerLayerMixin = (Base) =>
137
137
  live,
138
138
  } = options;
139
139
 
140
- let {
141
- regexPublishedLineName,
142
- publishedLineName,
143
- tripNumber,
144
- operator,
145
- } = options;
140
+ let { regexPublishedLineName, publishedLineName, tripNumber, operator } =
141
+ options;
146
142
 
147
143
  const initTrackerOptions = {
148
144
  pixelRatio: pixelRatio || window.devicePixelRatio || 1,
@@ -266,11 +262,10 @@ const TrackerLayerMixin = (Base) =>
266
262
  * Id of the hovered vehicle.
267
263
  */
268
264
  hoverVehicleId: {
269
- get: () => {
270
- return this.tracker
265
+ get: () =>
266
+ this.tracker
271
267
  ? this.tracker.hoverVehicleId
272
- : this.initTrackerOptions.hoverVehicleId;
273
- },
268
+ : this.initTrackerOptions.hoverVehicleId,
274
269
  set: (newHoverVehicleId) => {
275
270
  if (this.tracker) {
276
271
  this.tracker.hoverVehicleId = newHoverVehicleId;
@@ -334,36 +329,28 @@ const TrackerLayerMixin = (Base) =>
334
329
  * Filter properties used in combination with permalink parameters.
335
330
  */
336
331
  publishedLineName: {
337
- get: () => {
338
- return publishedLineName;
339
- },
332
+ get: () => publishedLineName,
340
333
  set: (newPublishedLineName) => {
341
334
  publishedLineName = newPublishedLineName;
342
335
  this.updateFilters();
343
336
  },
344
337
  },
345
338
  tripNumber: {
346
- get: () => {
347
- return tripNumber;
348
- },
339
+ get: () => tripNumber,
349
340
  set: (newTripNumber) => {
350
341
  tripNumber = newTripNumber;
351
342
  this.updateFilters();
352
343
  },
353
344
  },
354
345
  operator: {
355
- get: () => {
356
- return operator;
357
- },
346
+ get: () => operator,
358
347
  set: (newOperator) => {
359
348
  operator = newOperator;
360
349
  this.updateFilters();
361
350
  },
362
351
  },
363
352
  regexPublishedLineName: {
364
- get: () => {
365
- return regexPublishedLineName;
366
- },
353
+ get: () => regexPublishedLineName,
367
354
  set: (newRegex) => {
368
355
  regexPublishedLineName = newRegex;
369
356
  this.updateFilters();
@@ -398,6 +385,28 @@ const TrackerLayerMixin = (Base) =>
398
385
  // writable: true,
399
386
  // },
400
387
  });
388
+
389
+ // When we use the delay style we want to display delayed train on top by default
390
+ if (this.useDelayStyle && !this.sort) {
391
+ this.sort = (traj1, traj2) => {
392
+ if (traj1.delay === null && traj2.delay !== null) {
393
+ return 1;
394
+ }
395
+ if (traj2.delay === null && traj1.delay !== null) {
396
+ return -1;
397
+ }
398
+
399
+ // We put cancelled train inbetween green and yellow trains
400
+ // >=180000ms corresponds to yellow train
401
+ if (traj1.cancelled && !traj2.cancelled) {
402
+ return traj2.delay < 180000 ? -1 : 1;
403
+ }
404
+ if (traj2.cancelled && !traj1.cancelled) {
405
+ return traj1.delay < 180000 ? 1 : -1;
406
+ }
407
+ return traj2.delay - traj1.delay;
408
+ };
409
+ }
401
410
  }
402
411
 
403
412
  /**
@@ -133,9 +133,7 @@ const TrajservLayerMixin = (TrackerLayer) =>
133
133
  }
134
134
  Object.defineProperties(this, {
135
135
  requestIntervalSeconds: {
136
- get: () => {
137
- return requestIntervalSeconds;
138
- },
136
+ get: () => requestIntervalSeconds,
139
137
  set: (newRequestIntervalSeconds) => {
140
138
  if (newRequestIntervalSeconds !== requestIntervalSeconds) {
141
139
  requestIntervalSeconds = newRequestIntervalSeconds;
@@ -58,7 +58,7 @@ export class TralisLayerInterface {
58
58
  * @param {TralisMode} mode The mode to request. If not defined, the layer´s mode propetrty will be used.
59
59
  * @return {Promise<{stopSequence: StopSequence, fullTrajectory: FullTrajectory>} A promise that will be resolved with the trajectory informations.
60
60
  */
61
- getTrajectoryInfos(vehicleId, mode) {}
61
+ getTrajectoryInfos(id, mode) {}
62
62
 
63
63
  /**
64
64
  * Define the style of the vehicle.
@@ -109,9 +109,8 @@ const TralisLayerMixin = (TrackerLayer) =>
109
109
 
110
110
  // Bind callbacks
111
111
  this.onTrajectoryMessage = this.onTrajectoryMessage.bind(this);
112
- this.onDeleteTrajectoryMessage = this.onDeleteTrajectoryMessage.bind(
113
- this,
114
- );
112
+ this.onDeleteTrajectoryMessage =
113
+ this.onDeleteTrajectoryMessage.bind(this);
115
114
  }
116
115
 
117
116
  start() {
@@ -67,27 +67,8 @@ export const textColors = [
67
67
  * @ignore
68
68
  */
69
69
  export const timeSteps = [
70
- 100000,
71
- 50000,
72
- 40000,
73
- 30000,
74
- 20000,
75
- 15000,
76
- 10000,
77
- 5000,
78
- 2000,
79
- 1000,
80
- 400,
81
- 300,
82
- 250,
83
- 180,
84
- 90,
85
- 60,
86
- 50,
87
- 50,
88
- 50,
89
- 50,
90
- 50,
70
+ 100000, 50000, 40000, 30000, 20000, 15000, 10000, 5000, 2000, 1000, 400, 300,
71
+ 250, 180, 90, 60, 50, 50, 50, 50, 50,
91
72
  ];
92
73
 
93
74
  /**
@@ -103,9 +84,9 @@ export const getTypeIndex = (type) => {
103
84
  /**
104
85
  * @ignore
105
86
  */
106
- export const getRadius = (type = 0, zoom) => {
87
+ export const getRadius = (type, zoom) => {
107
88
  try {
108
- const typeIdx = getTypeIndex(type);
89
+ const typeIdx = getTypeIndex(type || 0);
109
90
  return trackerRadiusMapping[typeIdx][zoom];
110
91
  } catch (e) {
111
92
  return 1;
@@ -13,9 +13,9 @@ const removeDuplicate = (array) => {
13
13
  str.toLowerCase(),
14
14
  );
15
15
  const uniqueLowerCaseValues = [...new Set(lowerCasesValues)];
16
- const uniqueValues = uniqueLowerCaseValues.map((uniqueStr) => {
17
- return arrWithoutEmptyValues.find((str) => str.toLowerCase() === uniqueStr);
18
- });
16
+ const uniqueValues = uniqueLowerCaseValues.map((uniqueStr) =>
17
+ arrWithoutEmptyValues.find((str) => str.toLowerCase() === uniqueStr),
18
+ );
19
19
  return uniqueValues;
20
20
  };
21
21
 
@@ -14,7 +14,5 @@ ctx.strokeStyle = 'black';
14
14
  ctx.stroke();
15
15
  ctx.lineWidth = 3;
16
16
 
17
- const style = () => {
18
- return canvas;
19
- };
17
+ const style = () => canvas;
20
18
  export default style;