Using the Action API
The Action API exposes a service that can be injected in any Angular component. For more details, please refer to Action API documentation.
It allows configuring and performing UI-related actions from the web client. These actions can be associated with:
-
Buttons in the toolbar of custom views and dashboards, using the option Configure Toolbar. For more details, please refer to Section Understanding the Custom Views and Dashboards.
-
The Button widget. For more details, please refer to Section Using the Button Widget.
-
Custom entries in the Actions menu
available for each scenario displayed in the Scenario List widget. For more details, please refer to Section Using the Scenario List Widget.
When creating a new project, several action samples are provided in web/src/app/modules/sample-actions:
-
web/src/app/modules/sample-actions/actions/sample-action-copy-url.tsallows to perform a copy to clipboard of the current application URL. -
web/src/app/modules/sample-actions/actions/sample-action-hello-world-with-data.tsallows to use parameters and dynamic data. -
web/src/app/modules/sample-actions/actions/sample-action-open-webpage.tsallows to open a URL through a new web browser page, with preconditions. -
web/src/app/modules/sample-actions/actions/sample-action-scenario-info.tsallows to display information on a scenario and, through dynamic data and parameter resolution, add it to the Actions menuin the Scenario List widget.
Actions have an ID, a name and a description. The name and the description can be translated through the i18nName and i18nDescription keys, respectively.
An action is defined by several parts:
-
A
GeneActionExecutor, which is a function that will be called to execute the action. This method will return an observable ofGeneActionResult<R>whereRis a type of result. -
An optional execution parameter, which will be a value of a given type passed as a parameter of the action (if applicable).
-
An optional
parametersConfigurator,which, when provided, is a TypeScript class of the widget to use to configure the action parameter. The expected widget type must implement theGeneActionParametersAwareinterface. -
An optional
parametersResolverfunction, which, when provided, will resolve the parameter to use before the action execution. This is useful when an action parameter requires contextual runtime parameters that can not be configured beforehand. -
An optional list of
GenePrecondition, which, when provided, are evaluated and must all return{canExecute:true}to allow the user to execute the associated action. If one of the provided preconditions evaluates to{canExecute:false}, the web client will not let the user execute the action.
The following sample executes an action that copies the URL of the current page.
import { GeneAction, GeneActionExecutor, GeneActionManifest } from "@gene/widget-core";
import { GeneClipboardUtils } from "@gene/widget-data";
import { of } from "rxjs";
/**
* A sample action that copies the current URL to the clipboard
*/
export const SAMPLE_ACTION_COPY_URL: GeneActionManifest = {
id: "sample-copy-url",
name: "Sample Action — copy current URL",
icon: 'copy'
};
/**
* Executor function for SAMPLE_ACTION_COPY_URL action. Copy the current URL to the clipboard.
*/
export const copyUrlExecutor = ((_: GeneAction<string>) => {
const success = GeneClipboardUtils.copyUrlToClipboard();
return of( { success });
}) as GeneActionExecutor;
An action requires to be registered by code in order to be available in the web client at runtime. To do so, the GeneActionService#registerAction(...) must be used.
Here are some examples of action registrations that are available in the code samples of the web-frontend module.
-
Registering a simple action with no precondition: When executed, this action copies the current URL to the clipboard. It requires no preconditions and takes no parameters.
/** * A sample action that copies the current URL to the clipboard */ const SAMPLE_ACTION_COPY_URL: GeneActionManifest = { id: "sample-copy-url", name: "Sample Action — copy current URL", icon: 'copy' }; /** * Executor function for SAMPLE_ACTION_COPY_URL action. Copy the current URL to the clipboard. */ const copyUrlExecutor = ((_: GeneAction<string>) => { const success = GeneClipboardUtils.copyUrlToClipboard(); return of( { success }); }) as GeneActionExecutor; // Register the action actionService.registerAction({ manifest: SAMPLE_ACTION_COPY_URL, executor: copyUrlExecutor } as GeneActionRegistrationDescriptor<string>); -
Registering an action with preconditions: This action opens a new web browser page on a parameter URL. It has the precondition of having a parameter URL set and uses the
ActionTextInputConfiguratorComponentcomponent to configure the parameter URL./** * A sample action that opens a custom web page which URL is passed as parameter */ const SAMPLE_ACTION_OPEN_WEB_PAGE: GeneActionManifest = { id: "sample-open-web-page", name: "Sample Action — open a custom web page", icon: 'link', hasParameters: true }; /** * Executor function for SAMPLE_ACTION_OPEN_WEB_PAGE action. Open a new tab with the URL passed as parameter of the action. */ const openCustomWebPageExecutor = ((action: GeneAction<string>) => { const w = window.open(action.parameters, '_blank'); const success = w!=null; const result = success ? action.parameters : null; return of( { success, result }); }) as GeneActionExecutor; /** * A precondition that checks that the URL parameter is not null */ const notNullURLPrecondition : GenePrecondition<string> = { evaluate: (context: GeneContext, url?: string) => { const canExecute = url?.length > 0; const message = canExecute ? '' : 'The URL parameter must be provided.'; return of({ canExecute, message }) } }; // Register custom actions samples actionService.registerAction({ manifest: SAMPLE_ACTION_OPEN_WEB_PAGE, executor: openCustomWebPageExecutor, preconditions: [notNullURLPrecondition], parametersConfigurator: ActionTextInputConfiguratorComponent, } as GeneActionRegistrationDescriptor<string>);
Finally, actions can also be executed programmatically using GeneActionService to provide additional data to an action, which can be combined with a parameter resolver function, as in sample-action-hello-world-with-data.ts.
// Executing the action with runtime data to be used by the parameter resolver
actionService.executeAction({manifest:SAMPLE_ACTION_HELLO_WORLD}, `Jane Doe, born on ${new Date().toLocaleDateString()}`);
If need be, the Action API can display a configurator via the method getActionConfigurator.