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