Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger context #57870

Merged
merged 17 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { buildPipeline } from 'ui/visualize/loader/pipeline_helpers';
import { SavedObject } from 'ui/saved_objects/types';
import { npStart } from 'ui/new_platform';
import { IExpressionLoaderParams } from 'src/plugins/expressions/public';
import { EmbeddableVisTriggerContext } from 'src/plugins/embeddable/public';
import { VISUALIZE_EMBEDDABLE_TYPE } from './constants';
import {
IIndexPattern,
Expand All @@ -40,8 +41,8 @@ import {
EmbeddableOutput,
Embeddable,
Container,
VALUE_CLICK_TRIGGER,
SELECT_RANGE_TRIGGER,
selectRangeTrigger,
valueClickTrigger,
} from '../../../../../plugins/embeddable/public';
import { dispatchRenderComplete } from '../../../../../plugins/kibana_utils/public';
import { SavedSearch } from '../../../kibana/public/discover/np_ready/types';
Expand Down Expand Up @@ -301,13 +302,14 @@ export class VisualizeEmbeddable extends Embeddable<VisualizeInput, VisualizeOut
}

if (!this.input.disableTriggers) {
const eventName = event.name === 'brush' ? SELECT_RANGE_TRIGGER : VALUE_CLICK_TRIGGER;

npStart.plugins.uiActions.executeTriggerActions(eventName, {
const triggerId: 'SELECT_RANGE_TRIGGER' | 'VALUE_CLICK_TRIGGER' =
event.name === 'brush' ? selectRangeTrigger.id : valueClickTrigger.id;
const context: EmbeddableVisTriggerContext = {
embeddable: this,
timeFieldName: this.vis.indexPattern.timeFieldName,
data: event.data,
});
};
npStart.plugins.uiActions.getTrigger(triggerId).exec(context);
}
})
);
Expand Down
71 changes: 32 additions & 39 deletions src/plugins/embeddable/public/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,49 @@
* specific language governing permissions and limitations
* under the License.
*/

import { UiActionsSetup, Trigger } from 'src/plugins/ui_actions/public';
import { UiActionsSetup } from 'src/plugins/ui_actions/public';
import { Filter } from '../../data/public';
import {
CONTEXT_MENU_TRIGGER,
APPLY_FILTER_TRIGGER,
applyFilterTrigger,
contextMenuTrigger,
createFilterAction,
PANEL_BADGE_TRIGGER,
SELECT_RANGE_TRIGGER,
panelBadgeTrigger,
selectRangeTrigger,
valueClickTrigger,
EmbeddableVisTriggerContext,
IEmbeddable,
APPLY_FILTER_TRIGGER,
VALUE_CLICK_TRIGGER,
SELECT_RANGE_TRIGGER,
CONTEXT_MENU_TRIGGER,
PANEL_BADGE_TRIGGER,
} from './lib';

declare module '../../ui_actions/public' {
export interface TriggerContextMapping {
[SELECT_RANGE_TRIGGER]: EmbeddableVisTriggerContext;
[VALUE_CLICK_TRIGGER]: EmbeddableVisTriggerContext;
[APPLY_FILTER_TRIGGER]: {
embeddable: IEmbeddable;
filters: Filter[];
};
[CONTEXT_MENU_TRIGGER]: object;
[PANEL_BADGE_TRIGGER]: object;
}
}

/**
* This method initializes Embeddable plugin with initial set of
* triggers and actions.
*
* @param api
*/
export const bootstrap = (uiActions: UiActionsSetup) => {
const triggerContext: Trigger = {
id: CONTEXT_MENU_TRIGGER,
title: 'Context menu',
description: 'Triggered on top-right corner context-menu select.',
};
const triggerFilter: Trigger = {
id: APPLY_FILTER_TRIGGER,
title: 'Filter click',
description: 'Triggered when user applies filter to an embeddable.',
};
const triggerBadge: Trigger = {
id: PANEL_BADGE_TRIGGER,
title: 'Panel badges',
description: 'Actions appear in title bar when an embeddable loads in a panel',
};
const selectRangeTrigger: Trigger = {
id: SELECT_RANGE_TRIGGER,
title: 'Select range',
description: 'Applies a range filter',
};
const valueClickTrigger: Trigger = {
id: VALUE_CLICK_TRIGGER,
title: 'Value clicked',
description: 'Value was clicked',
};
uiActions.registerTrigger(contextMenuTrigger);
uiActions.registerTrigger(applyFilterTrigger);
uiActions.registerTrigger(panelBadgeTrigger);
uiActions.registerTrigger(selectRangeTrigger);
uiActions.registerTrigger(valueClickTrigger);

const actionApplyFilter = createFilterAction();

uiActions.registerTrigger(triggerContext);
uiActions.registerTrigger(triggerFilter);
uiActions.registerAction(actionApplyFilter);
uiActions.registerTrigger(triggerBadge);
uiActions.registerTrigger(selectRangeTrigger);
uiActions.registerTrigger(valueClickTrigger);
// uiActions.attachAction(triggerFilter.id, actionApplyFilter.id);
};
26 changes: 16 additions & 10 deletions src/plugins/embeddable/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,50 @@ import { PluginInitializerContext } from 'src/core/public';
import { EmbeddablePublicPlugin } from './plugin';

export {
Adapters,
ADD_PANEL_ACTION_ID,
AddPanelAction,
APPLY_FILTER_ACTION,
APPLY_FILTER_TRIGGER,
PANEL_BADGE_TRIGGER,
SELECT_RANGE_TRIGGER,
VALUE_CLICK_TRIGGER,
Adapters,
AddPanelAction,
CONTEXT_MENU_TRIGGER,
applyFilterTrigger,
Container,
ContainerInput,
ContainerOutput,
CONTEXT_MENU_TRIGGER,
contextMenuTrigger,
EDIT_PANEL_ACTION_ID,
EditPanelAction,
Embeddable,
EmbeddableChildPanel,
EmbeddableChildPanelProps,
EmbeddableFactory,
EmbeddableFactoryNotFoundError,
EmbeddableFactoryRenderer,
EmbeddableInput,
EmbeddableInstanceConfiguration,
EmbeddableOutput,
EmbeddablePanel,
EmbeddableRoot,
EmbeddableVisTriggerContext,
ErrorEmbeddable,
GetEmbeddableFactories,
GetEmbeddableFactory,
IContainer,
IEmbeddable,
isErrorEmbeddable,
openAddPanelFlyout,
OutputSpec,
PANEL_BADGE_TRIGGER,
panelBadgeTrigger,
PanelNotFoundError,
PanelState,
PropertySpec,
SELECT_RANGE_TRIGGER,
selectRangeTrigger,
VALUE_CLICK_TRIGGER,
valueClickTrigger,
ViewMode,
isErrorEmbeddable,
openAddPanelFlyout,
withEmbeddableSubscription,
EmbeddableFactoryRenderer,
EmbeddableRoot,
} from './lib';

export function plugin(initializerContext: PluginInitializerContext) {
Expand Down
6 changes: 1 addition & 5 deletions src/plugins/embeddable/public/lib/triggers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@
* under the License.
*/

export const CONTEXT_MENU_TRIGGER = 'CONTEXT_MENU_TRIGGER';
export const APPLY_FILTER_TRIGGER = 'FILTER_TRIGGER';
export const SELECT_RANGE_TRIGGER = 'SELECT_RANGE_TRIGGER';
export const VALUE_CLICK_TRIGGER = 'VALUE_CLICK_TRIGGER';
export const PANEL_BADGE_TRIGGER = 'PANEL_BADGE_TRIGGER';
export * from './triggers';
streamich marked this conversation as resolved.
Show resolved Hide resolved
65 changes: 65 additions & 0 deletions src/plugins/embeddable/public/lib/triggers/triggers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 '../../../../ui_actions/public';
import { IEmbeddable } from '..';

export interface EmbeddableVisTriggerContext {
embeddable: IEmbeddable;
timeFieldName: string;
data: {
e: MouseEvent;
data: unknown;
};
}

export const SELECT_RANGE_TRIGGER = 'SELECT_RANGE_TRIGGER';
export const selectRangeTrigger: Trigger<'SELECT_RANGE_TRIGGER'> = {
id: SELECT_RANGE_TRIGGER,
title: 'Select range',
description: 'Applies a range filter',
};

export const VALUE_CLICK_TRIGGER = 'VALUE_CLICK_TRIGGER';
export const valueClickTrigger: Trigger<'VALUE_CLICK_TRIGGER'> = {
id: VALUE_CLICK_TRIGGER,
title: 'Value clicked',
description: 'Value was clicked',
};

export const CONTEXT_MENU_TRIGGER = 'CONTEXT_MENU_TRIGGER';
export const contextMenuTrigger: Trigger<'CONTEXT_MENU_TRIGGER'> = {
id: CONTEXT_MENU_TRIGGER,
title: 'Context menu',
description: 'Triggered on top-right corner context-menu select.',
};

export const APPLY_FILTER_TRIGGER = 'FILTER_TRIGGER';
export const applyFilterTrigger: Trigger<'FILTER_TRIGGER'> = {
id: APPLY_FILTER_TRIGGER,
title: 'Filter click',
description: 'Triggered when user applies filter to an embeddable.',
};

export const PANEL_BADGE_TRIGGER = 'PANEL_BADGE_TRIGGER';
export const panelBadgeTrigger: Trigger<'PANEL_BADGE_TRIGGER'> = {
id: PANEL_BADGE_TRIGGER,
title: 'Panel badges',
description: 'Actions appear in title bar when an embeddable loads in a panel',
};
3 changes: 2 additions & 1 deletion src/plugins/ui_actions/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export { UiActionsSetup, UiActionsStart } from './plugin';
export { UiActionsServiceParams, UiActionsService } from './service';
export { Action, createAction, IncompatibleActionError } from './actions';
export { buildContextMenuForActions } from './context_menu';
export { Trigger } from './triggers';
export { Trigger, TriggerContext } from './triggers';
export { TriggerContextMapping } from './types';

/**
* @deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('UiActionsService', () => {

const trigger = service.getTrigger('bar');

expect(trigger).toEqual({
expect(trigger).toMatchObject({
description: 'foo',
id: 'bar',
title: 'baz',
Expand Down Expand Up @@ -345,8 +345,9 @@ describe('UiActionsService', () => {
id: 'bar',
title: 'baz',
});
const triggerContract = service.getTrigger('bar');

expect(triggers.get('bar')).toEqual({
expect(triggerContract).toMatchObject({
description: 'foo',
id: 'bar',
title: 'baz',
Expand Down
65 changes: 22 additions & 43 deletions src/plugins/ui_actions/public/service/ui_actions_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
* under the License.
*/

import { TriggerRegistry, ActionRegistry, TriggerToActionsRegistry } from '../types';
import { TriggerRegistry, ActionRegistry, TriggerToActionsRegistry, TriggerId } from '../types';
import { Action } from '../actions';
import { Trigger } from '../triggers/trigger';
import { buildContextMenuForActions, openContextMenu } from '../context_menu';
import { Trigger, TriggerContext } from '../triggers/trigger';
import { TriggerInternal } from '../triggers/trigger_internal';
import { TriggerContract } from '../triggers/trigger_contract';

export interface UiActionsServiceParams {
readonly triggers?: TriggerRegistry;
Expand Down Expand Up @@ -52,18 +53,20 @@ export class UiActionsService {
throw new Error(`Trigger [trigger.id = ${trigger.id}] already registered.`);
}

this.triggers.set(trigger.id, trigger);
const triggerInternal = new TriggerInternal(this, trigger);

this.triggers.set(trigger.id, triggerInternal);
this.triggerToActions.set(trigger.id, []);
};

public readonly getTrigger = (id: string) => {
const trigger = this.triggers.get(id);
public readonly getTrigger = <T extends TriggerId>(triggerId: T): TriggerContract<T> => {
const trigger = this.triggers.get(triggerId as string);

if (!trigger) {
throw new Error(`Trigger [triggerId = ${id}] does not exist.`);
throw new Error(`Trigger [triggerId = ${triggerId}] does not exist.`);
}

return trigger;
return trigger.contract;
};

public readonly registerAction = (action: Action) => {
Expand Down Expand Up @@ -128,41 +131,17 @@ export class UiActionsService {
);
};

private async executeSingleAction<A>(action: Action<A>, actionContext: A) {
const href = action.getHref && action.getHref(actionContext);

if (href) {
window.location.href = href;
return;
}

await action.execute(actionContext);
}

private async executeMultipleActions<C>(actions: Action[], actionContext: C) {
const panel = await buildContextMenuForActions({
actions,
actionContext,
closeMenu: () => session.close(),
});
const session = openContextMenu([panel]);
}

public readonly executeTriggerActions = async <C>(triggerId: string, actionContext: C) => {
const actions = await this.getTriggerCompatibleActions!(triggerId, actionContext);

if (!actions.length) {
throw new Error(
`No compatible actions found to execute for trigger [triggerId = ${triggerId}].`
);
}

if (actions.length === 1) {
await this.executeSingleAction(actions[0], actionContext);
return;
}

await this.executeMultipleActions(actions, actionContext);
/**
* @deprecated
*
* Use `plugins.uiActions.getTrigger(triggerId).exec(params)` instead.
*/
public readonly executeTriggerActions = async <T extends TriggerId>(
triggerId: T,
context: TriggerContext<T>
) => {
const trigger = this.getTrigger<T>(triggerId);
await trigger.exec(context);
};

/**
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/ui_actions/public/triggers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
* under the License.
*/

export { Trigger } from './trigger';
export * from './trigger';
export * from './trigger_contract';
export * from './trigger_internal';
Loading