Skip to content

Commit

Permalink
[7.x] [Maps] Update remaining client dependencies to NP (#6329… (#63687)
Browse files Browse the repository at this point in the history
* Routes dependencies updated (except for routes itself)

* Replace all chrome deps except map controller

* Replace npSetup and npStart with normal plugin init logic. Some clean up

* Bind kibana services in map controller

* Use kibana services in map controller

* Convert remaining deps that aren't critical to legacy operation

* Remove last angular bindings to gis map saved object loader

* Consolidate kibana services in NP

* Some fixes. Remove console logs

* Fix type errors

* Fix jest test path refs

* Accomodate legacy 'hacks' and init services for vis type alias

* Review feedback. Remove/update unused declarations

* getFileUpload actually just needed Component tacked on the end

* Handle visibility of toolbars for full screen mode using new core chrome. Should fix test

* Import source types in getInitialLayers to ensure registry
  • Loading branch information
Aaron Caldwell authored Apr 16, 2020
1 parent e13f0c6 commit db6c593
Show file tree
Hide file tree
Showing 33 changed files with 328 additions and 202 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

jest.mock('../selectors/map_selectors', () => ({}));
jest.mock('../kibana_services', () => ({}));
jest.mock('../../../../../plugins/maps/public/kibana_services', () => ({}));

import { mapExtentChanged, setMouseCoordinates } from './map_actions';

Expand Down
19 changes: 18 additions & 1 deletion x-pack/legacy/plugins/maps/public/angular/get_initial_layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@
* you may not use this file except in compliance with the Elastic License.
*/
import _ from 'lodash';
// Import each layer type, even those not used, to init in registry
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import '../../../../../plugins/maps/public/layers/sources/wms_source';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import '../../../../../plugins/maps/public/layers/sources/ems_file_source';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import '../../../../../plugins/maps/public/layers/sources/es_search_source';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import '../../../../../plugins/maps/public/layers/sources/es_pew_pew_source/es_pew_pew_source';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import '../../../../../plugins/maps/public/layers/sources/kibana_regionmap_source';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import '../../../../../plugins/maps/public/layers/sources/es_geo_grid_source';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import '../../../../../plugins/maps/public/layers/sources/xyz_tms_source';

// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { KibanaTilemapSource } from '../../../../../plugins/maps/public/layers/sources/kibana_tilemap_source';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { EMSTMSSource } from '../../../../../plugins/maps/public/layers/sources/ems_tms_source';
import { getInjectedVarFunc } from '../kibana_services';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { getInjectedVarFunc } from '../../../../../plugins/maps/public/kibana_services';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { getKibanaTileMap } from '../../../../../plugins/maps/public/meta';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
jest.mock('../../../../../plugins/maps/public/meta', () => {
return {};
});
jest.mock('../kibana_services');
jest.mock('../../../../../plugins/maps/public/kibana_services');

import { getInitialLayers } from './get_initial_layers';

const layerListNotProvided = undefined;

describe('Saved object has layer list', () => {
beforeEach(() => {
require('../kibana_services').getInjectedVarFunc = () => jest.fn();
require('../../../../../plugins/maps/public/kibana_services').getInjectedVarFunc = () =>
jest.fn();
});

it('Should get initial layers from saved object', () => {
Expand Down Expand Up @@ -65,7 +66,7 @@ describe('EMS is enabled', () => {
require('../../../../../plugins/maps/public/meta').getKibanaTileMap = () => {
return null;
};
require('../kibana_services').getInjectedVarFunc = () => key => {
require('../../../../../plugins/maps/public/kibana_services').getInjectedVarFunc = () => key => {
switch (key) {
case 'emsTileLayerId':
return {
Expand Down Expand Up @@ -110,7 +111,7 @@ describe('EMS is not enabled', () => {
return null;
};

require('../kibana_services').getInjectedVarFunc = () => key => {
require('../../../../../plugins/maps/public/kibana_services').getInjectedVarFunc = () => key => {
switch (key) {
case 'isEmsEnabled':
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import chrome from 'ui/chrome';

const settings = chrome.getUiSettingsClient();
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { getUiSettings } from '../../../../../plugins/maps/public/kibana_services';

export function getInitialQuery({ mapStateJSON, appState = {}, userQueryLanguage }) {
const settings = getUiSettings();

if (appState.query) {
return appState.query;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import chrome from 'ui/chrome';

const uiSettings = chrome.getUiSettingsClient();
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { getUiSettings } from '../../../../../plugins/maps/public/kibana_services';

export function getInitialRefreshConfig({ mapStateJSON, globalState = {} }) {
const uiSettings = getUiSettings();

if (mapStateJSON) {
const mapState = JSON.parse(mapStateJSON);
if (mapState.refreshConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import chrome from 'ui/chrome';

const uiSettings = chrome.getUiSettingsClient();
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { getUiSettings } from '../../../../../plugins/maps/public/kibana_services';

export function getInitialTimeFilters({ mapStateJSON, globalState = {} }) {
if (mapStateJSON) {
Expand All @@ -15,6 +14,6 @@ export function getInitialTimeFilters({ mapStateJSON, globalState = {} }) {
}
}

const defaultTime = uiSettings.get('timepicker:timeDefaults');
const defaultTime = getUiSettings().get('timepicker:timeDefaults');
return { ...defaultTime, ...globalState.time };
}
6 changes: 3 additions & 3 deletions x-pack/legacy/plugins/maps/public/angular/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<div id="maps-top-nav">
<div>
<kbn-top-nav
ng-show="chrome.getVisible()"
ng-show="isVisible"
app-name="'maps'"
config="topNavMenu"
show-search-bar="chrome.getVisible()"
show-filter-bar="chrome.getVisible()"
show-search-bar="isVisible"
show-filter-bar="isVisible"
show-date-picker="showDatePicker"
show-save-query="showSaveQuery"
query="query"
Expand Down
71 changes: 48 additions & 23 deletions x-pack/legacy/plugins/maps/public/angular/map_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,29 @@
*/

import _ from 'lodash';
import chrome from 'ui/chrome';
import rison from 'rison-node';
import 'ui/directives/listen';
import 'ui/directives/storage';
import React from 'react';
import { I18nProvider } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';
import { capabilities } from 'ui/capabilities';
import { render, unmountComponentAtNode } from 'react-dom';
import { uiModules } from 'ui/modules';
import { getTimeFilter, getIndexPatternService, getInspector } from '../kibana_services';
import { Provider } from 'react-redux';
import {
getTimeFilter,
getIndexPatternService,
getInspector,
getNavigation,
getData,
getCoreI18n,
getCoreChrome,
getMapsCapabilities,
getToasts,
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
} from '../../../../../plugins/maps/public/kibana_services';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { createMapStore } from '../../../../../plugins/maps/public/reducers/store';
import { Provider } from 'react-redux';
import { GisMap } from '../connected_components/gis_map';
import { addHelpMenuToAppChrome } from '../help_menu_util';
import {
Expand Down Expand Up @@ -51,29 +60,33 @@ import {
} from '../selectors/map_selectors';
// 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 { toastNotifications } from 'ui/notify';
import { getInitialLayers } from './get_initial_layers';
import { getInitialQuery } from './get_initial_query';
import { getInitialTimeFilters } from './get_initial_time_filters';
import { getInitialRefreshConfig } from './get_initial_refresh_config';
import { MAP_SAVED_OBJECT_TYPE, MAP_APP_PATH } from '../../common/constants';
import { npStart } from 'ui/new_platform';
import { npSetup, npStart } from 'ui/new_platform';
import { esFilters } from '../../../../../../src/plugins/data/public';
import {
SavedObjectSaveModal,
showSaveModal,
} from '../../../../../../src/plugins/saved_objects/public';
import { loadKbnTopNavDirectives } from '../../../../../../src/plugins/kibana_legacy/public';
loadKbnTopNavDirectives(npStart.plugins.navigation.ui);

const savedQueryService = npStart.plugins.data.query.savedQueries;
import {
bindSetupCoreAndPlugins as bindNpSetupCoreAndPlugins,
bindStartCoreAndPlugins as bindNpStartCoreAndPlugins,
} from '../../../../../plugins/maps/public/plugin'; // eslint-disable-line @kbn/eslint/no-restricted-paths

const REACT_ANCHOR_DOM_ELEMENT_ID = 'react-maps-root';

const app = uiModules.get(MAP_APP_PATH, []);

// Init required services. Necessary while in legacy
bindNpSetupCoreAndPlugins(npSetup.core, npSetup.plugins);
bindNpStartCoreAndPlugins(npStart.core, npStart.plugins);

loadKbnTopNavDirectives(getNavigation().ui);

function getInitialLayersFromUrlParam() {
const locationSplit = window.location.href.split('?');
if (locationSplit.length <= 1) {
Expand All @@ -87,7 +100,7 @@ function getInitialLayersFromUrlParam() {
try {
return rison.decode_array(mapAppParams.get('initialLayers'));
} catch (e) {
toastNotifications.addWarning({
getToasts().addWarning({
title: i18n.translate('xpack.maps.initialLayers.unableToParseTitle', {
defaultMessage: `Inital layers not added to map`,
}),
Expand All @@ -103,7 +116,8 @@ function getInitialLayersFromUrlParam() {
app.controller(
'GisMapController',
($scope, $route, kbnUrl, localStorage, AppState, globalState) => {
const { filterManager } = npStart.plugins.data.query;
const savedQueryService = getData().query.savedQueries;
const { filterManager } = getData().query;
const savedMap = $route.current.locals.map;
$scope.screenTitle = savedMap.title;
let unsubscribe;
Expand All @@ -115,6 +129,14 @@ app.controller(
return _.get($state, 'filters', []);
}

const visibleSubscription = getCoreChrome()
.getIsVisible$()
.subscribe(isVisible => {
$scope.$evalAsync(() => {
$scope.isVisible = isVisible;
});
});

$scope.$listen(globalState, 'fetch_with_changes', diff => {
if (diff.includes('time') || diff.includes('filters')) {
onQueryChange({
Expand Down Expand Up @@ -169,10 +191,10 @@ app.controller(
});

/* Saved Queries */
$scope.showSaveQuery = capabilities.get().maps.saveQuery;
$scope.showSaveQuery = getMapsCapabilities().saveQuery;

$scope.$watch(
() => capabilities.get().maps.saveQuery,
() => getMapsCapabilities().saveQuery,
newCapability => {
$scope.showSaveQuery = newCapability;
}
Expand Down Expand Up @@ -342,7 +364,7 @@ app.controller(
// clear old UI state
store.dispatch(setSelectedLayer(null));
store.dispatch(updateFlyout(FLYOUT_STATE.NONE));
store.dispatch(setReadOnly(!capabilities.get().maps.save));
store.dispatch(setReadOnly(!getMapsCapabilities().save));

handleStoreChanges(store);
unsubscribe = store.subscribe(() => {
Expand Down Expand Up @@ -446,6 +468,8 @@ app.controller(

$scope.$on('$destroy', () => {
window.removeEventListener('beforeunload', beforeUnload);
visibleSubscription.unsubscribe();
getCoreChrome().setIsVisible(true);

if (unsubscribe) {
unsubscribe();
Expand All @@ -457,7 +481,7 @@ app.controller(
});

const updateBreadcrumbs = () => {
chrome.breadcrumbs.set([
getCoreChrome().setBreadcrumbs([
{
text: i18n.translate('xpack.maps.mapController.mapsBreadcrumbLabel', {
defaultMessage: 'Maps',
Expand All @@ -482,7 +506,7 @@ app.controller(
};
updateBreadcrumbs();

addHelpMenuToAppChrome(chrome);
addHelpMenuToAppChrome();

async function doSave(saveOptions) {
await store.dispatch(clearTransientLayerStateAndCloseFlyout());
Expand All @@ -491,9 +515,9 @@ app.controller(

try {
id = await savedMap.save(saveOptions);
docTitle.change(savedMap.title);
getCoreChrome().docTitle.change(savedMap.title);
} catch (err) {
toastNotifications.addDanger({
getToasts().addDanger({
title: i18n.translate('xpack.maps.mapController.saveErrorMessage', {
defaultMessage: `Error on saving '{title}'`,
values: { title: savedMap.title },
Expand All @@ -505,7 +529,7 @@ app.controller(
}

if (id) {
toastNotifications.addSuccess({
getToasts().addSuccess({
title: i18n.translate('xpack.maps.mapController.saveSuccessMessage', {
defaultMessage: `Saved '{title}'`,
values: { title: savedMap.title },
Expand Down Expand Up @@ -539,6 +563,7 @@ app.controller(
}),
testId: 'mapsFullScreenMode',
run() {
getCoreChrome().setIsVisible(false);
store.dispatch(enableFullScreen());
},
},
Expand All @@ -556,7 +581,7 @@ app.controller(
getInspector().open(inspectorAdapters, {});
},
},
...(capabilities.get().maps.save
...(getMapsCapabilities().save
? [
{
id: 'save',
Expand Down Expand Up @@ -611,7 +636,7 @@ app.controller(
showDescription={false}
/>
);
showSaveModal(saveModal, npStart.core.i18n.Context);
showSaveModal(saveModal, getCoreI18n().Context);
},
},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@
* you may not use this file except in compliance with the Elastic License.
*/

import _ from 'lodash';
import { createSavedGisMapClass } from './saved_gis_map';
import { uiModules } from 'ui/modules';
import { SavedObjectLoader } from '../../../../../../../src/plugins/saved_objects/public';
import { npStart } from '../../../../../../../src/legacy/ui/public/new_platform';
import {
getCoreChrome,
getSavedObjectsClient,
getIndexPatternService,
getCoreOverlays,
getData,
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
} from '../../../../../../plugins/maps/public/kibana_services';

const module = uiModules.get('app/maps');

// This is the only thing that gets injected into controllers
module.service('gisMapSavedObjectLoader', function() {
const savedObjectsClient = npStart.core.savedObjects.client;
export const getMapsSavedObjectLoader = _.once(function() {
const services = {
savedObjectsClient,
indexPatterns: npStart.plugins.data.indexPatterns,
search: npStart.plugins.data.search,
chrome: npStart.core.chrome,
overlays: npStart.core.overlays,
savedObjectsClient: getSavedObjectsClient(),
indexPatterns: getIndexPatternService(),
search: getData().search,
chrome: getCoreChrome(),
overlays: getCoreOverlays(),
};
const SavedGisMap = createSavedGisMapClass(services);

return new SavedObjectLoader(SavedGisMap, npStart.core.savedObjects.client, npStart.core.chrome);
return new SavedObjectLoader(SavedGisMap, getSavedObjectsClient(), getCoreChrome());
});
Loading

0 comments on commit db6c593

Please sign in to comment.