Skip to content

Commit

Permalink
Refactor remove/hide layers
Browse files Browse the repository at this point in the history
Signed-off-by: Vijayan Balasubramanian <[email protected]>
  • Loading branch information
VijayanB committed Jan 31, 2023
1 parent a53df97 commit ebabf62
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 44 deletions.
4 changes: 2 additions & 2 deletions public/components/layer_control_panel/layer_control_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
} from '../../model/layerRenderController';
import { MapState } from '../../model/mapState';
import { ConfigSchema } from '../../../common/config';
import {moveLayers, updateLayerVisibility} from "../../model/map/layer_operations";
import {moveLayers, removeLayers, updateLayerVisibility} from "../../model/map/layer_operations";

interface MaplibreRef {
current: Maplibre | null;
Expand Down Expand Up @@ -300,7 +300,7 @@ export const LayerControlPanel = memo(

const onDeleteLayerConfirm = () => {
if (selectedDeleteLayer) {
layersFunctionMap[selectedDeleteLayer.type]?.remove(maplibreRef, selectedDeleteLayer);
removeLayers(maplibreRef.current!, selectedDeleteLayer.id, true);
removeLayer(selectedDeleteLayer.id);
setIsDeleteLayerModalVisible(false);
setSelectedDeleteLayer(undefined);
Expand Down
26 changes: 4 additions & 22 deletions public/model/OSMLayerFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Map as Maplibre, LayerSpecification } from 'maplibre-gl';
import { OSMLayerSpecification } from './mapLayerType';
import { getMaplibreBeforeLayerId } from './layersFunctions';
import { hasLayer } from './map/layer_operations';
import { hasLayer, removeLayers } from './map/layer_operations';

interface MaplibreRef {
current: Maplibre | null;
Expand Down Expand Up @@ -99,26 +99,8 @@ export const OSMLayerFunctions = {
) => {
// If layer already exist in maplibre source, update layer config
// else add new layer.
if (hasLayer(maplibreRef.current!, layerConfig.id)) {
updateLayerConfig(layerConfig, maplibreRef);
} else {
addNewLayer(layerConfig, maplibreRef, beforeLayerId);
}
},
remove: (maplibreRef: MaplibreRef, layerConfig: OSMLayerSpecification) => {
const layers = getCurrentStyleLayers(maplibreRef);
layers.forEach((mbLayer: { id: any }) => {
if (mbLayer.id.includes(layerConfig.id)) {
maplibreRef.current?.removeLayer(mbLayer.id);
}
});
},
hide: (maplibreRef: MaplibreRef, layerConfig: OSMLayerSpecification) => {
const layers = getCurrentStyleLayers(maplibreRef);
layers.forEach((mbLayer: { id: any }) => {
if (mbLayer.id.includes(layerConfig.id)) {
maplibreRef.current?.setLayoutProperty(mbLayer.id, 'visibility', layerConfig.visibility);
}
});
return hasLayer(maplibreRef.current!, layerConfig.id)
? updateLayerConfig(layerConfig, maplibreRef)
: addNewLayer(layerConfig, maplibreRef, beforeLayerId);
},
};
25 changes: 5 additions & 20 deletions public/model/customLayerFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Map as Maplibre, AttributionControl, RasterSourceSpecification } from 'maplibre-gl';
import { CustomLayerSpecification, OSMLayerSpecification } from './mapLayerType';
import { getMaplibreBeforeLayerId } from './layersFunctions';
import { hasLayer } from './map/layer_operations';
import { hasLayer, removeLayers } from './map/layer_operations';

interface MaplibreRef {
current: Maplibre | null;
Expand Down Expand Up @@ -98,26 +98,11 @@ export const CustomLayerFunctions = {
layerConfig: CustomLayerSpecification,
beforeLayerId: string | undefined
) => {
if (hasLayer(maplibreRef.current!, layerConfig.id)) {
updateLayerConfig(layerConfig, maplibreRef);
} else {
addNewLayer(layerConfig, maplibreRef, beforeLayerId);
}
return hasLayer(maplibreRef.current!, layerConfig.id)
? updateLayerConfig(layerConfig, maplibreRef)
: addNewLayer(layerConfig, maplibreRef, beforeLayerId);
},
remove: (maplibreRef: MaplibreRef, layerConfig: OSMLayerSpecification) => {
const layers = getCurrentStyleLayers(maplibreRef);
layers.forEach((mbLayer: { id: any }) => {
if (mbLayer.id.includes(layerConfig.id)) {
maplibreRef.current?.removeLayer(mbLayer.id);
}
});
},
hide: (maplibreRef: MaplibreRef, layerConfig: OSMLayerSpecification) => {
const layers = getCurrentStyleLayers(maplibreRef);
layers.forEach((mbLayer: { id: any }) => {
if (mbLayer.id.includes(layerConfig.id)) {
maplibreRef.current?.setLayoutProperty(mbLayer.id, 'visibility', layerConfig.visibility);
}
});
removeLayers(maplibreRef.current!, layerConfig.id, true);
},
};

0 comments on commit ebabf62

Please sign in to comment.