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, ThemeService } 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'
8 import { HotkeysService, Hotkey } from 'angular2-hotkeys'
12 templateUrl: './app.component.html',
13 styleUrls: [ './app.component.scss' ]
15 export class AppComponent implements OnInit {
16 notificationOptions = {
22 showProgressBar: false,
24 preventDuplicates: false,
25 preventLastDuplicates: 'visible',
29 isMenuDisplayed = true
34 private router: Router,
35 private authService: AuthService,
36 private serverService: ServerService,
37 private domSanitizer: DomSanitizer,
38 private redirectService: RedirectService,
39 private screenService: ScreenService,
40 private hotkeysService: HotkeysService,
41 private themeService: ThemeService
44 get serverVersion () {
45 return this.serverService.getConfig().serverVersion
49 return this.serverService.getConfig().instance.name
53 return RedirectService.DEFAULT_ROUTE
57 document.getElementById('incompatible-browser').className += ' browser-ok'
59 this.router.events.subscribe(e => {
60 if (e instanceof NavigationEnd) {
61 const pathname = window.location.pathname
62 if (!pathname || pathname === '/' || is18nPath(pathname)) {
63 this.redirectService.redirectToHomepage(true)
68 this.authService.loadClientCredentials()
70 if (this.isUserLoggedIn()) {
71 // The service will automatically redirect to the login page if the token is not valid anymore
72 this.authService.refreshUserInformation()
75 // Load custom data from server
76 this.serverService.loadConfig()
77 this.serverService.loadVideoCategories()
78 this.serverService.loadVideoLanguages()
79 this.serverService.loadVideoLicences()
80 this.serverService.loadVideoPrivacies()
82 // Do not display menu on small screens
83 if (this.screenService.isInSmallView()) {
84 this.isMenuDisplayed = false
87 this.router.events.subscribe(
89 // User clicked on a link in the menu, change the page
90 if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) {
91 this.isMenuDisplayed = false
97 this.serverService.configLoaded
99 const config = this.serverService.getConfig()
101 if (config.instance.customizations.javascript) {
103 // tslint:disable:no-eval
104 eval(config.instance.customizations.javascript)
106 console.error('Cannot eval custom JavaScript.', err)
111 // Inject CSS if modified (admin config settings)
112 this.serverService.configLoaded
113 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
115 const headStyle = document.querySelector('style.custom-css-style')
116 if (headStyle) headStyle.parentNode.removeChild(headStyle)
118 const config = this.serverService.getConfig()
120 // We test customCSS if the admin removed the css
121 if (this.customCSS || config.instance.customizations.css) {
122 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
123 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
127 this.hotkeysService.add([
128 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
129 document.getElementById('search-video').focus()
131 }, undefined, 'Focus the search bar'),
132 new Hotkey('b', (event: KeyboardEvent): boolean => {
135 }, undefined, 'Toggle the left menu'),
136 new Hotkey('g o', (event: KeyboardEvent): boolean => {
137 this.router.navigate([ '/videos/overview' ])
139 }, undefined, 'Go to the videos overview page'),
140 new Hotkey('g t', (event: KeyboardEvent): boolean => {
141 this.router.navigate([ '/videos/trending' ])
143 }, undefined, 'Go to the trending videos page'),
144 new Hotkey('g r', (event: KeyboardEvent): boolean => {
145 this.router.navigate([ '/videos/recently-added' ])
147 }, undefined, 'Go to the recently added videos page'),
148 new Hotkey('g l', (event: KeyboardEvent): boolean => {
149 this.router.navigate([ '/videos/local' ])
151 }, undefined, 'Go to the local videos page'),
152 new Hotkey('g u', (event: KeyboardEvent): boolean => {
153 this.router.navigate([ '/videos/upload' ])
155 }, undefined, 'Go to the videos upload page'),
156 new Hotkey('shift+t', (event: KeyboardEvent): boolean => {
157 this.themeService.toggleDarkTheme()
159 }, undefined, 'Toggle Dark theme')
164 return this.authService.isLoggedIn()
168 this.isMenuDisplayed = !this.isMenuDisplayed