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