Skip to content

Commit

Permalink
Refactor visibility property
Browse files Browse the repository at this point in the history
Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
  • Loading branch information
VijayanB committed Jan 31, 2023
1 parent ff5d859 commit 2175dd5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 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} from "../../model/map/layer_operations";
import {moveLayers, updateVisibility} from "../../model/map/layer_operations";

interface MaplibreRef {
current: Maplibre | null;
Expand Down Expand Up @@ -290,7 +290,7 @@ export const LayerControlPanel = memo(
layer.visibility = LAYER_VISIBILITY.VISIBLE;
setLayerVisibility(new Map(layerVisibility.set(layer.id, true)));
}
layersFunctionMap[layer.type]?.hide(maplibreRef, layer);
updateVisibility(maplibreRef.current!, layer.id, layer.visibility);
};

const onDeleteLayerIconClick = (layer: MapLayerSpecification) => {
Expand Down
24 changes: 10 additions & 14 deletions public/model/documentLayerFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { Map as Maplibre } from 'maplibre-gl';
import { parse } from 'wellknown';
import { DocumentLayerSpecification } from './mapLayerType';
import { convertGeoPointToGeoJSON, isGeoJSON } from '../utils/geo_formater';
import { getMaplibreBeforeLayerId} from './layersFunctions';
import { getMaplibreBeforeLayerId } from './layersFunctions';
import {
addCircleLayer,
addLineLayer,
addPolygonLayer, hasLayer, removeLayers,
addPolygonLayer,
hasLayer,
removeLayers,
updateCircleLayer,
updateLineLayer,
updatePolygonLayer,
updateVisibility,
} from './map/layer_operations';

interface MaplibreRef {
Expand Down Expand Up @@ -175,7 +178,7 @@ const addNewLayer = (
}
};

const updateLayerConfig = (
const updateLayer = (
layerConfig: DocumentLayerSpecification,
maplibreRef: MaplibreRef,
data: any
Expand Down Expand Up @@ -230,21 +233,14 @@ export const DocumentLayerFunctions = {
data: any,
beforeLayerId: string | undefined
) => {
if (hasLayer(maplibreRef.current!, layerConfig.id)) {
updateLayerConfig(layerConfig, maplibreRef, data);
} else {
addNewLayer(layerConfig, maplibreRef, data, beforeLayerId);
}
return hasLayer(maplibreRef.current!, layerConfig.id)
? updateLayer(layerConfig, maplibreRef, data)
: addNewLayer(layerConfig, maplibreRef, data, beforeLayerId);
},
remove: (maplibreRef: MaplibreRef, layerConfig: DocumentLayerSpecification) => {
removeLayers(maplibreRef.current!, layerConfig.id, true);
},
hide: (maplibreRef: MaplibreRef, layerConfig: DocumentLayerSpecification) => {
const layers = getCurrentStyleLayers(maplibreRef);
layers.forEach((layer) => {
if (layer.id.includes(layerConfig.id)) {
maplibreRef.current?.setLayoutProperty(layer.id, 'visibility', layerConfig.visibility);
}
});
updateVisibility(maplibreRef.current!, layerConfig.id, layerConfig.visibility);
},
};
15 changes: 14 additions & 1 deletion public/model/map/layer_operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
hasLayer, moveLayers, removeLayers,
updateCircleLayer,
updateLineLayer,
updatePolygonLayer,
updatePolygonLayer, updateVisibility,
} from './layer_operations';
import { Map as Maplibre } from 'maplibre-gl';
import { MockMaplibreMap } from './__mocks__/map';
Expand Down Expand Up @@ -359,3 +359,16 @@ describe('delete layer', function () {
).toEqual([]);
});
});

describe('update visibility', function () {
it('should update visibility for given layer', function () {
const mockLayer1: MockLayer = new MockLayer('layer-1');
const mockLayer2: MockLayer = new MockLayer('layer-11');
mockLayer1.setProperty('visibility', 'none');
const mockMap: MockMaplibreMap = new MockMaplibreMap([mockLayer1, mockLayer2]);
updateVisibility((mockMap as unknown) as Maplibre, 'layer-1', 'visible');
expect(mockMap.getLayers().map((layer) => String(layer.getProperty('visibility')))).toEqual(
Array(2).fill('visible')
);
});
});
8 changes: 7 additions & 1 deletion public/model/map/layer_operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const getLayers = (map: Maplibre, dashboardMapsLayerId?: string): LayerSp
: layers;
};

export const hasLayer = (map: Maplibre, dashboardMapsLayerId: string) => {
export const hasLayer = (map: Maplibre, dashboardMapsLayerId: string): boolean => {
const maplibreMapLayers = getLayers(map);
for (const layer of maplibreMapLayers) {
if (layer.id.includes(dashboardMapsLayerId)) {
Expand Down Expand Up @@ -42,6 +42,12 @@ export const removeLayers = (map: Maplibre, layerId: string, removeSource?: bool
}
};

export const updateVisibility = (map: Maplibre, layerId: string, visibility: string) => {
getLayers(map, layerId).forEach((layer) => {
map.setLayoutProperty(layer.id, 'visibility', visibility);
});
};

export interface LineLayerSpecification {
sourceId: string;
visibility: string;
Expand Down

0 comments on commit 2175dd5

Please sign in to comment.