Add server localization
[oweals/peertube.git] / client / src / app / app.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
3 import { GuardsCheckStart, NavigationEnd, Router } from '@angular/router'
4 import { AuthService, RedirectService, ServerService } from '@app/core'
5 import { isInSmallView } from '@app/shared/misc/utils'
6 import { is18nPath } from '../../../shared/models/i18n'
7
8 @Component({
9   selector: 'my-app',
10   templateUrl: './app.component.html',
11   styleUrls: [ './app.component.scss' ]
12 })
13 export class AppComponent implements OnInit {
14   notificationOptions = {
15     timeOut: 5000,
16     lastOnBottom: true,
17     clickToClose: true,
18     maxLength: 0,
19     maxStack: 7,
20     showProgressBar: false,
21     pauseOnHover: false,
22     preventDuplicates: false,
23     preventLastDuplicates: 'visible',
24     rtl: false
25   }
26
27   isMenuDisplayed = true
28
29   customCSS: SafeHtml
30
31   constructor (
32     private router: Router,
33     private authService: AuthService,
34     private serverService: ServerService,
35     private domSanitizer: DomSanitizer,
36     private redirectService: RedirectService
37   ) { }
38
39   get serverVersion () {
40     return this.serverService.getConfig().serverVersion
41   }
42
43   get instanceName () {
44     return this.serverService.getConfig().instance.name
45   }
46
47   get defaultRoute () {
48     return RedirectService.DEFAULT_ROUTE
49   }
50
51   ngOnInit () {
52     document.getElementById('incompatible-browser').className += ' browser-ok'
53
54     this.router.events.subscribe(e => {
55       if (e instanceof NavigationEnd) {
56         const pathname = window.location.pathname
57         if (!pathname || pathname === '/' || is18nPath(pathname)) {
58           this.redirectService.redirectToHomepage()
59         }
60       }
61     })
62
63     this.authService.loadClientCredentials()
64
65     if (this.isUserLoggedIn()) {
66       // The service will automatically redirect to the login page if the token is not valid anymore
67       this.authService.refreshUserInformation()
68     }
69
70     // Load custom data from server
71     this.serverService.loadConfig()
72     this.serverService.loadVideoCategories()
73     this.serverService.loadVideoLanguages()
74     this.serverService.loadVideoLicences()
75     this.serverService.loadVideoPrivacies()
76
77     // Do not display menu on small screens
78     if (isInSmallView()) {
79       this.isMenuDisplayed = false
80     }
81
82     this.router.events.subscribe(
83       e => {
84         // User clicked on a link in the menu, change the page
85         if (e instanceof GuardsCheckStart && isInSmallView()) {
86           this.isMenuDisplayed = false
87         }
88       }
89     )
90
91     this.serverService.configLoaded
92       .subscribe(() => {
93         const config = this.serverService.getConfig()
94
95         // We test customCSS if the admin removed the css
96         if (this.customCSS || config.instance.customizations.css) {
97           const styleTag = '<style>' + config.instance.customizations.css + '</style>'
98           this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
99         }
100
101         if (config.instance.customizations.javascript) {
102           try {
103             // tslint:disable:no-eval
104             eval(config.instance.customizations.javascript)
105           } catch (err) {
106             console.error('Cannot eval custom JavaScript.', err)
107           }
108         }
109       })
110   }
111
112   isUserLoggedIn () {
113     return this.authService.isLoggedIn()
114   }
115
116   toggleMenu () {
117     window.scrollTo(0, 0)
118     this.isMenuDisplayed = !this.isMenuDisplayed
119   }
120 }