1 import { Subscription } from 'rxjs'
2 import { filter } 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 = {
12 isDisplayed?: () => boolean // Default: () => true
17 iconName?: GlobalIconName
19 isDisplayed?: () => boolean // Default: () => true
24 selector: 'my-top-menu-dropdown',
25 templateUrl: './top-menu-dropdown.component.html',
26 styleUrls: [ './top-menu-dropdown.component.scss' ]
28 export class TopMenuDropdownComponent implements OnInit, OnDestroy {
29 @Input() menuEntries: TopMenuDropdownParam[] = []
31 @ViewChild('modal', { static: true }) modal: NgbModal
33 suffixLabels: { [ parentLabel: string ]: string }
36 currentMenuEntryIndex: number
38 private routeSub: Subscription
41 private router: Router,
42 private modalService: NgbModal,
43 private screen: ScreenService,
44 private menuService: MenuService
47 get isInSmallView () {
49 if (this.menuService.isMenuDisplayed) {
50 marginLeft = this.menuService.menuWidth
53 return this.screen.isInSmallView(marginLeft)
56 get isBroadcastMessageDisplayed () {
57 return this.screen.isBroadcastMessageDisplayed
61 this.updateChildLabels(window.location.pathname)
63 this.routeSub = this.router.events
64 .pipe(filter(event => event instanceof NavigationEnd))
65 .subscribe(() => this.updateChildLabels(window.location.pathname))
67 this.hasIcons = this.menuEntries.some(
68 e => e.children && e.children.some(c => !!c.iconName)
73 if (this.routeSub) this.routeSub.unsubscribe()
76 dropdownAnchorClicked (dropdown: NgbDropdown) {
77 return dropdown.toggle()
80 openModal (index: number) {
81 this.currentMenuEntryIndex = index
82 this.isModalOpened = true
84 this.modalService.open(this.modal, {
86 beforeDismiss: async () => {
94 this.isModalOpened = false
97 onActiveLinkScrollToTop (link: HTMLAnchorElement) {
98 if (!this.isBroadcastMessageDisplayed && this.router.url.includes(link.getAttribute('href'))) {
107 dismissOtherModals () {
108 this.modalService.dismissAll()
111 isDisplayed (obj: { isDisplayed?: () => boolean }) {
112 if (typeof obj.isDisplayed !== 'function') return true
114 return obj.isDisplayed()
117 private updateChildLabels (path: string) {
118 this.suffixLabels = {}
120 for (const entry of this.menuEntries) {
121 if (!entry.children) continue
123 for (const child of entry.children) {
124 if (path.startsWith(child.routerLink)) {
125 this.suffixLabels[entry.label] = child.label