]>
Commit | Line | Data |
---|---|---|
67ed6552 C |
1 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
2 | import { concat } from 'rxjs' | |
3 | import { filter, first, map, pairwise } from 'rxjs/operators' | |
4 | import { DOCUMENT, PlatformLocation, ViewportScroller } from '@angular/common' | |
5 | import { AfterViewInit, Component, Inject, LOCALE_ID, OnInit, ViewChild } from '@angular/core' | |
00b5556c | 6 | import { DomSanitizer, SafeHtml } from '@angular/platform-browser' |
489290b8 | 7 | import { Event, GuardsCheckStart, NavigationEnd, Router, Scroll } from '@angular/router' |
67ed6552 | 8 | import { AuthService, MarkdownService, RedirectService, ScreenService, ServerService, ThemeService, User } from '@app/core' |
93cae479 | 9 | import { HooksService } from '@app/core/plugins/hooks.service' |
67ed6552 | 10 | import { PluginService } from '@app/core/plugins/plugin.service' |
437e8e06 | 11 | import { CustomModalComponent } from '@app/modal/custom-modal.component' |
67ed6552 C |
12 | import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component' |
13 | import { WelcomeModalComponent } from '@app/modal/welcome-modal.component' | |
14 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
15 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
bd45d503 C |
16 | import { getShortLocale, is18nPath } from '@shared/core-utils/i18n' |
17 | import { BroadcastMessageLevel, ServerConfig, UserRole } from '@shared/models' | |
3b20bdd6 | 18 | import { MenuService } from './core/menu/menu.service' |
4504f09f RK |
19 | import { POP_STATE_MODAL_DISMISS } from './helpers' |
20 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' | |
67ed6552 | 21 | import { InstanceService } from './shared/shared-instance' |
e2a2d6c8 | 22 | |
dc8bc31b | 23 | @Component({ |
3154f382 C |
24 | selector: 'my-app', |
25 | templateUrl: './app.component.html', | |
26 | styleUrls: [ './app.component.scss' ] | |
dc8bc31b | 27 | }) |
437e8e06 | 28 | export class AppComponent implements OnInit, AfterViewInit { |
72c33e71 C |
29 | private static BROADCAST_MESSAGE_KEY = 'app-broadcast-message-dismissed' |
30 | ||
2f5d2ec5 C |
31 | @ViewChild('welcomeModal') welcomeModal: WelcomeModalComponent |
32 | @ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent | |
437e8e06 | 33 | @ViewChild('customModal') customModal: CustomModalComponent |
43d0ea7f | 34 | |
00b5556c | 35 | customCSS: SafeHtml |
72c33e71 | 36 | broadcastMessage: { message: string, dismissable: boolean, class: string } | null = null |
00b5556c | 37 | |
ba430d75 C |
38 | private serverConfig: ServerConfig |
39 | ||
df98563e | 40 | constructor ( |
140ea386 | 41 | @Inject(DOCUMENT) private document: Document, |
81fe3c67 | 42 | @Inject(LOCALE_ID) private localeId: string, |
e33f888b | 43 | private i18n: I18n, |
489290b8 | 44 | private viewportScroller: ViewportScroller, |
3154f382 | 45 | private router: Router, |
e2a2d6c8 | 46 | private authService: AuthService, |
00b5556c | 47 | private serverService: ServerService, |
18a6f04c | 48 | private pluginService: PluginService, |
43d0ea7f | 49 | private instanceService: InstanceService, |
901637bb | 50 | private domSanitizer: DomSanitizer, |
bbe0f064 | 51 | private redirectService: RedirectService, |
ee1fc23a | 52 | private screenService: ScreenService, |
1a00c561 | 53 | private hotkeysService: HotkeysService, |
93cae479 | 54 | private themeService: ThemeService, |
4334445d C |
55 | private hooks: HooksService, |
56 | private location: PlatformLocation, | |
3b20bdd6 | 57 | private modalService: NgbModal, |
72c33e71 | 58 | private markdownService: MarkdownService, |
3b20bdd6 | 59 | public menu: MenuService |
989e526a | 60 | ) { } |
a99593ed | 61 | |
36f9424f | 62 | get instanceName () { |
ba430d75 | 63 | return this.serverConfig.instance.name |
36f9424f C |
64 | } |
65 | ||
29f9b562 C |
66 | get defaultRoute () { |
67 | return RedirectService.DEFAULT_ROUTE | |
68 | } | |
69 | ||
df98563e | 70 | ngOnInit () { |
b3eeb529 | 71 | document.getElementById('incompatible-browser').className += ' browser-ok' |
73e09f27 | 72 | |
ba430d75 C |
73 | this.serverConfig = this.serverService.getTmpConfig() |
74 | this.serverService.getConfig() | |
75 | .subscribe(config => this.serverConfig = config) | |
76 | ||
a2ffd046 C |
77 | this.loadPlugins() |
78 | this.themeService.initialize() | |
79 | ||
d592e0a9 C |
80 | this.authService.loadClientCredentials() |
81 | ||
d414207f | 82 | if (this.isUserLoggedIn()) { |
e2a2d6c8 | 83 | // The service will automatically redirect to the login page if the token is not valid anymore |
bcd9f81e | 84 | this.authService.refreshUserInformation() |
e2a2d6c8 | 85 | } |
6e07c3de | 86 | |
489290b8 C |
87 | this.initRouteEvents() |
88 | this.injectJS() | |
89 | this.injectCSS() | |
72c33e71 | 90 | this.injectBroadcastMessage() |
489290b8 C |
91 | |
92 | this.initHotkeys() | |
93 | ||
60c2bc80 | 94 | this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS)) |
43d0ea7f C |
95 | |
96 | this.openModalsIfNeeded() | |
81fe3c67 RK |
97 | |
98 | this.document.documentElement.lang = getShortLocale(this.localeId) | |
489290b8 C |
99 | } |
100 | ||
437e8e06 K |
101 | ngAfterViewInit () { |
102 | this.pluginService.initializeCustomModal(this.customModal) | |
103 | } | |
104 | ||
489290b8 C |
105 | isUserLoggedIn () { |
106 | return this.authService.isLoggedIn() | |
107 | } | |
108 | ||
72c33e71 C |
109 | hideBroadcastMessage () { |
110 | peertubeLocalStorage.setItem(AppComponent.BROADCAST_MESSAGE_KEY, this.serverConfig.broadcastMessage.message) | |
111 | ||
112 | this.broadcastMessage = null | |
113 | } | |
114 | ||
489290b8 C |
115 | private initRouteEvents () { |
116 | let resetScroll = true | |
117 | const eventsObs = this.router.events | |
118 | ||
119 | const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)) | |
489290b8 C |
120 | |
121 | scrollEvent.subscribe(e => { | |
4a53fc82 K |
122 | // scrollToAnchor first to preserve anchor position when using history navigation |
123 | if (e.anchor) { | |
124 | setTimeout(() => { | |
125 | this.viewportScroller.scrollToAnchor(e.anchor) | |
126 | }) | |
127 | ||
128 | return | |
c8cf5952 | 129 | } |
00b5556c | 130 | |
4a53fc82 K |
131 | if (e.position) { |
132 | return this.viewportScroller.scrollToPosition(e.position) | |
489290b8 C |
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()) | |
3b20bdd6 | 185 | ).subscribe(() => this.menu.isMenuDisplayed = false) // User clicked on a link in the menu, change the page |
489290b8 C |
186 | } |
187 | ||
72c33e71 C |
188 | private injectBroadcastMessage () { |
189 | concat( | |
190 | this.serverService.getConfig().pipe(first()), | |
191 | this.serverService.configReloaded | |
192 | ).subscribe(async config => { | |
193 | this.broadcastMessage = null | |
194 | ||
195 | const messageConfig = config.broadcastMessage | |
196 | ||
197 | if (messageConfig.enabled) { | |
198 | // Already dismissed this message? | |
199 | if (messageConfig.dismissable && localStorage.getItem(AppComponent.BROADCAST_MESSAGE_KEY) === messageConfig.message) { | |
200 | return | |
201 | } | |
202 | ||
203 | const classes: { [id in BroadcastMessageLevel]: string } = { | |
204 | info: 'alert-info', | |
205 | warning: 'alert-warning', | |
206 | error: 'alert-danger' | |
207 | } | |
208 | ||
209 | this.broadcastMessage = { | |
210 | message: await this.markdownService.completeMarkdownToHTML(messageConfig.message), | |
211 | dismissable: messageConfig.dismissable, | |
212 | class: classes[messageConfig.level] | |
213 | } | |
214 | } | |
215 | }) | |
216 | } | |
217 | ||
489290b8 | 218 | private injectJS () { |
e032aec9 | 219 | // Inject JS |
ba430d75 C |
220 | this.serverService.getConfig() |
221 | .subscribe(config => { | |
e032aec9 C |
222 | if (config.instance.customizations.javascript) { |
223 | try { | |
224 | // tslint:disable:no-eval | |
225 | eval(config.instance.customizations.javascript) | |
226 | } catch (err) { | |
227 | console.error('Cannot eval custom JavaScript.', err) | |
228 | } | |
229 | } | |
230 | }) | |
489290b8 | 231 | } |
00b5556c | 232 | |
489290b8 | 233 | private injectCSS () { |
e032aec9 | 234 | // Inject CSS if modified (admin config settings) |
72c33e71 C |
235 | concat( |
236 | this.serverService.getConfig().pipe(first()), | |
237 | this.serverService.configReloaded | |
238 | ).subscribe(config => { | |
239 | const headStyle = document.querySelector('style.custom-css-style') | |
240 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
241 | ||
242 | // We test customCSS if the admin removed the css | |
243 | if (this.customCSS || config.instance.customizations.css) { | |
244 | const styleTag = '<style>' + config.instance.customizations.css + '</style>' | |
245 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
246 | } | |
247 | }) | |
489290b8 | 248 | } |
ee1fc23a | 249 | |
18a6f04c C |
250 | private async loadPlugins () { |
251 | this.pluginService.initializePlugins() | |
252 | ||
c9e3eeed | 253 | this.hooks.runAction('action:application.init', 'common') |
18a6f04c C |
254 | } |
255 | ||
43d0ea7f | 256 | private async openModalsIfNeeded () { |
ba430d75 | 257 | this.authService.userInformationLoaded |
43d0ea7f | 258 | .pipe( |
43d0ea7f C |
259 | map(() => this.authService.getUser()), |
260 | filter(user => user.role === UserRole.ADMINISTRATOR) | |
ba430d75 | 261 | ).subscribe(user => setTimeout(() => this._openAdminModalsIfNeeded(user))) // setTimeout because of ngIf in template |
43d0ea7f C |
262 | } |
263 | ||
ba430d75 | 264 | private async _openAdminModalsIfNeeded (user: User) { |
43d0ea7f C |
265 | if (user.noWelcomeModal !== true) return this.welcomeModal.show() |
266 | ||
ba430d75 | 267 | if (user.noInstanceConfigWarningModal === true || !this.serverConfig.signup.allowed) return |
589d9f55 C |
268 | |
269 | this.instanceService.getAbout() | |
270 | .subscribe(about => { | |
271 | if ( | |
ba430d75 | 272 | this.serverConfig.instance.name.toLowerCase() === 'peertube' || |
589d9f55 C |
273 | !about.instance.terms || |
274 | !about.instance.administrator || | |
275 | !about.instance.maintenanceLifetime | |
276 | ) { | |
277 | this.instanceConfigWarningModal.show(about) | |
278 | } | |
279 | }) | |
43d0ea7f C |
280 | } |
281 | ||
489290b8 | 282 | private initHotkeys () { |
ee1fc23a | 283 | this.hotkeysService.add([ |
8542dc33 | 284 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { |
ee1fc23a | 285 | document.getElementById('search-video').focus() |
8542dc33 | 286 | return false |
e33f888b | 287 | }, undefined, this.i18n('Focus the search bar')), |
43d0ea7f | 288 | |
8542dc33 | 289 | new Hotkey('b', (event: KeyboardEvent): boolean => { |
3b20bdd6 | 290 | this.menu.toggleMenu() |
8542dc33 | 291 | return false |
e33f888b | 292 | }, undefined, this.i18n('Toggle the left menu')), |
43d0ea7f | 293 | |
20d21199 | 294 | new Hotkey('g o', (event: KeyboardEvent): boolean => { |
a54991da RK |
295 | this.router.navigate([ '/videos/overview' ]) |
296 | return false | |
79a89941 | 297 | }, undefined, this.i18n('Go to the discover videos page')), |
43d0ea7f | 298 | |
20d21199 | 299 | new Hotkey('g t', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
300 | this.router.navigate([ '/videos/trending' ]) |
301 | return false | |
e33f888b | 302 | }, undefined, this.i18n('Go to the trending videos page')), |
43d0ea7f | 303 | |
20d21199 | 304 | new Hotkey('g r', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
305 | this.router.navigate([ '/videos/recently-added' ]) |
306 | return false | |
e33f888b | 307 | }, undefined, this.i18n('Go to the recently added videos page')), |
43d0ea7f | 308 | |
20d21199 | 309 | new Hotkey('g l', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
310 | this.router.navigate([ '/videos/local' ]) |
311 | return false | |
e33f888b | 312 | }, undefined, this.i18n('Go to the local videos page')), |
43d0ea7f | 313 | |
20d21199 | 314 | new Hotkey('g u', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
315 | this.router.navigate([ '/videos/upload' ]) |
316 | return false | |
ffb321be | 317 | }, undefined, this.i18n('Go to the videos upload page')) |
ee1fc23a | 318 | ]) |
67167390 | 319 | } |
dc8bc31b | 320 | } |