Skip to content

Commit eaec9da

Browse files
Convert VectorLayer to typescript (#78490) (#79239)
* [maps] convert VectorLayer to TS * more tslint fixes * clean up * more tslint fixes * more tslint fixes * remove unneeded casts * remove unneeded VectorStyle casts * revert changes to layer.getQuery * fix * update tile layer constructor * review feedback Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 27c412b commit eaec9da

File tree

26 files changed

+478
-292
lines changed

26 files changed

+478
-292
lines changed

x-pack/plugins/maps/common/descriptor_types/data_request_descriptor_types.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,21 @@ export type VectorSourceRequestMeta = MapFilters & {
3838
applyGlobalQuery: boolean;
3939
fieldNames: string[];
4040
geogridPrecision?: number;
41-
sourceQuery: MapQuery;
41+
sourceQuery?: MapQuery;
4242
sourceMeta: VectorSourceSyncMeta;
4343
};
4444

45+
export type VectorJoinSourceRequestMeta = MapFilters & {
46+
applyGlobalQuery: boolean;
47+
fieldNames: string[];
48+
sourceQuery: MapQuery;
49+
};
50+
4551
export type VectorStyleRequestMeta = MapFilters & {
4652
dynamicStyleFields: string[];
4753
isTimeAware: boolean;
4854
sourceQuery: MapQuery;
49-
timeFilters: unknown;
55+
timeFilters: TimeRange;
5056
};
5157

5258
export type ESSearchSourceResponseMeta = {
@@ -59,9 +65,12 @@ export type ESSearchSourceResponseMeta = {
5965
};
6066

6167
// Partial because objects are justified downstream in constructors
62-
export type DataMeta = Partial<VectorSourceRequestMeta> &
63-
Partial<VectorStyleRequestMeta> &
64-
Partial<ESSearchSourceResponseMeta>;
68+
export type DataMeta = Partial<
69+
VectorSourceRequestMeta &
70+
VectorJoinSourceRequestMeta &
71+
VectorStyleRequestMeta &
72+
ESSearchSourceResponseMeta
73+
>;
6574

6675
type NumericalStyleFieldData = {
6776
avg: number;

x-pack/plugins/maps/common/descriptor_types/map_descriptor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type MapExtent = {
1717
};
1818

1919
export type MapQuery = Query & {
20-
queryLastTriggeredAt: string;
20+
queryLastTriggeredAt?: string;
2121
};
2222

2323
export type MapRefreshConfig = {

x-pack/plugins/maps/common/elasticsearch_util/es_agg_utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ export function addFieldToDSL(dsl: object, field: IFieldType) {
3333
};
3434
}
3535

36+
export type BucketProperties = Record<string | number, unknown>;
37+
3638
export function extractPropertiesFromBucket(bucket: any, ignoreKeys: string[] = []) {
37-
const properties: Record<string | number, unknown> = {};
39+
const properties: BucketProperties = {};
3840
for (const key in bucket) {
3941
if (ignoreKeys.includes(key) || !bucket.hasOwnProperty(key)) {
4042
continue;

x-pack/plugins/maps/public/actions/data_request_actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const FIT_TO_BOUNDS_SCALE_FACTOR = 0.1;
4747

4848
export type DataRequestContext = {
4949
startLoading(dataId: string, requestToken: symbol, meta: DataMeta): void;
50-
stopLoading(dataId: string, requestToken: symbol, data: object, meta: DataMeta): void;
50+
stopLoading(dataId: string, requestToken: symbol, data: object, meta?: DataMeta): void;
5151
onLoadError(dataId: string, requestToken: symbol, errorMessage: string): void;
5252
updateSourceData(newData: unknown): void;
5353
isRequestStillActive(dataId: string, requestToken: symbol): boolean;

x-pack/plugins/maps/public/classes/joins/inner_join.d.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,40 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { Feature, GeoJsonProperties } from 'geojson';
78
import { IESTermSource } from '../sources/es_term_source';
8-
import { IJoin } from './join';
9+
import { IJoin, PropertiesMap } from './join';
910
import { JoinDescriptor } from '../../../common/descriptor_types';
1011
import { ISource } from '../sources/source';
12+
import { ITooltipProperty } from '../tooltips/tooltip_property';
13+
import { IField } from '../fields/field';
1114

1215
export class InnerJoin implements IJoin {
1316
constructor(joinDescriptor: JoinDescriptor, leftSource: ISource);
1417

18+
destroy: () => void;
19+
1520
getRightJoinSource(): IESTermSource;
1621

1722
toDescriptor(): JoinDescriptor;
1823

24+
getJoinFields: () => IField[];
25+
26+
getLeftField: () => IField;
27+
28+
getIndexPatternIds: () => string[];
29+
30+
getQueryableIndexPatternIds: () => string[];
31+
32+
getSourceDataRequestId: () => string;
33+
1934
getSourceMetaDataRequestId(): string;
2035

2136
getSourceFormattersDataRequestId(): string;
37+
38+
getTooltipProperties(properties: GeoJsonProperties): Promise<ITooltipProperty[]>;
39+
40+
hasCompleteConfig: () => boolean;
41+
42+
joinPropertiesToFeature: (feature: Feature, propertiesMap?: PropertiesMap) => boolean;
2243
}

x-pack/plugins/maps/public/classes/joins/join.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,39 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { Feature, GeoJsonProperties } from 'geojson';
78
import { IESTermSource } from '../sources/es_term_source';
89
import { JoinDescriptor } from '../../../common/descriptor_types';
10+
import { ITooltipProperty } from '../tooltips/tooltip_property';
11+
import { IField } from '../fields/field';
12+
import { BucketProperties } from '../../../common/elasticsearch_util';
13+
14+
export type PropertiesMap = Map<string, BucketProperties>;
915

1016
export interface IJoin {
11-
getRightJoinSource(): IESTermSource;
17+
destroy: () => void;
18+
19+
getRightJoinSource: () => IESTermSource;
20+
21+
toDescriptor: () => JoinDescriptor;
22+
23+
getJoinFields: () => IField[];
24+
25+
getLeftField: () => IField;
26+
27+
getIndexPatternIds: () => string[];
28+
29+
getQueryableIndexPatternIds: () => string[];
30+
31+
getSourceDataRequestId: () => string;
32+
33+
getSourceMetaDataRequestId: () => string;
34+
35+
getSourceFormattersDataRequestId: () => string;
1236

13-
toDescriptor(): JoinDescriptor;
37+
getTooltipProperties: (properties: GeoJsonProperties) => Promise<ITooltipProperty[]>;
1438

15-
getSourceMetaDataRequestId(): string;
39+
hasCompleteConfig: () => boolean;
1640

17-
getSourceFormattersDataRequestId(): string;
41+
joinPropertiesToFeature: (feature: Feature, propertiesMap?: PropertiesMap) => boolean;
1842
}

x-pack/plugins/maps/public/classes/layers/blended_vector_layer/blended_vector_layer.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import {
3737
LayerDescriptor,
3838
VectorLayerDescriptor,
3939
} from '../../../../common/descriptor_types';
40-
import { IStyle } from '../../styles/style';
4140
import { IVectorSource } from '../../sources/vector_source';
4241

4342
const ACTIVE_COUNT_DATA_ID = 'ACTIVE_COUNT_DATA_ID';
@@ -257,7 +256,7 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer {
257256
return clonedDescriptor;
258257
}
259258

260-
getSource() {
259+
getSource(): IVectorSource {
261260
return this._isClustered ? this._clusterSource : this._documentSource;
262261
}
263262

@@ -268,11 +267,11 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer {
268267
return this._documentSource;
269268
}
270269

271-
getCurrentStyle(): IStyle {
270+
getCurrentStyle(): IVectorStyle {
272271
return this._isClustered ? this._clusterStyle : this._documentStyle;
273272
}
274273

275-
getStyleForEditing(): IStyle {
274+
getStyleForEditing(): IVectorStyle {
276275
return this._documentStyle;
277276
}
278277

@@ -281,8 +280,8 @@ export class BlendedVectorLayer extends VectorLayer implements IVectorLayer {
281280
const requestToken = Symbol(`layer-active-count:${this.getId()}`);
282281
const searchFilters = this._getSearchFilters(
283282
syncContext.dataFilters,
284-
this.getSource() as IVectorSource,
285-
this.getCurrentStyle() as IVectorStyle
283+
this.getSource(),
284+
this.getCurrentStyle()
286285
);
287286
const canSkipFetch = await canSkipSourceUpdate({
288287
source: this.getSource(),

x-pack/plugins/maps/public/classes/layers/heatmap_layer/heatmap_layer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ export class HeatmapLayer extends VectorLayer {
3131
}
3232
}
3333

34+
getStyleForEditing() {
35+
return this._style;
36+
}
37+
38+
getStyle() {
39+
return this._style;
40+
}
41+
42+
getCurrentStyle() {
43+
return this._style;
44+
}
45+
3446
_getPropKeyOfSelectedMetric() {
3547
const metricfields = this.getSource().getMetricFields();
3648
return metricfields[0].getName();

x-pack/plugins/maps/public/classes/layers/layer.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import { AbstractLayer } from './layer';
99
import { ISource } from '../sources/source';
10-
import { IStyle } from '../styles/style';
1110
import { AGG_TYPE, FIELD_ORIGIN, LAYER_STYLE_TYPE, VECTOR_STYLES } from '../../../common/constants';
1211
import { ESTermSourceDescriptor, VectorStyleDescriptor } from '../../../common/descriptor_types';
1312
import { getDefaultDynamicProperties } from '../styles/vector/vector_style_defaults';
@@ -38,8 +37,6 @@ class MockSource {
3837
}
3938
}
4039

41-
class MockStyle {}
42-
4340
describe('cloneDescriptor', () => {
4441
describe('with joins', () => {
4542
const styleDescriptor = {
@@ -84,7 +81,6 @@ describe('cloneDescriptor', () => {
8481
const layer = new MockLayer({
8582
layerDescriptor,
8683
source: (new MockSource() as unknown) as ISource,
87-
style: (new MockStyle() as unknown) as IStyle,
8884
});
8985
const clonedDescriptor = await layer.cloneDescriptor();
9086
const clonedStyleProps = (clonedDescriptor.style as VectorStyleDescriptor).properties;
@@ -122,7 +118,6 @@ describe('cloneDescriptor', () => {
122118
const layer = new MockLayer({
123119
layerDescriptor,
124120
source: (new MockSource() as unknown) as ISource,
125-
style: (new MockStyle() as unknown) as IStyle,
126121
});
127122
const clonedDescriptor = await layer.cloneDescriptor();
128123
const clonedStyleProps = (clonedDescriptor.style as VectorStyleDescriptor).properties;
@@ -165,7 +160,6 @@ describe('isFittable', () => {
165160
const layer = new MockLayer({
166161
layerDescriptor,
167162
source: (new MockSource({ fitToBounds: test.fitToBounds }) as unknown) as ISource,
168-
style: (new MockStyle() as unknown) as IStyle,
169163
});
170164
expect(await layer.isFittable()).toBe(test.canFit);
171165
});

x-pack/plugins/maps/public/classes/layers/layer.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,11 @@ export type CustomIconAndTooltipContent = {
110110
export interface ILayerArguments {
111111
layerDescriptor: LayerDescriptor;
112112
source: ISource;
113-
style: IStyle;
114113
}
115114

116115
export class AbstractLayer implements ILayer {
117116
protected readonly _descriptor: LayerDescriptor;
118117
protected readonly _source: ISource;
119-
protected readonly _style: IStyle;
120118
protected readonly _dataRequests: DataRequest[];
121119

122120
static createDescriptor(options: Partial<LayerDescriptor>): LayerDescriptor {
@@ -140,10 +138,9 @@ export class AbstractLayer implements ILayer {
140138
}
141139
}
142140

143-
constructor({ layerDescriptor, source, style }: ILayerArguments) {
141+
constructor({ layerDescriptor, source }: ILayerArguments) {
144142
this._descriptor = AbstractLayer.createDescriptor(layerDescriptor);
145143
this._source = source;
146-
this._style = style;
147144
if (this._descriptor.__dataRequests) {
148145
this._dataRequests = this._descriptor.__dataRequests.map(
149146
(dataRequest) => new DataRequest(dataRequest)
@@ -257,11 +254,15 @@ export class AbstractLayer implements ILayer {
257254
}
258255

259256
getStyleForEditing(): IStyle {
260-
return this._style;
257+
throw new Error('Should implement AbstractLayer#getStyleForEditing');
261258
}
262259

263-
getStyle() {
264-
return this._style;
260+
getStyle(): IStyle {
261+
throw new Error('Should implement AbstractLayer#getStyle');
262+
}
263+
264+
getCurrentStyle(): IStyle {
265+
throw new Error('Should implement AbstractLayer#getCurrentStyle');
265266
}
266267

267268
getLabel(): string {
@@ -412,10 +413,6 @@ export class AbstractLayer implements ILayer {
412413
return this._descriptor.query ? this._descriptor.query : null;
413414
}
414415

415-
getCurrentStyle(): IStyle {
416-
return this._style;
417-
}
418-
419416
async getImmutableSourceProperties() {
420417
const source = this.getSource();
421418
return await source.getImmutableProperties();

0 commit comments

Comments
 (0)