mobility-toolbox-js 2.0.0-beta.34 → 2.0.0-beta.37

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 (195) hide show
  1. package/README.md +14 -8
  2. package/api/RealtimeAPI.d.ts +6 -6
  3. package/api/RealtimeAPI.d.ts.map +1 -1
  4. package/api/RealtimeAPI.js +170 -197
  5. package/api/RealtimeAPI.test.d.ts +2 -0
  6. package/api/RealtimeAPI.test.d.ts.map +1 -0
  7. package/api/RealtimeAPI.test.js +67 -0
  8. package/api/RoutingAPI.d.ts +6 -2
  9. package/api/RoutingAPI.d.ts.map +1 -1
  10. package/api/RoutingAPI.js +8 -38
  11. package/api/RoutingAPI.test.d.ts +2 -0
  12. package/api/RoutingAPI.test.d.ts.map +1 -0
  13. package/api/RoutingAPI.test.js +29 -0
  14. package/api/StopsAPI.d.ts +1 -1
  15. package/api/StopsAPI.d.ts.map +1 -1
  16. package/api/StopsAPI.js +8 -40
  17. package/api/StopsAPI.test.d.ts +2 -0
  18. package/api/StopsAPI.test.d.ts.map +1 -0
  19. package/api/StopsAPI.test.js +26 -0
  20. package/api/index.d.ts +3 -4
  21. package/api/index.d.ts.map +1 -1
  22. package/api/index.js +3 -10
  23. package/api/typedefs.js +1 -0
  24. package/common/api/HttpAPI.d.ts +2 -2
  25. package/common/api/HttpAPI.d.ts.map +1 -1
  26. package/common/api/HttpAPI.js +21 -48
  27. package/common/api/HttpAPI.test.d.ts +2 -0
  28. package/common/api/HttpAPI.test.d.ts.map +1 -0
  29. package/common/api/HttpAPI.test.js +54 -0
  30. package/common/api/WebSocketAPI.d.ts +8 -4
  31. package/common/api/WebSocketAPI.d.ts.map +1 -1
  32. package/common/api/WebSocketAPI.js +125 -129
  33. package/common/api/WebSocketAPI.test.d.ts +2 -0
  34. package/common/api/WebSocketAPI.test.d.ts.map +1 -0
  35. package/common/api/WebSocketAPI.test.js +380 -0
  36. package/common/controls/Control.d.ts +5 -5
  37. package/common/controls/Control.d.ts.map +1 -1
  38. package/common/controls/Control.js +44 -77
  39. package/common/controls/Control.test.d.ts +2 -0
  40. package/common/controls/Control.test.d.ts.map +1 -0
  41. package/common/controls/Control.test.js +89 -0
  42. package/common/index.js +2 -18
  43. package/common/layers/Layer.d.ts +11 -11
  44. package/common/layers/Layer.d.ts.map +1 -1
  45. package/common/layers/Layer.js +70 -104
  46. package/common/layers/Layer.test.d.ts +2 -0
  47. package/common/layers/Layer.test.d.ts.map +1 -0
  48. package/common/layers/Layer.test.js +137 -0
  49. package/common/mixins/CopyrightMixin.js +23 -52
  50. package/common/mixins/MapboxLayerMixin.js +162 -204
  51. package/common/mixins/RealtimeLayerMixin.js +580 -635
  52. package/common/mixins/StopFinderMixin.d.ts +3 -3
  53. package/common/mixins/StopFinderMixin.d.ts.map +1 -1
  54. package/common/mixins/StopFinderMixin.js +115 -157
  55. package/common/mixins/UserInteractionsLayerMixin.js +142 -175
  56. package/common/mixins/UserInteractionsLayerMixin.test.d.ts +2 -0
  57. package/common/mixins/UserInteractionsLayerMixin.test.d.ts.map +1 -0
  58. package/common/mixins/UserInteractionsLayerMixin.test.js +214 -0
  59. package/common/styles/index.js +4 -24
  60. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -1
  61. package/common/styles/realtimeDefaultStyle.js +67 -78
  62. package/common/styles/realtimeDelayStyle.js +4 -17
  63. package/common/styles/realtimeSimpleStyle.js +5 -7
  64. package/common/typedefs.js +1 -0
  65. package/common/utils/cleanStopTime.js +3 -5
  66. package/common/utils/compareDepartures.d.ts +1 -1
  67. package/common/utils/compareDepartures.d.ts.map +1 -1
  68. package/common/utils/compareDepartures.js +8 -11
  69. package/common/utils/createCanvas.js +3 -5
  70. package/common/utils/createTrackerFilters.d.ts +1 -1
  71. package/common/utils/createTrackerFilters.d.ts.map +1 -1
  72. package/common/utils/createTrackerFilters.js +22 -32
  73. package/common/utils/createTrackerFilters.test.d.ts +2 -0
  74. package/common/utils/createTrackerFilters.test.d.ts.map +1 -0
  75. package/common/utils/createTrackerFilters.test.js +79 -0
  76. package/common/utils/getLayersAsFlatArray.js +6 -8
  77. package/common/utils/getMapboxMapCopyrights.js +9 -11
  78. package/common/utils/getMapboxMapCopyrights.test.d.ts +2 -0
  79. package/common/utils/getMapboxMapCopyrights.test.d.ts.map +1 -0
  80. package/common/utils/getMapboxMapCopyrights.test.js +40 -0
  81. package/common/utils/getMapboxRender.js +12 -15
  82. package/common/utils/getMaplibreRender.js +10 -13
  83. package/common/utils/getRealtimeModeSuffix.js +2 -6
  84. package/common/utils/getUrlWithParams.js +5 -8
  85. package/common/utils/getVehiclePosition.js +20 -23
  86. package/common/utils/index.js +12 -37
  87. package/common/utils/removeDuplicate.d.ts +1 -1
  88. package/common/utils/removeDuplicate.d.ts.map +1 -1
  89. package/common/utils/removeDuplicate.js +6 -21
  90. package/common/utils/removeDuplicate.test.d.ts +2 -0
  91. package/common/utils/removeDuplicate.test.d.ts.map +1 -0
  92. package/common/utils/removeDuplicate.test.js +19 -0
  93. package/common/utils/renderTrajectories.js +31 -43
  94. package/common/utils/sortByDelay.js +4 -6
  95. package/common/utils/timeUtils.js +14 -24
  96. package/common/utils/timeUtils.test.d.ts +2 -0
  97. package/common/utils/timeUtils.test.d.ts.map +1 -0
  98. package/common/utils/timeUtils.test.js +10 -0
  99. package/common/utils/trackerConfig.js +27 -39
  100. package/common/utils/trackerConfig.test.d.ts +2 -0
  101. package/common/utils/trackerConfig.test.d.ts.map +1 -0
  102. package/common/utils/trackerConfig.test.js +23 -0
  103. package/iife.js +3 -5
  104. package/index.d.ts +4 -0
  105. package/index.js +9 -10
  106. package/mapbox/controls/CopyrightControl.d.ts +0 -1
  107. package/mapbox/controls/CopyrightControl.d.ts.map +1 -1
  108. package/mapbox/controls/CopyrightControl.js +18 -38
  109. package/mapbox/controls/index.js +1 -5
  110. package/mapbox/index.js +4 -20
  111. package/mapbox/layers/Layer.d.ts +1 -1
  112. package/mapbox/layers/Layer.d.ts.map +1 -1
  113. package/mapbox/layers/Layer.js +29 -71
  114. package/mapbox/layers/Layer.test.d.ts +2 -0
  115. package/mapbox/layers/Layer.test.d.ts.map +1 -0
  116. package/mapbox/layers/Layer.test.js +204 -0
  117. package/mapbox/layers/RealtimeLayer.d.ts +4 -4
  118. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -1
  119. package/mapbox/layers/RealtimeLayer.js +83 -125
  120. package/mapbox/layers/RealtimeLayer.test.d.ts +2 -0
  121. package/mapbox/layers/RealtimeLayer.test.d.ts.map +1 -0
  122. package/mapbox/layers/RealtimeLayer.test.js +10 -0
  123. package/mapbox/layers/index.js +2 -7
  124. package/mapbox/utils.js +19 -33
  125. package/mbt.js +59 -37
  126. package/mbt.js.map +2 -2
  127. package/mbt.min.js +10 -10
  128. package/mbt.min.js.map +2 -2
  129. package/ol/controls/CopyrightControl.js +26 -47
  130. package/ol/controls/CopyrightControl.test.d.ts +2 -0
  131. package/ol/controls/CopyrightControl.test.d.ts.map +1 -0
  132. package/ol/controls/CopyrightControl.test.js +177 -0
  133. package/ol/controls/RoutingControl.d.ts +6 -5
  134. package/ol/controls/RoutingControl.d.ts.map +1 -1
  135. package/ol/controls/RoutingControl.js +209 -270
  136. package/ol/controls/RoutingControl.test.d.ts +2 -0
  137. package/ol/controls/RoutingControl.test.d.ts.map +1 -0
  138. package/ol/controls/RoutingControl.test.js +150 -0
  139. package/ol/controls/StopFinderControl.js +9 -32
  140. package/ol/controls/StopFinderControl.test.d.ts +2 -0
  141. package/ol/controls/StopFinderControl.test.d.ts.map +1 -0
  142. package/ol/controls/StopFinderControl.test.js +49 -0
  143. package/ol/controls/index.js +3 -9
  144. package/ol/index.js +5 -21
  145. package/ol/layers/Layer.d.ts +1 -1
  146. package/ol/layers/Layer.d.ts.map +1 -1
  147. package/ol/layers/Layer.js +40 -72
  148. package/ol/layers/Layer.test.d.ts +2 -0
  149. package/ol/layers/Layer.test.d.ts.map +1 -0
  150. package/ol/layers/Layer.test.js +196 -0
  151. package/ol/layers/MapboxLayer.d.ts +7 -7
  152. package/ol/layers/MapboxLayer.d.ts.map +1 -1
  153. package/ol/layers/MapboxLayer.js +30 -66
  154. package/ol/layers/MapboxLayer.test.d.ts +2 -0
  155. package/ol/layers/MapboxLayer.test.d.ts.map +1 -0
  156. package/ol/layers/MapboxLayer.test.js +164 -0
  157. package/ol/layers/MapboxStyleLayer.d.ts +3 -3
  158. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -1
  159. package/ol/layers/MapboxStyleLayer.js +92 -135
  160. package/ol/layers/MapboxStyleLayer.test.d.ts +2 -0
  161. package/ol/layers/MapboxStyleLayer.test.d.ts.map +1 -0
  162. package/ol/layers/MapboxStyleLayer.test.js +232 -0
  163. package/ol/layers/MaplibreLayer.d.ts +1 -1
  164. package/ol/layers/MaplibreLayer.d.ts.map +1 -1
  165. package/ol/layers/MaplibreLayer.js +14 -48
  166. package/ol/layers/RealtimeLayer.d.ts +2 -2
  167. package/ol/layers/RealtimeLayer.d.ts.map +1 -1
  168. package/ol/layers/RealtimeLayer.js +111 -147
  169. package/ol/layers/RealtimeLayer.test.d.ts +2 -0
  170. package/ol/layers/RealtimeLayer.test.d.ts.map +1 -0
  171. package/ol/layers/RealtimeLayer.test.js +71 -0
  172. package/ol/layers/RoutingLayer.d.ts +2 -2
  173. package/ol/layers/RoutingLayer.d.ts.map +1 -1
  174. package/ol/layers/RoutingLayer.js +29 -61
  175. package/ol/layers/RoutingLayer.test.d.ts +2 -0
  176. package/ol/layers/RoutingLayer.test.d.ts.map +1 -0
  177. package/ol/layers/RoutingLayer.test.js +39 -0
  178. package/ol/layers/VectorLayer.d.ts +1 -1
  179. package/ol/layers/VectorLayer.d.ts.map +1 -1
  180. package/ol/layers/VectorLayer.js +14 -48
  181. package/ol/layers/VectorLayer.test.d.ts +2 -0
  182. package/ol/layers/VectorLayer.test.d.ts.map +1 -0
  183. package/ol/layers/VectorLayer.test.js +87 -0
  184. package/ol/layers/WMSLayer.d.ts +1 -1
  185. package/ol/layers/WMSLayer.d.ts.map +1 -1
  186. package/ol/layers/WMSLayer.js +34 -68
  187. package/ol/layers/WMSLayer.test.d.ts +2 -0
  188. package/ol/layers/WMSLayer.test.d.ts.map +1 -0
  189. package/ol/layers/WMSLayer.test.js +66 -0
  190. package/ol/layers/index.js +8 -19
  191. package/ol/styles/fullTrajectoryDelayStyle.js +11 -13
  192. package/ol/styles/fullTrajectoryStyle.js +16 -18
  193. package/ol/styles/index.js +2 -7
  194. package/package.json +5 -2
  195. package/setupTests.js +12 -14
@@ -1,48 +1,11 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __assign = (this && this.__assign) || function () {
18
- __assign = Object.assign || function(t) {
19
- for (var s, i = 1, n = arguments.length; i < n; i++) {
20
- s = arguments[i];
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
- t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
- if (ar || !(i in from)) {
31
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
- ar[i] = from[i];
33
- }
34
- }
35
- return to.concat(ar || Array.prototype.slice.call(from));
36
- };
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- var proj_1 = require("ol/proj");
39
- var Observable_1 = require("ol/Observable");
40
- var extent_1 = require("ol/extent");
41
- var transform_rotate_1 = require("@turf/transform-rotate");
42
- var helpers_1 = require("@turf/helpers");
43
- var RealtimeLayerMixin_1 = require("../../common/mixins/RealtimeLayerMixin");
44
- var Layer_1 = require("./Layer");
45
- var utils_1 = require("../utils");
1
+ import { fromLonLat } from 'ol/proj';
2
+ import { unByKey } from 'ol/Observable';
3
+ import { getWidth, getHeight } from 'ol/extent';
4
+ import transformRotate from '@turf/transform-rotate';
5
+ import { point } from '@turf/helpers';
6
+ import mixin from '../../common/mixins/RealtimeLayerMixin';
7
+ import Layer from './Layer';
8
+ import { getSourceCoordinates, getMercatorResolution } from '../utils';
46
9
  /**
47
10
  * Responsible for loading and display data from a Realtime service.
48
11
  *
@@ -60,22 +23,19 @@ var utils_1 = require("../utils");
60
23
  * @extends {Layer}
61
24
  * @implements {RealtimeLayerInterface}
62
25
  */
63
- var RealtimeLayer = /** @class */ (function (_super) {
64
- __extends(RealtimeLayer, _super);
65
- function RealtimeLayer(options) {
66
- if (options === void 0) { options = {}; }
67
- var _this = _super.call(this, __assign({}, options)) || this;
26
+ class RealtimeLayer extends mixin(Layer) {
27
+ constructor(options = {}) {
28
+ super(Object.assign({}, options));
68
29
  /** @ignore */
69
- _this.onLoad = _this.onLoad.bind(_this);
30
+ this.onLoad = this.onLoad.bind(this);
70
31
  /** @ignore */
71
- _this.onMove = _this.onMove.bind(_this);
32
+ this.onMove = this.onMove.bind(this);
72
33
  /** @ignore */
73
- _this.onMoveEnd = _this.onMoveEnd.bind(_this);
34
+ this.onMoveEnd = this.onMoveEnd.bind(this);
74
35
  /** @ignore */
75
- _this.onZoomEnd = _this.onZoomEnd.bind(_this);
36
+ this.onZoomEnd = this.onZoomEnd.bind(this);
76
37
  /** @ignore */
77
- _this.onVisibilityChange = _this.onVisibilityChange.bind(_this);
78
- return _this;
38
+ this.onVisibilityChange = this.onVisibilityChange.bind(this);
79
39
  }
80
40
  /**
81
41
  * Initialize the layer.
@@ -84,19 +44,19 @@ var RealtimeLayer = /** @class */ (function (_super) {
84
44
  * @param {string} beforeId Layer's id before which we want to add the new layer.
85
45
  * @override
86
46
  */
87
- RealtimeLayer.prototype.attachToMap = function (map, beforeId) {
47
+ attachToMap(map, beforeId) {
88
48
  if (!map) {
89
49
  return;
90
50
  }
91
- var canvas = map.getCanvas();
92
- _super.prototype.attachToMap.call(this, map, {
51
+ const canvas = map.getCanvas();
52
+ super.attachToMap(map, {
93
53
  width: canvas.width / this.pixelRatio,
94
54
  height: canvas.height / this.pixelRatio,
95
55
  });
96
56
  this.source = {
97
57
  type: 'canvas',
98
58
  canvas: this.canvas,
99
- coordinates: (0, utils_1.getSourceCoordinates)(map, this.pixelRatio),
59
+ coordinates: getSourceCoordinates(map, this.pixelRatio),
100
60
  // Set to true if the canvas source is animated. If the canvas is static, animate should be set to false to improve performance.
101
61
  animate: true,
102
62
  attribution: this.copyrights && this.copyrights.join(', '),
@@ -120,15 +80,15 @@ var RealtimeLayer = /** @class */ (function (_super) {
120
80
  }
121
81
  this.map.on('load', this.onLoad);
122
82
  this.listeners = [this.on('change:visible', this.onVisibilityChange)];
123
- };
83
+ }
124
84
  /**
125
85
  * Remove listeners from the Mapbox Map.
126
86
  */
127
- RealtimeLayer.prototype.detachFromMap = function () {
87
+ detachFromMap() {
128
88
  if (this.map) {
129
89
  this.map.off('load', this.onLoad);
130
- this.listeners.forEach(function (listener) {
131
- (0, Observable_1.unByKey)(listener);
90
+ this.listeners.forEach((listener) => {
91
+ unByKey(listener);
132
92
  });
133
93
  if (this.map.style && this.map.getLayer(this.key)) {
134
94
  this.map.removeLayer(this.key);
@@ -137,8 +97,8 @@ var RealtimeLayer = /** @class */ (function (_super) {
137
97
  this.map.removeSource(this.key);
138
98
  }
139
99
  }
140
- _super.prototype.detachFromMap.call(this);
141
- };
100
+ super.detachFromMap();
101
+ }
142
102
  /**
143
103
  * Start updating vehicles position.
144
104
  *
@@ -146,91 +106,90 @@ var RealtimeLayer = /** @class */ (function (_super) {
146
106
  * @listens {mapboxgl.map.event:mousemove} Listen to mousemove end.
147
107
  * @override
148
108
  */
149
- RealtimeLayer.prototype.start = function () {
150
- _super.prototype.start.call(this);
109
+ start() {
110
+ super.start();
151
111
  this.map.on('move', this.onMove);
152
112
  this.map.on('moveend', this.onMoveEnd);
153
113
  this.map.on('zoomend', this.onZoomEnd);
154
- };
114
+ }
155
115
  /**
156
116
  * Stop updating vehicles position, and unlisten events.
157
117
  *
158
118
  * @override
159
119
  */
160
- RealtimeLayer.prototype.stop = function () {
161
- _super.prototype.stop.call(this);
120
+ stop() {
121
+ super.stop();
162
122
  if (this.map) {
163
123
  this.map.off('move', this.onMove);
164
124
  this.map.off('moveend', this.onMoveEnd);
165
125
  this.map.off('zoomend', this.onZoomEnd);
166
126
  }
167
- };
168
- RealtimeLayer.prototype.onLoad = function () {
127
+ }
128
+ onLoad() {
169
129
  if (!this.map.getSource(this.key)) {
170
130
  this.map.addSource(this.key, this.source);
171
131
  }
172
132
  if (!this.map.getLayer(this.key)) {
173
133
  this.map.addLayer(this.layer, this.beforeId);
174
134
  }
175
- };
135
+ }
176
136
  /**
177
137
  * Function triggered when the user moves the cursor over the map.
178
138
  * @override
179
139
  */
180
- RealtimeLayer.prototype.onUserMoveCallback = function (evt) {
181
- _super.prototype.onUserMoveCallback.call(this, __assign({ coordinate: (0, proj_1.fromLonLat)(evt.lngLat.toArray()) }, evt));
182
- };
140
+ onUserMoveCallback(evt) {
141
+ super.onUserMoveCallback(Object.assign({ coordinate: fromLonLat(evt.lngLat.toArray()) }, evt));
142
+ }
183
143
  /**
184
144
  * Render the trajectories using current map's size, resolution and rotation.
185
145
  * @param {boolean} noInterpolate if true, renders the vehicles without interpolating theirs positions.
186
146
  * @overrides
187
147
  */
188
- RealtimeLayer.prototype.renderTrajectories = function (noInterpolate) {
148
+ renderTrajectories(noInterpolate) {
189
149
  if (!this.map) {
190
150
  return;
191
151
  }
192
- var _a = this.map.getCanvas(), width = _a.width, height = _a.height;
193
- var center = this.map.getCenter();
152
+ const { width, height } = this.map.getCanvas();
153
+ const center = this.map.getCenter();
194
154
  // We use turf here to have good transform.
195
- var leftBottom = this.map.unproject({
155
+ const leftBottom = this.map.unproject({
196
156
  x: 0,
197
157
  y: height / this.pixelRatio,
198
158
  }); // southWest
199
- var rightTop = this.map.unproject({ x: width / this.pixelRatio, y: 0 }); // north east
200
- var coord0 = (0, transform_rotate_1.default)((0, helpers_1.point)([leftBottom.lng, leftBottom.lat]), -this.map.getBearing(), {
159
+ const rightTop = this.map.unproject({ x: width / this.pixelRatio, y: 0 }); // north east
160
+ const coord0 = transformRotate(point([leftBottom.lng, leftBottom.lat]), -this.map.getBearing(), {
201
161
  pivot: [center.lng, center.lat],
202
162
  }).geometry.coordinates;
203
- var coord1 = (0, transform_rotate_1.default)((0, helpers_1.point)([rightTop.lng, rightTop.lat]), -this.map.getBearing(), {
163
+ const coord1 = transformRotate(point([rightTop.lng, rightTop.lat]), -this.map.getBearing(), {
204
164
  pivot: [center.lng, center.lat],
205
165
  }).geometry.coordinates;
206
- var bounds = __spreadArray(__spreadArray([], (0, proj_1.fromLonLat)(coord0), true), (0, proj_1.fromLonLat)(coord1), true);
207
- var xResolution = (0, extent_1.getWidth)(bounds) / (width / this.pixelRatio);
208
- var yResolution = (0, extent_1.getHeight)(bounds) / (height / this.pixelRatio);
209
- var res = Math.max(xResolution, yResolution);
166
+ const bounds = [...fromLonLat(coord0), ...fromLonLat(coord1)];
167
+ const xResolution = getWidth(bounds) / (width / this.pixelRatio);
168
+ const yResolution = getHeight(bounds) / (height / this.pixelRatio);
169
+ const res = Math.max(xResolution, yResolution);
210
170
  // Coordinate of trajectories are in mercator so we have to pass the proper resolution and center in mercator.
211
- var viewState = {
171
+ const viewState = {
212
172
  size: [width / this.pixelRatio, height / this.pixelRatio],
213
- center: (0, proj_1.fromLonLat)([center.lng, center.lat]),
173
+ center: fromLonLat([center.lng, center.lat]),
214
174
  extent: bounds,
215
175
  resolution: res,
216
176
  zoom: this.map.getZoom(),
217
177
  rotation: -(this.map.getBearing() * Math.PI) / 180,
218
178
  pixelRatio: this.pixelRatio,
219
179
  };
220
- _super.prototype.renderTrajectories.call(this, viewState, noInterpolate);
221
- };
180
+ super.renderTrajectories(viewState, noInterpolate);
181
+ }
222
182
  /**
223
183
  * Return the delay in ms before the next rendering.
224
184
  */
225
- RealtimeLayer.prototype.getRefreshTimeInMs = function () {
226
- return _super.prototype.getRefreshTimeInMs.call(this, this.map.getZoom());
227
- };
228
- RealtimeLayer.prototype.getFeatureInfoAtCoordinate = function (coordinate, options) {
229
- if (options === void 0) { options = {}; }
230
- var resolution = (0, utils_1.getMercatorResolution)(this.map);
231
- return _super.prototype.getFeatureInfoAtCoordinate.call(this, coordinate, __assign({ resolution: resolution }, options));
232
- };
233
- RealtimeLayer.prototype.onVisibilityChange = function () {
185
+ getRefreshTimeInMs() {
186
+ return super.getRefreshTimeInMs(this.map.getZoom());
187
+ }
188
+ getFeatureInfoAtCoordinate(coordinate, options = {}) {
189
+ const resolution = getMercatorResolution(this.map);
190
+ return super.getFeatureInfoAtCoordinate(coordinate, Object.assign({ resolution }, options));
191
+ }
192
+ onVisibilityChange() {
234
193
  if (this.visible && !this.map.getLayer(this.key)) {
235
194
  this.map.addLayer(this.layer, this.beforeId);
236
195
  }
@@ -243,70 +202,69 @@ var RealtimeLayer = /** @class */ (function (_super) {
243
202
  // 'visibilty',
244
203
  // this.visible ? 'visible' : 'none',
245
204
  // );
246
- };
205
+ }
247
206
  /**
248
207
  * Remove the trajectory form the list if necessary.
249
208
  *
250
209
  * @private
251
210
  */
252
- RealtimeLayer.prototype.purgeTrajectory = function (trajectory, extent, zoom) {
253
- return _super.prototype.purgeTrajectory.call(this, trajectory, extent || this.getMercatorExtent(), zoom || Math.floor(this.map.getZoom() + 1));
254
- };
211
+ purgeTrajectory(trajectory, extent, zoom) {
212
+ return super.purgeTrajectory(trajectory, extent || this.getMercatorExtent(), zoom || Math.floor(this.map.getZoom() + 1));
213
+ }
255
214
  /**
256
215
  * Send the current bbox to the websocket
257
216
  */
258
- RealtimeLayer.prototype.setBbox = function (extent, zoom) {
259
- var newExtent = extent;
260
- var newZoom = zoom;
217
+ setBbox(extent, zoom) {
218
+ let newExtent = extent;
219
+ let newZoom = zoom;
261
220
  if (!newExtent && this.isUpdateBboxOnMoveEnd) {
262
221
  newExtent = extent || this.getMercatorExtent();
263
222
  newZoom = Math.floor(this.getOlZoom());
264
223
  }
265
- _super.prototype.setBbox.call(this, newExtent, newZoom);
266
- };
224
+ super.setBbox(newExtent, newZoom);
225
+ }
267
226
  /**
268
227
  * Callback on 'move' event.
269
228
  *
270
229
  * @private
271
230
  */
272
- RealtimeLayer.prototype.onMove = function () {
231
+ onMove() {
273
232
  this.renderTrajectories();
274
- };
275
- RealtimeLayer.prototype.renderTrajectoriesInternal = function (viewState, noInterpolate) {
276
- var render = _super.prototype.renderTrajectoriesInternal.call(this, viewState, noInterpolate);
233
+ }
234
+ renderTrajectoriesInternal(viewState, noInterpolate) {
235
+ const render = super.renderTrajectoriesInternal(viewState, noInterpolate);
277
236
  if (render && this.map.style) {
278
- var extent = (0, utils_1.getSourceCoordinates)(this.map, this.pixelRatio);
279
- var source = this.map.getSource(this.key);
237
+ const extent = getSourceCoordinates(this.map, this.pixelRatio);
238
+ const source = this.map.getSource(this.key);
280
239
  if (source) {
281
240
  source.setCoordinates(extent);
282
241
  }
283
242
  }
284
243
  return render;
285
- };
244
+ }
286
245
  /**
287
246
  * Send the new BBOX to the websocket.
288
247
  *
289
248
  * @private
290
249
  * @override
291
250
  */
292
- RealtimeLayer.prototype.onMoveEnd = function () {
251
+ onMoveEnd() {
293
252
  this.renderTrajectories();
294
253
  if (this.visible && this.isUpdateBboxOnMoveEnd) {
295
254
  this.setBbox();
296
255
  }
297
- };
256
+ }
298
257
  /**
299
258
  * Update the cursor style when hovering a vehicle.
300
259
  *
301
260
  * @private
302
261
  * @override
303
262
  */
304
- RealtimeLayer.prototype.onFeatureHover = function (features, layer, coordinate) {
305
- _super.prototype.onFeatureHover.call(this, features, layer, coordinate);
263
+ onFeatureHover(features, layer, coordinate) {
264
+ super.onFeatureHover(features, layer, coordinate);
306
265
  this.map.getCanvasContainer().style.cursor = features.length
307
266
  ? 'pointer'
308
267
  : 'auto';
309
- };
310
- return RealtimeLayer;
311
- }((0, RealtimeLayerMixin_1.default)(Layer_1.default)));
312
- exports.default = RealtimeLayer;
268
+ }
269
+ }
270
+ export default RealtimeLayer;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=RealtimeLayer.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RealtimeLayer.test.d.ts","sourceRoot":"","sources":["../../../src/mapbox/layers/RealtimeLayer.test.js"],"names":[],"mappings":""}
@@ -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,7 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RealtimeLayer = exports.Layer = void 0;
4
- var Layer_1 = require("./Layer");
5
- Object.defineProperty(exports, "Layer", { enumerable: true, get: function () { return Layer_1.default; } });
6
- var RealtimeLayer_1 = require("./RealtimeLayer");
7
- Object.defineProperty(exports, "RealtimeLayer", { enumerable: true, get: function () { return RealtimeLayer_1.default; } });
1
+ export { default as Layer } from './Layer';
2
+ export { default as RealtimeLayer } from './RealtimeLayer';
package/mapbox/utils.js CHANGED
@@ -1,48 +1,35 @@
1
- "use strict";
2
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
- if (ar || !(i in from)) {
5
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
- ar[i] = from[i];
7
- }
8
- }
9
- return to.concat(ar || Array.prototype.slice.call(from));
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.getSourceCoordinates = exports.getMercatorResolution = void 0;
13
- var extent_1 = require("ol/extent");
14
- var proj_1 = require("ol/proj");
1
+ import { getWidth, getHeight } from 'ol/extent';
2
+ import { fromLonLat } from 'ol/proj';
15
3
  /**
16
4
  * Get the current resolution of a Mapbox map.
17
5
  * @param {mapboxgl.Map} map A map object.
18
6
  * @private
19
7
  */
20
- var getMercatorResolution = function (map) {
21
- var bounds = map.getBounds().toArray();
22
- var a = (0, proj_1.fromLonLat)(bounds[0]);
23
- var b = (0, proj_1.fromLonLat)(bounds[1]);
24
- var extent = __spreadArray(__spreadArray([], a, true), b, true);
25
- var _a = map.getCanvas(), width = _a.width, height = _a.height;
26
- var xResolution = (0, extent_1.getWidth)(extent) / width;
27
- var yResolution = (0, extent_1.getHeight)(extent) / height;
8
+ export const getMercatorResolution = (map) => {
9
+ const bounds = map.getBounds().toArray();
10
+ const a = fromLonLat(bounds[0]);
11
+ const b = fromLonLat(bounds[1]);
12
+ const extent = [...a, ...b];
13
+ const { width, height } = map.getCanvas();
14
+ const xResolution = getWidth(extent) / width;
15
+ const yResolution = getHeight(extent) / height;
28
16
  return Math.max(xResolution, yResolution);
29
17
  };
30
- exports.getMercatorResolution = getMercatorResolution;
31
18
  /**
32
19
  * Get the canvas source coordinates of the current map's extent.
33
20
  * @param {mapboxgl.Map} map A map object.
34
21
  * @private
35
22
  */
36
- var getSourceCoordinates = function (map, pixelRatio) {
23
+ export const getSourceCoordinates = (map, pixelRatio) => {
37
24
  // Requesting getBounds is not enough when we rotate the map, so we request manually each corner.
38
- var _a = map.getCanvas(), width = _a.width, height = _a.height;
39
- var leftTop = map.unproject({ x: 0, y: 0 });
40
- var leftBottom = map.unproject({ x: 0, y: height / pixelRatio }); // southWest
41
- var rightBottom = map.unproject({
25
+ const { width, height } = map.getCanvas();
26
+ const leftTop = map.unproject({ x: 0, y: 0 });
27
+ const leftBottom = map.unproject({ x: 0, y: height / pixelRatio }); // southWest
28
+ const rightBottom = map.unproject({
42
29
  x: width / pixelRatio,
43
30
  y: height / pixelRatio,
44
31
  });
45
- var rightTop = map.unproject({ x: width / pixelRatio, y: 0 }); // north east
32
+ const rightTop = map.unproject({ x: width / pixelRatio, y: 0 }); // north east
46
33
  return [
47
34
  [leftTop.lng, leftTop.lat],
48
35
  [rightTop.lng, rightTop.lat],
@@ -50,8 +37,7 @@ var getSourceCoordinates = function (map, pixelRatio) {
50
37
  [leftBottom.lng, leftBottom.lat],
51
38
  ];
52
39
  };
53
- exports.getSourceCoordinates = getSourceCoordinates;
54
- exports.default = {
55
- getMercatorResolution: exports.getMercatorResolution,
56
- getSourceCoordinates: exports.getSourceCoordinates,
40
+ export default {
41
+ getMercatorResolution,
42
+ getSourceCoordinates,
57
43
  };
package/mbt.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  (() => {
2
3
  var __create = Object.create;
3
4
  var __defProp = Object.defineProperty;
@@ -33355,7 +33356,10 @@ uniform ${i3} ${o3} u_${a3};
33355
33356
  this.apiKey = options.apiKey;
33356
33357
  }
33357
33358
  fetch(path, params, config) {
33358
- if (!this.apiKey && !/key=/.test(this.url)) {
33359
+ if (!this.url) {
33360
+ return Promise.reject(new Error(`No url defined for request to ${this.url}/${path}`));
33361
+ }
33362
+ if (!this.url && !this.apiKey && !/key=/.test(this.url)) {
33359
33363
  return Promise.reject(new Error(`No apiKey defined for request to ${this.url}`));
33360
33364
  }
33361
33365
  const searchParams = params || {};
@@ -33382,10 +33386,7 @@ uniform ${i3} ${o3} u_${a3};
33382
33386
  // src/api/RoutingAPI.ts
33383
33387
  var RoutingAPI = class extends HttpAPI_default {
33384
33388
  constructor(options) {
33385
- super({
33386
- url: "https://api.geops.io/routing/v1/",
33387
- ...options || {}
33388
- });
33389
+ super({ url: "https://api.geops.io/routing/v1/", ...options });
33389
33390
  }
33390
33391
  route(params, config) {
33391
33392
  return this.fetch("", params, config);
@@ -33491,8 +33492,26 @@ uniform ${i3} ${o3} u_${a3};
33491
33492
  send();
33492
33493
  }
33493
33494
  }
33495
+ addEvents(onMessage, onError) {
33496
+ if (this.websocket) {
33497
+ this.websocket.addEventListener("message", onMessage);
33498
+ if (onError) {
33499
+ this.websocket.addEventListener("error", onError);
33500
+ this.websocket.addEventListener("close", onError);
33501
+ }
33502
+ }
33503
+ }
33504
+ removeEvents(onMessage, onError) {
33505
+ if (this.websocket) {
33506
+ this.websocket.removeEventListener("message", onMessage);
33507
+ if (onError) {
33508
+ this.websocket.removeEventListener("error", onError);
33509
+ this.websocket.removeEventListener("close", onError);
33510
+ }
33511
+ }
33512
+ }
33494
33513
  listen(params, cb, errorCb) {
33495
- this.unlisten(params, cb);
33514
+ this.unlisten(params, cb, errorCb);
33496
33515
  const onMessage = (evt) => {
33497
33516
  let data = {};
33498
33517
  try {
@@ -33509,33 +33528,42 @@ uniform ${i3} ${o3} u_${a3};
33509
33528
  }
33510
33529
  });
33511
33530
  };
33512
- if (this.websocket) {
33513
- this.websocket.addEventListener("message", onMessage);
33514
- if (errorCb) {
33515
- this.websocket.addEventListener("error", errorCb);
33516
- this.websocket.addEventListener("close", errorCb);
33517
- }
33518
- }
33531
+ this.addEvents(onMessage, errorCb);
33519
33532
  return { onMessageCb: onMessage, onErrorCb: errorCb };
33520
33533
  }
33521
33534
  unlisten(params, cb) {
33522
- if (!this.websocket) {
33523
- return;
33524
- }
33525
- this.subscriptions.filter((s) => s.params.channel === params.channel && (!cb || s.cb === cb)).forEach(({ onMessageCb, onErrorCb }) => {
33526
- if (this.websocket) {
33527
- this.websocket.removeEventListener("message", onMessageCb);
33528
- if (onErrorCb) {
33529
- this.websocket.removeEventListener("error", onErrorCb);
33530
- this.websocket.removeEventListener("close", onErrorCb);
33531
- }
33532
- }
33535
+ [...this.subscriptions || [], ...this.requests || []].filter((s) => s.params.channel === params.channel && (!cb || s.cb === cb)).forEach(({ onMessageCb, onErrorCb }) => {
33536
+ this.removeEvents(onMessageCb, onErrorCb);
33533
33537
  });
33534
33538
  }
33535
33539
  get(params, cb, errorCb) {
33536
- const reqStr = WebSocketAPI.getRequestString("GET", params);
33537
- this.send(reqStr);
33538
- this.listen(params, cb, errorCb);
33540
+ const requestString = WebSocketAPI.getRequestString("GET", params);
33541
+ this.send(requestString);
33542
+ const once = (callback) => (...args) => {
33543
+ callback(...args);
33544
+ const index2 = this.requests.findIndex((request) => requestString === request.requestString && cb === request.cb);
33545
+ const { onMessageCb: onMessageCb2, onErrorCb: onErrorCb2 } = this.requests[index2];
33546
+ this.removeEvents(onMessageCb2, onErrorCb2);
33547
+ this.requests.splice(index2, 1);
33548
+ };
33549
+ const { onMessageCb, onErrorCb } = this.listen(params, once(cb), once(errorCb));
33550
+ if (!this.requests) {
33551
+ this.requests = [];
33552
+ }
33553
+ const index = this.requests.findIndex((request) => requestString === request.requestString && cb === request.cb);
33554
+ const newReq = {
33555
+ params,
33556
+ requestString,
33557
+ cb,
33558
+ errorCb,
33559
+ onMessageCb,
33560
+ onErrorCb
33561
+ };
33562
+ if (index > -1) {
33563
+ this.requests[index] = newReq;
33564
+ } else {
33565
+ this.requests.push(newReq);
33566
+ }
33539
33567
  }
33540
33568
  subscribe(params, cb, errorCb, quiet = false) {
33541
33569
  const { onMessageCb, onErrorCb } = this.listen(params, cb, errorCb);
@@ -33558,13 +33586,7 @@ uniform ${i3} ${o3} u_${a3};
33558
33586
  unsubscribe(source, cb) {
33559
33587
  const toRemove = this.subscriptions.filter((s) => s.params.channel === source && (!cb || s.cb === cb));
33560
33588
  toRemove.forEach(({ onMessageCb, onErrorCb }) => {
33561
- if (this.websocket) {
33562
- this.websocket.removeEventListener("message", onMessageCb);
33563
- if (onErrorCb) {
33564
- this.websocket.removeEventListener("error", onErrorCb);
33565
- this.websocket.removeEventListener("close", onErrorCb);
33566
- }
33567
- }
33589
+ this.removeEvents(onMessageCb, onErrorCb);
33568
33590
  });
33569
33591
  this.subscriptions = this.subscriptions.filter((s) => s.params.channel !== source || cb && s.cb !== cb);
33570
33592
  if (source && this.subscribed[source] && !this.subscriptions.find((s) => s.params.channel === source) && toRemove.find((subscr) => !subscr.quiet)) {
@@ -35648,7 +35670,7 @@ uniform ${i3} ${o3} u_${a3};
35648
35670
  getTextSize: getTextSize2
35649
35671
  } = options;
35650
35672
  const { zoom, pixelRatio } = viewState;
35651
- let { type, cancelled } = trajectory.properties;
35673
+ let { type } = trajectory.properties;
35652
35674
  const {
35653
35675
  train_id: id,
35654
35676
  line,
@@ -35657,7 +35679,7 @@ uniform ${i3} ${o3} u_${a3};
35657
35679
  operator_provides_realtime_journey: operatorProvidesRealtime
35658
35680
  } = trajectory.properties;
35659
35681
  let { name, text_color: textColor, color } = line || {};
35660
- cancelled = cancelled === true || state === "JOURNEY_CANCELLED";
35682
+ const cancelled = state === "JOURNEY_CANCELLED";
35661
35683
  if (!type) {
35662
35684
  type = "Rail";
35663
35685
  }
@@ -35755,7 +35777,7 @@ uniform ${i3} ${o3} u_${a3};
35755
35777
  var canvas;
35756
35778
  var realtimeSimpleStyle = () => {
35757
35779
  if (!canvas) {
35758
- document.createElement("canvas");
35780
+ canvas = document.createElement("canvas");
35759
35781
  canvas.width = 15;
35760
35782
  canvas.height = 15;
35761
35783
  const ctx = canvas.getContext("2d");