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