]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Subscription } from 'rxjs' |
ddb83e49 | 2 | import { filter, take } 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 | |
12 | ||
13 | children?: { | |
14 | label: string | |
15 | routerLink: string | |
a55052c9 C |
16 | |
17 | iconName?: GlobalIconName | |
ddb83e49 C |
18 | }[] |
19 | } | |
20 | ||
21 | @Component({ | |
22 | selector: 'my-top-menu-dropdown', | |
23 | templateUrl: './top-menu-dropdown.component.html', | |
24 | styleUrls: [ './top-menu-dropdown.component.scss' ] | |
25 | }) | |
26 | export class TopMenuDropdownComponent implements OnInit, OnDestroy { | |
27 | @Input() menuEntries: TopMenuDropdownParam[] = [] | |
28 | ||
d363ef53 K |
29 | @ViewChild('modal', { static: true }) modal: NgbModal |
30 | ||
ddb83e49 | 31 | suffixLabels: { [ parentLabel: string ]: string } |
a55052c9 | 32 | hasIcons = false |
d363ef53 K |
33 | isModalOpened = false |
34 | currentMenuEntryIndex: number | |
ddb83e49 | 35 | |
ddb83e49 C |
36 | private routeSub: Subscription |
37 | ||
64545a83 C |
38 | constructor ( |
39 | private router: Router, | |
d363ef53 | 40 | private modalService: NgbModal, |
8544d8f5 K |
41 | private screen: ScreenService, |
42 | private menuService: MenuService | |
d363ef53 K |
43 | ) { } |
44 | ||
45 | get isInSmallView () { | |
8544d8f5 K |
46 | let marginLeft = 0 |
47 | if (this.menuService.isMenuDisplayed) { | |
48 | marginLeft = this.menuService.menuWidth | |
49 | } | |
50 | ||
51 | return this.screen.isInSmallView(marginLeft) | |
d363ef53 | 52 | } |
ddb83e49 C |
53 | |
54 | ngOnInit () { | |
55 | this.updateChildLabels(window.location.pathname) | |
56 | ||
57 | this.routeSub = this.router.events | |
d363ef53 K |
58 | .pipe(filter(event => event instanceof NavigationEnd)) |
59 | .subscribe(() => this.updateChildLabels(window.location.pathname)) | |
a55052c9 C |
60 | |
61 | this.hasIcons = this.menuEntries.some( | |
62 | e => e.children && e.children.some(c => !!c.iconName) | |
63 | ) | |
ddb83e49 C |
64 | } |
65 | ||
66 | ngOnDestroy () { | |
67 | if (this.routeSub) this.routeSub.unsubscribe() | |
68 | } | |
69 | ||
80bfd33c | 70 | dropdownAnchorClicked (dropdown: NgbDropdown) { |
80bfd33c C |
71 | return dropdown.toggle() |
72 | } | |
73 | ||
d363ef53 K |
74 | openModal (index: number) { |
75 | this.currentMenuEntryIndex = index | |
76 | this.isModalOpened = true | |
77 | ||
78 | this.modalService.open(this.modal, { | |
79 | centered: true, | |
80 | beforeDismiss: async () => { | |
81 | this.onModalDismiss() | |
82 | return true | |
83 | } | |
84 | }) | |
85 | } | |
86 | ||
87 | onModalDismiss () { | |
88 | this.isModalOpened = false | |
89 | } | |
90 | ||
91 | dismissOtherModals () { | |
92 | this.modalService.dismissAll() | |
93 | } | |
94 | ||
ddb83e49 C |
95 | private updateChildLabels (path: string) { |
96 | this.suffixLabels = {} | |
97 | ||
98 | for (const entry of this.menuEntries) { | |
99 | if (!entry.children) continue | |
100 | ||
101 | for (const child of entry.children) { | |
102 | if (path.startsWith(child.routerLink)) { | |
103 | this.suffixLabels[entry.label] = child.label | |
104 | } | |
105 | } | |
106 | } | |
107 | } | |
108 | } |