Skip to content

Commit

Permalink
review: conform closer to NP standards
Browse files Browse the repository at this point in the history
  • Loading branch information
stacey-gammon committed Jun 24, 2019
1 parent 2e01b85 commit 9f855e4
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ import { resolve } from 'path';
export default function(kibana: any) {
return new kibana.Plugin({
uiExports: {
embeddableActions: ['plugins/dashboard_embeddable_container/actions/expand_panel_action'],
embeddableFactories: [
'plugins/dashboard_embeddable_container/embeddable/dashboard_container_factory',
],
hacks: 'plugins/dashboard_embeddable_container/shim',
styleSheetPaths: resolve(__dirname, 'public/index.scss'),
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@

import '../np_core.test.mocks';

import {
isErrorEmbeddable,
createRegistry,
EmbeddableFactory,
} from '../../../embeddable_api/public';
import { isErrorEmbeddable, EmbeddableFactory } from '../../../embeddable_api/public';
import { ExpandPanelAction } from './expand_panel_action';
import {
ContactCardEmbeddable,
Expand All @@ -35,7 +31,7 @@ import {
import { DashboardContainer } from '../embeddable';
import { getSampleDashboardInput, getSampleDashboardPanel } from '../test_helpers';

const embeddableFactories = createRegistry<EmbeddableFactory>();
const embeddableFactories = new Map<string, EmbeddableFactory>();
embeddableFactories.set(CONTACT_CARD_EMBEDDABLE, new ContactCardEmbeddableFactory());

let container: DashboardContainer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ import { i18n } from '@kbn/i18n';
import React from 'react';
import {
Action,
actionRegistry,
IEmbeddable,
CONTEXT_MENU_TRIGGER,
triggerRegistry,
ActionContext,
attachAction,
IncompatibleActionError,
} from '../../../embeddable_api/public';
import { DASHBOARD_CONTAINER_TYPE, DashboardContainer } from '../embeddable';
Expand Down Expand Up @@ -98,10 +94,3 @@ export class ExpandPanelAction extends Action {
});
}
}

actionRegistry.set(EXPAND_PANEL_ACTION, new ExpandPanelAction());

attachAction(triggerRegistry, {
triggerId: CONTEXT_MENU_TRIGGER,
actionId: EXPAND_PANEL_ACTION,
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
triggerRegistry,
CONTEXT_MENU_TRIGGER,
attachAction,
createRegistry,
EmbeddableFactory,
} from '../../../embeddable_api/public';
import { DashboardContainer } from './dashboard_container';
Expand All @@ -50,7 +49,7 @@ import {
} from '../../../embeddable_api/public/test_samples';

test('DashboardContainer initializes embeddables', async done => {
const embeddableFactories = createRegistry<EmbeddableFactory>();
const embeddableFactories = new Map<string, EmbeddableFactory>();
embeddableFactories.set(CONTACT_CARD_EMBEDDABLE, new ContactCardEmbeddableFactory());
const container = new DashboardContainer(
getSampleDashboardInput({
Expand Down Expand Up @@ -83,7 +82,7 @@ test('DashboardContainer initializes embeddables', async done => {
});

test('DashboardContainer.addNewEmbeddable', async () => {
const embeddableFactories = createRegistry<EmbeddableFactory>();
const embeddableFactories = new Map<string, EmbeddableFactory>();
embeddableFactories.set(CONTACT_CARD_EMBEDDABLE, new ContactCardEmbeddableFactory());
const container = new DashboardContainer(getSampleDashboardInput(), embeddableFactories);
const embeddable = await container.addNewEmbeddable<ContactCardEmbeddableInput>(
Expand All @@ -106,7 +105,7 @@ test('DashboardContainer.addNewEmbeddable', async () => {
});

test('Container view mode change propagates to existing children', async () => {
const embeddableFactories = createRegistry<EmbeddableFactory>();
const embeddableFactories = new Map<string, EmbeddableFactory>();
embeddableFactories.set(CONTACT_CARD_EMBEDDABLE, new ContactCardEmbeddableFactory());
const container = new DashboardContainer(
getSampleDashboardInput({
Expand All @@ -128,7 +127,7 @@ test('Container view mode change propagates to existing children', async () => {
});

test('Container view mode change propagates to new children', async () => {
const embeddableFactories = createRegistry<EmbeddableFactory>();
const embeddableFactories = new Map<string, EmbeddableFactory>();
embeddableFactories.set(CONTACT_CARD_EMBEDDABLE, new ContactCardEmbeddableFactory());
const container = new DashboardContainer(getSampleDashboardInput(), embeddableFactories);
const embeddable = await container.addNewEmbeddable<
Expand All @@ -154,7 +153,7 @@ test('DashboardContainer in edit mode shows edit mode actions', async () => {
actionId: editModeAction.id,
});

const embeddableFactories = createRegistry<EmbeddableFactory>();
const embeddableFactories = new Map<string, EmbeddableFactory>();
embeddableFactories.set(CONTACT_CARD_EMBEDDABLE, new ContactCardEmbeddableFactory());
const container = new DashboardContainer(
getSampleDashboardInput({ viewMode: ViewMode.VIEW }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
isErrorEmbeddable,
EmbeddableFactory,
IEmbeddable,
IRegistry,
} from '../../../embeddable_api/public/index';

import { DASHBOARD_CONTAINER_TYPE } from './dashboard_container_factory';
Expand Down Expand Up @@ -76,7 +75,7 @@ export class DashboardContainer extends Container<InheritedChildInput, Dashboard

constructor(
initialInput: DashboardContainerInput,
embeddableFactories: IRegistry<EmbeddableFactory>,
embeddableFactories: Map<string, EmbeddableFactory>,
parent?: Container
) {
super(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
* under the License.
*/

import { capabilities } from 'ui/capabilities';
import { i18n } from '@kbn/i18n';
import { SavedObjectMetaData } from 'ui/saved_objects/components/saved_object_finder';
import { SavedObjectAttributes } from 'target/types/server';
import {
ContainerOutput,
embeddableFactories,
Expand All @@ -36,9 +37,24 @@ export class DashboardContainerFactory extends EmbeddableFactory<
> {
public readonly isContainerType = true;
public readonly type = DASHBOARD_CONTAINER_TYPE;
private allowEditing: boolean;

constructor({
savedObjectMetaData,
capabilities,
}: {
savedObjectMetaData?: SavedObjectMetaData<SavedObjectAttributes>;
capabilities: {
showWriteControls: boolean;
createNew: boolean;
};
}) {
super({ savedObjectMetaData });
this.allowEditing = capabilities.createNew && capabilities.showWriteControls;
}

public isEditable() {
return capabilities.get().dashboard.save as boolean;
return this.allowEditing;
}

public getDisplayName() {
Expand All @@ -62,5 +78,3 @@ export class DashboardContainerFactory extends EmbeddableFactory<
return new DashboardContainer(initialInput, embeddableFactories, parent);
}
}

embeddableFactories.set(DASHBOARD_CONTAINER_TYPE, new DashboardContainerFactory());
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import sizeMe from 'react-sizeme';

import { skip } from 'rxjs/operators';

import { createRegistry, EmbeddableFactory } from '../../../../embeddable_api/public';
import { EmbeddableFactory } from '../../../../embeddable_api/public';
import {
ContactCardEmbeddableFactory,
CONTACT_CARD_EMBEDDABLE,
Expand All @@ -39,7 +39,7 @@ import { getSampleDashboardInput } from '../../test_helpers';
let dashboardContainer: DashboardContainer | undefined;

function getProps(props?: Partial<DashboardGridProps>): DashboardGridProps {
const embeddableFactories = createRegistry<EmbeddableFactory>();
const embeddableFactories = new Map<string, EmbeddableFactory>();
embeddableFactories.set(CONTACT_CARD_EMBEDDABLE, new ContactCardEmbeddableFactory());
dashboardContainer = new DashboardContainer(
getSampleDashboardInput({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
ContactCardEmbeddableFactory,
CONTACT_CARD_EMBEDDABLE,
} from '../../../../embeddable_api/public/test_samples';
import { EmbeddableFactory, createRegistry } from '../../../../embeddable_api/public';
import { EmbeddableFactory } from '../../../../embeddable_api/public';

import { DashboardViewport, DashboardViewportProps } from './dashboard_viewport';
import { DashboardContainer } from '../dashboard_container';
Expand All @@ -41,7 +41,7 @@ import { getSampleDashboardInput } from '../../test_helpers';
let dashboardContainer: DashboardContainer | undefined;

function getProps(props?: Partial<DashboardViewportProps>): DashboardViewportProps {
const embeddableFactories = createRegistry<EmbeddableFactory>();
const embeddableFactories = new Map<string, EmbeddableFactory>();
embeddableFactories.set(CONTACT_CARD_EMBEDDABLE, new ContactCardEmbeddableFactory());
dashboardContainer = new DashboardContainer(
getSampleDashboardInput({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { PluginInitializerContext } from 'kibana/public';
import { npSetup, npStart } from 'ui/new_platform';
import { embeddablePlugin } from '../../../embeddable_api/public';
import { Plugin } from './plugin';

export function plugin(initializerContext: PluginInitializerContext) {
const dashboardContainerPlugin = new Plugin(initializerContext);

dashboardContainerPlugin.setup(npSetup.core, {
embeddable: embeddablePlugin,
});

dashboardContainerPlugin.start(npStart.core, {
embeddable: embeddablePlugin,
});
}

plugin({} as any);
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { PluginInitializerContext, CoreSetup, CoreStart } from 'kibana/public';
import { EmbeddablePlugin, CONTEXT_MENU_TRIGGER } from '../../../embeddable_api/public';
import { ExpandPanelAction, EXPAND_PANEL_ACTION } from '../actions/expand_panel_action';
import { DashboardContainerFactory } from '../embeddable';

export class Plugin {
constructor(initializerContext: PluginInitializerContext) {}

public setup(core: CoreSetup, plugins: { embeddable: EmbeddablePlugin }) {
plugins.embeddable.addAction(new ExpandPanelAction());
plugins.embeddable.attachAction({
triggerId: CONTEXT_MENU_TRIGGER,
actionId: EXPAND_PANEL_ACTION,
});
}

public start(core: CoreStart, plugins: { embeddable: EmbeddablePlugin }) {
plugins.embeddable.addEmbeddableFactory(
new DashboardContainerFactory({
capabilities: core.application.capabilities.dashboard as {
showWriteControls: boolean;
createNew: boolean;
},
})
);
}

public stop() {}
}
5 changes: 2 additions & 3 deletions src/legacy/core_plugins/embeddable_api/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export {
isErrorEmbeddable,
} from './embeddables';

export { ViewMode, Trigger } from './types';
export { ViewMode, Trigger, EmbeddablePlugin } from './types';

export { actionRegistry, Action, ActionContext, IncompatibleActionError } from './actions';

Expand All @@ -52,5 +52,4 @@ export {

export { AddPanelAction, EmbeddablePanel, openAddPanelFlyout } from './panel';

// TODO: replace with version from kibana-utils plugin when available
export { createRegistry } from './create_registry';
export { embeddablePlugin } from './plugin';
30 changes: 30 additions & 0 deletions src/legacy/core_plugins/embeddable_api/public/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { Action, actionRegistry } from './actions';
import { EmbeddableFactory, embeddableFactories } from './embeddables';
import { EmbeddablePlugin } from './types';
import { attachAction, triggerRegistry } from './triggers';

export const embeddablePlugin: EmbeddablePlugin = {
addAction: (action: Action) => actionRegistry.set(action.id, action),
addEmbeddableFactory: (factory: EmbeddableFactory) =>
embeddableFactories.set(factory.type, factory),
attachAction: data => attachAction(triggerRegistry, data),
};
9 changes: 9 additions & 0 deletions src/legacy/core_plugins/embeddable_api/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* under the License.
*/

import { Action } from './actions';
import { EmbeddableFactory } from './embeddables';

export interface Trigger {
id: string;
title?: string;
Expand All @@ -40,3 +43,9 @@ export enum ViewMode {
EDIT = 'edit',
VIEW = 'view',
}

export interface EmbeddablePlugin {
addAction: (action: Action) => void;
addEmbeddableFactory: (factory: EmbeddableFactory) => void;
attachAction: (data: { triggerId: string; actionId: string }) => void;
}

0 comments on commit 9f855e4

Please sign in to comment.