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'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
13 templateUrl: './app.component.html',
14 styleUrls: [ './app.component.scss' ]
16 export class AppComponent implements OnInit {
17 notificationOptions = {
23 showProgressBar: false,
25 preventDuplicates: false,
26 preventLastDuplicates: 'visible',
30 isMenuDisplayed = true
36 private router: Router,
37 private authService: AuthService,
38 private serverService: ServerService,
39 private domSanitizer: DomSanitizer,
40 private redirectService: RedirectService,
41 private screenService: ScreenService,
42 private hotkeysService: HotkeysService,
43 private themeService: ThemeService
46 get serverVersion () {
47 return this.serverService.getConfig().serverVersion
51 const commit = this.serverService.getConfig().serverCommit || ''
52 return (commit !== '') ? '...' + commit : commit
56 return this.serverService.getConfig().instance.name
60 return RedirectService.DEFAULT_ROUTE
64 document.getElementById('incompatible-browser').className += ' browser-ok'
66 this.router.events.subscribe(e => {
67 if (e instanceof NavigationEnd) {
68 const pathname = window.location.pathname
69 if (!pathname || pathname === '/' || is18nPath(pathname)) {
70 this.redirectService.redirectToHomepage(true)
75 this.authService.loadClientCredentials()
77 if (this.isUserLoggedIn()) {
78 // The service will automatically redirect to the login page if the token is not valid anymore
79 this.authService.refreshUserInformation()
82 // Load custom data from server
83 this.serverService.loadConfig()
84 this.serverService.loadVideoCategories()
85 this.serverService.loadVideoLanguages()
86 this.serverService.loadVideoLicences()
87 this.serverService.loadVideoPrivacies()
89 // Do not display menu on small screens
90 if (this.screenService.isInSmallView()) {
91 this.isMenuDisplayed = false
94 this.router.events.subscribe(
96 // User clicked on a link in the menu, change the page
97 if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) {
98 this.isMenuDisplayed = false
104 this.serverService.configLoaded
106 const config = this.serverService.getConfig()
108 if (config.instance.customizations.javascript) {
110 // tslint:disable:no-eval
111 eval(config.instance.customizations.javascript)
113 console.error('Cannot eval custom JavaScript.', err)
118 // Inject CSS if modified (admin config settings)
119 this.serverService.configLoaded
120 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
122 const headStyle = document.querySelector('style.custom-css-style')
123 if (headStyle) headStyle.parentNode.removeChild(headStyle)
125 const config = this.serverService.getConfig()
127 // We test customCSS if the admin removed the css
128 if (this.customCSS || config.instance.customizations.css) {
129 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
130 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
134 this.hotkeysService.add([
135 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
136 document.getElementById('search-video').focus()
138 }, undefined, this.i18n('Focus the search bar')),
139 new Hotkey('b', (event: KeyboardEvent): boolean => {
142 }, undefined, this.i18n('Toggle the left menu')),
143 new Hotkey('g o', (event: KeyboardEvent): boolean => {
144 this.router.navigate([ '/videos/overview' ])
146 }, undefined, this.i18n('Go to the videos overview page')),
147 new Hotkey('g t', (event: KeyboardEvent): boolean => {
148 this.router.navigate([ '/videos/trending' ])
150 }, undefined, this.i18n('Go to the trending videos page')),
151 new Hotkey('g r', (event: KeyboardEvent): boolean => {
152 this.router.navigate([ '/videos/recently-added' ])
154 }, undefined, this.i18n('Go to the recently added videos page')),
155 new Hotkey('g l', (event: KeyboardEvent): boolean => {
156 this.router.navigate([ '/videos/local' ])
158 }, undefined, this.i18n('Go to the local videos page')),
159 new Hotkey('g u', (event: KeyboardEvent): boolean => {
160 this.router.navigate([ '/videos/upload' ])
162 }, undefined, this.i18n('Go to the videos upload page')),
163 new Hotkey('shift+t', (event: KeyboardEvent): boolean => {
164 this.themeService.toggleDarkTheme()
166 }, undefined, this.i18n('Toggle Dark theme'))
171 return this.authService.isLoggedIn()
175 this.isMenuDisplayed = !this.isMenuDisplayed