mobility-toolbox-js 1.3.11-beta.2 → 1.3.13-beta.2

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.
@@ -395,7 +395,7 @@ class TralisAPI {
395
395
  * @param {function(response: { content: Vehicle })} onMessage Function called on each message of the channel.
396
396
  */
397
397
  subscribeDeletedVehicles(mode, onMessage) {
398
- this.unsubscribeDeletedVehicles();
398
+ this.unsubscribeDeletedVehicles(onMessage);
399
399
  this.subscribe(
400
400
  `deleted_vehicles${getModeSuffix(mode, TralisModes)}`,
401
401
  onMessage,
package/common/Tracker.js CHANGED
@@ -30,8 +30,8 @@ export default class Tracker {
30
30
  this.renderedTrajectories = [];
31
31
 
32
32
  /**
33
- * Array of ol events key, returned by on() or once().
34
- * @type {Array<key>}
33
+ * Active interpolation calcultion or not. If false, the train will not move until we receive the next message for the websocket.
34
+ * @type {boolean}
35
35
  */
36
36
  this.interpolate = !!opts.interpolate;
37
37
 
@@ -45,13 +45,13 @@ export default class Tracker {
45
45
  * Id of the trajectory which is hovered.
46
46
  * @type {string}
47
47
  */
48
- this.hoverVehicleId = null;
48
+ this.hoverVehicleId = opts.hoverVehicleId;
49
49
 
50
50
  /**
51
51
  * Id of the trajectory which is selected.
52
52
  * @type {string}
53
53
  */
54
- this.selectedVehicleId = null;
54
+ this.selectedVehicleId = opts.selectedVehicleId;
55
55
 
56
56
  /**
57
57
  * Scale the vehicle icons with this value.
@@ -59,6 +59,24 @@ export default class Tracker {
59
59
  */
60
60
  this.iconScale = opts.iconScale;
61
61
 
62
+ /**
63
+ * Function use to filter the features displayed.
64
+ * @type {function}
65
+ */
66
+ this.filter = opts.filter;
67
+
68
+ /**
69
+ * Function use to sort the features displayed.
70
+ * @type {function}
71
+ */
72
+ this.sort = opts.sort;
73
+
74
+ /**
75
+ * Function use to style the features displayed.
76
+ * @type {function}
77
+ */
78
+ this.style = opts.style;
79
+
62
80
  // we draw directly on the canvas since openlayers is too slow.
63
81
  /**
64
82
  * HTML <canvas> element.
@@ -139,24 +157,20 @@ export default class Tracker {
139
157
  /**
140
158
  * Set the filter for tracker features.
141
159
  * @param {function} filter Filter function.
160
+ * @private
161
+ * @deprecated Set the property this.filter directly.
142
162
  */
143
163
  setFilter(filter) {
144
- /**
145
- * Current filter function.
146
- * @type {function}
147
- */
148
164
  this.filter = filter;
149
165
  }
150
166
 
151
167
  /**
152
168
  * Set the sort for tracker features.
153
169
  * @param {function} sort Sort function.
170
+ * @private
171
+ * @deprecated Set the property this.sort directly.
154
172
  */
155
173
  setSort(sort) {
156
- /**
157
- * The sort function for tracker features.
158
- * @type {function}
159
- */
160
174
  this.sort = sort;
161
175
  }
162
176
 
@@ -164,6 +178,7 @@ export default class Tracker {
164
178
  * Set the id of the trajectory which is hovered.
165
179
  * @param {string} id Id of a vehicle.
166
180
  * @private
181
+ * @deprecated Set the property this.hoverVehicleId directly.
167
182
  */
168
183
  setHoverVehicleId(id) {
169
184
  if (id !== this.hoverVehicleId) {
@@ -175,6 +190,7 @@ export default class Tracker {
175
190
  * Set the id of the trajectory which is selected.
176
191
  * @param {string} id Id of a vehicle.
177
192
  * @private
193
+ * @deprecated Set the property this.selectedVehicleId directly.
178
194
  */
179
195
  setSelectedVehicleId(id) {
180
196
  if (id !== this.selectedVehicleId) {
@@ -185,6 +201,8 @@ export default class Tracker {
185
201
  /**
186
202
  * set the scale of the vehicle icons.
187
203
  * @param {number} iconScale Scale value.
204
+ * @private
205
+ * @deprecated Set the property this.iconScale directly.
188
206
  */
189
207
  setIconScale(iconScale) {
190
208
  this.iconScale = iconScale;
@@ -192,14 +210,12 @@ export default class Tracker {
192
210
 
193
211
  /**
194
212
  * Set the tracker style.
195
- * @param {function} s OpenLayers style function.
213
+ * @param {function} style OpenLayers style function.
214
+ * @private
215
+ * @deprecated Set the property this.style directly.
196
216
  */
197
- setStyle(s) {
198
- /**
199
- * Style function.
200
- * @type {function}
201
- */
202
- this.style = s;
217
+ setStyle(style) {
218
+ this.style = style;
203
219
  }
204
220
 
205
221
  /**
@@ -54,6 +54,7 @@ const SearchMixin = (Base) =>
54
54
  apiOptions.url = url;
55
55
  }
56
56
  this.api = new StopsAPI(apiOptions);
57
+ this.abortController = new AbortController();
57
58
  }
58
59
 
59
60
  render(suggestions = []) {
@@ -101,7 +102,9 @@ const SearchMixin = (Base) =>
101
102
  this.inputElt.placeholder = this.placeholder;
102
103
  this.inputElt.autoComplete = 'off';
103
104
  this.inputElt.onkeyup = (evt) => {
104
- this.search(evt.target.value);
105
+ this.abortController.abort();
106
+ this.abortController = new AbortController();
107
+ this.search(evt.target.value, this.abortController);
105
108
  };
106
109
  Object.assign(this.inputElt.style, {
107
110
  padding: '10px 30px 10px 10px',
@@ -124,8 +124,33 @@ const TrackerLayerMixin = (Base) =>
124
124
  isHoverActive: true,
125
125
  ...options,
126
126
  };
127
+
128
+ // Tracker options use to build the tracker.
129
+ const {
130
+ interpolate,
131
+ hoverVehicleId,
132
+ selectedVehicleId,
133
+ filter,
134
+ sort,
135
+ } = options;
136
+ const initTrackerOptions = {
137
+ interpolate,
138
+ hoverVehicleId,
139
+ selectedVehicleId,
140
+ filter,
141
+ sort,
142
+ style,
143
+ };
144
+ Object.keys(initTrackerOptions).forEach(
145
+ (key) =>
146
+ initTrackerOptions[key] === undefined &&
147
+ delete initTrackerOptions[key],
148
+ );
149
+
127
150
  let cuurSpeed = speed || 1;
151
+
128
152
  super.defineProperties(options);
153
+
129
154
  Object.defineProperties(this, {
130
155
  isTrackerLayer: { value: true },
131
156
  isHoverActive: {
@@ -144,17 +169,17 @@ const TrackerLayerMixin = (Base) =>
144
169
  },
145
170
  filter: {
146
171
  get: () => this.tracker.filter,
147
- set: (filter) => {
172
+ set: (newFilter) => {
148
173
  if (this.tracker) {
149
- this.tracker.setFilter(filter);
174
+ this.tracker.filter = newFilter;
150
175
  }
151
176
  },
152
177
  },
153
178
  sort: {
154
179
  get: () => this.tracker.sort,
155
- set: (sort) => {
180
+ set: (newSort) => {
156
181
  if (this.sort) {
157
- this.tracker.setSort(sort);
182
+ this.tracker.sort = newSort;
158
183
  }
159
184
  },
160
185
  },
@@ -200,8 +225,8 @@ const TrackerLayerMixin = (Base) =>
200
225
  get: () => {
201
226
  return this.tracker.hoverVehicleId;
202
227
  },
203
- set: (hoverVehicleId) => {
204
- this.tracker.hoverVehicleId = hoverVehicleId;
228
+ set: (newHoverVehicleId) => {
229
+ this.tracker.hoverVehicleId = newHoverVehicleId;
205
230
  },
206
231
  },
207
232
 
@@ -212,11 +237,19 @@ const TrackerLayerMixin = (Base) =>
212
237
  get: () => {
213
238
  return this.tracker.selectedVehicleId;
214
239
  },
215
- set: (selectedVehicleId) => {
216
- this.tracker.selectedVehicleId = selectedVehicleId;
240
+ set: (newSelectedVehicleId) => {
241
+ this.tracker.selectedVehicleId = newSelectedVehicleId;
217
242
  },
218
243
  },
219
244
 
245
+ /**
246
+ * Options used by the constructor of the Tracker class.
247
+ */
248
+ initTrackerOptions: {
249
+ value: initTrackerOptions,
250
+ writable: false,
251
+ },
252
+
220
253
  /**
221
254
  * If true, encapsulates the renderTrajectories calls in a requestAnimationFrame
222
255
  */
@@ -231,14 +264,25 @@ const TrackerLayerMixin = (Base) =>
231
264
  * Initalize the Tracker.
232
265
  * @param {ol/Map~Map} map
233
266
  * @param {Object} options
234
- * @param {Number} [options.width] Canvas's width.
235
- * @param {Number} [options.height] Canvas's height.
267
+ * @param {number} [options.width] Canvas's width.
268
+ * @param {number} [options.height] Canvas's height.
269
+ * @param {bool} [options.interpolate] Convert an EPSG:3857 coordinate to a canvas pixel (origin top-left).
270
+ * @param {string} [options.hoverVehicleId] Id of the trajectory which is hovered.
271
+ * @param {string} [options.selectedVehicleId] Id of the trajectory which is selected.
272
+ * @param {number} [options.iconScale] Scale the vehicle icons with this value.
236
273
  * @param {function} [options.getPixelFromCoordinate] Convert an EPSG:3857 coordinate to a canvas pixel (origin top-left).
274
+ * @param {function} [options.filter] Function use to filter the features displayed.
275
+ * @param {function} [options.sort] Function use to sort the features displayed.
276
+ * @param {function} [options.style] Function use to style the features displayed.
237
277
  */
238
- init(map, options) {
278
+ init(map, options = {}) {
239
279
  super.init(map);
240
- this.tracker = new Tracker(options);
241
- this.tracker.setStyle((props, r) => this.style(props, r));
280
+
281
+ this.tracker = new Tracker({
282
+ style: (props, r) => this.style(props, r),
283
+ ...this.initTrackerOptions,
284
+ ...options,
285
+ });
242
286
 
243
287
  if (this.visible) {
244
288
  this.start();
@@ -270,8 +314,8 @@ const TrackerLayerMixin = (Base) =>
270
314
  * Start the clock.
271
315
  *
272
316
  * @param {Array<Number>} size Map's size: [width, height].
273
- * @param {Number} zoom Map's zoom level.
274
- * @param {Number} resolution Map's resolution.
317
+ * @param {number} zoom Map's zoom level.
318
+ * @param {number} resolution Map's resolution.
275
319
  */
276
320
  start(size, zoom, resolution) {
277
321
  this.stop();