]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { HotkeysService } from 'angular2-hotkeys' |
dfe3f7b7 K |
2 | import * as debug from 'debug' |
3 | import { switchMap } from 'rxjs/operators' | |
27f4a1ec | 4 | import { ViewportScroller } from '@angular/common' |
8afc19a6 | 5 | import { Component, OnInit, ViewChild } from '@angular/core' |
30d55e75 | 6 | import { Router } from '@angular/router' |
2539932e C |
7 | import { |
8 | AuthService, | |
9 | AuthStatus, | |
10 | AuthUser, | |
11 | MenuLink, | |
12 | MenuService, | |
13 | RedirectService, | |
14 | ScreenService, | |
15 | ServerService, | |
16 | UserService | |
17 | } from '@app/core' | |
27f4a1ec | 18 | import { scrollToTop } from '@app/helpers' |
8afc19a6 | 19 | import { LanguageChooserComponent } from '@app/menu/language-chooser.component' |
d3217560 | 20 | import { QuickSettingsModalComponent } from '@app/modal/quick-settings-modal.component' |
43a3d281 | 21 | import { PeertubeModalService } from '@app/shared/shared-main/peertube-modal/peertube-modal.service' |
27f4a1ec | 22 | import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap' |
2989628b | 23 | import { HTMLServerConfig, ServerConfig, UserRight, VideoConstant } from '@shared/models' |
602eb142 | 24 | |
dfe3f7b7 K |
25 | const logger = debug('peertube:menu:MenuComponent') |
26 | ||
602eb142 C |
27 | @Component({ |
28 | selector: 'my-menu', | |
383bfc83 | 29 | templateUrl: './menu.component.html', |
dfe3f7b7 | 30 | styleUrls: ['./menu.component.scss'] |
602eb142 C |
31 | }) |
32 | export class MenuComponent implements OnInit { | |
f36da21e | 33 | @ViewChild('languageChooserModal', { static: true }) languageChooserModal: LanguageChooserComponent |
d3217560 | 34 | @ViewChild('quickSettingsModal', { static: true }) quickSettingsModal: QuickSettingsModalComponent |
51a83970 | 35 | @ViewChild('dropdown') dropdown: NgbDropdown |
8afc19a6 | 36 | |
dfe3f7b7 | 37 | user: AuthUser |
df98563e | 38 | isLoggedIn: boolean |
d3217560 | 39 | |
954605a8 | 40 | userHasAdminAccess = false |
4a216666 | 41 | helpVisible = false |
954605a8 | 42 | |
111fdc26 | 43 | videoLanguages: string[] = [] |
68f6c87a C |
44 | nsfwPolicy: string |
45 | ||
68f6c87a | 46 | currentInterfaceLanguage: string |
111fdc26 | 47 | |
2539932e C |
48 | commonMenuLinks: MenuLink[] = [] |
49 | ||
111fdc26 | 50 | private languages: VideoConstant<string>[] = [] |
2989628b C |
51 | |
52 | private htmlServerConfig: HTMLServerConfig | |
ba430d75 | 53 | private serverConfig: ServerConfig |
2989628b | 54 | |
dfe3f7b7 | 55 | private routesPerRight: { [role in UserRight]?: string } = { |
954605a8 | 56 | [UserRight.MANAGE_USERS]: '/admin/users', |
4610bc5b | 57 | [UserRight.MANAGE_SERVER_FOLLOW]: '/admin/friends', |
d95d1559 | 58 | [UserRight.MANAGE_ABUSES]: '/admin/moderation/abuses', |
3487330d | 59 | [UserRight.MANAGE_VIDEO_BLACKLIST]: '/admin/moderation/video-blocks', |
ad76628b C |
60 | [UserRight.MANAGE_JOBS]: '/admin/jobs', |
61 | [UserRight.MANAGE_CONFIGURATION]: '/admin/config' | |
954605a8 | 62 | } |
602eb142 C |
63 | |
64 | constructor ( | |
f3081d64 | 65 | private viewportScroller: ViewportScroller, |
602eb142 | 66 | private authService: AuthService, |
d3217560 | 67 | private userService: UserService, |
db7af09b | 68 | private serverService: ServerService, |
8c985ef5 | 69 | private redirectService: RedirectService, |
d3217560 | 70 | private hotkeysService: HotkeysService, |
30d55e75 K |
71 | private screenService: ScreenService, |
72 | private menuService: MenuService, | |
43a3d281 | 73 | private modalService: PeertubeModalService, |
30d55e75 | 74 | private router: Router |
27f4a1ec | 75 | ) { } |
51a83970 K |
76 | |
77 | get isInMobileView () { | |
78 | return this.screenService.isInMobileView() | |
79 | } | |
80 | ||
81 | get dropdownContainer () { | |
27f4a1ec C |
82 | if (this.isInMobileView) return null |
83 | ||
84 | return 'body' as 'body' | |
51a83970 K |
85 | } |
86 | ||
87 | get language () { | |
88 | return this.languageChooserModal.getCurrentLanguage() | |
89 | } | |
ca4b1594 | 90 | |
17119e4a | 91 | get instanceName () { |
2989628b | 92 | return this.htmlServerConfig.instance.name |
17119e4a C |
93 | } |
94 | ||
df98563e | 95 | ngOnInit () { |
2989628b C |
96 | this.htmlServerConfig = this.serverService.getHTMLConfig() |
97 | this.buildMenuLinks() | |
ba430d75 | 98 | |
df98563e | 99 | this.isLoggedIn = this.authService.isLoggedIn() |
dfe3f7b7 K |
100 | if (this.isLoggedIn === true) { |
101 | this.user = this.authService.getUser() | |
68f6c87a C |
102 | |
103 | this.computeNSFWPolicy() | |
dfe3f7b7 K |
104 | this.computeVideosLink() |
105 | } | |
106 | ||
107 | this.computeAdminAccess() | |
602eb142 | 108 | |
68f6c87a C |
109 | this.currentInterfaceLanguage = this.languageChooserModal.getCurrentLanguage() |
110 | ||
602eb142 C |
111 | this.authService.loginChangedSource.subscribe( |
112 | status => { | |
113 | if (status === AuthStatus.LoggedIn) { | |
df98563e | 114 | this.isLoggedIn = true |
b33f657c | 115 | this.user = this.authService.getUser() |
dfe3f7b7 K |
116 | |
117 | this.computeAdminAccess() | |
118 | this.computeVideosLink() | |
119 | ||
120 | logger('Logged in.') | |
602eb142 | 121 | } else if (status === AuthStatus.LoggedOut) { |
df98563e | 122 | this.isLoggedIn = false |
b33f657c | 123 | this.user = undefined |
dfe3f7b7 K |
124 | |
125 | this.computeAdminAccess() | |
126 | ||
127 | logger('Logged out.') | |
602eb142 | 128 | } else { |
df98563e | 129 | console.error('Unknown auth status: ' + status) |
602eb142 C |
130 | } |
131 | } | |
df98563e | 132 | ) |
4a216666 | 133 | |
111fdc26 | 134 | this.hotkeysService.cheatSheetToggle |
dfe3f7b7 | 135 | .subscribe(isOpen => this.helpVisible = isOpen) |
111fdc26 C |
136 | |
137 | this.serverService.getVideoLanguages() | |
dfe3f7b7 K |
138 | .subscribe(languages => { |
139 | this.languages = languages | |
d3217560 | 140 | |
dfe3f7b7 K |
141 | this.authService.userInformationLoaded |
142 | .subscribe(() => this.buildUserLanguages()) | |
143 | }) | |
43a3d281 | 144 | |
145 | this.modalService.openQuickSettingsSubject | |
146 | .subscribe(() => this.openQuickSettings()) | |
d3217560 RK |
147 | } |
148 | ||
291e8d3e | 149 | isRegistrationAllowed () { |
2989628b C |
150 | if (!this.serverConfig) return false |
151 | ||
ba430d75 | 152 | return this.serverConfig.signup.allowed && |
dfe3f7b7 | 153 | this.serverConfig.signup.allowedForCurrentIP |
a184c71b C |
154 | } |
155 | ||
954605a8 C |
156 | getFirstAdminRightAvailable () { |
157 | const user = this.authService.getUser() | |
158 | if (!user) return undefined | |
159 | ||
160 | const adminRights = [ | |
161 | UserRight.MANAGE_USERS, | |
4610bc5b | 162 | UserRight.MANAGE_SERVER_FOLLOW, |
d95d1559 | 163 | UserRight.MANAGE_ABUSES, |
3487330d | 164 | UserRight.MANAGE_VIDEO_BLACKLIST, |
ad76628b C |
165 | UserRight.MANAGE_JOBS, |
166 | UserRight.MANAGE_CONFIGURATION | |
954605a8 C |
167 | ] |
168 | ||
169 | for (const adminRight of adminRights) { | |
170 | if (user.hasRight(adminRight)) { | |
171 | return adminRight | |
172 | } | |
173 | } | |
174 | ||
175 | return undefined | |
176 | } | |
177 | ||
178 | getFirstAdminRouteAvailable () { | |
179 | const right = this.getFirstAdminRightAvailable() | |
180 | ||
181 | return this.routesPerRight[right] | |
602eb142 C |
182 | } |
183 | ||
b33f657c C |
184 | logout (event: Event) { |
185 | event.preventDefault() | |
186 | ||
df98563e | 187 | this.authService.logout() |
602eb142 | 188 | // Redirect to home page |
b1d40cff | 189 | this.redirectService.redirectToHomepage() |
602eb142 | 190 | } |
954605a8 | 191 | |
8afc19a6 C |
192 | openLanguageChooser () { |
193 | this.languageChooserModal.show() | |
194 | } | |
195 | ||
4a216666 RK |
196 | openHotkeysCheatSheet () { |
197 | this.hotkeysService.cheatSheetToggle.next(!this.helpVisible) | |
198 | } | |
199 | ||
d3217560 RK |
200 | openQuickSettings () { |
201 | this.quickSettingsModal.show() | |
202 | } | |
203 | ||
204 | toggleUseP2P () { | |
205 | if (!this.user) return | |
206 | this.user.webTorrentEnabled = !this.user.webTorrentEnabled | |
111fdc26 C |
207 | |
208 | this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled }) | |
dfe3f7b7 | 209 | .subscribe(() => this.authService.refreshUserInformation()) |
d3217560 RK |
210 | } |
211 | ||
8ada87ac | 212 | langForLocale (localeId: string) { |
66357162 | 213 | if (localeId === '_unknown') return $localize`Unknown` |
bb3933ef | 214 | |
111fdc26 C |
215 | return this.languages.find(lang => lang.id === localeId).label |
216 | } | |
217 | ||
f3081d64 | 218 | onActiveLinkScrollToAnchor (link: HTMLAnchorElement) { |
30d55e75 K |
219 | const linkURL = link.getAttribute('href') |
220 | const linkHash = link.getAttribute('fragment') | |
221 | ||
222 | // On same url without fragment restore top scroll position | |
223 | if (!linkHash && this.router.url.includes(linkURL)) { | |
f3081d64 | 224 | scrollToTop('smooth') |
30d55e75 K |
225 | } |
226 | ||
227 | // On same url with fragment restore anchor scroll position | |
228 | if (linkHash && this.router.url === linkURL) { | |
f3081d64 | 229 | this.viewportScroller.scrollToAnchor(linkHash) |
30d55e75 K |
230 | } |
231 | ||
232 | if (this.screenService.isInSmallView()) { | |
233 | this.menuService.toggleMenu() | |
234 | } | |
235 | } | |
236 | ||
51a83970 K |
237 | // Lock menu scroll when menu scroll to avoid fleeing / detached dropdown |
238 | onMenuScrollEvent () { | |
239 | document.querySelector('menu').scrollTo(0, 0) | |
240 | } | |
241 | ||
242 | onDropdownOpenChange (opened: boolean) { | |
243 | if (this.screenService.isInMobileView()) return | |
244 | ||
245 | // Close dropdown when window scroll to avoid dropdown quick jump for re-position | |
246 | const onWindowScroll = () => { | |
56f08761 | 247 | this.dropdown?.close() |
51a83970 K |
248 | window.removeEventListener('scroll', onWindowScroll) |
249 | } | |
250 | ||
251 | if (opened) { | |
252 | window.addEventListener('scroll', onWindowScroll) | |
253 | document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock | |
254 | document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent) | |
255 | } else { | |
256 | document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent) | |
257 | } | |
258 | } | |
259 | ||
2539932e | 260 | private buildMenuLinks () { |
2989628b | 261 | this.commonMenuLinks = this.menuService.buildCommonLinks(this.htmlServerConfig) |
2539932e C |
262 | } |
263 | ||
111fdc26 C |
264 | private buildUserLanguages () { |
265 | if (!this.user) { | |
266 | this.videoLanguages = [] | |
267 | return | |
268 | } | |
269 | ||
270 | if (!this.user.videoLanguages) { | |
66357162 | 271 | this.videoLanguages = [$localize`any language`] |
111fdc26 C |
272 | return |
273 | } | |
274 | ||
275 | this.videoLanguages = this.user.videoLanguages | |
dfe3f7b7 K |
276 | .map(locale => this.langForLocale(locale)) |
277 | .map(value => value === undefined ? '?' : value) | |
d3217560 RK |
278 | } |
279 | ||
dfe3f7b7 | 280 | private computeAdminAccess () { |
954605a8 C |
281 | const right = this.getFirstAdminRightAvailable() |
282 | ||
283 | this.userHasAdminAccess = right !== undefined | |
284 | } | |
dfe3f7b7 K |
285 | |
286 | private computeVideosLink () { | |
287 | this.authService.userInformationLoaded | |
288 | .pipe( | |
289 | switchMap(() => this.user.computeCanSeeVideosLink(this.userService.getMyVideoQuotaUsed())) | |
290 | ).subscribe(res => { | |
291 | if (res === true) logger('User can see videos link.') | |
292 | else logger('User cannot see videos link.') | |
293 | }) | |
294 | } | |
68f6c87a C |
295 | |
296 | private computeNSFWPolicy () { | |
297 | if (!this.user) { | |
298 | this.nsfwPolicy = null | |
299 | return | |
300 | } | |
301 | ||
302 | switch (this.user.nsfwPolicy) { | |
303 | case 'do_not_list': | |
304 | this.nsfwPolicy = $localize`hide` | |
305 | break | |
306 | ||
307 | case 'blur': | |
308 | this.nsfwPolicy = $localize`blur` | |
309 | break | |
310 | ||
311 | case 'display': | |
312 | this.nsfwPolicy = $localize`display` | |
313 | break | |
314 | } | |
315 | } | |
602eb142 | 316 | } |