Skip to content

Commit b1431e8

Browse files
Aaron Caldwellelasticmachine
andauthored
[7.x] Remove injected vars from maps legacy (#69744) (#70504)
Co-authored-by: Elastic Machine <[email protected]> Co-authored-by: Elastic Machine <[email protected]>
1 parent 930826d commit b1431e8

File tree

11 files changed

+62
-112
lines changed

11 files changed

+62
-112
lines changed

src/legacy/core_plugins/kibana/public/__tests__/vis_type_vega/vega_visualization.js

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ import {
6666
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
6767
} from '../../../../../../plugins/vis_type_vega/public/services';
6868
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
69-
import { setInjectedVarFunc } from '../../../../../../plugins/maps_legacy/public/kibana_services';
70-
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
7169
import { ServiceSettings } from '../../../../../../plugins/maps_legacy/public/map/service_settings';
72-
import { getKibanaMapFactoryProvider } from '../../../../../../plugins/maps_legacy/public';
70+
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
71+
import { KibanaMap } from '../../../../../../plugins/maps_legacy/public/map/kibana_map';
7372

7473
const THRESHOLD = 0.1;
7574
const PIXEL_DIFF = 30;
@@ -82,18 +81,7 @@ describe('VegaVisualizations', () => {
8281
let vegaVisualizationDependencies;
8382
let vegaVisType;
8483

85-
const coreSetupMock = {
86-
notifications: {
87-
toasts: {},
88-
},
89-
uiSettings: {
90-
get: () => {},
91-
},
92-
injectedMetadata: {
93-
getInjectedVar: () => {},
94-
},
95-
};
96-
setKibanaMapFactory(getKibanaMapFactoryProvider(coreSetupMock));
84+
setKibanaMapFactory((...args) => new KibanaMap(...args));
9785
setInjectedVars({
9886
emsTileLayerId: {},
9987
enableExternalUrls: true,
@@ -139,30 +127,6 @@ describe('VegaVisualizations', () => {
139127
beforeEach(ngMock.module('kibana'));
140128
beforeEach(
141129
ngMock.inject(() => {
142-
setInjectedVarFunc((injectedVar) => {
143-
switch (injectedVar) {
144-
case 'mapConfig':
145-
return {
146-
emsFileApiUrl: '',
147-
emsTileApiUrl: '',
148-
emsLandingPageUrl: '',
149-
};
150-
case 'tilemapsConfig':
151-
return {
152-
deprecated: {
153-
config: {
154-
options: {
155-
attribution: '123',
156-
},
157-
},
158-
},
159-
};
160-
case 'version':
161-
return '123';
162-
default:
163-
return 'not found';
164-
}
165-
});
166130
const serviceSettings = new ServiceSettings(mockMapConfig, mockMapConfig.tilemap);
167131
vegaVisualizationDependencies = {
168132
serviceSettings,

src/plugins/maps_legacy/public/index.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
*/
1919

2020
// @ts-ignore
21-
import { CoreSetup, PluginInitializerContext } from 'kibana/public';
21+
import { PluginInitializerContext } from 'kibana/public';
2222
// @ts-ignore
2323
import { L } from './leaflet';
24-
// @ts-ignore
25-
import { KibanaMap } from './map/kibana_map';
26-
import { bindSetupCoreAndPlugins, MapsLegacyPlugin } from './plugin';
24+
import { MapsLegacyPlugin } from './plugin';
2725
// @ts-ignore
2826
import * as colorUtil from './map/color_util';
2927
// @ts-ignore
@@ -32,8 +30,6 @@ import { KibanaMapLayer } from './map/kibana_map_layer';
3230
import { convertToGeoJson } from './map/convert_to_geojson';
3331
// @ts-ignore
3432
import { scaleBounds, getPrecision, geoContains } from './map/decode_geo_hash';
35-
// @ts-ignore
36-
import { BaseMapsVisualizationProvider } from './map/base_maps_visualization';
3733
import {
3834
VectorLayer,
3935
FileLayerField,
@@ -75,20 +71,6 @@ export {
7571
L,
7672
};
7773

78-
// Due to a leaflet/leaflet-draw bug, it's not possible to consume leaflet maps w/ draw control
79-
// through a pipeline leveraging angular. For this reason, client plugins need to
80-
// init kibana map and the basemaps visualization directly rather than consume through
81-
// the usual plugin interface
82-
export function getKibanaMapFactoryProvider(core: CoreSetup) {
83-
bindSetupCoreAndPlugins(core);
84-
return (...args: any) => new KibanaMap(...args);
85-
}
86-
87-
export function getBaseMapsVis(core: CoreSetup, serviceSettings: IServiceSettings) {
88-
const getKibanaMap = getKibanaMapFactoryProvider(core);
89-
return new BaseMapsVisualizationProvider(getKibanaMap, serviceSettings);
90-
}
91-
9274
export * from './common/types';
9375
export { ORIGIN } from './common/constants/origin';
9476

src/plugins/maps_legacy/public/kibana_services.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ let uiSettings;
2525
export const setUiSettings = (coreUiSettings) => (uiSettings = coreUiSettings);
2626
export const getUiSettings = () => uiSettings;
2727

28-
let getInjectedVar;
29-
export const setInjectedVarFunc = (getInjectedVarFunc) => (getInjectedVar = getInjectedVarFunc);
30-
export const getInjectedVarFunc = () => getInjectedVar;
28+
let kibanaVersion;
29+
export const setKibanaVersion = (version) => (kibanaVersion = version);
30+
export const getKibanaVersion = () => kibanaVersion;
31+
32+
let mapsLegacyConfig;
33+
export const setMapsLegacyConfig = (config) => (mapsLegacyConfig = config);
34+
export const getMapsLegacyConfig = () => mapsLegacyConfig;
35+
36+
export const getEmsTileLayerId = () => getMapsLegacyConfig().emsTileLayerId;

src/plugins/maps_legacy/public/map/base_maps_visualization.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import _ from 'lodash';
2121
import { i18n } from '@kbn/i18n';
2222
import * as Rx from 'rxjs';
2323
import { filter, first } from 'rxjs/operators';
24-
import { getInjectedVarFunc, getUiSettings, getToasts } from '../kibana_services';
24+
import { getEmsTileLayerId, getUiSettings, getToasts } from '../kibana_services';
2525

2626
const WMS_MINZOOM = 0;
2727
const WMS_MAXZOOM = 22; //increase this to 22. Better for WMS
@@ -129,7 +129,7 @@ export function BaseMapsVisualizationProvider(getKibanaMap, mapServiceSettings)
129129
}
130130

131131
async _updateBaseLayer() {
132-
const emsTileLayerId = getInjectedVarFunc()('emsTileLayerId', true);
132+
const emsTileLayerId = getEmsTileLayerId();
133133

134134
if (!this._kibanaMap) {
135135
return;

src/plugins/maps_legacy/public/map/service_settings.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,20 @@ import _ from 'lodash';
2121
import MarkdownIt from 'markdown-it';
2222
import { EMSClient } from '@elastic/ems-client';
2323
import { i18n } from '@kbn/i18n';
24-
import { getInjectedVarFunc } from '../kibana_services';
24+
import { getKibanaVersion } from '../kibana_services';
2525
import { ORIGIN } from '../common/constants/origin';
2626

2727
const TMS_IN_YML_ID = 'TMS in config/kibana.yml';
2828

2929
export class ServiceSettings {
3030
constructor(mapConfig, tilemapsConfig) {
31-
const getInjectedVar = getInjectedVarFunc();
3231
this._mapConfig = mapConfig;
3332
this._tilemapsConfig = tilemapsConfig;
34-
const kbnVersion = getInjectedVar('version');
3533

3634
this._showZoomMessage = true;
3735
this._emsClient = new EMSClient({
3836
language: i18n.getLocale(),
39-
appVersion: kbnVersion,
37+
appVersion: getKibanaVersion(),
4038
appName: 'kibana',
4139
fileApiUrl: this._mapConfig.emsFileApiUrl,
4240
tileApiUrl: this._mapConfig.emsTileApiUrl,

src/plugins/maps_legacy/public/plugin.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,33 @@
2020
// @ts-ignore
2121
import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from 'kibana/public';
2222
// @ts-ignore
23-
import { setToasts, setUiSettings, setInjectedVarFunc } from './kibana_services';
23+
import { setToasts, setUiSettings, setKibanaVersion, setMapsLegacyConfig } from './kibana_services';
2424
// @ts-ignore
2525
import { ServiceSettings } from './map/service_settings';
2626
// @ts-ignore
2727
import { getPrecision, getZoomPrecision } from './map/precision';
28+
// @ts-ignore
29+
import { KibanaMap } from './map/kibana_map';
2830
import { MapsLegacyConfigType, MapsLegacyPluginSetup, MapsLegacyPluginStart } from './index';
2931
import { ConfigSchema } from '../config';
32+
// @ts-ignore
33+
import { BaseMapsVisualizationProvider } from './map/base_maps_visualization';
3034

3135
/**
3236
* These are the interfaces with your public contracts. You should export these
3337
* for other plugins to use in _their_ `SetupDeps`/`StartDeps` interfaces.
3438
* @public
3539
*/
3640

37-
export const bindSetupCoreAndPlugins = (core: CoreSetup) => {
41+
export const bindSetupCoreAndPlugins = (
42+
core: CoreSetup,
43+
config: MapsLegacyConfigType,
44+
kibanaVersion: string
45+
) => {
3846
setToasts(core.notifications.toasts);
3947
setUiSettings(core.uiSettings);
40-
setInjectedVarFunc(core.injectedMetadata.getInjectedVar);
48+
setKibanaVersion(kibanaVersion);
49+
setMapsLegacyConfig(config);
4150
};
4251

4352
// eslint-disable-next-line @typescript-eslint/no-empty-interface
@@ -53,15 +62,23 @@ export class MapsLegacyPlugin implements Plugin<MapsLegacyPluginSetup, MapsLegac
5362
}
5463

5564
public setup(core: CoreSetup, plugins: MapsLegacySetupDependencies) {
56-
bindSetupCoreAndPlugins(core);
57-
5865
const config = this._initializerContext.config.get<MapsLegacyConfigType>();
66+
const kibanaVersion = this._initializerContext.env.packageInfo.version;
67+
68+
bindSetupCoreAndPlugins(core, config, kibanaVersion);
69+
70+
const serviceSettings = new ServiceSettings(config, config.tilemap);
71+
const getKibanaMapFactoryProvider = (...args: any) => new KibanaMap(...args);
72+
const getBaseMapsVis = () =>
73+
new BaseMapsVisualizationProvider(getKibanaMapFactoryProvider, serviceSettings);
5974

6075
return {
61-
serviceSettings: new ServiceSettings(config, config.tilemap),
76+
serviceSettings,
6277
getZoomPrecision,
6378
getPrecision,
6479
config,
80+
getKibanaMapFactoryProvider,
81+
getBaseMapsVis,
6582
};
6683
}
6784

src/plugins/region_map/public/__tests__/region_map_visualization.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ import { ExprVis } from '../../../visualizations/public/expressions/vis';
5252
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
5353
import { BaseVisType } from '../../../visualizations/public/vis_types/base_vis_type';
5454
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
55-
import { setInjectedVarFunc } from '../../../maps_legacy/public/kibana_services';
56-
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
5755
import { ServiceSettings } from '../../../maps_legacy/public/map/service_settings';
58-
import { getBaseMapsVis } from '../../../maps_legacy/public';
56+
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
57+
import { BaseMapsVisualizationProvider } from '../../../maps_legacy/public/map/base_maps_visualization';
58+
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
59+
import { KibanaMap } from '../../../maps_legacy/public/map/kibana_map';
5960

6061
const THRESHOLD = 0.45;
6162
const PIXEL_DIFF = 96;
@@ -118,14 +119,6 @@ describe('RegionMapsVisualizationTests', function () {
118119
},
119120
},
120121
};
121-
setInjectedVarFunc((injectedVar) => {
122-
switch (injectedVar) {
123-
case 'version':
124-
return '123';
125-
default:
126-
return 'not found';
127-
}
128-
});
129122
const serviceSettings = new ServiceSettings(mapConfig, tilemapsConfig);
130123
const regionmapsConfig = {
131124
includeElasticMapsService: true,
@@ -142,7 +135,10 @@ describe('RegionMapsVisualizationTests', function () {
142135
getInjectedVar: () => {},
143136
},
144137
};
145-
const BaseMapsVisualization = getBaseMapsVis(coreSetupMock, serviceSettings);
138+
const BaseMapsVisualization = new BaseMapsVisualizationProvider(
139+
(...args) => new KibanaMap(...args),
140+
serviceSettings
141+
);
146142

147143
dependencies = {
148144
serviceSettings,

src/plugins/region_map/public/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { VisualizationsSetup } from '../../visualizations/public';
3030
import { createRegionMapFn } from './region_map_fn';
3131
// @ts-ignore
3232
import { createRegionMapTypeDefinition } from './region_map_type';
33-
import { getBaseMapsVis, IServiceSettings, MapsLegacyPluginSetup } from '../../maps_legacy/public';
33+
import { IServiceSettings, MapsLegacyPluginSetup } from '../../maps_legacy/public';
3434
import { setFormatService, setNotifications, setKibanaLegacy } from './kibana_services';
3535
import { DataPublicPluginStart } from '../../data/public';
3636
import { RegionMapsConfigType } from './index';
@@ -94,7 +94,7 @@ export class RegionMapPlugin implements Plugin<RegionMapPluginSetup, RegionMapPl
9494
uiSettings: core.uiSettings,
9595
regionmapsConfig: config as RegionMapsConfig,
9696
serviceSettings: mapsLegacy.serviceSettings,
97-
BaseMapsVisualization: getBaseMapsVis(core, mapsLegacy.serviceSettings),
97+
BaseMapsVisualization: mapsLegacy.getBaseMapsVis(),
9898
};
9999

100100
expressions.registerFunction(createRegionMapFn);

src/plugins/tile_map/public/__tests__/coordinate_maps_visualization.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ import {
5252
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
5353
import { ServiceSettings } from '../../../maps_legacy/public/map/service_settings';
5454
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
55-
import { setInjectedVarFunc } from '../../../maps_legacy/public/kibana_services';
56-
import { getBaseMapsVis } from '../../../maps_legacy/public';
55+
import { KibanaMap } from '../../../maps_legacy/public/map/kibana_map';
56+
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
57+
import { BaseMapsVisualizationProvider } from '../../../maps_legacy/public/map/base_maps_visualization';
5758

5859
function mockRawData() {
5960
const stack = [dummyESResponse];
@@ -105,26 +106,12 @@ describe('CoordinateMapsVisualizationTest', function () {
105106
},
106107
},
107108
};
108-
setInjectedVarFunc((injectedVar) => {
109-
switch (injectedVar) {
110-
case 'version':
111-
return '123';
112-
default:
113-
return 'not found';
114-
}
115-
});
116109

117-
const coreSetupMock = {
118-
notifications: {
119-
toasts: {},
120-
},
121-
uiSettings: {},
122-
injectedMetadata: {
123-
getInjectedVar: () => {},
124-
},
125-
};
126110
const serviceSettings = new ServiceSettings(mapConfig, tilemapsConfig);
127-
const BaseMapsVisualization = getBaseMapsVis(coreSetupMock, serviceSettings);
111+
const BaseMapsVisualization = new BaseMapsVisualizationProvider(
112+
(...args) => new KibanaMap(...args),
113+
serviceSettings
114+
);
128115
const uiSettings = $injector.get('config');
129116

130117
dependencies = {

src/plugins/tile_map/public/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import 'angular-sanitize';
3232
import { createTileMapFn } from './tile_map_fn';
3333
// @ts-ignore
3434
import { createTileMapTypeDefinition } from './tile_map_type';
35-
import { getBaseMapsVis, MapsLegacyPluginSetup } from '../../maps_legacy/public';
35+
import { MapsLegacyPluginSetup } from '../../maps_legacy/public';
3636
import { DataPublicPluginStart } from '../../data/public';
3737
import { setFormatService, setQueryService } from './services';
3838
import { setKibanaLegacy } from './services';
@@ -85,7 +85,7 @@ export class TileMapPlugin implements Plugin<TileMapPluginSetup, TileMapPluginSt
8585
const visualizationDependencies: Readonly<TileMapVisualizationDependencies> = {
8686
getZoomPrecision,
8787
getPrecision,
88-
BaseMapsVisualization: getBaseMapsVis(core, mapsLegacy.serviceSettings),
88+
BaseMapsVisualization: mapsLegacy.getBaseMapsVis(),
8989
uiSettings: core.uiSettings,
9090
};
9191

0 commit comments

Comments
 (0)