1 import { Component, Input, OnDestroy, OnInit } from '@angular/core'
2 import { filter, take } from 'rxjs/operators'
3 import { NavigationEnd, Router } from '@angular/router'
4 import { Subscription } from 'rxjs'
5 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
6 import { GlobalIconName } from '@app/shared/images/global-icon.component'
7 import { ScreenService } from '@app/shared/misc/screen.service'
9 export type TopMenuDropdownParam = {
17 iconName?: GlobalIconName
22 selector: 'my-top-menu-dropdown',
23 templateUrl: './top-menu-dropdown.component.html',
24 styleUrls: [ './top-menu-dropdown.component.scss' ]
26 export class TopMenuDropdownComponent implements OnInit, OnDestroy {
27 @Input() menuEntries: TopMenuDropdownParam[] = []
29 suffixLabels: { [ parentLabel: string ]: string }
31 container: undefined | 'body' = undefined
33 private openedOnHover = false
34 private routeSub: Subscription
37 private router: Router,
38 private screen: ScreenService
42 this.updateChildLabels(window.location.pathname)
44 this.routeSub = this.router.events
45 .pipe(filter(event => event instanceof NavigationEnd))
46 .subscribe(() => this.updateChildLabels(window.location.pathname))
48 this.hasIcons = this.menuEntries.some(
49 e => e.children && e.children.some(c => !!c.iconName)
52 // FIXME: We have to set body for the container to avoid because of scroll overflow on mobile view
53 // But this break our hovering system
54 if (this.screen.isInMobileView()) {
55 this.container = 'body'
60 if (this.routeSub) this.routeSub.unsubscribe()
63 openDropdownOnHover (dropdown: NgbDropdown) {
64 this.openedOnHover = true
70 .subscribe(() => this.openedOnHover = false)
73 dropdownAnchorClicked (dropdown: NgbDropdown) {
74 if (this.openedOnHover) {
75 this.openedOnHover = false
79 return dropdown.toggle()
82 closeDropdownIfHovered (dropdown: NgbDropdown) {
83 if (this.openedOnHover === false) return
86 this.openedOnHover = false
89 private updateChildLabels (path: string) {
90 this.suffixLabels = {}
92 for (const entry of this.menuEntries) {
93 if (!entry.children) continue
95 for (const child of entry.children) {
96 if (path.startsWith(child.routerLink)) {
97 this.suffixLabels[entry.label] = child.label