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