Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Aug 6, 2020
1 parent c460608 commit 4393299
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { createFilterAction } from './apply_filter_action';
import { expectErrorAsync } from '../../tests/helpers';
import { defaultTrigger } from '../../../../ui_actions/public/triggers';

test('has ACTION_APPLY_FILTER type and id', () => {
const action = createFilterAction();
Expand Down Expand Up @@ -51,7 +52,7 @@ describe('isCompatible()', () => {
}),
} as any,
filters: [],
trigger: null,
trigger: defaultTrigger,
});
expect(result).toBe(true);
});
Expand All @@ -67,7 +68,7 @@ describe('isCompatible()', () => {
}),
} as any,
filters: [],
trigger: null,
trigger: defaultTrigger,
});
expect(result).toBe(false);
});
Expand Down Expand Up @@ -121,7 +122,7 @@ describe('execute()', () => {
await action.execute({
embeddable,
filters: ['FILTER' as any],
trigger: null,
trigger: defaultTrigger,
});

expect(root.updateInput).toHaveBeenCalledTimes(1);
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/embeddable/public/lib/panel/embeddable_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
PANEL_BADGE_TRIGGER,
PANEL_NOTIFICATION_TRIGGER,
EmbeddableContext,
contextMenuTrigger,
} from '../triggers';
import { IEmbeddable, EmbeddableOutput, EmbeddableError } from '../embeddables/i_embeddable';
import { ViewMode } from '../types';
Expand Down Expand Up @@ -314,7 +315,7 @@ export class EmbeddablePanel extends React.Component<Props, State> {
actions: sortedActions.map((action) => ({
action,
context: { embeddable: this.props.embeddable },
trigger: null,
trigger: contextMenuTrigger,
})),
closeMenu: this.closeMyContextMenuPanel,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import React from 'react';
import { Action } from 'src/plugins/ui_actions/public';
import { PanelOptionsMenu } from './panel_options_menu';
import { IEmbeddable } from '../../embeddables';
import { EmbeddableContext } from '../../triggers';
import { EmbeddableContext, panelBadgeTrigger, panelNotificationTrigger } from '../../triggers';

export interface PanelHeaderProps {
title?: string;
Expand All @@ -49,11 +49,11 @@ function renderBadges(badges: Array<Action<EmbeddableContext>>, embeddable: IEmb
<EuiBadge
key={badge.id}
className="embPanel__headerBadge"
iconType={badge.getIconType({ embeddable, trigger: null })}
onClick={() => badge.execute({ embeddable, trigger: null })}
onClickAriaLabel={badge.getDisplayName({ embeddable, trigger: null })}
iconType={badge.getIconType({ embeddable, trigger: panelBadgeTrigger })}
onClick={() => badge.execute({ embeddable, trigger: panelBadgeTrigger })}
onClickAriaLabel={badge.getDisplayName({ embeddable, trigger: panelBadgeTrigger })}
>
{badge.getDisplayName({ embeddable, trigger: null })}
{badge.getDisplayName({ embeddable, trigger: panelBadgeTrigger })}
</EuiBadge>
));
}
Expand All @@ -70,14 +70,17 @@ function renderNotifications(
data-test-subj={`embeddablePanelNotification-${notification.id}`}
key={notification.id}
style={{ marginTop: '4px', marginRight: '4px' }}
onClick={() => notification.execute({ ...context, trigger: null })}
onClick={() => notification.execute({ ...context, trigger: panelNotificationTrigger })}
>
{notification.getDisplayName({ ...context, trigger: null })}
{notification.getDisplayName({ ...context, trigger: panelNotificationTrigger })}
</EuiNotificationBadge>
);

if (notification.getDisplayNameTooltip) {
const tooltip = notification.getDisplayNameTooltip({ ...context, trigger: null });
const tooltip = notification.getDisplayNameTooltip({
...context,
trigger: panelNotificationTrigger,
});

if (tooltip) {
badge = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from '../lib/test_samples';
// eslint-disable-next-line
import { esFilters } from '../../../data/public';
import { applyFilterTrigger } from '../../../ui_actions/public';

test('ApplyFilterAction applies the filter to the root of the container tree', async () => {
const { doStart, setup } = testPlugin();
Expand Down Expand Up @@ -86,7 +87,7 @@ test('ApplyFilterAction applies the filter to the root of the container tree', a
query: { match: { extension: { query: 'foo' } } },
};

await applyFilterAction.execute({ embeddable, filters: [filter], trigger: null });
await applyFilterAction.execute({ embeddable, filters: [filter], trigger: applyFilterTrigger });
expect(root.getInput().filters.length).toBe(1);
expect(node1.getInput().filters.length).toBe(1);
expect(embeddable.getInput().filters.length).toBe(1);
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/ui_actions/public/actions/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { ActionExecutionContext, createAction } from '../../../ui_actions/public';
import { ActionType } from '../types';
import { defaultTrigger } from '../triggers';

const sayHelloAction = createAction({
// Casting to ActionType is a hack - in a real situation use
Expand All @@ -31,15 +32,15 @@ const sayHelloAction = createAction({
test('action is not compatible based on context', async () => {
const isCompatible = await sayHelloAction.isCompatible({
amICompatible: false,
trigger: null,
trigger: defaultTrigger,
} as ActionExecutionContext);
expect(isCompatible).toBe(false);
});

test('action is compatible based on context', async () => {
const isCompatible = await sayHelloAction.isCompatible({
amICompatible: true,
trigger: null,
trigger: defaultTrigger,
} as ActionExecutionContext);
expect(isCompatible).toBe(true);
});
10 changes: 9 additions & 1 deletion src/plugins/ui_actions/public/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ export interface ActionExecutionMeta {
/**
* Trigger that executed the action
*/
trigger: Trigger | null;
trigger: Trigger;
}

/**
* Action methods are executed with Context from trigger + {@link ActionExecutionMeta}
*/
export type ActionExecutionContext<Context extends BaseContext = BaseContext> = Context &
ActionExecutionMeta;

/**
* Simplified action context for {@link ActionDefinition}
* When defining action consumer may use either it's own Context
* or an ActionExecutionContext<Context> to get access to {@link ActionExecutionMeta} params
*/
export type ActionDefinitionContext<Context extends BaseContext = BaseContext> =
| Context
| ActionExecutionContext<Context>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface ActionWithContext<Context extends BaseContext = BaseContext> {
/**
* Trigger that caused this action
*/
trigger: Trigger | null;
trigger: Trigger;
}

/**
Expand Down Expand Up @@ -105,7 +105,7 @@ async function convertPanelActionToContextMenuItem<Context extends object>({
}: {
action: Action<Context>;
actionContext: Context;
trigger: Trigger | null;
trigger: Trigger;
closeMenu: () => void;
}): Promise<EuiContextMenuPanelItemDescriptor> {
const menuPanelItem: EuiContextMenuPanelItemDescriptor = {
Expand Down
27 changes: 27 additions & 0 deletions src/plugins/ui_actions/public/triggers/default_trigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 { Trigger } from '.';

export const DEFAULT_TRIGGER = '';
export const defaultTrigger: Trigger<''> = {
id: DEFAULT_TRIGGER,
title: 'Unknown',
description: 'Unknown trigger.',
};
1 change: 1 addition & 0 deletions src/plugins/ui_actions/public/triggers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export * from './trigger_internal';
export * from './select_range_trigger';
export * from './value_click_trigger';
export * from './apply_filter_trigger';
export * from './default_trigger';
9 changes: 6 additions & 3 deletions src/plugins/ui_actions/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@

import { ActionInternal } from './actions/action_internal';
import { TriggerInternal } from './triggers/trigger_internal';
import { SELECT_RANGE_TRIGGER, VALUE_CLICK_TRIGGER, APPLY_FILTER_TRIGGER } from './triggers';
import {
SELECT_RANGE_TRIGGER,
VALUE_CLICK_TRIGGER,
APPLY_FILTER_TRIGGER,
DEFAULT_TRIGGER,
} from './triggers';
import type { RangeSelectContext, ValueClickContext } from '../../embeddable/public';
import type { ApplyGlobalFilterActionContext } from '../../data/public';

export type TriggerRegistry = Map<TriggerId, TriggerInternal<any>>;
export type ActionRegistry = Map<string, ActionInternal>;
export type TriggerToActionsRegistry = Map<TriggerId, string[]>;

const DEFAULT_TRIGGER = '';

export type TriggerId = keyof TriggerContextMapping;

export type BaseContext = object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import React from 'react';
import { ActionDefinitionByType } from '../../../../../../../../src/plugins/ui_actions/public';
import { ActionByType } from '../../../../../../../../src/plugins/ui_actions/public';
import {
reactToUiComponent,
toMountPoint,
Expand All @@ -23,8 +23,7 @@ export interface FlyoutEditDrilldownParams {
start: StartServicesGetter<Pick<StartDependencies, 'uiActionsEnhanced'>>;
}

export class FlyoutEditDrilldownAction
implements ActionDefinitionByType<typeof OPEN_FLYOUT_EDIT_DRILLDOWN> {
export class FlyoutEditDrilldownAction implements ActionByType<typeof OPEN_FLYOUT_EDIT_DRILLDOWN> {
public readonly type = OPEN_FLYOUT_EDIT_DRILLDOWN;
public readonly id = OPEN_FLYOUT_EDIT_DRILLDOWN;
public order = 10;
Expand Down

0 comments on commit 4393299

Please sign in to comment.