]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/misc/top-menu-dropdown.component.ts
Use onpush strategy for dropdown
[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,
98ab5dc8 87 beforeDismiss: () => {
d363ef53 88 this.onModalDismiss()
98ab5dc8 89
d363ef53
K
90 return true
91 }
92 })
93 }
94
95 onModalDismiss () {
96 this.isModalOpened = false
97 }
98
30d55e75
K
99 onActiveLinkScrollToTop (link: HTMLAnchorElement) {
100 if (!this.isBroadcastMessageDisplayed && this.router.url.includes(link.getAttribute('href'))) {
f3081d64 101 scrollToTop('smooth')
30d55e75
K
102 }
103 }
104
d363ef53
K
105 dismissOtherModals () {
106 this.modalService.dismissAll()
107 }
108
dfe3f7b7
K
109 isDisplayed (obj: { isDisplayed?: () => boolean }) {
110 if (typeof obj.isDisplayed !== 'function') return true
111
112 return obj.isDisplayed()
113 }
114
ddb83e49
C
115 private updateChildLabels (path: string) {
116 this.suffixLabels = {}
117
118 for (const entry of this.menuEntries) {
119 if (!entry.children) continue
120
121 for (const child of entry.children) {
122 if (path.startsWith(child.routerLink)) {
123 this.suffixLabels[entry.label] = child.label
124 }
125 }
126 }
127 }
128}