]>
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' |
9b82d49d | 7 | import { debounceTime, filter, map, pairwise } 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' | |
ba430d75 | 18 | import { ServerConfig, UserRole } from '@shared/models' |
43d0ea7f C |
19 | import { User } from '@app/shared' |
20 | import { InstanceService } from '@app/shared/instance/instance.service' | |
3b20bdd6 | 21 | import { MenuService } from './core/menu/menu.service' |
e2a2d6c8 | 22 | |
dc8bc31b | 23 | @Component({ |
3154f382 C |
24 | selector: 'my-app', |
25 | templateUrl: './app.component.html', | |
26 | styleUrls: [ './app.component.scss' ] | |
dc8bc31b | 27 | }) |
e2a2d6c8 | 28 | export class AppComponent implements OnInit { |
2f5d2ec5 C |
29 | @ViewChild('welcomeModal') welcomeModal: WelcomeModalComponent |
30 | @ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent | |
43d0ea7f | 31 | |
00b5556c C |
32 | customCSS: SafeHtml |
33 | ||
ba430d75 C |
34 | private serverConfig: ServerConfig |
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, | |
3b20bdd6 RK |
51 | private modalService: NgbModal, |
52 | public menu: MenuService | |
989e526a | 53 | ) { } |
a99593ed | 54 | |
36f9424f | 55 | get instanceName () { |
ba430d75 | 56 | return this.serverConfig.instance.name |
36f9424f C |
57 | } |
58 | ||
29f9b562 C |
59 | get defaultRoute () { |
60 | return RedirectService.DEFAULT_ROUTE | |
61 | } | |
62 | ||
df98563e | 63 | ngOnInit () { |
b3eeb529 | 64 | document.getElementById('incompatible-browser').className += ' browser-ok' |
73e09f27 | 65 | |
ba430d75 C |
66 | this.serverConfig = this.serverService.getTmpConfig() |
67 | this.serverService.getConfig() | |
68 | .subscribe(config => this.serverConfig = config) | |
69 | ||
a2ffd046 C |
70 | this.loadPlugins() |
71 | this.themeService.initialize() | |
72 | ||
d592e0a9 C |
73 | this.authService.loadClientCredentials() |
74 | ||
d414207f | 75 | if (this.isUserLoggedIn()) { |
e2a2d6c8 | 76 | // The service will automatically redirect to the login page if the token is not valid anymore |
bcd9f81e | 77 | this.authService.refreshUserInformation() |
e2a2d6c8 | 78 | } |
6e07c3de | 79 | |
489290b8 C |
80 | this.initRouteEvents() |
81 | this.injectJS() | |
82 | this.injectCSS() | |
83 | ||
84 | this.initHotkeys() | |
85 | ||
60c2bc80 | 86 | this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS)) |
43d0ea7f C |
87 | |
88 | this.openModalsIfNeeded() | |
489290b8 C |
89 | } |
90 | ||
91 | isUserLoggedIn () { | |
92 | return this.authService.isLoggedIn() | |
93 | } | |
94 | ||
489290b8 C |
95 | private initRouteEvents () { |
96 | let resetScroll = true | |
97 | const eventsObs = this.router.events | |
98 | ||
99 | const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)) | |
489290b8 C |
100 | |
101 | scrollEvent.subscribe(e => { | |
102 | if (e.position) { | |
103 | return this.viewportScroller.scrollToPosition(e.position) | |
c8cf5952 | 104 | } |
00b5556c | 105 | |
489290b8 C |
106 | if (e.anchor) { |
107 | return this.viewportScroller.scrollToAnchor(e.anchor) | |
108 | } | |
109 | ||
110 | if (resetScroll) { | |
111 | return this.viewportScroller.scrollToPosition([ 0, 0 ]) | |
112 | } | |
113 | }) | |
114 | ||
60c2bc80 C |
115 | const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd)) |
116 | ||
489290b8 C |
117 | // When we add the a-state parameter, we don't want to alter the scroll |
118 | navigationEndEvent.pipe(pairwise()) | |
119 | .subscribe(([ e1, e2 ]) => { | |
120 | try { | |
121 | resetScroll = false | |
122 | ||
722bca90 C |
123 | const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects) |
124 | const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects) | |
489290b8 C |
125 | |
126 | if (previousUrl.pathname !== nextUrl.pathname) { | |
127 | resetScroll = true | |
128 | return | |
129 | } | |
130 | ||
131 | const nextSearchParams = nextUrl.searchParams | |
132 | nextSearchParams.delete('a-state') | |
133 | ||
134 | const previousSearchParams = previousUrl.searchParams | |
135 | ||
136 | nextSearchParams.sort() | |
137 | previousSearchParams.sort() | |
138 | ||
139 | if (nextSearchParams.toString() !== previousSearchParams.toString()) { | |
140 | resetScroll = true | |
141 | } | |
142 | } catch (e) { | |
143 | console.error('Cannot parse URL to check next scroll.', e) | |
144 | resetScroll = true | |
145 | } | |
146 | }) | |
147 | ||
148 | navigationEndEvent.pipe( | |
149 | map(() => window.location.pathname), | |
150 | filter(pathname => !pathname || pathname === '/' || is18nPath(pathname)) | |
151 | ).subscribe(() => this.redirectService.redirectToHomepage(true)) | |
152 | ||
23bdacf8 C |
153 | navigationEndEvent.subscribe(e => { |
154 | this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url }) | |
155 | }) | |
156 | ||
489290b8 C |
157 | eventsObs.pipe( |
158 | filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart), | |
159 | filter(() => this.screenService.isInSmallView()) | |
3b20bdd6 | 160 | ).subscribe(() => this.menu.isMenuDisplayed = false) // User clicked on a link in the menu, change the page |
489290b8 C |
161 | } |
162 | ||
163 | private injectJS () { | |
e032aec9 | 164 | // Inject JS |
ba430d75 C |
165 | this.serverService.getConfig() |
166 | .subscribe(config => { | |
e032aec9 C |
167 | if (config.instance.customizations.javascript) { |
168 | try { | |
169 | // tslint:disable:no-eval | |
170 | eval(config.instance.customizations.javascript) | |
171 | } catch (err) { | |
172 | console.error('Cannot eval custom JavaScript.', err) | |
173 | } | |
174 | } | |
175 | }) | |
489290b8 | 176 | } |
00b5556c | 177 | |
489290b8 | 178 | private injectCSS () { |
e032aec9 | 179 | // Inject CSS if modified (admin config settings) |
ba430d75 | 180 | this.serverService.configReloaded |
e032aec9 C |
181 | .subscribe(() => { |
182 | const headStyle = document.querySelector('style.custom-css-style') | |
183 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
00b5556c | 184 | |
e032aec9 | 185 | // We test customCSS if the admin removed the css |
ba430d75 C |
186 | if (this.customCSS || this.serverConfig.instance.customizations.css) { |
187 | const styleTag = '<style>' + this.serverConfig.instance.customizations.css + '</style>' | |
e032aec9 | 188 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) |
00b5556c | 189 | } |
e032aec9 | 190 | }) |
489290b8 | 191 | } |
ee1fc23a | 192 | |
18a6f04c C |
193 | private async loadPlugins () { |
194 | this.pluginService.initializePlugins() | |
195 | ||
c9e3eeed | 196 | this.hooks.runAction('action:application.init', 'common') |
18a6f04c C |
197 | } |
198 | ||
43d0ea7f | 199 | private async openModalsIfNeeded () { |
ba430d75 | 200 | this.authService.userInformationLoaded |
43d0ea7f | 201 | .pipe( |
43d0ea7f C |
202 | map(() => this.authService.getUser()), |
203 | filter(user => user.role === UserRole.ADMINISTRATOR) | |
ba430d75 | 204 | ).subscribe(user => setTimeout(() => this._openAdminModalsIfNeeded(user))) // setTimeout because of ngIf in template |
43d0ea7f C |
205 | } |
206 | ||
ba430d75 | 207 | private async _openAdminModalsIfNeeded (user: User) { |
43d0ea7f C |
208 | if (user.noWelcomeModal !== true) return this.welcomeModal.show() |
209 | ||
ba430d75 | 210 | if (user.noInstanceConfigWarningModal === true || !this.serverConfig.signup.allowed) return |
589d9f55 C |
211 | |
212 | this.instanceService.getAbout() | |
213 | .subscribe(about => { | |
214 | if ( | |
ba430d75 | 215 | this.serverConfig.instance.name.toLowerCase() === 'peertube' || |
589d9f55 C |
216 | !about.instance.terms || |
217 | !about.instance.administrator || | |
218 | !about.instance.maintenanceLifetime | |
219 | ) { | |
220 | this.instanceConfigWarningModal.show(about) | |
221 | } | |
222 | }) | |
43d0ea7f C |
223 | } |
224 | ||
489290b8 | 225 | private initHotkeys () { |
ee1fc23a | 226 | this.hotkeysService.add([ |
8542dc33 | 227 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { |
ee1fc23a | 228 | document.getElementById('search-video').focus() |
8542dc33 | 229 | return false |
e33f888b | 230 | }, undefined, this.i18n('Focus the search bar')), |
43d0ea7f | 231 | |
8542dc33 | 232 | new Hotkey('b', (event: KeyboardEvent): boolean => { |
3b20bdd6 | 233 | this.menu.toggleMenu() |
8542dc33 | 234 | return false |
e33f888b | 235 | }, undefined, this.i18n('Toggle the left menu')), |
43d0ea7f | 236 | |
20d21199 | 237 | new Hotkey('g o', (event: KeyboardEvent): boolean => { |
a54991da RK |
238 | this.router.navigate([ '/videos/overview' ]) |
239 | return false | |
79a89941 | 240 | }, undefined, this.i18n('Go to the discover videos page')), |
43d0ea7f | 241 | |
20d21199 | 242 | new Hotkey('g t', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
243 | this.router.navigate([ '/videos/trending' ]) |
244 | return false | |
e33f888b | 245 | }, undefined, this.i18n('Go to the trending videos page')), |
43d0ea7f | 246 | |
20d21199 | 247 | new Hotkey('g r', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
248 | this.router.navigate([ '/videos/recently-added' ]) |
249 | return false | |
e33f888b | 250 | }, undefined, this.i18n('Go to the recently added videos page')), |
43d0ea7f | 251 | |
20d21199 | 252 | new Hotkey('g l', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
253 | this.router.navigate([ '/videos/local' ]) |
254 | return false | |
e33f888b | 255 | }, undefined, this.i18n('Go to the local videos page')), |
43d0ea7f | 256 | |
20d21199 | 257 | new Hotkey('g u', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
258 | this.router.navigate([ '/videos/upload' ]) |
259 | return false | |
ffb321be | 260 | }, undefined, this.i18n('Go to the videos upload page')) |
ee1fc23a | 261 | ]) |
67167390 | 262 | } |
dc8bc31b | 263 | } |