]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
2 | import { concat } from 'rxjs' | |
6b88559b | 3 | import { filter, first, map, pairwise } from 'rxjs/operators' |
67ed6552 C |
4 | import { DOCUMENT, PlatformLocation, ViewportScroller } from '@angular/common' |
5 | import { AfterViewInit, Component, Inject, LOCALE_ID, OnInit, ViewChild } from '@angular/core' | |
00b5556c | 6 | import { DomSanitizer, SafeHtml } from '@angular/platform-browser' |
489290b8 | 7 | import { Event, GuardsCheckStart, NavigationEnd, Router, Scroll } from '@angular/router' |
67ed6552 | 8 | import { AuthService, MarkdownService, RedirectService, ScreenService, ServerService, ThemeService, User } from '@app/core' |
93cae479 | 9 | import { HooksService } from '@app/core/plugins/hooks.service' |
67ed6552 | 10 | import { PluginService } from '@app/core/plugins/plugin.service' |
437e8e06 | 11 | import { CustomModalComponent } from '@app/modal/custom-modal.component' |
67ed6552 C |
12 | import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component' |
13 | import { WelcomeModalComponent } from '@app/modal/welcome-modal.component' | |
4f926722 | 14 | import { NgbConfig, NgbModal } from '@ng-bootstrap/ng-bootstrap' |
66357162 | 15 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' |
bd45d503 C |
16 | import { getShortLocale, is18nPath } from '@shared/core-utils/i18n' |
17 | import { BroadcastMessageLevel, ServerConfig, UserRole } from '@shared/models' | |
3b20bdd6 | 18 | import { MenuService } from './core/menu/menu.service' |
4504f09f | 19 | import { POP_STATE_MODAL_DISMISS } from './helpers' |
67ed6552 | 20 | import { InstanceService } from './shared/shared-instance' |
e2a2d6c8 | 21 | |
dc8bc31b | 22 | @Component({ |
3154f382 C |
23 | selector: 'my-app', |
24 | templateUrl: './app.component.html', | |
25 | styleUrls: [ './app.component.scss' ] | |
dc8bc31b | 26 | }) |
437e8e06 | 27 | export class AppComponent implements OnInit, AfterViewInit { |
72c33e71 C |
28 | private static BROADCAST_MESSAGE_KEY = 'app-broadcast-message-dismissed' |
29 | ||
2f5d2ec5 C |
30 | @ViewChild('welcomeModal') welcomeModal: WelcomeModalComponent |
31 | @ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent | |
437e8e06 | 32 | @ViewChild('customModal') customModal: CustomModalComponent |
43d0ea7f | 33 | |
00b5556c | 34 | customCSS: SafeHtml |
72c33e71 | 35 | broadcastMessage: { message: string, dismissable: boolean, class: string } | null = null |
00b5556c | 36 | |
ba430d75 C |
37 | private serverConfig: ServerConfig |
38 | ||
df98563e | 39 | constructor ( |
140ea386 | 40 | @Inject(DOCUMENT) private document: Document, |
81fe3c67 | 41 | @Inject(LOCALE_ID) private localeId: string, |
489290b8 | 42 | private viewportScroller: ViewportScroller, |
3154f382 | 43 | private router: Router, |
e2a2d6c8 | 44 | private authService: AuthService, |
00b5556c | 45 | private serverService: ServerService, |
18a6f04c | 46 | private pluginService: PluginService, |
43d0ea7f | 47 | private instanceService: InstanceService, |
901637bb | 48 | private domSanitizer: DomSanitizer, |
bbe0f064 | 49 | private redirectService: RedirectService, |
ee1fc23a | 50 | private screenService: ScreenService, |
1a00c561 | 51 | private hotkeysService: HotkeysService, |
93cae479 | 52 | private themeService: ThemeService, |
4334445d C |
53 | private hooks: HooksService, |
54 | private location: PlatformLocation, | |
3b20bdd6 | 55 | private modalService: NgbModal, |
72c33e71 | 56 | private markdownService: MarkdownService, |
4f926722 | 57 | private ngbConfig: NgbConfig, |
3b20bdd6 | 58 | public menu: MenuService |
4f926722 C |
59 | ) { |
60 | this.ngbConfig.animation = false | |
61 | } | |
a99593ed | 62 | |
36f9424f | 63 | get instanceName () { |
ba430d75 | 64 | return this.serverConfig.instance.name |
36f9424f C |
65 | } |
66 | ||
29f9b562 C |
67 | get defaultRoute () { |
68 | return RedirectService.DEFAULT_ROUTE | |
69 | } | |
70 | ||
df98563e | 71 | ngOnInit () { |
b3eeb529 | 72 | document.getElementById('incompatible-browser').className += ' browser-ok' |
73e09f27 | 73 | |
ba430d75 C |
74 | this.serverConfig = this.serverService.getTmpConfig() |
75 | this.serverService.getConfig() | |
76 | .subscribe(config => this.serverConfig = config) | |
77 | ||
a2ffd046 C |
78 | this.loadPlugins() |
79 | this.themeService.initialize() | |
80 | ||
d592e0a9 C |
81 | this.authService.loadClientCredentials() |
82 | ||
d414207f | 83 | if (this.isUserLoggedIn()) { |
e2a2d6c8 | 84 | // The service will automatically redirect to the login page if the token is not valid anymore |
bcd9f81e | 85 | this.authService.refreshUserInformation() |
e2a2d6c8 | 86 | } |
6e07c3de | 87 | |
489290b8 C |
88 | this.initRouteEvents() |
89 | this.injectJS() | |
90 | this.injectCSS() | |
72c33e71 | 91 | this.injectBroadcastMessage() |
489290b8 C |
92 | |
93 | this.initHotkeys() | |
94 | ||
60c2bc80 | 95 | this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS)) |
43d0ea7f C |
96 | |
97 | this.openModalsIfNeeded() | |
81fe3c67 RK |
98 | |
99 | this.document.documentElement.lang = getShortLocale(this.localeId) | |
489290b8 C |
100 | } |
101 | ||
437e8e06 K |
102 | ngAfterViewInit () { |
103 | this.pluginService.initializeCustomModal(this.customModal) | |
104 | } | |
105 | ||
d95bc702 C |
106 | getToggleTitle () { |
107 | if (this.menu.isDisplayed()) return $localize`Close the left menu` | |
108 | ||
109 | return $localize`Open the left menu` | |
110 | } | |
111 | ||
489290b8 C |
112 | isUserLoggedIn () { |
113 | return this.authService.isLoggedIn() | |
114 | } | |
115 | ||
72c33e71 C |
116 | hideBroadcastMessage () { |
117 | peertubeLocalStorage.setItem(AppComponent.BROADCAST_MESSAGE_KEY, this.serverConfig.broadcastMessage.message) | |
118 | ||
119 | this.broadcastMessage = null | |
7034b3c9 | 120 | this.screenService.isBroadcastMessageDisplayed = false |
72c33e71 C |
121 | } |
122 | ||
489290b8 C |
123 | private initRouteEvents () { |
124 | let resetScroll = true | |
125 | const eventsObs = this.router.events | |
126 | ||
127 | const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)) | |
489290b8 C |
128 | |
129 | scrollEvent.subscribe(e => { | |
4a53fc82 K |
130 | // scrollToAnchor first to preserve anchor position when using history navigation |
131 | if (e.anchor) { | |
132 | setTimeout(() => { | |
f3081d64 | 133 | this.viewportScroller.scrollToAnchor(e.anchor) |
4a53fc82 K |
134 | }) |
135 | ||
136 | return | |
c8cf5952 | 137 | } |
00b5556c | 138 | |
4a53fc82 K |
139 | if (e.position) { |
140 | return this.viewportScroller.scrollToPosition(e.position) | |
489290b8 C |
141 | } |
142 | ||
143 | if (resetScroll) { | |
144 | return this.viewportScroller.scrollToPosition([ 0, 0 ]) | |
145 | } | |
146 | }) | |
147 | ||
60c2bc80 C |
148 | const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd)) |
149 | ||
489290b8 C |
150 | // When we add the a-state parameter, we don't want to alter the scroll |
151 | navigationEndEvent.pipe(pairwise()) | |
152 | .subscribe(([ e1, e2 ]) => { | |
153 | try { | |
154 | resetScroll = false | |
155 | ||
722bca90 C |
156 | const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects) |
157 | const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects) | |
489290b8 C |
158 | |
159 | if (previousUrl.pathname !== nextUrl.pathname) { | |
160 | resetScroll = true | |
161 | return | |
162 | } | |
163 | ||
164 | const nextSearchParams = nextUrl.searchParams | |
165 | nextSearchParams.delete('a-state') | |
166 | ||
167 | const previousSearchParams = previousUrl.searchParams | |
168 | ||
169 | nextSearchParams.sort() | |
170 | previousSearchParams.sort() | |
171 | ||
172 | if (nextSearchParams.toString() !== previousSearchParams.toString()) { | |
173 | resetScroll = true | |
174 | } | |
175 | } catch (e) { | |
176 | console.error('Cannot parse URL to check next scroll.', e) | |
177 | resetScroll = true | |
178 | } | |
179 | }) | |
180 | ||
181 | navigationEndEvent.pipe( | |
182 | map(() => window.location.pathname), | |
183 | filter(pathname => !pathname || pathname === '/' || is18nPath(pathname)) | |
184 | ).subscribe(() => this.redirectService.redirectToHomepage(true)) | |
185 | ||
23bdacf8 C |
186 | navigationEndEvent.subscribe(e => { |
187 | this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url }) | |
188 | }) | |
189 | ||
489290b8 C |
190 | eventsObs.pipe( |
191 | filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart), | |
1bfc7b73 | 192 | filter(() => this.screenService.isInSmallView() || this.screenService.isInTouchScreen()) |
245b9d27 | 193 | ).subscribe(() => this.menu.setMenuDisplay(false)) // User clicked on a link in the menu, change the page |
489290b8 C |
194 | } |
195 | ||
72c33e71 C |
196 | private injectBroadcastMessage () { |
197 | concat( | |
198 | this.serverService.getConfig().pipe(first()), | |
199 | this.serverService.configReloaded | |
200 | ).subscribe(async config => { | |
201 | this.broadcastMessage = null | |
7034b3c9 | 202 | this.screenService.isBroadcastMessageDisplayed = false |
72c33e71 C |
203 | |
204 | const messageConfig = config.broadcastMessage | |
205 | ||
206 | if (messageConfig.enabled) { | |
207 | // Already dismissed this message? | |
208 | if (messageConfig.dismissable && localStorage.getItem(AppComponent.BROADCAST_MESSAGE_KEY) === messageConfig.message) { | |
209 | return | |
210 | } | |
211 | ||
212 | const classes: { [id in BroadcastMessageLevel]: string } = { | |
213 | info: 'alert-info', | |
214 | warning: 'alert-warning', | |
215 | error: 'alert-danger' | |
216 | } | |
217 | ||
218 | this.broadcastMessage = { | |
219 | message: await this.markdownService.completeMarkdownToHTML(messageConfig.message), | |
220 | dismissable: messageConfig.dismissable, | |
221 | class: classes[messageConfig.level] | |
222 | } | |
7034b3c9 | 223 | |
224 | this.screenService.isBroadcastMessageDisplayed = true | |
72c33e71 C |
225 | } |
226 | }) | |
227 | } | |
228 | ||
489290b8 | 229 | private injectJS () { |
e032aec9 | 230 | // Inject JS |
ba430d75 C |
231 | this.serverService.getConfig() |
232 | .subscribe(config => { | |
e032aec9 C |
233 | if (config.instance.customizations.javascript) { |
234 | try { | |
235 | // tslint:disable:no-eval | |
236 | eval(config.instance.customizations.javascript) | |
237 | } catch (err) { | |
238 | console.error('Cannot eval custom JavaScript.', err) | |
239 | } | |
240 | } | |
241 | }) | |
489290b8 | 242 | } |
00b5556c | 243 | |
489290b8 | 244 | private injectCSS () { |
e032aec9 | 245 | // Inject CSS if modified (admin config settings) |
72c33e71 C |
246 | concat( |
247 | this.serverService.getConfig().pipe(first()), | |
248 | this.serverService.configReloaded | |
249 | ).subscribe(config => { | |
250 | const headStyle = document.querySelector('style.custom-css-style') | |
251 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
252 | ||
253 | // We test customCSS if the admin removed the css | |
254 | if (this.customCSS || config.instance.customizations.css) { | |
255 | const styleTag = '<style>' + config.instance.customizations.css + '</style>' | |
256 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
257 | } | |
258 | }) | |
489290b8 | 259 | } |
ee1fc23a | 260 | |
18a6f04c C |
261 | private async loadPlugins () { |
262 | this.pluginService.initializePlugins() | |
263 | ||
c9e3eeed | 264 | this.hooks.runAction('action:application.init', 'common') |
18a6f04c C |
265 | } |
266 | ||
43d0ea7f | 267 | private async openModalsIfNeeded () { |
ba430d75 | 268 | this.authService.userInformationLoaded |
43d0ea7f | 269 | .pipe( |
43d0ea7f C |
270 | map(() => this.authService.getUser()), |
271 | filter(user => user.role === UserRole.ADMINISTRATOR) | |
ba430d75 | 272 | ).subscribe(user => setTimeout(() => this._openAdminModalsIfNeeded(user))) // setTimeout because of ngIf in template |
43d0ea7f C |
273 | } |
274 | ||
ba430d75 | 275 | private async _openAdminModalsIfNeeded (user: User) { |
43d0ea7f C |
276 | if (user.noWelcomeModal !== true) return this.welcomeModal.show() |
277 | ||
ba430d75 | 278 | if (user.noInstanceConfigWarningModal === true || !this.serverConfig.signup.allowed) return |
589d9f55 C |
279 | |
280 | this.instanceService.getAbout() | |
281 | .subscribe(about => { | |
282 | if ( | |
ba430d75 | 283 | this.serverConfig.instance.name.toLowerCase() === 'peertube' || |
589d9f55 C |
284 | !about.instance.terms || |
285 | !about.instance.administrator || | |
286 | !about.instance.maintenanceLifetime | |
287 | ) { | |
288 | this.instanceConfigWarningModal.show(about) | |
289 | } | |
290 | }) | |
43d0ea7f C |
291 | } |
292 | ||
489290b8 | 293 | private initHotkeys () { |
ee1fc23a | 294 | this.hotkeysService.add([ |
8542dc33 | 295 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { |
ee1fc23a | 296 | document.getElementById('search-video').focus() |
8542dc33 | 297 | return false |
66357162 | 298 | }, undefined, $localize`Focus the search bar`), |
43d0ea7f | 299 | |
8542dc33 | 300 | new Hotkey('b', (event: KeyboardEvent): boolean => { |
3b20bdd6 | 301 | this.menu.toggleMenu() |
8542dc33 | 302 | return false |
66357162 | 303 | }, undefined, $localize`Toggle the left menu`), |
43d0ea7f | 304 | |
20d21199 | 305 | new Hotkey('g o', (event: KeyboardEvent): boolean => { |
a54991da RK |
306 | this.router.navigate([ '/videos/overview' ]) |
307 | return false | |
66357162 | 308 | }, undefined, $localize`Go to the discover videos page`), |
43d0ea7f | 309 | |
20d21199 | 310 | new Hotkey('g t', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
311 | this.router.navigate([ '/videos/trending' ]) |
312 | return false | |
66357162 | 313 | }, undefined, $localize`Go to the trending videos page`), |
43d0ea7f | 314 | |
20d21199 | 315 | new Hotkey('g r', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
316 | this.router.navigate([ '/videos/recently-added' ]) |
317 | return false | |
66357162 | 318 | }, undefined, $localize`Go to the recently added videos page`), |
43d0ea7f | 319 | |
20d21199 | 320 | new Hotkey('g l', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
321 | this.router.navigate([ '/videos/local' ]) |
322 | return false | |
66357162 | 323 | }, undefined, $localize`Go to the local videos page`), |
43d0ea7f | 324 | |
20d21199 | 325 | new Hotkey('g u', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
326 | this.router.navigate([ '/videos/upload' ]) |
327 | return false | |
66357162 | 328 | }, undefined, $localize`Go to the videos upload page`) |
ee1fc23a | 329 | ]) |
67167390 | 330 | } |
dc8bc31b | 331 | } |