Skip to content

Commit eb533c8

Browse files
[Maps] convert tooltip classes to typescript (#59589)
* getting started * fix ts lint errors * TS es_tooltip_property * convert ESAggTooltipProperty to TS * final clean up * ts lint cleanup * review feedback * remove unused import Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 848188e commit eb533c8

File tree

13 files changed

+194
-151
lines changed

13 files changed

+194
-151
lines changed

x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import { IVectorSource } from '../sources/vector_source';
1313
import { ESDocField } from './es_doc_field';
1414
import { AGG_TYPE, FIELD_ORIGIN } from '../../../common/constants';
1515
import { isMetricCountable } from '../util/is_metric_countable';
16-
// @ts-ignore
17-
import { ESAggMetricTooltipProperty } from '../tooltips/es_aggmetric_tooltip_property';
1816
import { getField, addFieldToDSL } from '../util/es_agg_utils';
1917
import { TopTermPercentageField } from './top_term_percentage_field';
18+
import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property';
19+
import { ESAggTooltipProperty } from '../tooltips/es_agg_tooltip_property';
2020

2121
export interface IESAggField extends IField {
2222
getValueAggDsl(indexPattern: IndexPattern): unknown | null;
@@ -92,15 +92,10 @@ export class ESAggField implements IESAggField {
9292
return this._esDocField ? this._esDocField.getName() : '';
9393
}
9494

95-
async createTooltipProperty(value: number | string): Promise<unknown> {
95+
async createTooltipProperty(value: string | undefined): Promise<ITooltipProperty> {
9696
const indexPattern = await this._source.getIndexPattern();
97-
return new ESAggMetricTooltipProperty(
98-
this.getName(),
99-
await this.getLabel(),
100-
value,
101-
indexPattern,
102-
this
103-
);
97+
const tooltipProperty = new TooltipProperty(this.getName(), await this.getLabel(), value);
98+
return new ESAggTooltipProperty(tooltipProperty, indexPattern, this);
10499
}
105100

106101
getValueAggDsl(indexPattern: IndexPattern): unknown | null {

x-pack/legacy/plugins/maps/public/layers/fields/es_doc_field.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { AbstractField } from './field';
88
import { ESTooltipProperty } from '../tooltips/es_tooltip_property';
9+
import { TooltipProperty } from '../tooltips/tooltip_property';
910
import { COLOR_PALETTE_MAX_SIZE } from '../../../common/constants';
1011
import { indexPatterns } from '../../../../../../../src/plugins/data/public';
1112

@@ -20,7 +21,8 @@ export class ESDocField extends AbstractField {
2021

2122
async createTooltipProperty(value) {
2223
const indexPattern = await this._source.getIndexPattern();
23-
return new ESTooltipProperty(this.getName(), this.getName(), value, indexPattern);
24+
const tooltipProperty = new TooltipProperty(this.getName(), this.getName(), value);
25+
return new ESTooltipProperty(tooltipProperty, indexPattern, this);
2426
}
2527

2628
async getDataType() {

x-pack/legacy/plugins/maps/public/layers/fields/field.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
import { FIELD_ORIGIN } from '../../../common/constants';
88
import { IVectorSource } from '../sources/vector_source';
9+
import { ITooltipProperty } from '../tooltips/tooltip_property';
910

1011
export interface IField {
1112
getName(): string;
1213
getRootName(): string;
1314
canValueBeFormatted(): boolean;
1415
getLabel(): Promise<string>;
1516
getDataType(): Promise<string>;
17+
createTooltipProperty(value: string | undefined): Promise<ITooltipProperty>;
1618
getSource(): IVectorSource;
1719
getOrigin(): FIELD_ORIGIN;
1820
isValid(): boolean;
@@ -65,7 +67,7 @@ export class AbstractField implements IField {
6567
return this._fieldName;
6668
}
6769

68-
async createTooltipProperty(): Promise<unknown> {
70+
async createTooltipProperty(value: string | undefined): Promise<ITooltipProperty> {
6971
throw new Error('must implement Field#createTooltipProperty');
7072
}
7173

x-pack/legacy/plugins/maps/public/layers/fields/top_term_percentage_field.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { IESAggField } from './es_agg_field';
88
import { IVectorSource } from '../sources/vector_source';
99
// @ts-ignore
10-
import { TooltipProperty } from '../tooltips/tooltip_property';
10+
import { ITooltipProperty, TooltipProperty } from '../tooltips/tooltip_property';
1111
import { TOP_TERM_PERCENTAGE_SUFFIX } from '../../../common/constants';
1212
import { FIELD_ORIGIN } from '../../../common/constants';
1313

@@ -48,7 +48,7 @@ export class TopTermPercentageField implements IESAggField {
4848
return 'number';
4949
}
5050

51-
async createTooltipProperty(value: unknown): Promise<unknown> {
51+
async createTooltipProperty(value: string | undefined): Promise<ITooltipProperty> {
5252
return new TooltipProperty(this.getName(), await this.getLabel(), value);
5353
}
5454

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { IESTermSource } from '../sources/es_term_source';
8+
9+
export interface IJoin {
10+
getRightJoinSource(): IESTermSource;
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { IField } from '../fields/field';
8+
import { IESAggSource } from './es_agg_source';
9+
10+
export interface IESTermSource extends IESAggSource {
11+
getTermField(): IField;
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { ESTooltipProperty } from './es_tooltip_property';
7+
8+
export class ESAggTooltipProperty extends ESTooltipProperty {
9+
isFilterable(): boolean {
10+
return false;
11+
}
12+
}

x-pack/legacy/plugins/maps/public/layers/tooltips/es_aggmetric_tooltip_property.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

x-pack/legacy/plugins/maps/public/layers/tooltips/es_tooltip_property.js

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import _ from 'lodash';
8+
import { ITooltipProperty } from './tooltip_property';
9+
import { IField } from '../fields/field';
10+
import { esFilters, IFieldType, IndexPattern } from '../../../../../../../src/plugins/data/public';
11+
import { PhraseFilter } from '../../../../../../../src/plugins/data/public';
12+
13+
export class ESTooltipProperty implements ITooltipProperty {
14+
private readonly _tooltipProperty: ITooltipProperty;
15+
private readonly _indexPattern: IndexPattern;
16+
private readonly _field: IField;
17+
18+
constructor(tooltipProperty: ITooltipProperty, indexPattern: IndexPattern, field: IField) {
19+
this._tooltipProperty = tooltipProperty;
20+
this._indexPattern = indexPattern;
21+
this._field = field;
22+
}
23+
24+
getPropertyKey(): string {
25+
return this._tooltipProperty.getPropertyKey();
26+
}
27+
28+
getPropertyName(): string {
29+
return this._tooltipProperty.getPropertyName();
30+
}
31+
32+
getRawValue(): string | undefined {
33+
return this._tooltipProperty.getRawValue();
34+
}
35+
36+
_getIndexPatternField(): IFieldType | undefined {
37+
return this._indexPattern.fields.getByName(this._field.getRootName());
38+
}
39+
40+
getHtmlDisplayValue(): string {
41+
if (typeof this.getRawValue() === 'undefined') {
42+
return '-';
43+
}
44+
45+
const indexPatternField = this._getIndexPatternField();
46+
if (!indexPatternField || !this._field.canValueBeFormatted()) {
47+
return _.escape(this.getRawValue());
48+
}
49+
50+
const htmlConverter = indexPatternField.format.getConverterFor('html');
51+
return htmlConverter
52+
? htmlConverter(this.getRawValue())
53+
: indexPatternField.format.convert(this.getRawValue());
54+
}
55+
56+
isFilterable(): boolean {
57+
const indexPatternField = this._getIndexPatternField();
58+
return (
59+
!!indexPatternField &&
60+
(indexPatternField.type === 'string' ||
61+
indexPatternField.type === 'date' ||
62+
indexPatternField.type === 'ip' ||
63+
indexPatternField.type === 'number')
64+
);
65+
}
66+
67+
async getESFilters(): Promise<PhraseFilter[]> {
68+
const indexPatternField = this._getIndexPatternField();
69+
if (!indexPatternField) {
70+
return [];
71+
}
72+
73+
return [esFilters.buildPhraseFilter(indexPatternField, this.getRawValue(), this._indexPattern)];
74+
}
75+
}

0 commit comments

Comments
 (0)