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.
Files changed (259) hide show
  1. package/api/RealtimeAPI.d.ts +279 -0
  2. package/api/RealtimeAPI.d.ts.map +1 -0
  3. package/api/RealtimeAPI.js +466 -0
  4. package/api/RoutingAPI.d.ts +37 -0
  5. package/api/RoutingAPI.d.ts.map +1 -0
  6. package/api/RoutingAPI.js +32 -12
  7. package/api/StopsAPI.d.ts +38 -0
  8. package/api/StopsAPI.d.ts.map +1 -0
  9. package/api/StopsAPI.js +33 -9
  10. package/api/index.d.ts +4 -0
  11. package/api/index.d.ts.map +1 -0
  12. package/api/index.js +3 -3
  13. package/api/typedefs.d.ts +179 -0
  14. package/api/typedefs.d.ts.map +1 -0
  15. package/api/typedefs.js +75 -0
  16. package/common/api/HttpAPI.d.ts +31 -0
  17. package/common/api/HttpAPI.d.ts.map +1 -0
  18. package/common/api/HttpAPI.js +54 -27
  19. package/common/api/WebSocketAPI.d.ts +153 -0
  20. package/common/api/WebSocketAPI.d.ts.map +1 -0
  21. package/common/api/WebSocketAPI.js +330 -164
  22. package/common/controls/ControlCommon.d.ts +76 -0
  23. package/common/controls/ControlCommon.d.ts.map +1 -0
  24. package/common/controls/ControlCommon.js +150 -0
  25. package/common/controls/CopyrightControlCommon.d.ts +13 -0
  26. package/common/controls/CopyrightControlCommon.d.ts.map +1 -0
  27. package/common/controls/CopyrightControlCommon.js +34 -0
  28. package/common/controls/StopFinderControlCommon.d.ts +55 -0
  29. package/common/controls/StopFinderControlCommon.d.ts.map +1 -0
  30. package/common/controls/StopFinderControlCommon.js +144 -0
  31. package/common/index.d.ts +3 -0
  32. package/common/index.d.ts.map +1 -0
  33. package/common/index.js +2 -4
  34. package/common/layers/LayerCommon.d.ts +94 -0
  35. package/common/layers/LayerCommon.d.ts.map +1 -0
  36. package/common/layers/LayerCommon.js +244 -0
  37. package/common/mixins/RealtimeLayerMixin.d.ts +286 -0
  38. package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
  39. package/common/mixins/RealtimeLayerMixin.js +776 -0
  40. package/common/mixins/UserInteractionsLayerMixin.d.ts +60 -0
  41. package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
  42. package/common/mixins/UserInteractionsLayerMixin.js +241 -0
  43. package/common/styles/index.d.ts +5 -0
  44. package/common/styles/index.d.ts.map +1 -0
  45. package/common/styles/index.js +4 -4
  46. package/common/styles/realtimeDefaultStyle.d.ts +36 -0
  47. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
  48. package/common/styles/realtimeDefaultStyle.js +275 -0
  49. package/common/styles/realtimeDelayStyle.d.ts +12 -0
  50. package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
  51. package/common/styles/realtimeDelayStyle.js +13 -0
  52. package/common/styles/realtimeHeadingStyle.d.ts +12 -0
  53. package/common/styles/realtimeHeadingStyle.d.ts.map +1 -0
  54. package/common/styles/realtimeHeadingStyle.js +85 -0
  55. package/common/styles/realtimeSimpleStyle.d.ts +4 -0
  56. package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
  57. package/common/styles/realtimeSimpleStyle.js +23 -0
  58. package/common/typedefs.d.ts +111 -0
  59. package/common/typedefs.d.ts.map +1 -0
  60. package/common/typedefs.js +52 -0
  61. package/common/utils/compareDepartures.d.ts +11 -0
  62. package/common/utils/compareDepartures.d.ts.map +1 -0
  63. package/common/utils/compareDepartures.js +35 -0
  64. package/common/utils/createCanvas.d.ts +10 -0
  65. package/common/utils/createCanvas.d.ts.map +1 -0
  66. package/common/utils/createCanvas.js +27 -0
  67. package/common/utils/createRealtimeFilters.d.ts +13 -0
  68. package/common/utils/createRealtimeFilters.d.ts.map +1 -0
  69. package/common/utils/createRealtimeFilters.js +74 -0
  70. package/common/utils/debounceDeparturesMessages.d.ts +12 -0
  71. package/common/utils/debounceDeparturesMessages.d.ts.map +1 -0
  72. package/common/utils/debounceDeparturesMessages.js +24 -0
  73. package/common/utils/debounceWebsocketMessages.d.ts +11 -0
  74. package/common/utils/debounceWebsocketMessages.d.ts.map +1 -0
  75. package/common/utils/debounceWebsocketMessages.js +29 -0
  76. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  77. package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
  78. package/common/utils/getLayersAsFlatArray.js +15 -0
  79. package/common/utils/getMapboxMapCopyrights.d.ts +18 -0
  80. package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
  81. package/common/utils/getMapboxMapCopyrights.js +26 -15
  82. package/common/utils/getMapboxRender.d.ts +7 -0
  83. package/common/utils/getMapboxRender.d.ts.map +1 -0
  84. package/common/utils/getMapboxRender.js +87 -0
  85. package/common/utils/getMaplibreRender.d.ts +7 -0
  86. package/common/utils/getMaplibreRender.d.ts.map +1 -0
  87. package/common/utils/getMaplibreRender.js +38 -0
  88. package/common/utils/getRealtimeModeSuffix.d.ts +10 -0
  89. package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
  90. package/common/utils/getRealtimeModeSuffix.js +7 -0
  91. package/common/utils/getUrlWithParams.d.ts +9 -0
  92. package/common/utils/getUrlWithParams.d.ts.map +1 -0
  93. package/common/utils/getUrlWithParams.js +18 -0
  94. package/common/utils/getVehiclePosition.d.ts +16 -0
  95. package/common/utils/getVehiclePosition.d.ts.map +1 -0
  96. package/common/utils/getVehiclePosition.js +67 -37
  97. package/common/utils/index.d.ts +17 -0
  98. package/common/utils/index.d.ts.map +1 -0
  99. package/common/utils/index.js +17 -5
  100. package/common/utils/realtimeConfig.d.ts +49 -0
  101. package/common/utils/realtimeConfig.d.ts.map +1 -0
  102. package/common/utils/realtimeConfig.js +173 -0
  103. package/common/utils/removeDuplicate.d.ts +10 -0
  104. package/common/utils/removeDuplicate.d.ts.map +1 -0
  105. package/common/utils/removeDuplicate.js +12 -5
  106. package/common/utils/renderTrajectories.d.ts +17 -0
  107. package/common/utils/renderTrajectories.d.ts.map +1 -0
  108. package/common/utils/renderTrajectories.js +110 -0
  109. package/common/utils/sortAndFilterDepartures.d.ts +16 -0
  110. package/common/utils/sortAndFilterDepartures.d.ts.map +1 -0
  111. package/common/utils/sortAndFilterDepartures.js +58 -0
  112. package/common/utils/sortByDelay.d.ts +3 -0
  113. package/common/utils/sortByDelay.d.ts.map +1 -0
  114. package/common/utils/sortByDelay.js +17 -15
  115. package/common/utils/timeUtils.d.ts +24 -0
  116. package/common/utils/timeUtils.d.ts.map +1 -0
  117. package/common/utils/timeUtils.js +34 -15
  118. package/iife.d.ts +3 -0
  119. package/iife.d.ts.map +1 -0
  120. package/iife.js +5 -0
  121. package/index.d.ts +10 -0
  122. package/index.d.ts.map +1 -0
  123. package/index.js +8 -6
  124. package/mapbox/controls/CopyrightControl.d.ts +29 -0
  125. package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
  126. package/mapbox/controls/CopyrightControl.js +44 -25
  127. package/mapbox/controls/index.d.ts +2 -0
  128. package/mapbox/controls/index.d.ts.map +1 -0
  129. package/mapbox/controls/index.js +2 -1
  130. package/mapbox/index.d.ts +6 -0
  131. package/mapbox/index.d.ts.map +1 -0
  132. package/mapbox/index.js +5 -4
  133. package/mapbox/layers/Layer.d.ts +59 -0
  134. package/mapbox/layers/Layer.d.ts.map +1 -0
  135. package/mapbox/layers/Layer.js +99 -55
  136. package/mapbox/layers/RealtimeLayer.d.ts +181 -0
  137. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
  138. package/mapbox/layers/RealtimeLayer.js +276 -0
  139. package/mapbox/layers/index.d.ts +3 -0
  140. package/mapbox/layers/index.d.ts.map +1 -0
  141. package/mapbox/layers/index.js +2 -2
  142. package/mapbox/utils/getMercatorResolution.d.ts +9 -0
  143. package/mapbox/utils/getMercatorResolution.d.ts.map +1 -0
  144. package/mapbox/utils/getMercatorResolution.js +18 -0
  145. package/mapbox/utils/getSourceCoordinates.d.ts +9 -0
  146. package/mapbox/utils/getSourceCoordinates.d.ts.map +1 -0
  147. package/mapbox/utils/getSourceCoordinates.js +27 -0
  148. package/mapbox/utils/index.d.ts +3 -0
  149. package/mapbox/utils/index.d.ts.map +1 -0
  150. package/mapbox/utils/index.js +2 -0
  151. package/mbt.js +26061 -16500
  152. package/mbt.js.map +4 -4
  153. package/mbt.min.js +205 -126
  154. package/mbt.min.js.map +4 -4
  155. package/ol/controls/CopyrightControl.d.ts +31 -0
  156. package/ol/controls/CopyrightControl.d.ts.map +1 -0
  157. package/ol/controls/CopyrightControl.js +62 -36
  158. package/ol/controls/RoutingControl.d.ts +193 -0
  159. package/ol/controls/RoutingControl.d.ts.map +1 -0
  160. package/ol/controls/RoutingControl.js +601 -357
  161. package/ol/controls/StopFinderControl.d.ts +30 -0
  162. package/ol/controls/StopFinderControl.d.ts.map +1 -0
  163. package/ol/controls/StopFinderControl.js +30 -8
  164. package/ol/controls/index.d.ts +4 -0
  165. package/ol/controls/index.d.ts.map +1 -0
  166. package/ol/controls/index.js +3 -3
  167. package/ol/index.d.ts +6 -0
  168. package/ol/index.d.ts.map +1 -0
  169. package/ol/index.js +5 -5
  170. package/ol/layers/Layer.d.ts +86 -0
  171. package/ol/layers/Layer.d.ts.map +1 -0
  172. package/ol/layers/Layer.js +163 -77
  173. package/ol/layers/MapGlLayer.d.ts +67 -0
  174. package/ol/layers/MapGlLayer.d.ts.map +1 -0
  175. package/ol/layers/MapGlLayer.js +218 -0
  176. package/ol/layers/MapboxLayer.d.ts +50 -0
  177. package/ol/layers/MapboxLayer.d.ts.map +1 -0
  178. package/ol/layers/MapboxLayer.js +99 -193
  179. package/ol/layers/MapboxStyleLayer.d.ts +129 -0
  180. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
  181. package/ol/layers/MapboxStyleLayer.js +362 -171
  182. package/ol/layers/MaplibreLayer.d.ts +28 -0
  183. package/ol/layers/MaplibreLayer.d.ts.map +1 -0
  184. package/ol/layers/MaplibreLayer.js +30 -135
  185. package/ol/layers/RealtimeLayer.d.ts +202 -0
  186. package/ol/layers/RealtimeLayer.d.ts.map +1 -0
  187. package/ol/layers/RealtimeLayer.js +340 -0
  188. package/ol/layers/RoutingLayer.d.ts +34 -0
  189. package/ol/layers/RoutingLayer.d.ts.map +1 -0
  190. package/ol/layers/RoutingLayer.js +72 -48
  191. package/ol/layers/VectorLayer.d.ts +25 -0
  192. package/ol/layers/VectorLayer.d.ts.map +1 -0
  193. package/ol/layers/VectorLayer.js +34 -18
  194. package/ol/layers/WMSLayer.d.ts +42 -0
  195. package/ol/layers/WMSLayer.d.ts.map +1 -0
  196. package/ol/layers/WMSLayer.js +84 -34
  197. package/ol/layers/index.d.ts +9 -0
  198. package/ol/layers/index.d.ts.map +1 -0
  199. package/ol/layers/index.js +8 -8
  200. package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
  201. package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
  202. package/ol/styles/fullTrajectoryDelayStyle.js +26 -24
  203. package/ol/styles/fullTrajectoryStyle.d.ts +5 -0
  204. package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
  205. package/ol/styles/fullTrajectoryStyle.js +40 -39
  206. package/ol/styles/index.d.ts +3 -0
  207. package/ol/styles/index.d.ts.map +1 -0
  208. package/ol/styles/index.js +2 -2
  209. package/package.json +81 -133
  210. package/setupTests.d.ts +2 -0
  211. package/setupTests.d.ts.map +1 -0
  212. package/setupTests.js +26 -0
  213. package/types/common.d.ts +122 -0
  214. package/types/index.d.ts +11 -0
  215. package/types/realtime.d.ts +320 -0
  216. package/types/routing.d.ts +206 -0
  217. package/types/stops.d.ts +143 -0
  218. package/README.md +0 -23
  219. package/api/RoutingAPI.test.js +0 -25
  220. package/api/StopsAPI.test.js +0 -22
  221. package/api/TralisAPI.js +0 -359
  222. package/api/TralisAPI.test.js +0 -67
  223. package/api/TralisAPIUtils.js +0 -43
  224. package/common/Tracker.js +0 -93
  225. package/common/api/HttpAPI.test.js +0 -50
  226. package/common/api/WebSocketAPI.test.js +0 -311
  227. package/common/controls/Control.js +0 -81
  228. package/common/controls/Control.test.js +0 -87
  229. package/common/layers/Layer.js +0 -213
  230. package/common/layers/Layer.test.js +0 -526
  231. package/common/mixins/CopyrightMixin.js +0 -24
  232. package/common/mixins/SearchMixin.js +0 -110
  233. package/common/mixins/TralisLayerMixin.js +0 -479
  234. package/common/styles/trackerDefaultStyle.js +0 -197
  235. package/common/styles/trackerDelayStyle.js +0 -8
  236. package/common/styles/trackerSimpleStyle.js +0 -18
  237. package/common/trackerConfig.js +0 -152
  238. package/common/trackerConfig.test.js +0 -23
  239. package/common/utils/createTrackerFilters.js +0 -56
  240. package/common/utils/createTrackerFilters.test.js +0 -79
  241. package/common/utils/getMapboxMapCopyrights.test.js +0 -40
  242. package/common/utils/getMapboxStyleUrl.js +0 -22
  243. package/common/utils/removeDuplicate.test.js +0 -19
  244. package/common/utils/timeUtils.test.js +0 -10
  245. package/mapbox/layers/Layer.test.js +0 -182
  246. package/mapbox/layers/TralisLayer.js +0 -182
  247. package/mapbox/layers/TralisLayer.test.js +0 -38
  248. package/mapbox/utils.js +0 -32
  249. package/ol/controls/CopyrightControl.test.js +0 -165
  250. package/ol/controls/RoutingControl.test.js +0 -151
  251. package/ol/controls/StopFinderControl.test.js +0 -48
  252. package/ol/layers/Layer.test.js +0 -174
  253. package/ol/layers/MapboxLayer.test.js +0 -160
  254. package/ol/layers/MapboxStyleLayer.test.js +0 -231
  255. package/ol/layers/RoutingLayer.test.js +0 -40
  256. package/ol/layers/TralisLayer.js +0 -185
  257. package/ol/layers/TralisLayer.test.js +0 -79
  258. package/ol/layers/VectorLayer.test.js +0 -87
  259. 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"}
@@ -1,203 +1,109 @@
1
- import { toLonLat } from "ol/proj";
2
- import { Map } from "mapbox-gl";
3
- import Source from "ol/source/Source";
4
- import OLLayer from "ol/layer/Layer";
5
- import GeoJSON from "ol/format/GeoJSON";
6
- import Layer from "./Layer";
7
- import { getMapboxMapCopyrights, getMapboxStyleUrl } from "../../common/utils";
8
- export default class MapboxLayer extends Layer {
9
- constructor(options = {}) {
10
- const mbLayer = new OLLayer({
11
- source: new Source({}),
12
- render: (frameState) => {
13
- if (!this.mbMap) {
14
- console.warn("Mapbox map doesn't exist.");
15
- return null;
16
- }
17
- let changed = false;
18
- const canvas = this.mbMap.getCanvas();
19
- const { viewState } = frameState;
20
- const visible = this.olLayer.getVisible();
21
- if (this.renderState.visible !== visible) {
22
- canvas.style.display = visible ? "block" : "none";
23
- this.renderState.visible = visible;
24
- canvas.style.position = "absolute";
25
- }
26
- const opacity = this.olLayer.getOpacity();
27
- if (this.renderState.opacity !== opacity) {
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
- if (this.renderState.zoom !== viewState.zoom || this.renderState.center[0] !== viewState.center[0] || this.renderState.center[1] !== viewState.center[1]) {
40
- this.mbMap.jumpTo({
41
- center: toLonLat(viewState.center),
42
- zoom: viewState.zoom - 1,
43
- animate: false
44
- });
45
- changed = true;
46
- this.renderState.zoom = viewState.zoom;
47
- this.renderState.center = viewState.center;
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
- const size = this.map.getSize();
50
- if (this.renderState.size[0] !== size[0] || this.renderState.size[1] !== size[1]) {
51
- changed = true;
52
- this.renderState.size = size;
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
- if (this.mbMap && this.mbMap.style && this.mbMap.isStyleLoaded() && changed) {
55
- try {
56
- if (this.mbMap._frame) {
57
- this.mbMap._frame.cancel();
58
- this.mbMap._frame = null;
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
- this.mbMap.on("idle", this.updateAttribution);
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
- getFeatureInfoAtCoordinate(coordinate, options) {
176
- if (!options || !this.format || !this.mbMap || !this.mbMap.isStyleLoaded()) {
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
- let pixel = coordinate && this.mbMap.project(toLonLat(coordinate));
180
- if (this.hitTolerance) {
181
- const { x, y } = pixel;
182
- pixel = [
183
- { x: x - this.hitTolerance, y: y - this.hitTolerance },
184
- { x: x + this.hitTolerance, y: y + this.hitTolerance }
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
  }