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'
7 export type TopMenuDropdownParam = {
18 selector: 'my-top-menu-dropdown',
19 templateUrl: './top-menu-dropdown.component.html',
20 styleUrls: [ './top-menu-dropdown.component.scss' ]
22 export class TopMenuDropdownComponent implements OnInit, OnDestroy {
23 @Input() menuEntries: TopMenuDropdownParam[] = []
25 suffixLabels: { [ parentLabel: string ]: string }
27 private openedOnHover = false
28 private routeSub: Subscription
30 constructor (private router: Router) {}
33 this.updateChildLabels(window.location.pathname)
35 this.routeSub = this.router.events
36 .pipe(filter(event => event instanceof NavigationEnd))
37 .subscribe(() => this.updateChildLabels(window.location.pathname))
41 if (this.routeSub) this.routeSub.unsubscribe()
44 openDropdownOnHover (dropdown: NgbDropdown) {
45 this.openedOnHover = true
51 .subscribe(e => this.openedOnHover = false)
54 dropdownAnchorClicked (dropdown: NgbDropdown) {
55 if (this.openedOnHover) {
56 this.openedOnHover = false
60 return dropdown.toggle()
63 closeDropdownIfHovered (dropdown: NgbDropdown) {
64 if (this.openedOnHover === false) return
67 this.openedOnHover = false
70 private updateChildLabels (path: string) {
71 this.suffixLabels = {}
73 for (const entry of this.menuEntries) {
74 if (!entry.children) continue
76 for (const child of entry.children) {
77 if (path.startsWith(child.routerLink)) {
78 this.suffixLabels[entry.label] = child.label