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