]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/app.component.ts
Translate subtitle langs in player
[github/Chocobozzz/PeerTube.git] / client / src / app / app.component.ts
CommitLineData
fd45e8f4 1import { Component, OnInit } from '@angular/core'
00b5556c 2import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
989e526a 3import { GuardsCheckStart, NavigationEnd, Router } from '@angular/router'
901637bb 4import { AuthService, RedirectService, ServerService } from '@app/core'
989e526a 5import { is18nPath } from '../../../shared/models/i18n'
bbe0f064 6import { ScreenService } from '@app/shared/misc/screen.service'
e032aec9 7import { skip } from 'rxjs/operators'
e2a2d6c8 8
dc8bc31b 9@Component({
3154f382
C
10 selector: 'my-app',
11 templateUrl: './app.component.html',
12 styleUrls: [ './app.component.scss' ]
dc8bc31b 13})
e2a2d6c8 14export class AppComponent implements OnInit {
7ddd02c9 15 notificationOptions = {
35bf0c83 16 timeOut: 5000,
7ddd02c9
C
17 lastOnBottom: true,
18 clickToClose: true,
19 maxLength: 0,
20 maxStack: 7,
21 showProgressBar: false,
22 pauseOnHover: false,
23 preventDuplicates: false,
24 preventLastDuplicates: 'visible',
25 rtl: false
df98563e 26 }
7ddd02c9 27
df98563e 28 isMenuDisplayed = true
67167390 29
00b5556c
C
30 customCSS: SafeHtml
31
df98563e 32 constructor (
3154f382 33 private router: Router,
e2a2d6c8 34 private authService: AuthService,
00b5556c 35 private serverService: ServerService,
901637bb 36 private domSanitizer: DomSanitizer,
bbe0f064
C
37 private redirectService: RedirectService,
38 private screenService: ScreenService
989e526a 39 ) { }
a99593ed 40
915c5bbe
C
41 get serverVersion () {
42 return this.serverService.getConfig().serverVersion
43 }
44
36f9424f
C
45 get instanceName () {
46 return this.serverService.getConfig().instance.name
47 }
48
29f9b562
C
49 get defaultRoute () {
50 return RedirectService.DEFAULT_ROUTE
51 }
52
df98563e 53 ngOnInit () {
b3eeb529 54 document.getElementById('incompatible-browser').className += ' browser-ok'
73e09f27 55
b851dabf
C
56 this.router.events.subscribe(e => {
57 if (e instanceof NavigationEnd) {
58 const pathname = window.location.pathname
989e526a 59 if (!pathname || pathname === '/' || is18nPath(pathname)) {
7cf26f43 60 this.redirectService.redirectToHomepage(true)
b851dabf
C
61 }
62 }
63 })
901637bb 64
d592e0a9
C
65 this.authService.loadClientCredentials()
66
d414207f 67 if (this.isUserLoggedIn()) {
e2a2d6c8 68 // The service will automatically redirect to the login page if the token is not valid anymore
bcd9f81e 69 this.authService.refreshUserInformation()
e2a2d6c8 70 }
6e07c3de 71
db7af09b
C
72 // Load custom data from server
73 this.serverService.loadConfig()
74 this.serverService.loadVideoCategories()
75 this.serverService.loadVideoLanguages()
76 this.serverService.loadVideoLicences()
fd45e8f4 77 this.serverService.loadVideoPrivacies()
3eeeb87f
C
78
79 // Do not display menu on small screens
bbe0f064 80 if (this.screenService.isInSmallView()) {
df98563e 81 this.isMenuDisplayed = false
3eeeb87f 82 }
c8cf5952
C
83
84 this.router.events.subscribe(
85 e => {
86 // User clicked on a link in the menu, change the page
bbe0f064 87 if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) {
c8cf5952
C
88 this.isMenuDisplayed = false
89 }
90 }
91 )
00b5556c 92
e032aec9 93 // Inject JS
00b5556c 94 this.serverService.configLoaded
e032aec9
C
95 .subscribe(() => {
96 const config = this.serverService.getConfig()
97
98 if (config.instance.customizations.javascript) {
99 try {
100 // tslint:disable:no-eval
101 eval(config.instance.customizations.javascript)
102 } catch (err) {
103 console.error('Cannot eval custom JavaScript.', err)
104 }
105 }
106 })
00b5556c 107
e032aec9
C
108 // Inject CSS if modified (admin config settings)
109 this.serverService.configLoaded
110 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
111 .subscribe(() => {
112 const headStyle = document.querySelector('style.custom-css-style')
113 if (headStyle) headStyle.parentNode.removeChild(headStyle)
00b5556c 114
e032aec9
C
115 const config = this.serverService.getConfig()
116
117 // We test customCSS if the admin removed the css
118 if (this.customCSS || config.instance.customizations.css) {
119 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
120 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
00b5556c 121 }
e032aec9 122 })
e2a2d6c8
C
123 }
124
d414207f
C
125 isUserLoggedIn () {
126 return this.authService.isLoggedIn()
127 }
128
df98563e
C
129 toggleMenu () {
130 this.isMenuDisplayed = !this.isMenuDisplayed
67167390 131 }
dc8bc31b 132}