]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Subscription } from 'rxjs' |
ae2dd046 | 2 | import { filter } from 'rxjs/operators' |
67ed6552 | 3 | import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core' |
80bfd33c | 4 | import { NavigationEnd, Router } from '@angular/router' |
67ed6552 C |
5 | import { MenuService, ScreenService } from '@app/core' |
6 | import { GlobalIconName } from '@app/shared/shared-icons' | |
d363ef53 | 7 | import { NgbDropdown, NgbModal } from '@ng-bootstrap/ng-bootstrap' |
ddb83e49 C |
8 | |
9 | export type TopMenuDropdownParam = { | |
10 | label: string | |
11 | routerLink?: string | |
dfe3f7b7 | 12 | isDisplayed?: () => boolean // Default: () => true |
ddb83e49 C |
13 | |
14 | children?: { | |
15 | label: string | |
16 | routerLink: string | |
a55052c9 | 17 | iconName?: GlobalIconName |
dfe3f7b7 K |
18 | |
19 | isDisplayed?: () => boolean // Default: () => true | |
ddb83e49 C |
20 | }[] |
21 | } | |
22 | ||
23 | @Component({ | |
24 | selector: 'my-top-menu-dropdown', | |
25 | templateUrl: './top-menu-dropdown.component.html', | |
26 | styleUrls: [ './top-menu-dropdown.component.scss' ] | |
27 | }) | |
28 | export class TopMenuDropdownComponent implements OnInit, OnDestroy { | |
29 | @Input() menuEntries: TopMenuDropdownParam[] = [] | |
30 | ||
d363ef53 K |
31 | @ViewChild('modal', { static: true }) modal: NgbModal |
32 | ||
ddb83e49 | 33 | suffixLabels: { [ parentLabel: string ]: string } |
a55052c9 | 34 | hasIcons = false |
d363ef53 K |
35 | isModalOpened = false |
36 | currentMenuEntryIndex: number | |
ddb83e49 | 37 | |
ddb83e49 C |
38 | private routeSub: Subscription |
39 | ||
64545a83 C |
40 | constructor ( |
41 | private router: Router, | |
d363ef53 | 42 | private modalService: NgbModal, |
8544d8f5 K |
43 | private screen: ScreenService, |
44 | private menuService: MenuService | |
d363ef53 K |
45 | ) { } |
46 | ||
47 | get isInSmallView () { | |
8544d8f5 K |
48 | let marginLeft = 0 |
49 | if (this.menuService.isMenuDisplayed) { | |
50 | marginLeft = this.menuService.menuWidth | |
51 | } | |
52 | ||
53 | return this.screen.isInSmallView(marginLeft) | |
d363ef53 | 54 | } |
ddb83e49 | 55 | |
7034b3c9 | 56 | get isBroadcastMessageDisplayed () { |
57 | return this.screen.isBroadcastMessageDisplayed | |
58 | } | |
59 | ||
ddb83e49 C |
60 | ngOnInit () { |
61 | this.updateChildLabels(window.location.pathname) | |
62 | ||
63 | this.routeSub = this.router.events | |
d363ef53 K |
64 | .pipe(filter(event => event instanceof NavigationEnd)) |
65 | .subscribe(() => this.updateChildLabels(window.location.pathname)) | |
a55052c9 C |
66 | |
67 | this.hasIcons = this.menuEntries.some( | |
68 | e => e.children && e.children.some(c => !!c.iconName) | |
69 | ) | |
ddb83e49 C |
70 | } |
71 | ||
72 | ngOnDestroy () { | |
73 | if (this.routeSub) this.routeSub.unsubscribe() | |
74 | } | |
75 | ||
80bfd33c | 76 | dropdownAnchorClicked (dropdown: NgbDropdown) { |
80bfd33c C |
77 | return dropdown.toggle() |
78 | } | |
79 | ||
d363ef53 K |
80 | openModal (index: number) { |
81 | this.currentMenuEntryIndex = index | |
82 | this.isModalOpened = true | |
83 | ||
84 | this.modalService.open(this.modal, { | |
85 | centered: true, | |
86 | beforeDismiss: async () => { | |
87 | this.onModalDismiss() | |
88 | return true | |
89 | } | |
90 | }) | |
91 | } | |
92 | ||
93 | onModalDismiss () { | |
94 | this.isModalOpened = false | |
95 | } | |
96 | ||
30d55e75 K |
97 | onActiveLinkScrollToTop (link: HTMLAnchorElement) { |
98 | if (!this.isBroadcastMessageDisplayed && this.router.url.includes(link.getAttribute('href'))) { | |
99 | window.scrollTo({ | |
100 | left: 0, | |
101 | top: 0, | |
102 | behavior: 'smooth' | |
103 | }) | |
104 | } | |
105 | } | |
106 | ||
d363ef53 K |
107 | dismissOtherModals () { |
108 | this.modalService.dismissAll() | |
109 | } | |
110 | ||
dfe3f7b7 K |
111 | isDisplayed (obj: { isDisplayed?: () => boolean }) { |
112 | if (typeof obj.isDisplayed !== 'function') return true | |
113 | ||
114 | return obj.isDisplayed() | |
115 | } | |
116 | ||
ddb83e49 C |
117 | private updateChildLabels (path: string) { |
118 | this.suffixLabels = {} | |
119 | ||
120 | for (const entry of this.menuEntries) { | |
121 | if (!entry.children) continue | |
122 | ||
123 | for (const child of entry.children) { | |
124 | if (path.startsWith(child.routerLink)) { | |
125 | this.suffixLabels[entry.label] = child.label | |
126 | } | |
127 | } | |
128 | } | |
129 | } | |
130 | } |