Skip to content

Commit

Permalink
DocumentHandler map link mechanism correctly handles group layers:
Browse files Browse the repository at this point in the history
- Closes #1286.
- Utilizing the same mechanism as the rest of Hajk for showing/hiding layer is the prefered way to go. So I replaced the custom logic with a reference to the one in AppModel.
  • Loading branch information
jacobwod committed Feb 13, 2023
1 parent f0f884a commit e8d9a8d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 43 deletions.
1 change: 1 addition & 0 deletions new-client/src/plugins/DocumentHandler/DocumentHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class DocumentHandler extends React.PureComponent {
this.localObserver = Observer();

this.mapViewModel = new MapViewModel({
appModel: props.app,
localObserver: this.localObserver,
globalObserver: props.app.globalObserver,
map: props.map,
Expand Down
54 changes: 11 additions & 43 deletions new-client/src/plugins/DocumentHandler/MapViewModel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default class MapViewModel {
constructor(settings) {
this.appModel = settings.appModel;
this.localObserver = settings.localObserver;
this.globalObserver = settings.globalObserver;
this.map = settings.map;
Expand All @@ -19,6 +20,7 @@ export default class MapViewModel {
center: center,
zoom: params.get("z") || this.map.getView().getZoom(),
layers: params.get("l"), // Allow 'null', we handle it later
groupLayers: params.get("gl") || "{}", // Default to an empty stringified JSON object
};
} catch (error) {
console.error(error);
Expand All @@ -28,6 +30,7 @@ export default class MapViewModel {
center: this.map.getView().getCenter(),
zoom: this.map.getView().getZoom(),
layers: null,
groupLayers: "{}", // Default to an empty stringified JSON object
};
}
};
Expand All @@ -38,55 +41,20 @@ export default class MapViewModel {
const mapSettings = this.convertMapSettingsUrlToOlSettings(url);

if (mapSettings.layers !== null) {
const visibleLayers = mapSettings.layers.split(",");
const { layersToShow, layersToHide } =
this.getLayersToShowAndHide(visibleLayers);

this.setMapLayersVisibility(layersToShow, layersToHide);
// Let's use Hajk's generic layer visibility
// mechanism as exposed in AppModel
this.appModel.setLayerVisibilityFromParams(
mapSettings.layers,
mapSettings.groupLayers
);
}

// Let's ensure we end up in the correct location, even
// if no layer's visibility was changed
this.flyTo(this.map.getView(), mapSettings.center, mapSettings.zoom);
});
};

getLayersToShowAndHide = (visibleLayers) => {
const layersInMap = this.map.getLayers().getArray();
return layersInMap.reduce(
(layers, layer) => {
if (
layer.getProperties()["layerInfo"] &&
layer.getProperties()["layerInfo"]["layerType"]
) {
if (visibleLayers.includes(layer.getProperties()["name"])) {
layers.layersToShow.push(layer);
} else {
layers.layersToHide.push(layer);
}
}
return layers;
},
{ layersToShow: [], layersToHide: [] }
);
};

setMapLayersVisibility(layersToShow, layersToHide) {
layersToShow.forEach((mapLayerToShow) => {
if (mapLayerToShow.get("layerType") === "group") {
this.globalObserver.publish("layerswitcher.showLayer", mapLayerToShow);
} else if (!mapLayerToShow.getVisible()) {
mapLayerToShow.setVisible(true);
}
});

layersToHide.forEach((mapLayerToHide) => {
if (mapLayerToHide.get("layerType") === "group") {
this.globalObserver.publish("layerswitcher.hideLayer", mapLayerToHide);
} else if (mapLayerToHide.getVisible()) {
mapLayerToHide.setVisible(false);
}
});
}

flyTo(view, center, zoom) {
view.animate({
center: center,
Expand Down

0 comments on commit e8d9a8d

Please sign in to comment.