Skip to content

Commit

Permalink
Update beforeMbLayerId to addLayer
Browse files Browse the repository at this point in the history
Signed-off-by: Junqiu Lei <[email protected]>
  • Loading branch information
junqiu-lei committed Dec 31, 2022
1 parent 88122ed commit d19c7d1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 35 deletions.
14 changes: 2 additions & 12 deletions maps_dashboards/public/model/OSMLayerFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Map as Maplibre, LayerSpecification } from 'maplibre-gl';
import { OSMLayerSpecification } from './mapLayerType';
import { getMaplibreBeforeLayerId } from './layersFunctions';
import { getMaplibreBeforeLayerId, layerExistInMbSource } from './layersFunctions';

interface MaplibreRef {
current: Maplibre | null;
Expand Down Expand Up @@ -43,16 +43,6 @@ const handleStyleLayers = (layerConfig: OSMLayerSpecification, maplibreRef: Mapl
});
};

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

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

interface MaplibreRef {
current: Maplibre | null;
Expand All @@ -9,16 +10,6 @@ const getCurrentStyleLayers = (maplibreRef: MaplibreRef) => {
return maplibreRef.current?.getStyle().layers || [];
};

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

const updateLayerConfig = (layerConfig: CustomLayerSpecification, maplibreRef: MaplibreRef) => {
const maplibreInstance = maplibreRef.current;
if (maplibreInstance) {
Expand Down Expand Up @@ -71,13 +62,14 @@ const addNewLayer = (
tileSize: 256,
attribution: layerSource?.attribution,
});
const beforeMbLayerId = getMaplibreBeforeLayerId(layerConfig, maplibreRef, beforeLayerId);
maplibreInstance.addLayer(
{
id: layerConfig.id,
type: 'raster',
source: layerConfig.id,
},
beforeLayerId
beforeMbLayerId
);
}
};
Expand All @@ -99,7 +91,7 @@ export const CustomLayerFunctions = {
layerConfig: CustomLayerSpecification,
beforeLayerId: string | undefined
) => {
if (layerExistInMbSource(layerConfig, maplibreRef)) {
if (layerExistInMbSource(layerConfig.id, maplibreRef)) {
updateLayerConfig(layerConfig, maplibreRef);
} else {
addNewLayer(layerConfig, maplibreRef, beforeLayerId);
Expand Down
12 changes: 1 addition & 11 deletions maps_dashboards/public/model/documentLayerFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Map as Maplibre, Popup, MapGeoJSONFeature } from 'maplibre-gl';
import { createPopup, getPopupLngLat } from '../components/tooltip/create_tooltip';
import { DocumentLayerSpecification } from './mapLayerType';
import { convertGeoPointToGeoJSON, isGeoJSON } from '../utils/geo_formater';
import { getMaplibreBeforeLayerId } from './layersFunctions';
import { getMaplibreBeforeLayerId, layerExistInMbSource } from './layersFunctions';

interface MaplibreRef {
current: Maplibre | null;
Expand All @@ -29,16 +29,6 @@ const GeoJSONMaplibreMap = new Map<string, string>([
['Polygon', 'fill'],
]);

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;
};

const getFieldValue = (data: any, name: string) => {
if (!name) {
return null;
Expand Down
11 changes: 11 additions & 0 deletions maps_dashboards/public/model/layersFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ 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

0 comments on commit d19c7d1

Please sign in to comment.