]>
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 C |
55 | |
56 | ngOnInit () { | |
57 | this.updateChildLabels(window.location.pathname) | |
58 | ||
59 | this.routeSub = this.router.events | |
d363ef53 K |
60 | .pipe(filter(event => event instanceof NavigationEnd)) |
61 | .subscribe(() => this.updateChildLabels(window.location.pathname)) | |
a55052c9 C |
62 | |
63 | this.hasIcons = this.menuEntries.some( | |
64 | e => e.children && e.children.some(c => !!c.iconName) | |
65 | ) | |
ddb83e49 C |
66 | } |
67 | ||
68 | ngOnDestroy () { | |
69 | if (this.routeSub) this.routeSub.unsubscribe() | |
70 | } | |
71 | ||
80bfd33c | 72 | dropdownAnchorClicked (dropdown: NgbDropdown) { |
80bfd33c C |
73 | return dropdown.toggle() |
74 | } | |
75 | ||
d363ef53 K |
76 | openModal (index: number) { |
77 | this.currentMenuEntryIndex = index | |
78 | this.isModalOpened = true | |
79 | ||
80 | this.modalService.open(this.modal, { | |
81 | centered: true, | |
82 | beforeDismiss: async () => { | |
83 | this.onModalDismiss() | |
84 | return true | |
85 | } | |
86 | }) | |
87 | } | |
88 | ||
89 | onModalDismiss () { | |
90 | this.isModalOpened = false | |
91 | } | |
92 | ||
93 | dismissOtherModals () { | |
94 | this.modalService.dismissAll() | |
95 | } | |
96 | ||
dfe3f7b7 K |
97 | isDisplayed (obj: { isDisplayed?: () => boolean }) { |
98 | if (typeof obj.isDisplayed !== 'function') return true | |
99 | ||
100 | return obj.isDisplayed() | |
101 | } | |
102 | ||
ddb83e49 C |
103 | private updateChildLabels (path: string) { |
104 | this.suffixLabels = {} | |
105 | ||
106 | for (const entry of this.menuEntries) { | |
107 | if (!entry.children) continue | |
108 | ||
109 | for (const child of entry.children) { | |
110 | if (path.startsWith(child.routerLink)) { | |
111 | this.suffixLabels[entry.label] = child.label | |
112 | } | |
113 | } | |
114 | } | |
115 | } | |
116 | } |