]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/app.component.ts
Update credits
[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'
489290b8 3import { Event, GuardsCheckStart, NavigationEnd, Router, Scroll } 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'
489290b8
C
7import { debounceTime, filter, map, pairwise, skip } from 'rxjs/operators'
8import { Hotkey, HotkeysService } from 'angular2-hotkeys'
e33f888b 9import { I18n } from '@ngx-translate/i18n-polyfill'
a5858241 10import { fromEvent } from 'rxjs'
489290b8 11import { ViewportScroller } from '@angular/common'
e2a2d6c8 12
dc8bc31b 13@Component({
3154f382
C
14 selector: 'my-app',
15 templateUrl: './app.component.html',
16 styleUrls: [ './app.component.scss' ]
dc8bc31b 17})
e2a2d6c8 18export class AppComponent implements OnInit {
df98563e 19 isMenuDisplayed = true
a5858241 20 isMenuChangedByUser = false
67167390 21
00b5556c
C
22 customCSS: SafeHtml
23
df98563e 24 constructor (
e33f888b 25 private i18n: I18n,
489290b8 26 private viewportScroller: ViewportScroller,
3154f382 27 private router: Router,
e2a2d6c8 28 private authService: AuthService,
00b5556c 29 private serverService: ServerService,
901637bb 30 private domSanitizer: DomSanitizer,
bbe0f064 31 private redirectService: RedirectService,
ee1fc23a 32 private screenService: ScreenService,
1a00c561
RK
33 private hotkeysService: HotkeysService,
34 private themeService: ThemeService
989e526a 35 ) { }
a99593ed 36
915c5bbe
C
37 get serverVersion () {
38 return this.serverService.getConfig().serverVersion
39 }
40
abb2c792
RK
41 get serverCommit () {
42 const commit = this.serverService.getConfig().serverCommit || ''
43 return (commit !== '') ? '...' + commit : commit
44 }
45
36f9424f
C
46 get instanceName () {
47 return this.serverService.getConfig().instance.name
48 }
49
29f9b562
C
50 get defaultRoute () {
51 return RedirectService.DEFAULT_ROUTE
52 }
53
df98563e 54 ngOnInit () {
b3eeb529 55 document.getElementById('incompatible-browser').className += ' browser-ok'
73e09f27 56
d592e0a9
C
57 this.authService.loadClientCredentials()
58
d414207f 59 if (this.isUserLoggedIn()) {
e2a2d6c8 60 // The service will automatically redirect to the login page if the token is not valid anymore
bcd9f81e 61 this.authService.refreshUserInformation()
e2a2d6c8 62 }
6e07c3de 63
db7af09b
C
64 // Load custom data from server
65 this.serverService.loadConfig()
66 this.serverService.loadVideoCategories()
67 this.serverService.loadVideoLanguages()
68 this.serverService.loadVideoLicences()
fd45e8f4 69 this.serverService.loadVideoPrivacies()
830b4faf 70 this.serverService.loadVideoPlaylistPrivacies()
3eeeb87f
C
71
72 // Do not display menu on small screens
bbe0f064 73 if (this.screenService.isInSmallView()) {
df98563e 74 this.isMenuDisplayed = false
3eeeb87f 75 }
c8cf5952 76
489290b8
C
77 this.initRouteEvents()
78 this.injectJS()
79 this.injectCSS()
80
81 this.initHotkeys()
82
83 fromEvent(window, 'resize')
84 .pipe(debounceTime(200))
85 .subscribe(() => this.onResize())
86 }
87
88 isUserLoggedIn () {
89 return this.authService.isLoggedIn()
90 }
91
92 toggleMenu () {
93 this.isMenuDisplayed = !this.isMenuDisplayed
94 this.isMenuChangedByUser = true
95 }
96
97 onResize () {
98 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser
99 }
100
101 private initRouteEvents () {
102 let resetScroll = true
103 const eventsObs = this.router.events
104
105 const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll))
106 const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd))
107
108 scrollEvent.subscribe(e => {
109 if (e.position) {
110 return this.viewportScroller.scrollToPosition(e.position)
c8cf5952 111 }
00b5556c 112
489290b8
C
113 if (e.anchor) {
114 return this.viewportScroller.scrollToAnchor(e.anchor)
115 }
116
117 if (resetScroll) {
118 return this.viewportScroller.scrollToPosition([ 0, 0 ])
119 }
120 })
121
122 // When we add the a-state parameter, we don't want to alter the scroll
123 navigationEndEvent.pipe(pairwise())
124 .subscribe(([ e1, e2 ]) => {
125 try {
126 resetScroll = false
127
128 const previousUrl = new URL(window.location.origin + e1.url)
129 const nextUrl = new URL(window.location.origin + e2.url)
130
131 if (previousUrl.pathname !== nextUrl.pathname) {
132 resetScroll = true
133 return
134 }
135
136 const nextSearchParams = nextUrl.searchParams
137 nextSearchParams.delete('a-state')
138
139 const previousSearchParams = previousUrl.searchParams
140
141 nextSearchParams.sort()
142 previousSearchParams.sort()
143
144 if (nextSearchParams.toString() !== previousSearchParams.toString()) {
145 resetScroll = true
146 }
147 } catch (e) {
148 console.error('Cannot parse URL to check next scroll.', e)
149 resetScroll = true
150 }
151 })
152
153 navigationEndEvent.pipe(
154 map(() => window.location.pathname),
155 filter(pathname => !pathname || pathname === '/' || is18nPath(pathname))
156 ).subscribe(() => this.redirectService.redirectToHomepage(true))
157
158 eventsObs.pipe(
159 filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart),
160 filter(() => this.screenService.isInSmallView())
161 ).subscribe(() => this.isMenuDisplayed = false) // User clicked on a link in the menu, change the page
162 }
163
164 private injectJS () {
e032aec9 165 // Inject JS
00b5556c 166 this.serverService.configLoaded
e032aec9
C
167 .subscribe(() => {
168 const config = this.serverService.getConfig()
169
170 if (config.instance.customizations.javascript) {
171 try {
172 // tslint:disable:no-eval
173 eval(config.instance.customizations.javascript)
174 } catch (err) {
175 console.error('Cannot eval custom JavaScript.', err)
176 }
177 }
178 })
489290b8 179 }
00b5556c 180
489290b8 181 private injectCSS () {
e032aec9
C
182 // Inject CSS if modified (admin config settings)
183 this.serverService.configLoaded
184 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
185 .subscribe(() => {
186 const headStyle = document.querySelector('style.custom-css-style')
187 if (headStyle) headStyle.parentNode.removeChild(headStyle)
00b5556c 188
e032aec9
C
189 const config = this.serverService.getConfig()
190
191 // We test customCSS if the admin removed the css
192 if (this.customCSS || config.instance.customizations.css) {
193 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
194 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
00b5556c 195 }
e032aec9 196 })
489290b8 197 }
ee1fc23a 198
489290b8 199 private initHotkeys () {
ee1fc23a 200 this.hotkeysService.add([
8542dc33 201 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
ee1fc23a 202 document.getElementById('search-video').focus()
8542dc33 203 return false
e33f888b 204 }, undefined, this.i18n('Focus the search bar')),
8542dc33
RK
205 new Hotkey('b', (event: KeyboardEvent): boolean => {
206 this.toggleMenu()
207 return false
e33f888b 208 }, undefined, this.i18n('Toggle the left menu')),
20d21199 209 new Hotkey('g o', (event: KeyboardEvent): boolean => {
a54991da
RK
210 this.router.navigate([ '/videos/overview' ])
211 return false
e33f888b 212 }, undefined, this.i18n('Go to the videos overview page')),
20d21199 213 new Hotkey('g t', (event: KeyboardEvent): boolean => {
ee1fc23a
RK
214 this.router.navigate([ '/videos/trending' ])
215 return false
e33f888b 216 }, undefined, this.i18n('Go to the trending videos page')),
20d21199 217 new Hotkey('g r', (event: KeyboardEvent): boolean => {
ee1fc23a
RK
218 this.router.navigate([ '/videos/recently-added' ])
219 return false
e33f888b 220 }, undefined, this.i18n('Go to the recently added videos page')),
20d21199 221 new Hotkey('g l', (event: KeyboardEvent): boolean => {
ee1fc23a
RK
222 this.router.navigate([ '/videos/local' ])
223 return false
e33f888b 224 }, undefined, this.i18n('Go to the local videos page')),
20d21199 225 new Hotkey('g u', (event: KeyboardEvent): boolean => {
ee1fc23a
RK
226 this.router.navigate([ '/videos/upload' ])
227 return false
e33f888b 228 }, undefined, this.i18n('Go to the videos upload page')),
a157b3a3 229 new Hotkey('shift+t', (event: KeyboardEvent): boolean => {
1a00c561
RK
230 this.themeService.toggleDarkTheme()
231 return false
e33f888b 232 }, undefined, this.i18n('Toggle Dark theme'))
ee1fc23a 233 ])
67167390 234 }
dc8bc31b 235}