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

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 (195) hide show
  1. package/README.md +14 -8
  2. package/api/RealtimeAPI.d.ts +6 -6
  3. package/api/RealtimeAPI.d.ts.map +1 -1
  4. package/api/RealtimeAPI.js +170 -197
  5. package/api/RealtimeAPI.test.d.ts +2 -0
  6. package/api/RealtimeAPI.test.d.ts.map +1 -0
  7. package/api/RealtimeAPI.test.js +67 -0
  8. package/api/RoutingAPI.d.ts +6 -2
  9. package/api/RoutingAPI.d.ts.map +1 -1
  10. package/api/RoutingAPI.js +8 -38
  11. package/api/RoutingAPI.test.d.ts +2 -0
  12. package/api/RoutingAPI.test.d.ts.map +1 -0
  13. package/api/RoutingAPI.test.js +29 -0
  14. package/api/StopsAPI.d.ts +1 -1
  15. package/api/StopsAPI.d.ts.map +1 -1
  16. package/api/StopsAPI.js +8 -40
  17. package/api/StopsAPI.test.d.ts +2 -0
  18. package/api/StopsAPI.test.d.ts.map +1 -0
  19. package/api/StopsAPI.test.js +26 -0
  20. package/api/index.d.ts +3 -4
  21. package/api/index.d.ts.map +1 -1
  22. package/api/index.js +3 -10
  23. package/api/typedefs.js +1 -0
  24. package/common/api/HttpAPI.d.ts +2 -2
  25. package/common/api/HttpAPI.d.ts.map +1 -1
  26. package/common/api/HttpAPI.js +21 -48
  27. package/common/api/HttpAPI.test.d.ts +2 -0
  28. package/common/api/HttpAPI.test.d.ts.map +1 -0
  29. package/common/api/HttpAPI.test.js +54 -0
  30. package/common/api/WebSocketAPI.d.ts +8 -4
  31. package/common/api/WebSocketAPI.d.ts.map +1 -1
  32. package/common/api/WebSocketAPI.js +125 -129
  33. package/common/api/WebSocketAPI.test.d.ts +2 -0
  34. package/common/api/WebSocketAPI.test.d.ts.map +1 -0
  35. package/common/api/WebSocketAPI.test.js +380 -0
  36. package/common/controls/Control.d.ts +5 -5
  37. package/common/controls/Control.d.ts.map +1 -1
  38. package/common/controls/Control.js +44 -77
  39. package/common/controls/Control.test.d.ts +2 -0
  40. package/common/controls/Control.test.d.ts.map +1 -0
  41. package/common/controls/Control.test.js +89 -0
  42. package/common/index.js +2 -18
  43. package/common/layers/Layer.d.ts +11 -11
  44. package/common/layers/Layer.d.ts.map +1 -1
  45. package/common/layers/Layer.js +70 -104
  46. package/common/layers/Layer.test.d.ts +2 -0
  47. package/common/layers/Layer.test.d.ts.map +1 -0
  48. package/common/layers/Layer.test.js +137 -0
  49. package/common/mixins/CopyrightMixin.js +23 -52
  50. package/common/mixins/MapboxLayerMixin.js +162 -204
  51. package/common/mixins/RealtimeLayerMixin.js +580 -635
  52. package/common/mixins/StopFinderMixin.d.ts +3 -3
  53. package/common/mixins/StopFinderMixin.d.ts.map +1 -1
  54. package/common/mixins/StopFinderMixin.js +115 -157
  55. package/common/mixins/UserInteractionsLayerMixin.js +142 -175
  56. package/common/mixins/UserInteractionsLayerMixin.test.d.ts +2 -0
  57. package/common/mixins/UserInteractionsLayerMixin.test.d.ts.map +1 -0
  58. package/common/mixins/UserInteractionsLayerMixin.test.js +214 -0
  59. package/common/styles/index.js +4 -24
  60. package/common/styles/realtimeDefaultStyle.d.ts.map +1 -1
  61. package/common/styles/realtimeDefaultStyle.js +67 -78
  62. package/common/styles/realtimeDelayStyle.js +4 -17
  63. package/common/styles/realtimeSimpleStyle.js +5 -7
  64. package/common/typedefs.js +1 -0
  65. package/common/utils/cleanStopTime.js +3 -5
  66. package/common/utils/compareDepartures.d.ts +1 -1
  67. package/common/utils/compareDepartures.d.ts.map +1 -1
  68. package/common/utils/compareDepartures.js +8 -11
  69. package/common/utils/createCanvas.js +3 -5
  70. package/common/utils/createTrackerFilters.d.ts +1 -1
  71. package/common/utils/createTrackerFilters.d.ts.map +1 -1
  72. package/common/utils/createTrackerFilters.js +22 -32
  73. package/common/utils/createTrackerFilters.test.d.ts +2 -0
  74. package/common/utils/createTrackerFilters.test.d.ts.map +1 -0
  75. package/common/utils/createTrackerFilters.test.js +79 -0
  76. package/common/utils/getLayersAsFlatArray.js +6 -8
  77. package/common/utils/getMapboxMapCopyrights.js +9 -11
  78. package/common/utils/getMapboxMapCopyrights.test.d.ts +2 -0
  79. package/common/utils/getMapboxMapCopyrights.test.d.ts.map +1 -0
  80. package/common/utils/getMapboxMapCopyrights.test.js +40 -0
  81. package/common/utils/getMapboxRender.js +12 -15
  82. package/common/utils/getMaplibreRender.js +10 -13
  83. package/common/utils/getRealtimeModeSuffix.js +2 -6
  84. package/common/utils/getUrlWithParams.js +5 -8
  85. package/common/utils/getVehiclePosition.js +20 -23
  86. package/common/utils/index.js +12 -37
  87. package/common/utils/removeDuplicate.d.ts +1 -1
  88. package/common/utils/removeDuplicate.d.ts.map +1 -1
  89. package/common/utils/removeDuplicate.js +6 -21
  90. package/common/utils/removeDuplicate.test.d.ts +2 -0
  91. package/common/utils/removeDuplicate.test.d.ts.map +1 -0
  92. package/common/utils/removeDuplicate.test.js +19 -0
  93. package/common/utils/renderTrajectories.js +31 -43
  94. package/common/utils/sortByDelay.js +4 -6
  95. package/common/utils/timeUtils.js +14 -24
  96. package/common/utils/timeUtils.test.d.ts +2 -0
  97. package/common/utils/timeUtils.test.d.ts.map +1 -0
  98. package/common/utils/timeUtils.test.js +10 -0
  99. package/common/utils/trackerConfig.js +27 -39
  100. package/common/utils/trackerConfig.test.d.ts +2 -0
  101. package/common/utils/trackerConfig.test.d.ts.map +1 -0
  102. package/common/utils/trackerConfig.test.js +23 -0
  103. package/iife.js +3 -5
  104. package/index.d.ts +4 -0
  105. package/index.js +9 -10
  106. package/mapbox/controls/CopyrightControl.d.ts +0 -1
  107. package/mapbox/controls/CopyrightControl.d.ts.map +1 -1
  108. package/mapbox/controls/CopyrightControl.js +18 -38
  109. package/mapbox/controls/index.js +1 -5
  110. package/mapbox/index.js +4 -20
  111. package/mapbox/layers/Layer.d.ts +1 -1
  112. package/mapbox/layers/Layer.d.ts.map +1 -1
  113. package/mapbox/layers/Layer.js +29 -71
  114. package/mapbox/layers/Layer.test.d.ts +2 -0
  115. package/mapbox/layers/Layer.test.d.ts.map +1 -0
  116. package/mapbox/layers/Layer.test.js +204 -0
  117. package/mapbox/layers/RealtimeLayer.d.ts +4 -4
  118. package/mapbox/layers/RealtimeLayer.d.ts.map +1 -1
  119. package/mapbox/layers/RealtimeLayer.js +83 -125
  120. package/mapbox/layers/RealtimeLayer.test.d.ts +2 -0
  121. package/mapbox/layers/RealtimeLayer.test.d.ts.map +1 -0
  122. package/mapbox/layers/RealtimeLayer.test.js +10 -0
  123. package/mapbox/layers/index.js +2 -7
  124. package/mapbox/utils.js +19 -33
  125. package/mbt.js +59 -37
  126. package/mbt.js.map +2 -2
  127. package/mbt.min.js +10 -10
  128. package/mbt.min.js.map +2 -2
  129. package/ol/controls/CopyrightControl.js +26 -47
  130. package/ol/controls/CopyrightControl.test.d.ts +2 -0
  131. package/ol/controls/CopyrightControl.test.d.ts.map +1 -0
  132. package/ol/controls/CopyrightControl.test.js +177 -0
  133. package/ol/controls/RoutingControl.d.ts +6 -5
  134. package/ol/controls/RoutingControl.d.ts.map +1 -1
  135. package/ol/controls/RoutingControl.js +209 -270
  136. package/ol/controls/RoutingControl.test.d.ts +2 -0
  137. package/ol/controls/RoutingControl.test.d.ts.map +1 -0
  138. package/ol/controls/RoutingControl.test.js +150 -0
  139. package/ol/controls/StopFinderControl.js +9 -32
  140. package/ol/controls/StopFinderControl.test.d.ts +2 -0
  141. package/ol/controls/StopFinderControl.test.d.ts.map +1 -0
  142. package/ol/controls/StopFinderControl.test.js +49 -0
  143. package/ol/controls/index.js +3 -9
  144. package/ol/index.js +5 -21
  145. package/ol/layers/Layer.d.ts +1 -1
  146. package/ol/layers/Layer.d.ts.map +1 -1
  147. package/ol/layers/Layer.js +40 -72
  148. package/ol/layers/Layer.test.d.ts +2 -0
  149. package/ol/layers/Layer.test.d.ts.map +1 -0
  150. package/ol/layers/Layer.test.js +196 -0
  151. package/ol/layers/MapboxLayer.d.ts +7 -7
  152. package/ol/layers/MapboxLayer.d.ts.map +1 -1
  153. package/ol/layers/MapboxLayer.js +30 -66
  154. package/ol/layers/MapboxLayer.test.d.ts +2 -0
  155. package/ol/layers/MapboxLayer.test.d.ts.map +1 -0
  156. package/ol/layers/MapboxLayer.test.js +164 -0
  157. package/ol/layers/MapboxStyleLayer.d.ts +3 -3
  158. package/ol/layers/MapboxStyleLayer.d.ts.map +1 -1
  159. package/ol/layers/MapboxStyleLayer.js +92 -135
  160. package/ol/layers/MapboxStyleLayer.test.d.ts +2 -0
  161. package/ol/layers/MapboxStyleLayer.test.d.ts.map +1 -0
  162. package/ol/layers/MapboxStyleLayer.test.js +232 -0
  163. package/ol/layers/MaplibreLayer.d.ts +1 -1
  164. package/ol/layers/MaplibreLayer.d.ts.map +1 -1
  165. package/ol/layers/MaplibreLayer.js +14 -48
  166. package/ol/layers/RealtimeLayer.d.ts +2 -2
  167. package/ol/layers/RealtimeLayer.d.ts.map +1 -1
  168. package/ol/layers/RealtimeLayer.js +111 -147
  169. package/ol/layers/RealtimeLayer.test.d.ts +2 -0
  170. package/ol/layers/RealtimeLayer.test.d.ts.map +1 -0
  171. package/ol/layers/RealtimeLayer.test.js +71 -0
  172. package/ol/layers/RoutingLayer.d.ts +2 -2
  173. package/ol/layers/RoutingLayer.d.ts.map +1 -1
  174. package/ol/layers/RoutingLayer.js +29 -61
  175. package/ol/layers/RoutingLayer.test.d.ts +2 -0
  176. package/ol/layers/RoutingLayer.test.d.ts.map +1 -0
  177. package/ol/layers/RoutingLayer.test.js +39 -0
  178. package/ol/layers/VectorLayer.d.ts +1 -1
  179. package/ol/layers/VectorLayer.d.ts.map +1 -1
  180. package/ol/layers/VectorLayer.js +14 -48
  181. package/ol/layers/VectorLayer.test.d.ts +2 -0
  182. package/ol/layers/VectorLayer.test.d.ts.map +1 -0
  183. package/ol/layers/VectorLayer.test.js +87 -0
  184. package/ol/layers/WMSLayer.d.ts +1 -1
  185. package/ol/layers/WMSLayer.d.ts.map +1 -1
  186. package/ol/layers/WMSLayer.js +34 -68
  187. package/ol/layers/WMSLayer.test.d.ts +2 -0
  188. package/ol/layers/WMSLayer.test.d.ts.map +1 -0
  189. package/ol/layers/WMSLayer.test.js +66 -0
  190. package/ol/layers/index.js +8 -19
  191. package/ol/styles/fullTrajectoryDelayStyle.js +11 -13
  192. package/ol/styles/fullTrajectoryStyle.js +16 -18
  193. package/ol/styles/index.js +2 -7
  194. package/package.json +5 -2
  195. package/setupTests.js +12 -14
@@ -1,39 +1,17 @@
1
- "use strict";
2
1
  /* eslint-disable class-methods-use-this */
3
2
  /* eslint-disable max-classes-per-file */
4
- var __extends = (this && this.__extends) || (function () {
5
- var extendStatics = function (d, b) {
6
- extendStatics = Object.setPrototypeOf ||
7
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
8
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
9
- return extendStatics(d, b);
10
- };
11
- return function (d, b) {
12
- if (typeof b !== "function" && b !== null)
13
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
14
- extendStatics(d, b);
15
- function __() { this.constructor = d; }
16
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
17
- };
18
- })();
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.CopyrightInterface = void 0;
21
3
  /**
22
4
  * Copyright control interface.
23
5
  *
24
6
  */
25
- var CopyrightInterface = /** @class */ (function () {
26
- function CopyrightInterface() {
27
- }
7
+ export class CopyrightInterface {
28
8
  /**
29
9
  * Return an array of layer's copyright.
30
10
  *
31
11
  * @return {String[]} A list of copyrights to render.
32
12
  */
33
- CopyrightInterface.prototype.getCopyrights = function () { };
34
- return CopyrightInterface;
35
- }());
36
- exports.CopyrightInterface = CopyrightInterface;
13
+ getCopyrights() { }
14
+ }
37
15
  /**
38
16
  * Mixin for CopyrightInterface.
39
17
  *
@@ -41,32 +19,25 @@ exports.CopyrightInterface = CopyrightInterface;
41
19
  * @return {Class} A class that implements <CopyrightInterface> class and extends Base;
42
20
  * @private
43
21
  */
44
- var CopyrightMixin = function (Base) {
45
- return /** @class */ (function (_super) {
46
- __extends(class_1, _super);
47
- function class_1() {
48
- return _super !== null && _super.apply(this, arguments) || this;
22
+ const CopyrightMixin = (Base) => class extends Base {
23
+ render() {
24
+ if (!this.element) {
25
+ return;
49
26
  }
50
- class_1.prototype.render = function () {
51
- if (!this.element) {
52
- return;
53
- }
54
- this.element.innerHTML = this.active
55
- ? this.getCopyrights().join(' | ')
56
- : '';
57
- };
58
- class_1.prototype.createDefaultElement = function () {
59
- this.element = document.createElement('div');
60
- this.element.id = 'mbt-copyright';
61
- Object.assign(this.element.style, {
62
- position: 'absolute',
63
- bottom: 0,
64
- right: 0,
65
- fontSize: '.8rem',
66
- padding: '0 10px',
67
- });
68
- };
69
- return class_1;
70
- }(Base));
27
+ this.element.innerHTML = this.active
28
+ ? this.getCopyrights().join(' | ')
29
+ : '';
30
+ }
31
+ createDefaultElement() {
32
+ this.element = document.createElement('div');
33
+ this.element.id = 'mbt-copyright';
34
+ Object.assign(this.element.style, {
35
+ position: 'absolute',
36
+ bottom: 0,
37
+ right: 0,
38
+ fontSize: '.8rem',
39
+ padding: '0 10px',
40
+ });
41
+ }
71
42
  };
72
- exports.default = CopyrightMixin;
43
+ export default CopyrightMixin;
@@ -1,60 +1,27 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.MapboxLayerInterface = void 0;
30
1
  /* eslint-disable max-classes-per-file */
31
- var proj_1 = require("ol/proj");
32
- var Layer_1 = require("ol/layer/Layer");
33
- var Source_1 = require("ol/source/Source");
34
- var GeoJSON_1 = require("ol/format/GeoJSON");
35
- var utils_1 = require("../utils");
2
+ import { toLonLat } from 'ol/proj';
3
+ import OLLayer from 'ol/layer/Layer';
4
+ import Source from 'ol/source/Source';
5
+ import GeoJSON from 'ol/format/GeoJSON';
6
+ import { getUrlWithParams, getMapboxMapCopyrights } from '../utils';
36
7
  /**
37
8
  * MapboxLayerInterface
38
9
  */
39
- var MapboxLayerInterface = /** @class */ (function () {
40
- function MapboxLayerInterface() {
41
- }
10
+ export class MapboxLayerInterface {
42
11
  /**
43
12
  * Return the render function function for the ol layer.
44
13
  *
45
14
  */
46
15
  // eslint-disable-next-line class-methods-use-this
47
- MapboxLayerInterface.prototype.getOlLayerRender = function () { };
16
+ getOlLayerRender() { }
48
17
  /**
49
18
  * Return the Class to instanciate for the mapbox map.
50
19
  *
51
20
  * @return {mapboxgl.Map|maplibregl.Map} map
52
21
  */
53
22
  // eslint-disable-next-line class-methods-use-this
54
- MapboxLayerInterface.prototype.getMapboxMapClass = function () { };
55
- return MapboxLayerInterface;
56
- }());
57
- exports.MapboxLayerInterface = MapboxLayerInterface;
23
+ getMapboxMapClass() { }
24
+ }
58
25
  /**
59
26
  * Mixin for MapboxLayerLayerInterface.
60
27
  * It's used to share code between Mapbox and Maplibre layers without importing both libs
@@ -63,178 +30,169 @@ exports.MapboxLayerInterface = MapboxLayerInterface;
63
30
  * @return {Class} A class that implements {MapboxLayerLayerInterface} class and extends Base;
64
31
  * @private
65
32
  */
66
- var MapboxLayerMixin = function (Base) {
67
- return /** @class */ (function (_super) {
68
- __extends(class_1, _super);
69
- function class_1(options) {
70
- if (options === void 0) { options = {}; }
71
- var _this = _super.call(this, options) || this;
72
- _this.olLayer = new Layer_1.default({
73
- source: new Source_1.default({}),
74
- render: _this.getOlLayerRender(_this),
75
- });
76
- /**
77
- * Url of the mapbox style.
78
- * @type {string}
79
- * @private
80
- */
81
- _this.styleUrl = options.url;
82
- /**
83
- * Api key for the url of the mapbox style.
84
- * If set to false, the apiKey is not required.
85
- * @type {string}
86
- * @private
87
- */
88
- _this.apiKey = options.apiKey;
89
- /**
90
- * Name of the apiKey to set in the url request.
91
- * Default is 'key'.
92
- * @type {string}
93
- * @private
94
- */
95
- _this.apiKeyName = options.apiKeyName || 'key';
96
- /** @ignore */
97
- _this.updateAttribution = _this.updateAttribution.bind(_this);
98
- return _this;
99
- }
33
+ const MapboxLayerMixin = (Base) => class extends Base {
34
+ constructor(options = {}) {
35
+ super(options);
36
+ this.olLayer = new OLLayer({
37
+ source: new Source({}),
38
+ render: this.getOlLayerRender(this),
39
+ });
100
40
  /**
101
- * Initialize the layer and listen to feature clicks.
102
- * @param {ol/Map~Map} map
41
+ * Url of the mapbox style.
42
+ * @type {string}
43
+ * @private
103
44
  */
104
- class_1.prototype.attachToMap = function (map) {
105
- _super.prototype.attachToMap.call(this, map);
106
- if (!this.map) {
107
- return;
108
- }
109
- /**
110
- * The feature format.
111
- * @type {ol/format/GeoJSON}
112
- */
113
- this.format = new GeoJSON_1.default({
114
- featureProjection: this.map.getView().getProjection(),
115
- });
116
- this.loadMbMap();
117
- };
45
+ this.styleUrl = options.url;
118
46
  /**
119
- * Terminate what was initialized in init function. Remove layer, events...
47
+ * Api key for the url of the mapbox style.
48
+ * If set to false, the apiKey is not required.
49
+ * @type {string}
50
+ * @private
120
51
  */
121
- class_1.prototype.detachFromMap = function () {
122
- if (this.mbMap) {
123
- this.mbMap.off('idle', this.updateAttribution);
124
- // Some asynchrone repaints are triggered even if the mbMap has been removed,
125
- // to avoid display of errors we set an empty function.
126
- this.mbMap.triggerRepaint = function () { };
127
- this.mbMap.remove();
128
- this.mbMap = null;
129
- }
130
- this.loaded = false;
131
- _super.prototype.detachFromMap.call(this);
132
- };
52
+ this.apiKey = options.apiKey;
133
53
  /**
134
- * Create the mapbox map.
54
+ * Name of the apiKey to set in the url request.
55
+ * Default is 'key'.
56
+ * @type {string}
135
57
  * @private
136
58
  */
137
- class_1.prototype.loadMbMap = function () {
138
- var _a;
139
- var _this = this;
140
- this.olListenersKeys.push(this.map.on('change:target', function () {
141
- _this.loadMbMap();
142
- }));
143
- if (!this.map.getTargetElement()) {
144
- return;
145
- }
146
- if (!this.visible) {
147
- // On next change of visibility we load the map
148
- this.olListenersKeys.push(this.once('change:visible', function () {
149
- _this.loadMbMap();
150
- }));
151
- return;
152
- }
153
- var container = document.createElement('div');
154
- container.style.position = 'absolute';
155
- container.style.width = '100%';
156
- container.style.height = '100%';
157
- if (!this.apiKey && !this.styleUrl.includes(this.apiKeyName)) {
158
- // eslint-disable-next-line no-console
159
- console.error("No apiKey defined for mapbox layer with style url to ".concat(this.styleUrl));
160
- }
161
- var Map = this.getMapboxMapClass();
162
- /**
163
- * A mapbox map
164
- * @type {mapboxgl.Map}
165
- */
166
- this.mbMap = new Map(__assign({ style: (0, utils_1.getUrlWithParams)(this.styleUrl, (_a = {},
167
- _a[this.apiKeyName] = this.apiKey,
168
- _a)).toString(), container: container, interactive: false, trackResize: false, attributionControl: false }, (this.options.mapOptions || {})));
169
- this.mbMap.once('load', function () {
170
- /**
171
- * Is the map loaded.
172
- * @type {boolean}
173
- */
174
- _this.loaded = true;
175
- _this.dispatchEvent({
176
- type: 'load',
177
- target: _this,
178
- });
179
- });
180
- this.mbMap.on('idle', this.updateAttribution);
181
- };
59
+ this.apiKeyName = options.apiKeyName || 'key';
60
+ /** @ignore */
61
+ this.updateAttribution = this.updateAttribution.bind(this);
62
+ }
63
+ /**
64
+ * Initialize the layer and listen to feature clicks.
65
+ * @param {ol/Map~Map} map
66
+ */
67
+ attachToMap(map) {
68
+ super.attachToMap(map);
69
+ if (!this.map) {
70
+ return;
71
+ }
182
72
  /**
183
- * Update attributions of the source.
184
- * @private
73
+ * The feature format.
74
+ * @type {ol/format/GeoJSON}
185
75
  */
186
- class_1.prototype.updateAttribution = function (evt) {
187
- var _a, _b;
188
- var newAttributions = (0, utils_1.getMapboxMapCopyrights)(evt.target) || [];
189
- if (((_a = this.copyrights) === null || _a === void 0 ? void 0 : _a.toString()) !== newAttributions.toString()) {
190
- this.copyrights = newAttributions;
191
- (_b = this.olLayer.getSource()) === null || _b === void 0 ? void 0 : _b.setAttributions(newAttributions);
192
- }
193
- };
76
+ this.format = new GeoJSON({
77
+ featureProjection: this.map.getView().getProjection(),
78
+ });
79
+ this.loadMbMap();
80
+ }
81
+ /**
82
+ * Terminate what was initialized in init function. Remove layer, events...
83
+ */
84
+ detachFromMap() {
85
+ if (this.mbMap) {
86
+ this.mbMap.off('idle', this.updateAttribution);
87
+ // Some asynchrone repaints are triggered even if the mbMap has been removed,
88
+ // to avoid display of errors we set an empty function.
89
+ this.mbMap.triggerRepaint = () => { };
90
+ this.mbMap.remove();
91
+ this.mbMap = null;
92
+ }
93
+ this.loaded = false;
94
+ super.detachFromMap();
95
+ }
96
+ /**
97
+ * Create the mapbox map.
98
+ * @private
99
+ */
100
+ loadMbMap() {
101
+ this.olListenersKeys.push(this.map.on('change:target', () => {
102
+ this.loadMbMap();
103
+ }));
104
+ if (!this.map.getTargetElement()) {
105
+ return;
106
+ }
107
+ if (!this.visible) {
108
+ // On next change of visibility we load the map
109
+ this.olListenersKeys.push(this.once('change:visible', () => {
110
+ this.loadMbMap();
111
+ }));
112
+ return;
113
+ }
114
+ const container = document.createElement('div');
115
+ container.style.position = 'absolute';
116
+ container.style.width = '100%';
117
+ container.style.height = '100%';
118
+ if (!this.apiKey && !this.styleUrl.includes(this.apiKeyName)) {
119
+ // eslint-disable-next-line no-console
120
+ console.error(`No apiKey defined for mapbox layer with style url to ${this.styleUrl}`);
121
+ }
122
+ const Map = this.getMapboxMapClass();
194
123
  /**
195
- * Request feature information for a given coordinate.
196
- * @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
197
- * @param {Object} options A [mapboxgl.Map#queryrenderedfeatures](https://docs.mapbox.com/mapbox-gl-js/api/map/#map#queryrenderedfeatures) options parameter.
198
- * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate. The original Mapbox feature is available as a property named 'mapboxFeature'.
124
+ * A mapbox map
125
+ * @type {mapboxgl.Map}
199
126
  */
200
- class_1.prototype.getFeatureInfoAtCoordinate = function (coordinate, options) {
201
- var _this = this;
202
- // Ignore the getFeatureInfo until the mapbox map is loaded
203
- if (!options ||
204
- !this.format ||
205
- !this.mbMap ||
206
- !this.mbMap.isStyleLoaded()) {
207
- return Promise.resolve({ coordinate: coordinate, features: [], layer: this });
208
- }
209
- var pixel = coordinate && this.mbMap.project((0, proj_1.toLonLat)(coordinate));
210
- if (this.hitTolerance) {
211
- var x = pixel.x, y = pixel.y;
212
- pixel = [
213
- { x: x - this.hitTolerance, y: y - this.hitTolerance },
214
- { x: x + this.hitTolerance, y: y + this.hitTolerance },
215
- ];
216
- }
217
- // At this point we get GeoJSON Mapbox feature, we transform it to an OpenLayers
218
- // feature to be consistent with other layers.
219
- var features = this.mbMap
220
- .queryRenderedFeatures(pixel, options)
221
- .map(function (feature) {
222
- var olFeature = _this.format.readFeature(feature);
223
- if (olFeature) {
224
- // We save the original mapbox feature to avoid losing informations
225
- // potentially needed for other functionnality like highlighting
226
- // (id, layer id, source, sourceLayer ...)
227
- olFeature.set('mapboxFeature', feature);
228
- }
229
- return olFeature;
230
- });
231
- return Promise.resolve({
232
- layer: this,
233
- features: features,
234
- coordinate: coordinate,
127
+ this.mbMap = new Map(Object.assign({ style: getUrlWithParams(this.styleUrl, {
128
+ [this.apiKeyName]: this.apiKey,
129
+ }).toString(), container, interactive: false, trackResize: false, attributionControl: false }, (this.options.mapOptions || {})));
130
+ this.mbMap.once('load', () => {
131
+ /**
132
+ * Is the map loaded.
133
+ * @type {boolean}
134
+ */
135
+ this.loaded = true;
136
+ this.dispatchEvent({
137
+ type: 'load',
138
+ target: this,
235
139
  });
236
- };
237
- return class_1;
238
- }(Base));
140
+ });
141
+ this.mbMap.on('idle', this.updateAttribution);
142
+ }
143
+ /**
144
+ * Update attributions of the source.
145
+ * @private
146
+ */
147
+ updateAttribution(evt) {
148
+ var _a, _b;
149
+ const newAttributions = getMapboxMapCopyrights(evt.target) || [];
150
+ if (((_a = this.copyrights) === null || _a === void 0 ? void 0 : _a.toString()) !== newAttributions.toString()) {
151
+ this.copyrights = newAttributions;
152
+ (_b = this.olLayer.getSource()) === null || _b === void 0 ? void 0 : _b.setAttributions(newAttributions);
153
+ }
154
+ }
155
+ /**
156
+ * Request feature information for a given coordinate.
157
+ * @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
158
+ * @param {Object} options A [mapboxgl.Map#queryrenderedfeatures](https://docs.mapbox.com/mapbox-gl-js/api/map/#map#queryrenderedfeatures) options parameter.
159
+ * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate. The original Mapbox feature is available as a property named 'mapboxFeature'.
160
+ */
161
+ getFeatureInfoAtCoordinate(coordinate, options) {
162
+ // Ignore the getFeatureInfo until the mapbox map is loaded
163
+ if (!options ||
164
+ !this.format ||
165
+ !this.mbMap ||
166
+ !this.mbMap.isStyleLoaded()) {
167
+ return Promise.resolve({ coordinate, features: [], layer: this });
168
+ }
169
+ let pixel = coordinate && this.mbMap.project(toLonLat(coordinate));
170
+ if (this.hitTolerance) {
171
+ const { x, y } = pixel;
172
+ pixel = [
173
+ { x: x - this.hitTolerance, y: y - this.hitTolerance },
174
+ { x: x + this.hitTolerance, y: y + this.hitTolerance },
175
+ ];
176
+ }
177
+ // At this point we get GeoJSON Mapbox feature, we transform it to an OpenLayers
178
+ // feature to be consistent with other layers.
179
+ const features = this.mbMap
180
+ .queryRenderedFeatures(pixel, options)
181
+ .map((feature) => {
182
+ const olFeature = this.format.readFeature(feature);
183
+ if (olFeature) {
184
+ // We save the original mapbox feature to avoid losing informations
185
+ // potentially needed for other functionnality like highlighting
186
+ // (id, layer id, source, sourceLayer ...)
187
+ olFeature.set('mapboxFeature', feature);
188
+ }
189
+ return olFeature;
190
+ });
191
+ return Promise.resolve({
192
+ layer: this,
193
+ features,
194
+ coordinate,
195
+ });
196
+ }
239
197
  };
240
- exports.default = MapboxLayerMixin;
198
+ export default MapboxLayerMixin;