1 import { ChangeDetectionStrategy, Component, Input } from '@angular/core'
2 import { Params } from '@angular/router'
3 import { GlobalIconName } from '@app/shared/shared-icons'
5 export type DropdownAction<T> = {
7 iconName?: GlobalIconName
10 handler?: (a: T) => any
12 linkBuilder?: (a: T) => (string | number)[]
13 queryParamsBuilder?: (a: T) => Params
15 isDisplayed?: (a: T) => boolean
21 export type DropdownButtonSize = 'normal' | 'small'
22 export type DropdownTheme = 'orange' | 'grey'
23 export type DropdownDirection = 'horizontal' | 'vertical'
26 selector: 'my-action-dropdown',
27 styleUrls: [ './action-dropdown.component.scss' ],
28 templateUrl: './action-dropdown.component.html',
29 changeDetection: ChangeDetectionStrategy.OnPush
32 export class ActionDropdownComponent<T> {
33 @Input() actions: DropdownAction<T>[] | DropdownAction<T>[][] = []
36 @Input() placement = 'bottom-left auto'
37 @Input() container: null | 'body'
39 @Input() buttonSize: DropdownButtonSize = 'normal'
40 @Input() buttonDirection: DropdownDirection = 'horizontal'
41 @Input() buttonStyled = true
43 @Input() label: string
44 @Input() theme: DropdownTheme = 'grey'
46 getActions (): DropdownAction<T>[][] {
47 if (this.actions.length !== 0 && Array.isArray(this.actions[0])) return this.actions as DropdownAction<T>[][]
49 return [ this.actions as DropdownAction<T>[] ]
52 getQueryParams (action: DropdownAction<T>, entry: T) {
53 if (action.queryParamsBuilder) return action.queryParamsBuilder(entry)
58 areActionsDisplayed (actions: (DropdownAction<T> | DropdownAction<T>[])[], entry: T): boolean {
59 return actions.some(a => {
60 if (Array.isArray(a)) return this.areActionsDisplayed(a, entry)
62 return a.isHeader !== true && (a.isDisplayed === undefined || a.isDisplayed(entry))