Skip to content

Commit

Permalink
wire up layer visibility, with only the default layers, in a p.o.c. k…
Browse files Browse the repository at this point in the history
…ind of way

fix white-space value
  • Loading branch information
mbwatson committed May 1, 2024
1 parent 46f4939 commit a109812
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
27 changes: 13 additions & 14 deletions src/components/map/default-layers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment, useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react';
import { WMSTileLayer, GeoJSON } from 'react-leaflet';
import { CircleMarker } from 'leaflet';
import { useLayers } from '@context';
Expand Down Expand Up @@ -87,7 +87,12 @@ export const DefaultLayers = () => {
data.workbench.forEach(function (layer_id) {
const layer = getCatalogEntry(data.catalog, layer_id);
if (layer)
layer_list.push(layer);
layer_list.push({
...layer,
state: {
visible: true,
}
});

// TODO: do we really need to do this here??!
// if this is an obs layer, need to retrieve
Expand Down Expand Up @@ -130,15 +135,12 @@ export const DefaultLayers = () => {
getDefaultLayers().then();
}, []);

//console.log("defaultModelLayers: " + JSON.stringify(defaultModelLayers, null, 2))

return (
<>
{defaultModelLayers.map((layer, index) => {
return defaultModelLayers
.filter(layer => layer.state.visible)
.map((layer, index) => {
const pieces = layer.id.split('-');
const type = pieces[pieces.length-1];
//console.log("type: " + JSON.stringify(type, null, 2))
if( type === "obs" && obsData !== "") {
if (type === "obs" && obsData !== "") {
//console.log("obsData: " + JSON.stringify(obsData, null, 2));
return (
<GeoJSON
Expand All @@ -148,8 +150,7 @@ export const DefaultLayers = () => {
onEachFeature = {onEachObsFeature}
/>
);
}
else {
} else {
return (
<WMSTileLayer
key = {index}
Expand All @@ -168,7 +169,5 @@ export const DefaultLayers = () => {
/>
);
}
})};
</>
);
});
};
24 changes: 5 additions & 19 deletions src/components/trays/layers/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ import {
import { useLayers } from '@context';

export const LayersList = () => {
const { defaultModelLayers } = useLayers();
const { defaultModelLayers, toggleLayerVisibility } = useLayers();
const layers = [...defaultModelLayers];

console.log(layers);
console.table(layers[0]);

const [expandedIds, setExpandedIds] = useState(new Set());
// of course, this is dummy state.
// real state will be maintained in some higher-up context.
const [visibleIds, setVisibleIds] = useState(new Set());

const handleToggleExpansion = id => () => {
const _expandedIds = new Set([...expandedIds]);
Expand All @@ -39,23 +36,12 @@ export const LayersList = () => {
setExpandedIds(_expandedIds);
};

const handleToggleVisibilitySwitch = id => () => {
const _visibleIds = new Set([...visibleIds]);
if (_visibleIds.has(id)) {
_visibleIds.delete(id);
setVisibleIds(_visibleIds);
return;
}
_visibleIds.add(id);
setVisibleIds(_visibleIds);
};

return (
<AccordionGroup variant="soft">
{
layers.map(layer => {
const isExpanded = expandedIds.has(layer.id);
const isVisible = visibleIds.has(layer.id);
const isVisible = layer.state.visible;

return (
<Accordion
Expand Down Expand Up @@ -107,7 +93,7 @@ export const LayersList = () => {
sx={{
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'no-wrap',
whiteSpace: 'nowrap',
}}
>
{ layer.layers }
Expand All @@ -116,8 +102,8 @@ export const LayersList = () => {

<Switch
size="sm"
onChange={ handleToggleVisibilitySwitch(layer.id) }
checked={ isVisible }
onChange={ () => toggleLayerVisibility(layer.id) }
/>

<IconButton onClick={ handleToggleExpansion(layer.id) }>
Expand Down
25 changes: 21 additions & 4 deletions src/context/map-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,25 @@ export const LayersContext = createContext({});
export const useLayers = () => useContext(LayersContext);

export const LayersProvider = ({ children }) => {
const [defaultModelLayers, setDefaultModelLayers] = useState([]);
const [filteredModelLayers, setFilteredModelLayers] = useState([]);
const [map, setMap] = useState(null);
const [defaultModelLayers, setDefaultModelLayers] = useState([]);
const [filteredModelLayers, setFilteredModelLayers] = useState([]);
const [map, setMap] = useState(null);

const toggleLayerVisibility = id => {
const newLayers = [...defaultModelLayers];
const index = newLayers.findIndex(l => l.id === id);
if (index === -1) {
new Error('Could not locate layer', id);
return;
}
const alteredLayer = newLayers[index];
alteredLayer.state.visible = !alteredLayer.state.visible;
setDefaultModelLayers([
...newLayers.slice(0, index),
{ ...alteredLayer },
...newLayers.slice(index + 1),
]);
};

return (
<LayersContext.Provider
Expand All @@ -17,7 +33,8 @@ export const LayersProvider = ({ children }) => {
defaultModelLayers,
setDefaultModelLayers,
filteredModelLayers,
setFilteredModelLayers
setFilteredModelLayers,
toggleLayerVisibility,
}}
>
{children}
Expand Down

0 comments on commit a109812

Please sign in to comment.