6649b092accb278f93c04452342ebcb64fc49b61
[oweals/peertube.git] / client / src / app / shared / buttons / action-dropdown.component.ts
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   description?: string
8   title?: string
9   handler?: (a: T) => any
10   linkBuilder?: (a: T) => (string | number)[]
11   isDisplayed?: (a: T) => boolean
12 }
13
14 export type DropdownButtonSize = 'normal' | 'small'
15 export type DropdownTheme = 'orange' | 'grey'
16 export type DropdownDirection = 'horizontal' | 'vertical'
17
18 @Component({
19   selector: 'my-action-dropdown',
20   styleUrls: [ './action-dropdown.component.scss' ],
21   templateUrl: './action-dropdown.component.html'
22 })
23
24 export class ActionDropdownComponent<T> {
25   @Input() actions: DropdownAction<T>[] | DropdownAction<T>[][] = []
26   @Input() entry: T
27
28   @Input() placement = 'bottom-left auto'
29
30   @Input() buttonSize: DropdownButtonSize = 'normal'
31   @Input() buttonDirection: DropdownDirection = 'horizontal'
32   @Input() buttonStyled = true
33
34   @Input() label: string
35   @Input() theme: DropdownTheme = 'grey'
36
37   getActions (): DropdownAction<T>[][] {
38     if (this.actions.length !== 0 && Array.isArray(this.actions[0])) return this.actions as DropdownAction<T>[][]
39
40     return [ this.actions as DropdownAction<T>[] ]
41   }
42
43   areActionsDisplayed (actions: Array<DropdownAction<T> | DropdownAction<T>[]>, entry: T): boolean {
44     return actions.some(a => {
45       if (Array.isArray(a)) return this.areActionsDisplayed(a, entry)
46
47       return a.isDisplayed === undefined || a.isDisplayed(entry)
48     })
49   }
50 }