mobility-toolbox-js 2.0.0-beta.24 → 2.0.0-beta.27

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.
@@ -1,15 +1,13 @@
1
1
  import WebSocketAPI from "../common/api/WebSocketAPI";
2
- import {
3
- getModeSuffix,
4
- cleanStopTime,
5
- compareDepartures
6
- } from "./TralisAPIUtils";
7
- export const TralisModes = {
2
+ import cleanStopTime from "../common/utils/cleanStopTime";
3
+ import getModeSuffix from "../common/utils/getRealtimeModeSuffix";
4
+ import compareDepartures from "../common/utils/compareDepartures";
5
+ export const RealtimeModes = {
8
6
  RAW: "raw",
9
7
  TOPOGRAPHIC: "topographic",
10
8
  SCHEMATIC: "schematic"
11
9
  };
12
- class TralisAPI {
10
+ class RealtimeAPI {
13
11
  constructor(options = {}) {
14
12
  this.defineProperties(options);
15
13
  this.subscribedStationUic = null;
@@ -126,8 +124,8 @@ class TralisAPI {
126
124
  this.wsApi.subscribe({ channel }, onSuccess, onError, quiet);
127
125
  }
128
126
  unsubscribe(channel, suffix, cb) {
129
- this.wsApi.unsubscribe(`${channel}${getModeSuffix(TralisModes.SCHEMATIC, TralisModes)}${suffix}`, cb);
130
- this.wsApi.unsubscribe(`${channel}${getModeSuffix(TralisModes.TOPOGRAPHIC, TralisModes)}${suffix || ""}`, cb);
127
+ this.wsApi.unsubscribe(`${channel}${getModeSuffix(RealtimeModes.SCHEMATIC, RealtimeModes)}${suffix}`, cb);
128
+ this.wsApi.unsubscribe(`${channel}${getModeSuffix(RealtimeModes.TOPOGRAPHIC, RealtimeModes)}${suffix || ""}`, cb);
131
129
  }
132
130
  filterDepartures(depObject, sortByMinArrivalTime = false) {
133
131
  const departures = Object.keys(depObject).map((k) => depObject[k]);
@@ -203,7 +201,7 @@ class TralisAPI {
203
201
  }
204
202
  getStation(uic, mode) {
205
203
  const params = {
206
- channel: `station${getModeSuffix(mode, TralisModes)}`,
204
+ channel: `station${getModeSuffix(mode, RealtimeModes)}`,
207
205
  args: uic
208
206
  };
209
207
  return new Promise((resolve, reject) => {
@@ -219,7 +217,7 @@ class TralisAPI {
219
217
  getStations(mode) {
220
218
  const stations = [];
221
219
  const params = {
222
- channel: `station${getModeSuffix(mode, TralisModes)}`
220
+ channel: `station${getModeSuffix(mode, RealtimeModes)}`
223
221
  };
224
222
  window.clearTimeout(this.stationUpdateTimeout);
225
223
  return new Promise((resolve, reject) => {
@@ -238,7 +236,7 @@ class TralisAPI {
238
236
  }
239
237
  subscribeStations(mode, onMessage) {
240
238
  this.unsubscribeStations();
241
- this.subscribe(`station${getModeSuffix(mode, TralisModes)}`, (data) => {
239
+ this.subscribe(`station${getModeSuffix(mode, RealtimeModes)}`, (data) => {
242
240
  if (data.content) {
243
241
  onMessage(data.content);
244
242
  }
@@ -267,24 +265,24 @@ class TralisAPI {
267
265
  }
268
266
  subscribeTrajectory(mode, onMessage, quiet = false) {
269
267
  this.unsubscribeTrajectory(onMessage);
270
- this.subscribe(`trajectory${getModeSuffix(mode, TralisModes)}`, onMessage, null, quiet);
268
+ this.subscribe(`trajectory${getModeSuffix(mode, RealtimeModes)}`, onMessage, null, quiet);
271
269
  }
272
270
  unsubscribeTrajectory(cb) {
273
271
  this.unsubscribe(`trajectory`, "", cb);
274
272
  }
275
273
  subscribeDeletedVehicles(mode, onMessage, quiet = false) {
276
274
  this.unsubscribeDeletedVehicles(onMessage);
277
- this.subscribe(`deleted_vehicles${getModeSuffix(mode, TralisModes)}`, onMessage, null, quiet);
275
+ this.subscribe(`deleted_vehicles${getModeSuffix(mode, RealtimeModes)}`, onMessage, null, quiet);
278
276
  }
279
277
  unsubscribeDeletedVehicles(cb) {
280
278
  this.unsubscribe("deleted_vehicles", "", cb);
281
279
  }
282
280
  getFullTrajectory(id, mode, generalizationLevel) {
283
- const channel = [`full_trajectory${getModeSuffix(mode, TralisModes)}`];
281
+ const channel = [`full_trajectory${getModeSuffix(mode, RealtimeModes)}`];
284
282
  if (id) {
285
283
  channel.push(id);
286
284
  }
287
- if ((!mode || mode === TralisModes.TOPOGRAPHIC) && generalizationLevel) {
285
+ if ((!mode || mode === RealtimeModes.TOPOGRAPHIC) && generalizationLevel) {
288
286
  channel.push(`gen${generalizationLevel}`);
289
287
  }
290
288
  const params = {
@@ -304,7 +302,7 @@ class TralisAPI {
304
302
  }
305
303
  subscribeFullTrajectory(id, mode) {
306
304
  this.unsubscribeFullTrajectory(id);
307
- this.subscribe(`full_trajectory${getModeSuffix(mode, TralisModes)}_${id}`, (data) => {
305
+ this.subscribe(`full_trajectory${getModeSuffix(mode, RealtimeModes)}_${id}`, (data) => {
308
306
  console.log("subscribe full_trajectory", data);
309
307
  }, (err) => {
310
308
  console.log("subscribe full_trajectory error", err);
@@ -356,4 +354,4 @@ class TralisAPI {
356
354
  this.unsubscribe("healthcheck");
357
355
  }
358
356
  }
359
- export default TralisAPI;
357
+ export default RealtimeAPI;
@@ -1,19 +1,19 @@
1
- import { TralisAPI, TralisModes } from ".";
2
- describe("TralisAPI", () => {
3
- let tralisAPI;
1
+ import { RealtimeAPI, RealtimeModes } from ".";
2
+ describe("RealtimeAPI", () => {
3
+ let api;
4
4
  let get;
5
5
  beforeEach(() => {
6
6
  get = jest.fn((params, cb) => {
7
7
  cb({ content: "content" });
8
8
  });
9
- tralisAPI = new TralisAPI();
10
- tralisAPI.wsApi = {
9
+ api = new RealtimeAPI();
10
+ api.wsApi = {
11
11
  get
12
12
  };
13
13
  });
14
14
  describe("#getFullTrajectory() calls fullTrajectory channel", () => {
15
15
  test("without parameters", (done) => {
16
- tralisAPI.getFullTrajectory().then(() => {
16
+ api.getFullTrajectory().then(() => {
17
17
  expect(get.mock.calls.length).toBe(1);
18
18
  expect(get.mock.calls[0][0]).toEqual({
19
19
  channel: "full_trajectory"
@@ -21,10 +21,10 @@ describe("TralisAPI", () => {
21
21
  done();
22
22
  });
23
23
  });
24
- [null, TralisModes.TOPOGRAPHIC].forEach((mode) => {
24
+ [null, RealtimeModes.TOPOGRAPHIC].forEach((mode) => {
25
25
  describe(`using mode ${mode}`, () => {
26
26
  test("using id", (done) => {
27
- tralisAPI.getFullTrajectory("foo", mode).then(() => {
27
+ api.getFullTrajectory("foo", mode).then(() => {
28
28
  expect(get.mock.calls.length).toBe(1);
29
29
  expect(get.mock.calls[0][0]).toEqual({
30
30
  channel: "full_trajectory_foo"
@@ -33,7 +33,7 @@ describe("TralisAPI", () => {
33
33
  });
34
34
  });
35
35
  test("using id and generalizationLevel param", (done) => {
36
- tralisAPI.getFullTrajectory("foo", mode, 5).then(() => {
36
+ api.getFullTrajectory("foo", mode, 5).then(() => {
37
37
  expect(get.mock.calls.length).toBe(1);
38
38
  expect(get.mock.calls[0][0]).toEqual({
39
39
  channel: "full_trajectory_foo_gen5"
@@ -45,7 +45,7 @@ describe("TralisAPI", () => {
45
45
  });
46
46
  describe("using schematic mode ", () => {
47
47
  test("using id", (done) => {
48
- tralisAPI.getFullTrajectory("foo", TralisModes.SCHEMATIC).then(() => {
48
+ api.getFullTrajectory("foo", RealtimeModes.SCHEMATIC).then(() => {
49
49
  expect(get.mock.calls.length).toBe(1);
50
50
  expect(get.mock.calls[0][0]).toEqual({
51
51
  channel: "full_trajectory_schematic_foo"
@@ -54,7 +54,7 @@ describe("TralisAPI", () => {
54
54
  });
55
55
  });
56
56
  test("doesn't use generalizationLevel param", (done) => {
57
- tralisAPI.getFullTrajectory("foo", TralisModes.SCHEMATIC, 10).then(() => {
57
+ api.getFullTrajectory("foo", RealtimeModes.SCHEMATIC, 10).then(() => {
58
58
  expect(get.mock.calls.length).toBe(1);
59
59
  expect(get.mock.calls[0][0]).toEqual({
60
60
  channel: "full_trajectory_schematic_foo"
package/api/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { default as RoutingAPI } from "./RoutingAPI";
2
2
  export { default as StopsAPI } from "./StopsAPI";
3
- export { default as TralisAPI, TralisModes } from "./TralisAPI";
3
+ export { default as RealtimeAPI, RealtimeModes } from "./RealtimeAPI";
@@ -4,11 +4,11 @@ import GeoJSON from "ol/format/GeoJSON";
4
4
  import debounce from "lodash.debounce";
5
5
  import throttle from "lodash.throttle";
6
6
  import { fromLonLat } from "ol/proj";
7
- import trackerDefaultStyle from "../styles/trackerDefaultStyle";
8
- import { TralisAPI, TralisModes } from "../../api";
7
+ import realtimeDefaultStyle from "../styles/realtimeDefaultStyle";
8
+ import { RealtimeAPI, RealtimeModes } from "../../api";
9
9
  import renderTrajectories from "../utils/renderTrajectories";
10
10
  import * as trackerConfig from "../utils/trackerConfig";
11
- export class TralisLayerInterface {
11
+ export class RealtimeLayerInterface {
12
12
  constructor(options = {}) {
13
13
  }
14
14
  attachToMap(map) {
@@ -26,15 +26,15 @@ export class TralisLayerInterface {
26
26
  getTrajectoryInfos(id, mode) {
27
27
  }
28
28
  }
29
- const TralisLayerMixin = (Base) => class extends Base {
29
+ const RealtimeLayerMixin = (Base) => class extends Base {
30
30
  constructor(options = {}) {
31
31
  super({
32
32
  hitTolerance: 10,
33
33
  ...options
34
34
  });
35
35
  this.debug = options.debug;
36
- this.mode = options.mode || TralisModes.TOPOGRAPHIC;
37
- this.api = options.api || new TralisAPI(options);
36
+ this.mode = options.mode || RealtimeModes.TOPOGRAPHIC;
37
+ this.api = options.api || new RealtimeAPI(options);
38
38
  this.tenant = options.tenant || "";
39
39
  this.minZoomNonTrain = options.minZoomNonTrain || 9;
40
40
  this.minZoomInterpolation = options.minZoomInterpolation || 8;
@@ -117,7 +117,7 @@ const TralisLayerMixin = (Base) => class extends Base {
117
117
  value: canvas || document.createElement("canvas")
118
118
  },
119
119
  style: {
120
- value: style || trackerDefaultStyle
120
+ value: style || realtimeDefaultStyle
121
121
  },
122
122
  styleOptions: {
123
123
  value: { ...trackerConfig, ...styleOptions || {} }
@@ -389,7 +389,7 @@ const TralisLayerMixin = (Base) => class extends Base {
389
389
  if (this.purgeTrajectory(trajectory)) {
390
390
  return;
391
391
  }
392
- if (this.debug && this.mode === TralisModes.TOPOGRAPHIC && rawCoordinates) {
392
+ if (this.debug && this.mode === RealtimeModes.TOPOGRAPHIC && rawCoordinates) {
393
393
  trajectory.properties.olGeometry = {
394
394
  type: "Point",
395
395
  coordinates: fromLonLat(rawCoordinates, this.map.getView().getProjection())
@@ -430,4 +430,4 @@ const TralisLayerMixin = (Base) => class extends Base {
430
430
  }
431
431
  }
432
432
  };
433
- export default TralisLayerMixin;
433
+ export default RealtimeLayerMixin;
@@ -1,11 +1,11 @@
1
1
  import { StopsAPI } from "../../api";
2
- export class SearchInterface {
2
+ export class StopFinderInterface {
3
3
  constructor(options = {}) {
4
4
  }
5
5
  search(query, abortController) {
6
6
  }
7
7
  }
8
- const SearchMixin = (Base) => class extends Base {
8
+ const StopFinderMixin = (Base) => class extends Base {
9
9
  constructor(options = {}) {
10
10
  super(options);
11
11
  const { apiParams, apiKey, url } = options;
@@ -107,4 +107,4 @@ const SearchMixin = (Base) => class extends Base {
107
107
  this.clearElt.style.display = "none";
108
108
  }
109
109
  };
110
- export default SearchMixin;
110
+ export default StopFinderMixin;
@@ -1,4 +1,4 @@
1
- export { default as trackerDefaultStyle } from "./trackerDefaultStyle";
2
- export { default as trackerDelayStyle } from "./trackerDelayStyle";
3
- export { default as trackerSimpleStyle } from "./trackerSimpleStyle";
4
- export * from "./trackerDefaultStyle";
1
+ export { default as realtimeDefaultStyle } from "./realtimeDefaultStyle";
2
+ export { default as realtimeDelayStyle } from "./realtimeDelayStyle";
3
+ export { default as realtimeSimpleStyle } from "./realtimeSimpleStyle";
4
+ export * from "./realtimeDefaultStyle";
@@ -91,8 +91,8 @@ export const getTextCanvas = (text, origin, textSize, fillColor, strokeColor, ha
91
91
  }
92
92
  return cacheText[key];
93
93
  };
94
- const styleCache = {};
95
- const style = (trajectory, viewState, options) => {
94
+ const cache = {};
95
+ const realtimeDefaultStyle = (trajectory, viewState, options) => {
96
96
  const {
97
97
  hoverVehicleId,
98
98
  selectedVehicleId,
@@ -150,9 +150,9 @@ const style = (trajectory, viewState, options) => {
150
150
  if (mustDrawText) {
151
151
  key += `${name}${textColor}`;
152
152
  }
153
- if (!styleCache[key]) {
153
+ if (!cache[key]) {
154
154
  if (radius === 0) {
155
- styleCache[key] = null;
155
+ cache[key] = null;
156
156
  return null;
157
157
  }
158
158
  const margin = 1 * pixelRatio;
@@ -194,9 +194,9 @@ const style = (trajectory, viewState, options) => {
194
194
  const text = getTextCanvas(name, origin, textSize, textColor2, circleFillColor, hasStroke2, pixelRatio);
195
195
  ctx.drawImage(text, 0, 0);
196
196
  }
197
- styleCache[key] = canvas;
197
+ cache[key] = canvas;
198
198
  }
199
199
  }
200
- return styleCache[key];
200
+ return cache[key];
201
201
  };
202
- export default style;
202
+ export default realtimeDefaultStyle;
@@ -0,0 +1,8 @@
1
+ import realtimeDefaultStyle from "./realtimeDefaultStyle";
2
+ const realtimeDelayStyle = (trajectory, viewState, options) => {
3
+ return realtimeDefaultStyle(trajectory, viewState, {
4
+ ...options,
5
+ useDelayStyle: true
6
+ });
7
+ };
8
+ export default realtimeDelayStyle;
@@ -1,5 +1,5 @@
1
1
  let canvas;
2
- const style = () => {
2
+ const realtimeSimpleStyle = () => {
3
3
  if (!canvas) {
4
4
  document.createElement("canvas");
5
5
  canvas.width = 15;
@@ -15,4 +15,4 @@ const style = () => {
15
15
  }
16
16
  return canvas;
17
17
  };
18
- export default style;
18
+ export default realtimeSimpleStyle;
@@ -0,0 +1,19 @@
1
+ const cleanStopTime = (content) => {
2
+ if (!content) {
3
+ return;
4
+ }
5
+ content.stations.forEach((station) => {
6
+ station.arrivalTimeWithDelay = station.arrivalTime;
7
+ if (station.departureTime) {
8
+ station.departureTimeWithDelay = station.departureTime;
9
+ }
10
+ if (station.arrivalDelay) {
11
+ station.arrivalTime -= station.arrivalDelay;
12
+ if (station.departureTime) {
13
+ station.departureTime -= station.arrivalDelay;
14
+ }
15
+ }
16
+ });
17
+ return content;
18
+ };
19
+ export default cleanStopTime;
@@ -1,5 +1,4 @@
1
- export const getModeSuffix = (mode, modes) => mode === modes.SCHEMATIC ? "_schematic" : "";
2
- export const compareDepartures = (a, b, sortByMinArrivalTime = false) => {
1
+ const compareDepartures = (a, b, sortByMinArrivalTime = false) => {
3
2
  const topStates = ["HIDDEN", "LEAVING", "BOARDING"];
4
3
  const aTop = a.has_fzo && topStates.indexOf(a.state) > -1;
5
4
  const bTop = b.has_fzo && topStates.indexOf(b.state) > -1;
@@ -23,21 +22,4 @@ export const compareDepartures = (a, b, sortByMinArrivalTime = false) => {
23
22
  }
24
23
  return aDuration - bDuration;
25
24
  };
26
- export const cleanStopTime = (content) => {
27
- if (!content) {
28
- return;
29
- }
30
- content.stations.forEach((station) => {
31
- station.arrivalTimeWithDelay = station.arrivalTime;
32
- if (station.departureTime) {
33
- station.departureTimeWithDelay = station.departureTime;
34
- }
35
- if (station.arrivalDelay) {
36
- station.arrivalTime -= station.arrivalDelay;
37
- if (station.departureTime) {
38
- station.departureTime -= station.arrivalDelay;
39
- }
40
- }
41
- });
42
- return content;
43
- };
25
+ export default compareDepartures;
@@ -0,0 +1,2 @@
1
+ const getModeSuffix = (mode, modes) => mode === modes.SCHEMATIC ? "_schematic" : "";
2
+ export default getModeSuffix;
@@ -3,10 +3,10 @@ import { unByKey } from "ol/Observable";
3
3
  import { getWidth, getHeight } from "ol/extent";
4
4
  import transformRotate from "@turf/transform-rotate";
5
5
  import { point } from "@turf/helpers";
6
- import mixin from "../../common/mixins/TralisLayerMixin";
6
+ import mixin from "../../common/mixins/RealtimeLayerMixin";
7
7
  import Layer from "./Layer";
8
8
  import { getSourceCoordinates, getMercatorResolution } from "../utils";
9
- class TralisLayer extends mixin(Layer) {
9
+ class RealtimeLayer extends mixin(Layer) {
10
10
  constructor(options = {}) {
11
11
  super({
12
12
  ...options
@@ -97,6 +97,9 @@ class TralisLayer extends mixin(Layer) {
97
97
  });
98
98
  }
99
99
  renderTrajectories(noInterpolate) {
100
+ if (!this.map) {
101
+ return;
102
+ }
100
103
  const { width, height } = this.map.getCanvas();
101
104
  const center = this.map.getCenter();
102
105
  const leftBottom = this.map.unproject({
@@ -173,4 +176,4 @@ class TralisLayer extends mixin(Layer) {
173
176
  this.map.getCanvasContainer().style.cursor = features.length ? "pointer" : "auto";
174
177
  }
175
178
  }
176
- export default TralisLayer;
179
+ export default RealtimeLayer;
@@ -0,0 +1,10 @@
1
+ import RealtimeLayer from "./RealtimeLayer";
2
+ let layer;
3
+ describe("RealtimeLayer", () => {
4
+ beforeEach(() => {
5
+ layer = new RealtimeLayer();
6
+ });
7
+ test("should be instanced.", () => {
8
+ expect(layer).toBeInstanceOf(RealtimeLayer);
9
+ });
10
+ });
@@ -1,2 +1,2 @@
1
1
  export { default as Layer } from "./Layer";
2
- export { default as TralisLayer } from "./TralisLayer";
2
+ export { default as RealtimeLayer } from "./RealtimeLayer";