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,175 +0,0 @@
1
- import VectorLayer from "ol/layer/Vector";
2
- import VectorSource from "ol/source/Vector";
3
- import Map from "ol/Map";
4
- import Group from "ol/layer/Group";
5
- import Layer from "./Layer";
6
- let olLayer;
7
- let map;
8
- describe("Layer", () => {
9
- beforeEach(() => {
10
- map = new Map({});
11
- olLayer = new VectorLayer({ source: new VectorSource() });
12
- });
13
- test("should initialize.", () => {
14
- const layer = new Layer({ name: "Layer", olLayer });
15
- expect(layer).toBeInstanceOf(Layer);
16
- });
17
- test("should be visible by default.", () => {
18
- const layer = new Layer({ name: "Layer", olLayer });
19
- expect(layer.visible).toBe(true);
20
- });
21
- test("should be invisible if defined.", () => {
22
- const layer = new Layer({ name: "Layer", visible: false, olLayer });
23
- expect(layer.visible).toBe(false);
24
- });
25
- test("should be invisible if set.", () => {
26
- const layer = new Layer({ name: "Layer", olLayer });
27
- layer.visible = false;
28
- expect(layer.visible).toBe(false);
29
- });
30
- test("should visibility stay unchanged", () => {
31
- const layer = new Layer({ name: "Layer", visible: false, olLayer });
32
- layer.visible = false;
33
- expect(layer.visible).toBe(false);
34
- });
35
- test("should return its name.", () => {
36
- const layer = new Layer({ name: "Layer", visible: false, olLayer });
37
- expect(layer.name).toEqual("Layer");
38
- });
39
- test("should call terminate on initialization.", () => {
40
- const layer = new Layer({ name: "Layer", olLayer });
41
- const spy = jest.spyOn(layer, "detachFromMap");
42
- layer.attachToMap();
43
- expect(spy).toHaveBeenCalledTimes(1);
44
- });
45
- test("should remove the layer when we call terminate.", () => {
46
- const layer = new Layer({ name: "Layer", olLayer });
47
- const spy = jest.spyOn(layer, "detachFromMap");
48
- layer.attachToMap(map);
49
- expect(spy).toHaveBeenCalledTimes(1);
50
- layer.detachFromMap(map);
51
- expect(spy).toHaveBeenCalledTimes(2);
52
- });
53
- test("should manage copyrights as string.", () => {
54
- const spy = jest.spyOn(VectorSource.prototype, "setAttributions");
55
- const layer = new Layer({ name: "Layer", copyrights: "foo", olLayer });
56
- layer.attachToMap(map);
57
- expect(spy).toHaveBeenCalledWith(["foo"]);
58
- });
59
- test("should manage copyrights as array.", () => {
60
- const spy = jest.spyOn(VectorSource.prototype, "setAttributions");
61
- const layer = new Layer({ name: "Layer", copyrights: ["bar"], olLayer });
62
- layer.attachToMap(map);
63
- expect(spy).toHaveBeenCalledWith(["bar"]);
64
- });
65
- test("should set attributions for Group.", () => {
66
- const spy = jest.spyOn(VectorSource.prototype, "setAttributions");
67
- const layer = new Layer({
68
- name: "Layer",
69
- copyrights: ["bar"],
70
- olLayer: new Group({ layers: [olLayer] })
71
- });
72
- layer.attachToMap(map);
73
- expect(spy).toHaveBeenCalledWith(["bar"]);
74
- });
75
- test("should listen for click/hover events when layer is visible by default then should not when hidden.", async () => {
76
- global.console.error = jest.fn();
77
- const layer = new Layer({ name: "Layer", olLayer });
78
- expect(layer.visible).toBe(true);
79
- const spy = jest.fn();
80
- const spy2 = jest.fn();
81
- layer.onHover(spy);
82
- layer.onClick(spy2);
83
- layer.attachToMap(map);
84
- expect(spy).toHaveBeenCalledTimes(0);
85
- expect(spy2).toHaveBeenCalledTimes(0);
86
- await map.dispatchEvent({ type: "pointermove", map, coordinate: [0, 0] });
87
- await map.dispatchEvent({ type: "singleclick", map, coordinate: [0, 0] });
88
- expect(spy).toHaveBeenCalledTimes(1);
89
- expect(spy2).toHaveBeenCalledTimes(1);
90
- spy.mockReset();
91
- spy2.mockReset();
92
- layer.visible = false;
93
- await map.dispatchEvent({ type: "pointermove", map, coordinate: [0, 0] });
94
- await map.dispatchEvent({ type: "singleclick", map, coordinate: [0, 0] });
95
- expect(spy).toHaveBeenCalledTimes(0);
96
- expect(spy2).toHaveBeenCalledTimes(0);
97
- global.console.error.mockRestore();
98
- });
99
- test("should not listen for click/hover events when layer is not visible by default then should not when visible.", async () => {
100
- global.console.error = jest.fn();
101
- const layer = new Layer({ name: "Layer", olLayer, visible: false });
102
- expect(layer.visible).toBe(false);
103
- const spy = jest.fn();
104
- const spy2 = jest.fn();
105
- layer.onHover(spy);
106
- layer.onClick(spy2);
107
- layer.attachToMap(map);
108
- expect(spy).toHaveBeenCalledTimes(0);
109
- expect(spy2).toHaveBeenCalledTimes(0);
110
- await map.dispatchEvent({ type: "pointermove", map, coordinate: [0, 0] });
111
- await map.dispatchEvent({ type: "singleclick", map, coordinate: [0, 0] });
112
- expect(spy).toHaveBeenCalledTimes(0);
113
- expect(spy2).toHaveBeenCalledTimes(0);
114
- spy.mockReset();
115
- spy2.mockReset();
116
- layer.visible = true;
117
- await map.dispatchEvent({ type: "pointermove", map, coordinate: [0, 0] });
118
- await map.dispatchEvent({ type: "singleclick", map, coordinate: [0, 0] });
119
- expect(spy).toHaveBeenCalledTimes(1);
120
- expect(spy2).toHaveBeenCalledTimes(1);
121
- global.console.error.mockRestore();
122
- });
123
- test("should not listen for click/hover events after layer.detachFromMap()", async () => {
124
- global.console.error = jest.fn();
125
- const layer = new Layer({ name: "Layer", olLayer, visible: true });
126
- expect(layer.visible).toBe(true);
127
- const spy = jest.fn();
128
- const spy2 = jest.fn();
129
- layer.attachToMap(map);
130
- layer.onHover(spy);
131
- layer.onClick(spy2);
132
- layer.attachToMap(map);
133
- expect(spy).toHaveBeenCalledTimes(0);
134
- expect(spy2).toHaveBeenCalledTimes(0);
135
- await map.dispatchEvent({
136
- type: "pointermove",
137
- map,
138
- coordinate: [0, 0]
139
- });
140
- await map.dispatchEvent({
141
- type: "singleclick",
142
- map,
143
- coordinate: [0, 0]
144
- });
145
- expect(spy).toHaveBeenCalledTimes(1);
146
- expect(spy2).toHaveBeenCalledTimes(1);
147
- spy.mockReset();
148
- spy2.mockReset();
149
- layer.detachFromMap(map);
150
- await map.dispatchEvent({
151
- type: "pointermove",
152
- map,
153
- coordinate: [0, 0]
154
- });
155
- await map.dispatchEvent({
156
- type: "singleclick",
157
- map,
158
- coordinate: [0, 0]
159
- });
160
- expect(spy).toHaveBeenCalledTimes(0);
161
- expect(spy2).toHaveBeenCalledTimes(0);
162
- global.console.error.mockRestore();
163
- });
164
- test("should clone", () => {
165
- const layer = new Layer({
166
- name: "Layer",
167
- copyrights: ["bar"],
168
- olLayer: new Group({ layers: [olLayer] })
169
- });
170
- const clone = layer.clone({ name: "clone" });
171
- expect(clone).not.toBe(layer);
172
- expect(clone.name).toBe("clone");
173
- expect(clone).toBeInstanceOf(Layer);
174
- });
175
- });
@@ -1,204 +0,0 @@
1
- import { toLonLat } from "ol/proj";
2
- import { Map } from "mapbox-gl";
3
- import Source from "ol/source/Source";
4
- import OLLayer from "ol/layer/Layer";
5
- import GeoJSON from "ol/format/GeoJSON";
6
- import Layer from "./Layer";
7
- import { getMapboxMapCopyrights, getUrlWithParams } from "../../common/utils";
8
- export default class MapboxLayer extends Layer {
9
- constructor(options = {}) {
10
- const mbLayer = new OLLayer({
11
- source: new Source({}),
12
- render: (frameState) => {
13
- if (!this.map || !this.mbMap) {
14
- return null;
15
- }
16
- let changed = false;
17
- const canvas = this.mbMap.getCanvas();
18
- const { viewState } = frameState;
19
- const visible = this.olLayer.getVisible();
20
- if (this.renderState.visible !== visible) {
21
- canvas.style.display = visible ? "block" : "none";
22
- this.renderState.visible = visible;
23
- canvas.style.position = "absolute";
24
- }
25
- const opacity = this.olLayer.getOpacity();
26
- if (this.renderState.opacity !== opacity) {
27
- canvas.style.opacity = opacity;
28
- this.renderState.opacity = opacity;
29
- }
30
- const { rotation } = viewState;
31
- if (this.renderState.rotation !== rotation) {
32
- this.mbMap.rotateTo(-(rotation || 0) * 180 / Math.PI, {
33
- animate: false
34
- });
35
- changed = true;
36
- this.renderState.rotation = rotation;
37
- }
38
- if (this.renderState.zoom !== viewState.zoom || this.renderState.center[0] !== viewState.center[0] || this.renderState.center[1] !== viewState.center[1]) {
39
- this.mbMap.jumpTo({
40
- center: toLonLat(viewState.center),
41
- zoom: viewState.zoom - 1,
42
- animate: false
43
- });
44
- changed = true;
45
- this.renderState.zoom = viewState.zoom;
46
- this.renderState.center = viewState.center;
47
- }
48
- const size = this.map.getSize();
49
- if (this.renderState.size[0] !== size[0] || this.renderState.size[1] !== size[1]) {
50
- changed = true;
51
- this.renderState.size = size;
52
- }
53
- if (this.mbMap && this.mbMap.style && this.mbMap.isStyleLoaded() && changed) {
54
- try {
55
- if (this.mbMap._frame) {
56
- this.mbMap._frame.cancel();
57
- this.mbMap._frame = null;
58
- }
59
- this.mbMap._render();
60
- } catch (err) {
61
- console.warn(err);
62
- }
63
- }
64
- return this.mbMap.getContainer();
65
- }
66
- });
67
- super({
68
- ...options,
69
- olLayer: mbLayer
70
- });
71
- this.styleUrl = options.url;
72
- this.apiKey = options.apiKey;
73
- this.apiKeyName = options.apiKeyName || "key";
74
- this.updateAttribution = this.updateAttribution.bind(this);
75
- }
76
- attachToMap(map) {
77
- super.attachToMap(map);
78
- if (!this.map || this.mbMap) {
79
- return;
80
- }
81
- this.format = new GeoJSON({
82
- featureProjection: this.map.getView().getProjection()
83
- });
84
- this.loadMbMap();
85
- this.olListenersKeys.push(this.map.on("change:size", () => {
86
- try {
87
- if (this.mbMap) {
88
- this.mbMap.resize();
89
- }
90
- } catch (err) {
91
- console.warn(err);
92
- }
93
- }));
94
- }
95
- detachFromMap() {
96
- if (this.mbMap) {
97
- this.mbMap.off("idle", this.updateAttribution);
98
- this.mbMap.triggerRepaint = () => {
99
- };
100
- this.mbMap.remove();
101
- this.mbMap = null;
102
- }
103
- this.loaded = false;
104
- super.detachFromMap();
105
- }
106
- loadMbMap() {
107
- this.olListenersKeys.push(this.map.on("change:target", () => {
108
- this.loadMbMap();
109
- }));
110
- if (!this.map.getTargetElement()) {
111
- return;
112
- }
113
- if (!this.visible) {
114
- this.olListenersKeys.push(this.once("change:visible", () => {
115
- this.loadMbMap();
116
- }));
117
- return;
118
- }
119
- let [x, y] = this.map.getView().getCenter();
120
- if (!x || !y) {
121
- x = 0;
122
- y = 0;
123
- }
124
- const container = document.createElement("div");
125
- container.style.position = "absolute";
126
- container.style.width = "100%";
127
- container.style.height = "100%";
128
- if (!this.apiKey && !this.styleUrl.includes(this.apiKeyName)) {
129
- console.warn(`No apiKey is defined for request to ${this.styleUrl}`);
130
- }
131
- this.mbMap = new Map({
132
- style: getUrlWithParams(this.styleUrl, {
133
- [this.apiKeyName]: this.apiKey
134
- }).toString(),
135
- container,
136
- interactive: false,
137
- trackResize: false,
138
- attributionControl: false,
139
- ...this.options.mapOptions || {}
140
- });
141
- this.renderState = {
142
- center: [x, y],
143
- zoom: null,
144
- rotation: null,
145
- visible: null,
146
- opacity: null,
147
- size: [0, 0]
148
- };
149
- this.mbMap.once("load", () => {
150
- this.mbMap.resize();
151
- this.loaded = true;
152
- this.copyrights = getMapboxMapCopyrights(this.mbMap) || [];
153
- this.olLayer.getSource()?.setAttributions(this.copyrights);
154
- this.dispatchEvent({
155
- type: "load",
156
- target: this
157
- });
158
- });
159
- const mapboxCanvas = this.mbMap.getCanvas();
160
- if (mapboxCanvas) {
161
- if (this.options.tabIndex) {
162
- mapboxCanvas.setAttribute("tabindex", this.options.tabIndex);
163
- } else {
164
- mapboxCanvas.removeAttribute("tabindex");
165
- }
166
- }
167
- this.mbMap.on("idle", this.updateAttribution);
168
- }
169
- updateAttribution(evt) {
170
- const newAttributions = getMapboxMapCopyrights(evt.target) || [];
171
- if (this.copyrights.toString() !== newAttributions.toString()) {
172
- this.copyrights = newAttributions;
173
- this.olLayer.getSource()?.setAttributions(newAttributions);
174
- }
175
- }
176
- getFeatureInfoAtCoordinate(coordinate, options) {
177
- if (!options || !this.format || !this.mbMap || !this.mbMap.isStyleLoaded()) {
178
- return Promise.resolve({ coordinate, features: [], layer: this });
179
- }
180
- let pixel = coordinate && this.mbMap.project(toLonLat(coordinate));
181
- if (this.hitTolerance) {
182
- const { x, y } = pixel;
183
- pixel = [
184
- { x: x - this.hitTolerance, y: y - this.hitTolerance },
185
- { x: x + this.hitTolerance, y: y + this.hitTolerance }
186
- ];
187
- }
188
- const features = this.mbMap.queryRenderedFeatures(pixel, options).map((feature) => {
189
- const olFeature = this.format.readFeature(feature);
190
- if (olFeature) {
191
- olFeature.set("mapboxFeature", feature);
192
- }
193
- return olFeature;
194
- });
195
- return Promise.resolve({
196
- layer: this,
197
- features,
198
- coordinate
199
- });
200
- }
201
- clone(newOptions) {
202
- return new MapboxLayer({ ...this.options, ...newOptions });
203
- }
204
- }
@@ -1,160 +0,0 @@
1
- import OlMap from "ol/Map";
2
- import View from "ol/View";
3
- import gllib from "mapbox-gl";
4
- import MapboxLayer from "./MapboxLayer";
5
- let layer;
6
- let map;
7
- let consoleOutput;
8
- const styleUrl = "http://foo.com/styles";
9
- describe("MapboxLayer", () => {
10
- describe("without apiKey", () => {
11
- beforeEach(() => {
12
- consoleOutput = [];
13
- console.warn = (message) => consoleOutput.push(message);
14
- layer = new MapboxLayer({
15
- name: "Layer",
16
- url: styleUrl
17
- });
18
- map = new OlMap({
19
- target: document.createElement("div"),
20
- view: new View({ center: [0, 0] })
21
- });
22
- });
23
- test("should be instanced.", () => {
24
- expect(layer).toBeInstanceOf(MapboxLayer);
25
- expect(layer.styleUrl).toBe(styleUrl);
26
- });
27
- test("should not initalized mapbox map.", () => {
28
- layer.attachToMap();
29
- expect(layer.mbMap).toBe();
30
- });
31
- test("should initalized mapbox map and warn the user if there is no api key defined.", () => {
32
- layer.attachToMap(map);
33
- expect(layer.mbMap).toBeInstanceOf(gllib.Map);
34
- expect(consoleOutput[0]).toBe("No apiKey is defined for request to http://foo.com/styles");
35
- });
36
- test("should called terminate on initalization.", () => {
37
- const spy = jest.spyOn(layer, "detachFromMap");
38
- layer.attachToMap();
39
- expect(spy).toHaveBeenCalledTimes(1);
40
- });
41
- test("should clone", () => {
42
- const clone = layer.clone({ name: "clone" });
43
- expect(clone).not.toBe(layer);
44
- expect(clone.name).toBe("clone");
45
- expect(clone).toBeInstanceOf(MapboxLayer);
46
- });
47
- });
48
- describe("with apiKey", () => {
49
- beforeEach(() => {
50
- layer = new MapboxLayer({
51
- name: "Layer",
52
- url: styleUrl,
53
- apiKey: "apiKey"
54
- });
55
- map = new OlMap({
56
- target: document.createElement("div"),
57
- view: new View({ center: [0, 0] })
58
- });
59
- });
60
- test("should be instanced with apiKey.", () => {
61
- expect(layer).toBeInstanceOf(MapboxLayer);
62
- expect(layer.styleUrl).toBe(styleUrl);
63
- });
64
- test("should not initalized mapbox map.", () => {
65
- layer.attachToMap();
66
- expect(layer.mbMap).toBe();
67
- });
68
- test("should initalized mapbox map, with 'apiKey' prop", () => {
69
- const layer1 = new MapboxLayer({
70
- name: "Layer",
71
- url: styleUrl,
72
- apiKey: "apiKeyVal"
73
- });
74
- layer1.attachToMap(map);
75
- expect(layer1.mbMap.options.style).toBe("http://foo.com/styles?key=apiKeyVal");
76
- });
77
- test("should initalized mapbox map, with 'apiKeyName' prop", () => {
78
- const layer1 = new MapboxLayer({
79
- name: "Layer",
80
- url: styleUrl,
81
- apiKey: "test",
82
- apiKeyName: "apiKey"
83
- });
84
- layer1.attachToMap(map);
85
- expect(layer1.mbMap.options.style).toBe("http://foo.com/styles?apiKey=test");
86
- });
87
- });
88
- describe("#getFeatureInfoAtCoordinate()", () => {
89
- let layer1;
90
- beforeEach(() => {
91
- layer1 = new MapboxLayer({
92
- name: "Layer",
93
- url: styleUrl,
94
- apiKey: "test",
95
- apiKeyName: "apiKey"
96
- });
97
- layer1.attachToMap(map);
98
- layer1.mbMap.isStyleLoaded = jest.fn(() => true);
99
- layer1.mbMap.getSource = jest.fn(() => true);
100
- });
101
- afterEach(() => {
102
- layer1.mbMap.getSource.mockRestore();
103
- layer1.mbMap.isStyleLoaded.mockRestore();
104
- });
105
- test("should set the mapboxFeature as a property", (done) => {
106
- const mapboxFeature = {
107
- id: "2",
108
- type: "Feature",
109
- properties: {
110
- foo: "bar"
111
- },
112
- source: "barr",
113
- sourceLayer: "fooo"
114
- };
115
- layer1.mbMap.project = jest.fn((coord) => ({ x: coord[0], y: coord[1] }));
116
- layer1.mbMap.queryRenderedFeatures = jest.fn(() => [mapboxFeature]);
117
- layer1.getFeatureInfoAtCoordinate([0, 0], {}).then((featureInfo) => {
118
- expect(featureInfo.features[0].get("mapboxFeature")).toBe(mapboxFeature);
119
- done();
120
- });
121
- layer1.mbMap.project.mockRestore();
122
- layer1.mbMap.queryRenderedFeatures.mockRestore();
123
- });
124
- describe("should use hitTolerance property", () => {
125
- beforeEach(() => {
126
- layer1.mbMap.project = jest.fn((coord) => ({
127
- x: coord[0],
128
- y: coord[1]
129
- }));
130
- });
131
- afterEach(() => {
132
- layer1.mbMap.project.mockRestore();
133
- layer1.mbMap.queryRenderedFeatures.mockRestore();
134
- });
135
- test("when hitTolerance is not set", (done) => {
136
- layer1.mbMap.queryRenderedFeatures = jest.fn((pixelBounds) => {
137
- expect(pixelBounds).toEqual([
138
- { x: -5, y: -5 },
139
- { x: 5, y: 5 }
140
- ]);
141
- done();
142
- return [];
143
- });
144
- layer1.getFeatureInfoAtCoordinate([0, 0], {});
145
- });
146
- test("when hitTolerance is set to 10", (done) => {
147
- layer1.hitTolerance = 10;
148
- layer1.mbMap.queryRenderedFeatures = jest.fn((pixelBounds) => {
149
- expect(pixelBounds).toEqual([
150
- { x: -10, y: -10 },
151
- { x: 10, y: 10 }
152
- ]);
153
- done();
154
- return [];
155
- });
156
- layer1.getFeatureInfoAtCoordinate([0, 0], {});
157
- });
158
- });
159
- });
160
- });