1 import { Subscription } from 'rxjs'
2 import { filter, take } from 'rxjs/operators'
3 import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'
4 import { NavigationEnd, Router } from '@angular/router'
5 import { MenuService, ScreenService } from '@app/core'
6 import { GlobalIconName } from '@app/shared/shared-icons'
7 import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap'
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 @ViewChild('modal', { static: true }) modal: NgbModal
31 suffixLabels: { [ parentLabel: string ]: string }
34 currentMenuEntryIndex: number
36 private openedOnHover = false
37 private routeSub: Subscription
40 private router: Router,
41 private modalService: NgbModal,
42 private screen: ScreenService,
43 private menuService: MenuService
46 get isInSmallView () {
48 if (this.menuService.isMenuDisplayed) {
49 marginLeft = this.menuService.menuWidth
52 return this.screen.isInSmallView(marginLeft)
56 this.updateChildLabels(window.location.pathname)
58 this.routeSub = this.router.events
59 .pipe(filter(event => event instanceof NavigationEnd))
60 .subscribe(() => this.updateChildLabels(window.location.pathname))
62 this.hasIcons = this.menuEntries.some(
63 e => e.children && e.children.some(c => !!c.iconName)
68 if (this.routeSub) this.routeSub.unsubscribe()
71 openDropdownOnHover (dropdown: NgbDropdown) {
72 this.openedOnHover = true
78 .subscribe(() => this.openedOnHover = false)
81 dropdownAnchorClicked (dropdown: NgbDropdown) {
82 if (this.openedOnHover) {
83 this.openedOnHover = false
87 return dropdown.toggle()
90 closeDropdownIfHovered (dropdown: NgbDropdown) {
91 if (this.openedOnHover === false) return
94 this.openedOnHover = false
97 openModal (index: number) {
98 this.currentMenuEntryIndex = index
99 this.isModalOpened = true
101 this.modalService.open(this.modal, {
103 beforeDismiss: async () => {
104 this.onModalDismiss()
111 this.isModalOpened = false
114 dismissOtherModals () {
115 this.modalService.dismissAll()
118 private updateChildLabels (path: string) {
119 this.suffixLabels = {}
121 for (const entry of this.menuEntries) {
122 if (!entry.children) continue
124 for (const child of entry.children) {
125 if (path.startsWith(child.routerLink)) {
126 this.suffixLabels[entry.label] = child.label