mobility-toolbox-js 2.0.1-beta.13 → 2.1.0
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/api/RealtimeAPI.d.ts +279 -0
- package/api/RealtimeAPI.d.ts.map +1 -0
- package/api/RealtimeAPI.js +466 -0
- package/api/RoutingAPI.d.ts +37 -0
- package/api/RoutingAPI.d.ts.map +1 -0
- package/api/RoutingAPI.js +32 -12
- package/api/StopsAPI.d.ts +38 -0
- package/api/StopsAPI.d.ts.map +1 -0
- package/api/StopsAPI.js +33 -9
- package/api/index.d.ts +4 -0
- package/api/index.d.ts.map +1 -0
- package/api/index.js +3 -3
- package/api/typedefs.d.ts +179 -0
- package/api/typedefs.d.ts.map +1 -0
- package/api/typedefs.js +75 -0
- package/common/api/HttpAPI.d.ts +31 -0
- package/common/api/HttpAPI.d.ts.map +1 -0
- package/common/api/HttpAPI.js +54 -27
- package/common/api/WebSocketAPI.d.ts +153 -0
- package/common/api/WebSocketAPI.d.ts.map +1 -0
- package/common/api/WebSocketAPI.js +330 -164
- package/common/controls/ControlCommon.d.ts +76 -0
- package/common/controls/ControlCommon.d.ts.map +1 -0
- package/common/controls/ControlCommon.js +150 -0
- package/common/controls/CopyrightControlCommon.d.ts +13 -0
- package/common/controls/CopyrightControlCommon.d.ts.map +1 -0
- package/common/controls/CopyrightControlCommon.js +34 -0
- package/common/controls/StopFinderControlCommon.d.ts +55 -0
- package/common/controls/StopFinderControlCommon.d.ts.map +1 -0
- package/common/controls/StopFinderControlCommon.js +144 -0
- package/common/index.d.ts +3 -0
- package/common/index.d.ts.map +1 -0
- package/common/index.js +2 -4
- package/common/layers/LayerCommon.d.ts +94 -0
- package/common/layers/LayerCommon.d.ts.map +1 -0
- package/common/layers/LayerCommon.js +244 -0
- package/common/mixins/RealtimeLayerMixin.d.ts +286 -0
- package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
- package/common/mixins/RealtimeLayerMixin.js +776 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts +60 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
- package/common/mixins/UserInteractionsLayerMixin.js +241 -0
- package/common/styles/index.d.ts +5 -0
- package/common/styles/index.d.ts.map +1 -0
- package/common/styles/index.js +4 -4
- package/common/styles/realtimeDefaultStyle.d.ts +36 -0
- package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
- package/common/styles/realtimeDefaultStyle.js +275 -0
- package/common/styles/realtimeDelayStyle.d.ts +12 -0
- package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
- package/common/styles/realtimeDelayStyle.js +13 -0
- package/common/styles/realtimeHeadingStyle.d.ts +12 -0
- package/common/styles/realtimeHeadingStyle.d.ts.map +1 -0
- package/common/styles/realtimeHeadingStyle.js +85 -0
- package/common/styles/realtimeSimpleStyle.d.ts +4 -0
- package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
- package/common/styles/realtimeSimpleStyle.js +23 -0
- package/common/typedefs.d.ts +111 -0
- package/common/typedefs.d.ts.map +1 -0
- package/common/typedefs.js +52 -0
- package/common/utils/compareDepartures.d.ts +11 -0
- package/common/utils/compareDepartures.d.ts.map +1 -0
- package/common/utils/compareDepartures.js +35 -0
- package/common/utils/createCanvas.d.ts +10 -0
- package/common/utils/createCanvas.d.ts.map +1 -0
- package/common/utils/createCanvas.js +27 -0
- package/common/utils/createRealtimeFilters.d.ts +13 -0
- package/common/utils/createRealtimeFilters.d.ts.map +1 -0
- package/common/utils/createRealtimeFilters.js +74 -0
- package/common/utils/debounceDeparturesMessages.d.ts +12 -0
- package/common/utils/debounceDeparturesMessages.d.ts.map +1 -0
- package/common/utils/debounceDeparturesMessages.js +24 -0
- package/common/utils/debounceWebsocketMessages.d.ts +11 -0
- package/common/utils/debounceWebsocketMessages.d.ts.map +1 -0
- package/common/utils/debounceWebsocketMessages.js +29 -0
- package/common/utils/getLayersAsFlatArray.d.ts +3 -0
- package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
- package/common/utils/getLayersAsFlatArray.js +15 -0
- package/common/utils/getMapboxMapCopyrights.d.ts +18 -0
- package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
- package/common/utils/getMapboxMapCopyrights.js +26 -15
- package/common/utils/getMapboxRender.d.ts +7 -0
- package/common/utils/getMapboxRender.d.ts.map +1 -0
- package/common/utils/getMapboxRender.js +87 -0
- package/common/utils/getMaplibreRender.d.ts +7 -0
- package/common/utils/getMaplibreRender.d.ts.map +1 -0
- package/common/utils/getMaplibreRender.js +38 -0
- package/common/utils/getRealtimeModeSuffix.d.ts +10 -0
- package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
- package/common/utils/getRealtimeModeSuffix.js +7 -0
- package/common/utils/getUrlWithParams.d.ts +9 -0
- package/common/utils/getUrlWithParams.d.ts.map +1 -0
- package/common/utils/getUrlWithParams.js +18 -0
- package/common/utils/getVehiclePosition.d.ts +16 -0
- package/common/utils/getVehiclePosition.d.ts.map +1 -0
- package/common/utils/getVehiclePosition.js +67 -37
- package/common/utils/index.d.ts +17 -0
- package/common/utils/index.d.ts.map +1 -0
- package/common/utils/index.js +17 -5
- package/common/utils/realtimeConfig.d.ts +49 -0
- package/common/utils/realtimeConfig.d.ts.map +1 -0
- package/common/utils/realtimeConfig.js +173 -0
- package/common/utils/removeDuplicate.d.ts +10 -0
- package/common/utils/removeDuplicate.d.ts.map +1 -0
- package/common/utils/removeDuplicate.js +12 -5
- package/common/utils/renderTrajectories.d.ts +17 -0
- package/common/utils/renderTrajectories.d.ts.map +1 -0
- package/common/utils/renderTrajectories.js +110 -0
- package/common/utils/sortAndFilterDepartures.d.ts +16 -0
- package/common/utils/sortAndFilterDepartures.d.ts.map +1 -0
- package/common/utils/sortAndFilterDepartures.js +58 -0
- package/common/utils/sortByDelay.d.ts +3 -0
- package/common/utils/sortByDelay.d.ts.map +1 -0
- package/common/utils/sortByDelay.js +17 -15
- package/common/utils/timeUtils.d.ts +24 -0
- package/common/utils/timeUtils.d.ts.map +1 -0
- package/common/utils/timeUtils.js +34 -15
- package/iife.d.ts +3 -0
- package/iife.d.ts.map +1 -0
- package/iife.js +5 -0
- package/index.d.ts +10 -0
- package/index.d.ts.map +1 -0
- package/index.js +8 -6
- package/mapbox/controls/CopyrightControl.d.ts +29 -0
- package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
- package/mapbox/controls/CopyrightControl.js +44 -25
- package/mapbox/controls/index.d.ts +2 -0
- package/mapbox/controls/index.d.ts.map +1 -0
- package/mapbox/controls/index.js +2 -1
- package/mapbox/index.d.ts +6 -0
- package/mapbox/index.d.ts.map +1 -0
- package/mapbox/index.js +5 -4
- package/mapbox/layers/Layer.d.ts +59 -0
- package/mapbox/layers/Layer.d.ts.map +1 -0
- package/mapbox/layers/Layer.js +99 -55
- package/mapbox/layers/RealtimeLayer.d.ts +181 -0
- package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
- package/mapbox/layers/RealtimeLayer.js +276 -0
- package/mapbox/layers/index.d.ts +3 -0
- package/mapbox/layers/index.d.ts.map +1 -0
- package/mapbox/layers/index.js +2 -2
- package/mapbox/utils/getMercatorResolution.d.ts +9 -0
- package/mapbox/utils/getMercatorResolution.d.ts.map +1 -0
- package/mapbox/utils/getMercatorResolution.js +18 -0
- package/mapbox/utils/getSourceCoordinates.d.ts +9 -0
- package/mapbox/utils/getSourceCoordinates.d.ts.map +1 -0
- package/mapbox/utils/getSourceCoordinates.js +27 -0
- package/mapbox/utils/index.d.ts +3 -0
- package/mapbox/utils/index.d.ts.map +1 -0
- package/mapbox/utils/index.js +2 -0
- package/mbt.js +26061 -16500
- package/mbt.js.map +4 -4
- package/mbt.min.js +205 -126
- package/mbt.min.js.map +4 -4
- package/ol/controls/CopyrightControl.d.ts +31 -0
- package/ol/controls/CopyrightControl.d.ts.map +1 -0
- package/ol/controls/CopyrightControl.js +62 -36
- package/ol/controls/RoutingControl.d.ts +193 -0
- package/ol/controls/RoutingControl.d.ts.map +1 -0
- package/ol/controls/RoutingControl.js +601 -357
- package/ol/controls/StopFinderControl.d.ts +30 -0
- package/ol/controls/StopFinderControl.d.ts.map +1 -0
- package/ol/controls/StopFinderControl.js +30 -8
- package/ol/controls/index.d.ts +4 -0
- package/ol/controls/index.d.ts.map +1 -0
- package/ol/controls/index.js +3 -3
- package/ol/index.d.ts +6 -0
- package/ol/index.d.ts.map +1 -0
- package/ol/index.js +5 -5
- package/ol/layers/Layer.d.ts +86 -0
- package/ol/layers/Layer.d.ts.map +1 -0
- package/ol/layers/Layer.js +163 -77
- package/ol/layers/MapGlLayer.d.ts +67 -0
- package/ol/layers/MapGlLayer.d.ts.map +1 -0
- package/ol/layers/MapGlLayer.js +218 -0
- package/ol/layers/MapboxLayer.d.ts +50 -0
- package/ol/layers/MapboxLayer.d.ts.map +1 -0
- package/ol/layers/MapboxLayer.js +99 -193
- package/ol/layers/MapboxStyleLayer.d.ts +129 -0
- package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
- package/ol/layers/MapboxStyleLayer.js +362 -171
- package/ol/layers/MaplibreLayer.d.ts +28 -0
- package/ol/layers/MaplibreLayer.d.ts.map +1 -0
- package/ol/layers/MaplibreLayer.js +30 -135
- package/ol/layers/RealtimeLayer.d.ts +202 -0
- package/ol/layers/RealtimeLayer.d.ts.map +1 -0
- package/ol/layers/RealtimeLayer.js +340 -0
- package/ol/layers/RoutingLayer.d.ts +34 -0
- package/ol/layers/RoutingLayer.d.ts.map +1 -0
- package/ol/layers/RoutingLayer.js +72 -48
- package/ol/layers/VectorLayer.d.ts +25 -0
- package/ol/layers/VectorLayer.d.ts.map +1 -0
- package/ol/layers/VectorLayer.js +34 -18
- package/ol/layers/WMSLayer.d.ts +42 -0
- package/ol/layers/WMSLayer.d.ts.map +1 -0
- package/ol/layers/WMSLayer.js +84 -34
- package/ol/layers/index.d.ts +9 -0
- package/ol/layers/index.d.ts.map +1 -0
- package/ol/layers/index.js +8 -8
- package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryDelayStyle.js +26 -24
- package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
- package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryStyle.js +40 -39
- package/ol/styles/index.d.ts +3 -0
- package/ol/styles/index.d.ts.map +1 -0
- package/ol/styles/index.js +2 -2
- package/package.json +81 -133
- package/setupTests.d.ts +2 -0
- package/setupTests.d.ts.map +1 -0
- package/setupTests.js +26 -0
- package/types/common.d.ts +122 -0
- package/types/index.d.ts +11 -0
- package/types/realtime.d.ts +320 -0
- package/types/routing.d.ts +206 -0
- package/types/stops.d.ts +143 -0
- package/README.md +0 -23
- package/api/RoutingAPI.test.js +0 -25
- package/api/StopsAPI.test.js +0 -22
- package/api/TralisAPI.js +0 -359
- package/api/TralisAPI.test.js +0 -67
- package/api/TralisAPIUtils.js +0 -43
- package/common/Tracker.js +0 -93
- package/common/api/HttpAPI.test.js +0 -50
- package/common/api/WebSocketAPI.test.js +0 -311
- package/common/controls/Control.js +0 -81
- package/common/controls/Control.test.js +0 -87
- package/common/layers/Layer.js +0 -213
- package/common/layers/Layer.test.js +0 -526
- package/common/mixins/CopyrightMixin.js +0 -24
- package/common/mixins/SearchMixin.js +0 -110
- package/common/mixins/TralisLayerMixin.js +0 -479
- package/common/styles/trackerDefaultStyle.js +0 -197
- package/common/styles/trackerDelayStyle.js +0 -8
- package/common/styles/trackerSimpleStyle.js +0 -18
- package/common/trackerConfig.js +0 -152
- package/common/trackerConfig.test.js +0 -23
- package/common/utils/createTrackerFilters.js +0 -56
- package/common/utils/createTrackerFilters.test.js +0 -79
- package/common/utils/getMapboxMapCopyrights.test.js +0 -40
- package/common/utils/getMapboxStyleUrl.js +0 -22
- package/common/utils/removeDuplicate.test.js +0 -19
- package/common/utils/timeUtils.test.js +0 -10
- package/mapbox/layers/Layer.test.js +0 -182
- package/mapbox/layers/TralisLayer.js +0 -182
- package/mapbox/layers/TralisLayer.test.js +0 -38
- package/mapbox/utils.js +0 -32
- package/ol/controls/CopyrightControl.test.js +0 -165
- package/ol/controls/RoutingControl.test.js +0 -151
- package/ol/controls/StopFinderControl.test.js +0 -48
- package/ol/layers/Layer.test.js +0 -174
- package/ol/layers/MapboxLayer.test.js +0 -160
- package/ol/layers/MapboxStyleLayer.test.js +0 -231
- package/ol/layers/RoutingLayer.test.js +0 -40
- package/ol/layers/TralisLayer.js +0 -185
- package/ol/layers/TralisLayer.test.js +0 -79
- package/ol/layers/VectorLayer.test.js +0 -87
- package/ol/layers/WMSLayer.test.js +0 -76
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/* eslint-disable max-classes-per-file */
|
|
2
|
+
import { toLonLat } from 'ol/proj';
|
|
3
|
+
import OlLayer from 'ol/layer/Layer';
|
|
4
|
+
import Source from 'ol/source/Source';
|
|
5
|
+
import GeoJSON from 'ol/format/GeoJSON';
|
|
6
|
+
import BaseEvent from 'ol/events/Event';
|
|
7
|
+
import { getUrlWithParams, getMapboxMapCopyrights } from '../../common/utils';
|
|
8
|
+
import Layer from './Layer';
|
|
9
|
+
/**
|
|
10
|
+
* Common class for Mapbox and Maplibre and potential other fork from Mapbox.
|
|
11
|
+
* It's used to share code between Mapbox and Maplibre layers without importing both libs.
|
|
12
|
+
*/
|
|
13
|
+
class MapGlLayer extends Layer {
|
|
14
|
+
constructor(options) {
|
|
15
|
+
super(options);
|
|
16
|
+
this.olLayer = new OlLayer({
|
|
17
|
+
source: new Source({}),
|
|
18
|
+
render: this.getOlLayerRender(),
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* Url of the mapbox style.
|
|
22
|
+
* @type {string}
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
this.styleUrl = options.url;
|
|
26
|
+
/**
|
|
27
|
+
* Api key for the url of the mapbox style.
|
|
28
|
+
* If set to false, the apiKey is not required.
|
|
29
|
+
* @type {string}
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
this.apiKey = options.apiKey;
|
|
33
|
+
/**
|
|
34
|
+
* Name of the apiKey to set in the url request.
|
|
35
|
+
* Default is 'key'.
|
|
36
|
+
* @type {string}
|
|
37
|
+
* @private
|
|
38
|
+
*/
|
|
39
|
+
this.apiKeyName = options.apiKeyName || 'key';
|
|
40
|
+
/** @ignore */
|
|
41
|
+
this.updateAttribution = this.updateAttribution.bind(this);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Initialize the layer and listen to feature clicks.
|
|
45
|
+
* @param {ol/Map~Map} map
|
|
46
|
+
*/
|
|
47
|
+
attachToMap(map) {
|
|
48
|
+
super.attachToMap(map);
|
|
49
|
+
if (!this.map) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The feature format.
|
|
54
|
+
* @type {ol/format/GeoJSON}
|
|
55
|
+
*/
|
|
56
|
+
this.format = new GeoJSON({
|
|
57
|
+
featureProjection: this.map.getView().getProjection(),
|
|
58
|
+
});
|
|
59
|
+
this.loadMbMap();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Terminate what was initialized in init function. Remove layer, events...
|
|
63
|
+
*/
|
|
64
|
+
detachFromMap() {
|
|
65
|
+
if (this.mbMap) {
|
|
66
|
+
this.mbMap.off('idle', this.updateAttribution);
|
|
67
|
+
// Some asynchrone repaints are triggered even if the mbMap has been removed,
|
|
68
|
+
// to avoid display of errors we set an empty function.
|
|
69
|
+
this.mbMap.triggerRepaint = () => { };
|
|
70
|
+
this.mbMap.remove();
|
|
71
|
+
this.mbMap = undefined;
|
|
72
|
+
}
|
|
73
|
+
this.loaded = false;
|
|
74
|
+
super.detachFromMap();
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Create the mapbox map.
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
loadMbMap() {
|
|
81
|
+
var _a, _b, _c;
|
|
82
|
+
this.olListenersKeys.push(
|
|
83
|
+
// @ts-ignore
|
|
84
|
+
(_a = this.map) === null || _a === void 0 ? void 0 : _a.on('change:target', () => {
|
|
85
|
+
this.loadMbMap();
|
|
86
|
+
}));
|
|
87
|
+
if (!((_b = this.map) === null || _b === void 0 ? void 0 : _b.getTargetElement())) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (!this.visible) {
|
|
91
|
+
// On next change of visibility we load the map
|
|
92
|
+
this.olListenersKeys.push(
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
this.once('change:visible', () => {
|
|
95
|
+
this.loadMbMap();
|
|
96
|
+
}));
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const container = document.createElement('div');
|
|
100
|
+
container.style.position = 'absolute';
|
|
101
|
+
container.style.width = '100%';
|
|
102
|
+
container.style.height = '100%';
|
|
103
|
+
if (!this.styleUrl) {
|
|
104
|
+
// eslint-disable-next-line no-console
|
|
105
|
+
console.error(`No styleUrl defined for mapbox layer: ${this.styleUrl}`);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (!this.apiKey && !((_c = this.styleUrl) === null || _c === void 0 ? void 0 : _c.includes(this.apiKeyName))) {
|
|
109
|
+
// eslint-disable-next-line no-console
|
|
110
|
+
console.error(`No apiKey defined for mapbox layer with style url to ${this.styleUrl}`);
|
|
111
|
+
}
|
|
112
|
+
const Map = this.getMapboxMapClass();
|
|
113
|
+
/**
|
|
114
|
+
* A mapbox map
|
|
115
|
+
* @type {mapboxgl.Map}
|
|
116
|
+
*/
|
|
117
|
+
this.mbMap = new Map(Object.assign({ style: getUrlWithParams(this.styleUrl, {
|
|
118
|
+
[this.apiKeyName]: this.apiKey,
|
|
119
|
+
}).toString(), container, interactive: false, trackResize: false, attributionControl: false }, (this.options.mapOptions || {})));
|
|
120
|
+
this.mbMap.once('load', () => {
|
|
121
|
+
/**
|
|
122
|
+
* Is the map loaded.
|
|
123
|
+
* @type {boolean}
|
|
124
|
+
*/
|
|
125
|
+
this.loaded = true;
|
|
126
|
+
this.dispatchEvent(new BaseEvent('load'));
|
|
127
|
+
});
|
|
128
|
+
this.mbMap.on('idle', this.updateAttribution);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Update attributions of the source.
|
|
132
|
+
* @private
|
|
133
|
+
*/
|
|
134
|
+
updateAttribution(evt) {
|
|
135
|
+
var _a, _b, _c;
|
|
136
|
+
const newAttributions = getMapboxMapCopyrights(evt.target) || [];
|
|
137
|
+
if (((_a = this.copyrights) === null || _a === void 0 ? void 0 : _a.toString()) !== newAttributions.toString()) {
|
|
138
|
+
this.copyrights = newAttributions;
|
|
139
|
+
// @ts-ignore
|
|
140
|
+
(_c = (_b = this.olLayer) === null || _b === void 0 ? void 0 : _b.getSource()) === null || _c === void 0 ? void 0 : _c.setAttributions(newAttributions);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Request feature information for a given coordinate.
|
|
145
|
+
* @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
|
|
146
|
+
* @param {Object} options A [mapboxgl.Map#queryrenderedfeatures](https://docs.mapbox.com/mapbox-gl-js/api/map/#map#queryrenderedfeatures) options parameter.
|
|
147
|
+
* @return {Promise<FeatureInfo>} Promise with features, layer and coordinate. The original Mapbox feature is available as a property named 'mapboxFeature'.
|
|
148
|
+
*/
|
|
149
|
+
getFeatureInfoAtCoordinate(coordinate, options) {
|
|
150
|
+
// Ignore the getFeatureInfo until the mapbox map is loaded
|
|
151
|
+
if (!options ||
|
|
152
|
+
!this.format ||
|
|
153
|
+
!this.mbMap ||
|
|
154
|
+
!this.mbMap.isStyleLoaded()) {
|
|
155
|
+
return Promise.resolve({ coordinate, features: [], layer: this });
|
|
156
|
+
}
|
|
157
|
+
const pixel = coordinate &&
|
|
158
|
+
this.mbMap.project(toLonLat(coordinate));
|
|
159
|
+
let pixels;
|
|
160
|
+
if (this.hitTolerance) {
|
|
161
|
+
const { x, y } = pixel;
|
|
162
|
+
pixels = [
|
|
163
|
+
{
|
|
164
|
+
x: x - this.hitTolerance,
|
|
165
|
+
y: y - this.hitTolerance,
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
x: x + this.hitTolerance,
|
|
169
|
+
y: y + this.hitTolerance,
|
|
170
|
+
},
|
|
171
|
+
];
|
|
172
|
+
}
|
|
173
|
+
// At this point we get GeoJSON Mapbox feature, we transform it to an OpenLayers
|
|
174
|
+
// feature to be consistent with other layers.
|
|
175
|
+
const features = this.mbMap
|
|
176
|
+
// @ts-ignore
|
|
177
|
+
.queryRenderedFeatures(pixels || pixel, options)
|
|
178
|
+
.map((feature) => {
|
|
179
|
+
const olFeature = this.format.readFeature(feature);
|
|
180
|
+
if (olFeature) {
|
|
181
|
+
// We save the original mapbox feature to avoid losing informations
|
|
182
|
+
// potentially needed for other functionnality like highlighting
|
|
183
|
+
// (id, layer id, source, sourceLayer ...)
|
|
184
|
+
olFeature.set('mapboxFeature', feature);
|
|
185
|
+
}
|
|
186
|
+
return olFeature;
|
|
187
|
+
});
|
|
188
|
+
return Promise.resolve({
|
|
189
|
+
layer: this,
|
|
190
|
+
features,
|
|
191
|
+
coordinate,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Return the render function function for the ol layer.
|
|
196
|
+
*
|
|
197
|
+
*/
|
|
198
|
+
// eslint-disable-next-line class-methods-use-this
|
|
199
|
+
getOlLayerRender() {
|
|
200
|
+
// eslint-disable-next-line no-console
|
|
201
|
+
console.error('This function must be implemented in subclasses');
|
|
202
|
+
const div = document.createElement('div');
|
|
203
|
+
return () => div;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Return the Class to instanciate for the mapbox map.
|
|
207
|
+
*
|
|
208
|
+
* @return {mapboxgl.Map|maplibregl.Map} map
|
|
209
|
+
*/
|
|
210
|
+
// eslint-disable-next-line class-methods-use-this
|
|
211
|
+
getMapboxMapClass() {
|
|
212
|
+
// eslint-disable-next-line no-console
|
|
213
|
+
console.error('This function must be implemented in subclasses');
|
|
214
|
+
// @ts-ignore
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
export default MapGlLayer;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Map } from 'mapbox-gl';
|
|
2
|
+
import { Coordinate } from 'ol/coordinate';
|
|
3
|
+
import type OlMap from 'ol/Map';
|
|
4
|
+
import { Size } from 'ol/size';
|
|
5
|
+
import MapGlLayer, { MapGlLayerOptions } from './MapGlLayer';
|
|
6
|
+
/**
|
|
7
|
+
* A class representing Mapboxlayer to display on BasicMap
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* import { MapboxLayer } from 'mobility-toolbox-js/ol';
|
|
11
|
+
*
|
|
12
|
+
* const layer = new MapboxLayer({
|
|
13
|
+
* url: 'https://maps.geops.io/styles/travic_v2/style.json',
|
|
14
|
+
* apikey: 'yourApiKey',
|
|
15
|
+
* });
|
|
16
|
+
*
|
|
17
|
+
* @classproperty {ol/Map~Map} map - The map where the layer is displayed.
|
|
18
|
+
* @extends {Layer}
|
|
19
|
+
*/
|
|
20
|
+
export default class MapboxLayer extends MapGlLayer {
|
|
21
|
+
renderState?: {
|
|
22
|
+
center?: Coordinate;
|
|
23
|
+
zoom?: number;
|
|
24
|
+
visible?: boolean;
|
|
25
|
+
opacity?: number;
|
|
26
|
+
resolution?: number;
|
|
27
|
+
rotation?: number;
|
|
28
|
+
size?: Size;
|
|
29
|
+
};
|
|
30
|
+
tabIndex?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Initialize the layer and listen to feature clicks.
|
|
33
|
+
* @param {ol/Map~Map} map
|
|
34
|
+
*/
|
|
35
|
+
attachToMap(map: OlMap): void;
|
|
36
|
+
/**
|
|
37
|
+
* Create the mapbox map.
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
loadMbMap(): void;
|
|
41
|
+
getOlLayerRender(): import("ol/layer/Layer").RenderFunction;
|
|
42
|
+
getMapboxMapClass(): typeof Map;
|
|
43
|
+
/**
|
|
44
|
+
* Create a copy of the MapboxLayer.
|
|
45
|
+
* @param {Object} newOptions Options to override
|
|
46
|
+
* @return {MapboxLayer} A MapboxLayer
|
|
47
|
+
*/
|
|
48
|
+
clone(newOptions: MapGlLayerOptions): MapboxLayer;
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=MapboxLayer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MapboxLayer.d.ts","sourceRoot":"","sources":["../../../src/ol/layers/MapboxLayer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,KAAK,KAAK,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE/B,OAAO,UAAU,EAAE,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAE7D;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,OAAO,OAAO,WAAY,SAAQ,UAAU;IACjD,WAAW,CAAC,EAAE;QACZ,MAAM,CAAC,EAAE,UAAU,CAAC;QACpB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,IAAI,CAAC,EAAE,IAAI,CAAC;KACb,CAAC;IAEF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,WAAW,CAAC,GAAG,EAAE,KAAK;IAsBtB;;;OAGG;IACH,SAAS;IAqDT,gBAAgB;IAKhB,iBAAiB;IAIjB;;;;OAIG;IACH,KAAK,CAAC,UAAU,EAAE,iBAAiB;CAGpC"}
|
package/ol/layers/MapboxLayer.js
CHANGED
|
@@ -1,203 +1,109 @@
|
|
|
1
|
-
|
|
2
|
-
import { Map } from
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
canvas.style.opacity = opacity;
|
|
29
|
-
this.renderState.opacity = opacity;
|
|
30
|
-
}
|
|
31
|
-
const { rotation } = viewState;
|
|
32
|
-
if (this.renderState.rotation !== rotation) {
|
|
33
|
-
this.mbMap.rotateTo(-(rotation || 0) * 180 / Math.PI, {
|
|
34
|
-
animate: false
|
|
35
|
-
});
|
|
36
|
-
changed = true;
|
|
37
|
-
this.renderState.rotation = rotation;
|
|
1
|
+
/* eslint-disable no-underscore-dangle */
|
|
2
|
+
import { Map } from 'mapbox-gl';
|
|
3
|
+
import { getMapboxMapCopyrights, getMapboxRender } from '../../common/utils';
|
|
4
|
+
import MapGlLayer from './MapGlLayer';
|
|
5
|
+
/**
|
|
6
|
+
* A class representing Mapboxlayer to display on BasicMap
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import { MapboxLayer } from 'mobility-toolbox-js/ol';
|
|
10
|
+
*
|
|
11
|
+
* const layer = new MapboxLayer({
|
|
12
|
+
* url: 'https://maps.geops.io/styles/travic_v2/style.json',
|
|
13
|
+
* apikey: 'yourApiKey',
|
|
14
|
+
* });
|
|
15
|
+
*
|
|
16
|
+
* @classproperty {ol/Map~Map} map - The map where the layer is displayed.
|
|
17
|
+
* @extends {Layer}
|
|
18
|
+
*/
|
|
19
|
+
export default class MapboxLayer extends MapGlLayer {
|
|
20
|
+
/**
|
|
21
|
+
* Initialize the layer and listen to feature clicks.
|
|
22
|
+
* @param {ol/Map~Map} map
|
|
23
|
+
*/
|
|
24
|
+
attachToMap(map) {
|
|
25
|
+
super.attachToMap(map);
|
|
26
|
+
if (!this.map) {
|
|
27
|
+
return;
|
|
38
28
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
29
|
+
this.olListenersKeys.push(this.map.on('change:size', () => {
|
|
30
|
+
try {
|
|
31
|
+
if (this.mbMap) {
|
|
32
|
+
this.mbMap.resize();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
// ignore render errors
|
|
37
|
+
// eslint-disable-next-line no-console
|
|
38
|
+
console.warn(err);
|
|
39
|
+
}
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create the mapbox map.
|
|
44
|
+
* @private
|
|
45
|
+
*/
|
|
46
|
+
loadMbMap() {
|
|
47
|
+
var _a;
|
|
48
|
+
// If the map hasn't been resized, the center could be [NaN,NaN].
|
|
49
|
+
// We set default good value for the mapbox map, to avoid the app crashes.
|
|
50
|
+
let [x, y] = ((_a = this.map) === null || _a === void 0 ? void 0 : _a.getView().getCenter()) || [];
|
|
51
|
+
if (!x || !y) {
|
|
52
|
+
x = 0;
|
|
53
|
+
y = 0;
|
|
48
54
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
// Options the last render run did happen. If something changes
|
|
56
|
+
// we have to render again
|
|
57
|
+
/** @ignore */
|
|
58
|
+
this.renderState = {
|
|
59
|
+
center: [x, y],
|
|
60
|
+
zoom: undefined,
|
|
61
|
+
rotation: undefined,
|
|
62
|
+
visible: undefined,
|
|
63
|
+
opacity: undefined,
|
|
64
|
+
size: [0, 0],
|
|
65
|
+
};
|
|
66
|
+
super.loadMbMap();
|
|
67
|
+
if (!this.mbMap) {
|
|
68
|
+
// mbMap could ne bull if the map is not in the dom yet.
|
|
69
|
+
return;
|
|
53
70
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (this.mbMap
|
|
57
|
-
|
|
58
|
-
|
|
71
|
+
this.mbMap.once('load', () => {
|
|
72
|
+
var _a, _b;
|
|
73
|
+
if (!this.mbMap) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
this.mbMap.resize();
|
|
77
|
+
/** @ignore */
|
|
78
|
+
this.copyrights = getMapboxMapCopyrights(this.mbMap) || [];
|
|
79
|
+
// @ts-ignore
|
|
80
|
+
(_b = (_a = this.olLayer) === null || _a === void 0 ? void 0 : _a.getSource()) === null || _b === void 0 ? void 0 : _b.setAttributions(this.copyrights);
|
|
81
|
+
});
|
|
82
|
+
const mapboxCanvas = this.mbMap.getCanvas();
|
|
83
|
+
if (mapboxCanvas) {
|
|
84
|
+
if (this.options.tabIndex) {
|
|
85
|
+
mapboxCanvas.setAttribute('tabindex', `${this.options.tabIndex}`);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
// With a tabIndex='-1' the mouse events works but the map is not focused when we click on it
|
|
89
|
+
// so we remove completely the tabIndex attribute.
|
|
90
|
+
mapboxCanvas.removeAttribute('tabindex');
|
|
59
91
|
}
|
|
60
|
-
this.mbMap._render();
|
|
61
|
-
} catch (err) {
|
|
62
|
-
console.warn(err);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return this.mbMap.getContainer();
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
super({
|
|
69
|
-
...options,
|
|
70
|
-
olLayer: mbLayer
|
|
71
|
-
});
|
|
72
|
-
this.styleUrl = options.url;
|
|
73
|
-
this.apiKey = options.apiKey;
|
|
74
|
-
this.apiKeyName = options.apiKeyName || "key";
|
|
75
|
-
this.updateAttribution = this.updateAttribution.bind(this);
|
|
76
|
-
}
|
|
77
|
-
attachToMap(map) {
|
|
78
|
-
super.attachToMap(map);
|
|
79
|
-
if (!this.map || this.mbMap) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
this.format = new GeoJSON({
|
|
83
|
-
featureProjection: this.map.getView().getProjection()
|
|
84
|
-
});
|
|
85
|
-
this.loadMbMap();
|
|
86
|
-
this.olListenersKeys.push(this.map.on("change:size", () => {
|
|
87
|
-
try {
|
|
88
|
-
if (this.mbMap) {
|
|
89
|
-
this.mbMap.resize();
|
|
90
92
|
}
|
|
91
|
-
} catch (err) {
|
|
92
|
-
console.warn(err);
|
|
93
|
-
}
|
|
94
|
-
}));
|
|
95
|
-
}
|
|
96
|
-
detachFromMap() {
|
|
97
|
-
if (this.mbMap) {
|
|
98
|
-
this.mbMap.off("idle", this.updateAttribution);
|
|
99
|
-
this.mbMap.triggerRepaint = () => {
|
|
100
|
-
};
|
|
101
|
-
this.mbMap.remove();
|
|
102
|
-
this.mbMap = null;
|
|
103
|
-
}
|
|
104
|
-
this.loaded = false;
|
|
105
|
-
super.detachFromMap();
|
|
106
|
-
}
|
|
107
|
-
createStyleUrl() {
|
|
108
|
-
return getMapboxStyleUrl(this.apiKey, this.apiKeyName, this.styleUrl);
|
|
109
|
-
}
|
|
110
|
-
loadMbMap() {
|
|
111
|
-
this.olListenersKeys.push(this.map.on("change:target", () => {
|
|
112
|
-
this.loadMbMap();
|
|
113
|
-
}));
|
|
114
|
-
if (!this.map.getTargetElement()) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
if (!this.visible) {
|
|
118
|
-
this.olListenersKeys.push(this.once("change:visible", () => {
|
|
119
|
-
this.loadMbMap();
|
|
120
|
-
}));
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
let [x, y] = this.map.getView().getCenter();
|
|
124
|
-
if (!x || !y) {
|
|
125
|
-
x = 0;
|
|
126
|
-
y = 0;
|
|
127
|
-
}
|
|
128
|
-
const container = document.createElement("div");
|
|
129
|
-
container.style.position = "absolute";
|
|
130
|
-
container.style.width = "100%";
|
|
131
|
-
container.style.height = "100%";
|
|
132
|
-
this.mbMap = new Map({
|
|
133
|
-
style: this.createStyleUrl(),
|
|
134
|
-
container,
|
|
135
|
-
interactive: false,
|
|
136
|
-
trackResize: false,
|
|
137
|
-
attributionControl: false,
|
|
138
|
-
...this.options.mapOptions || {}
|
|
139
|
-
});
|
|
140
|
-
this.renderState = {
|
|
141
|
-
center: [x, y],
|
|
142
|
-
zoom: null,
|
|
143
|
-
rotation: null,
|
|
144
|
-
visible: null,
|
|
145
|
-
opacity: null,
|
|
146
|
-
size: [0, 0]
|
|
147
|
-
};
|
|
148
|
-
this.mbMap.once("load", () => {
|
|
149
|
-
this.mbMap.resize();
|
|
150
|
-
this.loaded = true;
|
|
151
|
-
this.copyrights = getMapboxMapCopyrights(this.mbMap) || [];
|
|
152
|
-
this.olLayer.getSource().setAttributions(this.copyrights);
|
|
153
|
-
this.dispatchEvent({
|
|
154
|
-
type: "load",
|
|
155
|
-
target: this
|
|
156
|
-
});
|
|
157
|
-
});
|
|
158
|
-
const mapboxCanvas = this.mbMap.getCanvas();
|
|
159
|
-
if (mapboxCanvas) {
|
|
160
|
-
if (this.options.tabIndex) {
|
|
161
|
-
mapboxCanvas.setAttribute("tabindex", this.options.tabIndex);
|
|
162
|
-
} else {
|
|
163
|
-
mapboxCanvas.removeAttribute("tabindex");
|
|
164
|
-
}
|
|
165
93
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
updateAttribution(evt) {
|
|
169
|
-
const newAttributions = getMapboxMapCopyrights(evt.target) || [];
|
|
170
|
-
if (this.copyrights.toString() !== newAttributions.toString()) {
|
|
171
|
-
this.copyrights = newAttributions;
|
|
172
|
-
this.olLayer.getSource().setAttributions(newAttributions);
|
|
94
|
+
getOlLayerRender() {
|
|
95
|
+
return getMapboxRender(this);
|
|
173
96
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
return Promise.resolve({ coordinate, features: [], layer: this });
|
|
97
|
+
// eslint-disable-next-line class-methods-use-this
|
|
98
|
+
getMapboxMapClass() {
|
|
99
|
+
return Map;
|
|
178
100
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Create a copy of the MapboxLayer.
|
|
103
|
+
* @param {Object} newOptions Options to override
|
|
104
|
+
* @return {MapboxLayer} A MapboxLayer
|
|
105
|
+
*/
|
|
106
|
+
clone(newOptions) {
|
|
107
|
+
return new MapboxLayer(Object.assign(Object.assign({}, this.options), newOptions));
|
|
186
108
|
}
|
|
187
|
-
const features = this.mbMap.queryRenderedFeatures(pixel, options).map((feature) => {
|
|
188
|
-
const olFeature = this.format.readFeature(feature);
|
|
189
|
-
if (olFeature) {
|
|
190
|
-
olFeature.set("mapboxFeature", feature);
|
|
191
|
-
}
|
|
192
|
-
return olFeature;
|
|
193
|
-
});
|
|
194
|
-
return Promise.resolve({
|
|
195
|
-
layer: this,
|
|
196
|
-
features,
|
|
197
|
-
coordinate
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
clone(newOptions) {
|
|
201
|
-
return new MapboxLayer({ ...this.options, ...newOptions });
|
|
202
|
-
}
|
|
203
109
|
}
|