Skip to content

Commit

Permalink
Refactor get/has layer
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 daff0e5 commit 219c5dc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 37 deletions.
5 changes: 3 additions & 2 deletions public/model/OSMLayerFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Map as Maplibre, LayerSpecification } from 'maplibre-gl';
import { OSMLayerSpecification } from './mapLayerType';
import { getMaplibreBeforeLayerId, layerExistInMbSource } from './layersFunctions';
import { getMaplibreBeforeLayerId } from './layersFunctions';
import { hasLayer } from './map/layer_operations';

interface MaplibreRef {
current: Maplibre | null;
Expand Down Expand Up @@ -98,7 +99,7 @@ export const OSMLayerFunctions = {
) => {
// If layer already exist in maplibre source, update layer config
// else add new layer.
if (layerExistInMbSource(layerConfig.id, maplibreRef)) {
if (hasLayer(maplibreRef.current!, layerConfig.id)) {
updateLayerConfig(layerConfig, maplibreRef);
} else {
addNewLayer(layerConfig, maplibreRef, beforeLayerId);
Expand Down
5 changes: 3 additions & 2 deletions public/model/customLayerFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Map as Maplibre, AttributionControl, RasterSourceSpecification } from 'maplibre-gl';
import { CustomLayerSpecification, OSMLayerSpecification } from './mapLayerType';
import { getMaplibreBeforeLayerId, layerExistInMbSource } from './layersFunctions';
import { getMaplibreBeforeLayerId } from './layersFunctions';
import { hasLayer } from './map/layer_operations';

interface MaplibreRef {
current: Maplibre | null;
Expand Down Expand Up @@ -97,7 +98,7 @@ export const CustomLayerFunctions = {
layerConfig: CustomLayerSpecification,
beforeLayerId: string | undefined
) => {
if (layerExistInMbSource(layerConfig.id, maplibreRef)) {
if (hasLayer(maplibreRef.current!, layerConfig.id)) {
updateLayerConfig(layerConfig, maplibreRef);
} else {
addNewLayer(layerConfig, maplibreRef, beforeLayerId);
Expand Down
6 changes: 3 additions & 3 deletions public/model/documentLayerFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { Map as Maplibre } from 'maplibre-gl';
import { parse } from 'wellknown';
import { DocumentLayerSpecification } from './mapLayerType';
import { convertGeoPointToGeoJSON, isGeoJSON } from '../utils/geo_formater';
import { getMaplibreBeforeLayerId, layerExistInMbSource } from './layersFunctions';
import { getMaplibreBeforeLayerId} from './layersFunctions';
import {
addCircleLayer,
addLineLayer,
addPolygonLayer,
addPolygonLayer, hasLayer,
updateCircleLayer,
updateLineLayer,
updatePolygonLayer,
Expand Down Expand Up @@ -230,7 +230,7 @@ export const DocumentLayerFunctions = {
data: any,
beforeLayerId: string | undefined
) => {
if (layerExistInMbSource(layerConfig.id, maplibreRef)) {
if (hasLayer(maplibreRef.current!, layerConfig.id)) {
updateLayerConfig(layerConfig, maplibreRef, data);
} else {
addNewLayer(layerConfig, maplibreRef, data, beforeLayerId);
Expand Down
32 changes: 4 additions & 28 deletions public/model/layersFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { OSMLayerFunctions } from './OSMLayerFunctions';
import { DocumentLayerFunctions } from './documentLayerFunctions';
import { MapLayerSpecification } from './mapLayerType';
import { CustomLayerFunctions } from './customLayerFunctions';
import { getLayers } from './map/layer_operations';

interface MaplibreRef {
current: Maplibre | null;
Expand All @@ -22,24 +23,13 @@ interface MaplibreRef {
current: Maplibre | null;
}

const getAllMaplibreLayersIncludesId = (maplibreRef: MaplibreRef, layerId?: string) => {
if (!layerId && !maplibreRef) {
return [];
}
return (
maplibreRef.current
?.getStyle()
.layers.filter((layer) => layer.id?.includes(String(layerId)) === true) || []
);
};

export const LayerActions = {
move: (maplibreRef: MaplibreRef, sourceId: string, beforeId?: string) => {
const sourceMaplibreLayers = getAllMaplibreLayersIncludesId(maplibreRef, sourceId);
const sourceMaplibreLayers = getLayers(maplibreRef.current!, sourceId);
if (!sourceMaplibreLayers) {
return;
}
const beforeMaplibreLayers = getAllMaplibreLayersIncludesId(maplibreRef, beforeId);
const beforeMaplibreLayers = getLayers(maplibreRef.current!, beforeId);
if (!beforeMaplibreLayers || beforeMaplibreLayers.length < 1) {
// move to top
sourceMaplibreLayers.forEach((layer) => maplibreRef.current?.moveLayer(layer.id));
Expand All @@ -65,16 +55,12 @@ export const layersTypeNameMap: { [key: string]: string } = {
[DASHBOARDS_MAPS_LAYER_TYPE.CUSTOM_MAP]: DASHBOARDS_MAPS_LAYER_NAME.CUSTOM_MAP,
};

const getCurrentStyleLayers = (maplibreRef: MaplibreRef) => {
return maplibreRef.current?.getStyle().layers || [];
};

export const getMaplibreBeforeLayerId = (
selectedLayer: MapLayerSpecification,
maplibreRef: MaplibreRef,
beforeLayerId: string | undefined
): string | undefined => {
const currentLoadedMbLayers = getCurrentStyleLayers(maplibreRef);
const currentLoadedMbLayers = getLayers(maplibreRef.current!);
if (beforeLayerId) {
const beforeMbLayer = currentLoadedMbLayers.find((mbLayer) =>
mbLayer.id.includes(beforeLayerId)
Expand All @@ -84,16 +70,6 @@ export const getMaplibreBeforeLayerId = (
return undefined;
};

export const layerExistInMbSource = (layerConfigId: string, maplibreRef: MaplibreRef) => {
const layers = getCurrentStyleLayers(maplibreRef);
for (const layer in layers) {
if (layers[layer].id.includes(layerConfigId)) {
return true;
}
}
return false;
};

export const layersTypeIconMap: { [key: string]: string } = {
[DASHBOARDS_MAPS_LAYER_TYPE.OPENSEARCH_MAP]: DASHBOARDS_MAPS_LAYER_ICON.OPENSEARCH_MAP,
[DASHBOARDS_MAPS_LAYER_TYPE.DOCUMENTS]: DASHBOARDS_MAPS_LAYER_ICON.DOCUMENTS,
Expand Down
9 changes: 7 additions & 2 deletions public/model/map/__mocks__/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ export class MockMaplibreMap {
return this._styles.layers.filter((layer) => (layer.getProperty('id') as string).includes(id));
}

getStyle(): { layers: MockLayer[] } {
getStyle(): { layers: LayerSpecification[] } {
const layerSpecs: LayerSpecification[] = this._styles.layers.map((layer) => {
return {
id: String(layer.getProperty('id')),
} as LayerSpecification;
});
return {
layers: [...this._styles.layers],
layers: layerSpecs,
};
}

Expand Down
24 changes: 24 additions & 0 deletions public/model/map/layer_operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {
addCircleLayer,
addLineLayer,
addPolygonLayer,
getLayers, hasLayer,
updateCircleLayer,
updateLineLayer,
updatePolygonLayer,
} from './layer_operations';
import { Map as Maplibre } from 'maplibre-gl';
import { MockMaplibreMap } from './__mocks__/map';
import { MockLayer } from './__mocks__/layer';

describe('Circle layer', () => {
it('add new circle layer', () => {
Expand Down Expand Up @@ -277,3 +279,25 @@ describe('Polygon layer', () => {
expect(outlineLayer.getProperty('line-width')).toBe(7);
});
});

describe('get layer', () => {
it('should get layer successfully', function () {
const mockLayer: MockLayer = new MockLayer('layer-1');
const mockMap: MockMaplibreMap = new MockMaplibreMap([mockLayer]);
const actualLayers = getLayers((mockMap as unknown) as Maplibre, 'layer-1');
expect(actualLayers.length).toBe(1);
expect(actualLayers[0].id).toBe(mockLayer.getProperty('id'));
});

it('should confirm no layer exists', function () {
const mockLayer: MockLayer = new MockLayer('layer-1');
const mockMap: MockMaplibreMap = new MockMaplibreMap([mockLayer]);
expect(hasLayer((mockMap as unknown) as Maplibre, 'layer-2')).toBe(false);
});

it('should confirm layer exists', function () {
const mockLayer: MockLayer = new MockLayer('layer-1');
const mockMap: MockMaplibreMap = new MockMaplibreMap([mockLayer]);
expect(hasLayer((mockMap as unknown) as Maplibre, 'layer-1')).toBe(true);
});
});

0 comments on commit 219c5dc

Please sign in to comment.