mobility-toolbox-js 3.0.0-beta.9 → 3.0.0

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 (105) hide show
  1. package/README.md +11 -3
  2. package/api/HttpAPI.d.ts +5 -16
  3. package/api/HttpAPI.js +1 -14
  4. package/api/RealtimeAPI.d.ts +188 -143
  5. package/api/RealtimeAPI.js +265 -206
  6. package/api/RoutingAPI.d.ts +21 -11
  7. package/api/RoutingAPI.js +17 -7
  8. package/api/StopsAPI.d.ts +20 -14
  9. package/api/StopsAPI.js +17 -11
  10. package/api/WebSocketAPI.d.ts +60 -66
  11. package/api/WebSocketAPI.js +164 -165
  12. package/api/index.js +1 -1
  13. package/api/typedefs.d.ts +0 -102
  14. package/api/typedefs.js +27 -42
  15. package/common/controls/StopFinderControlCommon.d.ts +13 -13
  16. package/common/controls/StopFinderControlCommon.js +32 -32
  17. package/common/index.d.ts +1 -1
  18. package/common/index.js +1 -1
  19. package/common/styles/realtimeDefaultStyle.js +6 -11
  20. package/common/styles/realtimeHeadingStyle.js +5 -10
  21. package/common/styles/realtimeSimpleStyle.d.ts +0 -1
  22. package/common/styles/realtimeSimpleStyle.js +0 -1
  23. package/common/typedefs.d.ts +11 -121
  24. package/common/typedefs.js +6 -31
  25. package/common/utils/RealtimeEngine.d.ts +214 -0
  26. package/common/utils/RealtimeEngine.js +554 -0
  27. package/common/utils/compareDepartures.d.ts +2 -2
  28. package/common/utils/compareDepartures.js +2 -2
  29. package/common/utils/debounceWebsocketMessages.d.ts +1 -1
  30. package/common/utils/getLayersAsFlatArray.d.ts +0 -1
  31. package/common/utils/getLayersAsFlatArray.js +0 -1
  32. package/common/utils/getMapGlCopyrights.d.ts +1 -1
  33. package/common/utils/getMapGlCopyrights.js +3 -3
  34. package/common/utils/getRealtimeModeSuffix.d.ts +1 -0
  35. package/common/utils/getRealtimeModeSuffix.js +1 -0
  36. package/common/utils/getVehiclePosition.d.ts +5 -4
  37. package/common/utils/getVehiclePosition.js +6 -3
  38. package/common/utils/realtimeConfig.d.ts +1 -1
  39. package/common/utils/realtimeConfig.js +0 -1
  40. package/common/utils/renderTrajectories.d.ts +2 -1
  41. package/common/utils/renderTrajectories.js +7 -6
  42. package/common/utils/sortAndFilterDepartures.d.ts +2 -1
  43. package/common/utils/sortAndFilterDepartures.js +2 -1
  44. package/common/utils/sortByDelay.d.ts +2 -2
  45. package/common/utils/sortByDelay.js +5 -1
  46. package/maplibre/controls/CopyrightControl.d.ts +9 -6
  47. package/maplibre/controls/CopyrightControl.js +11 -8
  48. package/maplibre/layers/Layer.d.ts +8 -7
  49. package/maplibre/layers/Layer.js +2 -3
  50. package/maplibre/layers/RealtimeLayer.d.ts +82 -118
  51. package/maplibre/layers/RealtimeLayer.js +154 -118
  52. package/maplibre/utils/getSourceCoordinates.d.ts +1 -0
  53. package/maplibre/utils/getSourceCoordinates.js +6 -5
  54. package/mbt.js +14611 -14591
  55. package/mbt.js.map +4 -4
  56. package/mbt.min.js +75 -75
  57. package/mbt.min.js.map +4 -4
  58. package/ol/controls/CopyrightControl.d.ts +13 -5
  59. package/ol/controls/CopyrightControl.js +13 -5
  60. package/ol/controls/RoutingControl.d.ts +105 -101
  61. package/ol/controls/RoutingControl.js +250 -264
  62. package/ol/controls/StopFinderControl.d.ts +24 -5
  63. package/ol/controls/StopFinderControl.js +24 -5
  64. package/ol/layers/Layer.d.ts +26 -0
  65. package/ol/layers/Layer.js +39 -0
  66. package/ol/layers/MaplibreLayer.d.ts +56 -28
  67. package/ol/layers/MaplibreLayer.js +154 -31
  68. package/ol/layers/MaplibreStyleLayer.d.ts +71 -149
  69. package/ol/layers/MaplibreStyleLayer.js +281 -210
  70. package/ol/layers/RealtimeLayer.d.ts +95 -230
  71. package/ol/layers/RealtimeLayer.js +209 -211
  72. package/ol/layers/VectorLayer.d.ts +17 -0
  73. package/ol/layers/VectorLayer.js +33 -0
  74. package/ol/layers/index.d.ts +2 -0
  75. package/ol/layers/index.js +3 -0
  76. package/ol/renderers/MaplibreLayerRenderer.d.ts +0 -20
  77. package/ol/renderers/MaplibreLayerRenderer.js +142 -114
  78. package/ol/renderers/MaplibreStyleLayerRenderer.d.ts +6 -6
  79. package/ol/renderers/MaplibreStyleLayerRenderer.js +20 -23
  80. package/ol/renderers/RealtimeLayerRenderer.d.ts +7 -7
  81. package/ol/renderers/RealtimeLayerRenderer.js +46 -66
  82. package/ol/styles/fullTrajectoryDelayStyle.js +5 -7
  83. package/ol/styles/fullTrajectoryStyle.d.ts +1 -2
  84. package/ol/styles/fullTrajectoryStyle.js +5 -7
  85. package/ol/styles/routingStyle.d.ts +0 -1
  86. package/ol/styles/routingStyle.js +13 -10
  87. package/ol/utils/defineDeprecatedProperties.d.ts +10 -0
  88. package/ol/utils/defineDeprecatedProperties.js +180 -0
  89. package/ol/utils/getFeatureInfoAtCoordinate.d.ts +1 -1
  90. package/ol/utils/getFeatureInfoAtCoordinate.js +11 -17
  91. package/package.json +44 -44
  92. package/setupTests.js +17 -4
  93. package/types/common.d.ts +53 -69
  94. package/types/index.d.ts +1 -1
  95. package/types/realtime.d.ts +98 -95
  96. package/types/routing.d.ts +60 -60
  97. package/types/stops.d.ts +62 -62
  98. package/common/mixins/RealtimeLayerMixin.d.ts +0 -273
  99. package/common/mixins/RealtimeLayerMixin.js +0 -743
  100. package/ol/layers/MapGlLayer.d.ts +0 -144
  101. package/ol/layers/MapGlLayer.js +0 -144
  102. package/ol/mixins/MobilityLayerMixin.d.ts +0 -98
  103. package/ol/mixins/MobilityLayerMixin.js +0 -6
  104. package/ol/mixins/PropertiesLayerMixin.d.ts +0 -127
  105. package/ol/mixins/PropertiesLayerMixin.js +0 -143
@@ -1,11 +1,11 @@
1
1
  import { Feature } from 'geojson';
2
2
  import Control, { Options } from 'ol/control/Control';
3
3
  import StopFinderControlCommon from '../../common/controls/StopFinderControlCommon';
4
- export type StopFinderControlOptions = Options & StopFinderControlCommon & {
4
+ export type StopFinderControlOptions = {
5
5
  className?: string;
6
- };
6
+ } & Options & StopFinderControlCommon;
7
7
  /**
8
- * Search stations.
8
+ * This OpenLayers control allows to search stations from the [geOps Stops API](https://developer.geops.io/apis/stops/).
9
9
  *
10
10
  * @example
11
11
  * import { Map } from 'ol';
@@ -23,15 +23,34 @@ export type StopFinderControlOptions = Options & StopFinderControlCommon & {
23
23
  *
24
24
  *
25
25
  * @see <a href="/example/ol-search">Openlayers search example</a>
26
+ *
27
+ * @extends {ol/control/Control~Control}
28
+ *
26
29
  * @public
27
30
  */
28
31
  declare class StopFinderControl extends Control {
29
32
  controller: StopFinderControlCommon;
30
- constructor(options: StopFinderControlOptions);
31
33
  /**
32
- * @private
34
+ * Constructor.
35
+ *
36
+ * @param {Object} options
37
+ * @param {HTMLElement} options.element HTML element where to attach input and suggestions.
38
+ * @param {string} options.apiKey Access key for [geOps APIs](https://developer.geops.io/).
39
+ * @param {StopsSearchParams} [options.apiParams={ limit: 20 }] Request parameters. See [geOps Stops API documentation](https://developer.geops.io/apis/5dcbd702a256d90001cf1361/).
40
+ * @param {string} [options.placeholder='Search for a stop...'] Input field placeholder.
41
+ * @param {string} [options.url='https://api.geops.io/stops/v1/'] [geOps Stops API](https://developer.geops.io/apis/stops/) url.
42
+ * @public
33
43
  */
44
+ constructor(options: StopFinderControlOptions);
34
45
  onSuggestionClick(suggestion: Feature): void;
46
+ /**
47
+ * Search for stations using a query.
48
+ *
49
+ * @param {string} q Query used to search stops.
50
+ * @param {AbortController} abortController Abort controller used to abort requests.
51
+ * @returns {Promise<Array<GeoJSONFeature>>}
52
+ * @public
53
+ */
35
54
  search(q: string, abortController: AbortController): Promise<void>;
36
55
  }
37
56
  export default StopFinderControl;
@@ -1,9 +1,9 @@
1
- import { fromLonLat } from 'ol/proj';
2
1
  import Control from 'ol/control/Control';
2
+ import { fromLonLat } from 'ol/proj';
3
3
  import StopFinderControlCommon from '../../common/controls/StopFinderControlCommon';
4
4
  import createDefaultStopFinderElement from '../../common/utils/createDefaultStopFinderElt';
5
5
  /**
6
- * Search stations.
6
+ * This OpenLayers control allows to search stations from the [geOps Stops API](https://developer.geops.io/apis/stops/).
7
7
  *
8
8
  * @example
9
9
  * import { Map } from 'ol';
@@ -21,9 +21,23 @@ import createDefaultStopFinderElement from '../../common/utils/createDefaultStop
21
21
  *
22
22
  *
23
23
  * @see <a href="/example/ol-search">Openlayers search example</a>
24
+ *
25
+ * @extends {ol/control/Control~Control}
26
+ *
24
27
  * @public
25
28
  */
26
29
  class StopFinderControl extends Control {
30
+ /**
31
+ * Constructor.
32
+ *
33
+ * @param {Object} options
34
+ * @param {HTMLElement} options.element HTML element where to attach input and suggestions.
35
+ * @param {string} options.apiKey Access key for [geOps APIs](https://developer.geops.io/).
36
+ * @param {StopsSearchParams} [options.apiParams={ limit: 20 }] Request parameters. See [geOps Stops API documentation](https://developer.geops.io/apis/5dcbd702a256d90001cf1361/).
37
+ * @param {string} [options.placeholder='Search for a stop...'] Input field placeholder.
38
+ * @param {string} [options.url='https://api.geops.io/stops/v1/'] [geOps Stops API](https://developer.geops.io/apis/stops/) url.
39
+ * @public
40
+ */
27
41
  constructor(options) {
28
42
  const element = createDefaultStopFinderElement();
29
43
  element.className = (options === null || options === void 0 ? void 0 : options.className) || 'mbt-stop-finder';
@@ -31,14 +45,19 @@ class StopFinderControl extends Control {
31
45
  super(opt);
32
46
  this.controller = new StopFinderControlCommon(Object.assign({ onSuggestionClick: this.onSuggestionClick.bind(this) }, opt));
33
47
  }
34
- /**
35
- * @private
36
- */
37
48
  onSuggestionClick(suggestion) {
38
49
  var _a;
39
50
  const coord = fromLonLat(suggestion.geometry.coordinates);
40
51
  (_a = this.getMap()) === null || _a === void 0 ? void 0 : _a.getView().setCenter(coord);
41
52
  }
53
+ /**
54
+ * Search for stations using a query.
55
+ *
56
+ * @param {string} q Query used to search stops.
57
+ * @param {AbortController} abortController Abort controller used to abort requests.
58
+ * @returns {Promise<Array<GeoJSONFeature>>}
59
+ * @public
60
+ */
42
61
  search(q, abortController) {
43
62
  return this.controller.search(q, abortController);
44
63
  }
@@ -0,0 +1,26 @@
1
+ import { Map } from 'ol';
2
+ import OLLayer from 'ol/layer/Layer';
3
+ import LayerRenderer from 'ol/renderer/Layer';
4
+ import type { Options } from 'ol/layer/Layer';
5
+ export type MobilityLayerOptions = {
6
+ children?: any[];
7
+ copyrights?: string[];
8
+ disabled?: boolean;
9
+ group?: string;
10
+ hitTolerance?: number;
11
+ key?: string;
12
+ map?: Map;
13
+ name?: string;
14
+ properties?: Record<string, any>;
15
+ visible?: boolean;
16
+ } & Options & Record<string, any>;
17
+ /**
18
+ * An OpenLayers layer here only for backward compatibility v2.
19
+ * @deprecated Use an OpenLayers Layer instead.
20
+ */
21
+ declare class Layer extends OLLayer {
22
+ constructor(options: MobilityLayerOptions);
23
+ clone(newOptions: MobilityLayerOptions): Layer;
24
+ createRenderer(): LayerRenderer<OLLayer>;
25
+ }
26
+ export default Layer;
@@ -0,0 +1,39 @@
1
+ import debounce from 'lodash.debounce';
2
+ import OLLayer from 'ol/layer/Layer';
3
+ import LayerRenderer from 'ol/renderer/Layer';
4
+ import defineDeprecatedProperties from '../utils/defineDeprecatedProperties';
5
+ let deprecated = () => { };
6
+ if (typeof window !== 'undefined' &&
7
+ new URLSearchParams(window.location.search).get('deprecated')) {
8
+ deprecated = debounce((message) => {
9
+ // eslint-disable-next-line no-console
10
+ console.warn(message);
11
+ }, 1000);
12
+ }
13
+ class EmptyLayerRenderer extends LayerRenderer {
14
+ prepareFrame() {
15
+ return true;
16
+ }
17
+ renderFrame() {
18
+ return null;
19
+ }
20
+ }
21
+ /**
22
+ * An OpenLayers layer here only for backward compatibility v2.
23
+ * @deprecated Use an OpenLayers Layer instead.
24
+ */
25
+ class Layer extends OLLayer {
26
+ constructor(options) {
27
+ super(options);
28
+ defineDeprecatedProperties(this, options);
29
+ deprecated('Layer is deprecated. Use an OpenLayers Layer instead.');
30
+ }
31
+ clone(newOptions) {
32
+ return new Layer(Object.assign(Object.assign({}, (this.get('options') || {})), (newOptions || {})));
33
+ }
34
+ // ol does not like when it returns null.
35
+ createRenderer() {
36
+ return new EmptyLayerRenderer(this);
37
+ }
38
+ }
39
+ export default Layer;
@@ -1,11 +1,19 @@
1
- import { Map, MapOptions } from 'maplibre-gl';
2
- import MapGlLayer, { MapGlLayerOptions } from './MapGlLayer';
3
- import MaplibreLayerRenderer from '../renderers/MaplibreLayerRenderer';
4
- export type MaplibreLayerOptions = MapGlLayerOptions & {
5
- mapOptions?: MapOptions;
6
- };
1
+ import { MapLibreLayer } from '@geoblocks/ol-maplibre-layer/lib';
2
+ import { EventsKey } from 'ol/events';
3
+ import Map from 'ol/Map';
4
+ import { MobilityLayerOptions } from './Layer';
5
+ import type { MapLibreLayerOptions, MapLibreOptions } from '@geoblocks/ol-maplibre-layer/lib/MapLibreLayer';
6
+ import type { QueryRenderedFeaturesOptions } from 'maplibre-gl';
7
+ export type MaplibreLayerOptions = {
8
+ apiKey?: string;
9
+ apiKeyName?: string;
10
+ mapLibreOptions?: MapLibreOptions;
11
+ queryRenderedFeaturesOptions?: QueryRenderedFeaturesOptions | undefined;
12
+ style?: maplibregl.StyleSpecification | null | string;
13
+ url?: string;
14
+ } & MapLibreLayerOptions & MobilityLayerOptions;
7
15
  /**
8
- * This layer is meant to facilitate the use of the geOps Maps api in an OpenLayers map using Maplibre.
16
+ * An OpenLayers layer able to display data from the [geOps Maps API](https://developer.geops.io/apis/maps).
9
17
  *
10
18
  * @example
11
19
  * import { MaplibreLayer } from 'mobility-toolbox-js/ol';
@@ -13,7 +21,7 @@ export type MaplibreLayerOptions = MapGlLayerOptions & {
13
21
  * const layer = new MaplibreLayer({
14
22
  * apiKey: 'yourApiKey',
15
23
  * // apiKeyName: 'key',
16
- * // mapOptions: {
24
+ * // mapLibreOptions: {
17
25
  * // interactive: false,
18
26
  * // trackResize: false,
19
27
  * // attributionControl: false,
@@ -25,39 +33,59 @@ export type MaplibreLayerOptions = MapGlLayerOptions & {
25
33
  * // url: 'https://maps.geops.io',
26
34
  * });
27
35
  *
28
- * @classproperty {maplibregl.Map} maplibreMap - The Maplibre map object. Readonly.
29
- * @classproperty {maplibregl.QueryRenderedFeaturesOptions} queryRenderedFeaturesOptions - Options used when we query features using map.getFeaturesAtPixel().
30
- * @classproperty {string} style - geOps Maps api style.
31
- * @extends {ol/layer/Layer~Layer}
36
+ * @classproperty {maplibregl.Map} mapLibreMap - The Maplibre map object. Readonly.
37
+ * @classproperty {string} style - The [geOps Maps API](https://developer.geops.io/apis/maps) style.
38
+ *
39
+ *
40
+ * @see <a href="/example/ol-maplibre-layer">OpenLayers Maplibre layer example</a>
41
+ *
42
+ * @extends {geoblocks/ol-maplibre-layer/MapLibreLayer}
32
43
  * @public
33
44
  */
34
- export default class MaplibreLayer extends MapGlLayer {
35
- options?: MaplibreLayerOptions;
36
- /** @private */
45
+ declare class MaplibreLayer extends MapLibreLayer {
46
+ olEventsKeys: EventsKey[];
47
+ set apiKey(newValue: string);
48
+ get apiKey(): string;
49
+ set apiKeyName(newValue: string);
50
+ get apiKeyName(): string;
51
+ /**
52
+ * @deprecated Use layer.mapLibreMap.
53
+ */
37
54
  get maplibreMap(): maplibregl.Map | undefined;
55
+ /**
56
+ * @deprecated Use layer.mapLibreMap.
57
+ */
58
+ get mbMap(): maplibregl.Map | undefined;
59
+ get style(): string;
60
+ set style(newValue: string);
61
+ get url(): string;
62
+ set url(newValue: string);
38
63
  /**
39
64
  * Constructor.
40
65
  *
41
- * @param {MaplibreLayerOptions} options
42
- * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
43
- * @param {string} [options.apiKeyName="key"] The geOps Maps api key name.
44
- * @param {maplibregl.MapOptions} [options.mapOptions={ interactive: false, trackResize: false, attributionControl: false }] Maplibre map options.
45
- * @param {string} [options.style="travic_v2"] The geOps Maps api style.
46
- * @param {string} [options.url="https://maps.geops.io"] The geOps Maps api url.
66
+ * @param {Object} options
67
+ * @param {string} options.apiKey Accesss key for [geOps APIs](https://developer.geops.io/).
68
+ * @param {string} [options.apiKeyName="key"] The [geOps Maps API](https://developer.geops.io/apis/maps) key name.
69
+ * @param {maplibregl.MapOptions} [options.mapLibreOptions={ interactive: false, trackResize: false, attributionControl: false }] MapLibre map options.
70
+ * @param {string} [options.style="travic_v2"] The [geOps Maps API](https://developer.geops.io/apis/maps) style.
71
+ * @param {string} [options.url="https://maps.geops.io"] The [geOps Maps API](https://developer.geops.io/apis/maps) url.
47
72
  */
48
73
  constructor(options: MaplibreLayerOptions);
49
74
  /**
50
- * @private
75
+ * Initialize the layer and listen to feature clicks.
51
76
  */
52
- createRenderer(): MaplibreLayerRenderer;
53
- /**
54
- * @private
55
- */
56
- createMap(options: MapOptions): Map;
77
+ attachToMap(): void;
57
78
  /**
58
79
  * Create a copy of the MaplibreLayer.
59
- * @param {MaplibreLayerOptions} newOptions Options to override
80
+ *
81
+ * @param {Object} newOptions Options to override. See constructor.
60
82
  * @return {MaplibreLayer} A MaplibreLayer layer
83
+ * @public
61
84
  */
62
85
  clone(newOptions: MaplibreLayerOptions): MaplibreLayer;
86
+ detachFromMap(): void;
87
+ getStyle(): any;
88
+ setMapInternal(map: Map): void;
89
+ updateMaplibreMap(): void;
63
90
  }
91
+ export default MaplibreLayer;
@@ -1,8 +1,23 @@
1
- import { Map } from 'maplibre-gl';
2
- import MapGlLayer from './MapGlLayer';
3
- import MaplibreLayerRenderer from '../renderers/MaplibreLayerRenderer';
1
+ import { MapLibreLayer } from '@geoblocks/ol-maplibre-layer/lib';
2
+ import debounce from 'lodash.debounce';
3
+ import { unByKey } from 'ol/Observable';
4
+ import { getUrlWithParams } from '../../common/utils';
5
+ import defineDeprecatedProperties from '../utils/defineDeprecatedProperties';
6
+ const buildStyleUrl = (url, style, apiKey, apiKeyName) => {
7
+ return getUrlWithParams(`${url}/styles/${style}/style.json`, {
8
+ [apiKeyName]: apiKey,
9
+ }).toString();
10
+ };
11
+ let deprecated = () => { };
12
+ if (typeof window !== 'undefined' &&
13
+ new URLSearchParams(window.location.search).get('deprecated')) {
14
+ deprecated = debounce((message) => {
15
+ // eslint-disable-next-line no-console
16
+ console.warn(message);
17
+ }, 1000);
18
+ }
4
19
  /**
5
- * This layer is meant to facilitate the use of the geOps Maps api in an OpenLayers map using Maplibre.
20
+ * An OpenLayers layer able to display data from the [geOps Maps API](https://developer.geops.io/apis/maps).
6
21
  *
7
22
  * @example
8
23
  * import { MaplibreLayer } from 'mobility-toolbox-js/ol';
@@ -10,7 +25,7 @@ import MaplibreLayerRenderer from '../renderers/MaplibreLayerRenderer';
10
25
  * const layer = new MaplibreLayer({
11
26
  * apiKey: 'yourApiKey',
12
27
  * // apiKeyName: 'key',
13
- * // mapOptions: {
28
+ * // mapLibreOptions: {
14
29
  * // interactive: false,
15
30
  * // trackResize: false,
16
31
  * // attributionControl: false,
@@ -22,49 +37,157 @@ import MaplibreLayerRenderer from '../renderers/MaplibreLayerRenderer';
22
37
  * // url: 'https://maps.geops.io',
23
38
  * });
24
39
  *
25
- * @classproperty {maplibregl.Map} maplibreMap - The Maplibre map object. Readonly.
26
- * @classproperty {maplibregl.QueryRenderedFeaturesOptions} queryRenderedFeaturesOptions - Options used when we query features using map.getFeaturesAtPixel().
27
- * @classproperty {string} style - geOps Maps api style.
28
- * @extends {ol/layer/Layer~Layer}
40
+ * @classproperty {maplibregl.Map} mapLibreMap - The Maplibre map object. Readonly.
41
+ * @classproperty {string} style - The [geOps Maps API](https://developer.geops.io/apis/maps) style.
42
+ *
43
+ *
44
+ * @see <a href="/example/ol-maplibre-layer">OpenLayers Maplibre layer example</a>
45
+ *
46
+ * @extends {geoblocks/ol-maplibre-layer/MapLibreLayer}
29
47
  * @public
30
48
  */
31
- export default class MaplibreLayer extends MapGlLayer {
32
- /** @private */
49
+ class MaplibreLayer extends MapLibreLayer {
50
+ set apiKey(newValue) {
51
+ this.set('apiKey', newValue);
52
+ }
53
+ get apiKey() {
54
+ return this.get('apiKey');
55
+ }
56
+ set apiKeyName(newValue) {
57
+ this.set('apiKeyName', newValue);
58
+ }
59
+ get apiKeyName() {
60
+ return this.get('apiKeyName');
61
+ }
62
+ /**
63
+ * @deprecated Use layer.mapLibreMap.
64
+ */
33
65
  get maplibreMap() {
34
- return this.mbMap;
66
+ deprecated('MaplibreLayer.maplibreMap is deprecated. Use layer.mapLibreMap.');
67
+ return this.mapLibreMap;
35
68
  }
69
+ // get queryRenderedFeaturesOptions(): maplibregl.QueryRenderedFeaturesOptions {
70
+ // return this.get('queryRenderedFeaturesOptions');
71
+ // }
72
+ // set queryRenderedFeaturesOptions(
73
+ // newValue: maplibregl.QueryRenderedFeaturesOptions,
74
+ // ) {
75
+ // this.set('queryRenderedFeaturesOptions', newValue);
76
+ // }
36
77
  /**
37
- * Constructor.
38
- *
39
- * @param {MaplibreLayerOptions} options
40
- * @param {string} options.apiKey Access key for [geOps apis](https://developer.geops.io/).
41
- * @param {string} [options.apiKeyName="key"] The geOps Maps api key name.
42
- * @param {maplibregl.MapOptions} [options.mapOptions={ interactive: false, trackResize: false, attributionControl: false }] Maplibre map options.
43
- * @param {string} [options.style="travic_v2"] The geOps Maps api style.
44
- * @param {string} [options.url="https://maps.geops.io"] The geOps Maps api url.
78
+ * @deprecated Use layer.mapLibreMap.
45
79
  */
46
- constructor(options) {
47
- super(Object.assign({}, options));
80
+ get mbMap() {
81
+ deprecated('MaplibreLayer.mbMap is deprecated. Use layer.maplibreMap.');
82
+ return this.maplibreMap;
83
+ }
84
+ get style() {
85
+ return this.get('style');
86
+ }
87
+ set style(newValue) {
88
+ this.set('style', newValue);
89
+ }
90
+ get url() {
91
+ return this.get('url');
92
+ }
93
+ set url(newValue) {
94
+ this.set('url', newValue);
48
95
  }
49
96
  /**
50
- * @private
97
+ * Constructor.
98
+ *
99
+ * @param {Object} options
100
+ * @param {string} options.apiKey Accesss key for [geOps APIs](https://developer.geops.io/).
101
+ * @param {string} [options.apiKeyName="key"] The [geOps Maps API](https://developer.geops.io/apis/maps) key name.
102
+ * @param {maplibregl.MapOptions} [options.mapLibreOptions={ interactive: false, trackResize: false, attributionControl: false }] MapLibre map options.
103
+ * @param {string} [options.style="travic_v2"] The [geOps Maps API](https://developer.geops.io/apis/maps) style.
104
+ * @param {string} [options.url="https://maps.geops.io"] The [geOps Maps API](https://developer.geops.io/apis/maps) url.
51
105
  */
52
- createRenderer() {
53
- return new MaplibreLayerRenderer(this);
106
+ constructor(options) {
107
+ var _a;
108
+ const newOptions = Object.assign(Object.assign({ apiKeyName: 'key', style: 'travic_v2', url: 'https://maps.geops.io' }, (options || {})), { mapLibreOptions: Object.assign({}, (options.mapLibreOptions || {})) });
109
+ if (!newOptions.mapLibreOptions.style &&
110
+ ((_a = newOptions.url) === null || _a === void 0 ? void 0 : _a.includes('style.json'))) {
111
+ newOptions.mapLibreOptions.style = newOptions.url;
112
+ }
113
+ else if (!newOptions.mapLibreOptions.style &&
114
+ newOptions.apiKey &&
115
+ newOptions.style &&
116
+ typeof newOptions.style === 'string') {
117
+ newOptions.mapLibreOptions.style = buildStyleUrl(newOptions.url, newOptions.style, newOptions.apiKey, newOptions.apiKeyName);
118
+ }
119
+ super(newOptions);
120
+ this.olEventsKeys = [];
121
+ // For backward compatibility with v2
122
+ defineDeprecatedProperties(this, options);
123
+ // We save the options to be able to clone the layer.
124
+ // and to see if the style is defined by the maplibreOptions given by the user.
125
+ this.set('options', options);
54
126
  }
55
127
  /**
56
- * @private
128
+ * Initialize the layer and listen to feature clicks.
57
129
  */
58
- // eslint-disable-next-line class-methods-use-this
59
- createMap(options) {
60
- return new Map(options);
130
+ attachToMap() {
131
+ const updateMaplibreMapDebounced = debounce(this.updateMaplibreMap.bind(this), 150);
132
+ updateMaplibreMapDebounced();
133
+ this.olEventsKeys.push(this.on('propertychange', (evt) => {
134
+ if (/(url|style|apiKey|apiKeyName)/.test(evt.key)) {
135
+ updateMaplibreMapDebounced();
136
+ }
137
+ }));
61
138
  }
62
139
  /**
63
140
  * Create a copy of the MaplibreLayer.
64
- * @param {MaplibreLayerOptions} newOptions Options to override
141
+ *
142
+ * @param {Object} newOptions Options to override. See constructor.
65
143
  * @return {MaplibreLayer} A MaplibreLayer layer
144
+ * @public
66
145
  */
67
146
  clone(newOptions) {
68
- return new MaplibreLayer(Object.assign(Object.assign({}, (this.options || {})), (newOptions || {})));
147
+ return new MaplibreLayer(Object.assign(Object.assign({}, (this.get('options') || {})), (newOptions || {})));
148
+ }
149
+ detachFromMap() {
150
+ unByKey(this.olEventsKeys);
151
+ }
152
+ getStyle() {
153
+ var _a, _b;
154
+ // If the style is a complete style object, use it directly.
155
+ if (this.style &&
156
+ typeof this.style === 'object' &&
157
+ this.style.name &&
158
+ this.style.version) {
159
+ return this.style;
160
+ }
161
+ // If the url set is already a complete style url, use it directly.
162
+ if (this.url.includes('style.json')) {
163
+ return this.url;
164
+ }
165
+ // If the user has defined the style by the maplibreOptions, we use it directly.
166
+ if ((_b = (_a = this.get('options')) === null || _a === void 0 ? void 0 : _a.mapLibreOptions) === null || _b === void 0 ? void 0 : _b.style) {
167
+ return this.get('options').mapLibreOptions.style;
168
+ }
169
+ /// Otherwise build the complete style url.
170
+ return buildStyleUrl(this.url, this.style, this.apiKey, this.apiKeyName);
171
+ }
172
+ setMapInternal(map) {
173
+ if (map) {
174
+ super.setMapInternal(map);
175
+ this.attachToMap();
176
+ }
177
+ else {
178
+ this.detachFromMap();
179
+ super.setMapInternal(map);
180
+ }
181
+ }
182
+ updateMaplibreMap() {
183
+ var _a;
184
+ try {
185
+ (_a = this.mapLibreMap) === null || _a === void 0 ? void 0 : _a.setStyle(this.getStyle(), { diff: false });
186
+ }
187
+ catch (e) {
188
+ // eslint-disable-next-line no-console
189
+ console.error('Error while updating MaplibreMap', e);
190
+ }
69
191
  }
70
192
  }
193
+ export default MaplibreLayer;