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