]>
Commit | Line | Data |
---|---|---|
43d0ea7f | 1 | import { Component, OnInit, ViewChild } from '@angular/core' |
00b5556c | 2 | import { DomSanitizer, SafeHtml } from '@angular/platform-browser' |
489290b8 | 3 | import { Event, GuardsCheckStart, NavigationEnd, Router, Scroll } 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' |
41413133 | 7 | import { debounceTime, filter, first, map, pairwise, skip, switchMap } from 'rxjs/operators' |
489290b8 | 8 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
e33f888b | 9 | import { I18n } from '@ngx-translate/i18n-polyfill' |
a5858241 | 10 | import { fromEvent } from 'rxjs' |
4334445d | 11 | import { PlatformLocation, ViewportScroller } from '@angular/common' |
18a6f04c | 12 | import { PluginService } from '@app/core/plugins/plugin.service' |
93cae479 | 13 | import { HooksService } from '@app/core/plugins/hooks.service' |
4334445d | 14 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
60c2bc80 | 15 | import { POP_STATE_MODAL_DISMISS } from '@app/shared/misc/constants' |
43d0ea7f C |
16 | import { WelcomeModalComponent } from '@app/modal/welcome-modal.component' |
17 | import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component' | |
18 | import { UserRole } from '@shared/models' | |
19 | import { User } from '@app/shared' | |
20 | import { InstanceService } from '@app/shared/instance/instance.service' | |
e2a2d6c8 | 21 | |
dc8bc31b | 22 | @Component({ |
3154f382 C |
23 | selector: 'my-app', |
24 | templateUrl: './app.component.html', | |
25 | styleUrls: [ './app.component.scss' ] | |
dc8bc31b | 26 | }) |
e2a2d6c8 | 27 | export class AppComponent implements OnInit { |
43d0ea7f C |
28 | @ViewChild('welcomeModal', { static: false }) welcomeModal: WelcomeModalComponent |
29 | @ViewChild('instanceConfigWarningModal', { static: false }) instanceConfigWarningModal: InstanceConfigWarningModalComponent | |
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, |
489290b8 | 38 | private viewportScroller: ViewportScroller, |
3154f382 | 39 | private router: Router, |
e2a2d6c8 | 40 | private authService: AuthService, |
00b5556c | 41 | private serverService: ServerService, |
18a6f04c | 42 | private pluginService: PluginService, |
43d0ea7f | 43 | private instanceService: InstanceService, |
901637bb | 44 | private domSanitizer: DomSanitizer, |
bbe0f064 | 45 | private redirectService: RedirectService, |
ee1fc23a | 46 | private screenService: ScreenService, |
1a00c561 | 47 | private hotkeysService: HotkeysService, |
93cae479 | 48 | private themeService: ThemeService, |
4334445d C |
49 | private hooks: HooksService, |
50 | private location: PlatformLocation, | |
51 | private modalService: NgbModal | |
989e526a | 52 | ) { } |
a99593ed | 53 | |
36f9424f C |
54 | get instanceName () { |
55 | return this.serverService.getConfig().instance.name | |
56 | } | |
57 | ||
29f9b562 C |
58 | get defaultRoute () { |
59 | return RedirectService.DEFAULT_ROUTE | |
60 | } | |
61 | ||
df98563e | 62 | ngOnInit () { |
b3eeb529 | 63 | document.getElementById('incompatible-browser').className += ' browser-ok' |
73e09f27 | 64 | |
a2ffd046 C |
65 | this.loadPlugins() |
66 | this.themeService.initialize() | |
67 | ||
d592e0a9 C |
68 | this.authService.loadClientCredentials() |
69 | ||
d414207f | 70 | if (this.isUserLoggedIn()) { |
e2a2d6c8 | 71 | // The service will automatically redirect to the login page if the token is not valid anymore |
bcd9f81e | 72 | this.authService.refreshUserInformation() |
e2a2d6c8 | 73 | } |
6e07c3de | 74 | |
db7af09b C |
75 | // Load custom data from server |
76 | this.serverService.loadConfig() | |
77 | this.serverService.loadVideoCategories() | |
78 | this.serverService.loadVideoLanguages() | |
79 | this.serverService.loadVideoLicences() | |
fd45e8f4 | 80 | this.serverService.loadVideoPrivacies() |
830b4faf | 81 | this.serverService.loadVideoPlaylistPrivacies() |
3eeeb87f C |
82 | |
83 | // Do not display menu on small screens | |
bbe0f064 | 84 | if (this.screenService.isInSmallView()) { |
df98563e | 85 | this.isMenuDisplayed = false |
3eeeb87f | 86 | } |
c8cf5952 | 87 | |
489290b8 C |
88 | this.initRouteEvents() |
89 | this.injectJS() | |
90 | this.injectCSS() | |
91 | ||
92 | this.initHotkeys() | |
93 | ||
94 | fromEvent(window, 'resize') | |
95 | .pipe(debounceTime(200)) | |
96 | .subscribe(() => this.onResize()) | |
4334445d | 97 | |
60c2bc80 | 98 | this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS)) |
43d0ea7f C |
99 | |
100 | this.openModalsIfNeeded() | |
489290b8 C |
101 | } |
102 | ||
103 | isUserLoggedIn () { | |
104 | return this.authService.isLoggedIn() | |
105 | } | |
106 | ||
107 | toggleMenu () { | |
108 | this.isMenuDisplayed = !this.isMenuDisplayed | |
109 | this.isMenuChangedByUser = true | |
110 | } | |
111 | ||
112 | onResize () { | |
113 | this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser | |
114 | } | |
115 | ||
dbdf2d51 C |
116 | getServerVersionAndCommit () { |
117 | return this.serverService.getServerVersionAndCommit() | |
118 | } | |
119 | ||
489290b8 C |
120 | private initRouteEvents () { |
121 | let resetScroll = true | |
122 | const eventsObs = this.router.events | |
123 | ||
124 | const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)) | |
489290b8 C |
125 | |
126 | scrollEvent.subscribe(e => { | |
127 | if (e.position) { | |
128 | return this.viewportScroller.scrollToPosition(e.position) | |
c8cf5952 | 129 | } |
00b5556c | 130 | |
489290b8 C |
131 | if (e.anchor) { |
132 | return this.viewportScroller.scrollToAnchor(e.anchor) | |
133 | } | |
134 | ||
135 | if (resetScroll) { | |
136 | return this.viewportScroller.scrollToPosition([ 0, 0 ]) | |
137 | } | |
138 | }) | |
139 | ||
60c2bc80 C |
140 | const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd)) |
141 | ||
489290b8 C |
142 | // When we add the a-state parameter, we don't want to alter the scroll |
143 | navigationEndEvent.pipe(pairwise()) | |
144 | .subscribe(([ e1, e2 ]) => { | |
145 | try { | |
146 | resetScroll = false | |
147 | ||
722bca90 C |
148 | const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects) |
149 | const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects) | |
489290b8 C |
150 | |
151 | if (previousUrl.pathname !== nextUrl.pathname) { | |
152 | resetScroll = true | |
153 | return | |
154 | } | |
155 | ||
156 | const nextSearchParams = nextUrl.searchParams | |
157 | nextSearchParams.delete('a-state') | |
158 | ||
159 | const previousSearchParams = previousUrl.searchParams | |
160 | ||
161 | nextSearchParams.sort() | |
162 | previousSearchParams.sort() | |
163 | ||
164 | if (nextSearchParams.toString() !== previousSearchParams.toString()) { | |
165 | resetScroll = true | |
166 | } | |
167 | } catch (e) { | |
168 | console.error('Cannot parse URL to check next scroll.', e) | |
169 | resetScroll = true | |
170 | } | |
171 | }) | |
172 | ||
173 | navigationEndEvent.pipe( | |
174 | map(() => window.location.pathname), | |
175 | filter(pathname => !pathname || pathname === '/' || is18nPath(pathname)) | |
176 | ).subscribe(() => this.redirectService.redirectToHomepage(true)) | |
177 | ||
23bdacf8 C |
178 | navigationEndEvent.subscribe(e => { |
179 | this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url }) | |
180 | }) | |
181 | ||
489290b8 C |
182 | eventsObs.pipe( |
183 | filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart), | |
184 | filter(() => this.screenService.isInSmallView()) | |
185 | ).subscribe(() => this.isMenuDisplayed = false) // User clicked on a link in the menu, change the page | |
186 | } | |
187 | ||
188 | private injectJS () { | |
e032aec9 | 189 | // Inject JS |
00b5556c | 190 | this.serverService.configLoaded |
e032aec9 C |
191 | .subscribe(() => { |
192 | const config = this.serverService.getConfig() | |
193 | ||
194 | if (config.instance.customizations.javascript) { | |
195 | try { | |
196 | // tslint:disable:no-eval | |
197 | eval(config.instance.customizations.javascript) | |
198 | } catch (err) { | |
199 | console.error('Cannot eval custom JavaScript.', err) | |
200 | } | |
201 | } | |
202 | }) | |
489290b8 | 203 | } |
00b5556c | 204 | |
489290b8 | 205 | private injectCSS () { |
e032aec9 C |
206 | // Inject CSS if modified (admin config settings) |
207 | this.serverService.configLoaded | |
208 | .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server | |
209 | .subscribe(() => { | |
210 | const headStyle = document.querySelector('style.custom-css-style') | |
211 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
00b5556c | 212 | |
e032aec9 C |
213 | const config = this.serverService.getConfig() |
214 | ||
215 | // We test customCSS if the admin removed the css | |
216 | if (this.customCSS || config.instance.customizations.css) { | |
217 | const styleTag = '<style>' + config.instance.customizations.css + '</style>' | |
218 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
00b5556c | 219 | } |
e032aec9 | 220 | }) |
489290b8 | 221 | } |
ee1fc23a | 222 | |
18a6f04c C |
223 | private async loadPlugins () { |
224 | this.pluginService.initializePlugins() | |
225 | ||
c9e3eeed | 226 | this.hooks.runAction('action:application.init', 'common') |
18a6f04c C |
227 | } |
228 | ||
43d0ea7f C |
229 | private async openModalsIfNeeded () { |
230 | this.serverService.configLoaded | |
231 | .pipe( | |
41413133 | 232 | first(), |
43d0ea7f C |
233 | switchMap(() => this.authService.userInformationLoaded), |
234 | map(() => this.authService.getUser()), | |
235 | filter(user => user.role === UserRole.ADMINISTRATOR) | |
236 | ).subscribe(user => setTimeout(() => this.openAdminModals(user))) // setTimeout because of ngIf in template | |
237 | } | |
238 | ||
239 | private async openAdminModals (user: User) { | |
240 | if (user.noWelcomeModal !== true) return this.welcomeModal.show() | |
241 | ||
242 | const config = this.serverService.getConfig() | |
589d9f55 C |
243 | if (user.noInstanceConfigWarningModal === true || !config.signup.allowed) return |
244 | ||
245 | this.instanceService.getAbout() | |
246 | .subscribe(about => { | |
247 | if ( | |
248 | config.instance.name.toLowerCase() === 'peertube' || | |
249 | !about.instance.terms || | |
250 | !about.instance.administrator || | |
251 | !about.instance.maintenanceLifetime | |
252 | ) { | |
253 | this.instanceConfigWarningModal.show(about) | |
254 | } | |
255 | }) | |
43d0ea7f C |
256 | } |
257 | ||
489290b8 | 258 | private initHotkeys () { |
ee1fc23a | 259 | this.hotkeysService.add([ |
8542dc33 | 260 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { |
ee1fc23a | 261 | document.getElementById('search-video').focus() |
8542dc33 | 262 | return false |
e33f888b | 263 | }, undefined, this.i18n('Focus the search bar')), |
43d0ea7f | 264 | |
8542dc33 RK |
265 | new Hotkey('b', (event: KeyboardEvent): boolean => { |
266 | this.toggleMenu() | |
267 | return false | |
e33f888b | 268 | }, undefined, this.i18n('Toggle the left menu')), |
43d0ea7f | 269 | |
20d21199 | 270 | new Hotkey('g o', (event: KeyboardEvent): boolean => { |
a54991da RK |
271 | this.router.navigate([ '/videos/overview' ]) |
272 | return false | |
79a89941 | 273 | }, undefined, this.i18n('Go to the discover videos page')), |
43d0ea7f | 274 | |
20d21199 | 275 | new Hotkey('g t', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
276 | this.router.navigate([ '/videos/trending' ]) |
277 | return false | |
e33f888b | 278 | }, undefined, this.i18n('Go to the trending videos page')), |
43d0ea7f | 279 | |
20d21199 | 280 | new Hotkey('g r', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
281 | this.router.navigate([ '/videos/recently-added' ]) |
282 | return false | |
e33f888b | 283 | }, undefined, this.i18n('Go to the recently added videos page')), |
43d0ea7f | 284 | |
20d21199 | 285 | new Hotkey('g l', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
286 | this.router.navigate([ '/videos/local' ]) |
287 | return false | |
e33f888b | 288 | }, undefined, this.i18n('Go to the local videos page')), |
43d0ea7f | 289 | |
20d21199 | 290 | new Hotkey('g u', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
291 | this.router.navigate([ '/videos/upload' ]) |
292 | return false | |
ffb321be | 293 | }, undefined, this.i18n('Go to the videos upload page')) |
ee1fc23a | 294 | ]) |
67167390 | 295 | } |
dc8bc31b | 296 | } |