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