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

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 (69) hide show
  1. package/api/RealtimeAPI.js +640 -0
  2. package/api/RoutingAPI.js +65 -0
  3. package/api/StopsAPI.js +70 -0
  4. package/api/index.js +10 -0
  5. package/api/typedefs.js +72 -0
  6. package/common/api/HttpAPI.d.ts +2 -2
  7. package/common/api/HttpAPI.d.ts.map +1 -1
  8. package/common/api/HttpAPI.js +84 -0
  9. package/common/api/WebSocketAPI.js +320 -0
  10. package/common/controls/Control.js +170 -0
  11. package/common/index.js +18 -0
  12. package/common/layers/Layer.js +257 -0
  13. package/common/mixins/CopyrightMixin.js +72 -0
  14. package/common/mixins/MapboxLayerMixin.js +240 -0
  15. package/common/mixins/RealtimeLayerMixin.js +705 -0
  16. package/common/mixins/StopFinderMixin.js +198 -0
  17. package/common/mixins/UserInteractionsLayerMixin.js +225 -0
  18. package/common/styles/index.js +24 -0
  19. package/common/styles/realtimeDefaultStyle.js +248 -0
  20. package/common/styles/realtimeDelayStyle.js +26 -0
  21. package/common/styles/realtimeSimpleStyle.js +24 -0
  22. package/common/typedefs.js +21 -0
  23. package/common/utils/cleanStopTime.js +30 -0
  24. package/common/utils/compareDepartures.js +37 -0
  25. package/common/utils/createCanvas.js +29 -0
  26. package/common/utils/createTrackerFilters.js +77 -0
  27. package/common/utils/getLayersAsFlatArray.js +16 -0
  28. package/common/utils/getMapboxMapCopyrights.js +26 -0
  29. package/common/utils/getMapboxRender.js +77 -0
  30. package/common/utils/getMaplibreRender.js +38 -0
  31. package/common/utils/getRealtimeModeSuffix.js +11 -0
  32. package/common/utils/getUrlWithParams.js +21 -0
  33. package/common/utils/getVehiclePosition.js +66 -0
  34. package/common/utils/index.js +37 -0
  35. package/common/utils/removeDuplicate.js +30 -0
  36. package/common/utils/renderTrajectories.js +119 -0
  37. package/common/utils/sortByDelay.js +22 -0
  38. package/common/utils/timeUtils.js +49 -0
  39. package/common/utils/trackerConfig.js +182 -0
  40. package/iife.js +7 -0
  41. package/index.js +11 -0
  42. package/mapbox/controls/CopyrightControl.js +73 -0
  43. package/mapbox/controls/index.js +6 -0
  44. package/mapbox/index.js +20 -0
  45. package/mapbox/layers/Layer.js +139 -0
  46. package/mapbox/layers/RealtimeLayer.js +312 -0
  47. package/mapbox/layers/index.js +7 -0
  48. package/mapbox/utils.js +57 -0
  49. package/mbt.js.map +2 -2
  50. package/mbt.min.js.map +2 -2
  51. package/ol/controls/CopyrightControl.js +90 -0
  52. package/ol/controls/RoutingControl.js +683 -0
  53. package/ol/controls/StopFinderControl.js +59 -0
  54. package/ol/controls/index.js +9 -0
  55. package/ol/index.js +21 -0
  56. package/ol/layers/Layer.js +180 -0
  57. package/ol/layers/MapboxLayer.js +137 -0
  58. package/ol/layers/MapboxStyleLayer.js +383 -0
  59. package/ol/layers/MaplibreLayer.js +69 -0
  60. package/ol/layers/RealtimeLayer.js +330 -0
  61. package/ol/layers/RoutingLayer.js +116 -0
  62. package/ol/layers/VectorLayer.js +72 -0
  63. package/ol/layers/WMSLayer.js +106 -0
  64. package/ol/layers/index.js +19 -0
  65. package/ol/styles/fullTrajectoryDelayStyle.js +35 -0
  66. package/ol/styles/fullTrajectoryStyle.js +46 -0
  67. package/ol/styles/index.js +7 -0
  68. package/package.json +1 -1
  69. package/setupTests.js +15 -0
@@ -0,0 +1,383 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ /* eslint-disable no-param-reassign */
30
+ var Layer_1 = require("./Layer");
31
+ /**
32
+ * Layer for visualizing a specific set of layer from a MapboxLayer.
33
+ *
34
+ * @example
35
+ * import { MapboxLayer, MapboxStyleLayer } from 'mobility-toolbox-js/ol';
36
+ *
37
+ * const mapboxLayer = new MapboxLayer({
38
+ * url: 'https://maps.geops.io/styles/travic_v2/style.json?key=[yourApiKey]',
39
+ * });
40
+ *
41
+ * const layer = new MapboxStyleLayer({
42
+ * mapboxLayer: mapboxLayer,
43
+ * styleLayersFilter: () => {},
44
+ * });
45
+ *
46
+ * @classproperty {ol/Map~Map} map - The map where the layer is displayed.
47
+ * @extends {Layer}
48
+ */
49
+ var MapboxStyleLayer = /** @class */ (function (_super) {
50
+ __extends(MapboxStyleLayer, _super);
51
+ /**
52
+ * Constructor.
53
+ *
54
+ * @param {Object} options
55
+ * @param {MapboxLayer} [options.mapboxLayer] The MapboxLayer to use.
56
+ * @param {Function} [options.styleLayersFilter] Filter function to decide which style layer to display.
57
+ */
58
+ function MapboxStyleLayer(options) {
59
+ if (options === void 0) { options = {}; }
60
+ var _this = _super.call(this, options) || this;
61
+ /**
62
+ * MapboxLayer provided for the style Layer.
63
+ * @type {MapboxLayer}
64
+ * @private
65
+ */
66
+ _this.mapboxLayer = options.mapboxLayer;
67
+ /**
68
+ * Define if the layer has data to display in the current mapbox layer.
69
+ */
70
+ _this.disabled = false;
71
+ /**
72
+ * Function to filter features to be displayed.
73
+ * @type {function}
74
+ * @private
75
+ */
76
+ _this.styleLayersFilter = options.styleLayersFilter;
77
+ /**
78
+ * Mapbox style layer id where to add the style layers.
79
+ * See [mapbox.map.addLayer](https://docs.mapbox.com/mapbox-gl-js/api/map/#map#addlayer) documentation.
80
+ * @type {String}
81
+ * @private
82
+ */
83
+ _this.beforeId = options.beforeId;
84
+ /**
85
+ * Function to filter features for getFeatureInfoAtCoordinate method.
86
+ * @type {function}
87
+ * @private
88
+ */
89
+ _this.featureInfoFilter = options.featureInfoFilter || (function (obj) { return obj; });
90
+ /**
91
+ * Function to query the rendered features.
92
+ * @type {function}
93
+ * @private
94
+ */
95
+ _this.queryRenderedLayersFilter = options.queryRenderedLayersFilter;
96
+ /**
97
+ * Array of features to highlight.
98
+ * @type {Array<ol/Feature~Feature>}
99
+ * @private
100
+ */
101
+ _this.highlightedFeatures = [];
102
+ /**
103
+ * Array of selected features.
104
+ * @type {Array<ol/Feature~Feature>}
105
+ * @private
106
+ */
107
+ _this.selectedFeatures = [];
108
+ /**
109
+ * Array of mapbox style layers to add.
110
+ * @type {Array<mapboxgl.styleLayer>}
111
+ * @private
112
+ */
113
+ _this.styleLayers =
114
+ (options.styleLayer ? [options.styleLayer] : options.styleLayers) || [];
115
+ /**
116
+ * @private
117
+ */
118
+ _this.addStyleLayers = _this.addStyleLayers.bind(_this);
119
+ /**
120
+ * @private
121
+ */
122
+ _this.onLoad = _this.onLoad.bind(_this);
123
+ if (options.filters) {
124
+ /** @private */
125
+ _this.addDynamicFilters = function () {
126
+ _this.setFilter(typeof options.filters === 'function'
127
+ ? options.filters(_this)
128
+ : options.filters);
129
+ };
130
+ }
131
+ if (!_this.styleLayersFilter && _this.styleLayers) {
132
+ var ids_1 = _this.styleLayers.map(function (s) { return s.id; });
133
+ _this.styleLayersFilter = function (styleLayer) { return ids_1.includes(styleLayer.id); };
134
+ }
135
+ return _this;
136
+ }
137
+ /**
138
+ * Initialize the layer.
139
+ * @param {mapboxgl.Map} map the mapbox map.
140
+ * @override
141
+ */
142
+ MapboxStyleLayer.prototype.attachToMap = function (map) {
143
+ var _this = this;
144
+ if (!this.mapboxLayer.map) {
145
+ this.mapboxLayer.attachToMap(map);
146
+ }
147
+ _super.prototype.attachToMap.call(this, map);
148
+ if (!this.map) {
149
+ return;
150
+ }
151
+ // Apply the initial visibiltity.
152
+ var mbMap = this.mapboxLayer.mbMap;
153
+ if (!mbMap) {
154
+ // If the mbMap is not yet created because the map has no target yet, we
155
+ // relaunch the initialisation when it's the case.
156
+ this.olListenersKeys.push(this.map.on('change:target', function () {
157
+ _this.attachToMap(map);
158
+ }));
159
+ return;
160
+ }
161
+ // mbMap.loaded() and mbMap.isStyleLoaded() are reliable only on the first call of init.
162
+ // On the next call (when a topic change for example), these functions returns false because
163
+ // the style is being modified.
164
+ // That's why we rely on a property instead for the next calls.
165
+ if (this.mapboxLayer.loaded || mbMap.isStyleLoaded() || mbMap.loaded()) {
166
+ this.onLoad();
167
+ }
168
+ else {
169
+ mbMap.once('load', this.onLoad);
170
+ }
171
+ // Apply the visibiltity when layer's visibility change.
172
+ this.olListenersKeys.push(this.on('change:visible', function (evt) {
173
+ // Once the map is loaded we can apply vsiiblity without waiting
174
+ // the style. Mapbox take care of the application of style changes.
175
+ _this.applyLayoutVisibility(evt);
176
+ }));
177
+ this.olListenersKeys.push(this.mapboxLayer.on('load', function () {
178
+ _this.onLoad();
179
+ }));
180
+ };
181
+ /**
182
+ * Terminate the layer.
183
+ * @param {mapboxgl.Map} map the mapbox map.
184
+ * @override
185
+ */
186
+ MapboxStyleLayer.prototype.detachFromMap = function (map) {
187
+ var mbMap = this.mapboxLayer.mbMap;
188
+ if (mbMap) {
189
+ mbMap.off('load', this.onLoad);
190
+ this.removeStyleLayers();
191
+ }
192
+ _super.prototype.detachFromMap.call(this, map);
193
+ };
194
+ /** @ignore */
195
+ MapboxStyleLayer.prototype.addStyleLayers = function () {
196
+ var _this = this;
197
+ var mbMap = this.mapboxLayer.mbMap;
198
+ if (!mbMap) {
199
+ return;
200
+ }
201
+ this.styleLayers.forEach(function (styleLayer) {
202
+ var id = styleLayer.id, source = styleLayer.source;
203
+ if (mbMap.getSource(source) && !mbMap.getLayer(id)) {
204
+ mbMap.addLayer(styleLayer, _this.beforeId);
205
+ }
206
+ });
207
+ this.applyLayoutVisibility();
208
+ };
209
+ /** @ignore */
210
+ MapboxStyleLayer.prototype.removeStyleLayers = function () {
211
+ var mbMap = this.mapboxLayer.mbMap;
212
+ if (!mbMap) {
213
+ return;
214
+ }
215
+ this.styleLayers.forEach(function (styleLayer) {
216
+ if (mbMap.getLayer(styleLayer.id)) {
217
+ mbMap.removeLayer(styleLayer.id);
218
+ }
219
+ });
220
+ };
221
+ /**
222
+ * On Mapbox map load callback function. Add style layers and dynaimc filters.
223
+ * @ignore
224
+ */
225
+ MapboxStyleLayer.prototype.onLoad = function () {
226
+ this.addStyleLayers();
227
+ if (this.addDynamicFilters) {
228
+ this.addDynamicFilters();
229
+ }
230
+ var mbMap = this.mapboxLayer.mbMap;
231
+ var style = mbMap.getStyle();
232
+ if (style && this.styleLayersFilter) {
233
+ var styles = style.layers.filter(this.styleLayersFilter);
234
+ this.disabled = !styles.length;
235
+ }
236
+ };
237
+ /**
238
+ * Request feature information for a given coordinate.
239
+ * @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
240
+ * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate.
241
+ */
242
+ MapboxStyleLayer.prototype.getFeatureInfoAtCoordinate = function (coordinate) {
243
+ var _this = this;
244
+ var mbMap = this.mapboxLayer.mbMap;
245
+ // Ignore the getFeatureInfo until the mapbox map is loaded
246
+ if (!mbMap || !mbMap.isStyleLoaded()) {
247
+ return Promise.resolve({ coordinate: coordinate, features: [], layer: this });
248
+ }
249
+ // We query features only on style layers used by this layer.
250
+ var layers = this.styleLayers || [];
251
+ if (this.styleLayersFilter) {
252
+ layers = mbMap.getStyle().layers.filter(this.styleLayersFilter);
253
+ }
254
+ if (this.queryRenderedLayersFilter) {
255
+ layers = mbMap.getStyle().layers.filter(this.queryRenderedLayersFilter);
256
+ }
257
+ return this.mapboxLayer
258
+ .getFeatureInfoAtCoordinate(coordinate, {
259
+ layers: layers.map(function (layer) { return layer && layer.id; }),
260
+ validate: false,
261
+ })
262
+ .then(function (featureInfo) {
263
+ var features = featureInfo.features.filter(function (feature) {
264
+ return _this.featureInfoFilter(feature, _this.map.getView().getResolution());
265
+ });
266
+ _this.highlight(features);
267
+ return __assign(__assign({}, featureInfo), { features: features, layer: _this });
268
+ });
269
+ };
270
+ /**
271
+ * Set filter that determines which features should be rendered in a style layer.
272
+ * @param {mapboxgl.filter} filter Determines which features should be rendered in a style layer.
273
+ */
274
+ MapboxStyleLayer.prototype.setFilter = function (filter) {
275
+ var mbMap = this.mapboxLayer.mbMap;
276
+ if (!mbMap) {
277
+ return;
278
+ }
279
+ this.styleLayers.forEach(function (_a) {
280
+ var id = _a.id;
281
+ if (mbMap.getLayer(id)) {
282
+ mbMap.setFilter(id, filter);
283
+ }
284
+ });
285
+ };
286
+ /**
287
+ * Set if features are hovered or not.
288
+ * @param {Array<ol/Feature~Feature>} features
289
+ * @param {boolean} state Is the feature hovered
290
+ * @private
291
+ */
292
+ MapboxStyleLayer.prototype.setHoverState = function (features, state) {
293
+ var mbMap = this.mapboxLayer.mbMap;
294
+ if (!features || !mbMap) {
295
+ return;
296
+ }
297
+ features.forEach(function (feature) {
298
+ var _a = feature.get('mapboxFeature') || {}, source = _a.source, sourceLayer = _a.sourceLayer;
299
+ if ((!source && !sourceLayer) || !feature.getId()) {
300
+ if (!feature.getId()) {
301
+ // eslint-disable-next-line no-console
302
+ console.warn("No feature's id found. To use the feature state functionnality, tiles must be generated with --generate-ids. See https://github.com/mapbox/tippecanoe#adding-calculated-attributes.", feature.getId(), feature.getProperties());
303
+ }
304
+ return;
305
+ }
306
+ mbMap.setFeatureState({
307
+ id: feature.getId(),
308
+ source: source,
309
+ sourceLayer: sourceLayer,
310
+ }, { hover: state });
311
+ });
312
+ };
313
+ /**
314
+ * Select a list of features.
315
+ * @param {Array<ol/Feature~Feature>} [features=[]] Features to select.
316
+ * @private
317
+ */
318
+ MapboxStyleLayer.prototype.select = function (features) {
319
+ if (features === void 0) { features = []; }
320
+ this.setHoverState(this.selectedFeatures, false);
321
+ this.selectedFeatures = features;
322
+ this.setHoverState(this.selectedFeatures, true);
323
+ };
324
+ /**
325
+ * Highlight a list of features.
326
+ * @param {Array<ol/Feature~Feature>} [features=[]] Features to highlight.
327
+ * @private
328
+ */
329
+ MapboxStyleLayer.prototype.highlight = function (features) {
330
+ var _this = this;
331
+ if (features === void 0) { features = []; }
332
+ // Filter out selected features
333
+ var filtered = this.highlightedFeatures.filter(function (feature) {
334
+ return !_this.selectedFeatures
335
+ .map(function (feat) { return feat.getId(); })
336
+ .includes(feature.getId());
337
+ });
338
+ // Remove previous highlight
339
+ this.setHoverState(filtered, false);
340
+ this.highlightedFeatures = features;
341
+ // Add highlight
342
+ this.setHoverState(this.highlightedFeatures, true);
343
+ };
344
+ /**
345
+ * Apply visibility to style layers that fits the styleLayersFilter function.
346
+ * @param {Event} evt Layer's event that has called the function.
347
+ * @private
348
+ */
349
+ // eslint-disable-next-line no-unused-vars
350
+ MapboxStyleLayer.prototype.applyLayoutVisibility = function (evt) {
351
+ var visible = this.visible;
352
+ var mbMap = this.mapboxLayer.mbMap;
353
+ var filterFunc = this.styleLayersFilter;
354
+ if (!mbMap) {
355
+ return;
356
+ }
357
+ var style = mbMap.getStyle();
358
+ if (!style) {
359
+ return;
360
+ }
361
+ if (filterFunc) {
362
+ var visibilityValue = visible ? 'visible' : 'none';
363
+ for (var i = 0; i < style.layers.length; i += 1) {
364
+ var styleLayer = style.layers[i];
365
+ if (filterFunc(styleLayer)) {
366
+ if (mbMap.getLayer(styleLayer.id)) {
367
+ mbMap.setLayoutProperty(styleLayer.id, 'visibility', visibilityValue);
368
+ }
369
+ }
370
+ }
371
+ }
372
+ };
373
+ /**
374
+ * Create a copy of the MapboxStyleLayer.
375
+ * @param {Object} newOptions Options to override.
376
+ * @return {MapboxStyleLayer} A MapboxStyleLayer.
377
+ */
378
+ MapboxStyleLayer.prototype.clone = function (newOptions) {
379
+ return new MapboxStyleLayer(__assign(__assign({}, this.options), newOptions));
380
+ };
381
+ return MapboxStyleLayer;
382
+ }(Layer_1.default));
383
+ exports.default = MapboxStyleLayer;
@@ -0,0 +1,69 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ var maplibre_gl_1 = require("maplibre-gl");
30
+ var Layer_1 = require("./Layer");
31
+ var MapboxLayerMixin_1 = require("../../common/mixins/MapboxLayerMixin");
32
+ var utils_1 = require("../../common/utils");
33
+ /**
34
+ * A class representing MaplibreLayer to display on BasicMap
35
+ *
36
+ * @example
37
+ * import { MaplibreLayer } from 'mobility-toolbox-js/ol';
38
+ *
39
+ * const layer = new MaplibreLayer({
40
+ * url: 'https://maps.geops.io/styles/travic_v2/style.json',
41
+ * apikey: 'yourApiKey',
42
+ * });
43
+ *
44
+ * @classproperty {ol/Map~Map} map - The map where the layer is displayed.
45
+ * @extends {Layer}
46
+ */
47
+ var MaplibreLayer = /** @class */ (function (_super) {
48
+ __extends(MaplibreLayer, _super);
49
+ function MaplibreLayer() {
50
+ return _super !== null && _super.apply(this, arguments) || this;
51
+ }
52
+ MaplibreLayer.prototype.getOlLayerRender = function () {
53
+ return (0, utils_1.getMaplibreRender)(this);
54
+ };
55
+ // eslint-disable-next-line class-methods-use-this
56
+ MaplibreLayer.prototype.getMapboxMapClass = function () {
57
+ return maplibre_gl_1.Map;
58
+ };
59
+ /**
60
+ * Create a copy of the MapboxLayer.
61
+ * @param {Object} newOptions Options to override
62
+ * @return {MapboxLayer} A MapboxLayer
63
+ */
64
+ MaplibreLayer.prototype.clone = function (newOptions) {
65
+ return new MaplibreLayer(__assign(__assign({}, this.options), newOptions));
66
+ };
67
+ return MaplibreLayer;
68
+ }((0, MapboxLayerMixin_1.default)(Layer_1.default)));
69
+ exports.default = MaplibreLayer;