]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/app.component.ts
Refractor notification service
[github/Chocobozzz/PeerTube.git] / client / src / app / app.component.ts
CommitLineData
fd45e8f4 1import { Component, OnInit } from '@angular/core'
00b5556c 2import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
989e526a 3import { GuardsCheckStart, NavigationEnd, Router } from '@angular/router'
1a00c561 4import { AuthService, RedirectService, ServerService, ThemeService } from '@app/core'
989e526a 5import { is18nPath } from '../../../shared/models/i18n'
bbe0f064 6import { ScreenService } from '@app/shared/misc/screen.service'
a5858241 7import { skip, debounceTime } from 'rxjs/operators'
ee1fc23a 8import { HotkeysService, Hotkey } from 'angular2-hotkeys'
e33f888b 9import { I18n } from '@ngx-translate/i18n-polyfill'
a5858241 10import { fromEvent } from 'rxjs'
e2a2d6c8 11
dc8bc31b 12@Component({
3154f382
C
13 selector: 'my-app',
14 templateUrl: './app.component.html',
15 styleUrls: [ './app.component.scss' ]
dc8bc31b 16})
e2a2d6c8 17export class AppComponent implements OnInit {
df98563e 18 isMenuDisplayed = true
a5858241 19 isMenuChangedByUser = false
67167390 20
00b5556c
C
21 customCSS: SafeHtml
22
df98563e 23 constructor (
e33f888b 24 private i18n: I18n,
3154f382 25 private router: Router,
e2a2d6c8 26 private authService: AuthService,
00b5556c 27 private serverService: ServerService,
901637bb 28 private domSanitizer: DomSanitizer,
bbe0f064 29 private redirectService: RedirectService,
ee1fc23a 30 private screenService: ScreenService,
1a00c561
RK
31 private hotkeysService: HotkeysService,
32 private themeService: ThemeService
989e526a 33 ) { }
a99593ed 34
915c5bbe
C
35 get serverVersion () {
36 return this.serverService.getConfig().serverVersion
37 }
38
abb2c792
RK
39 get serverCommit () {
40 const commit = this.serverService.getConfig().serverCommit || ''
41 return (commit !== '') ? '...' + commit : commit
42 }
43
36f9424f
C
44 get instanceName () {
45 return this.serverService.getConfig().instance.name
46 }
47
29f9b562
C
48 get defaultRoute () {
49 return RedirectService.DEFAULT_ROUTE
50 }
51
df98563e 52 ngOnInit () {
b3eeb529 53 document.getElementById('incompatible-browser').className += ' browser-ok'
73e09f27 54
b851dabf
C
55 this.router.events.subscribe(e => {
56 if (e instanceof NavigationEnd) {
57 const pathname = window.location.pathname
989e526a 58 if (!pathname || pathname === '/' || is18nPath(pathname)) {
7cf26f43 59 this.redirectService.redirectToHomepage(true)
b851dabf
C
60 }
61 }
62 })
901637bb 63
d592e0a9
C
64 this.authService.loadClientCredentials()
65
d414207f 66 if (this.isUserLoggedIn()) {
e2a2d6c8 67 // The service will automatically redirect to the login page if the token is not valid anymore
bcd9f81e 68 this.authService.refreshUserInformation()
e2a2d6c8 69 }
6e07c3de 70
db7af09b
C
71 // Load custom data from server
72 this.serverService.loadConfig()
73 this.serverService.loadVideoCategories()
74 this.serverService.loadVideoLanguages()
75 this.serverService.loadVideoLicences()
fd45e8f4 76 this.serverService.loadVideoPrivacies()
3eeeb87f
C
77
78 // Do not display menu on small screens
bbe0f064 79 if (this.screenService.isInSmallView()) {
df98563e 80 this.isMenuDisplayed = false
3eeeb87f 81 }
c8cf5952
C
82
83 this.router.events.subscribe(
84 e => {
85 // User clicked on a link in the menu, change the page
bbe0f064 86 if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) {
c8cf5952
C
87 this.isMenuDisplayed = false
88 }
89 }
90 )
00b5556c 91
e032aec9 92 // Inject JS
00b5556c 93 this.serverService.configLoaded
e032aec9
C
94 .subscribe(() => {
95 const config = this.serverService.getConfig()
96
97 if (config.instance.customizations.javascript) {
98 try {
99 // tslint:disable:no-eval
100 eval(config.instance.customizations.javascript)
101 } catch (err) {
102 console.error('Cannot eval custom JavaScript.', err)
103 }
104 }
105 })
00b5556c 106
e032aec9
C
107 // Inject CSS if modified (admin config settings)
108 this.serverService.configLoaded
109 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
110 .subscribe(() => {
111 const headStyle = document.querySelector('style.custom-css-style')
112 if (headStyle) headStyle.parentNode.removeChild(headStyle)
00b5556c 113
e032aec9
C
114 const config = this.serverService.getConfig()
115
116 // We test customCSS if the admin removed the css
117 if (this.customCSS || config.instance.customizations.css) {
118 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
119 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
00b5556c 120 }
e032aec9 121 })
ee1fc23a
RK
122
123 this.hotkeysService.add([
8542dc33 124 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
ee1fc23a 125 document.getElementById('search-video').focus()
8542dc33 126 return false
e33f888b 127 }, undefined, this.i18n('Focus the search bar')),
8542dc33
RK
128 new Hotkey('b', (event: KeyboardEvent): boolean => {
129 this.toggleMenu()
130 return false
e33f888b 131 }, undefined, this.i18n('Toggle the left menu')),
20d21199 132 new Hotkey('g o', (event: KeyboardEvent): boolean => {
a54991da
RK
133 this.router.navigate([ '/videos/overview' ])
134 return false
e33f888b 135 }, undefined, this.i18n('Go to the videos overview page')),
20d21199 136 new Hotkey('g t', (event: KeyboardEvent): boolean => {
ee1fc23a
RK
137 this.router.navigate([ '/videos/trending' ])
138 return false
e33f888b 139 }, undefined, this.i18n('Go to the trending videos page')),
20d21199 140 new Hotkey('g r', (event: KeyboardEvent): boolean => {
ee1fc23a
RK
141 this.router.navigate([ '/videos/recently-added' ])
142 return false
e33f888b 143 }, undefined, this.i18n('Go to the recently added videos page')),
20d21199 144 new Hotkey('g l', (event: KeyboardEvent): boolean => {
ee1fc23a
RK
145 this.router.navigate([ '/videos/local' ])
146 return false
e33f888b 147 }, undefined, this.i18n('Go to the local videos page')),
20d21199 148 new Hotkey('g u', (event: KeyboardEvent): boolean => {
ee1fc23a
RK
149 this.router.navigate([ '/videos/upload' ])
150 return false
e33f888b 151 }, undefined, this.i18n('Go to the videos upload page')),
a157b3a3 152 new Hotkey('shift+t', (event: KeyboardEvent): boolean => {
1a00c561
RK
153 this.themeService.toggleDarkTheme()
154 return false
e33f888b 155 }, undefined, this.i18n('Toggle Dark theme'))
ee1fc23a 156 ])
a5858241
B
157
158 fromEvent(window, 'resize')
159 .pipe(debounceTime(200))
160 .subscribe(() => this.onResize())
e2a2d6c8
C
161 }
162
d414207f
C
163 isUserLoggedIn () {
164 return this.authService.isLoggedIn()
165 }
166
df98563e
C
167 toggleMenu () {
168 this.isMenuDisplayed = !this.isMenuDisplayed
a5858241
B
169 this.isMenuChangedByUser = true
170 }
171
172 onResize () {
173 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser
67167390 174 }
dc8bc31b 175}