mobility-toolbox-js 1.5.0-beta.2 → 1.5.1

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.
@@ -367,7 +367,10 @@ export default class Layer extends Observable {
367
367
 
368
368
  return this.getFeatureInfoAtCoordinate(evt.coordinate)
369
369
  .then((featureInfo) => {
370
- this.clickCallbacks.forEach((callback) => callback(featureInfo));
370
+ const { features, layer, coordinate } = featureInfo;
371
+ this.clickCallbacks.forEach((callback) =>
372
+ callback(features, layer, coordinate),
373
+ );
371
374
  return featureInfo;
372
375
  })
373
376
  .catch(() => {
@@ -422,7 +425,10 @@ export default class Layer extends Observable {
422
425
 
423
426
  return this.getFeatureInfoAtCoordinate(evt.coordinate)
424
427
  .then((featureInfo) => {
425
- this.hoverCallbacks.forEach((callback) => callback(featureInfo));
428
+ const { features, layer, coordinate } = featureInfo;
429
+ this.hoverCallbacks.forEach((callback) =>
430
+ callback({ features, layer, coordinate }),
431
+ );
426
432
  return featureInfo;
427
433
  })
428
434
  .catch(() => {
@@ -302,7 +302,6 @@ const TrajservLayerMixin = (TrackerLayer) =>
302
302
  }
303
303
 
304
304
  stop() {
305
- this.journeyId = null;
306
305
  this.stopUpdateTrajectories();
307
306
  this.abortFetchTrajectories();
308
307
  super.stop();
@@ -336,20 +335,78 @@ const TrajservLayerMixin = (TrackerLayer) =>
336
335
  * @private
337
336
  * @override
338
337
  */
339
- onFeatureClick(featureInfo) {
340
- const {
341
- features: [feature],
342
- } = featureInfo;
338
+ onFeatureClick(features, layer, coordinate) {
339
+ const [feature] = features;
343
340
  if (feature) {
344
341
  /** @ignore */
345
342
  this.selectedVehicleId = feature.get('id');
346
343
  /** @ignore */
347
344
  this.journeyId = feature.get('journeyIdentifier');
348
- this.updateTrajectoryStations(this.selectedVehicleId);
345
+ this.highlightTrajectory();
349
346
  } else {
350
347
  this.selectedVehicleId = null;
348
+ this.journeyId = null;
351
349
  }
352
- super.onFeatureClick(featureInfo);
350
+ super.onFeatureClick(features, layer, coordinate);
351
+ }
352
+
353
+ /**
354
+ * Highlight the trajectory of journey.
355
+ * @private
356
+ */
357
+ highlightTrajectory() {
358
+ const { selectedVehicleId, journeyId } = this;
359
+ const promises = [
360
+ // Fetch stations information with a trajectory id.
361
+ this.api.fetchTrajectoryStations(
362
+ this.getParams({
363
+ id: selectedVehicleId,
364
+ time: getUTCTimeString(new Date()),
365
+ }),
366
+ ),
367
+ // Full trajectory.
368
+ this.api.fetchTrajectoryById(
369
+ this.getParams({
370
+ id: journeyId,
371
+ time: getUTCTimeString(new Date()),
372
+ }),
373
+ ),
374
+ ];
375
+
376
+ Promise.all(promises)
377
+ .then(([trajStations, fullTraj]) => {
378
+ const stationsCoords = [];
379
+ if (trajStations) {
380
+ trajStations.stations.forEach((station) => {
381
+ stationsCoords.push(station.coordinates);
382
+ });
383
+ }
384
+
385
+ if (fullTraj) {
386
+ const { p: multiLine, t, c: color } = fullTraj;
387
+ const lineCoords = [];
388
+ multiLine.forEach((line) => {
389
+ line.forEach((point) => {
390
+ lineCoords.push([point.x, point.y]);
391
+ });
392
+ });
393
+
394
+ const lineColor = color ? `#${color}` : getBgColor(t);
395
+ // Don't allow white lines, use red instead.
396
+ const vehiculeColor = /#ffffff/i.test(lineColor)
397
+ ? '#ff0000'
398
+ : lineColor;
399
+
400
+ this.drawFullTrajectory(
401
+ stationsCoords,
402
+ lineCoords,
403
+ this.useDelayStyle ? '#a0a0a0' : vehiculeColor,
404
+ );
405
+ }
406
+ })
407
+ .catch(() => {
408
+ this.drawFullTrajectory();
409
+ });
353
410
  }
354
411
 
355
412
  updateFilters() {
@@ -370,14 +427,6 @@ const TrajservLayerMixin = (TrackerLayer) =>
370
427
  }
371
428
  }
372
429
 
373
- updateTrajectoryStations(trajId) {
374
- const params = this.getParams({
375
- id: trajId,
376
- time: getUTCTimeString(new Date()),
377
- });
378
- return this.api.fetchTrajectoryStations(params);
379
- }
380
-
381
430
  getParams(extraParams = {}) {
382
431
  // The 5 seconds more are used as a buffer if the request takes too long.
383
432
  const requestIntervalInMs = (this.requestIntervalSeconds + 5) * 1000;
@@ -464,6 +513,16 @@ const TrajservLayerMixin = (TrackerLayer) =>
464
513
  });
465
514
  }
466
515
 
516
+ /**
517
+ * Draw the trajectory as a line with points for each stop.
518
+ *
519
+ * @param {Array} stationsCoords Array of station coordinates in EPSG:4326.
520
+ * @param {Array<ol/coordinate~Coordinate>} lineCoords A list of coordinates in EPSG:3857.
521
+ * @param {string} color The color of the line.
522
+ * @private
523
+ */
524
+ drawFullTrajectory(stationsCoords, lineCoords, color) {}
525
+
467
526
  /**
468
527
  * Define the style of the vehicle.
469
528
  *
@@ -147,17 +147,15 @@ const TralisLayerMixin = (TrackerLayer) =>
147
147
  * @private
148
148
  * @override
149
149
  */
150
- onFeatureClick(featureInfo) {
151
- const {
152
- features: [feature],
153
- } = featureInfo;
150
+ onFeatureClick(features, layer, coordinate) {
151
+ const [feature] = features;
154
152
  if (feature) {
155
153
  /** @ignore */
156
154
  this.selectedVehicleId = feature.get('train_id');
157
155
  } else {
158
156
  this.selectedVehicleId = null;
159
157
  }
160
- super.onFeatureClick(featureInfo);
158
+ super.onFeatureClick(features, layer, coordinate);
161
159
  }
162
160
 
163
161
  onMessage(data) {