]>
Commit | Line | Data |
---|---|---|
1 | import { Component, Input } from '@angular/core' | |
2 | import { GlobalIconName } from '@app/shared/images/global-icon.component' | |
3 | ||
4 | export type DropdownAction<T> = { | |
5 | label?: string | |
6 | iconName?: GlobalIconName | |
7 | handler?: (a: T) => any | |
8 | linkBuilder?: (a: T) => (string | number)[] | |
9 | isDisplayed?: (a: T) => boolean | |
10 | } | |
11 | ||
12 | export type DropdownButtonSize = 'normal' | 'small' | |
13 | export type DropdownTheme = 'orange' | 'grey' | |
14 | export type DropdownDirection = 'horizontal' | 'vertical' | |
15 | ||
16 | @Component({ | |
17 | selector: 'my-action-dropdown', | |
18 | styleUrls: [ './action-dropdown.component.scss' ], | |
19 | templateUrl: './action-dropdown.component.html' | |
20 | }) | |
21 | ||
22 | export class ActionDropdownComponent<T> { | |
23 | @Input() actions: DropdownAction<T>[] | DropdownAction<T>[][] = [] | |
24 | @Input() entry: T | |
25 | ||
26 | @Input() placement = 'bottom-left' | |
27 | ||
28 | @Input() buttonSize: DropdownButtonSize = 'normal' | |
29 | @Input() buttonDirection: DropdownDirection = 'horizontal' | |
30 | @Input() buttonStyled = true | |
31 | ||
32 | @Input() label: string | |
33 | @Input() theme: DropdownTheme = 'grey' | |
34 | ||
35 | getActions () { | |
36 | if (this.actions.length !== 0 && Array.isArray(this.actions[0])) return this.actions | |
37 | ||
38 | return [ this.actions ] | |
39 | } | |
40 | ||
41 | areActionsDisplayed (actions: Array<DropdownAction<T> | DropdownAction<T>[]>, entry: T): boolean { | |
42 | return actions.some(a => { | |
43 | if (Array.isArray(a)) return this.areActionsDisplayed(a, entry) | |
44 | ||
45 | return a.isDisplayed === undefined || a.isDisplayed(entry) | |
46 | }) | |
47 | } | |
48 | } |