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