1 import { Component, Input, OnInit } from '@angular/core';
3 import * as _ from 'lodash';
5 import { Icons } from '../../../shared/enum/icons.enum';
6 import { CdTableAction } from '../../models/cd-table-action';
7 import { CdTableSelection } from '../../models/cd-table-selection';
8 import { Permission } from '../../models/permissions';
11 selector: 'cd-table-actions',
12 templateUrl: './table-actions.component.html',
13 styleUrls: ['./table-actions.component.scss']
15 export class TableActionsComponent implements OnInit {
17 permission: Permission;
19 selection: CdTableSelection;
21 tableActions: CdTableAction[];
23 btnColor = 'secondary';
25 // Use this if you just want to display a drop down button,
26 // labeled with the given text, with all actions in it.
27 // This disables the main action button.
29 dropDownOnly?: string;
31 // Array with all visible actions
32 dropDownActions: CdTableAction[] = [];
39 this.removeActionsWithNoPermissions();
40 this.updateDropDownActions();
43 toClassName(name: string): string {
46 .replace(/[^a-z-]/gi, '')
51 * Removes all actions from 'tableActions' that need a permission the user doesn't have.
53 private removeActionsWithNoPermissions() {
54 if (!this.permission) {
55 this.tableActions = [];
58 const permissions = Object.keys(this.permission).filter((key) => this.permission[key]);
59 this.tableActions = this.tableActions.filter((action) =>
60 permissions.includes(action.permission)
64 private updateDropDownActions() {
65 this.dropDownActions = this.tableActions.filter((action) =>
66 action.visible ? action.visible(this.selection) : action
71 * Finds the next action that is used as main action for the button
73 * The order of the list is crucial to get the right main action.
75 * Default button conditions of actions:
76 * - 'create' actions can be used with no or multiple selections
77 * - 'update' and 'delete' actions can be used with one selection
79 * @returns {CdTableAction}
81 getCurrentButton(): CdTableAction {
82 if (this.dropDownOnly) {
85 let buttonAction = this.dropDownActions.find((tableAction) => this.showableAction(tableAction));
86 if (!buttonAction && this.dropDownActions.length > 0) {
87 buttonAction = this.dropDownActions[0];
93 * Determines if action can be used for the button
95 * @param {CdTableAction} action
98 private showableAction(action: CdTableAction): boolean {
99 const condition = action.canBePrimary;
100 const singleSelection = this.selection.hasSingleSelection;
101 const defaultCase = action.permission === 'create' ? !singleSelection : singleSelection;
102 return (condition && condition(this.selection)) || (!condition && defaultCase);
105 useRouterLink(action: CdTableAction): string {
106 if (!action.routerLink || this.disableSelectionAction(action)) {
109 return _.isString(action.routerLink) ? action.routerLink : action.routerLink();
113 * Determines if an action should be disabled
115 * Default disable conditions of 'update' and 'delete' actions:
116 * - If no or multiple selections are made
117 * - If one selection is made, but a task is executed on that item
119 * @param {CdTableAction} action
122 disableSelectionAction(action: CdTableAction): Boolean {
123 const disable = action.disable;
125 return Boolean(disable(this.selection));
127 const permission = action.permission;
128 const selected = this.selection.hasSingleSelection && this.selection.first();
130 ['update', 'delete'].includes(permission) && (!selected || selected.cdExecuting)
134 showDropDownActions() {
135 this.updateDropDownActions();
136 return this.dropDownActions.length > 1;
139 useClickAction(action: CdTableAction) {
141 * In order to show tooltips for deactivated menu items, the class
142 * 'pointer-events: auto;' has been added to the .scss file which also
143 * re-activates the click-event.
144 * To prevent calling the click-event on deactivated elements we also have
145 * to check here if it's disabled.
147 return !this.disableSelectionAction(action) && action.click && action.click();
150 useDisableDesc(action: CdTableAction) {
151 return action.disableDesc && action.disableDesc(this.selection);