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 const commit = this.serverService.getConfig().serverCommit || ''
50 return (commit !== '') ? '...' + commit : commit
54 return this.serverService.getConfig().instance.name
58 return RedirectService.DEFAULT_ROUTE
62 document.getElementById('incompatible-browser').className += ' browser-ok'
64 this.router.events.subscribe(e => {
65 if (e instanceof NavigationEnd) {
66 const pathname = window.location.pathname
67 if (!pathname || pathname === '/' || is18nPath(pathname)) {
68 this.redirectService.redirectToHomepage(true)
73 this.authService.loadClientCredentials()
75 if (this.isUserLoggedIn()) {
76 // The service will automatically redirect to the login page if the token is not valid anymore
77 this.authService.refreshUserInformation()
80 // Load custom data from server
81 this.serverService.loadConfig()
82 this.serverService.loadVideoCategories()
83 this.serverService.loadVideoLanguages()
84 this.serverService.loadVideoLicences()
85 this.serverService.loadVideoPrivacies()
87 // Do not display menu on small screens
88 if (this.screenService.isInSmallView()) {
89 this.isMenuDisplayed = false
92 this.router.events.subscribe(
94 // User clicked on a link in the menu, change the page
95 if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) {
96 this.isMenuDisplayed = false
102 this.serverService.configLoaded
104 const config = this.serverService.getConfig()
106 if (config.instance.customizations.javascript) {
108 // tslint:disable:no-eval
109 eval(config.instance.customizations.javascript)
111 console.error('Cannot eval custom JavaScript.', err)
116 // Inject CSS if modified (admin config settings)
117 this.serverService.configLoaded
118 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
120 const headStyle = document.querySelector('style.custom-css-style')
121 if (headStyle) headStyle.parentNode.removeChild(headStyle)
123 const config = this.serverService.getConfig()
125 // We test customCSS if the admin removed the css
126 if (this.customCSS || config.instance.customizations.css) {
127 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
128 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
132 this.hotkeysService.add([
133 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
134 document.getElementById('search-video').focus()
136 }, undefined, 'Focus the search bar'),
137 new Hotkey('b', (event: KeyboardEvent): boolean => {
140 }, undefined, 'Toggle the left menu'),
141 new Hotkey('g o', (event: KeyboardEvent): boolean => {
142 this.router.navigate([ '/videos/overview' ])
144 }, undefined, 'Go to the videos overview page'),
145 new Hotkey('g t', (event: KeyboardEvent): boolean => {
146 this.router.navigate([ '/videos/trending' ])
148 }, undefined, 'Go to the trending videos page'),
149 new Hotkey('g r', (event: KeyboardEvent): boolean => {
150 this.router.navigate([ '/videos/recently-added' ])
152 }, undefined, 'Go to the recently added videos page'),
153 new Hotkey('g l', (event: KeyboardEvent): boolean => {
154 this.router.navigate([ '/videos/local' ])
156 }, undefined, 'Go to the local videos page'),
157 new Hotkey('g u', (event: KeyboardEvent): boolean => {
158 this.router.navigate([ '/videos/upload' ])
160 }, undefined, 'Go to the videos upload page'),
161 new Hotkey('shift+t', (event: KeyboardEvent): boolean => {
162 this.themeService.toggleDarkTheme()
164 }, undefined, 'Toggle Dark theme')
169 return this.authService.isLoggedIn()
173 this.isMenuDisplayed = !this.isMenuDisplayed