mobility-toolbox-js 2.0.0-beta.32 → 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.
Files changed (210) hide show
  1. package/api/RealtimeAPI.d.ts +268 -0
  2. package/api/RealtimeAPI.d.ts.map +1 -0
  3. package/api/RoutingAPI.d.ts +33 -0
  4. package/api/RoutingAPI.d.ts.map +1 -0
  5. package/api/StopsAPI.d.ts +36 -0
  6. package/api/StopsAPI.d.ts.map +1 -0
  7. package/api/index.d.ts +5 -0
  8. package/api/index.d.ts.map +1 -0
  9. package/api/typedefs.d.ts +105 -0
  10. package/api/typedefs.d.ts.map +1 -0
  11. package/common/api/HttpAPI.d.ts +31 -0
  12. package/common/api/HttpAPI.d.ts.map +1 -0
  13. package/common/api/WebSocketAPI.d.ts +95 -0
  14. package/common/api/WebSocketAPI.d.ts.map +1 -0
  15. package/common/controls/Control.d.ts +74 -0
  16. package/common/controls/Control.d.ts.map +1 -0
  17. package/common/{index.js → index.d.ts} +1 -0
  18. package/common/index.d.ts.map +1 -0
  19. package/common/layers/Layer.d.ts +80 -0
  20. package/common/layers/Layer.d.ts.map +1 -0
  21. package/common/mixins/CopyrightMixin.d.ts +22 -0
  22. package/common/mixins/CopyrightMixin.d.ts.map +1 -0
  23. package/common/mixins/MapboxLayerMixin.d.ts +27 -0
  24. package/common/mixins/MapboxLayerMixin.d.ts.map +1 -0
  25. package/common/mixins/RealtimeLayerMixin.d.ts +58 -0
  26. package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
  27. package/common/mixins/StopFinderMixin.d.ts +40 -0
  28. package/common/mixins/StopFinderMixin.d.ts.map +1 -0
  29. package/common/mixins/UserInteractionsLayerMixin.d.ts +42 -0
  30. package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
  31. package/common/styles/{index.js → index.d.ts} +1 -0
  32. package/common/styles/index.d.ts.map +1 -0
  33. package/common/styles/realtimeDefaultStyle.d.ts +15 -0
  34. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
  35. package/common/styles/realtimeDelayStyle.d.ts +11 -0
  36. package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
  37. package/common/styles/realtimeSimpleStyle.d.ts +3 -0
  38. package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
  39. package/common/typedefs.d.ts +50 -0
  40. package/common/typedefs.d.ts.map +1 -0
  41. package/common/utils/cleanStopTime.d.ts +7 -0
  42. package/common/utils/cleanStopTime.d.ts.map +1 -0
  43. package/common/utils/compareDepartures.d.ts +9 -0
  44. package/common/utils/compareDepartures.d.ts.map +1 -0
  45. package/common/utils/createCanvas.d.ts +9 -0
  46. package/common/utils/createCanvas.d.ts.map +1 -0
  47. package/common/utils/createTrackerFilters.d.ts +12 -0
  48. package/common/utils/createTrackerFilters.d.ts.map +1 -0
  49. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  50. package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
  51. package/common/utils/getMapboxMapCopyrights.d.ts +8 -0
  52. package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
  53. package/common/utils/getMapboxRender.d.ts +5 -0
  54. package/common/utils/getMapboxRender.d.ts.map +1 -0
  55. package/common/utils/getMaplibreRender.d.ts +5 -0
  56. package/common/utils/getMaplibreRender.d.ts.map +1 -0
  57. package/common/utils/getRealtimeModeSuffix.d.ts +8 -0
  58. package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
  59. package/common/utils/getUrlWithParams.d.ts +9 -0
  60. package/common/utils/getUrlWithParams.d.ts.map +1 -0
  61. package/common/utils/getVehiclePosition.d.ts +14 -0
  62. package/common/utils/getVehiclePosition.d.ts.map +1 -0
  63. package/common/utils/{index.js → index.d.ts} +3 -0
  64. package/common/utils/index.d.ts.map +1 -0
  65. package/common/utils/removeDuplicate.d.ts +10 -0
  66. package/common/utils/removeDuplicate.d.ts.map +1 -0
  67. package/common/utils/renderTrajectories.d.ts +20 -0
  68. package/common/utils/renderTrajectories.d.ts.map +1 -0
  69. package/common/utils/sortByDelay.d.ts +3 -0
  70. package/common/utils/sortByDelay.d.ts.map +1 -0
  71. package/common/utils/timeUtils.d.ts +5 -0
  72. package/common/utils/timeUtils.d.ts.map +1 -0
  73. package/common/utils/trackerConfig.d.ts +24 -0
  74. package/common/utils/trackerConfig.d.ts.map +1 -0
  75. package/iife.d.ts +3 -0
  76. package/iife.d.ts.map +1 -0
  77. package/index.d.ts +6 -0
  78. package/index.d.ts.map +1 -0
  79. package/mapbox/controls/CopyrightControl.d.ts +32 -0
  80. package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
  81. package/mapbox/controls/{index.js → index.d.ts} +1 -0
  82. package/mapbox/controls/index.d.ts.map +1 -0
  83. package/mapbox/{index.js → index.d.ts} +1 -0
  84. package/mapbox/index.d.ts.map +1 -0
  85. package/mapbox/layers/Layer.d.ts +47 -0
  86. package/mapbox/layers/Layer.d.ts.map +1 -0
  87. package/mapbox/layers/RealtimeLayer.d.ts +118 -0
  88. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
  89. package/mapbox/layers/{index.js → index.d.ts} +1 -0
  90. package/mapbox/layers/index.d.ts.map +1 -0
  91. package/mapbox/utils.d.ts +8 -0
  92. package/mapbox/utils.d.ts.map +1 -0
  93. package/mbt.js +2049 -2106
  94. package/mbt.js.map +3 -3
  95. package/mbt.min.js +18 -18
  96. package/mbt.min.js.map +3 -3
  97. package/ol/controls/CopyrightControl.d.ts +31 -0
  98. package/ol/controls/CopyrightControl.d.ts.map +1 -0
  99. package/ol/controls/RoutingControl.d.ts +180 -0
  100. package/ol/controls/RoutingControl.d.ts.map +1 -0
  101. package/ol/controls/StopFinderControl.d.ts +32 -0
  102. package/ol/controls/StopFinderControl.d.ts.map +1 -0
  103. package/ol/controls/{index.js → index.d.ts} +1 -0
  104. package/ol/controls/index.d.ts.map +1 -0
  105. package/ol/{index.js → index.d.ts} +1 -0
  106. package/ol/index.d.ts.map +1 -0
  107. package/ol/layers/Layer.d.ts +49 -0
  108. package/ol/layers/Layer.d.ts.map +1 -0
  109. package/ol/layers/MapboxLayer.d.ts +42 -0
  110. package/ol/layers/MapboxLayer.d.ts.map +1 -0
  111. package/ol/layers/MapboxStyleLayer.d.ts +146 -0
  112. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
  113. package/ol/layers/MaplibreLayer.d.ts +27 -0
  114. package/ol/layers/MaplibreLayer.d.ts.map +1 -0
  115. package/ol/layers/RealtimeLayer.d.ts +119 -0
  116. package/ol/layers/RealtimeLayer.d.ts.map +1 -0
  117. package/ol/layers/RoutingLayer.d.ts +24 -0
  118. package/ol/layers/RoutingLayer.d.ts.map +1 -0
  119. package/ol/layers/VectorLayer.d.ts +23 -0
  120. package/ol/layers/VectorLayer.d.ts.map +1 -0
  121. package/ol/layers/WMSLayer.d.ts +38 -0
  122. package/ol/layers/WMSLayer.d.ts.map +1 -0
  123. package/ol/layers/{index.js → index.d.ts} +1 -0
  124. package/ol/layers/index.d.ts.map +1 -0
  125. package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
  126. package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
  127. package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
  128. package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
  129. package/ol/styles/{index.js → index.d.ts} +1 -0
  130. package/ol/styles/index.d.ts.map +1 -0
  131. package/package.json +14 -36
  132. package/setupTests.d.ts +2 -0
  133. package/setupTests.d.ts.map +1 -0
  134. package/types/index.d.ts +7 -6
  135. package/types/routing.d.ts +25 -25
  136. package/types/stops.d.ts +19 -19
  137. package/api/RealtimeAPI.js +0 -357
  138. package/api/RealtimeAPI.test.js +0 -67
  139. package/api/RoutingAPI.js +0 -15
  140. package/api/RoutingAPI.test.js +0 -25
  141. package/api/StopsAPI.js +0 -12
  142. package/api/StopsAPI.test.js +0 -22
  143. package/api/index.js +0 -3
  144. package/api/typedefs.js +0 -0
  145. package/common/api/HttpAPI.js +0 -32
  146. package/common/api/HttpAPI.test.js +0 -50
  147. package/common/api/WebSocketAPI.js +0 -175
  148. package/common/api/WebSocketAPI.test.js +0 -311
  149. package/common/controls/Control.js +0 -81
  150. package/common/controls/Control.test.js +0 -87
  151. package/common/layers/Layer.js +0 -140
  152. package/common/layers/Layer.test.js +0 -134
  153. package/common/mixins/CopyrightMixin.js +0 -24
  154. package/common/mixins/RealtimeLayerMixin.js +0 -436
  155. package/common/mixins/StopFinderMixin.js +0 -110
  156. package/common/mixins/UserInteractionsLayerMixin.js +0 -124
  157. package/common/mixins/UserInteractionsLayerMixin.test.js +0 -199
  158. package/common/styles/realtimeDefaultStyle.js +0 -202
  159. package/common/styles/realtimeDelayStyle.js +0 -8
  160. package/common/styles/realtimeSimpleStyle.js +0 -18
  161. package/common/typedefs.js +0 -0
  162. package/common/utils/cleanStopTime.js +0 -19
  163. package/common/utils/compareDepartures.js +0 -25
  164. package/common/utils/createCanvas.js +0 -17
  165. package/common/utils/createTrackerFilters.js +0 -56
  166. package/common/utils/createTrackerFilters.test.js +0 -79
  167. package/common/utils/getLayersAsFlatArray.js +0 -14
  168. package/common/utils/getMapboxMapCopyrights.js +0 -19
  169. package/common/utils/getMapboxMapCopyrights.test.js +0 -40
  170. package/common/utils/getRealtimeModeSuffix.js +0 -2
  171. package/common/utils/getUrlWithParams.js +0 -11
  172. package/common/utils/getVehiclePosition.js +0 -42
  173. package/common/utils/removeDuplicate.js +0 -8
  174. package/common/utils/removeDuplicate.test.js +0 -19
  175. package/common/utils/renderTrajectories.js +0 -86
  176. package/common/utils/sortByDelay.js +0 -18
  177. package/common/utils/timeUtils.js +0 -20
  178. package/common/utils/timeUtils.test.js +0 -10
  179. package/common/utils/trackerConfig.js +0 -129
  180. package/common/utils/trackerConfig.test.js +0 -23
  181. package/index.js +0 -8
  182. package/mapbox/controls/CopyrightControl.js +0 -29
  183. package/mapbox/layers/Layer.js +0 -56
  184. package/mapbox/layers/Layer.test.js +0 -182
  185. package/mapbox/layers/RealtimeLayer.js +0 -185
  186. package/mapbox/layers/RealtimeLayer.test.js +0 -10
  187. package/mapbox/utils.js +0 -32
  188. package/ol/controls/CopyrightControl.js +0 -42
  189. package/ol/controls/CopyrightControl.test.js +0 -165
  190. package/ol/controls/RoutingControl.js +0 -388
  191. package/ol/controls/RoutingControl.test.js +0 -146
  192. package/ol/controls/StopFinderControl.js +0 -10
  193. package/ol/controls/StopFinderControl.test.js +0 -48
  194. package/ol/layers/Layer.js +0 -83
  195. package/ol/layers/Layer.test.js +0 -175
  196. package/ol/layers/MapboxLayer.js +0 -204
  197. package/ol/layers/MapboxLayer.test.js +0 -160
  198. package/ol/layers/MapboxStyleLayer.js +0 -187
  199. package/ol/layers/MapboxStyleLayer.test.js +0 -226
  200. package/ol/layers/MaplibreLayer.js +0 -140
  201. package/ol/layers/RealtimeLayer.js +0 -187
  202. package/ol/layers/RealtimeLayer.test.js +0 -67
  203. package/ol/layers/RoutingLayer.js +0 -61
  204. package/ol/layers/RoutingLayer.test.js +0 -39
  205. package/ol/layers/VectorLayer.js +0 -22
  206. package/ol/layers/VectorLayer.test.js +0 -76
  207. package/ol/layers/WMSLayer.js +0 -38
  208. package/ol/layers/WMSLayer.test.js +0 -57
  209. package/ol/styles/fullTrajectoryDelayStyle.js +0 -31
  210. package/ol/styles/fullTrajectoryStyle.js +0 -42
@@ -1,388 +0,0 @@
1
- import { Feature } from "ol";
2
- import { LineString, Point } from "ol/geom";
3
- import { Modify } from "ol/interaction";
4
- import { unByKey } from "ol/Observable";
5
- import { click } from "ol/events/condition";
6
- import { GeoJSON } from "ol/format";
7
- import { buffer } from "ol/extent";
8
- import { fromLonLat, toLonLat } from "ol/proj";
9
- import GeomType from "ol/geom/GeometryType";
10
- import { RoutingAPI } from "../../api";
11
- import Control from "../../common/controls/Control";
12
- import RoutingLayer from "../layers/RoutingLayer";
13
- const REGEX_VIA_POINT = /^([^@$!\n]*)(@?([\d.]+),([\d.]+))?(\$?([a-zA-Z0-9]{0,2}))$/;
14
- const REGEX_VIA_POINT_COORD = /^([\d.]+),([\d.]+)$/;
15
- const REGEX_VIA_POINT_STATION_ID = /^!([^$]*)(\$?([a-zA-Z0-9]{0,2}))$/;
16
- const STOP_FETCH_ABORT_CONTROLLER_KEY = "stop-fetch";
17
- const getFlatCoordinatesFromSegments = (segmentArray) => {
18
- const coords = [];
19
- segmentArray.forEach((seg) => {
20
- coords.push(...seg.getGeometry().getCoordinates());
21
- });
22
- return coords;
23
- };
24
- class RoutingControl extends Control {
25
- constructor(options = {}) {
26
- super(options);
27
- Object.defineProperties(this, {
28
- mot: {
29
- get: () => this.get("mot"),
30
- set: (newMot) => {
31
- if (newMot) {
32
- this.set("mot", newMot);
33
- if (this.viaPoints) {
34
- this.drawRoute();
35
- }
36
- }
37
- }
38
- },
39
- loading: {
40
- get: () => this.get("loading"),
41
- set: (newLoading) => {
42
- this.set("loading", newLoading);
43
- }
44
- },
45
- modify: {
46
- get: () => this.get("modify"),
47
- set: (modify) => {
48
- this.set("modify", modify);
49
- }
50
- }
51
- });
52
- this.loading = false;
53
- this.graphs = options.graphs || [["osm", 0, 99]];
54
- this.mot = options.mot || "bus";
55
- this.modify = options.modify !== false;
56
- this.routingApiParams = options.routingApiParams || {};
57
- this.useRawViaPoints = options.useRawViaPoints || false;
58
- this.snapToClosestStation = options.snapToClosestStation || false;
59
- this.cacheStationData = {};
60
- this.abortControllers = [];
61
- this.apiKey = options.apiKey;
62
- this.stopsApiKey = options.stopsApiKey || this.apiKey;
63
- this.segments = [];
64
- this.stopsApiUrl = options.stopsApiUrl || "https://api.geops.io/stops/v1/";
65
- this.api = new RoutingAPI({
66
- ...options
67
- });
68
- this.routingLayer = options.routingLayer || new RoutingLayer({
69
- name: "routing-layer",
70
- style: options.style
71
- });
72
- this.onRouteError = options.onRouteError || ((error) => {
73
- this.dispatchEvent({
74
- type: "change:route",
75
- target: this
76
- });
77
- this.reset();
78
- console.error(error);
79
- });
80
- this.viaPoints = [];
81
- this.onMapClick = this.onMapClick.bind(this);
82
- this.onModifyEnd = this.onModifyEnd.bind(this);
83
- this.onModifyStart = this.onModifyStart.bind(this);
84
- this.apiChangeListener = () => this.drawRoute();
85
- this.createModifyInteraction();
86
- }
87
- static getGraphsResolutions(graphs, map) {
88
- const view = map.getView();
89
- return graphs.map(([, minZoom, maxZoom]) => [
90
- view.getResolutionForZoom(minZoom),
91
- view.getResolutionForZoom(maxZoom || minZoom + 1)
92
- ]);
93
- }
94
- addViaPoint(coordinatesOrString, index = -1, overwrite = 0) {
95
- this.viaPoints.splice(index === -1 ? this.viaPoints.length : index, overwrite, coordinatesOrString);
96
- this.drawRoute();
97
- this.dispatchEvent({
98
- type: "change:route",
99
- target: this
100
- });
101
- }
102
- removeViaPoint(index = this.viaPoints.length - 1) {
103
- if (this.viaPoints.length && this.viaPoints[index]) {
104
- this.viaPoints.splice(index, 1);
105
- }
106
- this.drawRoute();
107
- this.dispatchEvent({
108
- type: "change:route",
109
- target: this
110
- });
111
- }
112
- setViaPoints(coordinateArray) {
113
- this.viaPoints = [...coordinateArray];
114
- this.drawRoute();
115
- this.dispatchEvent({
116
- type: "change:route",
117
- target: this
118
- });
119
- }
120
- reset() {
121
- this.abortRequests();
122
- this.viaPoints = [];
123
- this.routingLayer.olLayer.getSource().clear();
124
- this.dispatchEvent({
125
- type: "change:route",
126
- target: this
127
- });
128
- }
129
- abortRequests() {
130
- this.graphs.forEach(([graph]) => {
131
- if (this.abortControllers[graph]) {
132
- this.abortControllers[graph].abort();
133
- }
134
- this.abortControllers[graph] = new AbortController();
135
- });
136
- this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY]?.abort();
137
- this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY] = new AbortController();
138
- this.loading = false;
139
- }
140
- drawRoute() {
141
- this.abortRequests();
142
- this.routingLayer.olLayer.getSource().clear();
143
- if (!this.viaPoints.length) {
144
- return null;
145
- }
146
- if (this.viaPoints.length === 1) {
147
- return this.drawViaPoint(this.viaPoints[0], 0, this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY]);
148
- }
149
- const formattedViaPoints = this.viaPoints.map((viaPoint) => {
150
- if (Array.isArray(viaPoint)) {
151
- const projection = this.map.getView().getProjection();
152
- const [lon, lat] = toLonLat(viaPoint, projection);
153
- return this.snapToClosestStation ? [`@${lat}`, lon] : [lat, lon];
154
- }
155
- return this.useRawViaPoints ? viaPoint : `!${viaPoint}`;
156
- });
157
- this.loading = true;
158
- this.routingLayer.olLayer.getSource().clear();
159
- this.viaPoints.forEach((viaPoint, idx) => this.drawViaPoint(viaPoint, idx, this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY]));
160
- return Promise.all(this.graphs.map(([graph], index) => {
161
- return this.api.route({
162
- graph,
163
- via: `${formattedViaPoints.join("|")}`,
164
- mot: `${this.mot}`,
165
- "resolve-hops": false,
166
- elevation: false,
167
- "coord-radius": 100,
168
- "coord-punish": 1e3,
169
- ...this.routingApiParams
170
- }, this.abortControllers[graph]).then((featureCollection) => {
171
- this.segments = this.format.readFeatures(featureCollection);
172
- if (this.mot === "foot") {
173
- const uniqueVias = this.segments.reduce((resultVias, currentFeat) => {
174
- const segTrg = currentFeat.get("trg");
175
- return resultVias.find((via) => via[0] === segTrg[0] && via[1] === segTrg[1]) ? resultVias : [...resultVias, segTrg];
176
- }, []);
177
- this.segments = uniqueVias.map((via) => {
178
- const viaSegments = this.segments.filter((seg) => {
179
- const segTrg = seg.get("trg");
180
- return segTrg[0] === via[0] && segTrg[1] === via[1];
181
- });
182
- const coords2 = getFlatCoordinatesFromSegments(viaSegments);
183
- return new Feature({
184
- geometry: new LineString(coords2)
185
- });
186
- });
187
- }
188
- const coords = getFlatCoordinatesFromSegments(this.segments);
189
- const routeFeature = new Feature({
190
- geometry: new LineString(coords)
191
- });
192
- routeFeature.set("graph", graph);
193
- routeFeature.set("mot", this.mot);
194
- routeFeature.set("minResolution", this.graphsResolutions[index][0]);
195
- routeFeature.set("maxResolution", this.graphsResolutions[index][1]);
196
- this.routingLayer.olLayer.getSource().addFeature(routeFeature);
197
- this.loading = false;
198
- }).catch((error) => {
199
- if (error.name === "AbortError") {
200
- return;
201
- }
202
- this.segments = [];
203
- this.dispatchEvent({
204
- type: "error",
205
- target: this
206
- });
207
- this.onRouteError(error, this);
208
- this.loading = false;
209
- });
210
- }));
211
- }
212
- drawViaPoint(viaPoint, idx, abortController) {
213
- const pointFeature = new Feature();
214
- pointFeature.set("viaPointIdx", idx);
215
- if (Array.isArray(viaPoint)) {
216
- pointFeature.setGeometry(new Point(viaPoint));
217
- this.routingLayer.olLayer.getSource().addFeature(pointFeature);
218
- return Promise.resolve(pointFeature);
219
- }
220
- if (!this.useRawViaPoints || REGEX_VIA_POINT_STATION_ID.test(viaPoint)) {
221
- let stationId;
222
- let track2;
223
- if (this.useRawViaPoints) {
224
- [, stationId, , track2] = REGEX_VIA_POINT_STATION_ID.exec(viaPoint);
225
- } else {
226
- [stationId, track2] = viaPoint.split("$");
227
- }
228
- return fetch(`${this.stopsApiUrl}lookup/${stationId}?key=${this.stopsApiKey}`, { signal: abortController.signal }).then((res) => res.json()).then((stationData) => {
229
- const { coordinates } = stationData.features[0].geometry;
230
- this.cacheStationData[viaPoint] = fromLonLat(coordinates);
231
- pointFeature.set("viaPointTrack", track2);
232
- pointFeature.setGeometry(new Point(fromLonLat(coordinates)));
233
- this.routingLayer.olLayer.getSource().addFeature(pointFeature);
234
- return pointFeature;
235
- }).catch((error) => {
236
- if (error.name === "AbortError") {
237
- return;
238
- }
239
- this.dispatchEvent({
240
- type: "error",
241
- target: this
242
- });
243
- this.onRouteError(error, this);
244
- this.loading = false;
245
- });
246
- }
247
- if (this.useRawViaPoints && REGEX_VIA_POINT_COORD.test(viaPoint)) {
248
- const [lat2, lon2] = REGEX_VIA_POINT_COORD.exec(viaPoint);
249
- const coordinates = fromLonLat([parseFloat(lon2), parseFloat(lat2)], this.map.getView().getProjection());
250
- pointFeature.setGeometry(new Point(coordinates));
251
- this.routingLayer.olLayer.getSource().addFeature(pointFeature);
252
- return Promise.resolve(pointFeature);
253
- }
254
- const [, stationName, , lat, lon, , track] = REGEX_VIA_POINT.exec(viaPoint);
255
- if (lon && lat) {
256
- const coordinates = fromLonLat([parseFloat(lon), parseFloat(lat)], this.map.getView().getProjection());
257
- pointFeature.set("viaPointTrack", track);
258
- pointFeature.setGeometry(new Point(coordinates));
259
- this.routingLayer.olLayer.getSource().addFeature(pointFeature);
260
- return Promise.resolve(pointFeature);
261
- }
262
- if (stationName) {
263
- return fetch(`${this.stopsApiUrl}?key=${this.stopsApiKey}&q=${stationName}&limit=1`, { signal: abortController.signal }).then((res) => res.json()).then((stationData) => {
264
- const { coordinates } = stationData.features[0].geometry;
265
- this.cacheStationData[viaPoint] = fromLonLat(coordinates);
266
- pointFeature.set("viaPointTrack", track);
267
- pointFeature.setGeometry(new Point(fromLonLat(coordinates)));
268
- this.routingLayer.olLayer.getSource().addFeature(pointFeature);
269
- return pointFeature;
270
- }).catch((error) => {
271
- this.dispatchEvent({
272
- type: "error",
273
- target: this
274
- });
275
- this.onRouteError(error, this);
276
- this.loading = false;
277
- return null;
278
- });
279
- }
280
- return Promise.resolve(null);
281
- }
282
- onMapClick(e) {
283
- const feats = e.target.getFeaturesAtPixel(e.pixel);
284
- const viaPoint = feats.find((feat) => feat.getGeometry()?.getType() === GeomType.POINT && feat.get("viaPointIdx") !== void 0);
285
- if (viaPoint) {
286
- this.removeViaPoint(viaPoint.get("viaPointIdx"));
287
- return;
288
- }
289
- this.addViaPoint(e.coordinate);
290
- }
291
- onModifyStart(evt) {
292
- let segmentIndex = -1;
293
- const route = evt.features.getArray().find((feat) => feat.getGeometry()?.getType() === GeomType.LINE_STRING);
294
- if (route) {
295
- const closestExtent = buffer(new Point(route.getGeometry().getClosestPoint(evt.mapBrowserEvent.coordinate)).getExtent(), 1e-3);
296
- segmentIndex = this.segments.findIndex((segment) => segment.getGeometry().intersectsExtent(closestExtent));
297
- }
298
- const viaPoint = (evt.features.getArray().filter((feat) => feat.getGeometry()?.getType() === GeomType.POINT) || [])[0];
299
- this.initialRouteDrag = {
300
- viaPoint,
301
- oldRoute: route && route.clone(),
302
- segmentIndex
303
- };
304
- }
305
- onModifyEnd(evt) {
306
- const coord = evt.mapBrowserEvent.coordinate;
307
- const { oldRoute, viaPoint, segmentIndex } = this.initialRouteDrag;
308
- if (viaPoint) {
309
- return this.addViaPoint(coord, viaPoint.get("viaPointIdx"), 1);
310
- }
311
- if (!oldRoute) {
312
- return this.addViaPoint(coord, 0, 1);
313
- }
314
- if (segmentIndex === -1) {
315
- return Promise.reject(new Error("No segment found"));
316
- }
317
- return this.addViaPoint(coord, segmentIndex + 1);
318
- }
319
- createDefaultElement() {
320
- this.element = document.createElement("button");
321
- this.element.id = "ol-toggle-routing";
322
- this.element.innerHTML = "Toggle Route Control";
323
- this.element.onclick = () => this.active ? this.deactivate() : this.activate();
324
- Object.assign(this.element.style, {
325
- position: "absolute",
326
- right: "10px",
327
- top: "10px"
328
- });
329
- }
330
- createModifyInteraction() {
331
- this.modifyInteraction = new Modify({
332
- source: this.routingLayer.olLayer.getSource(),
333
- pixelTolerance: 4,
334
- hitDetection: this.routingLayer.olLayer,
335
- deleteCondition: (e) => {
336
- const feats = e.target.getFeaturesAtPixel(e.pixel, {
337
- hitTolerance: 5
338
- });
339
- const viaPoint = feats.find((feat) => feat.getGeometry()?.getType() === GeomType.POINT && feat.get("index"));
340
- if (click(e) && viaPoint) {
341
- this.removeViaPoint(viaPoint.get("index"));
342
- return true;
343
- }
344
- return false;
345
- }
346
- });
347
- this.modifyInteraction.on("modifystart", this.onModifyStart);
348
- this.modifyInteraction.on("modifyend", this.onModifyEnd);
349
- this.modifyInteraction.setActive(false);
350
- }
351
- addListeners() {
352
- if (!this.modify) {
353
- return;
354
- }
355
- this.removeListeners();
356
- this.onMapClickKey = this.map.on("singleclick", this.onMapClick);
357
- }
358
- removeListeners() {
359
- unByKey(this.onMapClickKey);
360
- }
361
- activate() {
362
- super.activate();
363
- if (this.map) {
364
- this.format = new GeoJSON({
365
- featureProjection: this.map.getView().getProjection()
366
- });
367
- this.graphsResolutions = RoutingControl.getGraphsResolutions(this.graphs, this.map);
368
- this.map.removeInteraction(this.modifyInteraction);
369
- this.routingLayer.attachToMap(this.map);
370
- this.map.addInteraction(this.modifyInteraction);
371
- this.modifyInteraction.setActive(this.modify);
372
- this.addListeners();
373
- } else {
374
- this.format = new GeoJSON({ featureProjection: "EPSG:3857" });
375
- this.graphsResolutions = this.graphs;
376
- }
377
- }
378
- deactivate() {
379
- if (this.map) {
380
- this.routingLayer.detachFromMap(this.map);
381
- this.map.removeInteraction(this.modifyInteraction);
382
- this.removeListeners();
383
- this.reset();
384
- }
385
- super.deactivate();
386
- }
387
- }
388
- export default RoutingControl;
@@ -1,146 +0,0 @@
1
- import fetch from "jest-fetch-mock";
2
- import View from "ol/View";
3
- import Map from "ol/Map";
4
- import RoutingControl from "./RoutingControl";
5
- import RoutingControlStation1 from "./snapshots/RoutingControlStation1.json";
6
- import RoutingControlStation2 from "./snapshots/RoutingControlStation2.json";
7
- import RoutingControlRouteGen5 from "./snapshots/RoutingControlRouteGen5.json";
8
- import RoutingControlRouteGen10 from "./snapshots/RoutingControlRouteGen10.json";
9
- import RoutingControlRouteGen30 from "./snapshots/RoutingControlRouteGen30.json";
10
- import RoutingControlRouteGen100 from "./snapshots/RoutingControlRouteGen100.json";
11
- import RoutingControlRouteOSM from "./snapshots/RoutingControlRouteOSM.json";
12
- describe("RoutingControl", () => {
13
- let map;
14
- beforeEach(() => {
15
- const target = document.createElement("div");
16
- document.body.appendChild(target);
17
- map = new Map({
18
- target,
19
- view: new View({
20
- center: [0, 0],
21
- zoom: 0
22
- })
23
- });
24
- global.fetch = fetch;
25
- });
26
- afterEach(() => {
27
- if (map) {
28
- map.setTarget(null);
29
- map = null;
30
- }
31
- fetch.resetMocks();
32
- });
33
- test("should be activate by default", () => {
34
- const control = new RoutingControl();
35
- expect(control.active).toBe(true);
36
- expect(control.snapToClosestStation).toBe(false);
37
- expect(control.useRawViaPoints).toBe(false);
38
- });
39
- test("launch routing and add features", (done) => {
40
- fetch.mockResponseOnce(JSON.stringify(global.fetchRouteResponse));
41
- const control = new RoutingControl({
42
- url: "https://foo.ch",
43
- apiKey: "foo"
44
- });
45
- control.attachToMap(map);
46
- expect(map.getTargetElement().querySelector("#ol-toggle-routing")).toBeDefined();
47
- control.viaPoints = [
48
- [950476.4055933182, 6003322253698345e-9],
49
- [950389.0813034325, 6003656659274571e-9]
50
- ];
51
- control.drawRoute(control.viaPoints).then(() => {
52
- expect(fetch.mock.calls[0][0]).toEqual("https://foo.ch/?key=foo&graph=osm&via=47.3739194713294%2C8.538274823394632%7C47.37595378493421%2C8.537490375951839&mot=bus&resolve-hops=false&elevation=false&coord-radius=100&coord-punish=1000");
53
- expect(control.routingLayer.olLayer.getSource().getFeatures().length).toEqual(3);
54
- done();
55
- });
56
- });
57
- test("launch routing and add features for multiple graphs", (done) => {
58
- fetch.mockResponses([JSON.stringify(RoutingControlStation1), { status: 200 }], [JSON.stringify(RoutingControlStation2), { status: 200 }], [JSON.stringify(RoutingControlRouteGen5), { status: 200 }], [JSON.stringify(RoutingControlRouteGen10), { status: 200 }], [JSON.stringify(RoutingControlRouteGen30), { status: 200 }], [JSON.stringify(RoutingControlRouteGen100), { status: 200 }], [JSON.stringify(RoutingControlRouteOSM), { status: 200 }]);
59
- const control = new RoutingControl({
60
- url: "https://foo.ch/",
61
- stopsApiUrl: "https://foo.ch/",
62
- apiKey: "foo",
63
- graphs: [
64
- ["gen5", 6, 7],
65
- ["gen10", 8],
66
- ["gen30", 9, 10],
67
- ["gen100", 11, 13],
68
- ["osm", 14, 99]
69
- ]
70
- });
71
- control.attachToMap(map);
72
- control.viaPoints = ["a4dca961d199ff76", "e3666f03cba06b2b"];
73
- control.drawRoute(control.viaPoints).then(() => {
74
- expect(fetch.mock.calls[0][0]).toEqual("https://foo.ch/lookup/a4dca961d199ff76?key=foo");
75
- expect(fetch.mock.calls[1][0]).toEqual("https://foo.ch/lookup/e3666f03cba06b2b?key=foo");
76
- expect(fetch.mock.calls[2][0]).toEqual("https://foo.ch/?key=foo&graph=gen5&via=%21a4dca961d199ff76%7C%21e3666f03cba06b2b&mot=bus&resolve-hops=false&elevation=false&coord-radius=100&coord-punish=1000");
77
- expect(fetch.mock.calls[3][0]).toEqual("https://foo.ch/?key=foo&graph=gen10&via=%21a4dca961d199ff76%7C%21e3666f03cba06b2b&mot=bus&resolve-hops=false&elevation=false&coord-radius=100&coord-punish=1000");
78
- expect(fetch.mock.calls[4][0]).toEqual("https://foo.ch/?key=foo&graph=gen30&via=%21a4dca961d199ff76%7C%21e3666f03cba06b2b&mot=bus&resolve-hops=false&elevation=false&coord-radius=100&coord-punish=1000");
79
- expect(fetch.mock.calls[5][0]).toEqual("https://foo.ch/?key=foo&graph=gen100&via=%21a4dca961d199ff76%7C%21e3666f03cba06b2b&mot=bus&resolve-hops=false&elevation=false&coord-radius=100&coord-punish=1000");
80
- expect(fetch.mock.calls[6][0]).toEqual("https://foo.ch/?key=foo&graph=osm&via=%21a4dca961d199ff76%7C%21e3666f03cba06b2b&mot=bus&resolve-hops=false&elevation=false&coord-radius=100&coord-punish=1000");
81
- expect(control.routingLayer.olLayer.getSource().getFeatures().length).toEqual(7);
82
- done();
83
- });
84
- });
85
- test("ignores Abort Error and returns undefined", (done) => {
86
- const control = new RoutingControl({
87
- url: "https://foo.ch",
88
- apiKey: "foo"
89
- });
90
- control.attachToMap(map);
91
- control.viaPoints = [
92
- [950476.4055933182, 6003322253698345e-9],
93
- [950389.0813034325, 6003656659274571e-9]
94
- ];
95
- const error = new Error("Error");
96
- error.name = "AbortError";
97
- fetch.mockRejectOnce(error);
98
- return control.drawRoute().then((data) => {
99
- expect(data).toEqual([void 0]);
100
- done();
101
- });
102
- });
103
- test("calls routing api with @ before the coordinates when snapToClosestStation is true", (done) => {
104
- fetch.mockResponses([JSON.stringify(RoutingControlStation1), { status: 200 }], [JSON.stringify(global.fetchRouteResponse), { status: 200 }]);
105
- const control = new RoutingControl({
106
- apiKey: "foo",
107
- snapToClosestStation: true
108
- });
109
- control.attachToMap(map);
110
- expect(map.getTarget().querySelector("#ol-toggle-routing")).toBeDefined();
111
- control.viaPoints = [
112
- [950476.4055933182, 6003322253698345e-9],
113
- [950389.0813034325, 6003656659274571e-9],
114
- "e3666f03cba06b2b"
115
- ];
116
- control.drawRoute(control.viaPoints).then(() => {
117
- const { searchParams } = new URL(fetch.mock.calls[1][0]);
118
- expect(searchParams.get("via")).toBe("@47.3739194713294,8.538274823394632|@47.37595378493421,8.537490375951839|!e3666f03cba06b2b");
119
- done();
120
- });
121
- });
122
- test("calls routing api with raw via points", (done) => {
123
- fetch.mockResponses([JSON.stringify(RoutingControlStation1), { status: 200 }], [JSON.stringify(RoutingControlStation2), { status: 200 }], [JSON.stringify(global.fetchRouteResponse), { status: 200 }]);
124
- const control = new RoutingControl({
125
- apiKey: "foo",
126
- useRawViaPoints: true
127
- });
128
- control.attachToMap(map);
129
- expect(map.getTarget().querySelector("#ol-toggle-routing")).toBeDefined();
130
- control.viaPoints = [
131
- "46.2,7.1",
132
- "@46.2,7.1",
133
- "@46.2,7$1",
134
- "station name$2",
135
- "station name@46.2,7",
136
- "stationname@46.2,7.7$3",
137
- "!stationid",
138
- [950389, 6003656]
139
- ];
140
- control.drawRoute(control.viaPoints).then(() => {
141
- const params = new URL(fetch.mock.calls[2][0]).searchParams;
142
- expect(params.get("via")).toBe("46.2,7.1|@46.2,7.1|@46.2,7$1|station name$2|station name@46.2,7|stationname@46.2,7.7$3|!stationid|47.375949774398805,8.537489645590679");
143
- done();
144
- });
145
- });
146
- });
@@ -1,10 +0,0 @@
1
- import { fromLonLat } from "ol/proj";
2
- import Control from "../../common/controls/Control";
3
- import mixin from "../../common/mixins/StopFinderMixin";
4
- class StopFinderControl extends mixin(Control) {
5
- onSuggestionClick({ geometry }) {
6
- const coord = fromLonLat(geometry.coordinates);
7
- this.map.getView().setCenter(coord);
8
- }
9
- }
10
- export default StopFinderControl;
@@ -1,48 +0,0 @@
1
- import fetch from "jest-fetch-mock";
2
- import View from "ol/View";
3
- import Map from "ol/Map";
4
- import StopFinderControl from "./StopFinderControl";
5
- describe("StopFinderControl", () => {
6
- let map;
7
- beforeEach(() => {
8
- const target = document.createElement("div");
9
- document.body.appendChild(target);
10
- map = new Map({
11
- target,
12
- view: new View({
13
- center: [0, 0],
14
- zoom: 0
15
- })
16
- });
17
- global.fetch = fetch;
18
- });
19
- afterEach(() => {
20
- if (map) {
21
- map.setTarget(null);
22
- map = null;
23
- }
24
- fetch.resetMocks();
25
- });
26
- test("should be activate by default", () => {
27
- const control = new StopFinderControl();
28
- expect(control.active).toBe(true);
29
- });
30
- test("launch a search and display results", (done) => {
31
- fetch.mockResponseOnce(JSON.stringify(global.stopsSearchResponse));
32
- const control = new StopFinderControl({
33
- url: "https://foo.ch",
34
- apiKey: "foo",
35
- apiParams: {
36
- limit: 10,
37
- foo: "bar"
38
- }
39
- });
40
- control.attachToMap(map);
41
- expect(control.element).toBeDefined();
42
- control.search("foo").then(() => {
43
- expect(fetch.mock.calls[0][0]).toEqual("https://foo.ch/?key=foo&limit=10&foo=bar&q=foo");
44
- expect(control.element.querySelector("div").querySelector("div").innerHTML).toBe("Bern");
45
- done();
46
- });
47
- });
48
- });
@@ -1,83 +0,0 @@
1
- import { unByKey } from "ol/Observable";
2
- import LayerCommon from "../../common/layers/Layer";
3
- import userInteractionsMixin from "../../common/mixins/UserInteractionsLayerMixin";
4
- class Layer extends userInteractionsMixin(LayerCommon) {
5
- constructor(options) {
6
- super(options);
7
- if (this.olLayer) {
8
- this.olLayer.setVisible(this.visible);
9
- }
10
- }
11
- defineProperties(options) {
12
- super.defineProperties(options);
13
- Object.defineProperties(this, {
14
- olLayer: { value: options.olLayer, writable: true },
15
- olListenersKeys: {
16
- value: []
17
- }
18
- });
19
- }
20
- attachToMap(map) {
21
- super.attachToMap(map);
22
- if (!this.map) {
23
- return;
24
- }
25
- if (this.olLayer && !this.map?.getLayers()?.getArray()?.includes(this.olLayer)) {
26
- this.map.addLayer(this.olLayer);
27
- }
28
- this.olListenersKeys.push(this.on("change:visible", () => {
29
- if (this.olLayer) {
30
- this.olLayer.setVisible(this.visible);
31
- }
32
- }));
33
- this.olListenersKeys.push(this.map.getLayers().on("remove", (evt) => {
34
- if (evt.element === this.olLayer) {
35
- this.detachFromMap();
36
- }
37
- }));
38
- this.toggleVisibleListeners();
39
- this.olListenersKeys.push(this.on("change:visible", this.toggleVisibleListeners));
40
- if (this.copyrights && this.olLayer) {
41
- const attributions = this.copyrights || [];
42
- if (this.olLayer.getLayers) {
43
- this.olLayer.getLayers().getArray().forEach((layer) => {
44
- layer.getSource()?.setAttributions(attributions);
45
- });
46
- } else if (this.olLayer.getSource) {
47
- this.olLayer.getSource()?.setAttributions(attributions);
48
- }
49
- }
50
- }
51
- detachFromMap() {
52
- this.deactivateUserInteractions();
53
- unByKey(this.olListenersKeys);
54
- if (this.olLayer && this.map?.getLayers()?.getArray()?.includes(this.olLayer)) {
55
- this.map.removeLayer(this.olLayer);
56
- }
57
- super.detachFromMap();
58
- }
59
- activateUserInteractions() {
60
- this.deactivateUserInteractions();
61
- if (this.map && this.userInteractions && this.userClickInteractions && this.userClickCallbacks.length) {
62
- this.singleClickListenerKey = this.map.on("singleclick", this.onUserClickCallback);
63
- this.olListenersKeys.push(this.singleClickListenerKey);
64
- }
65
- if (this.map && this.userInteractions && this.userHoverInteractions && this.userHoverCallbacks.length) {
66
- this.pointerMoveListenerKey = this.map.on("pointermove", this.onUserMoveCallback);
67
- }
68
- }
69
- deactivateUserInteractions() {
70
- unByKey([this.pointerMoveListenerKey, this.singleClickListenerKey]);
71
- }
72
- toggleVisibleListeners() {
73
- if (this.visible) {
74
- this.activateUserInteractions();
75
- } else {
76
- this.deactivateUserInteractions();
77
- }
78
- }
79
- clone(newOptions) {
80
- return new Layer({ ...this.options, ...newOptions });
81
- }
82
- }
83
- export default Layer;