Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Jul 16, 2020
1 parent b34178c commit 4618512
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/plugins/embeddable/public/lib/panel/embeddable_panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ export class EmbeddablePanel extends React.Component<Props, State> {
const sortedActions = [...regularActions, ...extraActions].sort(sortByOrderField);

return await buildContextMenuForActions({
actions: sortedActions.map((action) => [action, { embeddable: this.props.embeddable }]),
actions: sortedActions.map((action) => ({
action,
context: { embeddable: this.props.embeddable },
})),
closeMenu: this.closeMyContextMenuPanel,
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/ui_actions/public/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type ActionByType<T extends ActionType> = Action<ActionContextMapping[T],
export interface ActionExecutionMeta {
/**
* Trigger that executed the action
* Since action could be trigger witout
* Optional - since action could be executed without a trigger
*/
trigger?: Trigger;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ export interface ActionDefinition<Context extends BaseContext = {}>
* without first showing up in context menu.
* false by default.
*/
shouldAutoExecute?(context: Context): Promise<boolean>;
shouldAutoExecute?(context: Context | ActionExecutionContext<Context>): Promise<boolean>;

/**
* This method should return a link if this item can be clicked on. The link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ export const defaultTitle = i18n.translate('uiActions.actionPanel.title', {
defaultMessage: 'Options',
});

type ActionWithContext<Context extends BaseContext = BaseContext> = [Action<Context>, Context];
interface ActionWithContext<Context extends BaseContext = BaseContext> {
action: Action<Context>;
context: Context;

/**
* Trigger that caused this action
*/
trigger?: Trigger;
}

/**
* Transforms an array of Actions to the shape EuiContextMenuPanel expects.
Expand Down Expand Up @@ -67,15 +75,18 @@ async function buildEuiContextMenuPanelItems({
closeMenu: () => void;
}) {
const items: EuiContextMenuPanelItemDescriptor[] = new Array(actions.length);
const promises = actions.map(async ([action, actionContext], index) => {
const isCompatible = await action.isCompatible(actionContext);
const promises = actions.map(async ({ action, context, trigger }, index) => {
const isCompatible = await action.isCompatible({
...context,
trigger,
});
if (!isCompatible) {
return;
}

items[index] = await convertPanelActionToContextMenuItem({
action,
actionContext,
actionContext: context,
closeMenu,
});
});
Expand All @@ -88,10 +99,12 @@ async function buildEuiContextMenuPanelItems({
async function convertPanelActionToContextMenuItem<Context extends object>({
action,
actionContext,
trigger,
closeMenu,
}: {
action: Action<Context>;
actionContext: Context;
trigger?: Trigger;
closeMenu: () => void;
}): Promise<EuiContextMenuPanelItemDescriptor> {
const menuPanelItem: EuiContextMenuPanelItemDescriptor = {
Expand All @@ -115,20 +128,29 @@ async function convertPanelActionToContextMenuItem<Context extends object>({
!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) // ignore clicks with modifier keys
) {
event.preventDefault();
action.execute(actionContext);
action.execute({
...actionContext,
trigger,
});
} else {
// let browser handle navigation
}
} else {
// not a link
action.execute(actionContext);
action.execute({
...actionContext,
trigger,
});
}

closeMenu();
};

if (action.getHref) {
const href = await action.getHref(actionContext);
const href = await action.getHref({
...actionContext,
trigger,
});
if (href) {
menuPanelItem.href = href;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ export class UiActionsExecutionService {
}, 0);
}

private async executeSingleTask({ context, action, defer }: ExecuteActionTask) {
private async executeSingleTask({ context, action, defer, trigger }: ExecuteActionTask) {
try {
await action.execute(context);
await action.execute({
...context,
trigger,
});
defer.resolve();
} catch (e) {
defer.reject(e);
Expand All @@ -107,7 +110,11 @@ export class UiActionsExecutionService {

private async executeMultipleActions(tasks: ExecuteActionTask[]) {
const panel = await buildContextMenuForActions({
actions: tasks.map(({ action, context }) => [action, context]),
actions: tasks.map(({ action, context, trigger }) => ({
action,
context,
trigger,
})),
title: tasks[0].trigger.title, // title of context menu is title of trigger which originated the chain
closeMenu: () => {
tasks.forEach((t) => t.defer.resolve());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test('executes a single action mapped to a trigger', async () => {
jest.runAllTimers();

expect(executeFn).toBeCalledTimes(1);
expect(executeFn).toBeCalledWith(context);
expect(executeFn).toBeCalledWith(expect.objectContaining(context));
});

test('throws an error if there are no compatible actions to execute', async () => {
Expand Down Expand Up @@ -202,3 +202,25 @@ test("doesn't show a context menu for auto executable actions", async () => {
expect(openContextMenu).toHaveBeenCalledTimes(0);
});
});

test('passes trigger into execute', async () => {
const { setup, doStart } = uiActions;
const trigger = {
id: 'MY-TRIGGER' as TriggerId,
title: 'My trigger',
};
const action = createTestAction<{ foo: string }>('test', () => true);

setup.registerTrigger(trigger);
setup.addTriggerAction(trigger.id, action);

const start = doStart();

const context = { foo: 'bar' };
await start.executeTriggerActions('MY-TRIGGER' as TriggerId, context);
jest.runAllTimers();
expect(executeFn).toBeCalledWith({
...context,
trigger,
});
});

0 comments on commit 4618512

Please sign in to comment.