aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/buttons/action-dropdown.component.ts
blob: f5345831b86dd5827d28a11a14411347945ecdc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Component, Input } from '@angular/core'
import { GlobalIconName } from '@app/shared/images/global-icon.component'

export type DropdownAction<T> = {
  label?: string
  iconName?: GlobalIconName
  handler?: (a: T) => any
  linkBuilder?: (a: T) => (string | number)[]
  isDisplayed?: (a: T) => boolean
}

export type DropdownButtonSize = 'normal' | 'small'
export type DropdownTheme = 'orange' | 'grey'
export type DropdownDirection = 'horizontal' | 'vertical'

@Component({
  selector: 'my-action-dropdown',
  styleUrls: [ './action-dropdown.component.scss' ],
  templateUrl: './action-dropdown.component.html'
})

export class ActionDropdownComponent<T> {
  @Input() actions: DropdownAction<T>[] | DropdownAction<T>[][] = []
  @Input() entry: T

  @Input() placement = 'bottom-left'

  @Input() buttonSize: DropdownButtonSize = 'normal'
  @Input() buttonDirection: DropdownDirection = 'horizontal'
  @Input() buttonStyled = true

  @Input() label: string
  @Input() theme: DropdownTheme = 'grey'

  getActions () {
    if (this.actions.length !== 0 && Array.isArray(this.actions[0])) return this.actions

    return [ this.actions ]
  }

  areActionsDisplayed (actions: DropdownAction<T>[], entry: T) {
    return actions.some(a => a.isDisplayed === undefined || a.isDisplayed(entry))
  }

  handleClick (event: Event, action: DropdownAction<T>) {
    event.preventDefault()

    // action.handler(entry)
  }
}