b9f7be48cff867d8def1bddd636c349677082319
[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   get defaultRoute () {
47     return RedirectService.DEFAULT_ROUTE
48   }
49
50   ngOnInit () {
51     document.getElementById('incompatible-browser').className += ' browser-ok'
52
53     const pathname = window.location.pathname
54     if (!pathname || pathname === '/') {
55       this.redirectService.redirectToHomepage()
56     }
57
58     this.authService.loadClientCredentials()
59
60     if (this.authService.isLoggedIn()) {
61       // The service will automatically redirect to the login page if the token is not valid anymore
62       this.authService.refreshUserInformation()
63     }
64
65     // Load custom data from server
66     this.serverService.loadConfig()
67     this.serverService.loadVideoCategories()
68     this.serverService.loadVideoLanguages()
69     this.serverService.loadVideoLicences()
70     this.serverService.loadVideoPrivacies()
71
72     // Do not display menu on small screens
73     if (isInSmallView()) {
74       this.isMenuDisplayed = false
75     }
76
77     this.router.events.subscribe(
78       e => {
79         // User clicked on a link in the menu, change the page
80         if (e instanceof GuardsCheckStart && isInSmallView()) {
81           this.isMenuDisplayed = false
82         }
83       }
84     )
85
86     this.serverService.configLoaded
87       .subscribe(() => {
88         const config = this.serverService.getConfig()
89
90         // We test customCSS in case or the admin removed the css
91         if (this.customCSS || config.instance.customizations.css) {
92           const styleTag = '<style>' + config.instance.customizations.css + '</style>'
93           this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
94         }
95
96         if (config.instance.customizations.javascript) {
97           try {
98             // tslint:disable:no-eval
99             eval(config.instance.customizations.javascript)
100           } catch (err) {
101             console.error('Cannot eval custom JavaScript.', err)
102           }
103         }
104       })
105   }
106
107   toggleMenu () {
108     window.scrollTo(0, 0)
109     this.isMenuDisplayed = !this.isMenuDisplayed
110   }
111
112   getMainColClasses () {
113     // Take all width is the menu is not displayed
114     if (this.isMenuDisplayed === false) return [ 'expanded' ]
115
116     return []
117   }
118 }