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