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