8 import { filter, take } from 'rxjs/operators'
9 import { NavigationEnd, Router } from '@angular/router'
10 import { Subscription } from 'rxjs'
11 import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap'
12 import { GlobalIconName } from '@app/shared/images/global-icon.component'
13 import { ScreenService } from '@app/shared/misc/screen.service'
14 import { MenuService } from '@app/core/menu'
16 export type TopMenuDropdownParam = {
24 iconName?: GlobalIconName
29 selector: 'my-top-menu-dropdown',
30 templateUrl: './top-menu-dropdown.component.html',
31 styleUrls: [ './top-menu-dropdown.component.scss' ]
33 export class TopMenuDropdownComponent implements OnInit, OnDestroy {
34 @Input() menuEntries: TopMenuDropdownParam[] = []
36 @ViewChild('modal', { static: true }) modal: NgbModal
38 suffixLabels: { [ parentLabel: string ]: string }
41 currentMenuEntryIndex: number
43 private openedOnHover = false
44 private routeSub: Subscription
47 private router: Router,
48 private modalService: NgbModal,
49 private screen: ScreenService,
50 private menuService: MenuService
53 get isInSmallView () {
55 if (this.menuService.isMenuDisplayed) {
56 marginLeft = this.menuService.menuWidth
59 return this.screen.isInSmallView(marginLeft)
63 this.updateChildLabels(window.location.pathname)
65 this.routeSub = this.router.events
66 .pipe(filter(event => event instanceof NavigationEnd))
67 .subscribe(() => this.updateChildLabels(window.location.pathname))
69 this.hasIcons = this.menuEntries.some(
70 e => e.children && e.children.some(c => !!c.iconName)
75 if (this.routeSub) this.routeSub.unsubscribe()
78 openDropdownOnHover (dropdown: NgbDropdown) {
79 this.openedOnHover = true
85 .subscribe(() => this.openedOnHover = false)
88 dropdownAnchorClicked (dropdown: NgbDropdown) {
89 if (this.openedOnHover) {
90 this.openedOnHover = false
94 return dropdown.toggle()
97 closeDropdownIfHovered (dropdown: NgbDropdown) {
98 if (this.openedOnHover === false) return
101 this.openedOnHover = false
104 openModal (index: number) {
105 this.currentMenuEntryIndex = index
106 this.isModalOpened = true
108 this.modalService.open(this.modal, {
110 beforeDismiss: async () => {
111 this.onModalDismiss()
118 this.isModalOpened = false
121 dismissOtherModals () {
122 this.modalService.dismissAll()
125 private updateChildLabels (path: string) {
126 this.suffixLabels = {}
128 for (const entry of this.menuEntries) {
129 if (!entry.children) continue
131 for (const child of entry.children) {
132 if (path.startsWith(child.routerLink)) {
133 this.suffixLabels[entry.label] = child.label