Skip to content

Commit bc5833e

Browse files
committed
fix: handle one or more features selected
1 parent d1cd372 commit bc5833e

4 files changed

Lines changed: 76 additions & 33 deletions

File tree

src/ol/layers/Layer.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import debounce from 'lodash.debounce';
22
import OLLayer from 'ol/layer/Layer';
3+
import { unByKey } from 'ol/Observable';
34
import LayerRenderer from 'ol/renderer/Layer';
45

56
import defineDeprecatedProperties from '../utils/defineDeprecatedProperties';
67

78
import type { Map } from 'ol';
9+
import type { EventsKey } from 'ol/events';
810
import type { Options } from 'ol/layer/Layer';
911

1012
export type MobilityLayerOptions = {
@@ -46,10 +48,15 @@ class EmptyLayerRenderer extends LayerRenderer<OLLayer> {
4648
* @deprecated Use an OpenLayers Layer instead.
4749
*/
4850
class Layer extends OLLayer {
51+
olEventsKeys: EventsKey[] = [];
52+
4953
constructor(options: MobilityLayerOptions = {}) {
5054
super(options);
5155
defineDeprecatedProperties(this, options);
5256
deprecated('Layer is deprecated. Use an OpenLayers Layer instead.');
57+
58+
// Backward compatibility
59+
this.olEventsKeys = [];
5360
}
5461

5562
clone(newOptions: MobilityLayerOptions): Layer {
@@ -63,6 +70,11 @@ class Layer extends OLLayer {
6370
createRenderer(): LayerRenderer<OLLayer> {
6471
return new EmptyLayerRenderer(this);
6572
}
73+
74+
detachFromMap() {
75+
// Backward compatibility
76+
unByKey(this.olEventsKeys);
77+
}
6678
}
6779

6880
export default Layer;

src/ol/layers/MaplibreLayer.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import type {
2-
MapLibreLayerOptions,
3-
MapLibreOptions,
4-
} from '@geoblocks/ol-maplibre-layer/lib/MapLibreLayer';
5-
import type { QueryRenderedFeaturesOptions } from 'maplibre-gl';
6-
71
import { MapLibreLayer } from '@geoblocks/ol-maplibre-layer/lib';
82
import debounce from 'lodash.debounce';
9-
import { EventsKey } from 'ol/events';
10-
import Map from 'ol/Map';
11-
import { ObjectEvent } from 'ol/Object';
123
import { unByKey } from 'ol/Observable';
134

145
import { getUrlWithParams } from '../../common/utils';
156
import MaplibreLayerRenderer from '../renderers/MaplibreLayerRenderer';
167
import defineDeprecatedProperties from '../utils/defineDeprecatedProperties';
17-
import { MobilityLayerOptions } from './Layer';
8+
9+
import type {
10+
MapLibreLayerOptions,
11+
MapLibreOptions,
12+
} from '@geoblocks/ol-maplibre-layer/lib/MapLibreLayer';
13+
import type { QueryRenderedFeaturesOptions } from 'maplibre-gl';
14+
import type { EventsKey } from 'ol/events';
15+
import type Map from 'ol/Map';
16+
import type { ObjectEvent } from 'ol/Object';
17+
18+
import type { MobilityLayerOptions } from './Layer';
1819

1920
export type MaplibreLayerOptions = {
2021
apiKey?: string;
@@ -150,6 +151,13 @@ class MaplibreLayer extends MapLibreLayer {
150151
* @param {string} [options.url="https://maps.geops.io"] The [geOps Maps API](https://developer.geops.io/apis/maps) url.
151152
*/
152153
constructor(options: MaplibreLayerOptions) {
154+
// Backward compatibility
155+
if (options.mapOptions && !options.mapLibreOptions) {
156+
deprecated(
157+
'MaplibreLayer.mapOptions is deprecated. Use mapLibreOptions instead.',
158+
);
159+
options.mapLibreOptions = options.mapOptions;
160+
}
153161
const newOptions = {
154162
apiKeyName: 'key',
155163
style: 'travic_v2',

src/ol/layers/RealtimeLayer.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
import { DebouncedFunc } from 'lodash';
21
import debounce from 'lodash.debounce';
3-
import { Map, MapEvent } from 'ol';
4-
import { EventsKey } from 'ol/events';
52
import { getIntersection, isEmpty } from 'ol/extent';
6-
import Feature, { FeatureLike } from 'ol/Feature';
73
import GeoJSON from 'ol/format/GeoJSON';
84
import Layer from 'ol/layer/Layer';
95
import VectorLayer from 'ol/layer/Vector';
10-
import { ObjectEvent } from 'ol/Object';
116
import { unByKey } from 'ol/Observable';
127
import { Vector as VectorSource } from 'ol/source';
138
import Source from 'ol/source/Source';
14-
import { State } from 'ol/View';
15-
16-
import { FilterFunction, SortFunction } from '../../common/typedefs';
17-
import RealtimeEngine, {
18-
RealtimeEngineOptions,
19-
} from '../../common/utils/RealtimeEngine';
20-
import { RealtimeAPI } from '../../maplibre';
21-
import {
9+
10+
import RealtimeEngine from '../../common/utils/RealtimeEngine';
11+
import RealtimeLayerRenderer from '../renderers/RealtimeLayerRenderer';
12+
import { fullTrajectoryStyle } from '../styles';
13+
import defineDeprecatedProperties from '../utils/defineDeprecatedProperties';
14+
15+
import type { DebouncedFunc } from 'lodash';
16+
import type { Map, MapEvent } from 'ol';
17+
import type { EventsKey } from 'ol/events';
18+
import type { FeatureLike } from 'ol/Feature';
19+
import type Feature from 'ol/Feature';
20+
import type { ObjectEvent } from 'ol/Object';
21+
import type { State } from 'ol/View';
22+
23+
import type { FilterFunction, SortFunction } from '../../common/typedefs';
24+
import type { RealtimeEngineOptions } from '../../common/utils/RealtimeEngine';
25+
import type { RealtimeAPI } from '../../maplibre';
26+
import type {
2227
RealtimeMode,
2328
RealtimeRenderState,
2429
RealtimeStopSequence,
2530
RealtimeTrainId,
2631
ViewState,
2732
} from '../../types';
28-
import RealtimeLayerRenderer from '../renderers/RealtimeLayerRenderer';
29-
import { fullTrajectoryStyle } from '../styles';
30-
import defineDeprecatedProperties from '../utils/defineDeprecatedProperties';
31-
import { MobilityLayerOptions } from './Layer';
33+
34+
import type { MobilityLayerOptions } from './Layer';
3235

3336
const format = new GeoJSON();
3437

@@ -356,8 +359,9 @@ class RealtimeLayer extends Layer {
356359
};
357360
}
358361

359-
highlight(feature: Feature) {
360-
const id = feature?.get('train_id');
362+
highlight(features: Feature | Feature[]) {
363+
const feat = Array.isArray(features) ? features[0] : features;
364+
const id: null | string | undefined = feat?.get('train_id') as string;
361365
if (this.hoverVehicleId !== id) {
362366
this.hoverVehicleId = id;
363367
this.engine.renderTrajectories(true);
@@ -437,18 +441,19 @@ class RealtimeLayer extends Layer {
437441
}
438442

439443
if (this.selectedVehicleId) {
440-
this.highlightTrajectory(this.selectedVehicleId);
444+
void this.highlightTrajectory(this.selectedVehicleId);
441445
}
442446
}
443447

444-
select(feature: Feature) {
445-
const id = feature?.get('train_id');
448+
select(features: Feature | Feature[]) {
449+
const feat = Array.isArray(features) ? features[0] : features;
450+
const id: null | string | undefined = feat?.get('train_id') as string;
446451
if (this.selectedVehicleId !== id) {
447452
this.cleanVectorLayer();
448453
this.selectedVehicleId = id;
449454
this.engine.renderTrajectories(true);
450455
}
451-
this.highlightTrajectory(id);
456+
void this.highlightTrajectory(id);
452457
}
453458

454459
override setMapInternal(map: Map) {

src/ol/utils/defineDeprecatedProperties.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,23 @@ const defineDeprecatedProperties = (
193193
);
194194
},
195195
},
196+
olListenersKeys: {
197+
/** @deprecated */
198+
get() {
199+
deprecated(
200+
'Layer.olListenersKeys is deprecated. Use the Layer.olEventsKeys instead.',
201+
);
202+
//@ts-expect-error Property just there for backward compatibility
203+
return obj.olEventsKeys || [];
204+
},
205+
set(newValue: string[]) {
206+
deprecated(
207+
'Layer.olListenersKeys is deprecated. Use the Layer.olEventsKeys instead.',
208+
);
209+
210+
//@ts-expect-error Property just there for backward compatibility
211+
obj.olEventsKeys = newValue;
212+
},
196213
options: {
197214
/** @deprecated */
198215
get(): MobilityLayerOptions {
@@ -241,6 +258,7 @@ const defineDeprecatedProperties = (
241258
obj.setProperties(newValue);
242259
},
243260
},
261+
244262
visible: {
245263
/** @deprecated */
246264
get(): boolean {

0 commit comments

Comments
 (0)