-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(journey-maps): introducing SbbRailNetworkOptions
- Loading branch information
Showing
3 changed files
with
86 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/journey-maps/angular/services/map/map-rail-network-layer.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/journey-maps/angular/services/map/map-rail-network.service.ts
This file was deleted.
Oops, something went wrong.