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