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 { scrollToTop } from '@app/helpers'
7 import { GlobalIconName } from '@app/shared/shared-icons'
8 import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap'
10 export type TopMenuDropdownParam = {
13 isDisplayed?: () => boolean // Default: () => true
18 iconName?: GlobalIconName
20 isDisplayed?: () => boolean // Default: () => true
25 selector: 'my-top-menu-dropdown',
26 templateUrl: './top-menu-dropdown.component.html',
27 styleUrls: [ './top-menu-dropdown.component.scss' ]
29 export class TopMenuDropdownComponent implements OnInit, OnDestroy {
30 @Input() menuEntries: TopMenuDropdownParam[] = []
32 @ViewChild('modal', { static: true }) modal: NgbModal
34 suffixLabels: { [ parentLabel: string ]: string }
37 currentMenuEntryIndex: number
39 private routeSub: Subscription
42 private router: Router,
43 private modalService: NgbModal,
44 private screen: ScreenService,
45 private menuService: MenuService
48 get isInSmallView () {
50 if (this.menuService.isMenuDisplayed) {
51 marginLeft = this.menuService.menuWidth
54 return this.screen.isInSmallView(marginLeft)
57 get isBroadcastMessageDisplayed () {
58 return this.screen.isBroadcastMessageDisplayed
62 this.updateChildLabels(window.location.pathname)
64 this.routeSub = this.router.events
65 .pipe(filter(event => event instanceof NavigationEnd))
66 .subscribe(() => this.updateChildLabels(window.location.pathname))
68 this.hasIcons = this.menuEntries.some(
69 e => e.children && e.children.some(c => !!c.iconName)
74 if (this.routeSub) this.routeSub.unsubscribe()
77 dropdownAnchorClicked (dropdown: NgbDropdown) {
78 return dropdown.toggle()
81 openModal (index: number) {
82 this.currentMenuEntryIndex = index
83 this.isModalOpened = true
85 this.modalService.open(this.modal, {
87 beforeDismiss: async () => {
95 this.isModalOpened = false
98 onActiveLinkScrollToTop (link: HTMLAnchorElement) {
99 if (!this.isBroadcastMessageDisplayed && this.router.url.includes(link.getAttribute('href'))) {
100 scrollToTop('smooth')
104 dismissOtherModals () {
105 this.modalService.dismissAll()
108 isDisplayed (obj: { isDisplayed?: () => boolean }) {
109 if (typeof obj.isDisplayed !== 'function') return true
111 return obj.isDisplayed()
114 private updateChildLabels (path: string) {
115 this.suffixLabels = {}
117 for (const entry of this.menuEntries) {
118 if (!entry.children) continue
120 for (const child of entry.children) {
121 if (path.startsWith(child.routerLink)) {
122 this.suffixLabels[entry.label] = child.label