mobility-toolbox-js 3.0.0-beta.8 → 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 (130) hide show
  1. package/README.md +11 -3
  2. package/api/HttpAPI.d.ts +20 -0
  3. package/api/HttpAPI.js +1 -14
  4. package/api/RealtimeAPI.d.ts +404 -0
  5. package/api/RealtimeAPI.js +265 -206
  6. package/api/RoutingAPI.d.ts +47 -0
  7. package/api/RoutingAPI.js +17 -7
  8. package/api/StopsAPI.d.ts +44 -0
  9. package/api/StopsAPI.js +17 -11
  10. package/api/WebSocketAPI.d.ts +147 -0
  11. package/api/WebSocketAPI.js +164 -165
  12. package/api/index.d.ts +3 -0
  13. package/api/index.js +1 -1
  14. package/api/typedefs.d.ts +76 -0
  15. package/api/typedefs.js +27 -42
  16. package/common/controls/StopFinderControlCommon.d.ts +53 -0
  17. package/common/controls/StopFinderControlCommon.js +32 -32
  18. package/common/index.d.ts +2 -0
  19. package/common/index.js +1 -1
  20. package/common/styles/index.d.ts +4 -0
  21. package/common/styles/realtimeDefaultStyle.d.ts +36 -0
  22. package/common/styles/realtimeDefaultStyle.js +6 -11
  23. package/common/styles/realtimeDelayStyle.d.ts +12 -0
  24. package/common/styles/realtimeHeadingStyle.d.ts +12 -0
  25. package/common/styles/realtimeHeadingStyle.js +5 -10
  26. package/common/styles/realtimeSimpleStyle.d.ts +3 -0
  27. package/common/styles/realtimeSimpleStyle.js +0 -1
  28. package/common/typedefs.d.ts +102 -0
  29. package/common/typedefs.js +6 -31
  30. package/common/utils/RealtimeEngine.d.ts +214 -0
  31. package/common/utils/RealtimeEngine.js +554 -0
  32. package/common/utils/compareDepartures.d.ts +10 -0
  33. package/common/utils/compareDepartures.js +2 -2
  34. package/common/utils/constants.d.ts +5 -0
  35. package/common/utils/createCanvas.d.ts +10 -0
  36. package/common/utils/createDefaultCopyrightElt.d.ts +5 -0
  37. package/common/utils/createDefaultStopFinderElt.d.ts +5 -0
  38. package/common/utils/createRealtimeFilters.d.ts +12 -0
  39. package/common/utils/debounceDeparturesMessages.d.ts +12 -0
  40. package/common/utils/debounceWebsocketMessages.d.ts +11 -0
  41. package/common/utils/getLayersAsFlatArray.d.ts +2 -0
  42. package/common/utils/getLayersAsFlatArray.js +0 -1
  43. package/common/utils/getMapGlCopyrights.d.ts +17 -0
  44. package/common/utils/getMapGlCopyrights.js +3 -3
  45. package/common/utils/getRealtimeModeSuffix.d.ts +10 -0
  46. package/common/utils/getRealtimeModeSuffix.js +1 -0
  47. package/common/utils/getUrlWithParams.d.ts +8 -0
  48. package/common/utils/getVehiclePosition.d.ts +17 -0
  49. package/common/utils/getVehiclePosition.js +6 -3
  50. package/common/utils/index.d.ts +16 -0
  51. package/common/utils/realtimeConfig.d.ts +64 -0
  52. package/common/utils/realtimeConfig.js +0 -1
  53. package/common/utils/removeDuplicate.d.ts +9 -0
  54. package/common/utils/renderTrajectories.d.ts +17 -0
  55. package/common/utils/renderTrajectories.js +7 -6
  56. package/common/utils/sortAndFilterDepartures.d.ts +16 -0
  57. package/common/utils/sortAndFilterDepartures.js +2 -1
  58. package/common/utils/sortByDelay.d.ts +3 -0
  59. package/common/utils/sortByDelay.js +5 -1
  60. package/common/utils/timeUtils.d.ts +23 -0
  61. package/common/utils/toMercatorExtent.d.ts +5 -0
  62. package/iife.d.ts +2 -0
  63. package/index.d.ts +9 -0
  64. package/maplibre/controls/CopyrightControl.d.ts +38 -0
  65. package/maplibre/controls/CopyrightControl.js +11 -8
  66. package/maplibre/controls/index.d.ts +1 -0
  67. package/maplibre/index.d.ts +5 -0
  68. package/maplibre/layers/Layer.d.ts +29 -0
  69. package/maplibre/layers/Layer.js +2 -3
  70. package/maplibre/layers/RealtimeLayer.d.ts +124 -0
  71. package/maplibre/layers/RealtimeLayer.js +154 -118
  72. package/maplibre/layers/index.d.ts +2 -0
  73. package/maplibre/utils/getMercatorResolution.d.ts +7 -0
  74. package/maplibre/utils/getSourceCoordinates.d.ts +8 -0
  75. package/maplibre/utils/getSourceCoordinates.js +6 -5
  76. package/maplibre/utils/index.d.ts +2 -0
  77. package/mbt.js +14585 -14557
  78. package/mbt.js.map +4 -4
  79. package/mbt.min.js +75 -75
  80. package/mbt.min.js.map +4 -4
  81. package/ol/controls/CopyrightControl.d.ts +39 -0
  82. package/ol/controls/CopyrightControl.js +13 -5
  83. package/ol/controls/RoutingControl.d.ts +213 -0
  84. package/ol/controls/RoutingControl.js +250 -264
  85. package/ol/controls/StopFinderControl.d.ts +56 -0
  86. package/ol/controls/StopFinderControl.js +24 -5
  87. package/ol/controls/index.d.ts +3 -0
  88. package/ol/index.d.ts +6 -0
  89. package/ol/layers/Layer.d.ts +26 -0
  90. package/ol/layers/Layer.js +39 -0
  91. package/ol/layers/MaplibreLayer.d.ts +91 -0
  92. package/ol/layers/MaplibreLayer.js +154 -31
  93. package/ol/layers/MaplibreStyleLayer.d.ts +157 -0
  94. package/ol/layers/MaplibreStyleLayer.js +281 -210
  95. package/ol/layers/RealtimeLayer.d.ts +150 -0
  96. package/ol/layers/RealtimeLayer.js +210 -206
  97. package/ol/layers/VectorLayer.d.ts +17 -0
  98. package/ol/layers/VectorLayer.js +33 -0
  99. package/ol/layers/index.d.ts +5 -0
  100. package/ol/layers/index.js +3 -0
  101. package/ol/renderers/MaplibreLayerRenderer.d.ts +0 -0
  102. package/ol/renderers/MaplibreLayerRenderer.js +142 -114
  103. package/ol/renderers/MaplibreStyleLayerRenderer.d.ts +20 -0
  104. package/ol/renderers/MaplibreStyleLayerRenderer.js +20 -23
  105. package/ol/renderers/RealtimeLayerRenderer.d.ts +22 -0
  106. package/ol/renderers/RealtimeLayerRenderer.js +50 -70
  107. package/ol/styles/fullTrajectoryDelayStyle.d.ts +6 -0
  108. package/ol/styles/fullTrajectoryDelayStyle.js +5 -7
  109. package/ol/styles/fullTrajectoryStyle.d.ts +4 -0
  110. package/ol/styles/fullTrajectoryStyle.js +5 -7
  111. package/ol/styles/index.d.ts +3 -0
  112. package/ol/styles/routingStyle.d.ts +3 -0
  113. package/ol/styles/routingStyle.js +13 -10
  114. package/ol/utils/defineDeprecatedProperties.d.ts +10 -0
  115. package/ol/utils/defineDeprecatedProperties.js +180 -0
  116. package/ol/utils/getFeatureInfoAtCoordinate.d.ts +8 -0
  117. package/ol/utils/getFeatureInfoAtCoordinate.js +11 -17
  118. package/ol/utils/index.d.ts +1 -0
  119. package/package.json +44 -44
  120. package/setupTests.d.ts +1 -0
  121. package/setupTests.js +17 -4
  122. package/types/common.d.ts +53 -69
  123. package/types/index.d.ts +1 -1
  124. package/types/realtime.d.ts +98 -95
  125. package/types/routing.d.ts +60 -60
  126. package/types/stops.d.ts +62 -62
  127. package/common/mixins/RealtimeLayerMixin.js +0 -743
  128. package/ol/layers/MapGlLayer.js +0 -142
  129. package/ol/mixins/MobilityLayerMixin.js +0 -9
  130. package/ol/mixins/PropertiesLayerMixin.js +0 -142
@@ -0,0 +1,56 @@
1
+ import { Feature } from 'geojson';
2
+ import Control, { Options } from 'ol/control/Control';
3
+ import StopFinderControlCommon from '../../common/controls/StopFinderControlCommon';
4
+ export type StopFinderControlOptions = {
5
+ className?: string;
6
+ } & Options & StopFinderControlCommon;
7
+ /**
8
+ * This OpenLayers control allows to search stations from the [geOps Stops API](https://developer.geops.io/apis/stops/).
9
+ *
10
+ * @example
11
+ * import { Map } from 'ol';
12
+ * import { StopFinderControl } from 'mobility-toolbox-js/ol';
13
+ *
14
+ * const map = new Map({
15
+ * target: 'map',
16
+ * });
17
+ *
18
+ * const control = new StopFinderControl({
19
+ * apiKey: [yourApiKey]
20
+ * });
21
+ *
22
+ * map.addControl(control);
23
+ *
24
+ *
25
+ * @see <a href="/example/ol-search">Openlayers search example</a>
26
+ *
27
+ * @extends {ol/control/Control~Control}
28
+ *
29
+ * @public
30
+ */
31
+ declare class StopFinderControl extends Control {
32
+ controller: StopFinderControlCommon;
33
+ /**
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
43
+ */
44
+ constructor(options: StopFinderControlOptions);
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
+ */
54
+ search(q: string, abortController: AbortController): Promise<void>;
55
+ }
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,3 @@
1
+ export { default as CopyrightControl } from "./CopyrightControl";
2
+ export { default as RoutingControl } from "./RoutingControl";
3
+ export { default as StopFinderControl } from "./StopFinderControl";
package/ol/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from "../api";
2
+ export * from "../common";
3
+ export * from "./controls";
4
+ export * from "./layers";
5
+ export * from "./styles";
6
+ export * from "./utils";
@@ -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;
@@ -0,0 +1,91 @@
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;
15
+ /**
16
+ * An OpenLayers layer able to display data from the [geOps Maps API](https://developer.geops.io/apis/maps).
17
+ *
18
+ * @example
19
+ * import { MaplibreLayer } from 'mobility-toolbox-js/ol';
20
+ *
21
+ * const layer = new MaplibreLayer({
22
+ * apiKey: 'yourApiKey',
23
+ * // apiKeyName: 'key',
24
+ * // mapLibreOptions: {
25
+ * // interactive: false,
26
+ * // trackResize: false,
27
+ * // attributionControl: false,
28
+ * // }
29
+ * // queryRenderedFeaturesOptions: {
30
+ * // layers: ['waters_lakes'], // map.getFeaturesAtPixel will only return lakes.
31
+ * // },
32
+ * // style: 'travic_v2',
33
+ * // url: 'https://maps.geops.io',
34
+ * });
35
+ *
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}
43
+ * @public
44
+ */
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
+ */
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);
63
+ /**
64
+ * Constructor.
65
+ *
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.
72
+ */
73
+ constructor(options: MaplibreLayerOptions);
74
+ /**
75
+ * Initialize the layer and listen to feature clicks.
76
+ */
77
+ attachToMap(): void;
78
+ /**
79
+ * Create a copy of the MaplibreLayer.
80
+ *
81
+ * @param {Object} newOptions Options to override. See constructor.
82
+ * @return {MaplibreLayer} A MaplibreLayer layer
83
+ * @public
84
+ */
85
+ clone(newOptions: MaplibreLayerOptions): MaplibreLayer;
86
+ detachFromMap(): void;
87
+ getStyle(): any;
88
+ setMapInternal(map: Map): void;
89
+ updateMaplibreMap(): void;
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;
@@ -0,0 +1,157 @@
1
+ import { FeatureState } from 'maplibre-gl';
2
+ import { Feature, Map } from 'ol';
3
+ import { Coordinate } from 'ol/coordinate';
4
+ import { EventsKey } from 'ol/events';
5
+ import { Layer } from 'ol/layer';
6
+ import { ObjectEvent } from 'ol/Object';
7
+ import { FilterFunction } from '../../common/typedefs';
8
+ import { LayerGetFeatureInfoResponse } from '../../types';
9
+ import MaplibreStyleLayerRenderer from '../renderers/MaplibreStyleLayerRenderer';
10
+ import MaplibreLayer, { MaplibreLayerOptions } from './MaplibreLayer';
11
+ export type MaplibreStyleLayerOptions = {
12
+ beforeId?: string;
13
+ layers?: maplibregl.AddLayerObject[];
14
+ layersFilter?: FilterFunction;
15
+ maplibreLayer?: MaplibreLayer;
16
+ queryRenderedLayersFilter?: FilterFunction;
17
+ } & MaplibreLayerOptions;
18
+ /**
19
+ * Layer that helps show/hide a specific subset of style layers of a [MaplibreLayer](./MaplibreLayer.js~MaplibreLayer.html).
20
+ *
21
+ * @example
22
+ * import { MaplibreLayer, MaplibreStyleLayer } from 'mobility-toolbox-js/ol';
23
+ *
24
+ * const maplibreLayer = new MaplibreLayer({
25
+ * apiKey: 'yourApiKey',
26
+ * });
27
+ *
28
+ * const layer = new MaplibreStyleLayer({
29
+ * maplibreLayer: maplibreLayer,
30
+ * layersFilter: (layer) => {
31
+ * // show/hide only style layers related to stations
32
+ * return /station/.test(layer.id);
33
+ * },
34
+ * });
35
+ *
36
+ * @see <a href="/example/ol-maplibre-style-layer">OpenLayers MaplibreStyle layer example</a>
37
+ * @extends {ol/layer/Layer~Layer}
38
+ * @public
39
+ */
40
+ declare class MaplibreStyleLayer extends Layer {
41
+ highlightedFeatures: Feature[];
42
+ olEventsKeys: EventsKey[];
43
+ selectedFeatures: Feature[];
44
+ get beforeId(): string;
45
+ set beforeId(newValue: string | undefined);
46
+ get layers(): maplibregl.AddLayerObject[];
47
+ set layers(newValue: maplibregl.AddLayerObject[]);
48
+ get layersFilter(): (layer: maplibregl.LayerSpecification) => boolean;
49
+ set layersFilter(newValue: (layer: maplibregl.LayerSpecification) => boolean);
50
+ /**
51
+ * @deprecated Use MaplibreStyleLayer.maplibreLayer instead.
52
+ */
53
+ get mapboxLayer(): MaplibreLayer | undefined;
54
+ get maplibreLayer(): MaplibreLayer;
55
+ set maplibreLayer(newValue: MaplibreLayer);
56
+ get queryRenderedLayersFilter(): (layer: maplibregl.LayerSpecification) => boolean;
57
+ set queryRenderedLayersFilter(newValue: (layer: maplibregl.LayerSpecification) => boolean);
58
+ get sources(): Record<string, maplibregl.SourceSpecification>;
59
+ set sources(newValue: Record<string, maplibregl.SourceSpecification>);
60
+ /**
61
+ * @deprecated Use MaplibreStyleLayer.layer instead.
62
+ */
63
+ get styleLayer(): maplibregl.AddLayerObject;
64
+ /**
65
+ * @deprecated
66
+ */
67
+ set styleLayer(newValue: maplibregl.AddLayerObject);
68
+ /**
69
+ * Apply visibility to style layers that fits the styleLayersFilter function.
70
+ */
71
+ /**
72
+ * @deprecated
73
+ */
74
+ get styleLayers(): maplibregl.AddLayerObject[];
75
+ /**
76
+ * @deprecated
77
+ */
78
+ set styleLayers(newValue: maplibregl.AddLayerObject[]);
79
+ /**
80
+ * Constructor.
81
+ *
82
+ * @param {Object} options
83
+ * @param {string} [options.beforeId] The style layer id to use when the options.layers property is defined, unsused otherwise.
84
+ * @param {maplibregl.AddLayerObject[]} [options.layers] The layers to add to the style on load.
85
+ * @param {FilterFunction} [options.layersFilter] Filter function to decide which style layer to apply visiblity on. If not provided, the 'layers' property is used.
86
+ * @param {MaplibreLayer} [options.maplibreLayer] The MaplibreLayer to use.
87
+ * @param {FilterFunction} [options.queryRenderedLayersFilter] Filter function to decide which style layer are available for query.
88
+ * @param {{[id: string]:maplibregl.SourceSpecification}} [options.sources] The sources to add to the style on load.
89
+ * @public
90
+ */
91
+ constructor(options?: MaplibreStyleLayerOptions);
92
+ addLayers(): void;
93
+ addSources(): void;
94
+ applyLayoutVisibility(evt?: ObjectEvent): void;
95
+ /**
96
+ * Initialize the layer.
97
+ * @param {ol/Map~Map} map the Maplibre map.
98
+ * @override
99
+ */
100
+ attachToMap(map: Map): void;
101
+ /**
102
+ * Create a copy of the MaplibreStyleLayer.
103
+ *
104
+ * @param {Object} newOptions Options to override. See constructor.
105
+ * @return {MapboxStyleLayer} A MaplibreStyleLayer.
106
+ * @public
107
+ */
108
+ clone(newOptions: MaplibreStyleLayerOptions): MaplibreStyleLayer;
109
+ createRenderer(): MaplibreStyleLayerRenderer;
110
+ /**
111
+ * Terminate the layer.
112
+ * @override
113
+ */
114
+ detachFromMap(): void;
115
+ /**
116
+ * Request feature information for a given coordinate.
117
+ * @param {ol/coordinate~Coordinate} coordinate Coordinate to request the information at.
118
+ * @return {Promise<FeatureInfo>} Promise with features, layer and coordinate.
119
+ * @deprecated Use getFeatureInfoAtCoordinate([layer], coordinate) from mobility-toolbox-ol package instead.
120
+ */
121
+ getFeatureInfoAtCoordinate(coordinate: Coordinate): Promise<LayerGetFeatureInfoResponse>;
122
+ /**
123
+ * Highlight a list of features.
124
+ * @param {Array<ol/Feature~Feature>} [features=[]] Features to highlight.
125
+ * @deprecated Use layer.setFeatureState(features, {hover: true|false}) instead.
126
+ */
127
+ highlight(features?: Feature[]): void;
128
+ /**
129
+ * On Maplibre map load callback function. Add style layers and dynaimc filters.
130
+ */
131
+ onLoad(): void;
132
+ removeLayers(): void;
133
+ removeSources(): void;
134
+ /**
135
+ * Select a list of features.
136
+ * @param {Array<ol/Feature~Feature>} [features=[]] Features to select.
137
+ * @deprecated Use layer.setFeatureState(features, {selected: true|false}) instead.
138
+ */
139
+ select(features?: Feature[]): void;
140
+ /**
141
+ * Set the [feature state](https://maplibre.org/maplibre-style-spec/expressions/#feature-state) of the features.
142
+ *
143
+ * @param {ol/Feature~Feature[]} features
144
+ * @param {{[key: string]: any}} state The feature state
145
+ * @public
146
+ */
147
+ setFeatureState(features: Feature[], state: FeatureState): void;
148
+ /**
149
+ * Set if features are hovered or not.
150
+ * @param {Array<ol/Feature~Feature>} features
151
+ * @param {boolean} state Is the feature hovered
152
+ * @deprecated Use layer.setFeatureState(features, {hover: true|false}) instead.
153
+ */
154
+ setHoverState(features: Feature[], state: boolean): void;
155
+ setMapInternal(map: Map): void;
156
+ }
157
+ export default MaplibreStyleLayer;