]>
Commit | Line | Data |
---|---|---|
34ba86a8 | 1 | import { ChangeDetectionStrategy, Component, Input } from '@angular/core' |
8bd4a1ed | 2 | import { Params } from '@angular/router' |
67ed6552 | 3 | import { GlobalIconName } from '@app/shared/shared-icons' |
eacb25c4 C |
4 | |
5 | export type DropdownAction<T> = { | |
eacb25c4 | 6 | label?: string |
3a0fb65c | 7 | iconName?: GlobalIconName |
9b82d49d RK |
8 | description?: string |
9 | title?: string | |
c199c427 | 10 | handler?: (a: T) => any |
8bd4a1ed | 11 | |
c199c427 | 12 | linkBuilder?: (a: T) => (string | number)[] |
8bd4a1ed C |
13 | queryParamsBuilder?: (a: T) => Params |
14 | ||
c199c427 | 15 | isDisplayed?: (a: T) => boolean |
a2c3564a C |
16 | |
17 | class?: string[] | |
bb152476 | 18 | isHeader?: boolean |
eacb25c4 C |
19 | } |
20 | ||
3a0fb65c C |
21 | export type DropdownButtonSize = 'normal' | 'small' |
22 | export type DropdownTheme = 'orange' | 'grey' | |
23 | export type DropdownDirection = 'horizontal' | 'vertical' | |
24 | ||
eacb25c4 C |
25 | @Component({ |
26 | selector: 'my-action-dropdown', | |
27 | styleUrls: [ './action-dropdown.component.scss' ], | |
34ba86a8 C |
28 | templateUrl: './action-dropdown.component.html', |
29 | changeDetection: ChangeDetectionStrategy.OnPush | |
eacb25c4 C |
30 | }) |
31 | ||
32 | export class ActionDropdownComponent<T> { | |
f97c91f7 | 33 | @Input() actions: DropdownAction<T>[] | DropdownAction<T>[][] = [] |
eacb25c4 | 34 | @Input() entry: T |
3a0fb65c | 35 | |
3921166d | 36 | @Input() placement = 'bottom-left auto' |
9b4241e3 | 37 | @Input() container: null | 'body' |
3a0fb65c C |
38 | |
39 | @Input() buttonSize: DropdownButtonSize = 'normal' | |
40 | @Input() buttonDirection: DropdownDirection = 'horizontal' | |
41 | @Input() buttonStyled = true | |
42 | ||
791645e6 | 43 | @Input() label: string |
3a0fb65c | 44 | @Input() theme: DropdownTheme = 'grey' |
f97c91f7 | 45 | |
be27ef3b C |
46 | getActions (): DropdownAction<T>[][] { |
47 | if (this.actions.length !== 0 && Array.isArray(this.actions[0])) return this.actions as DropdownAction<T>[][] | |
f97c91f7 | 48 | |
be27ef3b | 49 | return [ this.actions as DropdownAction<T>[] ] |
f97c91f7 | 50 | } |
3a0fb65c | 51 | |
d50e8d1a C |
52 | getQueryParams (action: DropdownAction<T>, entry: T) { |
53 | if (action.queryParamsBuilder) return action.queryParamsBuilder(entry) | |
54 | ||
55 | return {} | |
56 | } | |
57 | ||
d0fbc9fd | 58 | areActionsDisplayed (actions: (DropdownAction<T> | DropdownAction<T>[])[], entry: T): boolean { |
3d216ea0 C |
59 | return actions.some(a => { |
60 | if (Array.isArray(a)) return this.areActionsDisplayed(a, entry) | |
61 | ||
5b0ec7cd | 62 | return a.isHeader !== true && (a.isDisplayed === undefined || a.isDisplayed(entry)) |
3d216ea0 | 63 | }) |
3a0fb65c | 64 | } |
eacb25c4 | 65 | } |