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