mobility-toolbox-js 3.0.0-beta.15 → 3.0.0-beta.17
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.
- package/mbt.js +78 -65
- package/mbt.js.map +2 -2
- package/mbt.min.js +7 -7
- package/mbt.min.js.map +3 -3
- package/ol/layers/MaplibreLayer.d.ts +12 -1
- package/ol/layers/MaplibreLayer.js +14 -14
- package/ol/layers/MaplibreStyleLayer.js +52 -51
- package/ol/mixins/PropertiesLayerMixin.js +2 -2
- package/package.json +1 -1
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import OlMap from 'ol/Map';
|
|
2
2
|
import { MapLibreLayer } from '@geoblocks/ol-maplibre-layer';
|
|
3
|
-
import type { MapLibreLayerOptions } from '@geoblocks/ol-maplibre-layer/lib/types/MapLibreLayer';
|
|
3
|
+
import type { MapLibreLayerOptions, MapLibreOptions } from '@geoblocks/ol-maplibre-layer/lib/types/MapLibreLayer';
|
|
4
4
|
import { MobilityLayerOptions } from '../mixins/MobilityLayerMixin';
|
|
5
5
|
export type MaplibreLayerOptions = MobilityLayerOptions & MapLibreLayerOptions & {
|
|
6
6
|
apiKey?: string;
|
|
7
7
|
apiKeyName?: string;
|
|
8
8
|
style?: string | maplibregl.StyleSpecification;
|
|
9
9
|
url?: string;
|
|
10
|
+
mapLibreOptions?: MapLibreOptions;
|
|
10
11
|
};
|
|
11
12
|
declare const MaplibreLayer_base: {
|
|
12
13
|
new (...args: any[]): {
|
|
@@ -17,6 +18,16 @@ declare const MaplibreLayer_base: {
|
|
|
17
18
|
set copyrights(newCopyrights: string | string[]);
|
|
18
19
|
disabled: boolean;
|
|
19
20
|
readonly group: string;
|
|
21
|
+
/**
|
|
22
|
+
* Constructor.
|
|
23
|
+
*
|
|
24
|
+
* @param {MaplibreLayerOptions} options
|
|
25
|
+
* @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
|
|
26
|
+
* @param {string} [options.apiKeyName="key"] The geOps Maps API key name.
|
|
27
|
+
* @param {maplibregl.MapOptions} [options.mapLibreOptions={ interactive: false, trackResize: false, attributionControl: false }] Maplibre map options.
|
|
28
|
+
* @param {string} [options.style="travic_v2"] The geOps Maps API style.
|
|
29
|
+
* @param {string} [options.url="https://maps.geops.io"] The geOps Maps API url.
|
|
30
|
+
*/
|
|
20
31
|
readonly hitTolerance: number;
|
|
21
32
|
readonly key: string;
|
|
22
33
|
readonly map: OlMap;
|
|
@@ -2,6 +2,11 @@ import debounce from 'lodash.debounce';
|
|
|
2
2
|
import { MapLibreLayer } from '@geoblocks/ol-maplibre-layer';
|
|
3
3
|
import { getUrlWithParams } from '../../common/utils';
|
|
4
4
|
import MobilityLayerMixin from '../mixins/MobilityLayerMixin';
|
|
5
|
+
const buildStyleUrl = (url, style, apiKey, apiKeyName) => {
|
|
6
|
+
return getUrlWithParams(`${url}/styles/${style}/style.json`, {
|
|
7
|
+
[apiKeyName]: apiKey,
|
|
8
|
+
}).toString();
|
|
9
|
+
};
|
|
5
10
|
/**
|
|
6
11
|
* An OpenLayers layer able to display data from the [geOps Maps API](https://developer.geops.io/apis/maps).
|
|
7
12
|
*
|
|
@@ -31,12 +36,12 @@ import MobilityLayerMixin from '../mixins/MobilityLayerMixin';
|
|
|
31
36
|
class MaplibreLayer extends MobilityLayerMixin(MapLibreLayer) {
|
|
32
37
|
get mbMap() {
|
|
33
38
|
// eslint-disable-next-line no-console
|
|
34
|
-
console.warn('
|
|
39
|
+
console.warn('MaplibreLayer.mbMap is deprecated. Use layer.maplibreMap.');
|
|
35
40
|
return this.maplibreMap;
|
|
36
41
|
}
|
|
37
42
|
get maplibreMap() {
|
|
38
43
|
// eslint-disable-next-line no-console
|
|
39
|
-
console.warn('
|
|
44
|
+
console.warn('MaplibreLayer.maplibreMap is deprecated. Use layer.mapLibreMap.');
|
|
40
45
|
return this.mapLibreMap;
|
|
41
46
|
}
|
|
42
47
|
// get queryRenderedFeaturesOptions(): maplibregl.QueryRenderedFeaturesOptions {
|
|
@@ -70,7 +75,11 @@ class MaplibreLayer extends MobilityLayerMixin(MapLibreLayer) {
|
|
|
70
75
|
* @param {string} [options.url="https://maps.geops.io"] The geOps Maps API url.
|
|
71
76
|
*/
|
|
72
77
|
constructor(options) {
|
|
73
|
-
|
|
78
|
+
const newOptions = Object.assign(Object.assign({ apiKeyName: 'key', style: 'travic_v2', url: 'https://maps.geops.io' }, (options || {})), { mapLibreOptions: Object.assign({}, (options.mapLibreOptions || {})) });
|
|
79
|
+
if (!newOptions.mapLibreOptions.style && newOptions.apiKey) {
|
|
80
|
+
newOptions.mapLibreOptions.style = buildStyleUrl(newOptions.url, newOptions.style, newOptions.apiKey, newOptions.apiKeyName);
|
|
81
|
+
}
|
|
82
|
+
super(newOptions);
|
|
74
83
|
}
|
|
75
84
|
/**
|
|
76
85
|
* Initialize the layer and listen to feature clicks.
|
|
@@ -99,20 +108,11 @@ class MaplibreLayer extends MobilityLayerMixin(MapLibreLayer) {
|
|
|
99
108
|
return this.url;
|
|
100
109
|
}
|
|
101
110
|
/// Otherwise build the complete style url.
|
|
102
|
-
return
|
|
103
|
-
[this.get('apiKeyName')]: this.get('apiKey'),
|
|
104
|
-
}).toString();
|
|
111
|
+
return buildStyleUrl(this.url, this.style, this.get('apiKey'), this.get('apiKeyName'));
|
|
105
112
|
}
|
|
106
|
-
// eslint-disable-next-line class-methods-use-this
|
|
107
|
-
// createMap(options: MapOptions): Map {
|
|
108
|
-
// return new Map(options);
|
|
109
|
-
// }
|
|
110
|
-
// createRenderer(): MaplibreLayerRenderer {
|
|
111
|
-
// return new MaplibreLayerRenderer(this);
|
|
112
|
-
// }
|
|
113
113
|
updateMaplibreMap() {
|
|
114
114
|
var _a;
|
|
115
|
-
(_a = this.
|
|
115
|
+
(_a = this.mapLibreMap) === null || _a === void 0 ? void 0 : _a.setStyle(this.getStyle(), { diff: false });
|
|
116
116
|
}
|
|
117
117
|
/**
|
|
118
118
|
* Create a copy of the MaplibreLayer.
|
|
@@ -147,7 +147,7 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
147
147
|
return;
|
|
148
148
|
}
|
|
149
149
|
if (!this.map.getTargetElement()) {
|
|
150
|
-
// If ther e is no target element the
|
|
150
|
+
// If ther e is no target element the mapLibreMap is not yet created, we
|
|
151
151
|
// relaunch the initialisation when it's the case.
|
|
152
152
|
this.olEventsKeys.push(this.map.on('change:target', () => {
|
|
153
153
|
this.attachToMap(map);
|
|
@@ -155,17 +155,17 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
155
155
|
return;
|
|
156
156
|
}
|
|
157
157
|
// Apply the initial visibility if possible otherwise we wait for the load event of the layer
|
|
158
|
-
const {
|
|
159
|
-
if (
|
|
160
|
-
//
|
|
158
|
+
const { mapLibreMap } = this.maplibreLayer;
|
|
159
|
+
if (mapLibreMap) {
|
|
160
|
+
// mapLibreMap.loaded() and mapLibreMap.isStyleLoaded() are reliable only on the first call of init.
|
|
161
161
|
// On the next call (when a topic change for example), these functions returns false because
|
|
162
162
|
// the style is being modified.
|
|
163
163
|
// That's why we rely on a property instead for the next calls.
|
|
164
|
-
if (
|
|
164
|
+
if (mapLibreMap.loaded()) {
|
|
165
165
|
this.onLoad();
|
|
166
166
|
}
|
|
167
167
|
else {
|
|
168
|
-
|
|
168
|
+
mapLibreMap.once('load', this.onLoad);
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
// Apply the visibiltity when layer's visibility change.
|
|
@@ -188,8 +188,8 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
188
188
|
*/
|
|
189
189
|
detachFromMap() {
|
|
190
190
|
var _a;
|
|
191
|
-
if ((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.
|
|
192
|
-
this.maplibreLayer.
|
|
191
|
+
if ((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) {
|
|
192
|
+
this.maplibreLayer.mapLibreMap.off('load', this.onLoad);
|
|
193
193
|
this.removeLayers();
|
|
194
194
|
this.removeSources();
|
|
195
195
|
}
|
|
@@ -198,14 +198,14 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
198
198
|
/** @private */
|
|
199
199
|
addSources() {
|
|
200
200
|
var _a;
|
|
201
|
-
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.
|
|
201
|
+
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !this.sources) {
|
|
202
202
|
return;
|
|
203
203
|
}
|
|
204
|
-
const {
|
|
205
|
-
if (
|
|
204
|
+
const { mapLibreMap } = this.maplibreLayer;
|
|
205
|
+
if (mapLibreMap) {
|
|
206
206
|
Object.entries(this.sources).forEach(([id, source]) => {
|
|
207
|
-
if (!
|
|
208
|
-
|
|
207
|
+
if (!mapLibreMap.getSource(id)) {
|
|
208
|
+
mapLibreMap.addSource(id, source);
|
|
209
209
|
}
|
|
210
210
|
});
|
|
211
211
|
}
|
|
@@ -213,14 +213,14 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
213
213
|
/** @private */
|
|
214
214
|
removeSources() {
|
|
215
215
|
var _a;
|
|
216
|
-
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.
|
|
216
|
+
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !this.sources) {
|
|
217
217
|
return;
|
|
218
218
|
}
|
|
219
|
-
const {
|
|
220
|
-
if (
|
|
219
|
+
const { mapLibreMap } = this.maplibreLayer;
|
|
220
|
+
if (mapLibreMap) {
|
|
221
221
|
Object.keys(this.sources).forEach((id) => {
|
|
222
|
-
if (
|
|
223
|
-
|
|
222
|
+
if (mapLibreMap.getSource(id)) {
|
|
223
|
+
mapLibreMap.removeSource(id);
|
|
224
224
|
}
|
|
225
225
|
});
|
|
226
226
|
}
|
|
@@ -228,18 +228,18 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
228
228
|
/** @private */
|
|
229
229
|
addLayers() {
|
|
230
230
|
var _a;
|
|
231
|
-
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.
|
|
231
|
+
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !Array.isArray(this.layers)) {
|
|
232
232
|
return;
|
|
233
233
|
}
|
|
234
|
-
const {
|
|
235
|
-
if (
|
|
234
|
+
const { mapLibreMap } = this.maplibreLayer;
|
|
235
|
+
if (mapLibreMap) {
|
|
236
236
|
this.layers.forEach((layer) => {
|
|
237
237
|
// @ts-expect-error source is optional but exists in TS definition
|
|
238
238
|
const { id, source } = layer;
|
|
239
|
-
if ((!source || (source &&
|
|
239
|
+
if ((!source || (source && mapLibreMap.getSource(source))) &&
|
|
240
240
|
id &&
|
|
241
|
-
!
|
|
242
|
-
|
|
241
|
+
!mapLibreMap.getLayer(id)) {
|
|
242
|
+
mapLibreMap.addLayer(layer, this.beforeId);
|
|
243
243
|
}
|
|
244
244
|
});
|
|
245
245
|
this.applyLayoutVisibility();
|
|
@@ -248,15 +248,15 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
248
248
|
/** @private */
|
|
249
249
|
removeLayers() {
|
|
250
250
|
var _a;
|
|
251
|
-
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.
|
|
251
|
+
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !Array.isArray(this.layers)) {
|
|
252
252
|
return;
|
|
253
253
|
}
|
|
254
|
-
const {
|
|
255
|
-
if (
|
|
254
|
+
const { mapLibreMap } = this.maplibreLayer;
|
|
255
|
+
if (mapLibreMap) {
|
|
256
256
|
this.layers.forEach((styleLayer) => {
|
|
257
257
|
const { id } = styleLayer;
|
|
258
|
-
if (id &&
|
|
259
|
-
|
|
258
|
+
if (id && mapLibreMap.getLayer(id)) {
|
|
259
|
+
mapLibreMap.removeLayer(id);
|
|
260
260
|
}
|
|
261
261
|
});
|
|
262
262
|
}
|
|
@@ -267,17 +267,18 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
267
267
|
*/
|
|
268
268
|
onLoad() {
|
|
269
269
|
var _a;
|
|
270
|
-
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.
|
|
270
|
+
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap)) {
|
|
271
271
|
return;
|
|
272
272
|
}
|
|
273
273
|
this.addSources();
|
|
274
274
|
this.addLayers();
|
|
275
|
-
const {
|
|
276
|
-
const style =
|
|
275
|
+
const { mapLibreMap } = this.maplibreLayer;
|
|
276
|
+
const style = mapLibreMap.getStyle();
|
|
277
277
|
if ((style === null || style === void 0 ? void 0 : style.layers) && this.layersFilter) {
|
|
278
278
|
const styles = style.layers.filter(this.layersFilter);
|
|
279
|
-
this.disabled
|
|
279
|
+
this.set('disabled', !styles.length);
|
|
280
280
|
}
|
|
281
|
+
this.applyLayoutVisibility();
|
|
281
282
|
}
|
|
282
283
|
/**
|
|
283
284
|
* Set the feature state of the features.
|
|
@@ -287,10 +288,10 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
287
288
|
*/
|
|
288
289
|
setFeatureState(features, state) {
|
|
289
290
|
var _a;
|
|
290
|
-
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.
|
|
291
|
+
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) || !features.length) {
|
|
291
292
|
return;
|
|
292
293
|
}
|
|
293
|
-
const {
|
|
294
|
+
const { mapLibreMap } = this.maplibreLayer;
|
|
294
295
|
features.forEach((feature) => {
|
|
295
296
|
const { source, sourceLayer } = feature.get(VECTOR_TILE_FEATURE_PROPERTY) || {};
|
|
296
297
|
if ((!source && !sourceLayer) || !feature.getId()) {
|
|
@@ -300,7 +301,7 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
300
301
|
}
|
|
301
302
|
return;
|
|
302
303
|
}
|
|
303
|
-
|
|
304
|
+
mapLibreMap.setFeatureState({
|
|
304
305
|
id: feature.getId(),
|
|
305
306
|
source,
|
|
306
307
|
sourceLayer,
|
|
@@ -316,23 +317,23 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
316
317
|
var _a;
|
|
317
318
|
// eslint-disable-next-line no-console
|
|
318
319
|
console.warn(`Deprecated. getFeatureInfoAtCoordinate([layer], coordinate) from ol package instead.`);
|
|
319
|
-
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.
|
|
320
|
+
if (!((_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap)) {
|
|
320
321
|
return Promise.resolve({ coordinate, features: [], layer: this });
|
|
321
322
|
}
|
|
322
|
-
const {
|
|
323
|
+
const { mapLibreMap } = this.maplibreLayer;
|
|
323
324
|
// Ignore the getFeatureInfo until the Maplibre map is loaded
|
|
324
|
-
if (!
|
|
325
|
+
if (!mapLibreMap.isStyleLoaded()) {
|
|
325
326
|
return Promise.resolve({ coordinate, features: [], layer: this });
|
|
326
327
|
}
|
|
327
328
|
// We query features only on style layers used by this layer.
|
|
328
329
|
let layers = this.layers || [];
|
|
329
330
|
if (this.layersFilter) {
|
|
330
|
-
layers =
|
|
331
|
+
layers = mapLibreMap.getStyle().layers.filter(this.layersFilter);
|
|
331
332
|
}
|
|
332
333
|
if (this.queryRenderedLayersFilter) {
|
|
333
334
|
// @ts-ignore
|
|
334
335
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
335
|
-
layers =
|
|
336
|
+
layers = mapLibreMap
|
|
336
337
|
.getStyle()
|
|
337
338
|
.layers.filter(this.queryRenderedLayersFilter);
|
|
338
339
|
}
|
|
@@ -365,14 +366,14 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
365
366
|
// * @param {maplibregl.filter} filter Determines which features should be rendered in a style layer.
|
|
366
367
|
// */
|
|
367
368
|
// setFilter(filter: { [key: string]: any }) {
|
|
368
|
-
// if (!this.maplibreLayer?.
|
|
369
|
+
// if (!this.maplibreLayer?.mapLibreMap) {
|
|
369
370
|
// return;
|
|
370
371
|
// }
|
|
371
|
-
// const {
|
|
372
|
+
// const { mapLibreMap } = this.maplibreLayer;
|
|
372
373
|
// this.styleLayers.forEach(({ id }) => {
|
|
373
|
-
// if (id && filter &&
|
|
374
|
+
// if (id && filter && mapLibreMap.getLayer(id)) {
|
|
374
375
|
// // @ts-ignore
|
|
375
|
-
//
|
|
376
|
+
// mapLibreMap.setFilter(id, filter);
|
|
376
377
|
// }
|
|
377
378
|
// });
|
|
378
379
|
// }
|
|
@@ -424,21 +425,21 @@ class MaplibreStyleLayer extends MobilityLayerMixin(Layer) {
|
|
|
424
425
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
425
426
|
applyLayoutVisibility(evt) {
|
|
426
427
|
var _a, _b;
|
|
427
|
-
if (!((_b = (_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.
|
|
428
|
+
if (!((_b = (_a = this.maplibreLayer) === null || _a === void 0 ? void 0 : _a.mapLibreMap) === null || _b === void 0 ? void 0 : _b.getStyle()) || !this.layersFilter) {
|
|
428
429
|
return;
|
|
429
430
|
}
|
|
430
|
-
const {
|
|
431
|
-
const style =
|
|
431
|
+
const { mapLibreMap } = this.maplibreLayer;
|
|
432
|
+
const style = mapLibreMap.getStyle();
|
|
432
433
|
const visibilityValue = this.getVisible() ? 'visible' : 'none';
|
|
433
434
|
const layers = style.layers || [];
|
|
434
435
|
for (let i = 0; i < layers.length; i += 1) {
|
|
435
436
|
const layer = layers[i];
|
|
436
437
|
if (this.layersFilter(layer)) {
|
|
437
438
|
const { id } = layer;
|
|
438
|
-
if (
|
|
439
|
-
|
|
439
|
+
if (mapLibreMap.getLayer(id)) {
|
|
440
|
+
mapLibreMap.setLayoutProperty(id, 'visibility', visibilityValue);
|
|
440
441
|
if (this.getMinZoom() || this.getMaxZoom()) {
|
|
441
|
-
|
|
442
|
+
mapLibreMap.setLayerZoomRange(id, this.getMinZoom() ? this.getMinZoom() - 1 : 0, // Maplibre zoom = ol zoom - 1
|
|
442
443
|
this.getMaxZoom() ? this.getMaxZoom() - 1 : 24);
|
|
443
444
|
}
|
|
444
445
|
}
|
|
@@ -112,7 +112,7 @@ function PropertiesLayerMixin(Base) {
|
|
|
112
112
|
}
|
|
113
113
|
}));
|
|
114
114
|
this.options = options;
|
|
115
|
-
this.children
|
|
115
|
+
this.set('children', options.children || []); // Trigger the on children change event
|
|
116
116
|
}
|
|
117
117
|
// @ts-expect-error - this is a mixin
|
|
118
118
|
setMapInternal(map) {
|
|
@@ -130,7 +130,7 @@ function PropertiesLayerMixin(Base) {
|
|
|
130
130
|
(oldValue || []).forEach((child) => {
|
|
131
131
|
child.set('parent', undefined);
|
|
132
132
|
});
|
|
133
|
-
(this.children || []).forEach((child) => {
|
|
133
|
+
(this.get('children') || []).forEach((child) => {
|
|
134
134
|
child.set('parent', this);
|
|
135
135
|
});
|
|
136
136
|
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "mobility-toolbox-js",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"description": "Toolbox for JavaScript applications in the domains of mobility and logistics.",
|
|
5
|
-
"version": "3.0.0-beta.
|
|
5
|
+
"version": "3.0.0-beta.17",
|
|
6
6
|
"homepage": "https://mobility-toolbox-js.geops.io/",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./index.js",
|