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 routeSub: Subscription
39 private router: Router,
40 private modalService: NgbModal,
41 private screen: ScreenService,
42 private menuService: MenuService
45 get isInSmallView () {
47 if (this.menuService.isMenuDisplayed) {
48 marginLeft = this.menuService.menuWidth
51 return this.screen.isInSmallView(marginLeft)
55 this.updateChildLabels(window.location.pathname)
57 this.routeSub = this.router.events
58 .pipe(filter(event => event instanceof NavigationEnd))
59 .subscribe(() => this.updateChildLabels(window.location.pathname))
61 this.hasIcons = this.menuEntries.some(
62 e => e.children && e.children.some(c => !!c.iconName)
67 if (this.routeSub) this.routeSub.unsubscribe()
70 dropdownAnchorClicked (dropdown: NgbDropdown) {
71 return dropdown.toggle()
74 openModal (index: number) {
75 this.currentMenuEntryIndex = index
76 this.isModalOpened = true
78 this.modalService.open(this.modal, {
80 beforeDismiss: async () => {
88 this.isModalOpened = false
91 dismissOtherModals () {
92 this.modalService.dismissAll()
95 private updateChildLabels (path: string) {
96 this.suffixLabels = {}
98 for (const entry of this.menuEntries) {
99 if (!entry.children) continue
101 for (const child of entry.children) {
102 if (path.startsWith(child.routerLink)) {
103 this.suffixLabels[entry.label] = child.label