1 import { Component, OnInit } from '@angular/core'
2 import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
3 import { Event, GuardsCheckStart, NavigationEnd, Router, Scroll } 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 { debounceTime, filter, map, pairwise, skip } from 'rxjs/operators'
8 import { Hotkey, HotkeysService } from 'angular2-hotkeys'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { fromEvent } from 'rxjs'
11 import { PlatformLocation, ViewportScroller } from '@angular/common'
12 import { PluginService } from '@app/core/plugins/plugin.service'
13 import { HooksService } from '@app/core/plugins/hooks.service'
14 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
18 templateUrl: './app.component.html',
19 styleUrls: [ './app.component.scss' ]
21 export class AppComponent implements OnInit {
22 isMenuDisplayed = true
23 isMenuChangedByUser = false
29 private viewportScroller: ViewportScroller,
30 private router: Router,
31 private authService: AuthService,
32 private serverService: ServerService,
33 private pluginService: PluginService,
34 private domSanitizer: DomSanitizer,
35 private redirectService: RedirectService,
36 private screenService: ScreenService,
37 private hotkeysService: HotkeysService,
38 private themeService: ThemeService,
39 private hooks: HooksService,
40 private location: PlatformLocation,
41 private modalService: NgbModal
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.authService.loadClientCredentials()
66 if (this.isUserLoggedIn()) {
67 // The service will automatically redirect to the login page if the token is not valid anymore
68 this.authService.refreshUserInformation()
71 // Load custom data from server
72 this.serverService.loadConfig()
73 this.serverService.loadVideoCategories()
74 this.serverService.loadVideoLanguages()
75 this.serverService.loadVideoLicences()
76 this.serverService.loadVideoPrivacies()
77 this.serverService.loadVideoPlaylistPrivacies()
80 this.themeService.initialize()
82 // Do not display menu on small screens
83 if (this.screenService.isInSmallView()) {
84 this.isMenuDisplayed = false
87 this.initRouteEvents()
93 fromEvent(window, 'resize')
94 .pipe(debounceTime(200))
95 .subscribe(() => this.onResize())
97 this.location.onPopState(() => this.modalService.dismissAll())
101 return this.authService.isLoggedIn()
105 this.isMenuDisplayed = !this.isMenuDisplayed
106 this.isMenuChangedByUser = true
110 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser
113 private initRouteEvents () {
114 let resetScroll = true
115 const eventsObs = this.router.events
117 const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll))
118 const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd))
120 scrollEvent.subscribe(e => {
122 return this.viewportScroller.scrollToPosition(e.position)
126 return this.viewportScroller.scrollToAnchor(e.anchor)
130 return this.viewportScroller.scrollToPosition([ 0, 0 ])
134 // When we add the a-state parameter, we don't want to alter the scroll
135 navigationEndEvent.pipe(pairwise())
136 .subscribe(([ e1, e2 ]) => {
140 const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects)
141 const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects)
143 if (previousUrl.pathname !== nextUrl.pathname) {
148 const nextSearchParams = nextUrl.searchParams
149 nextSearchParams.delete('a-state')
151 const previousSearchParams = previousUrl.searchParams
153 nextSearchParams.sort()
154 previousSearchParams.sort()
156 if (nextSearchParams.toString() !== previousSearchParams.toString()) {
160 console.error('Cannot parse URL to check next scroll.', e)
165 navigationEndEvent.pipe(
166 map(() => window.location.pathname),
167 filter(pathname => !pathname || pathname === '/' || is18nPath(pathname))
168 ).subscribe(() => this.redirectService.redirectToHomepage(true))
170 navigationEndEvent.subscribe(e => {
171 this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url })
175 filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart),
176 filter(() => this.screenService.isInSmallView())
177 ).subscribe(() => this.isMenuDisplayed = false) // User clicked on a link in the menu, change the page
180 private injectJS () {
182 this.serverService.configLoaded
184 const config = this.serverService.getConfig()
186 if (config.instance.customizations.javascript) {
188 // tslint:disable:no-eval
189 eval(config.instance.customizations.javascript)
191 console.error('Cannot eval custom JavaScript.', err)
197 private injectCSS () {
198 // Inject CSS if modified (admin config settings)
199 this.serverService.configLoaded
200 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
202 const headStyle = document.querySelector('style.custom-css-style')
203 if (headStyle) headStyle.parentNode.removeChild(headStyle)
205 const config = this.serverService.getConfig()
207 // We test customCSS if the admin removed the css
208 if (this.customCSS || config.instance.customizations.css) {
209 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
210 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
215 private async loadPlugins () {
216 this.pluginService.initializePlugins()
218 this.hooks.runAction('action:application.init', 'common')
221 private initHotkeys () {
222 this.hotkeysService.add([
223 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
224 document.getElementById('search-video').focus()
226 }, undefined, this.i18n('Focus the search bar')),
227 new Hotkey('b', (event: KeyboardEvent): boolean => {
230 }, undefined, this.i18n('Toggle the left menu')),
231 new Hotkey('g o', (event: KeyboardEvent): boolean => {
232 this.router.navigate([ '/videos/overview' ])
234 }, undefined, this.i18n('Go to the discover videos page')),
235 new Hotkey('g t', (event: KeyboardEvent): boolean => {
236 this.router.navigate([ '/videos/trending' ])
238 }, undefined, this.i18n('Go to the trending videos page')),
239 new Hotkey('g r', (event: KeyboardEvent): boolean => {
240 this.router.navigate([ '/videos/recently-added' ])
242 }, undefined, this.i18n('Go to the recently added videos page')),
243 new Hotkey('g l', (event: KeyboardEvent): boolean => {
244 this.router.navigate([ '/videos/local' ])
246 }, undefined, this.i18n('Go to the local videos page')),
247 new Hotkey('g u', (event: KeyboardEvent): boolean => {
248 this.router.navigate([ '/videos/upload' ])
250 }, undefined, this.i18n('Go to the videos upload page'))