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