diff --git a/src/plugins/embeddable/public/bootstrap.ts b/src/plugins/embeddable/public/bootstrap.ts index a3dc090894c34..9989345df2796 100644 --- a/src/plugins/embeddable/public/bootstrap.ts +++ b/src/plugins/embeddable/public/bootstrap.ts @@ -16,8 +16,8 @@ * specific language governing permissions and limitations * under the License. */ - import { UiActionsSetup } from 'src/plugins/ui_actions/public'; +import { Filter } from '../../data/public'; import { applyFilterTrigger, contextMenuTrigger, @@ -26,15 +26,24 @@ import { 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; - CONTEXT_MENU_TRIGGER: object; - APPLY_FILTER_TRIGGER: object; - PANEL_BADGE_TRIGGER: object; + [SELECT_RANGE_TRIGGER]: EmbeddableVisTriggerContext; + [VALUE_CLICK_TRIGGER]: EmbeddableVisTriggerContext; + [APPLY_FILTER_TRIGGER]: { + embeddable: IEmbeddable; + filters: Filter[]; + }; + [CONTEXT_MENU_TRIGGER]: object; + [PANEL_BADGE_TRIGGER]: object; } } diff --git a/src/plugins/ui_actions/public/index.ts b/src/plugins/ui_actions/public/index.ts index f1a83462a563b..1ce48d5460b2e 100644 --- a/src/plugins/ui_actions/public/index.ts +++ b/src/plugins/ui_actions/public/index.ts @@ -29,7 +29,7 @@ export { UiActionsSetup, UiActionsStart } from './plugin'; export { UiActionsServiceParams, UiActionsService } from './service'; export { Action, createAction, IncompatibleActionError } from './actions'; export { buildContextMenuForActions } from './context_menu'; -export { Trigger, AnyTrigger, TriggerContext } from './triggers'; +export { Trigger, TriggerContext } from './triggers'; export { TriggerContextMapping } from './types'; /** diff --git a/src/plugins/ui_actions/public/service/ui_actions_service.ts b/src/plugins/ui_actions/public/service/ui_actions_service.ts index c906200e281db..ae409830bbb6e 100644 --- a/src/plugins/ui_actions/public/service/ui_actions_service.ts +++ b/src/plugins/ui_actions/public/service/ui_actions_service.ts @@ -17,14 +17,9 @@ * under the License. */ -import { - TriggerRegistry, - ActionRegistry, - TriggerToActionsRegistry, - TriggerContextMapping, -} from '../types'; +import { TriggerRegistry, ActionRegistry, TriggerToActionsRegistry, TriggerId } from '../types'; import { Action } from '../actions'; -import { AnyTrigger, Trigger } from '../triggers/trigger'; +import { Trigger, TriggerContext } from '../triggers/trigger'; import { TriggerInternal } from '../triggers/trigger_internal'; import { TriggerContract } from '../triggers/trigger_contract'; @@ -53,7 +48,7 @@ export class UiActionsService { this.triggerToActions = triggerToActions; } - public readonly registerTrigger = (trigger: T) => { + public readonly registerTrigger = (trigger: Trigger) => { if (this.triggers.has(trigger.id)) { throw new Error(`Trigger [trigger.id = ${trigger.id}] already registered.`); } @@ -64,9 +59,7 @@ export class UiActionsService { this.triggerToActions.set(trigger.id, []); }; - public readonly getTrigger = ( - triggerId: T - ): TriggerContract> => { + public readonly getTrigger = (triggerId: T): TriggerContract => { const trigger = this.triggers.get(triggerId as string); if (!trigger) { @@ -143,11 +136,11 @@ export class UiActionsService { * * Use `plugins.uiActions.getTrigger(triggerId).exec(params)` instead. */ - public readonly executeTriggerActions = async ( - triggerId: string, - context: TriggerContextMapping[T['id']] + public readonly executeTriggerActions = async ( + triggerId: T, + context: TriggerContext ) => { - const trigger = this.getTrigger(triggerId); + const trigger = this.getTrigger(triggerId); await trigger.exec(context); }; diff --git a/src/plugins/ui_actions/public/triggers/trigger.ts b/src/plugins/ui_actions/public/triggers/trigger.ts index 85c6fcd21569a..2c019b09881d1 100644 --- a/src/plugins/ui_actions/public/triggers/trigger.ts +++ b/src/plugins/ui_actions/public/triggers/trigger.ts @@ -17,7 +17,7 @@ * under the License. */ -import { TriggerContextMapping } from '../types'; +import { TriggerContextMapping, TriggerId } from '../types'; /** * This is a convenience interface used to register a *trigger*. @@ -30,7 +30,7 @@ import { TriggerContextMapping } from '../types'; * trigger is *called* it first displays a context menu for user to pick a * single action to execute. */ -export interface Trigger { +export interface Trigger { /** * Unique name of the trigger as identified in `ui_actions` plugin trigger * registry, such as "SELECT_RANGE_TRIGGER" or "VALUE_CLICK_TRIGGER". @@ -48,6 +48,4 @@ export interface Trigger { description?: string; } -export type AnyTrigger = Trigger; - -export type TriggerContext = T extends Trigger ? TriggerContextMapping[ID] : never; +export type TriggerContext = T extends TriggerId ? TriggerContextMapping[T] : never; diff --git a/src/plugins/ui_actions/public/triggers/trigger_contract.ts b/src/plugins/ui_actions/public/triggers/trigger_contract.ts index cfabd40bbfad8..853b83dccabcc 100644 --- a/src/plugins/ui_actions/public/triggers/trigger_contract.ts +++ b/src/plugins/ui_actions/public/triggers/trigger_contract.ts @@ -17,18 +17,19 @@ * under the License. */ -import { AnyTrigger, TriggerContext } from './trigger'; +import { TriggerContext } from './trigger'; import { TriggerInternal } from './trigger_internal'; +import { TriggerId } from '../types'; /** * This is a public representation of a trigger that is provided to other plugins. */ -export class TriggerContract { +export class TriggerContract { /** * Unique name of the trigger as identified in `ui_actions` plugin trigger * registry, such as "SELECT_RANGE_TRIGGER" or "VALUE_CLICK_TRIGGER". */ - public readonly id: string; + public readonly id: T; /** * User friendly name of the trigger. diff --git a/src/plugins/ui_actions/public/triggers/trigger_internal.ts b/src/plugins/ui_actions/public/triggers/trigger_internal.ts index b3e6d4531fd41..efcdc72ecad57 100644 --- a/src/plugins/ui_actions/public/triggers/trigger_internal.ts +++ b/src/plugins/ui_actions/public/triggers/trigger_internal.ts @@ -17,20 +17,21 @@ * under the License. */ -import { AnyTrigger, TriggerContext } from './trigger'; +import { TriggerContext, Trigger } from './trigger'; import { TriggerContract } from './trigger_contract'; import { UiActionsService } from '../service'; import { Action } from '../actions'; import { buildContextMenuForActions, openContextMenu } from '../context_menu'; +import { TriggerId } from '../types'; /** * Internal representation of a trigger kept for consumption only internally * within `ui_actions` plugin. */ -export class TriggerInternal { +export class TriggerInternal { public readonly contract = new TriggerContract(this); - constructor(public readonly service: UiActionsService, public readonly trigger: T) {} + constructor(public readonly service: UiActionsService, public readonly trigger: Trigger) {} public async execute(context: TriggerContext) { const triggerId = this.trigger.id; diff --git a/src/plugins/ui_actions/public/types.ts b/src/plugins/ui_actions/public/types.ts index 3bee5913038fe..8daa893eb4347 100644 --- a/src/plugins/ui_actions/public/types.ts +++ b/src/plugins/ui_actions/public/types.ts @@ -24,6 +24,8 @@ export type TriggerRegistry = Map>; export type ActionRegistry = Map; export type TriggerToActionsRegistry = Map; +export type TriggerId = string; + export interface TriggerContextMapping { [key: string]: object; }