Skip to content

Commit

Permalink
Refactor metadata viewing and related functions to reduce duplication (
Browse files Browse the repository at this point in the history
…#277)

Co-authored-by: Rob Quincey <rob.quincey@dorsetcouncil.gov.uk>
  • Loading branch information
VulpesFerrilata and RobQuincey-DC authored Jun 11, 2024
1 parent e667da7 commit 692e9f7
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { FeaturePropertiesHelper } from "./FeaturePropertiesHelper";
import { FeatureQueryTemplateHelper } from "./FeatureQueryTemplateHelper";
import { Source } from "ol/source";
import LayerRenderer from "ol/renderer/Layer";
import { MetadataViewer } from "../Metadata/MetadataViewer";

export class FeatureQueryResultRenderer {
_gifwMapInstance: GIFWMap;
Expand Down Expand Up @@ -172,6 +173,10 @@ export class FeatureQueryResultRenderer {
);
}

popupActions.push(new GIFWPopupAction("About this layer", () => {
MetadataViewer.showMetadataModal(gifwLayer, layer, this._gifwMapInstance)
},false,false))

if (parentResponses) {
popupActions.push(
new GIFWPopupAction(
Expand Down
38 changes: 38 additions & 0 deletions GIFrameworkMaps.Web/Scripts/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,44 @@ export class GIFWMap {
return withinBasemapExtent && withinMapProjectionExtent;
}

/**
* Returns a boolean indicating if the layer has a filter applied to it
* A 'user editable' filter is one they have either applied themselves, or a default one
* (applied by admins) that the user is allowed to modify
* @param layer The layer configuration information
* @param olLayer The OpenLayers layer
* @param userEditableOnly If true, will return true if a CQL filter is applied AND the user is able to edit it
* @return Boolean indicating if it does have a filter applied
*/
public getLayerFilteredStatus(
layer: Layer,
olLayer: olLayer.Layer,
userEditableOnly: boolean = true,
): boolean {
if (olLayer.get("gifw-filter-applied")) {
return true;
}
const source = olLayer.getSource();
if (source instanceof TileWMS || source instanceof ImageWMS) {
const params = (source as TileWMS | ImageWMS).getParams();
let cqlFilter: string;
for (const property in params) {
if (property.toLowerCase() === "cql_filter") {
cqlFilter = params[property];
}
}

if (cqlFilter) {
if (userEditableOnly) {
return layer.defaultFilterEditable;
} else {
return true;
}
}
}
return false;
}

/**
* Gets all Legend URLs for layers that are legendable, and a list of layer names that are not legendable
* @param additionalLegendOptions Optional string of additoinal options to add to the LEGEND_OPTIONS parameter of the GetLegendGraphic request
Expand Down
10 changes: 7 additions & 3 deletions GIFrameworkMaps.Web/Scripts/Metadata/MetadataViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Layer as olLayer } from "ol/layer";
import { ImageWMS, TileWMS } from "ol/source";
import { Metadata } from "./Metadata";
import { Helper, Mapping as MappingUtil } from "../Util";
import { GIFWMap } from "../Map";

export class MetadataViewer {
static async getCSWMetadataForLayer(
Expand Down Expand Up @@ -372,8 +373,7 @@ export class MetadataViewer {
static async showMetadataModal(
layerConfig: Layer,
olLayer: olLayer,
isFiltered: boolean = false,
proxyEndpoint: string = "",
gifwMapInstance: GIFWMap
) {
const metaModal = new Modal(document.getElementById("meta-modal"), {});
const metaModalContent = document.querySelector("#meta-modal .modal-body");
Expand All @@ -391,7 +391,11 @@ export class MetadataViewer {

metaModalContent.innerHTML = descriptionHTML;
metaModal.show();

let proxyEndpoint = ""
if (layerConfig.proxyMetaRequests) {
proxyEndpoint = `${document.location.protocol}//${gifwMapInstance.config.appRoot}proxy`;
}
const isFiltered = gifwMapInstance.getLayerFilteredStatus(layerConfig, olLayer, false);
const metadata = await MetadataViewer.getCSWMetadataForLayer(
layerConfig,
olLayer,
Expand Down
7 changes: 1 addition & 6 deletions GIFrameworkMaps.Web/Scripts/Panels/BasemapsPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,12 @@ export class BasemapsPanel implements SidebarPanel {
const layerConfig = (layerGroup.layers as Layer[]).filter(
(l) => l.id == eTarget.dataset.gifwAboutBasemap,
);
let proxyEndpoint = "";
if (layerConfig[0].proxyMetaRequests) {
proxyEndpoint = `${document.location.protocol}//${this.gifwMapInstance.config.appRoot}proxy`;
}
if (layerConfig && layerConfig.length === 1) {
const olLayer = this.gifwMapInstance.getActiveBasemap();
MetadataViewer.showMetadataModal(
layerConfig[0],
olLayer,
undefined,
proxyEndpoint,
this.gifwMapInstance,
);
}
e.preventDefault();
Expand Down
14 changes: 2 additions & 12 deletions GIFrameworkMaps.Web/Scripts/Panels/LayerList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,10 @@ export class LayerList {
(l) => l.id == eTarget.dataset.gifwAboutLayer,
);
if (layerConfig && layerConfig.length === 1) {
const isFiltered = this.layersPanelInstance.getLayerFilteredStatus(
layer,
olLayer as olLayer,
false,
);
let proxyEndpoint = "";
if (layerConfig[0].proxyMetaRequests) {
proxyEndpoint = `${document.location.protocol}//${this.gifwMapInstance.config.appRoot}proxy`;
}
MetadataViewer.showMetadataModal(
layerConfig[0],
olLayer as olLayer,
isFiltered,
proxyEndpoint,
this.gifwMapInstance
);
}
e.preventDefault();
Expand All @@ -221,7 +211,7 @@ export class LayerList {
//Determine if the layer has a user editable filter applied,
//and display the button as filled if so
const icon = `bi-funnel${
this.layersPanelInstance.getLayerFilteredStatus(
this.gifwMapInstance.getLayerFilteredStatus(
layer,
olLayer as olLayer,
)
Expand Down
57 changes: 8 additions & 49 deletions GIFrameworkMaps.Web/Scripts/Panels/LayersPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,7 @@ export class LayersPanel implements SidebarPanel {
if (layerCheckboxLabelContainer) {
layerCheckboxLabelContainer.setAttribute(
"title",
`This layer is out of range. Zoom ${
newZoom > layer.getMaxZoom() ? "out" : "in"
`This layer is out of range. Zoom ${newZoom > layer.getMaxZoom() ? "out" : "in"
} to view.`,
);

Expand Down Expand Up @@ -1316,15 +1315,13 @@ export class LayersPanel implements SidebarPanel {
isActive: boolean = false,
): HTMLElement {
const styleLinkContainer = document.createElement("a");
styleLinkContainer.className = `list-group-item list-group-item-action ${
isActive ? "active" : ""
}`;
styleLinkContainer.className = `list-group-item list-group-item-action ${isActive ? "active" : ""
}`;
styleLinkContainer.href = "#";
styleLinkContainer.dataset.gifwLayerStyleName = style.name;
styleLinkContainer.innerHTML = `<h5 class="mb-2">${style.title}</h5>`;
styleLinkContainer.innerHTML += `<p class="mb-1">${
style.abstract ? style.abstract : "No description provided"
}</p>`;
styleLinkContainer.innerHTML += `<p class="mb-1">${style.abstract ? style.abstract : "No description provided"
}</p>`;

styleLinkContainer.addEventListener("click", (e) => {
const selectedStyleName = (e.currentTarget as HTMLElement).dataset
Expand Down Expand Up @@ -1382,9 +1379,8 @@ export class LayersPanel implements SidebarPanel {
LayerGroupType.Overlay,
]);
if (olLayer && layer) {
const icon = `bi-funnel${
this.getLayerFilteredStatus(layer, olLayer as olLayer) ? "-fill" : ""
}`;
const icon = `bi-funnel${this.gifwMapInstance.getLayerFilteredStatus(layer, olLayer as olLayer) ? "-fill" : ""
}`;
if (layersListFilterButton) {
layersListFilterButton.querySelector(".bi").className = `bi ${icon}`;
}
Expand All @@ -1397,41 +1393,4 @@ export class LayersPanel implements SidebarPanel {
private updateSortOrderPreference() {
UserSettings.setItem("LayerControlSortOrderPreference", this.listSortOrder);
}

/**
* Returns a boolean indicating if the layer has a user editable filter applied to it
* A 'user editable' filter is one they have either applied themselves, or a default one
* (applied by admins) that the user is allowed to modify
* @param layer The layer configuration information
* @param olLayer The OpenLayers layer
* @return Boolean indicating if it does have a user editable filter applied
*/
public getLayerFilteredStatus(
layer: Layer,
olLayer: olLayer,
userEditableOnly: boolean = true,
): boolean {
if (olLayer.get("gifw-filter-applied")) {
return true;
}
const source = olLayer.getSource();
if (source instanceof TileWMS || source instanceof ImageWMS) {
const params = (source as TileWMS | ImageWMS).getParams();
let cqlFilter: string;
for (const property in params) {
if (property.toLowerCase() === "cql_filter") {
cqlFilter = params[property];
}
}

if (cqlFilter) {
if (userEditableOnly) {
return layer.defaultFilterEditable;
} else {
return true;
}
}
}
return false;
}
}
}

0 comments on commit 692e9f7

Please sign in to comment.