forked from equinor/webviz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb50584
commit 4334c7c
Showing
9 changed files
with
106 additions
and
26 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
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
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
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
56 changes: 56 additions & 0 deletions
56
frontend/src/modules/Intersection/utils/layers/LayerManager.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,56 @@ | ||
import { QueryClient } from "@tanstack/query-core"; | ||
|
||
import { BaseLayer } from "./BaseLayer"; | ||
|
||
export enum LayerManagerTopics { | ||
LAYERS_CHANGED = "layers-changed", | ||
} | ||
|
||
export class LayerManager { | ||
private _queryClient: QueryClient; | ||
private _layers: BaseLayer<any, any>[] = []; | ||
private _subscribers: Map<LayerManagerTopics, Set<() => void>> = new Map(); | ||
|
||
constructor(queryClient: QueryClient) { | ||
this._queryClient = queryClient; | ||
} | ||
|
||
addLayer(layer: BaseLayer<any, any>): void { | ||
layer.setName(this.makeUniqueLayerName(layer.getName())); | ||
layer.setQueryClient(this._queryClient); | ||
this._layers.push(layer); | ||
this.notifySubscribers(LayerManagerTopics.LAYERS_CHANGED); | ||
} | ||
|
||
removeLayer(id: string): void { | ||
this._layers = this._layers.filter((layer) => layer.getId() !== id); | ||
} | ||
|
||
getLayer(id: string): BaseLayer<any, any> | undefined { | ||
return this._layers.find((layer) => layer.getId() === id); | ||
} | ||
|
||
changeOrder(order: string[]): void { | ||
this._layers = order | ||
.map((id) => this._layers.find((layer) => layer.getId() === id)) | ||
.filter(Boolean) as BaseLayer<any, any>[]; | ||
this.notifySubscribers(LayerManagerTopics.LAYERS_CHANGED); | ||
} | ||
|
||
private notifySubscribers(topic: LayerManagerTopics): void { | ||
const subscribers = this._subscribers.get(topic); | ||
if (subscribers) { | ||
subscribers.forEach((subscriber) => subscriber()); | ||
} | ||
} | ||
|
||
private makeUniqueLayerName(name: string): string { | ||
let potentialName = name; | ||
let i = 1; | ||
while (this._layers.some((layer) => layer.getName() === potentialName)) { | ||
potentialName = `${name} (${i})`; | ||
i++; | ||
} | ||
return potentialName; | ||
} | ||
} |
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
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
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
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