Skip to content

Commit

Permalink
geosolutions-it#10026: Interactive legend for TOC layers
Browse files Browse the repository at this point in the history
Description:
- implement reset legend filter in case changing styles for WMS
  • Loading branch information
mahmoudadel54 committed Apr 2, 2024
1 parent 124e01b commit c291f32
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
9 changes: 9 additions & 0 deletions web/client/actions/layerFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export const OPEN_QUERY_BUILDER = 'LAYER_FILTER:OPEN_QUERY_BUILDER';

export const LAYER_FILTER_BY_LEGEND = 'LAYER_FILTER:LAYER_FILTER_BY_LEGEND';

export const RESET_LAYER_FILTER_BY_LEGEND = 'LAYER_FILTER:RESET_LAYER_FILTER_BY_LEGEND';

export function storeCurrentFilter() {
return {
type: STORE_CURRENT_APPLIED_FILTER
Expand Down Expand Up @@ -71,3 +73,10 @@ export function layerFilterByLegend(layerId, nodeType, legendCQLFilter) {
layerId
};
}

export function resetLegendFilter(style) {
return {
type: RESET_LAYER_FILTER_BY_LEGEND,
newSelectedStyle: style
};
}
16 changes: 14 additions & 2 deletions web/client/components/TOC/fragments/StyleBasedWMSJsonLegend.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { getJsonWMSLegend } from '../../../api/WMS';
import Message from '../../I18N/Message';

class Legend extends React.Component {
class StyleBasedWMSJsonLegend extends React.Component {
static propTypes = {
layer: PropTypes.object,
legendHeight: PropTypes.number,
Expand Down Expand Up @@ -50,6 +50,18 @@ class Legend extends React.Component {
jsonLegend: {}
}
componentDidMount() {
this.getLegendData();
}

componentDidUpdate(prevProps) {
const prevLayerStyle = prevProps?.layer?.style;
const currentLayerStyle = this.props?.layer?.style;
if (currentLayerStyle !== prevLayerStyle) {
this.getLegendData();
}
}

getLegendData() {
let jsonLegendUrl = this.getUrl(this.props);
getJsonWMSLegend(jsonLegendUrl).then(data => {
this.setState({ jsonLegend: data[0], loading: false });
Expand Down Expand Up @@ -132,4 +144,4 @@ class Legend extends React.Component {
}
}

export default Legend;
export default StyleBasedWMSJsonLegend;
6 changes: 5 additions & 1 deletion web/client/components/styleeditor/StyleList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const StyleList = ({
defaultStyle,
availableStyles = [],
onSelect,
onResetLegendFilterStyle = () => {},
formatColors = {
sld: "#33ffaa",
css: "#ffaa33"
Expand All @@ -80,7 +81,10 @@ const StyleList = ({
>
<SideGrid
size="sm"
onItemClick={({ name }) => onSelect({ style: name }, true)}
onItemClick={({ name }) => {
onResetLegendFilterStyle(name);
onSelect({ style: name }, true);
}}
items={availableStyles
.filter(
({
Expand Down
34 changes: 32 additions & 2 deletions web/client/epics/layerfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
DISCARD_CURRENT_FILTER,
APPLY_FILTER,
storeAppliedFilter,
LAYER_FILTER_BY_LEGEND
LAYER_FILTER_BY_LEGEND,
RESET_LAYER_FILTER_BY_LEGEND
} from '../actions/layerFilter';
import { featureTypeSelected, toggleLayerFilter, initQueryPanel } from '../actions/wfsquery';
import { getSelectedLayer } from '../selectors/layers';
Expand Down Expand Up @@ -168,9 +169,38 @@ export const applyWMSCQLFilterBasedOnLegendFilter = (action$, {getState}) => {
});
};

export const applyResetLegendFilter = (action$, {getState}) => {
return action$.ofType(RESET_LAYER_FILTER_BY_LEGEND)
.switchMap((action) => {
const state = getState();
const layer = getSelectedLayer(state);
// check if the chnaged param is style or not, if not cancel the epic
// const isChangeStyleParam = Object.keys(action.newParams).length === 1 && action.newParams?.style;
// check if the selected style is different than the current layer's one, if not cancel the epic
const isNewStyleSelected = layer.style !== action?.newSelectedStyle;
// check if the layer has interactive legend or not, if not cancel the epic
const isLayerWithJSONLegend = layer?.enableInteractiveLegend;
if (!isNewStyleSelected || !isLayerWithJSONLegend) return Rx.Observable.empty();
const queryFormFilterObj = {...get(getState(), "queryform", {})};
let filterObj = layer.layerFilter ? layer.layerFilter : queryFormFilterObj;
let searchUrl = layer.search.url;
// reset thte filter if legendCQLFilter is empty
const isLegendFilterExist = filterObj?.filters?.find(f => f.id === 'interactiveLegend');
if (isLegendFilterExist) {
filterObj = {
...filterObj, filters: filterObj?.filters?.filter(f => f.id !== 'interactiveLegend')
};
let newFilter = isNotEmptyFilter(filterObj) ? filterObj : undefined;
return Rx.Observable.of(search(searchUrl, newFilter), addFilterToLayer(layer.id, newFilter), storeAppliedFilter(newFilter));
}
return Rx.Observable.empty();
});
};

export default {
handleLayerFilterPanel,
restoreSavedFilter,
onApplyFilter,
applyWMSCQLFilterBasedOnLegendFilter
applyWMSCQLFilterBasedOnLegendFilter,
applyResetLegendFilter
};
4 changes: 3 additions & 1 deletion web/client/plugins/styleeditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { createSelector } from 'reselect';
import { updateOptionsByOwner } from '../../actions/additionallayers';
import { getLayerCapabilities } from '../../actions/layerCapabilities';
import { updateSettingsParams } from '../../actions/layers';
import { resetLegendFilter } from '../../actions/layerFilter';
import {
addStyle,
createStyle,
Expand Down Expand Up @@ -121,7 +122,8 @@ export const StyleList = compose(
})
),
{
onSelect: updateSettingsParams
onSelect: updateSettingsParams,
onResetLegendFilterStyle: resetLegendFilter
}
),
withState('filterText', 'onFilter', ''),
Expand Down

0 comments on commit c291f32

Please sign in to comment.