]>
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 | |
7034b3c9 | 113 | this.screenService.isBroadcastMessageDisplayed = false |
72c33e71 C |
114 | } |
115 | ||
489290b8 C |
116 | private initRouteEvents () { |
117 | let resetScroll = true | |
118 | const eventsObs = this.router.events | |
119 | ||
120 | const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)) | |
489290b8 C |
121 | |
122 | scrollEvent.subscribe(e => { | |
4a53fc82 K |
123 | // scrollToAnchor first to preserve anchor position when using history navigation |
124 | if (e.anchor) { | |
125 | setTimeout(() => { | |
126 | this.viewportScroller.scrollToAnchor(e.anchor) | |
127 | }) | |
128 | ||
129 | return | |
c8cf5952 | 130 | } |
00b5556c | 131 | |
4a53fc82 K |
132 | if (e.position) { |
133 | return this.viewportScroller.scrollToPosition(e.position) | |
489290b8 C |
134 | } |
135 | ||
136 | if (resetScroll) { | |
137 | return this.viewportScroller.scrollToPosition([ 0, 0 ]) | |
138 | } | |
139 | }) | |
140 | ||
60c2bc80 C |
141 | const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd)) |
142 | ||
489290b8 C |
143 | // When we add the a-state parameter, we don't want to alter the scroll |
144 | navigationEndEvent.pipe(pairwise()) | |
145 | .subscribe(([ e1, e2 ]) => { | |
146 | try { | |
147 | resetScroll = false | |
148 | ||
722bca90 C |
149 | const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects) |
150 | const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects) | |
489290b8 C |
151 | |
152 | if (previousUrl.pathname !== nextUrl.pathname) { | |
153 | resetScroll = true | |
154 | return | |
155 | } | |
156 | ||
157 | const nextSearchParams = nextUrl.searchParams | |
158 | nextSearchParams.delete('a-state') | |
159 | ||
160 | const previousSearchParams = previousUrl.searchParams | |
161 | ||
162 | nextSearchParams.sort() | |
163 | previousSearchParams.sort() | |
164 | ||
165 | if (nextSearchParams.toString() !== previousSearchParams.toString()) { | |
166 | resetScroll = true | |
167 | } | |
168 | } catch (e) { | |
169 | console.error('Cannot parse URL to check next scroll.', e) | |
170 | resetScroll = true | |
171 | } | |
172 | }) | |
173 | ||
174 | navigationEndEvent.pipe( | |
175 | map(() => window.location.pathname), | |
176 | filter(pathname => !pathname || pathname === '/' || is18nPath(pathname)) | |
177 | ).subscribe(() => this.redirectService.redirectToHomepage(true)) | |
178 | ||
23bdacf8 C |
179 | navigationEndEvent.subscribe(e => { |
180 | this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url }) | |
181 | }) | |
182 | ||
489290b8 C |
183 | eventsObs.pipe( |
184 | filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart), | |
185 | filter(() => this.screenService.isInSmallView()) | |
3b20bdd6 | 186 | ).subscribe(() => this.menu.isMenuDisplayed = false) // User clicked on a link in the menu, change the page |
489290b8 C |
187 | } |
188 | ||
72c33e71 C |
189 | private injectBroadcastMessage () { |
190 | concat( | |
191 | this.serverService.getConfig().pipe(first()), | |
192 | this.serverService.configReloaded | |
193 | ).subscribe(async config => { | |
194 | this.broadcastMessage = null | |
7034b3c9 | 195 | this.screenService.isBroadcastMessageDisplayed = false |
72c33e71 C |
196 | |
197 | const messageConfig = config.broadcastMessage | |
198 | ||
199 | if (messageConfig.enabled) { | |
200 | // Already dismissed this message? | |
201 | if (messageConfig.dismissable && localStorage.getItem(AppComponent.BROADCAST_MESSAGE_KEY) === messageConfig.message) { | |
202 | return | |
203 | } | |
204 | ||
205 | const classes: { [id in BroadcastMessageLevel]: string } = { | |
206 | info: 'alert-info', | |
207 | warning: 'alert-warning', | |
208 | error: 'alert-danger' | |
209 | } | |
210 | ||
211 | this.broadcastMessage = { | |
212 | message: await this.markdownService.completeMarkdownToHTML(messageConfig.message), | |
213 | dismissable: messageConfig.dismissable, | |
214 | class: classes[messageConfig.level] | |
215 | } | |
7034b3c9 | 216 | |
217 | this.screenService.isBroadcastMessageDisplayed = true | |
72c33e71 C |
218 | } |
219 | }) | |
220 | } | |
221 | ||
489290b8 | 222 | private injectJS () { |
e032aec9 | 223 | // Inject JS |
ba430d75 C |
224 | this.serverService.getConfig() |
225 | .subscribe(config => { | |
e032aec9 C |
226 | if (config.instance.customizations.javascript) { |
227 | try { | |
228 | // tslint:disable:no-eval | |
229 | eval(config.instance.customizations.javascript) | |
230 | } catch (err) { | |
231 | console.error('Cannot eval custom JavaScript.', err) | |
232 | } | |
233 | } | |
234 | }) | |
489290b8 | 235 | } |
00b5556c | 236 | |
489290b8 | 237 | private injectCSS () { |
e032aec9 | 238 | // Inject CSS if modified (admin config settings) |
72c33e71 C |
239 | concat( |
240 | this.serverService.getConfig().pipe(first()), | |
241 | this.serverService.configReloaded | |
242 | ).subscribe(config => { | |
243 | const headStyle = document.querySelector('style.custom-css-style') | |
244 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
245 | ||
246 | // We test customCSS if the admin removed the css | |
247 | if (this.customCSS || config.instance.customizations.css) { | |
248 | const styleTag = '<style>' + config.instance.customizations.css + '</style>' | |
249 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
250 | } | |
251 | }) | |
489290b8 | 252 | } |
ee1fc23a | 253 | |
18a6f04c C |
254 | private async loadPlugins () { |
255 | this.pluginService.initializePlugins() | |
256 | ||
c9e3eeed | 257 | this.hooks.runAction('action:application.init', 'common') |
18a6f04c C |
258 | } |
259 | ||
43d0ea7f | 260 | private async openModalsIfNeeded () { |
ba430d75 | 261 | this.authService.userInformationLoaded |
43d0ea7f | 262 | .pipe( |
43d0ea7f C |
263 | map(() => this.authService.getUser()), |
264 | filter(user => user.role === UserRole.ADMINISTRATOR) | |
ba430d75 | 265 | ).subscribe(user => setTimeout(() => this._openAdminModalsIfNeeded(user))) // setTimeout because of ngIf in template |
43d0ea7f C |
266 | } |
267 | ||
ba430d75 | 268 | private async _openAdminModalsIfNeeded (user: User) { |
43d0ea7f C |
269 | if (user.noWelcomeModal !== true) return this.welcomeModal.show() |
270 | ||
ba430d75 | 271 | if (user.noInstanceConfigWarningModal === true || !this.serverConfig.signup.allowed) return |
589d9f55 C |
272 | |
273 | this.instanceService.getAbout() | |
274 | .subscribe(about => { | |
275 | if ( | |
ba430d75 | 276 | this.serverConfig.instance.name.toLowerCase() === 'peertube' || |
589d9f55 C |
277 | !about.instance.terms || |
278 | !about.instance.administrator || | |
279 | !about.instance.maintenanceLifetime | |
280 | ) { | |
281 | this.instanceConfigWarningModal.show(about) | |
282 | } | |
283 | }) | |
43d0ea7f C |
284 | } |
285 | ||
489290b8 | 286 | private initHotkeys () { |
ee1fc23a | 287 | this.hotkeysService.add([ |
8542dc33 | 288 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { |
ee1fc23a | 289 | document.getElementById('search-video').focus() |
8542dc33 | 290 | return false |
e33f888b | 291 | }, undefined, this.i18n('Focus the search bar')), |
43d0ea7f | 292 | |
8542dc33 | 293 | new Hotkey('b', (event: KeyboardEvent): boolean => { |
3b20bdd6 | 294 | this.menu.toggleMenu() |
8542dc33 | 295 | return false |
e33f888b | 296 | }, undefined, this.i18n('Toggle the left menu')), |
43d0ea7f | 297 | |
20d21199 | 298 | new Hotkey('g o', (event: KeyboardEvent): boolean => { |
a54991da RK |
299 | this.router.navigate([ '/videos/overview' ]) |
300 | return false | |
79a89941 | 301 | }, undefined, this.i18n('Go to the discover videos page')), |
43d0ea7f | 302 | |
20d21199 | 303 | new Hotkey('g t', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
304 | this.router.navigate([ '/videos/trending' ]) |
305 | return false | |
e33f888b | 306 | }, undefined, this.i18n('Go to the trending videos page')), |
43d0ea7f | 307 | |
20d21199 | 308 | new Hotkey('g r', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
309 | this.router.navigate([ '/videos/recently-added' ]) |
310 | return false | |
e33f888b | 311 | }, undefined, this.i18n('Go to the recently added videos page')), |
43d0ea7f | 312 | |
20d21199 | 313 | new Hotkey('g l', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
314 | this.router.navigate([ '/videos/local' ]) |
315 | return false | |
e33f888b | 316 | }, undefined, this.i18n('Go to the local videos page')), |
43d0ea7f | 317 | |
20d21199 | 318 | new Hotkey('g u', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
319 | this.router.navigate([ '/videos/upload' ]) |
320 | return false | |
ffb321be | 321 | }, undefined, this.i18n('Go to the videos upload page')) |
ee1fc23a | 322 | ]) |
67167390 | 323 | } |
dc8bc31b | 324 | } |