forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.ts
More file actions
177 lines (151 loc) · 5.82 KB
/
action.ts
File metadata and controls
177 lines (151 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* 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 { UiComponent } from 'src/plugins/opensearch_dashboards_utils/public';
import { EuiIconType } from '@elastic/eui/src/components/icon/icon';
import { ActionType, ActionContextMapping, BaseContext } from '../types';
import { Presentable } from '../util/presentable';
import { Trigger } from '../triggers';
export type ActionByType<T extends ActionType> = Action<ActionContextMapping[T], T>;
export type ActionDefinitionByType<T extends ActionType> = ActionDefinition<
ActionContextMapping[T]
>;
/**
* During action execution we can provide additional information,
* for example, trigger, that caused the action execution
*/
export interface ActionExecutionMeta {
/**
* Trigger that executed the action
*/
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>;
export interface Action<Context extends BaseContext = {}, T = ActionType>
extends Partial<Presentable<ActionExecutionContext<Context>>> {
/**
* Determined the order when there is more than one action matched to a trigger.
* Higher numbers are displayed first.
*/
order?: number;
/**
* A unique identifier for this action instance.
*/
id: string;
/**
* The action type is what determines the context shape.
*/
readonly type: T;
isDisabled?(context: ActionExecutionContext<Context>): boolean;
getTooltip?(context: ActionExecutionContext<Context>): string;
/**
* Optional EUI icon type that can be displayed along with the title.
*/
getIconType(context: ActionExecutionContext<Context>): EuiIconType | undefined;
/**
* Returns a title to be displayed to the user.
* @param context
*/
getDisplayName(context: ActionExecutionContext<Context>): JSX.Element | string;
/**
* `UiComponent` to render when displaying this action as a context menu item.
* If not provided, `getDisplayName` will be used instead.
*/
MenuItem?: UiComponent<{ context: ActionExecutionContext<Context> }>;
/**
* Returns a promise that resolves to true if this action is compatible given the context,
* otherwise resolves to false.
*/
isCompatible(context: ActionExecutionContext<Context>): Promise<boolean>;
/**
* Executes the action.
*/
execute(context: ActionExecutionContext<Context>): Promise<void>;
/**
* This method should return a link if this item can be clicked on. The link
* is used to navigate user if user middle-clicks it or Ctrl + clicks or
* right-clicks and selects "Open in new tab".
*/
getHref?(context: ActionExecutionContext<Context>): Promise<string | undefined>;
/**
* Determines if action should be executed automatically,
* without first showing up in context menu.
* false by default.
*/
shouldAutoExecute?(context: ActionExecutionContext<Context>): Promise<boolean>;
}
/**
* A convenience interface used to register an action.
*/
export interface ActionDefinition<Context extends BaseContext = {}>
extends Partial<Presentable<ActionDefinitionContext<Context>>> {
/**
* ID of the action that uniquely identifies this action in the actions registry.
*/
readonly id: string;
/**
* ID of the factory for this action. Used to construct dynamic actions.
*/
readonly type?: ActionType;
/**
* Returns a promise that resolves to true if this item is compatible given
* the context and should be displayed to user, otherwise resolves to false.
*/
isCompatible?(context: ActionDefinitionContext<Context>): Promise<boolean>;
/**
* Executes the action.
*/
execute(context: ActionDefinitionContext<Context>): Promise<void>;
/**
* Determines if action should be executed automatically,
* without first showing up in context menu.
* false by default.
*/
isDisabled?(context: ActionExecutionContext<Context>): boolean;
getTooltip?(context: ActionExecutionContext<Context>): string;
shouldAutoExecute?(context: ActionDefinitionContext<Context>): Promise<boolean>;
/**
* This method should return a link if this item can be clicked on. The link
* is used to navigate user if user middle-clicks it or Ctrl + clicks or
* right-clicks and selects "Open in new tab".
*/
getHref?(context: ActionDefinitionContext<Context>): Promise<string | undefined>;
}
export type ActionContext<A> = A extends ActionDefinition<infer Context> ? Context : never;