From a7f25d610542f12c556608240fccbb4b812974cc Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 14 Sep 2020 11:38:39 -0400 Subject: [PATCH 01/14] init --- .../public/classes/fields/es_agg_field.ts | 8 ++++-- .../maps/public/classes/fields/field.ts | 26 ++++++++++++++++++- .../fields/top_term_percentage_field.ts | 7 ++++- .../dynamic_orientation_property.ts | 12 ++++----- .../properties/dynamic_style_property.tsx | 18 +++++++++++++ .../properties/dynamic_text_property.ts | 11 ++++---- .../classes/styles/vector/vector_style.tsx | 11 +++----- 7 files changed, 70 insertions(+), 23 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts b/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts index 7b184819b839b..48374464e67aa 100644 --- a/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts +++ b/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts @@ -5,12 +5,12 @@ */ import { IndexPattern } from 'src/plugins/data/public'; -import { IField } from './field'; +import { IField, getMbPropertyName } from './field'; import { AggDescriptor } from '../../../common/descriptor_types'; import { IESAggSource } from '../sources/es_agg_source'; import { IVectorSource } from '../sources/vector_source'; import { ESDocField } from './es_doc_field'; -import { AGG_TYPE, FIELD_ORIGIN } from '../../../common/constants'; +import { AGG_TYPE, FIELD_ORIGIN, VECTOR_STYLES } from '../../../common/constants'; import { isMetricCountable } from '../util/is_metric_countable'; import { getField, addFieldToDSL } from '../util/es_agg_utils'; import { TopTermPercentageField } from './top_term_percentage_field'; @@ -138,6 +138,10 @@ export class ESAggField implements IESAggField { canReadFromGeoJson(): boolean { return true; } + + getMbPropertyName(styleName: VECTOR_STYLES): string { + return getMbPropertyName(this, styleName); + } } export function esAggFieldsFactory( diff --git a/x-pack/plugins/maps/public/classes/fields/field.ts b/x-pack/plugins/maps/public/classes/fields/field.ts index 2c190d54f0265..99c82252d4341 100644 --- a/x-pack/plugins/maps/public/classes/fields/field.ts +++ b/x-pack/plugins/maps/public/classes/fields/field.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { FIELD_ORIGIN } from '../../../common/constants'; +import { FIELD_ORIGIN, VECTOR_STYLES } from '../../../common/constants'; import { IVectorSource } from '../sources/vector_source'; import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property'; @@ -32,6 +32,14 @@ export interface IField { supportsFieldMeta(): boolean; canReadFromGeoJson(): boolean; + + // Returns the name that should be used for accessing the data from the mb-style rule + // Depending on + // - whether the field is used for labeling, icon-orientation, or other properties (color, size, ...), `feature-state` and or `get` is used + // - whether the field was run through a field-formatter, a new dynamic field is created with the formatted-value + // The combination of both will inform what field-name (e.g. the "raw" field name from the properties, the "computed field-name" for an on-the-fly created property (e.g. for feature-state or field-formatting). + // todo: There is an existing limitation to .mvt backed sources, where the field-formatters are not applied. Here, the raw-data needs to be accessed. + getMbPropertyName(styleName: VECTOR_STYLES): string; } export class AbstractField implements IField { @@ -99,4 +107,20 @@ export class AbstractField implements IField { canReadFromGeoJson(): boolean { return true; } + + getMbPropertyName(styleName: VECTOR_STYLES): string { + return getMbPropertyName(this, styleName); + } +} + +export function getMbPropertyName(field: IField, styleName: VECTOR_STYLES) { + let targetName; + if (field.canReadFromGeoJson()) { + targetName = field.supportsAutoDomain() + ? getComputedFieldName(styleName, field.getName()) + : field.getName(); + } else { + targetName = field.getName(); + } + return targetName; } diff --git a/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts b/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts index fc931b13619ef..0502cbc80c14b 100644 --- a/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts +++ b/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts @@ -5,10 +5,11 @@ */ import { IESAggField } from './es_agg_field'; +import { getMbPropertyName } from './field'; import { IVectorSource } from '../sources/vector_source'; // @ts-ignore import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property'; -import { TOP_TERM_PERCENTAGE_SUFFIX } from '../../../common/constants'; +import { TOP_TERM_PERCENTAGE_SUFFIX, VECTOR_STYLES } from '../../../common/constants'; import { FIELD_ORIGIN } from '../../../common/constants'; export class TopTermPercentageField implements IESAggField { @@ -83,4 +84,8 @@ export class TopTermPercentageField implements IESAggField { canReadFromGeoJson(): boolean { return true; } + + getMbPropertyName(styleName: VECTOR_STYLES): string { + return getMbPropertyName(this, styleName); + } } diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts index dd976027a50f2..e563d89622388 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts @@ -5,17 +5,13 @@ */ import { Map as MbMap } from 'mapbox-gl'; -import { DynamicStyleProperty } from './dynamic_style_property'; -import { getComputedFieldName } from '../style_util'; -import { VECTOR_STYLES } from '../../../../../common/constants'; +import { DynamicStyleProperty, getNumericalMbFeatureStateValue } from './dynamic_style_property'; import { OrientationDynamicOptions } from '../../../../../common/descriptor_types'; export class DynamicOrientationProperty extends DynamicStyleProperty { syncIconRotationWithMb(symbolLayerId: string, mbMap: MbMap) { if (this._field && this._field.isValid()) { - const targetName = this._field.supportsAutoDomain() - ? getComputedFieldName(VECTOR_STYLES.ICON_ORIENTATION, this.getFieldName()) - : this._field.getName(); + const targetName = this._field.getMbPropertyName(this.getStyleName()); // Using property state instead of feature-state because layout properties do not support feature-state mbMap.setLayoutProperty(symbolLayerId, 'icon-rotate', ['coalesce', ['get', targetName], 0]); } else { @@ -26,4 +22,8 @@ export class DynamicOrientationProperty extends DynamicStyleProperty extends IStyleProperty { pluckOrdinalStyleMetaFromFeatures(features: Feature[]): RangeFieldMeta | null; pluckCategoricalStyleMetaFromFeatures(features: Feature[]): CategoryFieldMeta | null; getValueSuggestions(query: string): Promise; + getMbPropertyValue(value: string | number | null | undefined): string | number | null | undefined; } export type FieldFormatter = (value: string | number | undefined) => string | number; @@ -345,4 +346,21 @@ export class DynamicStyleProperty /> ); } + + getMbPropertyValue( + rawValue: string | number | null | undefined + ): string | number | null | undefined { + if (this.supportsMbFeatureState()) { + return getNumericalMbFeatureStateValue(rawValue); + } else { + return this.isOrdinal() + ? getNumericalMbFeatureStateValue(rawValue) + : this.formatField(rawValue); + } + } +} + +export function getNumericalMbFeatureStateValue(value: string | number) { + const valueAsFloat = parseFloat(value); + return isNaN(valueAsFloat) ? null : valueAsFloat; } diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts index d55a6e1cfb444..92fc974bda64b 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts @@ -6,17 +6,12 @@ import { Map as MbMap } from 'mapbox-gl'; import { DynamicStyleProperty } from './dynamic_style_property'; -import { getComputedFieldName } from '../style_util'; import { LabelDynamicOptions } from '../../../../../common/descriptor_types'; export class DynamicTextProperty extends DynamicStyleProperty { syncTextFieldWithMb(mbLayerId: string, mbMap: MbMap) { if (this._field && this._field.isValid()) { - // Fields that support auto-domain are normalized with a field-formatter and stored into a computed-field - // Otherwise, the raw value is just carried over and no computed field is created. - const targetName = this._field.supportsAutoDomain() - ? getComputedFieldName(this._styleName, this.getFieldName()) - : this._field.getName(); + const targetName = this._field.getMbPropertyName(this.getStyleName()); mbMap.setLayoutProperty(mbLayerId, 'text-field', ['coalesce', ['get', targetName], '']); } else { mbMap.setLayoutProperty(mbLayerId, 'text-field', null); @@ -34,4 +29,8 @@ export class DynamicTextProperty extends DynamicStyleProperty>; getDynamicPropertiesArray(): Array>; @@ -619,15 +614,17 @@ export class VectorStyle implements IVectorStyle { for (let j = 0; j < dynamicStyleProps.length; j++) { const dynamicStyleProp = dynamicStyleProps[j]; const name = dynamicStyleProp.getFieldName(); - const computedName = getComputedFieldName(dynamicStyleProp.getStyleName(), name); + + const field = dynamicStyleProp.getField(); + const targetMbName = field.getMbPropertyName(dynamicStyleProp.getStyleName()); const rawValue = feature.properties ? feature.properties[name] : undefined; + const targetMbValue = dynamicStyleProp.getMbPropertyValue(rawValue); if (dynamicStyleProp.supportsMbFeatureState()) { tmpFeatureState[name] = getNumericalMbFeatureStateValue(rawValue); // the same value will be potentially overridden multiple times, if the name remains identical } else { // in practice, a new system property will only be created for: // - label text: this requires the value to be formatted first. // - icon orientation: this is a lay-out property which do not support feature-state (but we're still coercing to a number) - const formattedValue = dynamicStyleProp.isOrdinal() ? getNumericalMbFeatureStateValue(rawValue) : dynamicStyleProp.formatField(rawValue); From 092d4ef107b6bf1495cec6207d7f54ab4dc8e4db Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 14 Sep 2020 11:45:15 -0400 Subject: [PATCH 02/14] remove comment --- .../styles/vector/properties/dynamic_orientation_property.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts index e563d89622388..975ee268a7494 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts @@ -12,7 +12,6 @@ export class DynamicOrientationProperty extends DynamicStyleProperty Date: Mon, 14 Sep 2020 11:59:16 -0400 Subject: [PATCH 03/14] use interface-methods --- .../classes/styles/vector/vector_style.tsx | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx b/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx index d59626ad14709..bcb5a8dcac78b 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx @@ -613,23 +613,18 @@ export class VectorStyle implements IVectorStyle { for (let j = 0; j < dynamicStyleProps.length; j++) { const dynamicStyleProp = dynamicStyleProps[j]; - const name = dynamicStyleProp.getFieldName(); - const field = dynamicStyleProp.getField(); const targetMbName = field.getMbPropertyName(dynamicStyleProp.getStyleName()); - const rawValue = feature.properties ? feature.properties[name] : undefined; + const rawValue = feature.properties + ? feature.properties[dynamicStyleProp.getFieldName()] + : undefined; const targetMbValue = dynamicStyleProp.getMbPropertyValue(rawValue); if (dynamicStyleProp.supportsMbFeatureState()) { - tmpFeatureState[name] = getNumericalMbFeatureStateValue(rawValue); // the same value will be potentially overridden multiple times, if the name remains identical + tmpFeatureState[targetMbName] = targetMbValue; // the same value will be potentially overridden multiple times, if the name remains identical } else { - // in practice, a new system property will only be created for: - // - label text: this requires the value to be formatted first. - // - icon orientation: this is a lay-out property which do not support feature-state (but we're still coercing to a number) - const formattedValue = dynamicStyleProp.isOrdinal() - ? getNumericalMbFeatureStateValue(rawValue) - : dynamicStyleProp.formatField(rawValue); - - if (feature.properties) feature.properties[computedName] = formattedValue; + if (feature.properties) { + feature.properties[targetMbName] = targetMbValue; + } } } tmpFeatureIdentifier.source = mbSourceId; From 78502d419a1de113b748e7f39dbb3d9e4227d1af Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 14 Sep 2020 12:03:43 -0400 Subject: [PATCH 04/14] add missing import --- x-pack/plugins/maps/public/classes/fields/field.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/plugins/maps/public/classes/fields/field.ts b/x-pack/plugins/maps/public/classes/fields/field.ts index 99c82252d4341..ada4b7deeaf07 100644 --- a/x-pack/plugins/maps/public/classes/fields/field.ts +++ b/x-pack/plugins/maps/public/classes/fields/field.ts @@ -7,6 +7,7 @@ import { FIELD_ORIGIN, VECTOR_STYLES } from '../../../common/constants'; import { IVectorSource } from '../sources/vector_source'; import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property'; +import { getComputedFieldName } from '../styles/vector/style_util'; export interface IField { getName(): string; From f86f671a7b3d60f83ef0abab0dcc56f8d68b1f71 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 14 Sep 2020 14:50:30 -0400 Subject: [PATCH 05/14] move getMbPropertyName to style for clarity --- .../public/classes/fields/es_agg_field.ts | 4 --- .../maps/public/classes/fields/field.ts | 24 ------------- .../fields/top_term_percentage_field.ts | 4 --- .../dynamic_orientation_property.ts | 2 +- .../properties/dynamic_style_property.tsx | 34 +++++++++++++++++++ .../properties/dynamic_text_property.ts | 2 +- .../classes/styles/vector/vector_style.tsx | 5 ++- 7 files changed, 38 insertions(+), 37 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts b/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts index 48374464e67aa..667ae096e6eac 100644 --- a/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts +++ b/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts @@ -138,10 +138,6 @@ export class ESAggField implements IESAggField { canReadFromGeoJson(): boolean { return true; } - - getMbPropertyName(styleName: VECTOR_STYLES): string { - return getMbPropertyName(this, styleName); - } } export function esAggFieldsFactory( diff --git a/x-pack/plugins/maps/public/classes/fields/field.ts b/x-pack/plugins/maps/public/classes/fields/field.ts index ada4b7deeaf07..7e8106a96f2e5 100644 --- a/x-pack/plugins/maps/public/classes/fields/field.ts +++ b/x-pack/plugins/maps/public/classes/fields/field.ts @@ -33,14 +33,6 @@ export interface IField { supportsFieldMeta(): boolean; canReadFromGeoJson(): boolean; - - // Returns the name that should be used for accessing the data from the mb-style rule - // Depending on - // - whether the field is used for labeling, icon-orientation, or other properties (color, size, ...), `feature-state` and or `get` is used - // - whether the field was run through a field-formatter, a new dynamic field is created with the formatted-value - // The combination of both will inform what field-name (e.g. the "raw" field name from the properties, the "computed field-name" for an on-the-fly created property (e.g. for feature-state or field-formatting). - // todo: There is an existing limitation to .mvt backed sources, where the field-formatters are not applied. Here, the raw-data needs to be accessed. - getMbPropertyName(styleName: VECTOR_STYLES): string; } export class AbstractField implements IField { @@ -108,20 +100,4 @@ export class AbstractField implements IField { canReadFromGeoJson(): boolean { return true; } - - getMbPropertyName(styleName: VECTOR_STYLES): string { - return getMbPropertyName(this, styleName); - } -} - -export function getMbPropertyName(field: IField, styleName: VECTOR_STYLES) { - let targetName; - if (field.canReadFromGeoJson()) { - targetName = field.supportsAutoDomain() - ? getComputedFieldName(styleName, field.getName()) - : field.getName(); - } else { - targetName = field.getName(); - } - return targetName; } diff --git a/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts b/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts index 0502cbc80c14b..0cf432e9d3e78 100644 --- a/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts +++ b/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts @@ -84,8 +84,4 @@ export class TopTermPercentageField implements IESAggField { canReadFromGeoJson(): boolean { return true; } - - getMbPropertyName(styleName: VECTOR_STYLES): string { - return getMbPropertyName(this, styleName); - } } diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts index 975ee268a7494..a81d24a35ad36 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts @@ -11,7 +11,7 @@ import { OrientationDynamicOptions } from '../../../../../common/descriptor_type export class DynamicOrientationProperty extends DynamicStyleProperty { syncIconRotationWithMb(symbolLayerId: string, mbMap: MbMap) { if (this._field && this._field.isValid()) { - const targetName = this._field.getMbPropertyName(this.getStyleName()); + const targetName = this.getMbPropertyName(); mbMap.setLayoutProperty(symbolLayerId, 'icon-rotate', ['coalesce', ['get', targetName], 0]); } else { mbMap.setLayoutProperty(symbolLayerId, 'icon-rotate', 0); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx index 4659d2728e63b..7eb2e8768f9bf 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx @@ -28,6 +28,7 @@ import { IField } from '../../../fields/field'; import { IVectorLayer } from '../../../layers/vector_layer/vector_layer'; import { IJoin } from '../../../joins/join'; import { IVectorStyle } from '../vector_style'; +import { getComputedFieldName } from '../style_util'; export interface IDynamicStyleProperty extends IStyleProperty { getFieldMetaOptions(): FieldMetaOptions; @@ -46,6 +47,14 @@ export interface IDynamicStyleProperty extends IStyleProperty { pluckOrdinalStyleMetaFromFeatures(features: Feature[]): RangeFieldMeta | null; pluckCategoricalStyleMetaFromFeatures(features: Feature[]): CategoryFieldMeta | null; getValueSuggestions(query: string): Promise; + + // Returns the name that should be used for accessing the data from the mb-style rule + // Depending on + // - whether the field is used for labeling, icon-orientation, or other properties (color, size, ...), `feature-state` and or `get` is used + // - whether the field was run through a field-formatter, a new dynamic field is created with the formatted-value + // The combination of both will inform what field-name (e.g. the "raw" field name from the properties, the "computed field-name" for an on-the-fly created property (e.g. for feature-state or field-formatting). + // todo: There is an existing limitation to .mvt backed sources, where the field-formatters are not applied. Here, the raw-data needs to be accessed. + getMbPropertyName(): string; getMbPropertyValue(value: string | number | null | undefined): string | number | null | undefined; } @@ -347,6 +356,31 @@ export class DynamicStyleProperty ); } + getMbPropertyName() { + if (!this._field) { + return ''; + } + + let targetName; + if (this.supportsMbFeatureState()) { + // Base case for any properties that can support feature-state (e.g. color, size, ...) + // They just re-use the original property-name + targetName = this._field.getName(); + } else { + if (this._field.canReadFromGeoJson()) { + // Geojson-sources can support rewrite + // e.g. field-formatters will create duplicate field + targetName = this._field.supportsAutoDomain() + ? getComputedFieldName(this.getStyleName(), this._field.getName()) + : this._field.getName(); + } else { + // Non-geojson sources (e.g. mvt) + targetName = this._field.getName(); + } + } + return targetName; + } + getMbPropertyValue( rawValue: string | number | null | undefined ): string | number | null | undefined { diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts index 92fc974bda64b..8bd2c700dd55f 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts @@ -11,7 +11,7 @@ import { LabelDynamicOptions } from '../../../../../common/descriptor_types'; export class DynamicTextProperty extends DynamicStyleProperty { syncTextFieldWithMb(mbLayerId: string, mbMap: MbMap) { if (this._field && this._field.isValid()) { - const targetName = this._field.getMbPropertyName(this.getStyleName()); + const targetName = this.getMbPropertyName(); mbMap.setLayoutProperty(mbLayerId, 'text-field', ['coalesce', ['get', targetName], '']); } else { mbMap.setLayoutProperty(mbLayerId, 'text-field', null); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx b/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx index bcb5a8dcac78b..1244c53afe9a6 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/vector_style.tsx @@ -29,7 +29,7 @@ import { import { StyleMeta } from './style_meta'; import { VectorIcon } from './components/legend/vector_icon'; import { VectorStyleLegend } from './components/legend/vector_style_legend'; -import { getComputedFieldName, isOnlySingleFeatureType } from './style_util'; +import { isOnlySingleFeatureType } from './style_util'; import { StaticStyleProperty } from './properties/static_style_property'; import { DynamicStyleProperty } from './properties/dynamic_style_property'; import { DynamicSizeProperty } from './properties/dynamic_size_property'; @@ -613,8 +613,7 @@ export class VectorStyle implements IVectorStyle { for (let j = 0; j < dynamicStyleProps.length; j++) { const dynamicStyleProp = dynamicStyleProps[j]; - const field = dynamicStyleProp.getField(); - const targetMbName = field.getMbPropertyName(dynamicStyleProp.getStyleName()); + const targetMbName = dynamicStyleProp.getMbPropertyName(); const rawValue = feature.properties ? feature.properties[dynamicStyleProp.getFieldName()] : undefined; From e2414bd9376afc8e649661326b08fd562aa099ee Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 14 Sep 2020 14:52:35 -0400 Subject: [PATCH 06/14] remove duplicate implementation --- .../styles/vector/properties/dynamic_style_property.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx index 7eb2e8768f9bf..9383c622b8bac 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx @@ -387,9 +387,7 @@ export class DynamicStyleProperty if (this.supportsMbFeatureState()) { return getNumericalMbFeatureStateValue(rawValue); } else { - return this.isOrdinal() - ? getNumericalMbFeatureStateValue(rawValue) - : this.formatField(rawValue); + return rawValue; } } } From c70e99c5e8ecaabd9967c61e1a3ebc8e0325e826 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 14 Sep 2020 14:53:32 -0400 Subject: [PATCH 07/14] simplify --- .../styles/vector/properties/dynamic_style_property.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx index 9383c622b8bac..932c1a75345c1 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx @@ -384,11 +384,7 @@ export class DynamicStyleProperty getMbPropertyValue( rawValue: string | number | null | undefined ): string | number | null | undefined { - if (this.supportsMbFeatureState()) { - return getNumericalMbFeatureStateValue(rawValue); - } else { - return rawValue; - } + return this.supportsMbFeatureState() ? getNumericalMbFeatureStateValue(rawValue) : rawValue; } } From 4d451131d0a4f33894c39f9c9ab36981f0619298 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 14 Sep 2020 15:07:51 -0400 Subject: [PATCH 08/14] type check --- .../plugins/maps/public/classes/fields/es_agg_field.ts | 4 ++-- x-pack/plugins/maps/public/classes/fields/field.ts | 3 +-- .../public/classes/fields/top_term_percentage_field.ts | 3 +-- .../vector/properties/dynamic_orientation_property.ts | 6 ++++-- .../vector/properties/dynamic_style_property.tsx | 10 +++++++--- .../styles/vector/properties/dynamic_text_property.ts | 4 +++- .../classes/styles/vector/properties/style_property.ts | 7 +++---- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts b/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts index 667ae096e6eac..7b184819b839b 100644 --- a/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts +++ b/x-pack/plugins/maps/public/classes/fields/es_agg_field.ts @@ -5,12 +5,12 @@ */ import { IndexPattern } from 'src/plugins/data/public'; -import { IField, getMbPropertyName } from './field'; +import { IField } from './field'; import { AggDescriptor } from '../../../common/descriptor_types'; import { IESAggSource } from '../sources/es_agg_source'; import { IVectorSource } from '../sources/vector_source'; import { ESDocField } from './es_doc_field'; -import { AGG_TYPE, FIELD_ORIGIN, VECTOR_STYLES } from '../../../common/constants'; +import { AGG_TYPE, FIELD_ORIGIN } from '../../../common/constants'; import { isMetricCountable } from '../util/is_metric_countable'; import { getField, addFieldToDSL } from '../util/es_agg_utils'; import { TopTermPercentageField } from './top_term_percentage_field'; diff --git a/x-pack/plugins/maps/public/classes/fields/field.ts b/x-pack/plugins/maps/public/classes/fields/field.ts index 7e8106a96f2e5..2c190d54f0265 100644 --- a/x-pack/plugins/maps/public/classes/fields/field.ts +++ b/x-pack/plugins/maps/public/classes/fields/field.ts @@ -4,10 +4,9 @@ * you may not use this file except in compliance with the Elastic License. */ -import { FIELD_ORIGIN, VECTOR_STYLES } from '../../../common/constants'; +import { FIELD_ORIGIN } from '../../../common/constants'; import { IVectorSource } from '../sources/vector_source'; import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property'; -import { getComputedFieldName } from '../styles/vector/style_util'; export interface IField { getName(): string; diff --git a/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts b/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts index 0cf432e9d3e78..fc931b13619ef 100644 --- a/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts +++ b/x-pack/plugins/maps/public/classes/fields/top_term_percentage_field.ts @@ -5,11 +5,10 @@ */ import { IESAggField } from './es_agg_field'; -import { getMbPropertyName } from './field'; import { IVectorSource } from '../sources/vector_source'; // @ts-ignore import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property'; -import { TOP_TERM_PERCENTAGE_SUFFIX, VECTOR_STYLES } from '../../../common/constants'; +import { TOP_TERM_PERCENTAGE_SUFFIX } from '../../../common/constants'; import { FIELD_ORIGIN } from '../../../common/constants'; export class TopTermPercentageField implements IESAggField { diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts index a81d24a35ad36..e219d3bc857b0 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts @@ -18,11 +18,13 @@ export class DynamicOrientationProperty extends DynamicStyleProperty }; } - formatField(value: string | number | undefined): string | number { + formatField(value: string | number | undefined | null): string | number { if (this.getField()) { const fieldName = this.getFieldName(); const fieldFormatter = this._getFieldFormatter(fieldName); - return fieldFormatter ? fieldFormatter(value) : super.formatField(value); + return fieldFormatter && value !== null ? fieldFormatter(value) : super.formatField(value); } else { return super.formatField(value); } @@ -388,7 +388,11 @@ export class DynamicStyleProperty } } -export function getNumericalMbFeatureStateValue(value: string | number) { +export function getNumericalMbFeatureStateValue(value: string | number | null | undefined) { + if (typeof value !== 'string') { + return value; + } + const valueAsFloat = parseFloat(value); return isNaN(valueAsFloat) ? null : valueAsFloat; } diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts index 8bd2c700dd55f..14240300f22c1 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts @@ -30,7 +30,9 @@ export class DynamicTextProperty extends DynamicStyleProperty { isDynamic(): boolean; isComplete(): boolean; - formatField(value: string | number | undefined): string | number; + formatField(value: string | number | undefined | null): string | number; getStyleName(): VECTOR_STYLES; getOptions(): T; renderLegendDetailRow(legendProps: LegendProps): ReactElement | null; @@ -53,9 +53,8 @@ export class AbstractStyleProperty implements IStyleProperty { return true; } - formatField(value: string | number | undefined): string | number { - // eslint-disable-next-line eqeqeq - return value == undefined ? '' : value; + formatField(value: string | number | undefined | null): string | number { + return typeof value === 'undefined' || value === null ? '' : value; } getStyleName(): VECTOR_STYLES { From 9b092a66505cfc51023573ccc7ebfa19995f9a37 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Mon, 14 Sep 2020 18:15:38 -0400 Subject: [PATCH 09/14] feedback --- .../properties/dynamic_orientation_property.ts | 16 +++++++++++----- .../vector/properties/dynamic_style_property.tsx | 16 +++++++--------- .../vector/properties/dynamic_text_property.ts | 12 +++++++----- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts index e219d3bc857b0..829509be5b113 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts @@ -5,14 +5,22 @@ */ import { Map as MbMap } from 'mapbox-gl'; -import { DynamicStyleProperty, getNumericalMbFeatureStateValue } from './dynamic_style_property'; +import { + DynamicStyleProperty, + getNumericalMbFeatureStateValue, + RawValue, +} from './dynamic_style_property'; import { OrientationDynamicOptions } from '../../../../../common/descriptor_types'; export class DynamicOrientationProperty extends DynamicStyleProperty { syncIconRotationWithMb(symbolLayerId: string, mbMap: MbMap) { if (this._field && this._field.isValid()) { const targetName = this.getMbPropertyName(); - mbMap.setLayoutProperty(symbolLayerId, 'icon-rotate', ['coalesce', ['get', targetName], 0]); + mbMap.setLayoutProperty(symbolLayerId, 'icon-rotate', [ + 'coalesce', + [this.getMbLookupFunction(), targetName], + 0, + ]); } else { mbMap.setLayoutProperty(symbolLayerId, 'icon-rotate', 0); } @@ -22,9 +30,7 @@ export class DynamicOrientationProperty extends DynamicStyleProperty extends IStyleProperty { getFieldMetaOptions(): FieldMetaOptions; getField(): IField | null; @@ -55,7 +57,7 @@ export interface IDynamicStyleProperty extends IStyleProperty { // The combination of both will inform what field-name (e.g. the "raw" field name from the properties, the "computed field-name" for an on-the-fly created property (e.g. for feature-state or field-formatting). // todo: There is an existing limitation to .mvt backed sources, where the field-formatters are not applied. Here, the raw-data needs to be accessed. getMbPropertyName(): string; - getMbPropertyValue(value: string | number | null | undefined): string | number | null | undefined; + getMbPropertyValue(value: RawValue): RawValue; } export type FieldFormatter = (value: string | number | undefined) => string | number; @@ -367,23 +369,19 @@ export class DynamicStyleProperty // They just re-use the original property-name targetName = this._field.getName(); } else { - if (this._field.canReadFromGeoJson()) { + if (this._field.canReadFromGeoJson() && this._field.supportsAutoDomain()) { // Geojson-sources can support rewrite // e.g. field-formatters will create duplicate field - targetName = this._field.supportsAutoDomain() - ? getComputedFieldName(this.getStyleName(), this._field.getName()) - : this._field.getName(); + targetName = getComputedFieldName(this.getStyleName(), this._field.getName()); } else { - // Non-geojson sources (e.g. mvt) + // Non-geojson sources (e.g. 3rd party mvt or ES-source as mvt) targetName = this._field.getName(); } } return targetName; } - getMbPropertyValue( - rawValue: string | number | null | undefined - ): string | number | null | undefined { + getMbPropertyValue(rawValue: RawValue): RawValue { return this.supportsMbFeatureState() ? getNumericalMbFeatureStateValue(rawValue) : rawValue; } } diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts index 14240300f22c1..9e375891fbbd9 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts @@ -5,14 +5,18 @@ */ import { Map as MbMap } from 'mapbox-gl'; -import { DynamicStyleProperty } from './dynamic_style_property'; +import { DynamicStyleProperty, RawValue } from './dynamic_style_property'; import { LabelDynamicOptions } from '../../../../../common/descriptor_types'; export class DynamicTextProperty extends DynamicStyleProperty { syncTextFieldWithMb(mbLayerId: string, mbMap: MbMap) { if (this._field && this._field.isValid()) { const targetName = this.getMbPropertyName(); - mbMap.setLayoutProperty(mbLayerId, 'text-field', ['coalesce', ['get', targetName], '']); + mbMap.setLayoutProperty(mbLayerId, 'text-field', [ + 'coalesce', + [this.getMbLookupFunction(), targetName], + '', + ]); } else { mbMap.setLayoutProperty(mbLayerId, 'text-field', null); } @@ -30,9 +34,7 @@ export class DynamicTextProperty extends DynamicStyleProperty Date: Tue, 15 Sep 2020 09:18:38 -0400 Subject: [PATCH 10/14] add clarifying comment --- .../styles/vector/properties/dynamic_style_property.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx index b1d00d36cd446..ba5cc9e8c130b 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx @@ -382,6 +382,9 @@ export class DynamicStyleProperty } getMbPropertyValue(rawValue: RawValue): RawValue { + // Maps only uses feature-state for numerical values. + // `supportsMbFeatureState` will only return true when the mb-style rule does a feature-state lookup on a numerical value + // Calling `isOrdinal` would be equivalent. return this.supportsMbFeatureState() ? getNumericalMbFeatureStateValue(rawValue) : rawValue; } } From c5ec78cbbddec7ae2c94b975e9cf1b89a03e15a3 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Tue, 15 Sep 2020 11:44:48 -0400 Subject: [PATCH 11/14] align types --- .../styles/vector/properties/dynamic_style_property.tsx | 2 +- .../public/classes/styles/vector/properties/style_property.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx index ba5cc9e8c130b..a95b674e511f0 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx @@ -325,7 +325,7 @@ export class DynamicStyleProperty }; } - formatField(value: string | number | undefined | null): string | number { + formatField(value: RawValue): string | number { if (this.getField()) { const fieldName = this.getFieldName(); const fieldFormatter = this._getFieldFormatter(fieldName); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts index 7847f0f8743b5..890219dcbba89 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts @@ -20,7 +20,7 @@ export type LegendProps = { export interface IStyleProperty { isDynamic(): boolean; isComplete(): boolean; - formatField(value: string | number | undefined | null): string | number; + formatField(value: RawValue): string | number; getStyleName(): VECTOR_STYLES; getOptions(): T; renderLegendDetailRow(legendProps: LegendProps): ReactElement | null; @@ -53,7 +53,7 @@ export class AbstractStyleProperty implements IStyleProperty { return true; } - formatField(value: string | number | undefined | null): string | number { + formatField(value: RawValue): string | number { return typeof value === 'undefined' || value === null ? '' : value; } From 0c0eb69e76b6bd48c3dac73270d45b805dc103c3 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Tue, 15 Sep 2020 11:58:21 -0400 Subject: [PATCH 12/14] ts fixes --- .../vector/properties/dynamic_orientation_property.ts | 7 ++----- .../styles/vector/properties/dynamic_style_property.tsx | 4 +--- .../styles/vector/properties/dynamic_text_property.ts | 3 ++- .../classes/styles/vector/properties/style_property.ts | 1 + 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts index 829509be5b113..81b5e46d88af9 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts @@ -5,12 +5,9 @@ */ import { Map as MbMap } from 'mapbox-gl'; -import { - DynamicStyleProperty, - getNumericalMbFeatureStateValue, - RawValue, -} from './dynamic_style_property'; +import { DynamicStyleProperty, getNumericalMbFeatureStateValue } from './dynamic_style_property'; import { OrientationDynamicOptions } from '../../../../../common/descriptor_types'; +import { RawValue } from './style_property'; export class DynamicOrientationProperty extends DynamicStyleProperty { syncIconRotationWithMb(symbolLayerId: string, mbMap: MbMap) { diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx index a95b674e511f0..bf7c480babfe6 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx @@ -7,7 +7,7 @@ import _ from 'lodash'; import React from 'react'; import { Feature } from 'geojson'; -import { AbstractStyleProperty, IStyleProperty } from './style_property'; +import { AbstractStyleProperty, IStyleProperty, RawValue } from './style_property'; import { DEFAULT_SIGMA } from '../vector_style_defaults'; import { FIELD_ORIGIN, @@ -30,8 +30,6 @@ import { IJoin } from '../../../joins/join'; import { IVectorStyle } from '../vector_style'; import { getComputedFieldName } from '../style_util'; -export type RawValue = string | number | undefined | null; - export interface IDynamicStyleProperty extends IStyleProperty { getFieldMetaOptions(): FieldMetaOptions; getField(): IField | null; diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts index 9e375891fbbd9..fcda64c738dc7 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts @@ -5,8 +5,9 @@ */ import { Map as MbMap } from 'mapbox-gl'; -import { DynamicStyleProperty, RawValue } from './dynamic_style_property'; +import { DynamicStyleProperty } from './dynamic_style_property'; import { LabelDynamicOptions } from '../../../../../common/descriptor_types'; +import { RawValue } from './style_property'; export class DynamicTextProperty extends DynamicStyleProperty { syncTextFieldWithMb(mbLayerId: string, mbMap: MbMap) { diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts index 890219dcbba89..f3af1f665bf06 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts @@ -10,6 +10,7 @@ import { ReactElement } from 'react'; import { getVectorStyleLabel } from '../components/get_vector_style_label'; import { FieldMetaOptions } from '../../../../../common/descriptor_types'; import { VECTOR_STYLES } from '../../../../../common/constants'; +export type RawValue = string | number | undefined | null; export type LegendProps = { isPointsOnly: boolean; From 51b376e8a2a2dc4edd47e119530f4b13b7cad923 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Tue, 15 Sep 2020 14:20:11 -0400 Subject: [PATCH 13/14] reuse type --- .../classes/styles/vector/properties/dynamic_style_property.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx index bf7c480babfe6..26be467ad64dc 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx @@ -387,7 +387,7 @@ export class DynamicStyleProperty } } -export function getNumericalMbFeatureStateValue(value: string | number | null | undefined) { +export function getNumericalMbFeatureStateValue(value: RawValue) { if (typeof value !== 'string') { return value; } From d570f9abc67c9d321c2d6f1da6a676c4d3bb5902 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Tue, 15 Sep 2020 16:25:53 -0400 Subject: [PATCH 14/14] isolate type --- x-pack/plugins/maps/common/constants.ts | 4 ++++ x-pack/plugins/maps/public/classes/sources/source.ts | 4 +--- .../vector/properties/dynamic_color_property.test.tsx | 6 +++--- .../vector/properties/dynamic_icon_property.test.tsx | 4 ++-- .../vector/properties/dynamic_orientation_property.ts | 2 +- .../vector/properties/dynamic_size_property.test.tsx | 4 ++-- .../vector/properties/dynamic_size_property.tsx | 4 ++-- .../vector/properties/dynamic_style_property.tsx | 8 ++++---- .../styles/vector/properties/dynamic_text_property.ts | 2 +- .../styles/vector/properties/style_property.ts | 11 ++++++++--- 10 files changed, 28 insertions(+), 21 deletions(-) diff --git a/x-pack/plugins/maps/common/constants.ts b/x-pack/plugins/maps/common/constants.ts index a4f20caedfc9b..7dbeb0f2ab1a8 100644 --- a/x-pack/plugins/maps/common/constants.ts +++ b/x-pack/plugins/maps/common/constants.ts @@ -262,3 +262,7 @@ export enum MB_LOOKUP_FUNCTION { GET = 'get', FEATURE_STATE = 'feature-state', } + +export type RawValue = string | number | boolean | undefined | null; + +export type FieldFormatter = (value: RawValue) => string | number; diff --git a/x-pack/plugins/maps/public/classes/sources/source.ts b/x-pack/plugins/maps/public/classes/sources/source.ts index 4a050cc3d7d19..7e7a7bd8f049d 100644 --- a/x-pack/plugins/maps/public/classes/sources/source.ts +++ b/x-pack/plugins/maps/public/classes/sources/source.ts @@ -12,7 +12,7 @@ import { Adapters } from 'src/plugins/inspector/public'; import { copyPersistentState } from '../../reducers/util'; import { IField } from '../fields/field'; -import { MAX_ZOOM, MIN_ZOOM } from '../../../common/constants'; +import { FieldFormatter, MAX_ZOOM, MIN_ZOOM } from '../../../common/constants'; import { AbstractSourceDescriptor } from '../../../common/descriptor_types'; import { OnSourceChangeArgs } from '../../connected_components/layer_panel/view'; @@ -37,8 +37,6 @@ export type PreIndexedShape = { path: string; }; -export type FieldFormatter = (value: string | number | null | undefined | boolean) => string; - export interface ISource { destroy(): void; getDisplayName(): Promise; diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_color_property.test.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_color_property.test.tsx index de8f3b5c09175..c9188a0a19b0d 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_color_property.test.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_color_property.test.tsx @@ -15,7 +15,7 @@ import { shallow } from 'enzyme'; import { Feature, Point } from 'geojson'; import { DynamicColorProperty } from './dynamic_color_property'; -import { COLOR_MAP_TYPE, VECTOR_STYLES } from '../../../../../common/constants'; +import { COLOR_MAP_TYPE, RawValue, VECTOR_STYLES } from '../../../../../common/constants'; import { mockField, MockLayer, MockStyle } from './__tests__/test_util'; import { ColorDynamicOptions } from '../../../../../common/descriptor_types'; import { IVectorLayer } from '../../../layers/vector_layer/vector_layer'; @@ -28,7 +28,7 @@ const makeProperty = (options: ColorDynamicOptions, style?: MockStyle, field?: I field ? field : mockField, (new MockLayer(style ? style : new MockStyle()) as unknown) as IVectorLayer, () => { - return (value: string | number | undefined) => value + '_format'; + return (value: RawValue) => value + '_format'; } ); }; @@ -273,7 +273,7 @@ describe('supportsFieldMeta', () => { null, (new MockLayer(new MockStyle()) as unknown) as IVectorLayer, () => { - return (value: string | number | undefined) => value + '_format'; + return (value: RawValue) => value + '_format'; } ); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_icon_property.test.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_icon_property.test.tsx index 06987ab8bcc48..2f9e4709c1c0b 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_icon_property.test.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_icon_property.test.tsx @@ -13,7 +13,7 @@ jest.mock('../components/vector_style_editor', () => ({ })); import React from 'react'; -import { VECTOR_STYLES } from '../../../../../common/constants'; +import { RawValue, VECTOR_STYLES } from '../../../../../common/constants'; // @ts-ignore import { DynamicIconProperty } from './dynamic_icon_property'; import { mockField, MockLayer } from './__tests__/test_util'; @@ -33,7 +33,7 @@ const makeProperty = (options: Partial, field: IField = mock field, mockVectorLayer, () => { - return (value: string | number | undefined) => value + '_format'; + return (value: RawValue) => value + '_format'; } ); }; diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts index 81b5e46d88af9..192fa1f4db6e0 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_orientation_property.ts @@ -7,7 +7,7 @@ import { Map as MbMap } from 'mapbox-gl'; import { DynamicStyleProperty, getNumericalMbFeatureStateValue } from './dynamic_style_property'; import { OrientationDynamicOptions } from '../../../../../common/descriptor_types'; -import { RawValue } from './style_property'; +import { RawValue } from '../../../../../common/constants'; export class DynamicOrientationProperty extends DynamicStyleProperty { syncIconRotationWithMb(symbolLayerId: string, mbMap: MbMap) { diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_size_property.test.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_size_property.test.tsx index c5298067f6cbe..b4244cf7829c4 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_size_property.test.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_size_property.test.tsx @@ -15,7 +15,7 @@ import { shallow } from 'enzyme'; // @ts-ignore import { DynamicSizeProperty } from './dynamic_size_property'; -import { VECTOR_STYLES } from '../../../../../common/constants'; +import { RawValue, VECTOR_STYLES } from '../../../../../common/constants'; import { IField } from '../../../fields/field'; import { Map as MbMap } from 'mapbox-gl'; import { SizeDynamicOptions } from '../../../../../common/descriptor_types'; @@ -48,7 +48,7 @@ const makeProperty = ( field, (new MockLayer(mockStyle) as unknown) as IVectorLayer, () => { - return (value: string | number | undefined) => value + '_format'; + return (value: RawValue) => value + '_format'; }, false ); diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_size_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_size_property.tsx index 35c830f3cb5e3..4e75a61539ad9 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_size_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_size_property.tsx @@ -7,7 +7,7 @@ import _ from 'lodash'; import React from 'react'; import { Map as MbMap } from 'mapbox-gl'; -import { DynamicStyleProperty, FieldFormatter } from './dynamic_style_property'; +import { DynamicStyleProperty } from './dynamic_style_property'; import { OrdinalLegend } from '../components/legend/ordinal_legend'; import { makeMbClampedNumberExpression } from '../style_util'; import { @@ -16,7 +16,7 @@ import { SMALL_MAKI_ICON_SIZE, // @ts-expect-error } from '../symbol_utils'; -import { MB_LOOKUP_FUNCTION, VECTOR_STYLES } from '../../../../../common/constants'; +import { FieldFormatter, MB_LOOKUP_FUNCTION, VECTOR_STYLES } from '../../../../../common/constants'; import { SizeDynamicOptions } from '../../../../../common/descriptor_types'; import { IField } from '../../../fields/field'; import { IVectorLayer } from '../../../layers/vector_layer/vector_layer'; diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx index 26be467ad64dc..2bc819daeea90 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_style_property.tsx @@ -7,7 +7,7 @@ import _ from 'lodash'; import React from 'react'; import { Feature } from 'geojson'; -import { AbstractStyleProperty, IStyleProperty, RawValue } from './style_property'; +import { AbstractStyleProperty, IStyleProperty } from './style_property'; import { DEFAULT_SIGMA } from '../vector_style_defaults'; import { FIELD_ORIGIN, @@ -15,6 +15,8 @@ import { SOURCE_META_DATA_REQUEST_ID, STYLE_TYPE, VECTOR_STYLES, + RawValue, + FieldFormatter, } from '../../../../../common/constants'; import { OrdinalFieldMetaPopover } from '../components/field_meta/ordinal_field_meta_popover'; import { CategoricalFieldMetaPopover } from '../components/field_meta/categorical_field_meta_popover'; @@ -58,8 +60,6 @@ export interface IDynamicStyleProperty extends IStyleProperty { getMbPropertyValue(value: RawValue): RawValue; } -export type FieldFormatter = (value: string | number | undefined) => string | number; - export class DynamicStyleProperty extends AbstractStyleProperty implements IDynamicStyleProperty { @@ -327,7 +327,7 @@ export class DynamicStyleProperty if (this.getField()) { const fieldName = this.getFieldName(); const fieldFormatter = this._getFieldFormatter(fieldName); - return fieldFormatter && value !== null ? fieldFormatter(value) : super.formatField(value); + return fieldFormatter ? fieldFormatter(value) : super.formatField(value); } else { return super.formatField(value); } diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts index fcda64c738dc7..ec79d71eb7587 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/dynamic_text_property.ts @@ -7,7 +7,7 @@ import { Map as MbMap } from 'mapbox-gl'; import { DynamicStyleProperty } from './dynamic_style_property'; import { LabelDynamicOptions } from '../../../../../common/descriptor_types'; -import { RawValue } from './style_property'; +import { RawValue } from '../../../../../common/constants'; export class DynamicTextProperty extends DynamicStyleProperty { syncTextFieldWithMb(mbLayerId: string, mbMap: MbMap) { diff --git a/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts b/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts index f3af1f665bf06..20e0bb5465561 100644 --- a/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts +++ b/x-pack/plugins/maps/public/classes/styles/vector/properties/style_property.ts @@ -9,8 +9,7 @@ import { ReactElement } from 'react'; // @ts-ignore import { getVectorStyleLabel } from '../components/get_vector_style_label'; import { FieldMetaOptions } from '../../../../../common/descriptor_types'; -import { VECTOR_STYLES } from '../../../../../common/constants'; -export type RawValue = string | number | undefined | null; +import { RawValue, VECTOR_STYLES } from '../../../../../common/constants'; export type LegendProps = { isPointsOnly: boolean; @@ -55,7 +54,13 @@ export class AbstractStyleProperty implements IStyleProperty { } formatField(value: RawValue): string | number { - return typeof value === 'undefined' || value === null ? '' : value; + if (typeof value === 'undefined' || value === null) { + return ''; + } else if (typeof value === 'boolean') { + return value.toString(); + } else { + return value; + } } getStyleName(): VECTOR_STYLES {