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 { is18nPath } from '../../../shared/models/i18n'
6 import { ScreenService } from '@app/shared/misc/screen.service'
7 import { skip } from 'rxjs/operators'
11 templateUrl: './app.component.html',
12 styleUrls: [ './app.component.scss' ]
14 export class AppComponent implements OnInit {
15 notificationOptions = {
21 showProgressBar: false,
23 preventDuplicates: false,
24 preventLastDuplicates: 'visible',
28 isMenuDisplayed = true
33 private router: Router,
34 private authService: AuthService,
35 private serverService: ServerService,
36 private domSanitizer: DomSanitizer,
37 private redirectService: RedirectService,
38 private screenService: ScreenService
41 get serverVersion () {
42 return this.serverService.getConfig().serverVersion
46 return this.serverService.getConfig().instance.name
50 return RedirectService.DEFAULT_ROUTE
54 document.getElementById('incompatible-browser').className += ' browser-ok'
56 this.router.events.subscribe(e => {
57 if (e instanceof NavigationEnd) {
58 const pathname = window.location.pathname
59 if (!pathname || pathname === '/' || is18nPath(pathname)) {
60 this.redirectService.redirectToHomepage(true)
65 this.authService.loadClientCredentials()
67 if (this.isUserLoggedIn()) {
68 // The service will automatically redirect to the login page if the token is not valid anymore
69 this.authService.refreshUserInformation()
72 // Load custom data from server
73 this.serverService.loadConfig()
74 this.serverService.loadVideoCategories()
75 this.serverService.loadVideoLanguages()
76 this.serverService.loadVideoLicences()
77 this.serverService.loadVideoPrivacies()
79 // Do not display menu on small screens
80 if (this.screenService.isInSmallView()) {
81 this.isMenuDisplayed = false
84 this.router.events.subscribe(
86 // User clicked on a link in the menu, change the page
87 if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) {
88 this.isMenuDisplayed = false
94 this.serverService.configLoaded
96 const config = this.serverService.getConfig()
98 if (config.instance.customizations.javascript) {
100 // tslint:disable:no-eval
101 eval(config.instance.customizations.javascript)
103 console.error('Cannot eval custom JavaScript.', err)
108 // Inject CSS if modified (admin config settings)
109 this.serverService.configLoaded
110 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
112 const headStyle = document.querySelector('style.custom-css-style')
113 if (headStyle) headStyle.parentNode.removeChild(headStyle)
115 const config = this.serverService.getConfig()
117 // We test customCSS if the admin removed the css
118 if (this.customCSS || config.instance.customizations.css) {
119 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
120 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
126 return this.authService.isLoggedIn()
130 this.isMenuDisplayed = !this.isMenuDisplayed