Skip to content

Commit

Permalink
Adding reset context for coming in/out of compare mode
Browse files Browse the repository at this point in the history
  • Loading branch information
PhillipsOwen committed Sep 30, 2024
1 parent 587eb9f commit fe427f8
Showing 1 changed file with 48 additions and 38 deletions.
86 changes: 48 additions & 38 deletions src/context/map-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const LayersProvider = ({ children }) => {
*/
// default for the pane compare name
const defaultSelected = 'Not Selected';
const [needsLayerDefaultView, setNeedsLayerDefaultView] = useState(false);

// create some state for the left/right name/type selections
const [leftPaneType, setLeftPaneType] = useState(defaultSelected);
Expand All @@ -78,7 +79,7 @@ export const LayersProvider = ({ children }) => {
* get the layer icon
*
* @param productType
* @returns {JSX.Element}
* @returns JSX.Element
*/
const getLayerIcon = ( productType )=> {
// grab the icon
Expand Down Expand Up @@ -112,6 +113,21 @@ export const LayersProvider = ({ children }) => {
*
*/
const resetCompare = () => {
// if there is something to reset
if (needsLayerDefaultView) {
// get the group id for the topmost (default) layer
const groupID = defaultModelLayers[0]['group'];

// turn on visibility for the default layer
const layer = defaultModelLayers.filter((l => l.group === groupID && l.properties['product_type'] === 'maxele63'));

// set the visibility of the default layer
toggleLayerVisibility(layer[0].id, true);

// no default layers need to be reset
setNeedsLayerDefaultView(false);
}

// clear the left layer type/ID/properties/layer
setLeftPaneType(defaultSelected);
setLeftPaneID(defaultSelected);
Expand Down Expand Up @@ -170,35 +186,41 @@ export const LayersProvider = ({ children }) => {
}
};

const toggleHurricaneLayerVisibility = id => {
const toggleHurricaneLayerVisibility = (id, visibility=undefined) => {
const newLayers = [...hurricaneTrackLayers];
const index = newLayers.findIndex(l => l.id === id);
if (index === -1) {
console.error('Could not locate layer', id);
return;
}
const alteredLayer = newLayers[index];
alteredLayer.state.visible = !alteredLayer.state.visible;

// set the layer visibility
alteredLayer.state.visible = (visibility === undefined) ? !alteredLayer.state.visible : visibility;

setHurricaneTrackLayers([
...newLayers.slice(0, index),
{ ...alteredLayer },
...newLayers.slice(index + 1),
]);
};

const toggleLayerVisibility = id => {
const toggleLayerVisibility = (id, visibility=undefined) => {
const newLayers = [...defaultModelLayers];
const index = newLayers.findIndex(l => l.id === id);
if (index === -1) {
console.error('Could not locate layer', id);
return;
}

// if this is an observation layer remove all observation layers/dialogs from the map
// remove all observation dialogs from the map on a change
removeObservations(id);

const alteredLayer = newLayers[index];
alteredLayer.state.visible = !alteredLayer.state.visible;

// set the layer visibility
alteredLayer.state.visible = (visibility === undefined) ? !alteredLayer.state.visible : visibility;

setDefaultModelLayers([
...newLayers.slice(0, index),
{ ...alteredLayer },
Expand All @@ -214,14 +236,14 @@ export const LayersProvider = ({ children }) => {
return;
}

// if this is an observation layer remove all observation layers/dialogs from the map
// remove all observation dialogs from the map on a change
removeObservations(id);

const alteredLayer = newLayers[index];
alteredLayer.state.visible = !alteredLayer.state.visible;

// if we are toggle a raster layer, turn off the other raster layers
// if this is a observation layer, just leave the raster layers alone
// if this is an observation layer, just leave the raster layers alone
let invisibleLayers = getAllRasterLayersInvisible();
if (alteredLayer.properties.product_type === "obs") {
invisibleLayers = [...newLayers];
Expand All @@ -248,22 +270,21 @@ export const LayersProvider = ({ children }) => {

const getAllHurricaneLayersInvisible = () => {
const currentLayers = [...hurricaneTrackLayers];
const alteredLayers = currentLayers
return currentLayers
.map((layer) => {
const opacity = layer.state.opacity;
return {
...layer,
state: {visible: false, opacity: opacity}
};
});

return alteredLayers;

};

// get ADCIRC raster layers as invisible
const getAllRasterLayersInvisible = () => {
const currentLayers = [...defaultModelLayers];
const alteredLayers = currentLayers
return currentLayers
.map((layer) => {
const opacity = layer.state.opacity;
if (layer.properties.product_type !== "obs") {
Expand All @@ -278,8 +299,6 @@ export const LayersProvider = ({ children }) => {
};
}
});

return alteredLayers;
};

const swapLayers = (i, j) => {
Expand Down Expand Up @@ -307,7 +326,7 @@ export const LayersProvider = ({ children }) => {
const newLayers = defaultModelLayers.filter(l => l.id !== id);
setDefaultModelLayers(newLayers);

// if this is a observation layer remove all observation layers/dialogs from the map
// remove all observation dialogs from the map on a change
removeObservations(id);
};

Expand All @@ -324,18 +343,18 @@ export const LayersProvider = ({ children }) => {
layer.state = newLayerDefaultState(layer, newLayers[0].group);
});

// remove all observation dialogs when there is a model run removal
// remove all observation dialogs from the map on a change
removeObservations();

setDefaultModelLayers(newLayers);
};

/**
* removes all selected model runs
* removes all selected model run
*/
const removeAllModelRuns = () => {

// remove all observation dialogs when removing all model runs
// remove all observation dialogs from the map on a change
removeObservations();

// reset the default layers array
Expand Down Expand Up @@ -372,32 +391,23 @@ export const LayersProvider = ({ children }) => {
return (
<LayersContext.Provider
value={{
map,
setMap,
defaultModelLayers,
setDefaultModelLayers,
hurricaneTrackLayers,
setHurricaneTrackLayers,
toggleHurricaneLayerVisibility,
toggleLayerVisibility,
toggleLayerVisibility2,
getAllLayersInvisible,
getAllHurricaneLayersInvisible,
map, setMap,
baseMap, setBaseMap,

defaultModelLayers, setDefaultModelLayers,
hurricaneTrackLayers, setHurricaneTrackLayers,
selectedObservations, setSelectedObservations,
showShareComment, setShowShareComment,
swapLayers,
removeLayer,
removeModelRun,
removeAllModelRuns,
removeObservations,
layerTypes,
getLayerIcon,
baseMap,
setBaseMap,
setLayerOpacity,

toggleHurricaneLayerVisibility, toggleLayerVisibility, toggleLayerVisibility2,
getAllLayersInvisible, getAllHurricaneLayersInvisible, getAllRasterLayersInvisible,
swapLayers, removeLayer, removeModelRun, removeAllModelRuns, removeObservations,
getLayerIcon, setLayerOpacity,

// declare access to the compare mode items
defaultSelected,
setNeedsLayerDefaultView,
leftPaneID, setLeftPaneID,
rightPaneID, setRightPaneID,
leftPaneType, setLeftPaneType,
Expand Down

0 comments on commit fe427f8

Please sign in to comment.