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
@@ -1,180 +1,154 @@
1
- import OlMap from 'ol/Map';
2
- import View from 'ol/View';
3
- import gllib from 'mapbox-gl';
4
- import MapboxLayer from './MapboxLayer';
5
-
1
+ import OlMap from "ol/Map";
2
+ import View from "ol/View";
3
+ import gllib from "mapbox-gl";
4
+ import MapboxLayer from "./MapboxLayer";
6
5
  let layer;
7
6
  let map;
8
7
  let consoleOutput;
9
- const styleUrl = 'foo.com/styles';
10
-
11
- describe('MapboxLayer', () => {
12
- describe('without apiKey', () => {
8
+ const styleUrl = "foo.com/styles";
9
+ describe("MapboxLayer", () => {
10
+ describe("without apiKey", () => {
13
11
  beforeEach(() => {
14
- // Mock console statement
15
12
  consoleOutput = [];
16
- // eslint-disable-next-line no-console
17
13
  console.warn = (message) => consoleOutput.push(message);
18
14
  layer = new MapboxLayer({
19
- name: 'Layer',
20
- url: styleUrl,
15
+ name: "Layer",
16
+ url: styleUrl
21
17
  });
22
18
  map = new OlMap({
23
- target: document.createElement('div'),
24
- view: new View({ center: [0, 0] }),
19
+ target: document.createElement("div"),
20
+ view: new View({ center: [0, 0] })
25
21
  });
26
22
  });
27
-
28
- test('should be instanced.', () => {
23
+ test("should be instanced.", () => {
29
24
  expect(layer).toBeInstanceOf(MapboxLayer);
30
25
  expect(layer.styleUrl).toBe(styleUrl);
31
26
  });
32
-
33
- test('should not initalized mapbox map.', () => {
34
- layer.init();
27
+ test("should not initalized mapbox map.", () => {
28
+ layer.attachToMap();
35
29
  expect(layer.mbMap).toBe();
36
30
  });
37
-
38
- test('should initalized mapbox map and warn the user if there is no api key defined.', () => {
39
- layer.init(map);
31
+ test("should initalized mapbox map and warn the user if there is no api key defined.", () => {
32
+ layer.attachToMap(map);
40
33
  expect(layer.mbMap).toBeInstanceOf(gllib.Map);
41
- expect(consoleOutput[0]).toBe(
42
- 'No apiKey is defined for request to foo.com/styles',
43
- );
34
+ expect(consoleOutput[0]).toBe("No apiKey is defined for request to foo.com/styles");
44
35
  });
45
-
46
- test('should called terminate on initalization.', () => {
47
- const spy = jest.spyOn(layer, 'terminate');
48
- layer.init();
36
+ test("should called terminate on initalization.", () => {
37
+ const spy = jest.spyOn(layer, "detachFromMap");
38
+ layer.attachToMap();
49
39
  expect(spy).toHaveBeenCalledTimes(1);
50
40
  });
51
-
52
- test('should clone', () => {
53
- const clone = layer.clone({ name: 'clone' });
41
+ test("should clone", () => {
42
+ const clone = layer.clone({ name: "clone" });
54
43
  expect(clone).not.toBe(layer);
55
- expect(clone.name).toBe('clone');
44
+ expect(clone.name).toBe("clone");
56
45
  expect(clone).toBeInstanceOf(MapboxLayer);
57
46
  });
58
47
  });
59
-
60
- describe('with apiKey', () => {
48
+ describe("with apiKey", () => {
61
49
  beforeEach(() => {
62
50
  layer = new MapboxLayer({
63
- name: 'Layer',
51
+ name: "Layer",
64
52
  url: styleUrl,
65
- apiKey: 'apiKey',
53
+ apiKey: "apiKey"
66
54
  });
67
55
  map = new OlMap({
68
- target: document.createElement('div'),
69
- view: new View({ center: [0, 0] }),
56
+ target: document.createElement("div"),
57
+ view: new View({ center: [0, 0] })
70
58
  });
71
59
  });
72
-
73
- test('should be instanced with apiKey.', () => {
60
+ test("should be instanced with apiKey.", () => {
74
61
  expect(layer).toBeInstanceOf(MapboxLayer);
75
62
  expect(layer.styleUrl).toBe(styleUrl);
76
63
  });
77
-
78
- test('should not initalized mapbox map.', () => {
79
- layer.init();
64
+ test("should not initalized mapbox map.", () => {
65
+ layer.attachToMap();
80
66
  expect(layer.mbMap).toBe();
81
67
  });
82
-
83
68
  test("should initalized mapbox map, with 'apiKey' prop", () => {
84
69
  const layer1 = new MapboxLayer({
85
- name: 'Layer',
70
+ name: "Layer",
86
71
  url: styleUrl,
87
- apiKey: 'apiKeyVal',
72
+ apiKey: "apiKeyVal"
88
73
  });
89
- layer1.init(map);
90
- expect(layer1.mbMap.options.style).toBe('foo.com/styles?key=apiKeyVal');
74
+ layer1.attachToMap(map);
75
+ expect(layer1.mbMap.options.style).toBe("foo.com/styles?key=apiKeyVal");
91
76
  });
92
-
93
77
  test("should initalized mapbox map, with 'apiKeyName' prop", () => {
94
78
  const layer1 = new MapboxLayer({
95
- name: 'Layer',
79
+ name: "Layer",
96
80
  url: styleUrl,
97
- apiKey: 'test',
98
- apiKeyName: 'apiKey',
81
+ apiKey: "test",
82
+ apiKeyName: "apiKey"
99
83
  });
100
- layer1.init(map);
101
- expect(layer1.mbMap.options.style).toBe('foo.com/styles?apiKey=test');
84
+ layer1.attachToMap(map);
85
+ expect(layer1.mbMap.options.style).toBe("foo.com/styles?apiKey=test");
102
86
  });
103
87
  });
104
-
105
- describe('#getFeatureInfoAtCoordinate()', () => {
88
+ describe("#getFeatureInfoAtCoordinate()", () => {
106
89
  let layer1;
107
90
  beforeEach(() => {
108
91
  layer1 = new MapboxLayer({
109
- name: 'Layer',
92
+ name: "Layer",
110
93
  url: styleUrl,
111
- apiKey: 'test',
112
- apiKeyName: 'apiKey',
94
+ apiKey: "test",
95
+ apiKeyName: "apiKey"
113
96
  });
114
- layer1.init(map);
97
+ layer1.attachToMap(map);
115
98
  layer1.mbMap.isStyleLoaded = jest.fn(() => true);
116
99
  layer1.mbMap.getSource = jest.fn(() => true);
117
100
  });
118
-
119
101
  afterEach(() => {
120
102
  layer1.mbMap.getSource.mockRestore();
121
103
  layer1.mbMap.isStyleLoaded.mockRestore();
122
104
  });
123
-
124
- test('should set the mapboxFeature as a property', (done) => {
105
+ test("should set the mapboxFeature as a property", (done) => {
125
106
  const mapboxFeature = {
126
- id: '2',
127
- type: 'Feature',
107
+ id: "2",
108
+ type: "Feature",
128
109
  properties: {
129
- foo: 'bar',
110
+ foo: "bar"
130
111
  },
131
- source: 'barr',
132
- sourceLayer: 'fooo',
112
+ source: "barr",
113
+ sourceLayer: "fooo"
133
114
  };
134
115
  layer1.mbMap.project = jest.fn((coord) => ({ x: coord[0], y: coord[1] }));
135
116
  layer1.mbMap.queryRenderedFeatures = jest.fn(() => [mapboxFeature]);
136
117
  layer1.getFeatureInfoAtCoordinate([0, 0], {}).then((featureInfo) => {
137
- expect(featureInfo.features[0].get('mapboxFeature')).toBe(
138
- mapboxFeature,
139
- );
118
+ expect(featureInfo.features[0].get("mapboxFeature")).toBe(mapboxFeature);
140
119
  done();
141
120
  });
142
121
  layer1.mbMap.project.mockRestore();
143
122
  layer1.mbMap.queryRenderedFeatures.mockRestore();
144
123
  });
145
- describe('should use hitTolerance property', () => {
124
+ describe("should use hitTolerance property", () => {
146
125
  beforeEach(() => {
147
126
  layer1.mbMap.project = jest.fn((coord) => ({
148
127
  x: coord[0],
149
- y: coord[1],
128
+ y: coord[1]
150
129
  }));
151
130
  });
152
-
153
131
  afterEach(() => {
154
132
  layer1.mbMap.project.mockRestore();
155
133
  layer1.mbMap.queryRenderedFeatures.mockRestore();
156
134
  });
157
-
158
- test('when hitTolerance is not set', (done) => {
135
+ test("when hitTolerance is not set", (done) => {
159
136
  layer1.mbMap.queryRenderedFeatures = jest.fn((pixelBounds) => {
160
- // Use default hoitTolerance
161
137
  expect(pixelBounds).toEqual([
162
138
  { x: -5, y: -5 },
163
- { x: 5, y: 5 },
139
+ { x: 5, y: 5 }
164
140
  ]);
165
141
  done();
166
142
  return [];
167
143
  });
168
144
  layer1.getFeatureInfoAtCoordinate([0, 0], {});
169
145
  });
170
-
171
- test('when hitTolerance is set to 10', (done) => {
146
+ test("when hitTolerance is set to 10", (done) => {
172
147
  layer1.hitTolerance = 10;
173
148
  layer1.mbMap.queryRenderedFeatures = jest.fn((pixelBounds) => {
174
- // Use default hoitTolerance
175
149
  expect(pixelBounds).toEqual([
176
150
  { x: -10, y: -10 },
177
- { x: 10, y: 10 },
151
+ { x: 10, y: 10 }
178
152
  ]);
179
153
  done();
180
154
  return [];
@@ -0,0 +1,187 @@
1
+ import Layer from "./Layer";
2
+ class MapboxStyleLayer extends Layer {
3
+ constructor(options = {}) {
4
+ super(options);
5
+ this.mapboxLayer = options.mapboxLayer;
6
+ this.disabled = false;
7
+ this.styleLayersFilter = options.styleLayersFilter;
8
+ this.beforeId = options.beforeId;
9
+ this.featureInfoFilter = options.featureInfoFilter || ((obj) => obj);
10
+ this.queryRenderedLayersFilter = options.queryRenderedLayersFilter;
11
+ this.highlightedFeatures = [];
12
+ this.selectedFeatures = [];
13
+ this.styleLayers = (options.styleLayer ? [options.styleLayer] : options.styleLayers) || [];
14
+ this.addStyleLayers = this.addStyleLayers.bind(this);
15
+ this.onLoad = this.onLoad.bind(this);
16
+ if (options.filters) {
17
+ this.addDynamicFilters = () => {
18
+ this.setFilter(typeof options.filters === "function" ? options.filters(this) : options.filters);
19
+ };
20
+ }
21
+ if (!this.styleLayersFilter && this.styleLayers) {
22
+ const ids = this.styleLayers.map((s) => s.id);
23
+ this.styleLayersFilter = (styleLayer) => ids.includes(styleLayer.id);
24
+ }
25
+ }
26
+ attachToMap(map) {
27
+ if (!this.mapboxLayer.map) {
28
+ this.mapboxLayer.attachToMap(map);
29
+ }
30
+ super.attachToMap(map);
31
+ if (!this.map) {
32
+ return;
33
+ }
34
+ const { mbMap } = this.mapboxLayer;
35
+ if (!mbMap) {
36
+ this.olListenersKeys.push(this.map.on("change:target", () => {
37
+ this.attachToMap(map);
38
+ }));
39
+ return;
40
+ }
41
+ if (this.mapboxLayer.loaded || mbMap.isStyleLoaded() || mbMap.loaded()) {
42
+ this.onLoad();
43
+ } else {
44
+ mbMap.once("load", this.onLoad);
45
+ }
46
+ this.olListenersKeys.push(this.on("change:visible", (evt) => {
47
+ this.applyLayoutVisibility(evt);
48
+ }));
49
+ this.olListenersKeys.push(this.mapboxLayer.on("load", () => {
50
+ this.onLoad();
51
+ }));
52
+ }
53
+ detachFromMap(map) {
54
+ const { mbMap } = this.mapboxLayer;
55
+ if (mbMap) {
56
+ mbMap.off("load", this.onLoad);
57
+ this.removeStyleLayers();
58
+ }
59
+ super.detachFromMap(map);
60
+ }
61
+ addStyleLayers() {
62
+ const { mbMap } = this.mapboxLayer;
63
+ if (!mbMap) {
64
+ return;
65
+ }
66
+ this.styleLayers.forEach((styleLayer) => {
67
+ const { id, source } = styleLayer;
68
+ if (mbMap.getSource(source) && !mbMap.getLayer(id)) {
69
+ mbMap.addLayer(styleLayer, this.beforeId);
70
+ }
71
+ });
72
+ this.applyLayoutVisibility();
73
+ }
74
+ removeStyleLayers() {
75
+ const { mbMap } = this.mapboxLayer;
76
+ if (!mbMap) {
77
+ return;
78
+ }
79
+ this.styleLayers.forEach((styleLayer) => {
80
+ if (mbMap.getLayer(styleLayer.id)) {
81
+ mbMap.removeLayer(styleLayer.id);
82
+ }
83
+ });
84
+ }
85
+ onLoad() {
86
+ this.addStyleLayers();
87
+ if (this.addDynamicFilters) {
88
+ this.addDynamicFilters();
89
+ }
90
+ const { mbMap } = this.mapboxLayer;
91
+ const style = mbMap.getStyle();
92
+ if (style && this.styleLayersFilter) {
93
+ const styles = style.layers.filter(this.styleLayersFilter);
94
+ this.disabled = !styles.length;
95
+ }
96
+ }
97
+ getFeatureInfoAtCoordinate(coordinate) {
98
+ const { mbMap } = this.mapboxLayer;
99
+ if (!mbMap || !mbMap.isStyleLoaded()) {
100
+ return Promise.resolve({ coordinate, features: [], layer: this });
101
+ }
102
+ let layers = this.styleLayers || [];
103
+ if (this.styleLayersFilter) {
104
+ layers = mbMap.getStyle().layers.filter(this.styleLayersFilter);
105
+ }
106
+ if (this.queryRenderedLayersFilter) {
107
+ layers = mbMap.getStyle().layers.filter(this.queryRenderedLayersFilter);
108
+ }
109
+ return this.mapboxLayer.getFeatureInfoAtCoordinate(coordinate, {
110
+ layers: layers.map((layer) => layer && layer.id),
111
+ validate: false
112
+ }).then((featureInfo) => {
113
+ const features = featureInfo.features.filter((feature) => this.featureInfoFilter(feature, this.map.getView().getResolution()));
114
+ this.highlight(features);
115
+ return { ...featureInfo, features, layer: this };
116
+ });
117
+ }
118
+ setFilter(filter) {
119
+ const { mbMap } = this.mapboxLayer;
120
+ if (!mbMap) {
121
+ return;
122
+ }
123
+ this.styleLayers.forEach(({ id }) => {
124
+ if (mbMap.getLayer(id)) {
125
+ mbMap.setFilter(id, filter);
126
+ }
127
+ });
128
+ }
129
+ setHoverState(features, state) {
130
+ const { mbMap } = this.mapboxLayer;
131
+ if (!features || !mbMap) {
132
+ return;
133
+ }
134
+ features.forEach((feature) => {
135
+ const { source, sourceLayer } = feature.get("mapboxFeature") || {};
136
+ if (!source && !sourceLayer || !feature.getId()) {
137
+ if (!feature.getId()) {
138
+ console.warn("No feature's id found. To use the feature state functionnality, tiles must be generated with --generate-ids. See https://github.com/mapbox/tippecanoe#adding-calculated-attributes.", feature.getId(), feature.getProperties());
139
+ }
140
+ return;
141
+ }
142
+ mbMap.setFeatureState({
143
+ id: feature.getId(),
144
+ source,
145
+ sourceLayer
146
+ }, { hover: state });
147
+ });
148
+ }
149
+ select(features = []) {
150
+ this.setHoverState(this.selectedFeatures, false);
151
+ this.selectedFeatures = features;
152
+ this.setHoverState(this.selectedFeatures, true);
153
+ }
154
+ highlight(features = []) {
155
+ const filtered = this.highlightedFeatures.filter((feature) => !this.selectedFeatures.map((feat) => feat.getId()).includes(feature.getId()));
156
+ this.setHoverState(filtered, false);
157
+ this.highlightedFeatures = features;
158
+ this.setHoverState(this.highlightedFeatures, true);
159
+ }
160
+ applyLayoutVisibility(evt) {
161
+ const { visible } = this;
162
+ const { mbMap } = this.mapboxLayer;
163
+ const filterFunc = this.styleLayersFilter;
164
+ if (!mbMap) {
165
+ return;
166
+ }
167
+ const style = mbMap.getStyle();
168
+ if (!style) {
169
+ return;
170
+ }
171
+ if (filterFunc) {
172
+ const visibilityValue = visible ? "visible" : "none";
173
+ for (let i = 0; i < style.layers.length; i += 1) {
174
+ const styleLayer = style.layers[i];
175
+ if (filterFunc(styleLayer)) {
176
+ if (mbMap.getLayer(styleLayer.id)) {
177
+ mbMap.setLayoutProperty(styleLayer.id, "visibility", visibilityValue);
178
+ }
179
+ }
180
+ }
181
+ }
182
+ }
183
+ clone(newOptions) {
184
+ return new MapboxStyleLayer({ ...this.options, ...newOptions });
185
+ }
186
+ }
187
+ export default MapboxStyleLayer;