]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/misc/top-menu-dropdown.component.ts
Fix css lint
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / misc / top-menu-dropdown.component.ts
CommitLineData
67ed6552 1import { Subscription } from 'rxjs'
ae2dd046 2import { filter } from 'rxjs/operators'
67ed6552 3import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'
80bfd33c 4import { NavigationEnd, Router } from '@angular/router'
67ed6552
C
5import { MenuService, ScreenService } from '@app/core'
6import { GlobalIconName } from '@app/shared/shared-icons'
d363ef53 7import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap'
ddb83e49
C
8
9export type TopMenuDropdownParam = {
10 label: string
11 routerLink?: string
dfe3f7b7 12 isDisplayed?: () => boolean // Default: () => true
ddb83e49
C
13
14 children?: {
15 label: string
16 routerLink: string
a55052c9 17 iconName?: GlobalIconName
dfe3f7b7
K
18
19 isDisplayed?: () => boolean // Default: () => true
ddb83e49
C
20 }[]
21}
22
23@Component({
24 selector: 'my-top-menu-dropdown',
25 templateUrl: './top-menu-dropdown.component.html',
26 styleUrls: [ './top-menu-dropdown.component.scss' ]
27})
28export class TopMenuDropdownComponent implements OnInit, OnDestroy {
29 @Input() menuEntries: TopMenuDropdownParam[] = []
30
d363ef53
K
31 @ViewChild('modal', { static: true }) modal: NgbModal
32
ddb83e49 33 suffixLabels: { [ parentLabel: string ]: string }
a55052c9 34 hasIcons = false
d363ef53
K
35 isModalOpened = false
36 currentMenuEntryIndex: number
ddb83e49 37
ddb83e49
C
38 private routeSub: Subscription
39
64545a83
C
40 constructor (
41 private router: Router,
d363ef53 42 private modalService: NgbModal,
8544d8f5
K
43 private screen: ScreenService,
44 private menuService: MenuService
d363ef53
K
45 ) { }
46
47 get isInSmallView () {
8544d8f5
K
48 let marginLeft = 0
49 if (this.menuService.isMenuDisplayed) {
50 marginLeft = this.menuService.menuWidth
51 }
52
53 return this.screen.isInSmallView(marginLeft)
d363ef53 54 }
ddb83e49 55
7034b3c9 56 get isBroadcastMessageDisplayed () {
57 return this.screen.isBroadcastMessageDisplayed
58 }
59
ddb83e49
C
60 ngOnInit () {
61 this.updateChildLabels(window.location.pathname)
62
63 this.routeSub = this.router.events
d363ef53
K
64 .pipe(filter(event => event instanceof NavigationEnd))
65 .subscribe(() => this.updateChildLabels(window.location.pathname))
a55052c9
C
66
67 this.hasIcons = this.menuEntries.some(
68 e => e.children && e.children.some(c => !!c.iconName)
69 )
ddb83e49
C
70 }
71
72 ngOnDestroy () {
73 if (this.routeSub) this.routeSub.unsubscribe()
74 }
75
80bfd33c 76 dropdownAnchorClicked (dropdown: NgbDropdown) {
80bfd33c
C
77 return dropdown.toggle()
78 }
79
d363ef53
K
80 openModal (index: number) {
81 this.currentMenuEntryIndex = index
82 this.isModalOpened = true
83
84 this.modalService.open(this.modal, {
85 centered: true,
86 beforeDismiss: async () => {
87 this.onModalDismiss()
88 return true
89 }
90 })
91 }
92
93 onModalDismiss () {
94 this.isModalOpened = false
95 }
96
97 dismissOtherModals () {
98 this.modalService.dismissAll()
99 }
100
dfe3f7b7
K
101 isDisplayed (obj: { isDisplayed?: () => boolean }) {
102 if (typeof obj.isDisplayed !== 'function') return true
103
104 return obj.isDisplayed()
105 }
106
ddb83e49
C
107 private updateChildLabels (path: string) {
108 this.suffixLabels = {}
109
110 for (const entry of this.menuEntries) {
111 if (!entry.children) continue
112
113 for (const child of entry.children) {
114 if (path.startsWith(child.routerLink)) {
115 this.suffixLabels[entry.label] = child.label
116 }
117 }
118 }
119 }
120}