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,187 +0,0 @@
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;
@@ -1,226 +0,0 @@
1
- import OlMap from "ol/Map";
2
- import View from "ol/View";
3
- import gllib from "maplibre-gl";
4
- import Layer from "./Layer";
5
- import MaplibreLayer from "./MaplibreLayer";
6
- import MapboxStyleLayer from "./MapboxStyleLayer";
7
- let source;
8
- let layer;
9
- let map;
10
- let onClick;
11
- const styleLayer = {
12
- id: "layer"
13
- };
14
- describe("MapboxStyleLayer", () => {
15
- beforeEach(() => {
16
- onClick = jest.fn();
17
- source = new MaplibreLayer({
18
- name: "Layer",
19
- apiKey: "foo",
20
- url: "https://foo.com/styles"
21
- });
22
- layer = new MapboxStyleLayer({
23
- name: "mapbox layer",
24
- visible: true,
25
- mapboxLayer: source,
26
- styleLayer,
27
- onClick
28
- });
29
- map = new OlMap({
30
- target: document.createElement("div"),
31
- view: new View({ center: [0, 0] })
32
- });
33
- });
34
- afterEach(() => {
35
- if (layer.map) {
36
- layer.detachFromMap(map);
37
- }
38
- if (source.map) {
39
- source.detachFromMap(map);
40
- }
41
- });
42
- test("should be instanced.", () => {
43
- expect(layer).toBeInstanceOf(MapboxStyleLayer);
44
- expect(layer.styleLayers[0]).toBe(styleLayer);
45
- });
46
- test("should not initalized mapbox map.", () => {
47
- layer.attachToMap();
48
- expect(layer.mbMap).toBe();
49
- layer.detachFromMap();
50
- });
51
- test("should initalized mapbox map.", () => {
52
- source.attachToMap(map);
53
- layer.attachToMap(map);
54
- expect(layer.mapboxLayer.mbMap).toBeInstanceOf(gllib.Map);
55
- layer.detachFromMap();
56
- source.detachFromMap();
57
- });
58
- test("should called terminate on initalization.", () => {
59
- const spy = jest.spyOn(layer, "detachFromMap");
60
- layer.attachToMap();
61
- expect(spy).toHaveBeenCalledTimes(1);
62
- layer.detachFromMap(map);
63
- });
64
- test("should return coordinates, features and a layer instance.", async () => {
65
- source.attachToMap(map);
66
- layer.attachToMap(map);
67
- const data = await layer.getFeatureInfoAtCoordinate([50, 50]);
68
- expect(data.coordinate).toEqual([50, 50]);
69
- expect(data.features).toEqual([]);
70
- expect(data.layer).toBeInstanceOf(MapboxStyleLayer);
71
- layer.detachFromMap(map);
72
- source.detachFromMap(map);
73
- });
74
- test("should call onClick callback", async () => {
75
- const coordinate = [500, 500];
76
- const features = [];
77
- const evt = { type: "singleclick", map, coordinate };
78
- layer.attachToMap(map);
79
- expect(onClick).toHaveBeenCalledTimes(0);
80
- await map.dispatchEvent(evt);
81
- expect(onClick).toHaveBeenCalledTimes(1);
82
- expect(onClick).toHaveBeenCalledWith(features, layer, coordinate);
83
- layer.detachFromMap();
84
- });
85
- test("should call super class terminate function.", () => {
86
- layer.attachToMap(map);
87
- const spy = jest.spyOn(Layer.prototype, "detachFromMap");
88
- layer.detachFromMap(map);
89
- expect(spy).toHaveBeenCalledTimes(1);
90
- spy.mockRestore();
91
- });
92
- test("should call super class terminate if the mapboxLayer associated has been terminated before.", () => {
93
- layer.attachToMap(map);
94
- source.detachFromMap(map);
95
- const spy = jest.spyOn(Layer.prototype, "detachFromMap");
96
- layer.detachFromMap(map);
97
- expect(spy).toHaveBeenCalledTimes(1);
98
- spy.mockRestore();
99
- });
100
- test("should clone", () => {
101
- const clone = layer.clone({ name: "clone" });
102
- expect(clone).not.toBe(layer);
103
- expect(clone.name).toBe("clone");
104
- expect(clone).toBeInstanceOf(MapboxStyleLayer);
105
- });
106
- test("should add layer on load", () => {
107
- const style = { layers: [] };
108
- layer.mapboxLayer.mbMap = {
109
- getStyle: () => style,
110
- getSource: () => ({}),
111
- getLayer: () => null,
112
- setLayoutProperty: () => null,
113
- addLayer: (styleLayerr) => style.layers.push(styleLayerr)
114
- };
115
- layer.onLoad();
116
- expect(style.layers[0]).toBe(styleLayer);
117
- });
118
- describe("should set disabled property to false on load", () => {
119
- test("when layer uses styleLayer property", () => {
120
- const styles = { layers: [] };
121
- layer.mapboxLayer.mbMap = {
122
- getStyle: () => styles,
123
- getSource: () => ({}),
124
- getLayer: () => null,
125
- setLayoutProperty: () => null,
126
- addLayer: (styleLayerr) => styles.layers.push(styleLayerr)
127
- };
128
- expect(layer).toBeInstanceOf(MapboxStyleLayer);
129
- layer.onLoad();
130
- expect(layer.disabled).toBe(false);
131
- });
132
- });
133
- describe("should set disabled property to true on load", () => {
134
- test("when layer uses styleLayersFilter property", () => {
135
- const styles = { layers: [styleLayer] };
136
- const layer2 = new MapboxStyleLayer({
137
- name: "mapbox layer",
138
- mapboxLayer: source,
139
- styleLayersFilter: () => false
140
- });
141
- layer2.mapboxLayer.mbMap = {
142
- getStyle: () => styles,
143
- getSource: () => ({}),
144
- getLayer: () => null,
145
- setLayoutProperty: () => null,
146
- addLayer: () => ({})
147
- };
148
- layer2.onLoad();
149
- expect(layer2.disabled).toBe(true);
150
- });
151
- });
152
- describe("#getFeatureInfoAtCoordinate()", () => {
153
- beforeEach(() => {
154
- source.attachToMap(map);
155
- source.mbMap.isStyleLoaded = jest.fn(() => true);
156
- source.mbMap.getSource = jest.fn(() => true);
157
- });
158
- afterEach(() => {
159
- source.mbMap.getSource.mockRestore();
160
- source.mbMap.isStyleLoaded.mockRestore();
161
- });
162
- test("should request features on layers ids from styleLayers property", () => {
163
- source.mbMap.getStyle = jest.fn(() => ({
164
- layers: [{ id: "foo" }, { id: "layer" }, { id: "bar" }]
165
- }));
166
- layer.attachToMap(map);
167
- layer.mapboxLayer.getFeatureInfoAtCoordinate = jest.fn(() => Promise.resolve({ features: [] }));
168
- layer.getFeatureInfoAtCoordinate([0, 0]).then(() => {
169
- });
170
- expect(layer.mapboxLayer.getFeatureInfoAtCoordinate).toHaveBeenCalledWith([0, 0], { layers: ["layer"], validate: false });
171
- layer.mapboxLayer.getFeatureInfoAtCoordinate.mockRestore();
172
- source.mbMap.getStyle.mockRestore();
173
- });
174
- test("should request features on layers ids from styleLayersFilter property", () => {
175
- source.mbMap.getStyle = jest.fn(() => ({
176
- layers: [{ id: "foo" }, { id: "layer" }, { id: "bar" }, { id: "foo2" }]
177
- }));
178
- const layer2 = new MapboxStyleLayer({
179
- name: "mapbox layer",
180
- visible: true,
181
- mapboxLayer: source,
182
- styleLayer,
183
- styleLayersFilter: ({ id }) => /foo/.test(id)
184
- });
185
- layer2.attachToMap(map);
186
- layer2.mapboxLayer.getFeatureInfoAtCoordinate = jest.fn(() => Promise.resolve({ features: [] }));
187
- layer2.getFeatureInfoAtCoordinate([0, 0]).then(() => {
188
- });
189
- expect(layer2.mapboxLayer.getFeatureInfoAtCoordinate).toHaveBeenCalledWith([0, 0], {
190
- layers: ["foo", "foo2"],
191
- validate: false
192
- });
193
- layer2.mapboxLayer.getFeatureInfoAtCoordinate.mockRestore();
194
- source.mbMap.getStyle.mockRestore();
195
- });
196
- test("should request features on layers ids from queryRenderedLayersFilter property", () => {
197
- source.mbMap.getStyle = jest.fn(() => ({
198
- layers: [
199
- { id: "foo" },
200
- { id: "bar2" },
201
- { id: "layer" },
202
- { id: "bar" },
203
- { id: "foo2" }
204
- ]
205
- }));
206
- const layer2 = new MapboxStyleLayer({
207
- name: "mapbox layer",
208
- visible: true,
209
- mapboxLayer: source,
210
- styleLayer,
211
- styleLayersFilter: ({ id }) => /foo/.test(id),
212
- queryRenderedLayersFilter: ({ id }) => /bar/.test(id)
213
- });
214
- layer2.attachToMap(map);
215
- layer2.mapboxLayer.getFeatureInfoAtCoordinate = jest.fn(() => Promise.resolve({ features: [] }));
216
- layer2.getFeatureInfoAtCoordinate([0, 0]).then(() => {
217
- });
218
- expect(layer2.mapboxLayer.getFeatureInfoAtCoordinate).toHaveBeenCalledWith([0, 0], {
219
- layers: ["bar2", "bar"],
220
- validate: false
221
- });
222
- layer2.mapboxLayer.getFeatureInfoAtCoordinate.mockRestore();
223
- source.mbMap.getStyle.mockRestore();
224
- });
225
- });
226
- });
@@ -1,140 +0,0 @@
1
- import { toLonLat } from "ol/proj";
2
- import { Map } from "maplibre-gl";
3
- import Source from "ol/source/Source";
4
- import OLLayer from "ol/layer/Layer";
5
- import GeoJSON from "ol/format/GeoJSON";
6
- import { toDegrees } from "ol/math";
7
- import Layer from "./Layer";
8
- import { getMapboxMapCopyrights, getUrlWithParams } from "../../common/utils";
9
- export default class MaplibreLayer extends Layer {
10
- constructor(options = {}) {
11
- const mbLayer = new OLLayer({
12
- source: new Source({}),
13
- render: (frameState) => {
14
- if (!this.map || !this.mbMap) {
15
- return null;
16
- }
17
- const canvas = this.mbMap.getCanvas();
18
- const { viewState } = frameState;
19
- const opacity = this.olLayer.getOpacity();
20
- canvas.style.opacity = opacity;
21
- this.mbMap.jumpTo({
22
- center: toLonLat(viewState.center),
23
- zoom: viewState.zoom - 1,
24
- bearing: toDegrees(-viewState.rotation),
25
- animate: false
26
- });
27
- if (!canvas.isConnected) {
28
- this.map.render();
29
- } else if (canvas.width !== frameState.size[0] || canvas.height !== frameState.size[1]) {
30
- this.mbMap.resize();
31
- }
32
- this.mbMap.redraw();
33
- return this.mbMap.getContainer();
34
- }
35
- });
36
- super({
37
- ...options,
38
- olLayer: mbLayer
39
- });
40
- this.styleUrl = options.url;
41
- this.apiKey = options.apiKey;
42
- this.apiKeyName = options.apiKeyName || "key";
43
- this.updateAttribution = this.updateAttribution.bind(this);
44
- }
45
- attachToMap(map) {
46
- super.attachToMap(map);
47
- if (!this.map) {
48
- return;
49
- }
50
- this.format = new GeoJSON({
51
- featureProjection: this.map.getView().getProjection()
52
- });
53
- this.loadMbMap();
54
- }
55
- detachFromMap() {
56
- if (this.mbMap) {
57
- this.mbMap.off("idle", this.updateAttribution);
58
- this.mbMap.triggerRepaint = () => {
59
- };
60
- this.mbMap.remove();
61
- this.mbMap = null;
62
- }
63
- this.loaded = false;
64
- super.detachFromMap();
65
- }
66
- loadMbMap() {
67
- this.olListenersKeys.push(this.map.on("change:target", () => {
68
- this.loadMbMap();
69
- }));
70
- if (!this.map.getTargetElement()) {
71
- return;
72
- }
73
- if (!this.visible) {
74
- this.olListenersKeys.push(this.once("change:visible", () => {
75
- this.loadMbMap();
76
- }));
77
- return;
78
- }
79
- const container = document.createElement("div");
80
- container.style.position = "absolute";
81
- container.style.width = "100%";
82
- container.style.height = "100%";
83
- if (!this.apiKey && !this.styleUrl.includes(this.apiKeyName)) {
84
- console.error(`No apiKey defined for mapbox layer with style url to ${this.url}`);
85
- }
86
- this.mbMap = new Map({
87
- style: getUrlWithParams(this.styleUrl, {
88
- [this.apiKeyName]: this.apiKey
89
- }).toString(),
90
- container,
91
- interactive: false,
92
- trackResize: false,
93
- attributionControl: false,
94
- ...this.options.mapOptions || {}
95
- });
96
- this.mbMap.once("load", () => {
97
- this.loaded = true;
98
- this.dispatchEvent({
99
- type: "load",
100
- target: this
101
- });
102
- });
103
- this.mbMap.on("idle", this.updateAttribution);
104
- }
105
- updateAttribution(evt) {
106
- const newAttributions = getMapboxMapCopyrights(evt.target) || [];
107
- if (this.copyrights?.toString() !== newAttributions.toString()) {
108
- this.copyrights = newAttributions;
109
- this.olLayer.getSource()?.setAttributions(newAttributions);
110
- }
111
- }
112
- getFeatureInfoAtCoordinate(coordinate, options) {
113
- if (!options || !this.format || !this.mbMap || !this.mbMap.isStyleLoaded()) {
114
- return Promise.resolve({ coordinate, features: [], layer: this });
115
- }
116
- let pixel = coordinate && this.mbMap.project(toLonLat(coordinate));
117
- if (this.hitTolerance) {
118
- const { x, y } = pixel;
119
- pixel = [
120
- { x: x - this.hitTolerance, y: y - this.hitTolerance },
121
- { x: x + this.hitTolerance, y: y + this.hitTolerance }
122
- ];
123
- }
124
- const features = this.mbMap.queryRenderedFeatures(pixel, options).map((feature) => {
125
- const olFeature = this.format.readFeature(feature);
126
- if (olFeature) {
127
- olFeature.set("mapboxFeature", feature);
128
- }
129
- return olFeature;
130
- });
131
- return Promise.resolve({
132
- layer: this,
133
- features,
134
- coordinate
135
- });
136
- }
137
- clone(newOptions) {
138
- return new MaplibreLayer({ ...this.options, ...newOptions });
139
- }
140
- }