Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions x-pack/legacy/plugins/maps/common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export const EMS_FILE = 'EMS_FILE';
export const ES_GEO_GRID = 'ES_GEO_GRID';
export const ES_SEARCH = 'ES_SEARCH';
export const ES_PEW_PEW = 'ES_PEW_PEW';

export const FIELD_ORIGIN = {
SOURCE: 'source',
JOIN: 'join'
};

export const SOURCE_DATA_ID_ORIGIN = 'source';

export const GEOJSON_FILE = 'GEOJSON_FILE';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,9 @@ export class ESSearchSource extends AbstractESSource {
path: geoField.name,
};
}

_getRawFieldName(fieldName) {
// fieldName is rawFieldName for documents source since the source uses raw documents instead of aggregated metrics
return fieldName;
}
}
29 changes: 29 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/sources/es_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,33 @@ export class AbstractESSource extends AbstractVectorSource {
return this._descriptor.id;
}

_getRawFieldName(fieldName) {
const metricField = this.getMetricFields().find(({ propertyKey }) => {
return propertyKey === fieldName;
});

return metricField ? metricField.field : null;
}

async getFieldFormatter(fieldName) {
// fieldName could be an aggregation so it needs to be unpacked to expose raw field.
const rawFieldName = this._getRawFieldName(fieldName);
if (!rawFieldName) {
return null;
}

let indexPattern;
try {
indexPattern = await this._getIndexPattern();
} catch(error) {
return null;
}

const fieldFromIndexPattern = indexPattern.fields.byName[rawFieldName];
if (!fieldFromIndexPattern) {
return null;
}

return fieldFromIndexPattern.format.getConverterFor('text');
}
}
5 changes: 5 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/sources/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ export class AbstractSource {
async getPreIndexedShape(/* properties */) {
return null;
}

// Returns function used to format value
async getFieldFormatter(/* fieldName */) {
return null;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,39 @@ function renderHeaderWithIcons(icons) {
);
}

const EMPTY_VALUE = '';

export class StylePropertyLegendRow extends Component {

state = {
label: '',
hasLoadedFieldFormatter: false,
}

componentDidMount() {
this._isMounted = true;
this._prevLabel = undefined;
this._fieldValueFormatter = undefined;
this._loadLabel();
this._loadFieldFormatter();
}

componentDidUpdate() {
// label could change so it needs to be loaded on update
this._loadLabel();
}

componentWillUnmount() {
this._isMounted = false;
}

async _loadFieldFormatter() {
this._fieldValueFormatter = await this.props.getFieldFormatter(this.props.options.field);
if (this._isMounted) {
this.setState({ hasLoadedFieldFormatter: true });
}
}

_loadLabel = async () => {
if (this._isStatic()) {
return;
Expand All @@ -112,6 +125,14 @@ export class StylePropertyLegendRow extends Component {
!this.props.options.field || !this.props.options.field.name;
}

_formatValue = value => {
if (!this.state.hasLoadedFieldFormatter || !this._fieldValueFormatter || value === EMPTY_VALUE) {
return value;
}

return this._fieldValueFormatter(value);
}

render() {
const { name, options, range } = this.props;
if (this._isStatic()) {
Expand All @@ -130,8 +151,8 @@ export class StylePropertyLegendRow extends Component {
return (
<StyleLegendRow
header={header}
minLabel={_.get(range, 'min', '')}
maxLabel={_.get(range, 'max', '')}
minLabel={this._formatValue(_.get(range, 'min', EMPTY_VALUE))}
maxLabel={this._formatValue(_.get(range, 'max', EMPTY_VALUE))}
propertyLabel={getVectorStyleLabel(name)}
fieldLabel={this.state.label}
/>
Expand All @@ -145,4 +166,5 @@ StylePropertyLegendRow.propTypes = {
options: PropTypes.oneOfType(styleOptionShapes).isRequired,
range: rangeShape,
getFieldLabel: PropTypes.func.isRequired,
getFieldFormatter: PropTypes.func.isRequired,
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import PropTypes from 'prop-types';
import { styleOptionShapes, rangeShape } from '../style_option_shapes';
import { StylePropertyLegendRow } from './style_property_legend_row';

export function VectorStyleLegend({ getFieldLabel, styleProperties }) {
export function VectorStyleLegend({ getFieldLabel, getFieldFormatter, styleProperties }) {
return styleProperties.map(styleProperty => {
return (
<StylePropertyLegendRow
Expand All @@ -20,6 +20,7 @@ export function VectorStyleLegend({ getFieldLabel, styleProperties }) {
options={styleProperty.options}
range={styleProperty.range}
getFieldLabel={getFieldLabel}
getFieldFormatter={getFieldFormatter}
/>
);
});
Expand All @@ -34,5 +35,6 @@ const stylePropertyShape = PropTypes.shape({

VectorStyleLegend.propTypes = {
styleProperties: PropTypes.arrayOf(stylePropertyShape).isRequired,
getFieldLabel: PropTypes.func.isRequired
getFieldLabel: PropTypes.func.isRequired,
getFieldFormatter: PropTypes.func.isRequired,
};
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export class VectorStyle extends AbstractStyle {
);
}

getLegendDetails(getFieldLabel) {
getLegendDetails(getFieldLabel, getFieldFormatter) {
const styles = this.getProperties();
const styleProperties = Object.keys(styles).map(styleName => {
const { type, options } = styles[styleName];
Expand All @@ -319,6 +319,7 @@ export class VectorStyle extends AbstractStyle {
<VectorStyleLegend
styleProperties={styleProperties}
getFieldLabel={getFieldLabel}
getFieldFormatter={getFieldFormatter}
/>
);
}
Expand Down
41 changes: 37 additions & 4 deletions x-pack/legacy/plugins/maps/public/layers/vector_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
SOURCE_DATA_ID_ORIGIN,
FEATURE_VISIBLE_PROPERTY_NAME,
EMPTY_FEATURE_COLLECTION,
LAYER_TYPE
LAYER_TYPE,
FIELD_ORIGIN,
} from '../../common/constants';
import _ from 'lodash';
import { JoinTooltipProperty } from './tooltips/join_tooltip_property';
Expand Down Expand Up @@ -202,7 +203,16 @@ export class VectorLayer extends AbstractLayer {
return field ? field.label : fieldName;
};

return this._style.getLegendDetails(getFieldLabel);
const getFieldFormatter = async field => {
const source = this._getFieldSource(field);
if (!source) {
return null;
}

return await source.getFieldFormatter(field.name);
};

return this._style.getLegendDetails(getFieldLabel, getFieldFormatter);
}

_getBoundsBasedOnData() {
Expand Down Expand Up @@ -249,15 +259,15 @@ export class VectorLayer extends AbstractLayer {
return {
label,
name,
origin: SOURCE_DATA_ID_ORIGIN
origin: FIELD_ORIGIN.SOURCE
};
});
const joinFields = [];
this.getValidJoins().forEach(join => {
const fields = join.getJoinFields().map(joinField => {
return {
...joinField,
origin: 'join',
origin: FIELD_ORIGIN.JOIN,
};
});
joinFields.push(...fields);
Expand Down Expand Up @@ -796,4 +806,27 @@ export class VectorLayer extends AbstractLayer {
});
}

_getFieldSource(field) {
if (!field) {
return null;
}

if (field.origin === FIELD_ORIGIN.SOURCE) {
return this._source;
}

const join = this.getValidJoins().find(join => {
const matchingField = join.getJoinFields().find(joinField => {
return joinField.name === field.name;
});
return !!matchingField;
});

if (!join) {
return null;
}

return join.getRightJoinSource();
}

}