diff --git a/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts b/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts index 604575a6e6220..4c24f8435e4c2 100644 --- a/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts +++ b/src/legacy/core_plugins/kibana/public/management/saved_object_registry.ts @@ -17,64 +17,45 @@ * under the License. */ -import _ from 'lodash'; import { i18n } from '@kbn/i18n'; -import { npStart } from 'ui/new_platform'; -import { SavedObjectLoader } from '../../../../../plugins/saved_objects/public'; +import { npSetup, npStart } from 'ui/new_platform'; import { createSavedDashboardLoader } from '../dashboard'; import { TypesService, createSavedVisLoader } from '../../../visualizations/public'; import { createSavedSearchesLoader } from '../../../../../plugins/discover/public'; -/** - * This registry is used for the editing mode of Saved Searches, Visualizations, - * Dashboard and Time Lion saved objects. - */ -interface SavedObjectRegistryEntry { - id: string; - service: SavedObjectLoader; - title: string; +const registry = npSetup.plugins.savedObjectsManagement?.serviceRegistry; + +// TODO: should no longer be needed after migration +export const savedObjectManagementRegistry = registry!; + +if (registry) { + const services = { + savedObjectsClient: npStart.core.savedObjects.client, + indexPatterns: npStart.plugins.data.indexPatterns, + chrome: npStart.core.chrome, + overlays: npStart.core.overlays, + }; + + savedObjectManagementRegistry.register({ + id: 'savedVisualizations', + service: createSavedVisLoader({ + ...services, + ...{ visualizationTypes: new TypesService().start() }, + }), + title: 'visualizations', + }); + + savedObjectManagementRegistry.register({ + id: 'savedDashboards', + service: createSavedDashboardLoader(services), + title: i18n.translate('kbn.dashboard.savedDashboardsTitle', { + defaultMessage: 'dashboards', + }), + }); + + savedObjectManagementRegistry.register({ + id: 'savedSearches', + service: createSavedSearchesLoader(services), + title: 'searches', + }); } - -const registry: SavedObjectRegistryEntry[] = []; - -export const savedObjectManagementRegistry = { - register: (service: SavedObjectRegistryEntry) => { - registry.push(service); - }, - all: () => { - return registry; - }, - get: (id: string) => { - return _.find(registry, { id }); - }, -}; - -const services = { - savedObjectsClient: npStart.core.savedObjects.client, - indexPatterns: npStart.plugins.data.indexPatterns, - chrome: npStart.core.chrome, - overlays: npStart.core.overlays, -}; - -savedObjectManagementRegistry.register({ - id: 'savedVisualizations', - service: createSavedVisLoader({ - ...services, - ...{ visualizationTypes: new TypesService().start() }, - }), - title: 'visualizations', -}); - -savedObjectManagementRegistry.register({ - id: 'savedDashboards', - service: createSavedDashboardLoader(services), - title: i18n.translate('kbn.dashboard.savedDashboardsTitle', { - defaultMessage: 'dashboards', - }), -}); - -savedObjectManagementRegistry.register({ - id: 'savedSearches', - service: createSavedSearchesLoader(services), - title: 'searches', -}); diff --git a/src/legacy/ui/public/new_platform/new_platform.ts b/src/legacy/ui/public/new_platform/new_platform.ts index 00d76bc341322..540c94329608f 100644 --- a/src/legacy/ui/public/new_platform/new_platform.ts +++ b/src/legacy/ui/public/new_platform/new_platform.ts @@ -52,6 +52,7 @@ import { NavigationPublicPluginStart, } from '../../../../plugins/navigation/public'; import { VisTypeVegaSetup } from '../../../../plugins/vis_type_vega/public'; +import { SavedObjectsManagementPluginSetup } from '../../../../plugins/so_management/public'; export interface PluginsSetup { bfetch: BfetchPublicSetup; @@ -71,6 +72,7 @@ export interface PluginsSetup { management: ManagementSetup; visTypeVega: VisTypeVegaSetup; telemetry?: TelemetryPluginSetup; + savedObjectsManagement?: SavedObjectsManagementPluginSetup; } export interface PluginsStart { diff --git a/src/plugins/so_management/public/index.ts b/src/plugins/so_management/public/index.ts index 36088d1979f0d..13dc11e48e571 100644 --- a/src/plugins/so_management/public/index.ts +++ b/src/plugins/so_management/public/index.ts @@ -20,6 +20,9 @@ import { PluginInitializerContext } from 'kibana/public'; import { SavedObjectsManagementPlugin } from './plugin'; +export { SavedObjectsManagementPluginSetup } from './types'; +export { ISavedObjectsManagementRegistry } from './management_registry'; + export function plugin(initializerContext: PluginInitializerContext) { return new SavedObjectsManagementPlugin(); } diff --git a/src/plugins/so_management/public/management/index.tsx b/src/plugins/so_management/public/management/index.tsx index bb19ae474a673..4e6b585d74382 100644 --- a/src/plugins/so_management/public/management/index.tsx +++ b/src/plugins/so_management/public/management/index.tsx @@ -27,19 +27,21 @@ import { CoreSetup, CoreStart } from 'src/core/public'; import { ManagementSetup } from '../../../management/public'; import { DataPublicPluginStart } from '../../../data/public'; import { StartDependencies } from '../plugin'; +import { ISavedObjectsManagementRegistry } from '../management_registry'; import { SavedObjectsTable } from './saved_objects_table'; import { getAllowedTypes } from './lib'; interface RegisterOptions { core: CoreSetup; sections: ManagementSetup['sections']; + serviceRegistry: ISavedObjectsManagementRegistry; } const title = i18n.translate('kbn.management.objects.savedObjectsSectionLabel', { defaultMessage: 'Saved Objects XXX', }); -export const registerManagementSection = ({ core, sections }: RegisterOptions) => { +export const registerManagementSection = ({ core, sections, serviceRegistry }: RegisterOptions) => { const kibanaSection = sections.getSection('kibana'); if (!kibanaSection) { throw new Error('`kibana` management section not found.'); @@ -60,6 +62,7 @@ export const registerManagementSection = ({ core, sections }: RegisterOptions) = @@ -80,16 +83,19 @@ const SavedObjectsTablePage = ({ coreStart, dataStart, allowedTypes, + serviceRegistry, }: { coreStart: CoreStart; dataStart: DataPublicPluginStart; allowedTypes: string[]; + serviceRegistry: ISavedObjectsManagementRegistry; }) => { const capabilities = coreStart.application.capabilities; const itemsPerPage = coreStart.uiSettings.get('savedObjects:perPage', 50); return ( ; + +export class SavedObjectsManagementRegistry { + private readonly registry = new Map(); + + public register(service: SavedObjectsManagementRegistryEntry) { + if (this.registry.has(service.id)) { + throw new Error(''); + } + this.registry.set(service.id, service); + } + + public all(): SavedObjectsManagementRegistryEntry[] { + return Object.values(this.registry); + } + + public get(id: string): SavedObjectsManagementRegistryEntry | undefined { + return this.registry.get(id); + } +} diff --git a/src/plugins/so_management/public/plugin.ts b/src/plugins/so_management/public/plugin.ts index 27de865f62a2f..369fa5d5398f7 100644 --- a/src/plugins/so_management/public/plugin.ts +++ b/src/plugins/so_management/public/plugin.ts @@ -21,6 +21,8 @@ import { CoreSetup, CoreStart, Plugin } from 'src/core/public'; import { ManagementSetup } from '../../management/public'; import { DataPublicPluginStart } from '../../data/public'; import { registerManagementSection } from './management'; +import { SavedObjectsManagementPluginSetup } from './types'; +import { SavedObjectsManagementRegistry } from './management_registry'; export interface SetupDependencies { management: ManagementSetup; @@ -31,14 +33,22 @@ export interface StartDependencies { } export class SavedObjectsManagementPlugin - implements Plugin<{}, {}, SetupDependencies, StartDependencies> { - public setup(core: CoreSetup, { management }: SetupDependencies) { + implements Plugin { + private readonly serviceRegistry = new SavedObjectsManagementRegistry(); + + public setup( + core: CoreSetup, + { management }: SetupDependencies + ): SavedObjectsManagementPluginSetup { registerManagementSection({ core, + serviceRegistry: this.serviceRegistry, sections: management.sections, }); - return {}; + return { + serviceRegistry: this.serviceRegistry, + }; } public start(core: CoreStart) { diff --git a/src/plugins/so_management/public/types.ts b/src/plugins/so_management/public/types.ts index 2d538caaae67b..034cd300ce3d1 100644 --- a/src/plugins/so_management/public/types.ts +++ b/src/plugins/so_management/public/types.ts @@ -18,6 +18,11 @@ */ import { SavedObject } from 'src/core/public'; +import { ISavedObjectsManagementRegistry } from './management_registry'; + +export interface SavedObjectsManagementPluginSetup { + serviceRegistry: ISavedObjectsManagementRegistry; +} export type SavedObjectWithMetadata = SavedObject & { meta: { diff --git a/src/plugins/so_management/server/services/management.mock.ts b/src/plugins/so_management/server/services/management.mock.ts index 2099cc0f77bcc..080f48de11cc5 100644 --- a/src/plugins/so_management/server/services/management.mock.ts +++ b/src/plugins/so_management/server/services/management.mock.ts @@ -22,13 +22,18 @@ import { SavedObjectsManagement } from './management'; type Management = PublicMethodsOf; const createManagementMock = () => { const mocked: jest.Mocked = { - isImportAndExportable: jest.fn().mockReturnValue(true), + getImportableAndExportableTypes: jest.fn(), + isImportAndExportable: jest.fn(), getDefaultSearchField: jest.fn(), getIcon: jest.fn(), getTitle: jest.fn(), getEditUrl: jest.fn(), getInAppUrl: jest.fn(), }; + + mocked.getImportableAndExportableTypes.mockReturnValue([]); + mocked.isImportAndExportable.mockReturnValue(true); + return mocked; };