]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1import { Subscription } from 'rxjs'
2import { filter } from 'rxjs/operators'
3import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'
4import { NavigationEnd, Router } from '@angular/router'
5import { MenuService, ScreenService } from '@app/core'
6import { scrollToTop } from '@app/helpers'
7import { GlobalIconName } from '@app/shared/shared-icons'
8import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap'
9
10export type TopMenuDropdownParam = {
11 label: string
12 routerLink?: string
13 isDisplayed?: () => boolean // Default: () => true
14
15 children?: {
16 label: string
17 routerLink: string
18 iconName?: GlobalIconName
19
20 isDisplayed?: () => boolean // Default: () => true
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
32 @ViewChild('modal', { static: true }) modal: NgbModal
33
34 suffixLabels: { [ parentLabel: string ]: string }
35 hasIcons = false
36 isModalOpened = false
37 currentMenuEntryIndex: number
38
39 private routeSub: Subscription
40
41 constructor (
42 private router: Router,
43 private modalService: NgbModal,
44 private screen: ScreenService,
45 private menuService: MenuService
46 ) { }
47
48 get isInSmallView () {
49 let marginLeft = 0
50 if (this.menuService.isMenuDisplayed) {
51 marginLeft = this.menuService.menuWidth
52 }
53
54 return this.screen.isInSmallView(marginLeft)
55 }
56
57 get isBroadcastMessageDisplayed () {
58 return this.screen.isBroadcastMessageDisplayed
59 }
60
61 ngOnInit () {
62 this.updateChildLabels(window.location.pathname)
63
64 this.routeSub = this.router.events
65 .pipe(filter(event => event instanceof NavigationEnd))
66 .subscribe(() => this.updateChildLabels(window.location.pathname))
67
68 this.hasIcons = this.menuEntries.some(
69 e => e.children?.some(c => !!c.iconName)
70 )
71 }
72
73 ngOnDestroy () {
74 if (this.routeSub) this.routeSub.unsubscribe()
75 }
76
77 dropdownAnchorClicked (dropdown: NgbDropdown) {
78 return dropdown.toggle()
79 }
80
81 openModal (index: number) {
82 this.currentMenuEntryIndex = index
83 this.isModalOpened = true
84
85 this.modalService.open(this.modal, {
86 centered: true,
87 beforeDismiss: () => {
88 this.onModalDismiss()
89
90 return true
91 }
92 })
93 }
94
95 onModalDismiss () {
96 this.isModalOpened = false
97 }
98
99 onActiveLinkScrollToTop (link: HTMLAnchorElement) {
100 if (!this.isBroadcastMessageDisplayed && this.router.url.includes(link.getAttribute('href'))) {
101 scrollToTop('smooth')
102 }
103 }
104
105 dismissOtherModals () {
106 this.modalService.dismissAll()
107 }
108
109 isDisplayed (obj: { isDisplayed?: () => boolean }) {
110 if (typeof obj.isDisplayed !== 'function') return true
111
112 return obj.isDisplayed()
113 }
114
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}