mobility-toolbox-js 2.0.0-beta.8 → 2.0.1-beta.13

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 (304) hide show
  1. package/api/RoutingAPI.js +15 -0
  2. package/api/RoutingAPI.test.js +25 -0
  3. package/api/StopsAPI.js +12 -0
  4. package/api/StopsAPI.test.js +22 -0
  5. package/api/TralisAPI.js +359 -0
  6. package/api/TralisAPI.test.js +67 -0
  7. package/{src/api/tralis → api}/TralisAPIUtils.js +2 -32
  8. package/api/index.js +3 -0
  9. package/{index.html → api/typedefs.js} +0 -0
  10. package/common/Tracker.js +93 -0
  11. package/common/api/HttpAPI.js +30 -0
  12. package/common/api/HttpAPI.test.js +50 -0
  13. package/common/api/WebSocketAPI.js +175 -0
  14. package/{src/api/tralis/WebSocketConnector.test.js → common/api/WebSocketAPI.test.js} +100 -145
  15. package/common/controls/Control.js +81 -0
  16. package/{src/common → common}/controls/Control.test.js +32 -43
  17. package/common/index.js +4 -0
  18. package/common/layers/Layer.js +213 -0
  19. package/{src/common → common}/layers/Layer.test.js +185 -244
  20. package/common/mixins/CopyrightMixin.js +24 -0
  21. package/common/mixins/SearchMixin.js +110 -0
  22. package/common/mixins/TralisLayerMixin.js +479 -0
  23. package/common/styles/index.js +4 -0
  24. package/{src/common → common}/styles/trackerDefaultStyle.js +39 -175
  25. package/common/styles/trackerDelayStyle.js +8 -0
  26. package/{src/common → common}/styles/trackerSimpleStyle.js +4 -8
  27. package/{src/common → common}/trackerConfig.js +61 -99
  28. package/common/trackerConfig.test.js +23 -0
  29. package/{src/doc/examples/ol-mapbox-layer.md → common/typedefs.js} +0 -0
  30. package/common/utils/createTrackerFilters.js +56 -0
  31. package/common/utils/createTrackerFilters.test.js +79 -0
  32. package/{src/common → common}/utils/getMapboxMapCopyrights.js +3 -16
  33. package/common/utils/getMapboxMapCopyrights.test.js +40 -0
  34. package/{src/common → common}/utils/getMapboxStyleUrl.js +3 -13
  35. package/{src/common → common}/utils/getVehiclePosition.js +3 -33
  36. package/common/utils/index.js +5 -0
  37. package/common/utils/removeDuplicate.js +8 -0
  38. package/common/utils/removeDuplicate.test.js +19 -0
  39. package/{src/common → common}/utils/sortByDelay.js +2 -7
  40. package/common/utils/timeUtils.js +20 -0
  41. package/common/utils/timeUtils.test.js +10 -0
  42. package/index.js +8 -0
  43. package/mapbox/controls/CopyrightControl.js +29 -0
  44. package/mapbox/controls/index.js +1 -0
  45. package/mapbox/index.js +4 -0
  46. package/mapbox/layers/Layer.js +57 -0
  47. package/mapbox/layers/Layer.test.js +182 -0
  48. package/mapbox/layers/TralisLayer.js +182 -0
  49. package/{src/mapbox → mapbox}/layers/TralisLayer.test.js +12 -14
  50. package/mapbox/layers/index.js +2 -0
  51. package/{src/mapbox → mapbox}/utils.js +7 -21
  52. package/mbt.js +50444 -0
  53. package/mbt.js.map +7 -0
  54. package/mbt.min.js +1005 -0
  55. package/mbt.min.js.map +7 -0
  56. package/ol/controls/CopyrightControl.js +42 -0
  57. package/ol/controls/CopyrightControl.test.js +165 -0
  58. package/ol/controls/RoutingControl.js +387 -0
  59. package/ol/controls/RoutingControl.test.js +151 -0
  60. package/ol/controls/StopFinderControl.js +10 -0
  61. package/ol/controls/StopFinderControl.test.js +48 -0
  62. package/ol/controls/index.js +3 -0
  63. package/ol/index.js +5 -0
  64. package/ol/layers/Layer.js +88 -0
  65. package/ol/layers/Layer.test.js +174 -0
  66. package/ol/layers/MapboxLayer.js +203 -0
  67. package/{src/ol → ol}/layers/MapboxLayer.test.js +58 -84
  68. package/ol/layers/MapboxStyleLayer.js +187 -0
  69. package/{src/ol → ol}/layers/MapboxStyleLayer.test.js +97 -128
  70. package/ol/layers/MaplibreLayer.js +139 -0
  71. package/ol/layers/RoutingLayer.js +61 -0
  72. package/{src/ol → ol}/layers/RoutingLayer.test.js +15 -24
  73. package/ol/layers/TralisLayer.js +185 -0
  74. package/ol/layers/TralisLayer.test.js +79 -0
  75. package/ol/layers/VectorLayer.js +22 -0
  76. package/{src/ol → ol}/layers/VectorLayer.test.js +34 -45
  77. package/ol/layers/WMSLayer.js +38 -0
  78. package/ol/layers/WMSLayer.test.js +76 -0
  79. package/ol/layers/index.js +8 -0
  80. package/{src/ol → ol}/styles/fullTrajectoryDelayStyle.js +11 -15
  81. package/ol/styles/fullTrajectoryStyle.js +43 -0
  82. package/ol/styles/index.js +2 -0
  83. package/package.json +34 -74
  84. package/.babelrc +0 -6
  85. package/.esdoc.json +0 -17
  86. package/.eslintignore +0 -1
  87. package/.github/workflows/conventional-pr-title.yml +0 -21
  88. package/.github/workflows/cypress.yml +0 -29
  89. package/.github/workflows/nodejs.yml +0 -28
  90. package/.husky/commit-msg +0 -4
  91. package/.husky/post-checkout +0 -4
  92. package/.husky/post-merge +0 -4
  93. package/.husky/post-rebase +0 -4
  94. package/.husky/pre-commit +0 -4
  95. package/.neutrinorc.js +0 -176
  96. package/.nvmrc +0 -1
  97. package/CHANGELOG.md +0 -10
  98. package/LICENSE +0 -21
  99. package/__mocks__/mapbox-gl.js +0 -81
  100. package/__mocks__/maplibre-gl.js +0 -81
  101. package/commitlint.config.js +0 -1
  102. package/cypress/fixtures/example.json +0 -5
  103. package/cypress/integration/examples/api.spec.js +0 -7
  104. package/cypress/integration/examples/examples.spec.js +0 -7
  105. package/cypress/integration/examples/navigation.spec.js +0 -29
  106. package/cypress/plugins/index.js +0 -21
  107. package/cypress/support/commands.js +0 -25
  108. package/cypress/support/index.js +0 -20
  109. package/cypress.json +0 -4
  110. package/data/fetchRoute.json +0 -292
  111. package/data/fetchTrajectories.json +0 -18
  112. package/data/fetchTrajectoryById.json +0 -3
  113. package/data/fetchTrajectoryStations.json +0 -18
  114. package/data/stopsSearch.json +0 -15
  115. package/documentation.yml +0 -4
  116. package/esdoc/README.md +0 -27
  117. package/esdoc/plugins/MyPlugin.js +0 -69
  118. package/esdoc/plugins/dynamic-property-plugin/Plugin.js +0 -50
  119. package/esdoc/plugins/externals-plugin/Plugin.js +0 -45
  120. package/esdoc/plugins/externals-plugin/externals.js +0 -96
  121. package/global-setup.js +0 -3
  122. package/indexweb.html +0 -49
  123. package/jest.config.js +0 -5
  124. package/pull_request_template.md +0 -17
  125. package/renovate.json +0 -4
  126. package/scripts/read-pkg-json.js +0 -22
  127. package/src/api/index.js +0 -3
  128. package/src/api/routing/RoutingAPI.js +0 -44
  129. package/src/api/routing/RoutingAPI.test.js +0 -41
  130. package/src/api/stops/StopsAPI.js +0 -41
  131. package/src/api/stops/StopsAPI.test.js +0 -34
  132. package/src/api/tralis/TralisAPI.js +0 -731
  133. package/src/api/tralis/TralisAPI.test.js +0 -75
  134. package/src/api/tralis/WebSocketConnector.js +0 -338
  135. package/src/api/tralis/typedefs.js +0 -81
  136. package/src/assets/Lato-Black.ttf +0 -0
  137. package/src/assets/Lato-BlackItalic.ttf +0 -0
  138. package/src/assets/Lato-Bold.ttf +0 -0
  139. package/src/assets/Lato-BoldItalic.ttf +0 -0
  140. package/src/assets/Lato-Italic.ttf +0 -0
  141. package/src/assets/Lato-Light.ttf +0 -0
  142. package/src/assets/Lato-LightItalic.ttf +0 -0
  143. package/src/assets/Lato-Regular.ttf +0 -0
  144. package/src/assets/Lato-Thin.ttf +0 -0
  145. package/src/assets/Lato-ThinItalic.ttf +0 -0
  146. package/src/assets/OFL.txt +0 -93
  147. package/src/common/Tracker.js +0 -197
  148. package/src/common/api/api.js +0 -64
  149. package/src/common/api/api.test.js +0 -68
  150. package/src/common/controls/Control.js +0 -146
  151. package/src/common/index.js +0 -5
  152. package/src/common/layers/Layer.js +0 -404
  153. package/src/common/mixins/CopyrightMixin.js +0 -48
  154. package/src/common/mixins/SearchMixin.js +0 -176
  155. package/src/common/mixins/TralisLayerMixin.js +0 -930
  156. package/src/common/styles/index.js +0 -4
  157. package/src/common/styles/trackerDelayStyle.js +0 -17
  158. package/src/common/trackerConfig.test.js +0 -25
  159. package/src/common/typedefs.js +0 -23
  160. package/src/common/utils/createTrackerFilters.js +0 -87
  161. package/src/common/utils/createTrackerFilters.test.js +0 -95
  162. package/src/common/utils/getMapboxMapCopyrights.test.js +0 -47
  163. package/src/common/utils/index.js +0 -5
  164. package/src/common/utils/removeDuplicate.js +0 -22
  165. package/src/common/utils/removeDuplicate.test.js +0 -22
  166. package/src/common/utils/timeUtils.js +0 -44
  167. package/src/common/utils/timeUtils.test.js +0 -16
  168. package/src/doc/App.js +0 -116
  169. package/src/doc/App.scss +0 -51
  170. package/src/doc/_redirects +0 -2
  171. package/src/doc/components/CodeSandboxButton.js +0 -103
  172. package/src/doc/components/Documentation.js +0 -40
  173. package/src/doc/components/Esdoc/Anchor.js +0 -57
  174. package/src/doc/components/Esdoc/ClassDoc.js +0 -272
  175. package/src/doc/components/Esdoc/DeprecatedHTML.js +0 -16
  176. package/src/doc/components/Esdoc/DetailDocs.js +0 -281
  177. package/src/doc/components/Esdoc/DetailHTML.js +0 -33
  178. package/src/doc/components/Esdoc/DirectSubclassHTML.js +0 -30
  179. package/src/doc/components/Esdoc/DocBuilderUtils.js +0 -694
  180. package/src/doc/components/Esdoc/DocLinkHTML.js +0 -62
  181. package/src/doc/components/Esdoc/DocsLinkHTML.js +0 -38
  182. package/src/doc/components/Esdoc/Esdoc.js +0 -63
  183. package/src/doc/components/Esdoc/EsdocContent.js +0 -51
  184. package/src/doc/components/Esdoc/EsdocNavigation.js +0 -13
  185. package/src/doc/components/Esdoc/EsdocSearch.js +0 -81
  186. package/src/doc/components/Esdoc/ExperimentalHTML.js +0 -17
  187. package/src/doc/components/Esdoc/ExtendsChainHTML.js +0 -32
  188. package/src/doc/components/Esdoc/FileDocLinkHTML.js +0 -60
  189. package/src/doc/components/Esdoc/IdentifiersDoc.js +0 -113
  190. package/src/doc/components/Esdoc/IndirectSubclassHTML.js +0 -30
  191. package/src/doc/components/Esdoc/InheritedSummaryDoc.js +0 -70
  192. package/src/doc/components/Esdoc/InheritedSummaryHTML.js +0 -38
  193. package/src/doc/components/Esdoc/MixinClassesHTML.js +0 -29
  194. package/src/doc/components/Esdoc/NavDoc.js +0 -112
  195. package/src/doc/components/Esdoc/OverrideMethod.js +0 -44
  196. package/src/doc/components/Esdoc/OverrideMethodDescription.js +0 -35
  197. package/src/doc/components/Esdoc/Properties.js +0 -89
  198. package/src/doc/components/Esdoc/README.md +0 -45
  199. package/src/doc/components/Esdoc/SignatureHTML.js +0 -123
  200. package/src/doc/components/Esdoc/SingleDoc.js +0 -31
  201. package/src/doc/components/Esdoc/SummaryDoc.js +0 -160
  202. package/src/doc/components/Esdoc/SummaryHTML.js +0 -96
  203. package/src/doc/components/Esdoc/TypeDocLinkHTML.js +0 -249
  204. package/src/doc/components/Esdoc/css/identifiers.css +0 -38
  205. package/src/doc/components/Esdoc/css/search.css +0 -76
  206. package/src/doc/components/Esdoc/css/style.css +0 -603
  207. package/src/doc/components/Esdoc/index.js +0 -7
  208. package/src/doc/components/Example.js +0 -153
  209. package/src/doc/components/Examples.js +0 -183
  210. package/src/doc/components/Home.js +0 -106
  211. package/src/doc/components/TrackerExample.js +0 -38
  212. package/src/doc/examples/assets/tralis-live-map/index.js +0 -11
  213. package/src/doc/examples/assets/tralis-live-map/s1kreis.svg +0 -105
  214. package/src/doc/examples/assets/tralis-live-map/s20kreis.svg +0 -101
  215. package/src/doc/examples/assets/tralis-live-map/s2kreis.svg +0 -95
  216. package/src/doc/examples/assets/tralis-live-map/s3kreis.svg +0 -95
  217. package/src/doc/examples/assets/tralis-live-map/s4kreis.svg +0 -95
  218. package/src/doc/examples/assets/tralis-live-map/s6kreis.svg +0 -95
  219. package/src/doc/examples/assets/tralis-live-map/s7kreis.svg +0 -95
  220. package/src/doc/examples/assets/tralis-live-map/s8kreis.svg +0 -93
  221. package/src/doc/examples/assets/tralis-live-map/unknown.svg +0 -107
  222. package/src/doc/examples/mb-copyright.html +0 -26
  223. package/src/doc/examples/mb-copyright.js +0 -37
  224. package/src/doc/examples/mb-tracker.html +0 -1
  225. package/src/doc/examples/mb-tracker.js +0 -39
  226. package/src/doc/examples/mb-tracker.md +0 -1
  227. package/src/doc/examples/mb-tralis.html +0 -1
  228. package/src/doc/examples/mb-tralis.js +0 -34
  229. package/src/doc/examples/ol-copyright.html +0 -26
  230. package/src/doc/examples/ol-copyright.js +0 -43
  231. package/src/doc/examples/ol-mapbox-layer.html +0 -1
  232. package/src/doc/examples/ol-mapbox-layer.js +0 -28
  233. package/src/doc/examples/ol-mapbox-style-layer.html +0 -12
  234. package/src/doc/examples/ol-mapbox-style-layer.js +0 -44
  235. package/src/doc/examples/ol-query.html +0 -32
  236. package/src/doc/examples/ol-query.js +0 -83
  237. package/src/doc/examples/ol-routing.html +0 -26
  238. package/src/doc/examples/ol-routing.js +0 -59
  239. package/src/doc/examples/ol-routing.md +0 -1
  240. package/src/doc/examples/ol-stop-finder.html +0 -15
  241. package/src/doc/examples/ol-stop-finder.js +0 -31
  242. package/src/doc/examples/ol-stop-finder.md +0 -1
  243. package/src/doc/examples/ol-tracker.html +0 -1
  244. package/src/doc/examples/ol-tracker.js +0 -44
  245. package/src/doc/examples/ol-tracker.md +0 -1
  246. package/src/doc/examples/ol-tralis.html +0 -5
  247. package/src/doc/examples/ol-tralis.js +0 -57
  248. package/src/doc/examples/tralis-live-map.html +0 -1
  249. package/src/doc/examples/tralis-live-map.js +0 -51
  250. package/src/doc/examples/tralis-live-map.md +0 -3
  251. package/src/doc/examples.js +0 -107
  252. package/src/doc/img/examples/live_tracker_mb.jpg +0 -0
  253. package/src/doc/img/examples/live_tracker_munich.jpg +0 -0
  254. package/src/doc/img/examples/live_tracker_ol.jpg +0 -0
  255. package/src/doc/img/examples/mapbox.jpg +0 -0
  256. package/src/doc/img/examples/mapbox_style.jpg +0 -0
  257. package/src/doc/img/examples/ol-copyright.png +0 -0
  258. package/src/doc/img/examples/query_objects.jpg +0 -0
  259. package/src/doc/img/examples/routing.jpg +0 -0
  260. package/src/doc/img/examples/simple_map.jpg +0 -0
  261. package/src/doc/img/examples/stops.jpg +0 -0
  262. package/src/doc/img/favico.ico +0 -0
  263. package/src/doc/index.js +0 -21
  264. package/src/iife.js +0 -7
  265. package/src/index.js +0 -10
  266. package/src/mapbox/controls/CopyrightControl.js +0 -58
  267. package/src/mapbox/controls/index.js +0 -2
  268. package/src/mapbox/index.js +0 -4
  269. package/src/mapbox/layers/Layer.js +0 -118
  270. package/src/mapbox/layers/Layer.test.js +0 -202
  271. package/src/mapbox/layers/TralisLayer.js +0 -329
  272. package/src/mapbox/layers/index.js +0 -2
  273. package/src/ol/README.md +0 -0
  274. package/src/ol/controls/CopyrightControl.js +0 -80
  275. package/src/ol/controls/CopyrightControl.test.js +0 -211
  276. package/src/ol/controls/RoutingControl.js +0 -752
  277. package/src/ol/controls/RoutingControl.test.js +0 -216
  278. package/src/ol/controls/StopFinderControl.js +0 -38
  279. package/src/ol/controls/StopFinderControl.test.js +0 -59
  280. package/src/ol/controls/index.js +0 -3
  281. package/src/ol/controls/snapshots/RoutingControlRouteGen10.json +0 -58
  282. package/src/ol/controls/snapshots/RoutingControlRouteGen100.json +0 -292
  283. package/src/ol/controls/snapshots/RoutingControlRouteGen30.json +0 -69
  284. package/src/ol/controls/snapshots/RoutingControlRouteGen5.json +0 -58
  285. package/src/ol/controls/snapshots/RoutingControlRouteOSM.json +0 -759
  286. package/src/ol/controls/snapshots/RoutingControlStation1.json +0 -60
  287. package/src/ol/controls/snapshots/RoutingControlStation2.json +0 -49
  288. package/src/ol/index.js +0 -5
  289. package/src/ol/layers/Layer.js +0 -193
  290. package/src/ol/layers/Layer.test.js +0 -197
  291. package/src/ol/layers/MapboxLayer.js +0 -378
  292. package/src/ol/layers/MapboxStyleLayer.js +0 -417
  293. package/src/ol/layers/MaplibreLayer.js +0 -280
  294. package/src/ol/layers/RoutingLayer.js +0 -91
  295. package/src/ol/layers/TralisLayer.js +0 -359
  296. package/src/ol/layers/TralisLayer.test.js +0 -97
  297. package/src/ol/layers/VectorLayer.js +0 -43
  298. package/src/ol/layers/WMSLayer.js +0 -80
  299. package/src/ol/layers/WMSLayer.test.js +0 -84
  300. package/src/ol/layers/index.js +0 -8
  301. package/src/ol/styles/fullTrajectoryStyle.js +0 -51
  302. package/src/ol/styles/index.js +0 -2
  303. package/src/setupTests.js +0 -15
  304. package/webpack.config.js +0 -6
@@ -0,0 +1,42 @@
1
+ import { inView } from "ol/layer/Layer";
2
+ import Control from "../../common/controls/Control";
3
+ import mixin from "../../common/mixins/CopyrightMixin";
4
+ import removeDuplicate from "../../common/utils/removeDuplicate";
5
+ class CopyrightControl extends mixin(Control) {
6
+ constructor(options) {
7
+ super(options);
8
+ this.onPostRender = this.onPostRender.bind(this);
9
+ }
10
+ getCopyrights() {
11
+ if (!this.frameState) {
12
+ return [];
13
+ }
14
+ let copyrights = [];
15
+ this.frameState.layerStatesArray.forEach((layerState) => {
16
+ const { layer } = layerState;
17
+ if (inView(layerState, this.frameState.viewState) && layer && layer.getSource && layer.getSource() && layer.getSource().getAttributions()) {
18
+ copyrights = copyrights.concat(layer.getSource().getAttributions()(this.frameState));
19
+ }
20
+ });
21
+ return removeDuplicate(copyrights);
22
+ }
23
+ activate() {
24
+ super.activate();
25
+ if (this.map) {
26
+ this.map.on("postrender", this.onPostRender);
27
+ }
28
+ }
29
+ deactivate() {
30
+ if (this.map) {
31
+ this.map.un("postrender", this.onPostRender);
32
+ }
33
+ super.deactivate();
34
+ }
35
+ onPostRender(evt) {
36
+ if (this.map && this.element) {
37
+ this.frameState = evt.frameState;
38
+ this.render();
39
+ }
40
+ }
41
+ }
42
+ export default CopyrightControl;
@@ -0,0 +1,165 @@
1
+ import View from "ol/View";
2
+ import Tile from "ol/Tile";
3
+ import TileLayer from "ol/layer/Tile";
4
+ import TileSource from "ol/source/Tile";
5
+ import { createXYZ } from "ol/tilegrid";
6
+ import { Map } from "ol";
7
+ import Layer from "../layers/Layer";
8
+ import CopyrightControl from "./CopyrightControl";
9
+ const tileLoadFunction = () => {
10
+ const tile = new Tile([0, 0, -1], 2);
11
+ tile.getImage = () => {
12
+ const image = new Image();
13
+ image.width = 256;
14
+ image.height = 256;
15
+ return image;
16
+ };
17
+ return tile;
18
+ };
19
+ const getOLTileLayer = () => {
20
+ const layer = new TileLayer({
21
+ source: new TileSource({
22
+ projection: "EPSG:3857",
23
+ tileGrid: createXYZ()
24
+ })
25
+ });
26
+ layer.getSource().getTile = tileLoadFunction;
27
+ return layer;
28
+ };
29
+ const getLayer = (copyrights, visible = true) => new Layer({
30
+ visible,
31
+ copyrights,
32
+ olLayer: getOLTileLayer()
33
+ });
34
+ describe("CopyrightControl", () => {
35
+ let map;
36
+ beforeEach(() => {
37
+ const target = document.createElement("div");
38
+ document.body.appendChild(target);
39
+ map = new Map({
40
+ target,
41
+ view: new View({
42
+ center: [0, 0],
43
+ zoom: 0
44
+ })
45
+ });
46
+ getLayer(true, "bar").attachToMap(map);
47
+ map.setSize([200, 200]);
48
+ map.renderSync();
49
+ });
50
+ afterEach(() => {
51
+ if (map) {
52
+ map.setTarget(null);
53
+ map = null;
54
+ }
55
+ });
56
+ test("should be activate by default", () => {
57
+ const control = new CopyrightControl();
58
+ expect(control.active).toBe(true);
59
+ });
60
+ test("renders a string copyright", () => {
61
+ const control = new CopyrightControl();
62
+ control.attachToMap(map);
63
+ expect(control.element.innerHTML).toBe("");
64
+ getLayer("copyright").attachToMap(map);
65
+ map.renderSync();
66
+ expect(control.element.innerHTML).toBe("copyright");
67
+ });
68
+ test("renders an array of copyrights", () => {
69
+ const control = new CopyrightControl();
70
+ control.attachToMap(map);
71
+ expect(control.element.innerHTML).toBe("");
72
+ getLayer(["copyright 1", "copyright 2"]).attachToMap(map);
73
+ map.renderSync();
74
+ expect(control.element.innerHTML).toBe("copyright 1 | copyright 2");
75
+ });
76
+ test("renders unique copyrights", () => {
77
+ const control = new CopyrightControl();
78
+ control.attachToMap(map);
79
+ expect(control.element.innerHTML).toBe("");
80
+ getLayer(["copyright 1", "copyright 2"]).attachToMap(map);
81
+ getLayer("copyright 1").attachToMap(map);
82
+ getLayer(["copyright 2"]).attachToMap(map);
83
+ map.renderSync();
84
+ expect(control.element.innerHTML).toBe("copyright 1 | copyright 2");
85
+ });
86
+ test("doesn't render copyright of an hidden layer", () => {
87
+ const control = new CopyrightControl();
88
+ control.attachToMap(map);
89
+ expect(control.element.innerHTML).toBe("");
90
+ const layer1 = getLayer("copyright hidden", false);
91
+ layer1.attachToMap(map);
92
+ const layer2 = getLayer("copyright", true);
93
+ layer2.attachToMap(map);
94
+ map.renderSync();
95
+ expect(control.element.innerHTML).toBe("copyright");
96
+ layer1.setVisible(true);
97
+ map.renderSync();
98
+ layer2.setVisible(false);
99
+ map.renderSync();
100
+ expect(control.element.innerHTML).toBe("copyright hidden");
101
+ });
102
+ test("should activate the control on construction then deactivate it", () => {
103
+ getLayer("copyright 1").attachToMap(map);
104
+ const control = new CopyrightControl({ active: true });
105
+ control.attachToMap(map);
106
+ map.renderSync();
107
+ expect(control.element.parentNode).toBe(map.getTargetElement());
108
+ expect(control.active).toBe(true);
109
+ expect(control.element.innerHTML).toBe("copyright 1");
110
+ control.active = false;
111
+ map.renderSync();
112
+ expect(control.element.innerHTML).toBe("");
113
+ });
114
+ test("should deactivate the control on constrcution then activate it", () => {
115
+ getLayer("copyright 1").attachToMap(map);
116
+ const control = new CopyrightControl({ active: false });
117
+ control.attachToMap(map);
118
+ map.renderSync();
119
+ expect(control.element.parentNode).toBe(map.getTargetElement());
120
+ expect(control.active).toBe(false);
121
+ map.renderSync();
122
+ expect(control.element.innerHTML).toBe("");
123
+ control.active = true;
124
+ map.renderSync();
125
+ expect(control.element.innerHTML).toBe("copyright 1");
126
+ });
127
+ test("should add copyrights in the map container element then remove it.", () => {
128
+ getLayer("copyright value").attachToMap(map);
129
+ map.renderSync();
130
+ const control = new CopyrightControl();
131
+ control.attachToMap(map);
132
+ expect(control.element.parentNode).toBe(map.getTargetElement());
133
+ control.detachFromMap();
134
+ expect(control.element.parentNode).toBe(null);
135
+ });
136
+ test("should add copyrights in the target element then remove it.", () => {
137
+ getLayer(["copyright value"]).attachToMap(map);
138
+ map.renderSync();
139
+ const target = document.createElement("div");
140
+ target.setAttribute("id", "copyright");
141
+ document.body.appendChild(target);
142
+ const control = new CopyrightControl({
143
+ target: document.getElementById("copyright")
144
+ });
145
+ control.attachToMap(map);
146
+ expect(control.element.parentNode).toBe(target);
147
+ control.detachFromMap();
148
+ expect(control.element.parentNode).toBe(null);
149
+ });
150
+ test("renders custom copyrights with the render method ", () => {
151
+ const control = new CopyrightControl({
152
+ active: true,
153
+ render() {
154
+ if (!this.element) {
155
+ return;
156
+ }
157
+ this.element.innerHTML = this.active ? this.getCopyrights().join(", ") : "";
158
+ }
159
+ });
160
+ control.attachToMap(map);
161
+ getLayer(["copyright 1", "copyright 2", "copyright 3"]).attachToMap(map);
162
+ map.renderSync();
163
+ expect(control.element.innerHTML).toBe("copyright 1, copyright 2, copyright 3");
164
+ });
165
+ });
@@ -0,0 +1,387 @@
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 { RoutingAPI } from "../../api";
10
+ import Control from "../../common/controls/Control";
11
+ import RoutingLayer from "../layers/RoutingLayer";
12
+ const REGEX_VIA_POINT = /^([^@$!\n]*)(@?([\d.]+),([\d.]+))?(\$?([a-zA-Z0-9]{0,2}))$/;
13
+ const REGEX_VIA_POINT_COORD = /^([\d.]+),([\d.]+)$/;
14
+ const REGEX_VIA_POINT_STATION_ID = /^!([^$]*)(\$?([a-zA-Z0-9]{0,2}))$/;
15
+ const STOP_FETCH_ABORT_CONTROLLER_KEY = "stop-fetch";
16
+ const getFlatCoordinatesFromSegments = (segmentArray) => {
17
+ const coords = [];
18
+ segmentArray.forEach((seg) => {
19
+ coords.push(...seg.getGeometry().getCoordinates());
20
+ });
21
+ return coords;
22
+ };
23
+ class RoutingControl extends Control {
24
+ constructor(options = {}) {
25
+ super(options);
26
+ Object.defineProperties(this, {
27
+ mot: {
28
+ get: () => this.get("mot"),
29
+ set: (newMot) => {
30
+ if (newMot) {
31
+ this.set("mot", newMot);
32
+ if (this.viaPoints) {
33
+ this.drawRoute();
34
+ }
35
+ }
36
+ }
37
+ },
38
+ loading: {
39
+ get: () => this.get("loading"),
40
+ set: (newLoading) => {
41
+ this.set("loading", newLoading);
42
+ }
43
+ },
44
+ modify: {
45
+ get: () => this.get("modify"),
46
+ set: (modify) => {
47
+ this.set("modify", modify);
48
+ }
49
+ }
50
+ });
51
+ this.loading = false;
52
+ this.graphs = options.graphs || [["osm", 0, 99]];
53
+ this.mot = options.mot || "bus";
54
+ this.modify = options.modify !== false;
55
+ this.routingApiParams = options.routingApiParams || {};
56
+ this.useRawViaPoints = options.useRawViaPoints || false;
57
+ this.snapToClosestStation = options.snapToClosestStation || false;
58
+ this.cacheStationData = {};
59
+ this.abortControllers = [];
60
+ this.apiKey = options.apiKey;
61
+ this.stopsApiKey = options.stopsApiKey || this.apiKey;
62
+ this.segments = [];
63
+ this.stopsApiUrl = options.stopsApiUrl || "https://api.geops.io/stops/v1/";
64
+ this.api = new RoutingAPI({
65
+ ...options
66
+ });
67
+ this.routingLayer = options.routingLayer || new RoutingLayer({
68
+ name: "routing-layer",
69
+ style: options.style
70
+ });
71
+ this.onRouteError = options.onRouteError || ((error) => {
72
+ this.dispatchEvent({
73
+ type: "change:route",
74
+ target: this
75
+ });
76
+ this.reset();
77
+ console.error(error);
78
+ });
79
+ this.viaPoints = [];
80
+ this.onMapClick = this.onMapClick.bind(this);
81
+ this.onModifyEnd = this.onModifyEnd.bind(this);
82
+ this.onModifyStart = this.onModifyStart.bind(this);
83
+ this.apiChangeListener = () => this.drawRoute();
84
+ this.createModifyInteraction();
85
+ }
86
+ static getGraphsResolutions(graphs, map) {
87
+ const view = map.getView();
88
+ return graphs.map(([, minZoom, maxZoom]) => [
89
+ view.getResolutionForZoom(minZoom),
90
+ view.getResolutionForZoom(maxZoom || minZoom + 1)
91
+ ]);
92
+ }
93
+ addViaPoint(coordinatesOrString, index = this.viaPoints.length, overwrite = 0) {
94
+ this.viaPoints.splice(index, overwrite, coordinatesOrString);
95
+ this.drawRoute();
96
+ this.dispatchEvent({
97
+ type: "change:route",
98
+ target: this
99
+ });
100
+ }
101
+ removeViaPoint(index = this.viaPoints.length - 1) {
102
+ if (this.viaPoints.length && this.viaPoints[index]) {
103
+ this.viaPoints.splice(index, 1);
104
+ }
105
+ this.drawRoute();
106
+ this.dispatchEvent({
107
+ type: "change:route",
108
+ target: this
109
+ });
110
+ }
111
+ setViaPoints(coordinateArray) {
112
+ this.viaPoints = [...coordinateArray];
113
+ this.drawRoute();
114
+ this.dispatchEvent({
115
+ type: "change:route",
116
+ target: this
117
+ });
118
+ }
119
+ reset() {
120
+ this.abortRequests();
121
+ this.viaPoints = [];
122
+ this.routingLayer.olLayer.getSource().clear();
123
+ this.dispatchEvent({
124
+ type: "change:route",
125
+ target: this
126
+ });
127
+ }
128
+ abortRequests() {
129
+ this.graphs.forEach(([graph]) => {
130
+ if (this.abortControllers[graph]) {
131
+ this.abortControllers[graph].abort();
132
+ }
133
+ this.abortControllers[graph] = new AbortController();
134
+ });
135
+ this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY]?.abort();
136
+ this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY] = new AbortController();
137
+ this.loading = false;
138
+ }
139
+ drawRoute() {
140
+ this.abortRequests();
141
+ this.routingLayer.olLayer.getSource().clear();
142
+ if (!this.viaPoints.length) {
143
+ return null;
144
+ }
145
+ if (this.viaPoints.length === 1) {
146
+ return this.drawViaPoint(this.viaPoints[0], 0, this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY]);
147
+ }
148
+ const formattedViaPoints = this.viaPoints.map((viaPoint) => {
149
+ if (Array.isArray(viaPoint)) {
150
+ const projection = this.map.getView().getProjection();
151
+ const [lon, lat] = toLonLat(viaPoint, projection);
152
+ return this.snapToClosestStation ? [`@${lat}`, lon] : [lat, lon];
153
+ }
154
+ return this.useRawViaPoints ? viaPoint : `!${viaPoint}`;
155
+ });
156
+ this.loading = true;
157
+ this.routingLayer.olLayer.getSource().clear();
158
+ this.viaPoints.forEach((viaPoint, idx) => this.drawViaPoint(viaPoint, idx, this.abortControllers[STOP_FETCH_ABORT_CONTROLLER_KEY]));
159
+ return Promise.all(this.graphs.map(([graph], index) => {
160
+ return this.api.route({
161
+ graph,
162
+ via: `${formattedViaPoints.join("|")}`,
163
+ mot: `${this.mot}`,
164
+ "resolve-hops": false,
165
+ elevation: false,
166
+ "coord-radius": 100,
167
+ "coord-punish": 1e3,
168
+ ...this.routingApiParams
169
+ }, this.abortControllers[graph]).then((featureCollection) => {
170
+ this.segments = this.format.readFeatures(featureCollection);
171
+ if (this.mot === "foot") {
172
+ const uniqueVias = this.segments.reduce((resultVias, currentFeat) => {
173
+ const segTrg = currentFeat.get("trg");
174
+ return resultVias.find((via) => via[0] === segTrg[0] && via[1] === segTrg[1]) ? resultVias : [...resultVias, segTrg];
175
+ }, []);
176
+ this.segments = uniqueVias.map((via) => {
177
+ const viaSegments = this.segments.filter((seg) => {
178
+ const segTrg = seg.get("trg");
179
+ return segTrg[0] === via[0] && segTrg[1] === via[1];
180
+ });
181
+ const coords2 = getFlatCoordinatesFromSegments(viaSegments);
182
+ return new Feature({
183
+ geometry: new LineString(coords2)
184
+ });
185
+ });
186
+ }
187
+ const coords = getFlatCoordinatesFromSegments(this.segments);
188
+ const routeFeature = new Feature({
189
+ geometry: new LineString(coords)
190
+ });
191
+ routeFeature.set("graph", graph);
192
+ routeFeature.set("mot", this.mot);
193
+ routeFeature.set("minResolution", this.graphsResolutions[index][0]);
194
+ routeFeature.set("maxResolution", this.graphsResolutions[index][1]);
195
+ this.routingLayer.olLayer.getSource().addFeature(routeFeature);
196
+ this.loading = false;
197
+ }).catch((error) => {
198
+ if (error.name === "AbortError") {
199
+ return;
200
+ }
201
+ this.segments = [];
202
+ this.dispatchEvent({
203
+ type: "error",
204
+ target: this
205
+ });
206
+ this.onRouteError(error, this);
207
+ this.loading = false;
208
+ });
209
+ }));
210
+ }
211
+ drawViaPoint(viaPoint, idx, abortController) {
212
+ const pointFeature = new Feature();
213
+ pointFeature.set("viaPointIdx", idx);
214
+ if (Array.isArray(viaPoint)) {
215
+ pointFeature.setGeometry(new Point(viaPoint));
216
+ this.routingLayer.olLayer.getSource().addFeature(pointFeature);
217
+ return Promise.resolve(pointFeature);
218
+ }
219
+ if (!this.useRawViaPoints || REGEX_VIA_POINT_STATION_ID.test(viaPoint)) {
220
+ let stationId;
221
+ let track2;
222
+ if (this.useRawViaPoints) {
223
+ [, stationId, , track2] = REGEX_VIA_POINT_STATION_ID.exec(viaPoint);
224
+ } else {
225
+ [stationId, track2] = viaPoint.split("$");
226
+ }
227
+ return fetch(`${this.stopsApiUrl}lookup/${stationId}?key=${this.stopsApiKey}`, { signal: abortController.signal }).then((res) => res.json()).then((stationData) => {
228
+ const { coordinates } = stationData.features[0].geometry;
229
+ this.cacheStationData[viaPoint] = fromLonLat(coordinates);
230
+ pointFeature.set("viaPointTrack", track2);
231
+ pointFeature.setGeometry(new Point(fromLonLat(coordinates)));
232
+ this.routingLayer.olLayer.getSource().addFeature(pointFeature);
233
+ return pointFeature;
234
+ }).catch((error) => {
235
+ if (error.name === "AbortError") {
236
+ return;
237
+ }
238
+ this.dispatchEvent({
239
+ type: "error",
240
+ target: this
241
+ });
242
+ this.onRouteError(error, this);
243
+ this.loading = false;
244
+ });
245
+ }
246
+ if (this.useRawViaPoints && REGEX_VIA_POINT_COORD.test(viaPoint)) {
247
+ const [lat2, lon2] = REGEX_VIA_POINT_COORD.exec(viaPoint);
248
+ const coordinates = fromLonLat([parseFloat(lon2), parseFloat(lat2)], this.map.getView().getProjection());
249
+ pointFeature.setGeometry(new Point(coordinates));
250
+ this.routingLayer.olLayer.getSource().addFeature(pointFeature);
251
+ return Promise.resolve(pointFeature);
252
+ }
253
+ const [, stationName, , lat, lon, , track] = REGEX_VIA_POINT.exec(viaPoint);
254
+ if (lon && lat) {
255
+ const coordinates = fromLonLat([parseFloat(lon), parseFloat(lat)], this.map.getView().getProjection());
256
+ pointFeature.set("viaPointTrack", track);
257
+ pointFeature.setGeometry(new Point(coordinates));
258
+ this.routingLayer.olLayer.getSource().addFeature(pointFeature);
259
+ return Promise.resolve(pointFeature);
260
+ }
261
+ if (stationName) {
262
+ return fetch(`${this.stopsApiUrl}?key=${this.stopsApiKey}&q=${stationName}&limit=1`, { signal: abortController.signal }).then((res) => res.json()).then((stationData) => {
263
+ const { coordinates } = stationData.features[0].geometry;
264
+ this.cacheStationData[viaPoint] = fromLonLat(coordinates);
265
+ pointFeature.set("viaPointTrack", track);
266
+ pointFeature.setGeometry(new Point(fromLonLat(coordinates)));
267
+ this.routingLayer.olLayer.getSource().addFeature(pointFeature);
268
+ return pointFeature;
269
+ }).catch((error) => {
270
+ this.dispatchEvent({
271
+ type: "error",
272
+ target: this
273
+ });
274
+ this.onRouteError(error, this);
275
+ this.loading = false;
276
+ return null;
277
+ });
278
+ }
279
+ return Promise.resolve(null);
280
+ }
281
+ onMapClick(e) {
282
+ const feats = e.target.getFeaturesAtPixel(e.pixel);
283
+ const viaPoint = feats.find((feat) => feat.getGeometry() instanceof Point && feat.get("viaPointIdx") !== void 0);
284
+ if (viaPoint) {
285
+ this.removeViaPoint(viaPoint.get("viaPointIdx"));
286
+ return;
287
+ }
288
+ this.addViaPoint(e.coordinate);
289
+ }
290
+ onModifyStart(evt) {
291
+ let segmentIndex = -1;
292
+ const route = evt.features.getArray().find((feat) => feat.getGeometry() instanceof LineString);
293
+ if (route) {
294
+ const closestExtent = buffer(new Point(route.getGeometry().getClosestPoint(evt.mapBrowserEvent.coordinate)).getExtent(), 1e-3);
295
+ segmentIndex = this.segments.findIndex((segment) => segment.getGeometry().intersectsExtent(closestExtent));
296
+ }
297
+ const viaPoint = (evt.features.getArray().filter((feat) => feat.getGeometry() instanceof Point) || [])[0];
298
+ this.initialRouteDrag = {
299
+ viaPoint,
300
+ oldRoute: route && route.clone(),
301
+ segmentIndex
302
+ };
303
+ }
304
+ onModifyEnd(evt) {
305
+ const coord = evt.mapBrowserEvent.coordinate;
306
+ const { oldRoute, viaPoint, segmentIndex } = this.initialRouteDrag;
307
+ if (viaPoint) {
308
+ return this.addViaPoint(coord, viaPoint.get("viaPointIdx"), 1);
309
+ }
310
+ if (!oldRoute) {
311
+ return this.addViaPoint(coord, 0, 1);
312
+ }
313
+ if (segmentIndex === -1) {
314
+ return Promise.reject(new Error("No segment found"));
315
+ }
316
+ return this.addViaPoint(coord, segmentIndex + 1);
317
+ }
318
+ createDefaultElement() {
319
+ this.element = document.createElement("button");
320
+ this.element.id = "ol-toggle-routing";
321
+ this.element.innerHTML = "Toggle Route Control";
322
+ this.element.onclick = () => this.active ? this.deactivate() : this.activate();
323
+ Object.assign(this.element.style, {
324
+ position: "absolute",
325
+ right: "10px",
326
+ top: "10px"
327
+ });
328
+ }
329
+ createModifyInteraction() {
330
+ this.modifyInteraction = new Modify({
331
+ source: this.routingLayer.olLayer.getSource(),
332
+ pixelTolerance: 4,
333
+ hitDetection: this.routingLayer.olLayer,
334
+ deleteCondition: (e) => {
335
+ const feats = e.target.getFeaturesAtPixel(e.pixel, {
336
+ hitTolerance: 5
337
+ });
338
+ const viaPoint = feats.find((feat) => feat.getGeometry() instanceof Point && feat.get("index"));
339
+ if (click(e) && viaPoint) {
340
+ this.removeViaPoint(viaPoint.get("index"));
341
+ return true;
342
+ }
343
+ return false;
344
+ }
345
+ });
346
+ this.modifyInteraction.on("modifystart", this.onModifyStart);
347
+ this.modifyInteraction.on("modifyend", this.onModifyEnd);
348
+ this.modifyInteraction.setActive(false);
349
+ }
350
+ addListeners() {
351
+ if (!this.modify) {
352
+ return;
353
+ }
354
+ this.removeListeners();
355
+ this.onMapClickKey = this.map.on("singleclick", this.onMapClick);
356
+ }
357
+ removeListeners() {
358
+ unByKey(this.onMapClickKey);
359
+ }
360
+ activate() {
361
+ super.activate();
362
+ if (this.map) {
363
+ this.format = new GeoJSON({
364
+ featureProjection: this.map.getView().getProjection()
365
+ });
366
+ this.graphsResolutions = RoutingControl.getGraphsResolutions(this.graphs, this.map);
367
+ this.map.removeInteraction(this.modifyInteraction);
368
+ this.routingLayer.attachToMap(this.map);
369
+ this.map.addInteraction(this.modifyInteraction);
370
+ this.modifyInteraction.setActive(this.modify);
371
+ this.addListeners();
372
+ } else {
373
+ this.format = new GeoJSON({ featureProjection: "EPSG:3857" });
374
+ this.graphsResolutions = this.graphs;
375
+ }
376
+ }
377
+ deactivate() {
378
+ if (this.map) {
379
+ this.routingLayer.detachFromMap(this.map);
380
+ this.map.removeInteraction(this.modifyInteraction);
381
+ this.removeListeners();
382
+ this.reset();
383
+ }
384
+ super.deactivate();
385
+ }
386
+ }
387
+ export default RoutingControl;