mobility-toolbox-js 2.0.0-beta.30 → 2.0.0-beta.34

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 (225) hide show
  1. package/api/RealtimeAPI.d.ts +268 -0
  2. package/api/RealtimeAPI.d.ts.map +1 -0
  3. package/api/RealtimeAPI.js +626 -343
  4. package/api/RoutingAPI.d.ts +33 -0
  5. package/api/RoutingAPI.d.ts.map +1 -0
  6. package/api/RoutingAPI.js +65 -15
  7. package/api/StopsAPI.d.ts +36 -0
  8. package/api/StopsAPI.d.ts.map +1 -0
  9. package/api/StopsAPI.js +70 -12
  10. package/api/index.d.ts +5 -0
  11. package/api/index.d.ts.map +1 -0
  12. package/api/index.js +10 -3
  13. package/api/typedefs.d.ts +105 -0
  14. package/api/typedefs.d.ts.map +1 -0
  15. package/api/typedefs.js +72 -0
  16. package/common/api/HttpAPI.d.ts +31 -0
  17. package/common/api/HttpAPI.d.ts.map +1 -0
  18. package/common/api/HttpAPI.js +82 -30
  19. package/common/api/WebSocketAPI.d.ts +95 -0
  20. package/common/api/WebSocketAPI.d.ts.map +1 -0
  21. package/common/api/WebSocketAPI.js +313 -168
  22. package/common/controls/Control.d.ts +74 -0
  23. package/common/controls/Control.d.ts.map +1 -0
  24. package/common/controls/Control.js +167 -78
  25. package/common/index.d.ts +3 -0
  26. package/common/index.d.ts.map +1 -0
  27. package/common/index.js +18 -2
  28. package/common/layers/Layer.d.ts +80 -0
  29. package/common/layers/Layer.d.ts.map +1 -0
  30. package/common/layers/Layer.js +251 -134
  31. package/common/mixins/CopyrightMixin.d.ts +22 -0
  32. package/common/mixins/CopyrightMixin.d.ts.map +1 -0
  33. package/common/mixins/CopyrightMixin.js +70 -22
  34. package/common/mixins/MapboxLayerMixin.d.ts +27 -0
  35. package/common/mixins/MapboxLayerMixin.d.ts.map +1 -0
  36. package/common/mixins/MapboxLayerMixin.js +240 -0
  37. package/common/mixins/RealtimeLayerMixin.d.ts +58 -0
  38. package/common/mixins/RealtimeLayerMixin.d.ts.map +1 -0
  39. package/common/mixins/RealtimeLayerMixin.js +698 -429
  40. package/common/mixins/StopFinderMixin.d.ts +40 -0
  41. package/common/mixins/StopFinderMixin.d.ts.map +1 -0
  42. package/common/mixins/StopFinderMixin.js +195 -107
  43. package/common/mixins/UserInteractionsLayerMixin.d.ts +42 -0
  44. package/common/mixins/UserInteractionsLayerMixin.d.ts.map +1 -0
  45. package/common/mixins/UserInteractionsLayerMixin.js +222 -121
  46. package/common/styles/index.d.ts +5 -0
  47. package/common/styles/index.d.ts.map +1 -0
  48. package/common/styles/index.js +24 -4
  49. package/common/styles/realtimeDefaultStyle.d.ts +15 -0
  50. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -0
  51. package/common/styles/realtimeDefaultStyle.js +236 -190
  52. package/common/styles/realtimeDelayStyle.d.ts +11 -0
  53. package/common/styles/realtimeDelayStyle.d.ts.map +1 -0
  54. package/common/styles/realtimeDelayStyle.js +25 -7
  55. package/common/styles/realtimeSimpleStyle.d.ts +3 -0
  56. package/common/styles/realtimeSimpleStyle.d.ts.map +1 -0
  57. package/common/styles/realtimeSimpleStyle.js +23 -17
  58. package/common/typedefs.d.ts +50 -0
  59. package/common/typedefs.d.ts.map +1 -0
  60. package/common/typedefs.js +21 -0
  61. package/common/utils/cleanStopTime.d.ts +7 -0
  62. package/common/utils/cleanStopTime.d.ts.map +1 -0
  63. package/common/utils/cleanStopTime.js +28 -17
  64. package/common/utils/compareDepartures.d.ts +9 -0
  65. package/common/utils/compareDepartures.d.ts.map +1 -0
  66. package/common/utils/compareDepartures.js +34 -22
  67. package/common/utils/createCanvas.d.ts +9 -0
  68. package/common/utils/createCanvas.d.ts.map +1 -0
  69. package/common/utils/createCanvas.js +28 -16
  70. package/common/utils/createTrackerFilters.d.ts +12 -0
  71. package/common/utils/createTrackerFilters.d.ts.map +1 -0
  72. package/common/utils/createTrackerFilters.js +75 -54
  73. package/common/utils/getLayersAsFlatArray.d.ts +3 -0
  74. package/common/utils/getLayersAsFlatArray.d.ts.map +1 -0
  75. package/common/utils/getLayersAsFlatArray.js +15 -13
  76. package/common/utils/getMapboxMapCopyrights.d.ts +8 -0
  77. package/common/utils/getMapboxMapCopyrights.d.ts.map +1 -0
  78. package/common/utils/getMapboxMapCopyrights.js +24 -17
  79. package/common/utils/getMapboxRender.d.ts +5 -0
  80. package/common/utils/getMapboxRender.d.ts.map +1 -0
  81. package/common/utils/getMapboxRender.js +77 -0
  82. package/common/utils/getMaplibreRender.d.ts +5 -0
  83. package/common/utils/getMaplibreRender.d.ts.map +1 -0
  84. package/common/utils/getMaplibreRender.js +38 -0
  85. package/common/utils/getRealtimeModeSuffix.d.ts +8 -0
  86. package/common/utils/getRealtimeModeSuffix.d.ts.map +1 -0
  87. package/common/utils/getRealtimeModeSuffix.js +11 -2
  88. package/common/utils/getUrlWithParams.d.ts +9 -0
  89. package/common/utils/getUrlWithParams.d.ts.map +1 -0
  90. package/common/utils/getUrlWithParams.js +20 -10
  91. package/common/utils/getVehiclePosition.d.ts +14 -0
  92. package/common/utils/getVehiclePosition.d.ts.map +1 -0
  93. package/common/utils/getVehiclePosition.js +63 -39
  94. package/common/utils/index.d.ts +12 -0
  95. package/common/utils/index.d.ts.map +1 -0
  96. package/common/utils/index.js +37 -9
  97. package/common/utils/removeDuplicate.d.ts +10 -0
  98. package/common/utils/removeDuplicate.d.ts.map +1 -0
  99. package/common/utils/removeDuplicate.js +29 -7
  100. package/common/utils/renderTrajectories.d.ts +20 -0
  101. package/common/utils/renderTrajectories.d.ts.map +1 -0
  102. package/common/utils/renderTrajectories.js +111 -78
  103. package/common/utils/sortByDelay.d.ts +3 -0
  104. package/common/utils/sortByDelay.d.ts.map +1 -0
  105. package/common/utils/sortByDelay.js +21 -17
  106. package/common/utils/timeUtils.d.ts +5 -0
  107. package/common/utils/timeUtils.d.ts.map +1 -0
  108. package/common/utils/timeUtils.js +47 -18
  109. package/common/utils/trackerConfig.d.ts +24 -0
  110. package/common/utils/trackerConfig.d.ts.map +1 -0
  111. package/common/utils/trackerConfig.js +171 -118
  112. package/iife.d.ts +3 -0
  113. package/iife.d.ts.map +1 -0
  114. package/iife.js +7 -0
  115. package/index.d.ts +6 -0
  116. package/index.d.ts.map +1 -0
  117. package/index.js +10 -7
  118. package/mapbox/controls/CopyrightControl.d.ts +32 -0
  119. package/mapbox/controls/CopyrightControl.d.ts.map +1 -0
  120. package/mapbox/controls/CopyrightControl.js +72 -28
  121. package/mapbox/controls/index.d.ts +2 -0
  122. package/mapbox/controls/index.d.ts.map +1 -0
  123. package/mapbox/controls/index.js +6 -1
  124. package/mapbox/index.d.ts +5 -0
  125. package/mapbox/index.d.ts.map +1 -0
  126. package/mapbox/index.js +20 -4
  127. package/mapbox/layers/Layer.d.ts +47 -0
  128. package/mapbox/layers/Layer.d.ts.map +1 -0
  129. package/mapbox/layers/Layer.js +137 -54
  130. package/mapbox/layers/RealtimeLayer.d.ts +118 -0
  131. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -0
  132. package/mapbox/layers/RealtimeLayer.js +310 -183
  133. package/mapbox/layers/index.d.ts +3 -0
  134. package/mapbox/layers/index.d.ts.map +1 -0
  135. package/mapbox/layers/index.js +7 -2
  136. package/mapbox/utils.d.ts +8 -0
  137. package/mapbox/utils.d.ts.map +1 -0
  138. package/mapbox/utils.js +54 -29
  139. package/mbt.js +2052 -2109
  140. package/mbt.js.map +3 -3
  141. package/mbt.min.js +18 -18
  142. package/mbt.min.js.map +3 -3
  143. package/ol/controls/CopyrightControl.d.ts +31 -0
  144. package/ol/controls/CopyrightControl.d.ts.map +1 -0
  145. package/ol/controls/CopyrightControl.js +89 -41
  146. package/ol/controls/RoutingControl.d.ts +180 -0
  147. package/ol/controls/RoutingControl.d.ts.map +1 -0
  148. package/ol/controls/RoutingControl.js +666 -371
  149. package/ol/controls/StopFinderControl.d.ts +32 -0
  150. package/ol/controls/StopFinderControl.d.ts.map +1 -0
  151. package/ol/controls/StopFinderControl.js +59 -10
  152. package/ol/controls/index.d.ts +4 -0
  153. package/ol/controls/index.d.ts.map +1 -0
  154. package/ol/controls/index.js +9 -3
  155. package/ol/index.d.ts +6 -0
  156. package/ol/index.d.ts.map +1 -0
  157. package/ol/index.js +21 -5
  158. package/ol/layers/Layer.d.ts +49 -0
  159. package/ol/layers/Layer.d.ts.map +1 -0
  160. package/ol/layers/Layer.js +178 -81
  161. package/ol/layers/MapboxLayer.d.ts +42 -0
  162. package/ol/layers/MapboxLayer.d.ts.map +1 -0
  163. package/ol/layers/MapboxLayer.js +131 -198
  164. package/ol/layers/MapboxStyleLayer.d.ts +146 -0
  165. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -0
  166. package/ol/layers/MapboxStyleLayer.js +382 -186
  167. package/ol/layers/MaplibreLayer.d.ts +27 -0
  168. package/ol/layers/MaplibreLayer.d.ts.map +1 -0
  169. package/ol/layers/MaplibreLayer.js +67 -138
  170. package/ol/layers/RealtimeLayer.d.ts +119 -0
  171. package/ol/layers/RealtimeLayer.d.ts.map +1 -0
  172. package/ol/layers/RealtimeLayer.js +327 -184
  173. package/ol/layers/RoutingLayer.d.ts +24 -0
  174. package/ol/layers/RoutingLayer.d.ts.map +1 -0
  175. package/ol/layers/RoutingLayer.js +111 -56
  176. package/ol/layers/VectorLayer.d.ts +23 -0
  177. package/ol/layers/VectorLayer.d.ts.map +1 -0
  178. package/ol/layers/VectorLayer.js +71 -21
  179. package/ol/layers/WMSLayer.d.ts +38 -0
  180. package/ol/layers/WMSLayer.d.ts.map +1 -0
  181. package/ol/layers/WMSLayer.js +105 -37
  182. package/ol/layers/index.d.ts +9 -0
  183. package/ol/layers/index.d.ts.map +1 -0
  184. package/ol/layers/index.js +19 -8
  185. package/ol/styles/fullTrajectoryDelayStyle.d.ts +4 -0
  186. package/ol/styles/fullTrajectoryDelayStyle.d.ts.map +1 -0
  187. package/ol/styles/fullTrajectoryDelayStyle.js +32 -28
  188. package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
  189. package/ol/styles/fullTrajectoryStyle.d.ts.map +1 -0
  190. package/ol/styles/fullTrajectoryStyle.js +44 -40
  191. package/ol/styles/index.d.ts +3 -0
  192. package/ol/styles/index.d.ts.map +1 -0
  193. package/ol/styles/index.js +7 -2
  194. package/package.json +14 -36
  195. package/setupTests.d.ts +2 -0
  196. package/setupTests.d.ts.map +1 -0
  197. package/setupTests.js +15 -0
  198. package/types/index.d.ts +7 -6
  199. package/types/routing.d.ts +25 -25
  200. package/types/stops.d.ts +19 -19
  201. package/api/RealtimeAPI.test.js +0 -67
  202. package/api/RoutingAPI.test.js +0 -25
  203. package/api/StopsAPI.test.js +0 -22
  204. package/common/api/HttpAPI.test.js +0 -50
  205. package/common/api/WebSocketAPI.test.js +0 -311
  206. package/common/controls/Control.test.js +0 -87
  207. package/common/layers/Layer.test.js +0 -134
  208. package/common/mixins/UserInteractionsLayerMixin.test.js +0 -199
  209. package/common/utils/createTrackerFilters.test.js +0 -79
  210. package/common/utils/getMapboxMapCopyrights.test.js +0 -40
  211. package/common/utils/removeDuplicate.test.js +0 -19
  212. package/common/utils/timeUtils.test.js +0 -10
  213. package/common/utils/trackerConfig.test.js +0 -23
  214. package/mapbox/layers/Layer.test.js +0 -182
  215. package/mapbox/layers/RealtimeLayer.test.js +0 -10
  216. package/ol/controls/CopyrightControl.test.js +0 -165
  217. package/ol/controls/RoutingControl.test.js +0 -146
  218. package/ol/controls/StopFinderControl.test.js +0 -48
  219. package/ol/layers/Layer.test.js +0 -175
  220. package/ol/layers/MapboxLayer.test.js +0 -160
  221. package/ol/layers/MapboxStyleLayer.test.js +0 -226
  222. package/ol/layers/RealtimeLayer.test.js +0 -67
  223. package/ol/layers/RoutingLayer.test.js +0 -39
  224. package/ol/layers/VectorLayer.test.js +0 -76
  225. package/ol/layers/WMSLayer.test.js +0 -57
@@ -1,185 +1,312 @@
1
- import { fromLonLat } from "ol/proj";
2
- import { unByKey } from "ol/Observable";
3
- import { getWidth, getHeight } from "ol/extent";
4
- import transformRotate from "@turf/transform-rotate";
5
- import { point } from "@turf/helpers";
6
- import mixin from "../../common/mixins/RealtimeLayerMixin";
7
- import Layer from "./Layer";
8
- import { getSourceCoordinates, getMercatorResolution } from "../utils";
9
- class RealtimeLayer extends mixin(Layer) {
10
- constructor(options = {}) {
11
- super({
12
- ...options
13
- });
14
- this.onLoad = this.onLoad.bind(this);
15
- this.onMove = this.onMove.bind(this);
16
- this.onMoveEnd = this.onMoveEnd.bind(this);
17
- this.onZoomEnd = this.onZoomEnd.bind(this);
18
- this.onVisibilityChange = this.onVisibilityChange.bind(this);
19
- }
20
- attachToMap(map, beforeId) {
21
- if (!map) {
22
- return;
23
- }
24
- const canvas = map.getCanvas();
25
- super.attachToMap(map, {
26
- width: canvas.width / this.pixelRatio,
27
- height: canvas.height / this.pixelRatio
28
- });
29
- this.source = {
30
- type: "canvas",
31
- canvas: this.canvas,
32
- coordinates: getSourceCoordinates(map, this.pixelRatio),
33
- animate: true,
34
- attribution: this.copyrights && this.copyrights.join(", ")
35
- };
36
- this.beforeId = beforeId;
37
- this.layer = {
38
- id: this.key,
39
- type: "raster",
40
- source: this.key,
41
- layout: {
42
- visibility: this.visible ? "visible" : "none"
43
- },
44
- paint: {
45
- "raster-opacity": 1,
46
- "raster-fade-duration": 0,
47
- "raster-resampling": "nearest"
48
- }
49
- };
50
- if (map.isStyleLoaded()) {
51
- this.onLoad();
52
- }
53
- this.map.on("load", this.onLoad);
54
- this.listeners = [this.on("change:visible", this.onVisibilityChange)];
55
- }
56
- detachFromMap() {
57
- if (this.map) {
58
- this.map.off("load", this.onLoad);
59
- this.listeners.forEach((listener) => {
60
- unByKey(listener);
61
- });
62
- if (this.map.getLayer(this.key)) {
63
- this.map.removeLayer(this.key);
64
- }
65
- if (this.map.getSource(this.key)) {
66
- this.map.removeSource(this.key);
67
- }
68
- }
69
- super.detachFromMap();
70
- }
71
- start() {
72
- super.start();
73
- this.map.on("move", this.onMove);
74
- this.map.on("moveend", this.onMoveEnd);
75
- this.map.on("zoomend", this.onZoomEnd);
76
- }
77
- stop() {
78
- super.stop();
79
- if (this.map) {
80
- this.map.off("move", this.onMove);
81
- this.map.off("moveend", this.onMoveEnd);
82
- this.map.off("zoomend", this.onZoomEnd);
83
- }
84
- }
85
- onLoad() {
86
- if (!this.map.getSource(this.key)) {
87
- this.map.addSource(this.key, this.source);
88
- }
89
- if (!this.map.getLayer(this.key)) {
90
- this.map.addLayer(this.layer, this.beforeId);
91
- }
92
- }
93
- onUserMoveCallback(evt) {
94
- super.onUserMoveCallback({
95
- coordinate: fromLonLat(evt.lngLat.toArray()),
96
- ...evt
97
- });
98
- }
99
- renderTrajectories(noInterpolate) {
100
- if (!this.map) {
101
- return;
102
- }
103
- const { width, height } = this.map.getCanvas();
104
- const center = this.map.getCenter();
105
- const leftBottom = this.map.unproject({
106
- x: 0,
107
- y: height / this.pixelRatio
108
- });
109
- const rightTop = this.map.unproject({ x: width / this.pixelRatio, y: 0 });
110
- const coord0 = transformRotate(point([leftBottom.lng, leftBottom.lat]), -this.map.getBearing(), {
111
- pivot: [center.lng, center.lat]
112
- }).geometry.coordinates;
113
- const coord1 = transformRotate(point([rightTop.lng, rightTop.lat]), -this.map.getBearing(), {
114
- pivot: [center.lng, center.lat]
115
- }).geometry.coordinates;
116
- const bounds = [...fromLonLat(coord0), ...fromLonLat(coord1)];
117
- const xResolution = getWidth(bounds) / (width / this.pixelRatio);
118
- const yResolution = getHeight(bounds) / (height / this.pixelRatio);
119
- const res = Math.max(xResolution, yResolution);
120
- const viewState = {
121
- size: [width / this.pixelRatio, height / this.pixelRatio],
122
- center: fromLonLat([center.lng, center.lat]),
123
- extent: bounds,
124
- resolution: res,
125
- zoom: this.map.getZoom(),
126
- rotation: -(this.map.getBearing() * Math.PI) / 180,
127
- pixelRatio: this.pixelRatio
128
- };
129
- super.renderTrajectories(viewState, noInterpolate);
130
- }
131
- getRefreshTimeInMs() {
132
- return super.getRefreshTimeInMs(this.map.getZoom());
133
- }
134
- getFeatureInfoAtCoordinate(coordinate, options = {}) {
135
- const resolution = getMercatorResolution(this.map);
136
- return super.getFeatureInfoAtCoordinate(coordinate, {
137
- resolution,
138
- ...options
139
- });
140
- }
141
- onVisibilityChange() {
142
- if (this.visible && !this.map.getLayer(this.key)) {
143
- this.map.addLayer(this.layer, this.beforeId);
144
- } else if (this.map.getLayer(this.key)) {
145
- this.map.removeLayer(this.key);
146
- }
147
- }
148
- purgeTrajectory(trajectory, extent, zoom) {
149
- return super.purgeTrajectory(trajectory, extent || this.getMercatorExtent(), zoom || Math.floor(this.map.getZoom() + 1));
150
- }
151
- setBbox(extent, zoom) {
152
- let newExtent = extent;
153
- let newZoom = zoom;
154
- if (!newExtent && this.isUpdateBboxOnMoveEnd) {
155
- newExtent = extent || this.getMercatorExtent();
156
- newZoom = Math.floor(this.getOlZoom());
157
- }
158
- super.setBbox(newExtent, newZoom);
159
- }
160
- onMove() {
161
- this.renderTrajectories();
162
- }
163
- renderTrajectoriesInternal(viewState, noInterpolate) {
164
- const render = super.renderTrajectoriesInternal(viewState, noInterpolate);
165
- if (render) {
166
- const extent = getSourceCoordinates(this.map, this.pixelRatio);
167
- const source = this.map.getSource(this.key);
168
- if (source) {
169
- source.setCoordinates(extent);
170
- }
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __assign = (this && this.__assign) || function () {
18
+ __assign = Object.assign || function(t) {
19
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
20
+ s = arguments[i];
21
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
+ t[p] = s[p];
23
+ }
24
+ return t;
25
+ };
26
+ return __assign.apply(this, arguments);
27
+ };
28
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
29
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
30
+ if (ar || !(i in from)) {
31
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
32
+ ar[i] = from[i];
33
+ }
171
34
  }
172
- return render;
173
- }
174
- onMoveEnd() {
175
- this.renderTrajectories();
176
- if (this.visible && this.isUpdateBboxOnMoveEnd) {
177
- this.setBbox();
35
+ return to.concat(ar || Array.prototype.slice.call(from));
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ var proj_1 = require("ol/proj");
39
+ var Observable_1 = require("ol/Observable");
40
+ var extent_1 = require("ol/extent");
41
+ var transform_rotate_1 = require("@turf/transform-rotate");
42
+ var helpers_1 = require("@turf/helpers");
43
+ var RealtimeLayerMixin_1 = require("../../common/mixins/RealtimeLayerMixin");
44
+ var Layer_1 = require("./Layer");
45
+ var utils_1 = require("../utils");
46
+ /**
47
+ * Responsible for loading and display data from a Realtime service.
48
+ *
49
+ * @example
50
+ * import { RealtimeLayer } from 'mobility-toolbox-js/mapbox';
51
+ *
52
+ * const layer = new RealtimeLayer({
53
+ * url: [yourUrl],
54
+ * apiKey: [yourApiKey],
55
+ * });
56
+ *
57
+ *
58
+ * @see <a href="/api/class/src/api/RealtimeAPI%20js~RealtimeAPI%20html">RealtimeAPI</a>
59
+ *
60
+ * @extends {Layer}
61
+ * @implements {RealtimeLayerInterface}
62
+ */
63
+ var RealtimeLayer = /** @class */ (function (_super) {
64
+ __extends(RealtimeLayer, _super);
65
+ function RealtimeLayer(options) {
66
+ if (options === void 0) { options = {}; }
67
+ var _this = _super.call(this, __assign({}, options)) || this;
68
+ /** @ignore */
69
+ _this.onLoad = _this.onLoad.bind(_this);
70
+ /** @ignore */
71
+ _this.onMove = _this.onMove.bind(_this);
72
+ /** @ignore */
73
+ _this.onMoveEnd = _this.onMoveEnd.bind(_this);
74
+ /** @ignore */
75
+ _this.onZoomEnd = _this.onZoomEnd.bind(_this);
76
+ /** @ignore */
77
+ _this.onVisibilityChange = _this.onVisibilityChange.bind(_this);
78
+ return _this;
178
79
  }
179
- }
180
- onFeatureHover(features, layer, coordinate) {
181
- super.onFeatureHover(features, layer, coordinate);
182
- this.map.getCanvasContainer().style.cursor = features.length ? "pointer" : "auto";
183
- }
184
- }
185
- export default RealtimeLayer;
80
+ /**
81
+ * Initialize the layer.
82
+ *
83
+ * @param {mapboxgl.Map} map A [mapbox Map](https://docs.mapbox.com/mapbox-gl-js/api/map/).
84
+ * @param {string} beforeId Layer's id before which we want to add the new layer.
85
+ * @override
86
+ */
87
+ RealtimeLayer.prototype.attachToMap = function (map, beforeId) {
88
+ if (!map) {
89
+ return;
90
+ }
91
+ var canvas = map.getCanvas();
92
+ _super.prototype.attachToMap.call(this, map, {
93
+ width: canvas.width / this.pixelRatio,
94
+ height: canvas.height / this.pixelRatio,
95
+ });
96
+ this.source = {
97
+ type: 'canvas',
98
+ canvas: this.canvas,
99
+ coordinates: (0, utils_1.getSourceCoordinates)(map, this.pixelRatio),
100
+ // Set to true if the canvas source is animated. If the canvas is static, animate should be set to false to improve performance.
101
+ animate: true,
102
+ attribution: this.copyrights && this.copyrights.join(', '),
103
+ };
104
+ this.beforeId = beforeId;
105
+ this.layer = {
106
+ id: this.key,
107
+ type: 'raster',
108
+ source: this.key,
109
+ layout: {
110
+ visibility: this.visible ? 'visible' : 'none',
111
+ },
112
+ paint: {
113
+ 'raster-opacity': 1,
114
+ 'raster-fade-duration': 0,
115
+ 'raster-resampling': 'nearest', // important otherwise it looks blurry
116
+ },
117
+ };
118
+ if (map.isStyleLoaded()) {
119
+ this.onLoad();
120
+ }
121
+ this.map.on('load', this.onLoad);
122
+ this.listeners = [this.on('change:visible', this.onVisibilityChange)];
123
+ };
124
+ /**
125
+ * Remove listeners from the Mapbox Map.
126
+ */
127
+ RealtimeLayer.prototype.detachFromMap = function () {
128
+ if (this.map) {
129
+ this.map.off('load', this.onLoad);
130
+ this.listeners.forEach(function (listener) {
131
+ (0, Observable_1.unByKey)(listener);
132
+ });
133
+ if (this.map.style && this.map.getLayer(this.key)) {
134
+ this.map.removeLayer(this.key);
135
+ }
136
+ if (this.map.style && this.map.getSource(this.key)) {
137
+ this.map.removeSource(this.key);
138
+ }
139
+ }
140
+ _super.prototype.detachFromMap.call(this);
141
+ };
142
+ /**
143
+ * Start updating vehicles position.
144
+ *
145
+ * @listens {mapboxgl.map.event:zoomend} Listen to zoom end event.
146
+ * @listens {mapboxgl.map.event:mousemove} Listen to mousemove end.
147
+ * @override
148
+ */
149
+ RealtimeLayer.prototype.start = function () {
150
+ _super.prototype.start.call(this);
151
+ this.map.on('move', this.onMove);
152
+ this.map.on('moveend', this.onMoveEnd);
153
+ this.map.on('zoomend', this.onZoomEnd);
154
+ };
155
+ /**
156
+ * Stop updating vehicles position, and unlisten events.
157
+ *
158
+ * @override
159
+ */
160
+ RealtimeLayer.prototype.stop = function () {
161
+ _super.prototype.stop.call(this);
162
+ if (this.map) {
163
+ this.map.off('move', this.onMove);
164
+ this.map.off('moveend', this.onMoveEnd);
165
+ this.map.off('zoomend', this.onZoomEnd);
166
+ }
167
+ };
168
+ RealtimeLayer.prototype.onLoad = function () {
169
+ if (!this.map.getSource(this.key)) {
170
+ this.map.addSource(this.key, this.source);
171
+ }
172
+ if (!this.map.getLayer(this.key)) {
173
+ this.map.addLayer(this.layer, this.beforeId);
174
+ }
175
+ };
176
+ /**
177
+ * Function triggered when the user moves the cursor over the map.
178
+ * @override
179
+ */
180
+ RealtimeLayer.prototype.onUserMoveCallback = function (evt) {
181
+ _super.prototype.onUserMoveCallback.call(this, __assign({ coordinate: (0, proj_1.fromLonLat)(evt.lngLat.toArray()) }, evt));
182
+ };
183
+ /**
184
+ * Render the trajectories using current map's size, resolution and rotation.
185
+ * @param {boolean} noInterpolate if true, renders the vehicles without interpolating theirs positions.
186
+ * @overrides
187
+ */
188
+ RealtimeLayer.prototype.renderTrajectories = function (noInterpolate) {
189
+ if (!this.map) {
190
+ return;
191
+ }
192
+ var _a = this.map.getCanvas(), width = _a.width, height = _a.height;
193
+ var center = this.map.getCenter();
194
+ // We use turf here to have good transform.
195
+ var leftBottom = this.map.unproject({
196
+ x: 0,
197
+ y: height / this.pixelRatio,
198
+ }); // southWest
199
+ var rightTop = this.map.unproject({ x: width / this.pixelRatio, y: 0 }); // north east
200
+ var coord0 = (0, transform_rotate_1.default)((0, helpers_1.point)([leftBottom.lng, leftBottom.lat]), -this.map.getBearing(), {
201
+ pivot: [center.lng, center.lat],
202
+ }).geometry.coordinates;
203
+ var coord1 = (0, transform_rotate_1.default)((0, helpers_1.point)([rightTop.lng, rightTop.lat]), -this.map.getBearing(), {
204
+ pivot: [center.lng, center.lat],
205
+ }).geometry.coordinates;
206
+ var bounds = __spreadArray(__spreadArray([], (0, proj_1.fromLonLat)(coord0), true), (0, proj_1.fromLonLat)(coord1), true);
207
+ var xResolution = (0, extent_1.getWidth)(bounds) / (width / this.pixelRatio);
208
+ var yResolution = (0, extent_1.getHeight)(bounds) / (height / this.pixelRatio);
209
+ var res = Math.max(xResolution, yResolution);
210
+ // Coordinate of trajectories are in mercator so we have to pass the proper resolution and center in mercator.
211
+ var viewState = {
212
+ size: [width / this.pixelRatio, height / this.pixelRatio],
213
+ center: (0, proj_1.fromLonLat)([center.lng, center.lat]),
214
+ extent: bounds,
215
+ resolution: res,
216
+ zoom: this.map.getZoom(),
217
+ rotation: -(this.map.getBearing() * Math.PI) / 180,
218
+ pixelRatio: this.pixelRatio,
219
+ };
220
+ _super.prototype.renderTrajectories.call(this, viewState, noInterpolate);
221
+ };
222
+ /**
223
+ * Return the delay in ms before the next rendering.
224
+ */
225
+ RealtimeLayer.prototype.getRefreshTimeInMs = function () {
226
+ return _super.prototype.getRefreshTimeInMs.call(this, this.map.getZoom());
227
+ };
228
+ RealtimeLayer.prototype.getFeatureInfoAtCoordinate = function (coordinate, options) {
229
+ if (options === void 0) { options = {}; }
230
+ var resolution = (0, utils_1.getMercatorResolution)(this.map);
231
+ return _super.prototype.getFeatureInfoAtCoordinate.call(this, coordinate, __assign({ resolution: resolution }, options));
232
+ };
233
+ RealtimeLayer.prototype.onVisibilityChange = function () {
234
+ if (this.visible && !this.map.getLayer(this.key)) {
235
+ this.map.addLayer(this.layer, this.beforeId);
236
+ }
237
+ else if (this.map.getLayer(this.key)) {
238
+ this.map.removeLayer(this.key);
239
+ }
240
+ // We can't use setLayoutProperty it triggers an error probably a bug in mapbox
241
+ // this.map.setLayoutProperty(
242
+ // this.key,
243
+ // 'visibilty',
244
+ // this.visible ? 'visible' : 'none',
245
+ // );
246
+ };
247
+ /**
248
+ * Remove the trajectory form the list if necessary.
249
+ *
250
+ * @private
251
+ */
252
+ RealtimeLayer.prototype.purgeTrajectory = function (trajectory, extent, zoom) {
253
+ return _super.prototype.purgeTrajectory.call(this, trajectory, extent || this.getMercatorExtent(), zoom || Math.floor(this.map.getZoom() + 1));
254
+ };
255
+ /**
256
+ * Send the current bbox to the websocket
257
+ */
258
+ RealtimeLayer.prototype.setBbox = function (extent, zoom) {
259
+ var newExtent = extent;
260
+ var newZoom = zoom;
261
+ if (!newExtent && this.isUpdateBboxOnMoveEnd) {
262
+ newExtent = extent || this.getMercatorExtent();
263
+ newZoom = Math.floor(this.getOlZoom());
264
+ }
265
+ _super.prototype.setBbox.call(this, newExtent, newZoom);
266
+ };
267
+ /**
268
+ * Callback on 'move' event.
269
+ *
270
+ * @private
271
+ */
272
+ RealtimeLayer.prototype.onMove = function () {
273
+ this.renderTrajectories();
274
+ };
275
+ RealtimeLayer.prototype.renderTrajectoriesInternal = function (viewState, noInterpolate) {
276
+ var render = _super.prototype.renderTrajectoriesInternal.call(this, viewState, noInterpolate);
277
+ if (render && this.map.style) {
278
+ var extent = (0, utils_1.getSourceCoordinates)(this.map, this.pixelRatio);
279
+ var source = this.map.getSource(this.key);
280
+ if (source) {
281
+ source.setCoordinates(extent);
282
+ }
283
+ }
284
+ return render;
285
+ };
286
+ /**
287
+ * Send the new BBOX to the websocket.
288
+ *
289
+ * @private
290
+ * @override
291
+ */
292
+ RealtimeLayer.prototype.onMoveEnd = function () {
293
+ this.renderTrajectories();
294
+ if (this.visible && this.isUpdateBboxOnMoveEnd) {
295
+ this.setBbox();
296
+ }
297
+ };
298
+ /**
299
+ * Update the cursor style when hovering a vehicle.
300
+ *
301
+ * @private
302
+ * @override
303
+ */
304
+ RealtimeLayer.prototype.onFeatureHover = function (features, layer, coordinate) {
305
+ _super.prototype.onFeatureHover.call(this, features, layer, coordinate);
306
+ this.map.getCanvasContainer().style.cursor = features.length
307
+ ? 'pointer'
308
+ : 'auto';
309
+ };
310
+ return RealtimeLayer;
311
+ }((0, RealtimeLayerMixin_1.default)(Layer_1.default)));
312
+ exports.default = RealtimeLayer;
@@ -0,0 +1,3 @@
1
+ export { default as Layer } from "./Layer";
2
+ export { default as RealtimeLayer } from "./RealtimeLayer";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/mapbox/layers/index.js"],"names":[],"mappings":""}
@@ -1,2 +1,7 @@
1
- export { default as Layer } from "./Layer";
2
- export { default as RealtimeLayer } from "./RealtimeLayer";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RealtimeLayer = exports.Layer = void 0;
4
+ var Layer_1 = require("./Layer");
5
+ Object.defineProperty(exports, "Layer", { enumerable: true, get: function () { return Layer_1.default; } });
6
+ var RealtimeLayer_1 = require("./RealtimeLayer");
7
+ Object.defineProperty(exports, "RealtimeLayer", { enumerable: true, get: function () { return RealtimeLayer_1.default; } });
@@ -0,0 +1,8 @@
1
+ export function getMercatorResolution(map: mapboxgl.Map): number;
2
+ export function getSourceCoordinates(map: mapboxgl.Map, pixelRatio: any): any[][];
3
+ declare namespace _default {
4
+ export { getMercatorResolution };
5
+ export { getSourceCoordinates };
6
+ }
7
+ export default _default;
8
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/mapbox/utils.js"],"names":[],"mappings":"AAQO,iEASN;AAOM,kFAgBN"}
package/mapbox/utils.js CHANGED
@@ -1,32 +1,57 @@
1
- import { getWidth, getHeight } from "ol/extent";
2
- import { fromLonLat } from "ol/proj";
3
- export const getMercatorResolution = (map) => {
4
- const bounds = map.getBounds().toArray();
5
- const a = fromLonLat(bounds[0]);
6
- const b = fromLonLat(bounds[1]);
7
- const extent = [...a, ...b];
8
- const { width, height } = map.getCanvas();
9
- const xResolution = getWidth(extent) / width;
10
- const yResolution = getHeight(extent) / height;
11
- return Math.max(xResolution, yResolution);
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
12
10
  };
13
- export const getSourceCoordinates = (map, pixelRatio) => {
14
- const { width, height } = map.getCanvas();
15
- const leftTop = map.unproject({ x: 0, y: 0 });
16
- const leftBottom = map.unproject({ x: 0, y: height / pixelRatio });
17
- const rightBottom = map.unproject({
18
- x: width / pixelRatio,
19
- y: height / pixelRatio
20
- });
21
- const rightTop = map.unproject({ x: width / pixelRatio, y: 0 });
22
- return [
23
- [leftTop.lng, leftTop.lat],
24
- [rightTop.lng, rightTop.lat],
25
- [rightBottom.lng, rightBottom.lat],
26
- [leftBottom.lng, leftBottom.lat]
27
- ];
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.getSourceCoordinates = exports.getMercatorResolution = void 0;
13
+ var extent_1 = require("ol/extent");
14
+ var proj_1 = require("ol/proj");
15
+ /**
16
+ * Get the current resolution of a Mapbox map.
17
+ * @param {mapboxgl.Map} map A map object.
18
+ * @private
19
+ */
20
+ var getMercatorResolution = function (map) {
21
+ var bounds = map.getBounds().toArray();
22
+ var a = (0, proj_1.fromLonLat)(bounds[0]);
23
+ var b = (0, proj_1.fromLonLat)(bounds[1]);
24
+ var extent = __spreadArray(__spreadArray([], a, true), b, true);
25
+ var _a = map.getCanvas(), width = _a.width, height = _a.height;
26
+ var xResolution = (0, extent_1.getWidth)(extent) / width;
27
+ var yResolution = (0, extent_1.getHeight)(extent) / height;
28
+ return Math.max(xResolution, yResolution);
28
29
  };
29
- export default {
30
- getMercatorResolution,
31
- getSourceCoordinates
30
+ exports.getMercatorResolution = getMercatorResolution;
31
+ /**
32
+ * Get the canvas source coordinates of the current map's extent.
33
+ * @param {mapboxgl.Map} map A map object.
34
+ * @private
35
+ */
36
+ var getSourceCoordinates = function (map, pixelRatio) {
37
+ // Requesting getBounds is not enough when we rotate the map, so we request manually each corner.
38
+ var _a = map.getCanvas(), width = _a.width, height = _a.height;
39
+ var leftTop = map.unproject({ x: 0, y: 0 });
40
+ var leftBottom = map.unproject({ x: 0, y: height / pixelRatio }); // southWest
41
+ var rightBottom = map.unproject({
42
+ x: width / pixelRatio,
43
+ y: height / pixelRatio,
44
+ });
45
+ var rightTop = map.unproject({ x: width / pixelRatio, y: 0 }); // north east
46
+ return [
47
+ [leftTop.lng, leftTop.lat],
48
+ [rightTop.lng, rightTop.lat],
49
+ [rightBottom.lng, rightBottom.lat],
50
+ [leftBottom.lng, leftBottom.lat],
51
+ ];
52
+ };
53
+ exports.getSourceCoordinates = getSourceCoordinates;
54
+ exports.default = {
55
+ getMercatorResolution: exports.getMercatorResolution,
56
+ getSourceCoordinates: exports.getSourceCoordinates,
32
57
  };