mobility-toolbox-js 2.0.0-beta.3 → 2.0.0-beta.33
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/README.md +4 -1
- package/api/RealtimeAPI.d.ts +268 -0
- package/api/RealtimeAPI.d.ts.map +1 -0
- package/api/RoutingAPI.d.ts +33 -0
- package/api/RoutingAPI.d.ts.map +1 -0
- package/api/StopsAPI.d.ts +36 -0
- package/api/StopsAPI.d.ts.map +1 -0
- package/api/index.d.ts +5 -0
- package/api/index.d.ts.map +1 -0
- package/api/typedefs.d.ts +105 -0
- package/api/typedefs.d.ts.map +1 -0
- package/common/api/HttpAPI.d.ts +31 -0
- package/common/api/HttpAPI.d.ts.map +1 -0
- package/common/api/WebSocketAPI.d.ts +95 -0
- package/common/api/WebSocketAPI.d.ts.map +1 -0
- package/common/controls/Control.d.ts +74 -0
- package/common/controls/Control.d.ts.map +1 -0
- package/common/index.d.ts +3 -0
- package/common/index.d.ts.map +1 -0
- package/common/layers/Layer.d.ts +80 -0
- package/common/layers/Layer.d.ts.map +1 -0
- package/common/mixins/CopyrightMixin.d.ts +22 -0
- package/common/mixins/CopyrightMixin.d.ts.map +1 -0
- package/common/mixins/MapboxLayerMixin.d.ts +27 -0
- package/common/mixins/MapboxLayerMixin.d.ts.map +1 -0
- package/common/mixins/RealtimeLayerMixin.d.ts +58 -0
- package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
- package/common/mixins/StopFinderMixin.d.ts +40 -0
- package/common/mixins/StopFinderMixin.d.ts.map +1 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts +42 -0
- package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
- package/common/styles/index.d.ts +5 -0
- package/common/styles/index.d.ts.map +1 -0
- package/common/styles/realtimeDefaultStyle.d.ts +15 -0
- package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
- package/common/styles/{trackerDelayStyle.js → realtimeDelayStyle.d.ts} +3 -9
- package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
- package/common/styles/realtimeSimpleStyle.d.ts +3 -0
- package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
- package/common/typedefs.d.ts +50 -0
- package/common/typedefs.d.ts.map +1 -0
- package/common/utils/cleanStopTime.d.ts +7 -0
- package/common/utils/cleanStopTime.d.ts.map +1 -0
- package/common/utils/compareDepartures.d.ts +9 -0
- package/common/utils/compareDepartures.d.ts.map +1 -0
- package/common/utils/createCanvas.d.ts +9 -0
- package/common/utils/createCanvas.d.ts.map +1 -0
- package/common/utils/createTrackerFilters.d.ts +12 -0
- package/common/utils/createTrackerFilters.d.ts.map +1 -0
- package/common/utils/getLayersAsFlatArray.d.ts +3 -0
- package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
- package/common/utils/getMapboxMapCopyrights.d.ts +8 -0
- package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
- package/common/utils/getMapboxRender.d.ts +5 -0
- package/common/utils/getMapboxRender.d.ts.map +1 -0
- package/common/utils/getMaplibreRender.d.ts +5 -0
- package/common/utils/getMaplibreRender.d.ts.map +1 -0
- package/common/utils/getRealtimeModeSuffix.d.ts +8 -0
- package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
- package/common/utils/getUrlWithParams.d.ts +9 -0
- package/common/utils/getUrlWithParams.d.ts.map +1 -0
- package/common/utils/getVehiclePosition.d.ts +14 -0
- package/common/utils/getVehiclePosition.d.ts.map +1 -0
- package/common/utils/index.d.ts +12 -0
- package/common/utils/index.d.ts.map +1 -0
- package/common/utils/removeDuplicate.d.ts +10 -0
- package/common/utils/removeDuplicate.d.ts.map +1 -0
- package/common/utils/renderTrajectories.d.ts +20 -0
- package/common/utils/renderTrajectories.d.ts.map +1 -0
- package/common/utils/sortByDelay.d.ts +3 -0
- package/common/utils/sortByDelay.d.ts.map +1 -0
- package/common/utils/timeUtils.d.ts +5 -0
- package/common/utils/timeUtils.d.ts.map +1 -0
- package/common/utils/trackerConfig.d.ts +24 -0
- package/common/utils/trackerConfig.d.ts.map +1 -0
- package/iife.d.ts +3 -0
- package/iife.d.ts.map +1 -0
- package/index.d.ts +6 -0
- package/index.d.ts.map +1 -0
- package/mapbox/controls/CopyrightControl.d.ts +32 -0
- package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
- package/mapbox/controls/index.d.ts +2 -0
- package/mapbox/controls/index.d.ts.map +1 -0
- package/mapbox/index.d.ts +5 -0
- package/mapbox/index.d.ts.map +1 -0
- package/mapbox/layers/Layer.d.ts +47 -0
- package/mapbox/layers/Layer.d.ts.map +1 -0
- package/mapbox/layers/RealtimeLayer.d.ts +118 -0
- package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
- package/mapbox/layers/index.d.ts +3 -0
- package/mapbox/layers/index.d.ts.map +1 -0
- package/mapbox/utils.d.ts +8 -0
- package/mapbox/utils.d.ts.map +1 -0
- package/mbt.js +50042 -0
- package/mbt.js.map +7 -0
- package/mbt.min.js +1008 -0
- package/mbt.min.js.map +7 -0
- package/ol/controls/CopyrightControl.d.ts +31 -0
- package/ol/controls/CopyrightControl.d.ts.map +1 -0
- package/ol/controls/RoutingControl.d.ts +180 -0
- package/ol/controls/RoutingControl.d.ts.map +1 -0
- package/ol/controls/StopFinderControl.d.ts +32 -0
- package/ol/controls/StopFinderControl.d.ts.map +1 -0
- package/ol/controls/index.d.ts +4 -0
- package/ol/controls/index.d.ts.map +1 -0
- package/ol/index.d.ts +6 -0
- package/ol/index.d.ts.map +1 -0
- package/ol/layers/Layer.d.ts +49 -0
- package/ol/layers/Layer.d.ts.map +1 -0
- package/ol/layers/MapboxLayer.d.ts +42 -0
- package/ol/layers/MapboxLayer.d.ts.map +1 -0
- package/ol/layers/MapboxStyleLayer.d.ts +146 -0
- package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
- package/ol/layers/MaplibreLayer.d.ts +27 -0
- package/ol/layers/MaplibreLayer.d.ts.map +1 -0
- package/ol/layers/RealtimeLayer.d.ts +119 -0
- package/ol/layers/RealtimeLayer.d.ts.map +1 -0
- package/ol/layers/RoutingLayer.d.ts +24 -0
- package/ol/layers/RoutingLayer.d.ts.map +1 -0
- package/ol/layers/VectorLayer.d.ts +23 -0
- package/ol/layers/VectorLayer.d.ts.map +1 -0
- package/ol/layers/WMSLayer.d.ts +38 -0
- package/ol/layers/WMSLayer.d.ts.map +1 -0
- package/ol/layers/index.d.ts +9 -0
- package/ol/layers/index.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
- package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
- package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
- package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
- package/ol/styles/index.d.ts +3 -0
- package/ol/styles/index.d.ts.map +1 -0
- package/package.json +56 -95
- package/setupTests.d.ts +2 -0
- package/setupTests.d.ts.map +1 -0
- package/types/index.d.ts +10 -0
- package/types/realtime.d.ts +24 -0
- package/types/routing.d.ts +206 -0
- package/types/stops.d.ts +143 -0
- package/api/index.js +0 -3
- package/api/routing/RoutingAPI.js +0 -44
- package/api/routing/RoutingAPI.test.js +0 -41
- package/api/stops/StopsAPI.js +0 -41
- package/api/stops/StopsAPI.test.js +0 -34
- package/api/tralis/TralisAPI.js +0 -731
- package/api/tralis/TralisAPI.test.js +0 -75
- package/api/tralis/TralisAPIUtils.js +0 -73
- package/api/tralis/WebSocketConnector.js +0 -338
- package/api/tralis/WebSocketConnector.test.js +0 -356
- package/api/tralis/typedefs.js +0 -81
- package/common/Tracker.js +0 -197
- package/common/api/api.js +0 -64
- package/common/api/api.test.js +0 -68
- package/common/controls/Control.js +0 -146
- package/common/controls/Control.test.js +0 -98
- package/common/layers/Layer.js +0 -404
- package/common/layers/Layer.test.js +0 -585
- package/common/mixins/CopyrightMixin.js +0 -48
- package/common/mixins/SearchMixin.js +0 -176
- package/common/mixins/TralisLayerMixin.js +0 -930
- package/common/styles/index.js +0 -4
- package/common/styles/trackerDefaultStyle.js +0 -333
- package/common/styles/trackerSimpleStyle.js +0 -22
- package/common/trackerConfig.js +0 -190
- package/common/trackerConfig.test.js +0 -25
- package/common/typedefs.js +0 -23
- package/common/utils/createTrackerFilters.js +0 -87
- package/common/utils/createTrackerFilters.test.js +0 -95
- package/common/utils/getMapboxMapCopyrights.js +0 -32
- package/common/utils/getMapboxMapCopyrights.test.js +0 -47
- package/common/utils/getMapboxStyleUrl.js +0 -32
- package/common/utils/getVehiclePosition.js +0 -72
- package/common/utils/index.js +0 -6
- package/common/utils/removeDuplicate.js +0 -22
- package/common/utils/removeDuplicate.test.js +0 -22
- package/common/utils/sortByDelay.js +0 -23
- package/common/utils/timeUtils.js +0 -44
- package/common/utils/timeUtils.test.js +0 -16
- package/index.js +0 -2
- package/index.js.map +0 -1
- package/mapbox/controls/CopyrightControl.js +0 -58
- package/mapbox/index.js +0 -3
- package/mapbox/layers/Layer.js +0 -118
- package/mapbox/layers/Layer.test.js +0 -202
- package/mapbox/layers/TralisLayer.js +0 -329
- package/mapbox/layers/TralisLayer.test.js +0 -40
- package/mapbox/utils.js +0 -46
- package/module.js +0 -23
- package/ol/README.md +0 -0
- package/ol/controls/CopyrightControl.js +0 -80
- package/ol/controls/CopyrightControl.test.js +0 -211
- package/ol/controls/RoutingControl.js +0 -752
- package/ol/controls/RoutingControl.test.js +0 -216
- package/ol/controls/StopFinderControl.js +0 -38
- package/ol/controls/StopFinderControl.test.js +0 -59
- package/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
- package/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
- package/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
- package/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
- package/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
- package/ol/controls/snapshots/RoutingControlStation1.json +0 -60
- package/ol/controls/snapshots/RoutingControlStation2.json +0 -49
- package/ol/index.js +0 -13
- package/ol/layers/Layer.js +0 -193
- package/ol/layers/Layer.test.js +0 -197
- package/ol/layers/MapboxLayer.js +0 -378
- package/ol/layers/MapboxLayer.test.js +0 -186
- package/ol/layers/MapboxStyleLayer.js +0 -417
- package/ol/layers/MapboxStyleLayer.test.js +0 -262
- package/ol/layers/MaplibreLayer.js +0 -280
- package/ol/layers/RoutingLayer.js +0 -91
- package/ol/layers/RoutingLayer.test.js +0 -49
- package/ol/layers/TralisLayer.js +0 -359
- package/ol/layers/TralisLayer.test.js +0 -97
- package/ol/layers/VectorLayer.js +0 -43
- package/ol/layers/VectorLayer.test.js +0 -98
- package/ol/layers/WMSLayer.js +0 -80
- package/ol/layers/WMSLayer.test.js +0 -84
- package/ol/styles/fullTrajectoryDelayStyle.js +0 -35
- package/ol/styles/fullTrajectoryStyle.js +0 -51
- package/ol/styles/index.js +0 -2
package/ol/layers/MapboxLayer.js
DELETED
|
@@ -1,378 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-underscore-dangle */
|
|
2
|
-
import { toLonLat } from 'ol/proj';
|
|
3
|
-
import { Map } from 'mapbox-gl';
|
|
4
|
-
import Source from 'ol/source/Source';
|
|
5
|
-
import OLLayer from 'ol/layer/Layer';
|
|
6
|
-
import GeoJSON from 'ol/format/GeoJSON';
|
|
7
|
-
import Layer from './Layer';
|
|
8
|
-
import { getMapboxMapCopyrights, getMapboxStyleUrl } from '../../common/utils';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* A class representing Mapboxlayer to display on BasicMap
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* import { MapboxLayer } from 'mobility-toolbox-js/ol';
|
|
15
|
-
*
|
|
16
|
-
* const layer = new MapboxLayer({
|
|
17
|
-
* url: 'https://maps.geops.io/styles/travic_v2/style.json',
|
|
18
|
-
* apikey: 'yourApiKey',
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* @classproperty {ol/Map~Map} map - The map where the layer is displayed.
|
|
22
|
-
* @extends {Layer}
|
|
23
|
-
*/
|
|
24
|
-
export default class MapboxLayer extends Layer {
|
|
25
|
-
/**
|
|
26
|
-
* Constructor.
|
|
27
|
-
*
|
|
28
|
-
* @param {Object} options
|
|
29
|
-
* @param {boolean} [options.preserveDrawingBuffer=false] If true able to export the canvas.
|
|
30
|
-
* @param {number} [options.fadeDuration=300] Duration of the fade effect in ms.
|
|
31
|
-
*/
|
|
32
|
-
constructor(options = {}) {
|
|
33
|
-
const mbLayer = new OLLayer({
|
|
34
|
-
source: new Source({}),
|
|
35
|
-
render: (frameState) => {
|
|
36
|
-
if (!this.mbMap) {
|
|
37
|
-
// eslint-disable-next-line no-console
|
|
38
|
-
console.warn("Mapbox map doesn't exist.");
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
let changed = false;
|
|
42
|
-
const canvas = this.mbMap.getCanvas();
|
|
43
|
-
const { viewState } = frameState;
|
|
44
|
-
|
|
45
|
-
const visible = this.olLayer.getVisible();
|
|
46
|
-
if (this.renderState.visible !== visible) {
|
|
47
|
-
canvas.style.display = visible ? 'block' : 'none';
|
|
48
|
-
this.renderState.visible = visible;
|
|
49
|
-
// Needed since mapbox-gl 1.9.0.
|
|
50
|
-
// Without you don't see others ol layers on top.
|
|
51
|
-
canvas.style.position = 'absolute';
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const opacity = this.olLayer.getOpacity();
|
|
55
|
-
if (this.renderState.opacity !== opacity) {
|
|
56
|
-
canvas.style.opacity = opacity;
|
|
57
|
-
this.renderState.opacity = opacity;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// adjust view parameters in mapbox
|
|
61
|
-
const { rotation } = viewState;
|
|
62
|
-
if (this.renderState.rotation !== rotation) {
|
|
63
|
-
this.mbMap.rotateTo((-(rotation || 0) * 180) / Math.PI, {
|
|
64
|
-
animate: false,
|
|
65
|
-
});
|
|
66
|
-
changed = true;
|
|
67
|
-
this.renderState.rotation = rotation;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
this.renderState.zoom !== viewState.zoom ||
|
|
72
|
-
this.renderState.center[0] !== viewState.center[0] ||
|
|
73
|
-
this.renderState.center[1] !== viewState.center[1]
|
|
74
|
-
) {
|
|
75
|
-
this.mbMap.jumpTo({
|
|
76
|
-
center: toLonLat(viewState.center),
|
|
77
|
-
zoom: viewState.zoom - 1,
|
|
78
|
-
animate: false,
|
|
79
|
-
});
|
|
80
|
-
changed = true;
|
|
81
|
-
this.renderState.zoom = viewState.zoom;
|
|
82
|
-
this.renderState.center = viewState.center;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const size = this.map.getSize();
|
|
86
|
-
if (
|
|
87
|
-
this.renderState.size[0] !== size[0] ||
|
|
88
|
-
this.renderState.size[1] !== size[1]
|
|
89
|
-
) {
|
|
90
|
-
changed = true;
|
|
91
|
-
this.renderState.size = size;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// cancel the scheduled update & trigger synchronous redraw
|
|
95
|
-
// see https://github.com/mapbox/mapbox-gl-js/issues/7893#issue-408992184
|
|
96
|
-
// NOTE: THIS MIGHT BREAK WHEN UPDATING MAPBOX
|
|
97
|
-
if (
|
|
98
|
-
this.mbMap &&
|
|
99
|
-
this.mbMap.style &&
|
|
100
|
-
this.mbMap.isStyleLoaded() &&
|
|
101
|
-
changed
|
|
102
|
-
) {
|
|
103
|
-
try {
|
|
104
|
-
if (this.mbMap._frame) {
|
|
105
|
-
this.mbMap._frame.cancel();
|
|
106
|
-
this.mbMap._frame = null;
|
|
107
|
-
}
|
|
108
|
-
this.mbMap._render();
|
|
109
|
-
} catch (err) {
|
|
110
|
-
// ignore render errors because it's probably related to
|
|
111
|
-
// a render during an update of the style.
|
|
112
|
-
// eslint-disable-next-line no-console
|
|
113
|
-
console.warn(err);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return this.mbMap.getContainer();
|
|
118
|
-
},
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
super({
|
|
122
|
-
...options,
|
|
123
|
-
olLayer: mbLayer,
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Url of the mapbox style.
|
|
128
|
-
* @type {string}
|
|
129
|
-
* @private
|
|
130
|
-
*/
|
|
131
|
-
this.styleUrl = options.url;
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Api key for the url of the mapbox style.
|
|
135
|
-
* If set to false, the apiKey is not required.
|
|
136
|
-
* @type {string}
|
|
137
|
-
* @private
|
|
138
|
-
*/
|
|
139
|
-
this.apiKey = options.apiKey;
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Name of the apiKey to set in the url request.
|
|
143
|
-
* Default is 'key'.
|
|
144
|
-
* @type {string}
|
|
145
|
-
* @private
|
|
146
|
-
*/
|
|
147
|
-
this.apiKeyName = options.apiKeyName || 'key';
|
|
148
|
-
|
|
149
|
-
/** @ignore */
|
|
150
|
-
this.updateAttribution = this.updateAttribution.bind(this);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Initialize the layer and listen to feature clicks.
|
|
155
|
-
* @param {ol/Map~Map} map
|
|
156
|
-
*/
|
|
157
|
-
init(map) {
|
|
158
|
-
super.init(map);
|
|
159
|
-
|
|
160
|
-
if (!this.map || this.mbMap) {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* The feature format.
|
|
166
|
-
* @type {ol/format/GeoJSON}
|
|
167
|
-
*/
|
|
168
|
-
this.format = new GeoJSON({
|
|
169
|
-
featureProjection: this.map.getView().getProjection(),
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
this.loadMbMap();
|
|
173
|
-
|
|
174
|
-
this.olListenersKeys.push(
|
|
175
|
-
this.map.on('change:size', () => {
|
|
176
|
-
try {
|
|
177
|
-
if (this.mbMap) {
|
|
178
|
-
this.mbMap.resize();
|
|
179
|
-
}
|
|
180
|
-
} catch (err) {
|
|
181
|
-
// ignore render errors
|
|
182
|
-
// eslint-disable-next-line no-console
|
|
183
|
-
console.warn(err);
|
|
184
|
-
}
|
|
185
|
-
}),
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Terminate what was initialized in init function. Remove layer, events...
|
|
191
|
-
*/
|
|
192
|
-
terminate() {
|
|
193
|
-
if (this.mbMap) {
|
|
194
|
-
this.mbMap.off('idle', this.updateAttribution);
|
|
195
|
-
// Some asynchrone repaints are triggered even if the mbMap has been removed,
|
|
196
|
-
// to avoid display of errors we set an empty function.
|
|
197
|
-
this.mbMap.triggerRepaint = () => {};
|
|
198
|
-
this.mbMap.remove();
|
|
199
|
-
this.mbMap = null;
|
|
200
|
-
}
|
|
201
|
-
this.loaded = false;
|
|
202
|
-
super.terminate();
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Returns a style URL with apiKey & apiKeyName infos.
|
|
207
|
-
* @private
|
|
208
|
-
*/
|
|
209
|
-
createStyleUrl() {
|
|
210
|
-
return getMapboxStyleUrl(this.apiKey, this.apiKeyName, this.styleUrl);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Create the mapbox map.
|
|
215
|
-
* @private
|
|
216
|
-
*/
|
|
217
|
-
loadMbMap() {
|
|
218
|
-
this.olListenersKeys.push(
|
|
219
|
-
this.map.on('change:target', () => {
|
|
220
|
-
this.loadMbMap();
|
|
221
|
-
}),
|
|
222
|
-
);
|
|
223
|
-
|
|
224
|
-
if (!this.map.getTargetElement()) {
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (!this.visible) {
|
|
229
|
-
// On next change of visibility we load the map
|
|
230
|
-
this.olListenersKeys.push(
|
|
231
|
-
this.once('change:visible', () => {
|
|
232
|
-
this.loadMbMap();
|
|
233
|
-
}),
|
|
234
|
-
);
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// If the map hasn't been resized, the center could be [NaN,NaN].
|
|
239
|
-
// We set default good value for the mapbox map, to avoid the app crashes.
|
|
240
|
-
let [x, y] = this.map.getView().getCenter();
|
|
241
|
-
if (!x || !y) {
|
|
242
|
-
x = 0;
|
|
243
|
-
y = 0;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
const container = document.createElement('div');
|
|
247
|
-
container.style.position = 'absolute';
|
|
248
|
-
container.style.width = '100%';
|
|
249
|
-
container.style.height = '100%';
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* A mapbox map
|
|
253
|
-
* @type {mapboxgl.Map}
|
|
254
|
-
*/
|
|
255
|
-
this.mbMap = new Map({
|
|
256
|
-
style: this.createStyleUrl(),
|
|
257
|
-
container,
|
|
258
|
-
interactive: false,
|
|
259
|
-
trackResize: false,
|
|
260
|
-
attributionControl: false,
|
|
261
|
-
...(this.options.mapOptions || {}),
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
// Options the last render run did happen. If something changes
|
|
265
|
-
// we have to render again
|
|
266
|
-
/** @ignore */
|
|
267
|
-
this.renderState = {
|
|
268
|
-
center: [x, y],
|
|
269
|
-
zoom: null,
|
|
270
|
-
rotation: null,
|
|
271
|
-
visible: null,
|
|
272
|
-
opacity: null,
|
|
273
|
-
size: [0, 0],
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
this.mbMap.once('load', () => {
|
|
277
|
-
this.mbMap.resize();
|
|
278
|
-
/**
|
|
279
|
-
* Is the map loaded.
|
|
280
|
-
* @type {boolean}
|
|
281
|
-
*/
|
|
282
|
-
this.loaded = true;
|
|
283
|
-
|
|
284
|
-
/** @ignore */
|
|
285
|
-
this.copyrights = getMapboxMapCopyrights(this.mbMap) || [];
|
|
286
|
-
|
|
287
|
-
this.olLayer.getSource().setAttributions(this.copyrights);
|
|
288
|
-
|
|
289
|
-
this.dispatchEvent({
|
|
290
|
-
type: 'load',
|
|
291
|
-
target: this,
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
const mapboxCanvas = this.mbMap.getCanvas();
|
|
296
|
-
if (mapboxCanvas) {
|
|
297
|
-
if (this.options.tabIndex) {
|
|
298
|
-
mapboxCanvas.setAttribute('tabindex', this.options.tabIndex);
|
|
299
|
-
} else {
|
|
300
|
-
// With a tabIndex='-1' the mouse events works but the map is not focused when we click on it
|
|
301
|
-
// so we remove completely the tabIndex attribute.
|
|
302
|
-
mapboxCanvas.removeAttribute('tabindex');
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
this.mbMap.on('idle', this.updateAttribution);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* Update attributions of the source.
|
|
311
|
-
* @private
|
|
312
|
-
*/
|
|
313
|
-
updateAttribution(evt) {
|
|
314
|
-
const newAttributions = getMapboxMapCopyrights(evt.target) || [];
|
|
315
|
-
if (this.copyrights.toString() !== newAttributions.toString()) {
|
|
316
|
-
this.copyrights = newAttributions;
|
|
317
|
-
this.olLayer.getSource().setAttributions(newAttributions);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* Request feature information for a given coordinate.
|
|
323
|
-
* @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
|
|
324
|
-
* @param {Object} options A [mapboxgl.Map#queryrenderedfeatures](https://docs.mapbox.com/mapbox-gl-js/api/map/#map#queryrenderedfeatures) options parameter.
|
|
325
|
-
* @return {Promise<FeatureInfo>} Promise with features, layer and coordinate. The original Mapbox feature is available as a property named 'mapboxFeature'.
|
|
326
|
-
*/
|
|
327
|
-
getFeatureInfoAtCoordinate(coordinate, options) {
|
|
328
|
-
// Ignore the getFeatureInfo until the mapbox map is loaded
|
|
329
|
-
if (
|
|
330
|
-
!options ||
|
|
331
|
-
!this.format ||
|
|
332
|
-
!this.mbMap ||
|
|
333
|
-
!this.mbMap.isStyleLoaded()
|
|
334
|
-
) {
|
|
335
|
-
return Promise.resolve({ coordinate, features: [], layer: this });
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
let pixel = coordinate && this.mbMap.project(toLonLat(coordinate));
|
|
339
|
-
|
|
340
|
-
if (this.hitTolerance) {
|
|
341
|
-
const { x, y } = pixel;
|
|
342
|
-
pixel = [
|
|
343
|
-
{ x: x - this.hitTolerance, y: y - this.hitTolerance },
|
|
344
|
-
{ x: x + this.hitTolerance, y: y + this.hitTolerance },
|
|
345
|
-
];
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
// At this point we get GeoJSON Mapbox feature, we transform it to an OpenLayers
|
|
349
|
-
// feature to be consistent with other layers.
|
|
350
|
-
const features = this.mbMap
|
|
351
|
-
.queryRenderedFeatures(pixel, options)
|
|
352
|
-
.map((feature) => {
|
|
353
|
-
const olFeature = this.format.readFeature(feature);
|
|
354
|
-
if (olFeature) {
|
|
355
|
-
// We save the original mapbox feature to avoid losing informations
|
|
356
|
-
// potentially needed for other functionnality like highlighting
|
|
357
|
-
// (id, layer id, source, sourceLayer ...)
|
|
358
|
-
olFeature.set('mapboxFeature', feature);
|
|
359
|
-
}
|
|
360
|
-
return olFeature;
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
return Promise.resolve({
|
|
364
|
-
layer: this,
|
|
365
|
-
features,
|
|
366
|
-
coordinate,
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* Create a copy of the MapboxLayer.
|
|
372
|
-
* @param {Object} newOptions Options to override
|
|
373
|
-
* @return {MapboxLayer} A MapboxLayer
|
|
374
|
-
*/
|
|
375
|
-
clone(newOptions) {
|
|
376
|
-
return new MapboxLayer({ ...this.options, ...newOptions });
|
|
377
|
-
}
|
|
378
|
-
}
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import OlMap from 'ol/Map';
|
|
2
|
-
import View from 'ol/View';
|
|
3
|
-
import gllib from 'mapbox-gl';
|
|
4
|
-
import MapboxLayer from './MapboxLayer';
|
|
5
|
-
|
|
6
|
-
let layer;
|
|
7
|
-
let map;
|
|
8
|
-
let consoleOutput;
|
|
9
|
-
const styleUrl = 'foo.com/styles';
|
|
10
|
-
|
|
11
|
-
describe('MapboxLayer', () => {
|
|
12
|
-
describe('without apiKey', () => {
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
// Mock console statement
|
|
15
|
-
consoleOutput = [];
|
|
16
|
-
// eslint-disable-next-line no-console
|
|
17
|
-
console.warn = (message) => consoleOutput.push(message);
|
|
18
|
-
layer = new MapboxLayer({
|
|
19
|
-
name: 'Layer',
|
|
20
|
-
url: styleUrl,
|
|
21
|
-
});
|
|
22
|
-
map = new OlMap({
|
|
23
|
-
target: document.createElement('div'),
|
|
24
|
-
view: new View({ center: [0, 0] }),
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test('should be instanced.', () => {
|
|
29
|
-
expect(layer).toBeInstanceOf(MapboxLayer);
|
|
30
|
-
expect(layer.styleUrl).toBe(styleUrl);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test('should not initalized mapbox map.', () => {
|
|
34
|
-
layer.init();
|
|
35
|
-
expect(layer.mbMap).toBe();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('should initalized mapbox map and warn the user if there is no api key defined.', () => {
|
|
39
|
-
layer.init(map);
|
|
40
|
-
expect(layer.mbMap).toBeInstanceOf(gllib.Map);
|
|
41
|
-
expect(consoleOutput[0]).toBe(
|
|
42
|
-
'No apiKey is defined for request to foo.com/styles',
|
|
43
|
-
);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
test('should called terminate on initalization.', () => {
|
|
47
|
-
const spy = jest.spyOn(layer, 'terminate');
|
|
48
|
-
layer.init();
|
|
49
|
-
expect(spy).toHaveBeenCalledTimes(1);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test('should clone', () => {
|
|
53
|
-
const clone = layer.clone({ name: 'clone' });
|
|
54
|
-
expect(clone).not.toBe(layer);
|
|
55
|
-
expect(clone.name).toBe('clone');
|
|
56
|
-
expect(clone).toBeInstanceOf(MapboxLayer);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
describe('with apiKey', () => {
|
|
61
|
-
beforeEach(() => {
|
|
62
|
-
layer = new MapboxLayer({
|
|
63
|
-
name: 'Layer',
|
|
64
|
-
url: styleUrl,
|
|
65
|
-
apiKey: 'apiKey',
|
|
66
|
-
});
|
|
67
|
-
map = new OlMap({
|
|
68
|
-
target: document.createElement('div'),
|
|
69
|
-
view: new View({ center: [0, 0] }),
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
test('should be instanced with apiKey.', () => {
|
|
74
|
-
expect(layer).toBeInstanceOf(MapboxLayer);
|
|
75
|
-
expect(layer.styleUrl).toBe(styleUrl);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('should not initalized mapbox map.', () => {
|
|
79
|
-
layer.init();
|
|
80
|
-
expect(layer.mbMap).toBe();
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test("should initalized mapbox map, with 'apiKey' prop", () => {
|
|
84
|
-
const layer1 = new MapboxLayer({
|
|
85
|
-
name: 'Layer',
|
|
86
|
-
url: styleUrl,
|
|
87
|
-
apiKey: 'apiKeyVal',
|
|
88
|
-
});
|
|
89
|
-
layer1.init(map);
|
|
90
|
-
expect(layer1.mbMap.options.style).toBe('foo.com/styles?key=apiKeyVal');
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
test("should initalized mapbox map, with 'apiKeyName' prop", () => {
|
|
94
|
-
const layer1 = new MapboxLayer({
|
|
95
|
-
name: 'Layer',
|
|
96
|
-
url: styleUrl,
|
|
97
|
-
apiKey: 'test',
|
|
98
|
-
apiKeyName: 'apiKey',
|
|
99
|
-
});
|
|
100
|
-
layer1.init(map);
|
|
101
|
-
expect(layer1.mbMap.options.style).toBe('foo.com/styles?apiKey=test');
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
describe('#getFeatureInfoAtCoordinate()', () => {
|
|
106
|
-
let layer1;
|
|
107
|
-
beforeEach(() => {
|
|
108
|
-
layer1 = new MapboxLayer({
|
|
109
|
-
name: 'Layer',
|
|
110
|
-
url: styleUrl,
|
|
111
|
-
apiKey: 'test',
|
|
112
|
-
apiKeyName: 'apiKey',
|
|
113
|
-
});
|
|
114
|
-
layer1.init(map);
|
|
115
|
-
layer1.mbMap.isStyleLoaded = jest.fn(() => true);
|
|
116
|
-
layer1.mbMap.getSource = jest.fn(() => true);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
afterEach(() => {
|
|
120
|
-
layer1.mbMap.getSource.mockRestore();
|
|
121
|
-
layer1.mbMap.isStyleLoaded.mockRestore();
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test('should set the mapboxFeature as a property', (done) => {
|
|
125
|
-
const mapboxFeature = {
|
|
126
|
-
id: '2',
|
|
127
|
-
type: 'Feature',
|
|
128
|
-
properties: {
|
|
129
|
-
foo: 'bar',
|
|
130
|
-
},
|
|
131
|
-
source: 'barr',
|
|
132
|
-
sourceLayer: 'fooo',
|
|
133
|
-
};
|
|
134
|
-
layer1.mbMap.project = jest.fn((coord) => ({ x: coord[0], y: coord[1] }));
|
|
135
|
-
layer1.mbMap.queryRenderedFeatures = jest.fn(() => [mapboxFeature]);
|
|
136
|
-
layer1.getFeatureInfoAtCoordinate([0, 0], {}).then((featureInfo) => {
|
|
137
|
-
expect(featureInfo.features[0].get('mapboxFeature')).toBe(
|
|
138
|
-
mapboxFeature,
|
|
139
|
-
);
|
|
140
|
-
done();
|
|
141
|
-
});
|
|
142
|
-
layer1.mbMap.project.mockRestore();
|
|
143
|
-
layer1.mbMap.queryRenderedFeatures.mockRestore();
|
|
144
|
-
});
|
|
145
|
-
describe('should use hitTolerance property', () => {
|
|
146
|
-
beforeEach(() => {
|
|
147
|
-
layer1.mbMap.project = jest.fn((coord) => ({
|
|
148
|
-
x: coord[0],
|
|
149
|
-
y: coord[1],
|
|
150
|
-
}));
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
afterEach(() => {
|
|
154
|
-
layer1.mbMap.project.mockRestore();
|
|
155
|
-
layer1.mbMap.queryRenderedFeatures.mockRestore();
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
test('when hitTolerance is not set', (done) => {
|
|
159
|
-
layer1.mbMap.queryRenderedFeatures = jest.fn((pixelBounds) => {
|
|
160
|
-
// Use default hoitTolerance
|
|
161
|
-
expect(pixelBounds).toEqual([
|
|
162
|
-
{ x: -5, y: -5 },
|
|
163
|
-
{ x: 5, y: 5 },
|
|
164
|
-
]);
|
|
165
|
-
done();
|
|
166
|
-
return [];
|
|
167
|
-
});
|
|
168
|
-
layer1.getFeatureInfoAtCoordinate([0, 0], {});
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
test('when hitTolerance is set to 10', (done) => {
|
|
172
|
-
layer1.hitTolerance = 10;
|
|
173
|
-
layer1.mbMap.queryRenderedFeatures = jest.fn((pixelBounds) => {
|
|
174
|
-
// Use default hoitTolerance
|
|
175
|
-
expect(pixelBounds).toEqual([
|
|
176
|
-
{ x: -10, y: -10 },
|
|
177
|
-
{ x: 10, y: 10 },
|
|
178
|
-
]);
|
|
179
|
-
done();
|
|
180
|
-
return [];
|
|
181
|
-
});
|
|
182
|
-
layer1.getFeatureInfoAtCoordinate([0, 0], {});
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
});
|