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'
15 import { POP_STATE_MODAL_DISMISS } from '@app/shared/misc/constants'
19 templateUrl: './app.component.html',
20 styleUrls: [ './app.component.scss' ]
22 export class AppComponent implements OnInit {
23 isMenuDisplayed = true
24 isMenuChangedByUser = false
30 private viewportScroller: ViewportScroller,
31 private router: Router,
32 private authService: AuthService,
33 private serverService: ServerService,
34 private pluginService: PluginService,
35 private domSanitizer: DomSanitizer,
36 private redirectService: RedirectService,
37 private screenService: ScreenService,
38 private hotkeysService: HotkeysService,
39 private themeService: ThemeService,
40 private hooks: HooksService,
41 private location: PlatformLocation,
42 private modalService: NgbModal
45 get serverVersion () {
46 return this.serverService.getConfig().serverVersion
50 const commit = this.serverService.getConfig().serverCommit || ''
51 return (commit !== '') ? '...' + commit : commit
55 return this.serverService.getConfig().instance.name
59 return RedirectService.DEFAULT_ROUTE
63 document.getElementById('incompatible-browser').className += ' browser-ok'
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()
78 this.serverService.loadVideoPlaylistPrivacies()
81 this.themeService.initialize()
83 // Do not display menu on small screens
84 if (this.screenService.isInSmallView()) {
85 this.isMenuDisplayed = false
88 this.initRouteEvents()
94 fromEvent(window, 'resize')
95 .pipe(debounceTime(200))
96 .subscribe(() => this.onResize())
98 this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS))
102 return this.authService.isLoggedIn()
106 this.isMenuDisplayed = !this.isMenuDisplayed
107 this.isMenuChangedByUser = true
111 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser
114 private initRouteEvents () {
115 let resetScroll = true
116 const eventsObs = this.router.events
118 const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll))
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 const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd))
136 // When we add the a-state parameter, we don't want to alter the scroll
137 navigationEndEvent.pipe(pairwise())
138 .subscribe(([ e1, e2 ]) => {
142 const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects)
143 const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects)
145 if (previousUrl.pathname !== nextUrl.pathname) {
150 const nextSearchParams = nextUrl.searchParams
151 nextSearchParams.delete('a-state')
153 const previousSearchParams = previousUrl.searchParams
155 nextSearchParams.sort()
156 previousSearchParams.sort()
158 if (nextSearchParams.toString() !== previousSearchParams.toString()) {
162 console.error('Cannot parse URL to check next scroll.', e)
167 navigationEndEvent.pipe(
168 map(() => window.location.pathname),
169 filter(pathname => !pathname || pathname === '/' || is18nPath(pathname))
170 ).subscribe(() => this.redirectService.redirectToHomepage(true))
172 navigationEndEvent.subscribe(e => {
173 this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url })
177 filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart),
178 filter(() => this.screenService.isInSmallView())
179 ).subscribe(() => this.isMenuDisplayed = false) // User clicked on a link in the menu, change the page
182 private injectJS () {
184 this.serverService.configLoaded
186 const config = this.serverService.getConfig()
188 if (config.instance.customizations.javascript) {
190 // tslint:disable:no-eval
191 eval(config.instance.customizations.javascript)
193 console.error('Cannot eval custom JavaScript.', err)
199 private injectCSS () {
200 // Inject CSS if modified (admin config settings)
201 this.serverService.configLoaded
202 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
204 const headStyle = document.querySelector('style.custom-css-style')
205 if (headStyle) headStyle.parentNode.removeChild(headStyle)
207 const config = this.serverService.getConfig()
209 // We test customCSS if the admin removed the css
210 if (this.customCSS || config.instance.customizations.css) {
211 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
212 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
217 private async loadPlugins () {
218 this.pluginService.initializePlugins()
220 this.hooks.runAction('action:application.init', 'common')
223 private initHotkeys () {
224 this.hotkeysService.add([
225 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
226 document.getElementById('search-video').focus()
228 }, undefined, this.i18n('Focus the search bar')),
229 new Hotkey('b', (event: KeyboardEvent): boolean => {
232 }, undefined, this.i18n('Toggle the left menu')),
233 new Hotkey('g o', (event: KeyboardEvent): boolean => {
234 this.router.navigate([ '/videos/overview' ])
236 }, undefined, this.i18n('Go to the discover videos page')),
237 new Hotkey('g t', (event: KeyboardEvent): boolean => {
238 this.router.navigate([ '/videos/trending' ])
240 }, undefined, this.i18n('Go to the trending videos page')),
241 new Hotkey('g r', (event: KeyboardEvent): boolean => {
242 this.router.navigate([ '/videos/recently-added' ])
244 }, undefined, this.i18n('Go to the recently added videos page')),
245 new Hotkey('g l', (event: KeyboardEvent): boolean => {
246 this.router.navigate([ '/videos/local' ])
248 }, undefined, this.i18n('Go to the local videos page')),
249 new Hotkey('g u', (event: KeyboardEvent): boolean => {
250 this.router.navigate([ '/videos/upload' ])
252 }, undefined, this.i18n('Go to the videos upload page'))