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