diff --git a/x-pack/legacy/plugins/maps/public/angular/get_initial_layers.js b/x-pack/legacy/plugins/maps/public/angular/get_initial_layers.js index 3cae75231d28e..8fc32aef54770 100644 --- a/x-pack/legacy/plugins/maps/public/angular/get_initial_layers.js +++ b/x-pack/legacy/plugins/maps/public/angular/get_initial_layers.js @@ -6,7 +6,7 @@ import _ from 'lodash'; import { KibanaTilemapSource } from '../layers/sources/kibana_tilemap_source'; import { EMSTMSSource } from '../layers/sources/ems_tms_source'; -import chrome from 'ui/chrome'; +import { getInjectedVarFunc } from '../kibana_services'; import { getKibanaTileMap } from '../meta'; export function getInitialLayers(layerListJSON, initialLayers = []) { @@ -22,7 +22,7 @@ export function getInitialLayers(layerListJSON, initialLayers = []) { return [layer.toLayerDescriptor(), ...initialLayers]; } - const isEmsEnabled = chrome.getInjected('isEmsEnabled', true); + const isEmsEnabled = getInjectedVarFunc()('isEmsEnabled', true); if (isEmsEnabled) { const descriptor = EMSTMSSource.createDescriptor({ isAutoSelect: true }); const source = new EMSTMSSource(descriptor); diff --git a/x-pack/legacy/plugins/maps/public/angular/get_initial_layers.test.js b/x-pack/legacy/plugins/maps/public/angular/get_initial_layers.test.js index a62d46475a549..f41ed26b2a05d 100644 --- a/x-pack/legacy/plugins/maps/public/angular/get_initial_layers.test.js +++ b/x-pack/legacy/plugins/maps/public/angular/get_initial_layers.test.js @@ -7,16 +7,17 @@ jest.mock('../meta', () => { return {}; }); - -jest.mock('ui/chrome', () => { - return {}; -}); +jest.mock('../kibana_services'); import { getInitialLayers } from './get_initial_layers'; const layerListNotProvided = undefined; describe('Saved object has layer list', () => { + beforeEach(() => { + require('../kibana_services').getInjectedVarFunc = () => jest.fn(); + }); + it('Should get initial layers from saved object', () => { const layerListFromSavedObject = [ { @@ -64,7 +65,7 @@ describe('EMS is enabled', () => { require('../meta').getKibanaTileMap = () => { return null; }; - require('ui/chrome').getInjected = key => { + require('../kibana_services').getInjectedVarFunc = () => key => { switch (key) { case 'emsTileLayerId': return { @@ -75,7 +76,7 @@ describe('EMS is enabled', () => { case 'isEmsEnabled': return true; default: - throw new Error(`Unexpected call to chrome.getInjected with key ${key}`); + throw new Error(`Unexpected call to getInjectedVarFunc with key ${key}`); } }; }); @@ -109,12 +110,12 @@ describe('EMS is not enabled', () => { return null; }; - require('ui/chrome').getInjected = key => { + require('../kibana_services').getInjectedVarFunc = () => key => { switch (key) { case 'isEmsEnabled': return false; default: - throw new Error(`Unexpected call to chrome.getInjected with key ${key}`); + throw new Error(`Unexpected call to getInjectedVarFunc with key ${key}`); } }; }); diff --git a/x-pack/legacy/plugins/maps/public/angular/map_controller.js b/x-pack/legacy/plugins/maps/public/angular/map_controller.js index 7b3dc74d777b2..519ba0b1e3d96 100644 --- a/x-pack/legacy/plugins/maps/public/angular/map_controller.js +++ b/x-pack/legacy/plugins/maps/public/angular/map_controller.js @@ -15,7 +15,7 @@ import { i18n } from '@kbn/i18n'; import { capabilities } from 'ui/capabilities'; import { render, unmountComponentAtNode } from 'react-dom'; import { uiModules } from 'ui/modules'; -import { timefilter } from 'ui/timefilter'; +import { getTimeFilter, getIndexPatternService, getInspector } from '../kibana_services'; import { Provider } from 'react-redux'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { createMapStore } from '../../../../../plugins/maps/public/reducers/store'; @@ -52,7 +52,7 @@ import { // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { getInspectorAdapters } from '../../../../../plugins/maps/public/reducers/non_serializable_instances'; import { docTitle } from 'ui/doc_title'; -import { indexPatternService, getInspector } from '../kibana_services'; + import { toastNotifications } from 'ui/notify'; import { getInitialLayers } from './get_initial_layers'; import { getInitialQuery } from './get_initial_query'; @@ -396,7 +396,7 @@ app.controller( const indexPatterns = []; const getIndexPatternPromises = nextIndexPatternIds.map(async indexPatternId => { try { - const indexPattern = await indexPatternService.get(indexPatternId); + const indexPattern = await getIndexPatternService().get(indexPatternId); indexPatterns.push(indexPattern); } catch (err) { // unable to fetch index pattern @@ -519,8 +519,8 @@ app.controller( } // Hide angular timepicer/refresh UI from top nav - timefilter.disableTimeRangeSelector(); - timefilter.disableAutoRefreshSelector(); + getTimeFilter().disableTimeRangeSelector(); + getTimeFilter().disableAutoRefreshSelector(); $scope.showDatePicker = true; // used by query-bar directive to enable timepikcer in query bar $scope.topNavMenu = [ { diff --git a/x-pack/legacy/plugins/maps/public/components/geo_field_with_index.ts b/x-pack/legacy/plugins/maps/public/components/geo_field_with_index.ts index 863e0adda8fb2..3962da23bd073 100644 --- a/x-pack/legacy/plugins/maps/public/components/geo_field_with_index.ts +++ b/x-pack/legacy/plugins/maps/public/components/geo_field_with_index.ts @@ -7,7 +7,7 @@ // Maps can contain geo fields from multiple index patterns. GeoFieldWithIndex is used to: // 1) Combine the geo field along with associated index pattern state. -// 2) Package asynchronously looked up state via indexPatternService to avoid +// 2) Package asynchronously looked up state via getIndexPatternService() to avoid // PITA of looking up async state in downstream react consumers. export type GeoFieldWithIndex = { geoFieldName: string; diff --git a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/filter_editor/filter_editor.js b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/filter_editor/filter_editor.js index 60bbaa9825db7..f6bcac0dfc339 100644 --- a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/filter_editor/filter_editor.js +++ b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/filter_editor/filter_editor.js @@ -20,7 +20,7 @@ import { import { FormattedMessage } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; -import { indexPatternService } from '../../../kibana_services'; +import { getIndexPatternService } from '../../../kibana_services'; import { GlobalFilterCheckbox } from '../../../components/global_filter_checkbox'; import { npStart } from 'ui/new_platform'; @@ -47,7 +47,7 @@ export class FilterEditor extends Component { const indexPatterns = []; const getIndexPatternPromises = indexPatternIds.map(async indexPatternId => { try { - const indexPattern = await indexPatternService.get(indexPatternId); + const indexPattern = await getIndexPatternService().get(indexPatternId); indexPatterns.push(indexPattern); } catch (err) { // unable to fetch index pattern diff --git a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js index c2c9f333a675c..0df6bd40d1a31 100644 --- a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js +++ b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join.js @@ -14,7 +14,7 @@ import { WhereExpression } from './where_expression'; import { GlobalFilterCheckbox } from '../../../../components/global_filter_checkbox'; import { indexPatterns } from '../../../../../../../../../src/plugins/data/public'; -import { indexPatternService } from '../../../../kibana_services'; +import { getIndexPatternService } from '../../../../kibana_services'; export class Join extends Component { state = { @@ -39,7 +39,7 @@ export class Join extends Component { let indexPattern; try { - indexPattern = await indexPatternService.get(indexPatternId); + indexPattern = await getIndexPatternService().get(indexPatternId); } catch (err) { if (this._isMounted) { this.setState({ diff --git a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join_expression.js b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join_expression.js index 777c8ae0923fe..f7edcf6e85e25 100644 --- a/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join_expression.js +++ b/x-pack/legacy/plugins/maps/public/connected_components/layer_panel/join_editor/resources/join_expression.js @@ -19,11 +19,10 @@ import { i18n } from '@kbn/i18n'; import { SingleFieldSelect } from '../../../../components/single_field_select'; import { FormattedMessage } from '@kbn/i18n/react'; import { getTermsFields } from '../../../../index_pattern_util'; - -import { indexPatternService } from '../../../../kibana_services'; - -import { npStart } from 'ui/new_platform'; -const { IndexPatternSelect } = npStart.plugins.data.ui; +import { + getIndexPatternService, + getIndexPatternSelectComponent, +} from '../../../../kibana_services'; export class JoinExpression extends Component { state = { @@ -44,7 +43,7 @@ export class JoinExpression extends Component { _onRightSourceChange = async indexPatternId => { try { - const indexPattern = await indexPatternService.get(indexPatternId); + const indexPattern = await getIndexPatternService().get(indexPatternId); this.props.onRightSourceChange({ indexPatternId, indexPatternTitle: indexPattern.title, @@ -106,6 +105,7 @@ export class JoinExpression extends Component { if (!this.props.leftValue) { return null; } + const IndexPatternSelect = getIndexPatternSelectComponent(); return ( APP_ICON, }, }); + // Init required services. Necessary while in legacy bindSetupCoreAndPlugins(npSetup.core, npSetup.plugins); + bindStartCoreAndPlugins(npStart.core, npStart.plugins); } isEditable() { return capabilities.get().maps.save; @@ -76,7 +78,7 @@ export class MapEmbeddableFactory extends EmbeddableFactory { const promises = queryableIndexPatternIds.map(async indexPatternId => { try { - return await indexPatternService.get(indexPatternId); + return await getIndexPatternService().get(indexPatternId); } catch (error) { // Unable to load index pattern, better to not throw error so map embeddable can render // Error will be surfaced by map embeddable since it too will be unable to locate the index pattern diff --git a/x-pack/legacy/plugins/maps/public/index_pattern_util.js b/x-pack/legacy/plugins/maps/public/index_pattern_util.js index 7aa87ab32cdf5..30a0a6826db83 100644 --- a/x-pack/legacy/plugins/maps/public/index_pattern_util.js +++ b/x-pack/legacy/plugins/maps/public/index_pattern_util.js @@ -4,14 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ -import { indexPatternService } from './kibana_services'; +import { getIndexPatternService } from './kibana_services'; import { indexPatterns } from '../../../../../src/plugins/data/public'; import { ES_GEO_FIELD_TYPE } from '../common/constants'; export async function getIndexPatternsFromIds(indexPatternIds = []) { const promises = []; indexPatternIds.forEach(id => { - const indexPatternPromise = indexPatternService.get(id); + const indexPatternPromise = getIndexPatternService().get(id); if (indexPatternPromise) { promises.push(indexPatternPromise); } diff --git a/x-pack/legacy/plugins/maps/public/kibana_services.js b/x-pack/legacy/plugins/maps/public/kibana_services.js index 5702eb1c6f846..3b0f501dc0f60 100644 --- a/x-pack/legacy/plugins/maps/public/kibana_services.js +++ b/x-pack/legacy/plugins/maps/public/kibana_services.js @@ -6,12 +6,18 @@ import { esFilters, search } from '../../../../../src/plugins/data/public'; const { getRequestInspectorStats, getResponseInspectorStats } = search; -import { npStart } from 'ui/new_platform'; export const SPATIAL_FILTER_TYPE = esFilters.FILTERS.SPATIAL_FILTER; export { SearchSource } from '../../../../../src/plugins/data/public'; -export const indexPatternService = npStart.plugins.data.indexPatterns; -export const autocompleteService = npStart.plugins.data.autocomplete; + +let indexPatternService; +export const setIndexPatternService = dataIndexPatterns => + (indexPatternService = dataIndexPatterns); +export const getIndexPatternService = () => indexPatternService; + +let autocompleteService; +export const setAutocompleteService = dataAutoComplete => (autocompleteService = dataAutoComplete); +export const getAutocompleteService = () => autocompleteService; let licenseId; export const setLicenseId = latestLicenseId => (licenseId = latestLicenseId); @@ -31,6 +37,31 @@ export const getFileUploadComponent = () => { return fileUploadPlugin.JsonUploadAndParse; }; +let getInjectedVar; +export const setInjectedVarFunc = getInjectedVarFunc => (getInjectedVar = getInjectedVarFunc); +export const getInjectedVarFunc = () => getInjectedVar; + +let uiSettings; +export const setUiSettings = coreUiSettings => (uiSettings = coreUiSettings); +export const getUiSettings = () => uiSettings; + +let indexPatternSelectComponent; +export const setIndexPatternSelect = indexPatternSelect => + (indexPatternSelectComponent = indexPatternSelect); +export const getIndexPatternSelectComponent = () => indexPatternSelectComponent; + +let coreHttp; +export const setHttp = http => (coreHttp = http); +export const getHttp = () => coreHttp; + +let dataTimeFilter; +export const setTimeFilter = timeFilter => (dataTimeFilter = timeFilter); +export const getTimeFilter = () => dataTimeFilter; + +let toast; +export const setToasts = notificationToast => (toast = notificationToast); +export const getToasts = () => toast; + export async function fetchSearchSourceAndRecordWithInspector({ searchSource, requestId, diff --git a/x-pack/legacy/plugins/maps/public/layers/joins/inner_join.test.js b/x-pack/legacy/plugins/maps/public/layers/joins/inner_join.test.js index 4a91ed3a3eafb..65c37860ffa18 100644 --- a/x-pack/legacy/plugins/maps/public/layers/joins/inner_join.test.js +++ b/x-pack/legacy/plugins/maps/public/layers/joins/inner_join.test.js @@ -7,7 +7,6 @@ import { InnerJoin } from './inner_join'; jest.mock('../../kibana_services', () => {}); -jest.mock('ui/timefilter', () => {}); jest.mock('../vector_layer', () => {}); const rightSource = { diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/ems_tms_source/ems_tms_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/ems_tms_source/ems_tms_source.js index 76ecc18f2f7d7..5a2124622694c 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/ems_tms_source/ems_tms_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/ems_tms_source/ems_tms_source.js @@ -5,7 +5,6 @@ */ import _ from 'lodash'; -import chrome from 'ui/chrome'; import React from 'react'; import { AbstractTMSSource } from '../tms_source'; import { VectorTileLayer } from '../../vector_tile_layer'; @@ -16,6 +15,7 @@ import { UpdateSourceEditor } from './update_source_editor'; import { i18n } from '@kbn/i18n'; import { getDataSourceLabel } from '../../../../common/i18n_getters'; import { EMS_TMS } from '../../../../common/constants'; +import { getInjectedVarFunc, getUiSettings } from '../../../kibana_services'; export class EMSTMSSource extends AbstractTMSSource { static type = EMS_TMS; @@ -152,8 +152,8 @@ export class EMSTMSSource extends AbstractTMSSource { return this._descriptor.id; } - const isDarkMode = chrome.getUiSettingsClient().get('theme:darkMode', false); - const emsTileLayerId = chrome.getInjected('emsTileLayerId'); + const isDarkMode = getUiSettings().get('theme:darkMode', false); + const emsTileLayerId = getInjectedVarFunc()('emsTileLayerId'); return isDarkMode ? emsTileLayerId.dark : emsTileLayerId.bright; } } diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/ems_unavailable_message.js b/x-pack/legacy/plugins/maps/public/layers/sources/ems_unavailable_message.js index 22b1088047539..bc50890a0f4a3 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/ems_unavailable_message.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/ems_unavailable_message.js @@ -4,11 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import chrome from 'ui/chrome'; +import { getInjectedVarFunc } from '../../kibana_services'; import { i18n } from '@kbn/i18n'; export function getEmsUnavailableMessage() { - const isEmsEnabled = chrome.getInjected('isEmsEnabled', true); + const isEmsEnabled = getInjectedVarFunc()('isEmsEnabled', true); if (isEmsEnabled) { return i18n.translate('xpack.maps.source.ems.noAccessDescription', { defaultMessage: diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/create_source_editor.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/create_source_editor.js index 00cbfbbb6c5a7..148683269ef78 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/create_source_editor.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_geo_grid_source/create_source_editor.js @@ -10,7 +10,7 @@ import PropTypes from 'prop-types'; import { SingleFieldSelect } from '../../../components/single_field_select'; import { RENDER_AS } from '../../../../common/constants'; -import { indexPatternService } from '../../../kibana_services'; +import { getIndexPatternService, getIndexPatternSelectComponent } from '../../../kibana_services'; import { NoIndexPatternCallout } from '../../../components/no_index_pattern_callout'; import { i18n } from '@kbn/i18n'; @@ -20,9 +20,6 @@ import { getAggregatableGeoFields, } from '../../../index_pattern_util'; -import { npStart } from 'ui/new_platform'; -const { IndexPatternSelect } = npStart.plugins.data.ui; - const requestTypeOptions = [ { label: i18n.translate('xpack.maps.source.esGeoGrid.gridRectangleDropdownOption', { @@ -92,7 +89,7 @@ export class CreateSourceEditor extends Component { let indexPattern; try { - indexPattern = await indexPatternService.get(indexPatternId); + indexPattern = await getIndexPatternService().get(indexPatternId); } catch (err) { // index pattern no longer exists return; @@ -205,6 +202,8 @@ export class CreateSourceEditor extends Component { } _renderIndexPatternSelect() { + const IndexPatternSelect = getIndexPatternSelectComponent(); + return ( { return ( @@ -81,8 +81,10 @@ export class CreateSourceEditor extends Component { }; loadIndexDocCount = async indexPatternTitle => { - const { count } = await kfetch({ - pathname: `../${GIS_API_PATH}/indexCount`, + const http = getHttp(); + const { count } = await http.fetch(`../${GIS_API_PATH}/indexCount`, { + method: 'GET', + credentials: 'same-origin', query: { index: indexPatternTitle, }, @@ -97,7 +99,7 @@ export class CreateSourceEditor extends Component { let indexPattern; try { - indexPattern = await indexPatternService.get(indexPatternId); + indexPattern = await getIndexPatternService().get(indexPatternId); } catch (err) { // index pattern no longer exists return; @@ -249,6 +251,8 @@ export class CreateSourceEditor extends Component { } render() { + const IndexPatternSelect = getIndexPatternSelectComponent(); + return ( {this._renderNoIndexPatternWarning()} diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/es_search_source.test.ts b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/es_search_source.test.ts index 59120e221ca49..2197e24aedb59 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/es_search_source.test.ts +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/es_search_source.test.ts @@ -4,6 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ jest.mock('ui/new_platform'); +jest.mock('../../../kibana_services'); import { ESSearchSource } from './es_search_source'; import { VectorLayer } from '../../vector_layer'; @@ -19,6 +20,11 @@ const descriptor: ESSearchSourceDescriptor = { }; describe('ES Search Source', () => { + beforeEach(() => { + require('../../../kibana_services').getUiSettings = () => ({ + get: jest.fn(), + }); + }); it('should create a vector layer', () => { const source = new ESSearchSource(descriptor, null); const layer = source.createDefaultLayer(); diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/load_index_settings.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/load_index_settings.js index 1a58b5b073b08..811291de26d35 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/load_index_settings.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/load_index_settings.js @@ -9,8 +9,7 @@ import { DEFAULT_MAX_INNER_RESULT_WINDOW, INDEX_SETTINGS_API_PATH, } from '../../../../common/constants'; -import { kfetch } from 'ui/kfetch'; -import { toastNotifications } from 'ui/notify'; +import { getHttp, getToasts } from '../../../kibana_services'; import { i18n } from '@kbn/i18n'; let toastDisplayed = false; @@ -27,9 +26,12 @@ export async function loadIndexSettings(indexPatternTitle) { } async function fetchIndexSettings(indexPatternTitle) { + const http = getHttp(); + const toasts = getToasts(); try { - const indexSettings = await kfetch({ - pathname: `../${INDEX_SETTINGS_API_PATH}`, + const indexSettings = await http.fetch(`../${INDEX_SETTINGS_API_PATH}`, { + method: 'GET', + credentials: 'same-origin', query: { indexPatternTitle, }, @@ -47,7 +49,7 @@ async function fetchIndexSettings(indexPatternTitle) { if (!toastDisplayed) { // Only show toast for first failure to avoid flooding user with warnings toastDisplayed = true; - toastNotifications.addWarning(warningMsg); + toasts.addWarning(warningMsg); } console.warn(warningMsg); return { diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/update_source_editor.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/update_source_editor.js index b85cca113cf98..4d1e32087ab8c 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/update_source_editor.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_search_source/update_source_editor.js @@ -19,7 +19,7 @@ import { import { SingleFieldSelect } from '../../../components/single_field_select'; import { TooltipSelector } from '../../../components/tooltip_selector'; -import { indexPatternService } from '../../../kibana_services'; +import { getIndexPatternService } from '../../../kibana_services'; import { i18n } from '@kbn/i18n'; import { getTermsFields, getSourceFields } from '../../../index_pattern_util'; import { ValidatedRange } from '../../../components/validated_range'; @@ -69,7 +69,7 @@ export class UpdateSourceEditor extends Component { async loadIndexSettings() { try { - const indexPattern = await indexPatternService.get(this.props.indexPatternId); + const indexPattern = await getIndexPatternService().get(this.props.indexPatternId); const { maxInnerResultWindow, maxResultWindow } = await loadIndexSettings(indexPattern.title); if (this._isMounted) { this.setState({ maxInnerResultWindow, maxResultWindow }); @@ -82,7 +82,7 @@ export class UpdateSourceEditor extends Component { async loadFields() { let indexPattern; try { - indexPattern = await indexPatternService.get(this.props.indexPatternId); + indexPattern = await getIndexPatternService().get(this.props.indexPatternId); } catch (err) { if (this._isMounted) { this.setState({ diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js index c5bf9a8be75bd..8b079b5202f7f 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js @@ -6,13 +6,13 @@ import { AbstractVectorSource } from './vector_source'; import { - autocompleteService, + getAutocompleteService, fetchSearchSourceAndRecordWithInspector, - indexPatternService, + getIndexPatternService, SearchSource, + getTimeFilter, } from '../../kibana_services'; import { createExtentFilter } from '../../elasticsearch_geo_utils'; -import { timefilter } from 'ui/timefilter'; import _ from 'lodash'; import { i18n } from '@kbn/i18n'; import uuid from 'uuid/v4'; @@ -125,7 +125,7 @@ export class AbstractESSource extends AbstractVectorSource { allFilters.push(createExtentFilter(buffer, geoField.name, geoField.type)); } if (isTimeAware) { - allFilters.push(timefilter.createFilter(indexPattern, searchFilters.timeFilters)); + allFilters.push(getTimeFilter().createFilter(indexPattern, searchFilters.timeFilters)); } const searchSource = new SearchSource(initialSearchContext); @@ -208,7 +208,7 @@ export class AbstractESSource extends AbstractVectorSource { } try { - this.indexPattern = await indexPatternService.get(this.getIndexPatternId()); + this.indexPattern = await getIndexPatternService().get(this.getIndexPatternId()); return this.indexPattern; } catch (error) { throw new Error( @@ -305,7 +305,7 @@ export class AbstractESSource extends AbstractVectorSource { } if (style.isTimeAware() && (await this.isTimeAware())) { searchSource.setField('filter', [ - timefilter.createFilter(indexPattern, searchFilters.timeFilters), + getTimeFilter().createFilter(indexPattern, searchFilters.timeFilters), ]); } @@ -332,7 +332,7 @@ export class AbstractESSource extends AbstractVectorSource { getValueSuggestions = async (field, query) => { try { const indexPattern = await this.getIndexPattern(); - return await autocompleteService.getValueSuggestions({ + return await getAutocompleteService().getValueSuggestions({ indexPattern, field: indexPattern.fields.getByName(field.getRootName()), query, diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.test.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.test.js index 890b1e3aaac1f..14ffd068df465 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.test.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_term_source.test.js @@ -8,7 +8,6 @@ import { ESTermSource, extractPropertiesMap } from './es_term_source'; jest.mock('ui/new_platform'); jest.mock('../vector_layer', () => {}); -jest.mock('ui/timefilter', () => {}); const indexPatternTitle = 'myIndex'; const termFieldName = 'myTermField'; diff --git a/x-pack/legacy/plugins/maps/public/layers/styles/color_utils.js b/x-pack/legacy/plugins/maps/public/layers/styles/color_utils.js index fc305f8daed59..a619eaba21aef 100644 --- a/x-pack/legacy/plugins/maps/public/layers/styles/color_utils.js +++ b/x-pack/legacy/plugins/maps/public/layers/styles/color_utils.js @@ -7,11 +7,7 @@ import React from 'react'; import tinycolor from 'tinycolor2'; import chroma from 'chroma-js'; - import { euiPaletteColorBlind } from '@elastic/eui/lib/services'; - -import { getLegendColors, getColor } from 'ui/vis/map/color_util'; - import { ColorGradient } from './components/color_gradient'; import { COLOR_PALETTE_MAX_SIZE } from '../../../common/constants'; import { vislibColorMaps } from '../../../../../../../src/plugins/charts/public'; @@ -30,6 +26,24 @@ export const DEFAULT_LINE_COLORS = [ '#FFF', ]; +function getLegendColors(colorRamp, numLegendColors = 4) { + const colors = []; + colors[0] = getColor(colorRamp, 0); + for (let i = 1; i < numLegendColors - 1; i++) { + colors[i] = getColor(colorRamp, Math.floor((colorRamp.length * i) / numLegendColors)); + } + colors[numLegendColors - 1] = getColor(colorRamp, colorRamp.length - 1); + return colors; +} + +function getColor(colorRamp, i) { + const color = colorRamp[i][1]; + const red = Math.floor(color[0] * 255); + const green = Math.floor(color[1] * 255); + const blue = Math.floor(color[2] * 255); + return `rgb(${red},${green},${blue})`; +} + function getColorRamp(colorRampName) { const colorRamp = vislibColorMaps[colorRampName]; if (!colorRamp) { diff --git a/x-pack/legacy/plugins/maps/public/layers/styles/vector/components/symbol/vector_style_icon_editor.js b/x-pack/legacy/plugins/maps/public/layers/styles/vector/components/symbol/vector_style_icon_editor.js index d5ec09f515954..36b6c1a76470c 100644 --- a/x-pack/legacy/plugins/maps/public/layers/styles/vector/components/symbol/vector_style_icon_editor.js +++ b/x-pack/legacy/plugins/maps/public/layers/styles/vector/components/symbol/vector_style_icon_editor.js @@ -6,7 +6,7 @@ import React from 'react'; -import chrome from 'ui/chrome'; +import { getUiSettings } from '../../../../../kibana_services'; import { StylePropEditor } from '../style_prop_editor'; import { DynamicIconForm } from './dynamic_icon_form'; import { StaticIconForm } from './static_icon_form'; @@ -16,13 +16,13 @@ export function VectorStyleIconEditor(props) { const iconForm = props.styleProperty.isDynamic() ? ( ) : ( ); diff --git a/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.test.js b/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.test.js index 66b7ae5e02c5f..b3f653a70f472 100644 --- a/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.test.js +++ b/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style.test.js @@ -9,6 +9,7 @@ import { DataRequest } from '../../util/data_request'; import { VECTOR_SHAPE_TYPES } from '../../sources/vector_feature_types'; import { FIELD_ORIGIN } from '../../../../common/constants'; +jest.mock('../../../kibana_services'); jest.mock('ui/new_platform'); class MockField { @@ -65,7 +66,13 @@ describe('getDescriptorWithMissingStylePropsRemoved', () => { }, }; - it('Should return no changes when next oridinal fields contain existing style property fields', () => { + beforeEach(() => { + require('../../../kibana_services').getUiSettings = () => ({ + get: jest.fn(), + }); + }); + + it('Should return no changes when next ordinal fields contain existing style property fields', () => { const vectorStyle = new VectorStyle({ properties }, new MockSource()); const nextFields = [new MockField({ fieldName })]; @@ -73,7 +80,7 @@ describe('getDescriptorWithMissingStylePropsRemoved', () => { expect(hasChanges).toBe(false); }); - it('Should clear missing fields when next oridinal fields do not contain existing style property fields', () => { + it('Should clear missing fields when next ordinal fields do not contain existing style property fields', () => { const vectorStyle = new VectorStyle({ properties }, new MockSource()); const nextFields = []; diff --git a/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style_defaults.js b/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style_defaults.js index dd2cf79318d8e..fdfd71d240989 100644 --- a/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style_defaults.js +++ b/x-pack/legacy/plugins/maps/public/layers/styles/vector/vector_style_defaults.js @@ -12,7 +12,7 @@ import { DEFAULT_FILL_COLORS, DEFAULT_LINE_COLORS, } from '../color_utils'; -import chrome from 'ui/chrome'; +import { getUiSettings } from '../../../kibana_services'; export const MIN_SIZE = 1; export const MAX_SIZE = 64; @@ -67,7 +67,7 @@ export function getDefaultStaticProperties(mapColors = []) { const nextFillColor = DEFAULT_FILL_COLORS[nextColorIndex]; const nextLineColor = DEFAULT_LINE_COLORS[nextColorIndex]; - const isDarkMode = chrome.getUiSettingsClient().get('theme:darkMode', false); + const isDarkMode = getUiSettings().get('theme:darkMode', false); return { [VECTOR_STYLES.ICON]: { diff --git a/x-pack/legacy/plugins/maps/public/legacy.ts b/x-pack/legacy/plugins/maps/public/legacy.ts index 6adab529daf86..96d9e09c1d09a 100644 --- a/x-pack/legacy/plugins/maps/public/legacy.ts +++ b/x-pack/legacy/plugins/maps/public/legacy.ts @@ -19,9 +19,5 @@ const setupPlugins = { np: npSetup.plugins, }; -const startPlugins = { - np: npStart.plugins, -}; - export const setup = pluginInstance.setup(npSetup.core, setupPlugins); -export const start = pluginInstance.start(npStart.core, startPlugins); +export const start = pluginInstance.start(npStart.core, npStart.plugins); diff --git a/x-pack/legacy/plugins/maps/public/plugin.ts b/x-pack/legacy/plugins/maps/public/plugin.ts index e2d1d43295646..1f8f83e44a769 100644 --- a/x-pack/legacy/plugins/maps/public/plugin.ts +++ b/x-pack/legacy/plugins/maps/public/plugin.ts @@ -8,14 +8,33 @@ import { Plugin, CoreStart, CoreSetup } from 'src/core/public'; // @ts-ignore import { wrapInI18nContext } from 'ui/i18n'; // @ts-ignore -import { MapListing } from './components/map_listing'; +import { Start as InspectorStartContract } from 'src/plugins/inspector/public'; // @ts-ignore -import { setInjectedVarFunc } from '../../../../plugins/maps/public/kibana_services'; // eslint-disable-line @kbn/eslint/no-restricted-paths +import { MapListing } from './components/map_listing'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { + setLicenseId, + setInspector, + setFileUpload, + setIndexPatternSelect, + setHttp, + setTimeFilter, + setUiSettings, + setInjectedVarFunc, + setToasts, + setIndexPatternService, + setAutocompleteService, + // @ts-ignore +} from './kibana_services'; // @ts-ignore -import { setLicenseId, setInspector, setFileUpload } from './kibana_services'; +import { setInjectedVarFunc as npSetInjectedVarFunc } from '../../../../plugins/maps/public/kibana_services'; // eslint-disable-line @kbn/eslint/no-restricted-paths import { HomePublicPluginSetup } from '../../../../../src/plugins/home/public'; import { LicensingPluginSetup } from '../../../../plugins/licensing/public'; import { featureCatalogueEntry } from './feature_catalogue_entry'; +import { + DataPublicPluginSetup, + DataPublicPluginStart, +} from '../../../../../src/plugins/data/public'; /** * These are the interfaces with your public contracts. You should export these @@ -30,16 +49,38 @@ interface MapsPluginSetupDependencies { np: { licensing?: LicensingPluginSetup; home: HomePublicPluginSetup; + data: DataPublicPluginSetup; }; } +interface MapsPluginStartDependencies { + data: DataPublicPluginStart; + inspector: InspectorStartContract; + // file_upload TODO: Export type from file upload and use here +} + export const bindSetupCoreAndPlugins = (core: CoreSetup, plugins: any) => { const { licensing } = plugins; - const { injectedMetadata } = core; + const { injectedMetadata, http } = core; if (licensing) { licensing.license$.subscribe(({ uid }: { uid: string }) => setLicenseId(uid)); } setInjectedVarFunc(injectedMetadata.getInjectedVar); + setHttp(http); + setUiSettings(core.uiSettings); + setInjectedVarFunc(core.injectedMetadata.getInjectedVar); + npSetInjectedVarFunc(core.injectedMetadata.getInjectedVar); + setToasts(core.notifications.toasts); +}; + +export const bindStartCoreAndPlugins = (core: CoreStart, plugins: any) => { + const { file_upload, data, inspector } = plugins; + setInspector(inspector); + setFileUpload(file_upload); + setIndexPatternSelect(data.ui.IndexPatternSelect); + setTimeFilter(data.query.timefilter.timefilter); + setIndexPatternService(data.indexPatterns); + setAutocompleteService(data.autocompleteService); }; /** @internal */ @@ -56,9 +97,7 @@ export class MapsPlugin implements Plugin { np.home.featureCatalogue.register(featureCatalogueEntry); } - public start(core: CoreStart, plugins: any) { - const { inspector, file_upload } = plugins.np; - setInspector(inspector); - setFileUpload(file_upload); + public start(core: CoreStart, plugins: MapsPluginStartDependencies) { + bindStartCoreAndPlugins(core, plugins); } } diff --git a/x-pack/legacy/plugins/maps/public/selectors/map_selectors.js b/x-pack/legacy/plugins/maps/public/selectors/map_selectors.js index 79d890bc21f14..61eea2d172ae4 100644 --- a/x-pack/legacy/plugins/maps/public/selectors/map_selectors.js +++ b/x-pack/legacy/plugins/maps/public/selectors/map_selectors.js @@ -12,7 +12,7 @@ import { VectorLayer } from '../layers/vector_layer'; import { HeatmapLayer } from '../layers/heatmap_layer'; import { BlendedVectorLayer } from '../layers/blended_vector_layer'; import { ALL_SOURCES } from '../layers/sources/all_sources'; -import { timefilter } from 'ui/timefilter'; +import { getTimeFilter } from '../kibana_services'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { getInspectorAdapters } from '../../../../../plugins/maps/public/reducers/non_serializable_instances'; import { @@ -109,7 +109,7 @@ export const getMapCenter = ({ map }) => export const getMouseCoordinates = ({ map }) => map.mapState.mouseCoordinates; export const getTimeFilters = ({ map }) => - map.mapState.timeFilters ? map.mapState.timeFilters : timefilter.getTime(); + map.mapState.timeFilters ? map.mapState.timeFilters : getTimeFilter().getTime(); export const getQuery = ({ map }) => map.mapState.query; @@ -132,7 +132,7 @@ export const getRefreshConfig = ({ map }) => { return map.mapState.refreshConfig; } - const refreshInterval = timefilter.getRefreshInterval(); + const refreshInterval = getTimeFilter().getRefreshInterval(); return { isPaused: refreshInterval.pause, interval: refreshInterval.value, diff --git a/x-pack/legacy/plugins/maps/public/selectors/map_selectors.test.js b/x-pack/legacy/plugins/maps/public/selectors/map_selectors.test.js index ef2e23e51a092..e7f071d5729c6 100644 --- a/x-pack/legacy/plugins/maps/public/selectors/map_selectors.test.js +++ b/x-pack/legacy/plugins/maps/public/selectors/map_selectors.test.js @@ -15,15 +15,15 @@ jest.mock('../../../../../plugins/maps/public/reducers/non_serializable_instance return {}; }, })); -jest.mock('ui/timefilter', () => ({ - timefilter: { +jest.mock('../kibana_services', () => ({ + getTimeFilter: () => ({ getTime: () => { return { to: 'now', from: 'now-15m', }; }, - }, + }), })); import { getTimeFilters } from './map_selectors';