]>
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 C |
35 | |
36 | private openedOnHover = false | |
37 | private routeSub: Subscription | |
38 | ||
64545a83 C |
39 | constructor ( |
40 | private router: Router, | |
d363ef53 | 41 | private modalService: NgbModal, |
8544d8f5 K |
42 | private screen: ScreenService, |
43 | private menuService: MenuService | |
d363ef53 K |
44 | ) { } |
45 | ||
46 | get isInSmallView () { | |
8544d8f5 K |
47 | let marginLeft = 0 |
48 | if (this.menuService.isMenuDisplayed) { | |
49 | marginLeft = this.menuService.menuWidth | |
50 | } | |
51 | ||
52 | return this.screen.isInSmallView(marginLeft) | |
d363ef53 | 53 | } |
ddb83e49 C |
54 | |
55 | ngOnInit () { | |
56 | this.updateChildLabels(window.location.pathname) | |
57 | ||
58 | this.routeSub = this.router.events | |
d363ef53 K |
59 | .pipe(filter(event => event instanceof NavigationEnd)) |
60 | .subscribe(() => this.updateChildLabels(window.location.pathname)) | |
a55052c9 C |
61 | |
62 | this.hasIcons = this.menuEntries.some( | |
63 | e => e.children && e.children.some(c => !!c.iconName) | |
64 | ) | |
ddb83e49 C |
65 | } |
66 | ||
67 | ngOnDestroy () { | |
68 | if (this.routeSub) this.routeSub.unsubscribe() | |
69 | } | |
70 | ||
71 | openDropdownOnHover (dropdown: NgbDropdown) { | |
72 | this.openedOnHover = true | |
73 | dropdown.open() | |
74 | ||
75 | // Menu was closed | |
76 | dropdown.openChange | |
77 | .pipe(take(1)) | |
a55052c9 | 78 | .subscribe(() => this.openedOnHover = false) |
ddb83e49 C |
79 | } |
80 | ||
80bfd33c C |
81 | dropdownAnchorClicked (dropdown: NgbDropdown) { |
82 | if (this.openedOnHover) { | |
83 | this.openedOnHover = false | |
84 | return | |
85 | } | |
86 | ||
87 | return dropdown.toggle() | |
88 | } | |
89 | ||
ddb83e49 C |
90 | closeDropdownIfHovered (dropdown: NgbDropdown) { |
91 | if (this.openedOnHover === false) return | |
92 | ||
93 | dropdown.close() | |
94 | this.openedOnHover = false | |
95 | } | |
96 | ||
d363ef53 K |
97 | openModal (index: number) { |
98 | this.currentMenuEntryIndex = index | |
99 | this.isModalOpened = true | |
100 | ||
101 | this.modalService.open(this.modal, { | |
102 | centered: true, | |
103 | beforeDismiss: async () => { | |
104 | this.onModalDismiss() | |
105 | return true | |
106 | } | |
107 | }) | |
108 | } | |
109 | ||
110 | onModalDismiss () { | |
111 | this.isModalOpened = false | |
112 | } | |
113 | ||
114 | dismissOtherModals () { | |
115 | this.modalService.dismissAll() | |
116 | } | |
117 | ||
ddb83e49 C |
118 | private updateChildLabels (path: string) { |
119 | this.suffixLabels = {} | |
120 | ||
121 | for (const entry of this.menuEntries) { | |
122 | if (!entry.children) continue | |
123 | ||
124 | for (const child of entry.children) { | |
125 | if (path.startsWith(child.routerLink)) { | |
126 | this.suffixLabels[entry.label] = child.label | |
127 | } | |
128 | } | |
129 | } | |
130 | } | |
131 | } |