Skip to content

Commit

Permalink
inject chrome properly
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Dec 18, 2019
1 parent f112668 commit e817686
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import React from 'react';
import { render } from 'react-dom';
import angular, { ICompileService } from 'angular';
import chrome from 'ui/chrome';
import {
DocViewRenderProps,
AngularScope,
AngularController,
AngularDirective,
} from './doc_views_types';
import { DocViewerError } from '../components/doc_viewer/doc_viewer_render_error';
import { Chrome } from '../kibana_services';

/**
* Compiles and injects the give angular template into the given dom node
Expand All @@ -36,7 +36,8 @@ export async function injectAngularElement(
domNode: Element,
template: string,
scopeProps: DocViewRenderProps,
Controller: AngularController
Controller: AngularController,
chrome: Chrome
): Promise<() => void> {
const $injector = await chrome.dangerouslyGetActiveInjector();
const rootScope: AngularScope = $injector.get('$rootScope');
Expand Down Expand Up @@ -68,15 +69,16 @@ export async function injectAngularElement(
* Converts a given legacy angular directive to a render function
* for usage in a react component. Note that the rendering is async
*/
export function convertDirectiveToRenderFn(directive: AngularDirective) {
export function convertDirectiveToRenderFn(directive: AngularDirective, chrome: Chrome) {
return (domNode: Element, props: DocViewRenderProps) => {
let rejected = false;

const cleanupFnPromise = injectAngularElement(
domNode,
directive.template,
props,
directive.controller
directive.controller,
chrome
);
cleanupFnPromise.catch(e => {
rejected = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@
*/
import { convertDirectiveToRenderFn } from './doc_views_helpers';
import { DocView, DocViewInput, ElasticSearchHit, DocViewInputFn } from './doc_views_types';
import { Chrome } from '../kibana_services';

export { DocViewRenderProps, DocView, DocViewRenderFn } from './doc_views_types';

export class DocViewsRegistry {
private docViews: DocView[] = [];

constructor(private legacyChrome: Chrome) {}

/**
* Extends and adds the given doc view to the registry array
*/
addDocView(docViewRaw: DocViewInput | DocViewInputFn) {
const docView = typeof docViewRaw === 'function' ? docViewRaw() : docViewRaw;
if (docView.directive) {
// convert angular directive to render function for backwards compatibility
docView.render = convertDirectiveToRenderFn(docView.directive);
docView.render = convertDirectiveToRenderFn(docView.directive, this.legacyChrome);
}
if (typeof docView.shouldShow !== 'function') {
docView.shouldShow = () => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function buildServices(
core: CoreStart,
plugins: DiscoverStartPlugins,
docViewsRegistry: DocViewsRegistry
) {
): Promise<DiscoverServices> {
const services = {
savedObjectsClient: core.savedObjects.client,
indexPatterns: plugins.data.indexPatterns,
Expand Down
8 changes: 7 additions & 1 deletion src/legacy/core_plugins/kibana/public/discover/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import chrome from 'ui/chrome';
import { PluginInitializer, PluginInitializerContext } from 'kibana/public';
import { npSetup, npStart } from 'ui/new_platform';
import { SavedObjectRegistryProvider } from 'ui/saved_objects';
Expand All @@ -28,7 +29,12 @@ export const plugin: PluginInitializer<DiscoverSetup, DiscoverStart> = () => {

// Legacy compatiblity part - to be removed at cutover, replaced by a kibana.json file
export const pluginInstance = plugin({} as PluginInitializerContext);
export const setup = pluginInstance.setup(npSetup.core, npSetup.plugins);
export const setup = pluginInstance.setup(npSetup.core, {
...npSetup.plugins,
__LEGACY: {
chrome,
},
});
export const start = pluginInstance.start(npStart.core, npStart.plugins);

SavedObjectRegistryProvider.register((savedSearches: any) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ export {
} from '../../../../../plugins/data/public';
export { ElasticSearchHit } from './doc_views/doc_views_types';
export { Adapters } from 'ui/inspector/types';
export { Chrome } from 'ui/chrome';
16 changes: 8 additions & 8 deletions src/legacy/core_plugins/kibana/public/discover/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { registerFeature } from './helpers/register_feature';
import './kibana_services';
import { IEmbeddableStart, IEmbeddableSetup } from '../../../../../plugins/embeddable/public';
import { getInnerAngularModule, getInnerAngularModuleEmbeddable } from './get_inner_angular';
import { setAngularModule, setServices } from './kibana_services';
import { Chrome, setAngularModule, setServices } from './kibana_services';
import { NavigationPublicPluginStart as NavigationStart } from '../../../../../plugins/navigation/public';
import { EuiUtilsStart } from '../../../../../plugins/eui_utils/public';
import { buildServices } from './helpers/build_services';
Expand All @@ -49,6 +49,9 @@ export interface DiscoverSetupPlugins {
uiActions: IUiActionsStart;
embeddable: IEmbeddableSetup;
kibana_legacy: KibanaLegacySetup;
__LEGACY: {
chrome: Chrome;
};
}
export interface DiscoverStartPlugins {
uiActions: IUiActionsStart;
Expand All @@ -70,16 +73,16 @@ const embeddableAngularName = 'app/discoverEmbeddable';
export class DiscoverPlugin implements Plugin<DiscoverSetup, DiscoverStart> {
private servicesInitialized: boolean = false;
private innerAngularInitialized: boolean = false;
private readonly docViewsRegistry: DocViewsRegistry;
private docViewsRegistry: DocViewsRegistry | null = null;
/**
* why are those functions public? they are needed for some mocha tests
* can be removed once all is Jest
*/
public initializeInnerAngular?: () => void;
public initializeServices?: () => void;

constructor() {
this.docViewsRegistry = new DocViewsRegistry();
setup(core: CoreSetup, plugins: DiscoverSetupPlugins): DiscoverSetup {
this.docViewsRegistry = new DocViewsRegistry(plugins.__LEGACY.chrome);
this.docViewsRegistry.addDocView({
title: i18n.translate('kbn.discover.docViews.table.tableTitle', {
defaultMessage: 'Table',
Expand All @@ -94,9 +97,6 @@ export class DiscoverPlugin implements Plugin<DiscoverSetup, DiscoverStart> {
order: 20,
component: JsonCodeBlock,
});
}

setup(core: CoreSetup, plugins: DiscoverSetupPlugins): DiscoverSetup {
plugins.kibana_legacy.registerLegacyApp({
id: 'discover',
title: 'Discover',
Expand Down Expand Up @@ -140,7 +140,7 @@ export class DiscoverPlugin implements Plugin<DiscoverSetup, DiscoverStart> {
if (this.servicesInitialized) {
return;
}
const services = await buildServices(core, plugins, this.docViewsRegistry);
const services = await buildServices(core, plugins, this.docViewsRegistry!);
setServices(services);
this.servicesInitialized = true;
};
Expand Down

0 comments on commit e817686

Please sign in to comment.