Skip to content

Commit

Permalink
feature(journey-maps): introducing SbbRailNetworkOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mstankala committed Mar 9, 2023
1 parent b553f2e commit ab4df27
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 48 deletions.
23 changes: 17 additions & 6 deletions src/journey-maps/angular/journey-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { SbbMapJourneyService } from './services/map/map-journey-service';
import { SbbMapLeitPoiService } from './services/map/map-leit-poi-service';
import { SbbMapMarkerService } from './services/map/map-marker-service';
import { SbbMapOverflowingLabelService } from './services/map/map-overflowing-label-service';
import { SbbMapRailNetworkService } from './services/map/map-rail-network.service';
import { SbbMapRailNetworkLayerService } from './services/map/map-rail-network-layer.service';
import { SbbMapRoutesService } from './services/map/map-routes.service';
import { SbbMapService } from './services/map/map-service';
import { SbbMapTransferService } from './services/map/map-transfer-service';
Expand Down Expand Up @@ -206,7 +206,7 @@ export class SbbJourneyMaps implements OnInit, AfterViewInit, OnDestroy, OnChang
private _mapJourneyService: SbbMapJourneyService,
private _mapTransferService: SbbMapTransferService,
private _mapRoutesService: SbbMapRoutesService,
private _mapRailNetworkService: SbbMapRailNetworkService,
private _mapRailNetworkLayerService: SbbMapRailNetworkLayerService,
private _mapZoneService: SbbMapZoneService,
private _mapLeitPoiService: SbbMapLeitPoiService,
private _urlService: SbbMapUrlService,
Expand Down Expand Up @@ -491,10 +491,15 @@ export class SbbJourneyMaps implements OnInit, AfterViewInit, OnDestroy, OnChang
if (changes.journeyMapsRoutingOption.currentValue?.routes) {
this._updateRoutes();
}
this._mapRailNetworkService.updateOptions(
this._map,
changes.journeyMapsRoutingOption.currentValue?.railNetworkOptions
);
if (
changes.journeyMapsRoutingOption.currentValue?.railNetworkOptions ||
changes.journeyMapsRoutingOption.previousValue?.railNetworkOptions
) {
this._mapRailNetworkLayerService.updateOptions(
this._map,
changes.journeyMapsRoutingOption.currentValue?.railNetworkOptions
);
}
});
}
}
Expand Down Expand Up @@ -783,6 +788,12 @@ export class SbbJourneyMaps implements OnInit, AfterViewInit, OnDestroy, OnChang
if (this.journeyMapsZones) {
this._updateZones();
}
if (this.journeyMapsRoutingOption?.railNetworkOptions) {
this._mapRailNetworkLayerService.updateOptions(
this._map,
this.journeyMapsRoutingOption?.railNetworkOptions
);
}
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Injectable } from '@angular/core';
import { SbbRailNetworkOptions } from '@sbb-esta/journey-maps/angular';
import { LineLayerSpecification, Map as MaplibreMap } from 'maplibre-gl';

const RAIL_NETWORK_LAYER_CONST = {
idPart: '-track',
layerType: 'line',
colorPropName: 'line-color',
};

interface RailLayerMetadata extends Record<string, any> {
railNetworkDefaultLineColor?: string;
}

@Injectable({ providedIn: 'root' })
export class SbbMapRailNetworkLayerService {
updateOptions(map: MaplibreMap, options?: SbbRailNetworkOptions) {
map.getStyle().layers?.forEach((layer) => {
if (
layer.type === RAIL_NETWORK_LAYER_CONST.layerType &&
layer.id.includes(RAIL_NETWORK_LAYER_CONST.idPart)
) {
const lineLayer = layer as LineLayerSpecification;
if (!options) {
this._restoreLayerOptions(map, lineLayer);
} else {
this._updateLayerOptions(map, lineLayer, options);
}
}
});
}

private _updateLayerOptions(
map: MaplibreMap,
railLineLayer: LineLayerSpecification,
options: SbbRailNetworkOptions
): void {
this._backupLayerOptions(railLineLayer);
map.setPaintProperty(
railLineLayer.id,
RAIL_NETWORK_LAYER_CONST.colorPropName,
options?.railNetworkColor
);
}

private _backupLayerOptions(railLineLayer: LineLayerSpecification) {
const layerMetadata = (railLineLayer.metadata ?? {}) as RailLayerMetadata;
if (layerMetadata.railNetworkDefaultLineColor) {
return;
}
const linePaintOptions = (railLineLayer.paint || {}) as Record<string, any>;
layerMetadata.railNetworkDefaultLineColor =
linePaintOptions[RAIL_NETWORK_LAYER_CONST.colorPropName];
railLineLayer.metadata = layerMetadata;
}

private _restoreLayerOptions(map: MaplibreMap, railLineLayer: LineLayerSpecification): void {
const layerMetadata = (railLineLayer.metadata ?? {}) as RailLayerMetadata;
if (!layerMetadata.railNetworkDefaultLineColor) {
return;
}
map.setPaintProperty(
railLineLayer.id,
RAIL_NETWORK_LAYER_CONST.colorPropName,
layerMetadata.railNetworkDefaultLineColor
);
delete layerMetadata.railNetworkDefaultLineColor;
}
}
42 changes: 0 additions & 42 deletions src/journey-maps/angular/services/map/map-rail-network.service.ts

This file was deleted.

0 comments on commit ab4df27

Please sign in to comment.