Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chore] Create factory for LayerLegendHeader and LayerLegendContent #1589

Merged
merged 2 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ export {default as LayerSelectorPanelFactory} from './map/layer-selector-panel';
export {default as LocalePanelFactory} from './map/locale-panel';
export {default as MapControlPanelFactory} from './map/map-control-panel';
export {default as MapControlTooltipFactory} from './map/map-control-tooltip';
export {default as MapLegendFactory} from './map/map-legend';
export {
default as MapLegendFactory,
LayerLegendHeaderFactory,
LayerLegendContentFactory
} from './map/map-legend';
export {default as MapDrawPanelFactory} from './map/map-draw-panel';
export {default as SplitMapButtonFactory} from './map/split-map-button';
export {default as MapLegendPanelFactory} from './map/map-legend-panel';
Expand Down Expand Up @@ -185,9 +189,6 @@ export {
LayerColorSelector
} from './side-panel/layer-panel/layer-configurator';

// map components
export {default as MapLegend} from 'components/map/map-legend';

export * from './common/styled-components';
import * as Icons from './common/icons';

Expand Down
27 changes: 27 additions & 0 deletions src/components/map/map-legend.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import {FunctionComponent} from 'react';
import {Layer, LayerConfig, VisualChannel, VisualChannelDescription} from 'layers';
import {MapLegendPanelComponent} from './map-legend-panel';

export type LayerSizeLegendProps = {
label: string,
name: string
};

export const LayerSizeLegend: FunctionComponent<LayerSizeLegendProps>;

export type SingleColorLegendProps = {
width: number,
color: string
Expand All @@ -18,6 +25,26 @@ export type LayerColorLegendProps = {

export const LayerColorLegend: FunctionComponent<LayerColorLegendProps>;

export type LayerLegendHeaderProps = {
layer: Layer,
options?: {
showLayerName?: boolean
}
};

export const LayerLegendHeader: FunctionComponent<LayerLegendHeaderProps>;

export function LayerLegendHeaderFactory(): LayerLegendHeader;

export type LayerLegendContentProps = {
layer: Layer,
containerW: number,
};

export const LayerLegendContent: FunctionComponent<LayerLegendContentProps>;

export function LayerLegendContentFactory(): LayerLegendContent;

export type MapLegendProps = {
layers?: ReadonlyArray<Layer>,
width?: number,
Expand Down
89 changes: 55 additions & 34 deletions src/components/map/map-legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const VisualChannelMetric = ({name}) => {
);
};

/** @type {typeof import('./map-legend').LayerSizeLegend} */
export const LayerSizeLegend = ({label, name}) => (
<div className="legend--layer_size-schema">
<p>
Expand Down Expand Up @@ -143,7 +144,58 @@ LayerColorLegend.displayName = 'LayerColorLegend';
const isColorChannel = visualChannel =>
[CHANNEL_SCALES.color, CHANNEL_SCALES.colorAggr].includes(visualChannel.channelScaleType);

function MapLegendFactory() {
export function LayerLegendHeaderFactory() {
/** @type {typeof import('./map-legend').LayerLegendHeader }> */
const LayerLegendHeader = ({options, layer}) => {
return options?.showLayerName !== false ? (
<div className="legend--layer_name">{layer.config.label}</div>
) : null;
};
return LayerLegendHeader;
}

export function LayerLegendContentFactory() {
/** @type {typeof import('./map-legend').LayerLegendContent }> */
const LayerLegendContent = ({layer, containerW}) => {
const colorChannels = Object.values(layer.visualChannels).filter(isColorChannel);
const nonColorChannels = Object.values(layer.visualChannels).filter(vc => !isColorChannel(vc));

return (
<>
{colorChannels.map(colorChannel =>
!colorChannel.condition || colorChannel.condition(layer.config) ? (
<LayerColorLegend
key={colorChannel.key}
description={layer.getVisualChannelDescription(colorChannel.key)}
config={layer.config}
width={containerW - 2 * DIMENSIONS.mapControl.padding}
colorChannel={colorChannel}
/>
) : null
)}
{nonColorChannels.map(visualChannel => {
const matchCondition = !visualChannel.condition || visualChannel.condition(layer.config);
const enabled = layer.config[visualChannel.field] || visualChannel.defaultMeasure;

const description = layer.getVisualChannelDescription(visualChannel.key);

return matchCondition && enabled ? (
<LayerSizeLegend
key={visualChannel.key}
label={description.label}
name={description.measure}
/>
) : null;
})}
</>
);
};

return LayerLegendContent;
}

MapLegendFactory.deps = [LayerLegendHeaderFactory, LayerLegendContentFactory];
function MapLegendFactory(LayerLegendHeader, LayerLegendContent) {
/** @type {typeof import('./map-legend').MapLegend }> */
const MapLegend = ({layers = [], width, options}) => (
<div className="map-legend">
Expand All @@ -152,10 +204,6 @@ function MapLegendFactory() {
return null;
}
const containerW = width || DIMENSIONS.mapControl.width;
const colorChannels = Object.values(layer.visualChannels).filter(isColorChannel);
const nonColorChannels = Object.values(layer.visualChannels).filter(
vc => !isColorChannel(vc)
);

return (
<StyledMapControlLegend
Expand All @@ -164,35 +212,8 @@ function MapLegendFactory() {
key={index}
width={containerW}
>
{options?.showLayerName !== false ? (
<div className="legend--layer_name">{layer.config.label}</div>
) : null}
{colorChannels.map(colorChannel =>
!colorChannel.condition || colorChannel.condition(layer.config) ? (
<LayerColorLegend
key={colorChannel.key}
description={layer.getVisualChannelDescription(colorChannel.key)}
config={layer.config}
width={containerW - 2 * DIMENSIONS.mapControl.padding}
colorChannel={colorChannel}
/>
) : null
)}
{nonColorChannels.map(visualChannel => {
const matchCondition =
!visualChannel.condition || visualChannel.condition(layer.config);
const enabled = layer.config[visualChannel.field] || visualChannel.defaultMeasure;

const description = layer.getVisualChannelDescription(visualChannel.key);

return matchCondition && enabled ? (
<LayerSizeLegend
key={visualChannel.key}
label={description.label}
name={description.measure}
/>
) : null;
})}
<LayerLegendHeader options={options} layer={layer} />
<LayerLegendContent containerW={containerW} layer={layer} />
</StyledMapControlLegend>
);
})}
Expand Down
1 change: 1 addition & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {
ADD_MAP_STYLE_ID,
AGGREGATION_TYPES,
ALL_FIELD_TYPES,
CHANNEL_SCALES,
DATA_TABLE_ID,
DATASET_FORMATS,
DEFAULT_LAYER_GROUPS,
Expand Down
2 changes: 1 addition & 1 deletion src/localization/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export default {
},
header: {
visibleLayers: 'Visible layers',
layerLegend: 'Layer Legend'
layerLegend: 'Legend'
},
interactions: {
tooltip: 'Tooltip',
Expand Down