src/app/core/entity-details/entity-actions-menu/entity-actions-menu.service.ts
Register and access actions that can be performed on an entity and displayed to users in the entity details context menu.
Feature Modules can register additional actions here.
TODO: rename existing EntityActionsService to avoid confusions? (I would prefer this one here to be called EntityActionsService)
Methods |
getActions | ||||||
getActions(entity?: Entity)
|
||||||
Parameters :
Returns :
EntityAction[]
|
registerActions | ||||||
registerActions(newActions: EntityAction[])
|
||||||
Add (static) actions to be shown for all entity actions context menus.
Parameters :
Returns :
void
|
registerActionsFactories | ||||||
registerActionsFactories(newActions: EntityActionsFactory[])
|
||||||
Add factory functions to generate additional actions for the entity context menu depending on the specific entity for which the menu is displayed (e.g. only for entities of specific states).
Parameters :
Returns :
void
|
unregisterActions | ||||||||
unregisterActions(actionKeys: string[])
|
||||||||
Remove multiple registered static actions by their action keys.
Parameters :
Returns :
void
|
import { Injectable } from "@angular/core";
import { EntityAction } from "./entity-action.interface";
import { Entity } from "../../entity/model/entity";
/**
* Register and access actions that can be performed on an entity
* and displayed to users in the entity details context menu.
*
* Feature Modules can register additional actions here.
*
* TODO: rename existing EntityActionsService to avoid confusions? (I would prefer this one here to be called EntityActionsService)
*/
@Injectable({
providedIn: "root",
})
export class EntityActionsMenuService {
private actions: EntityAction[] = [];
private actionsFactories: EntityActionsFactory[] = [];
getActions(entity?: Entity): EntityAction[] {
return [
...this.actions,
...this.actionsFactories.flatMap((factory) => factory(entity)),
];
}
/**
* Add (static) actions to be shown for all entity actions context menus.
*/
registerActions(newActions: EntityAction[]) {
this.actions.push(...newActions);
}
/**
* Add factory functions to generate additional actions for the entity context menu
* depending on the specific entity for which the menu is displayed (e.g. only for entities of specific states).
*/
registerActionsFactories(newActions: EntityActionsFactory[]) {
this.actionsFactories.push(...newActions);
}
/**
* Remove multiple registered static actions by their action keys.
* @param actionKeys Array of action keys to unregister.
*/
unregisterActions(actionKeys: string[]): void {
this.actions = this.actions.filter(
(action) => !actionKeys.includes(action.action),
);
}
}
export type EntityActionsFactory = (entity: Entity) => EntityAction[];