PeerTube/client/src/app/app.component.ts

264 lines
9.3 KiB
TypeScript
Raw Normal View History

2019-08-28 14:40:06 +02:00
import { Component, OnInit, ViewChild } from '@angular/core'
import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
2019-03-21 16:49:46 +01:00
import { Event, GuardsCheckStart, NavigationEnd, Router, Scroll } from '@angular/router'
2018-09-06 12:00:53 +02:00
import { AuthService, RedirectService, ServerService, ThemeService } from '@app/core'
2018-05-31 18:12:15 +02:00
import { is18nPath } from '../../../shared/models/i18n'
import { ScreenService } from '@app/shared/misc/screen.service'
2020-01-15 19:25:51 +01:00
import { debounceTime, filter, map, pairwise } from 'rxjs/operators'
2019-03-21 16:49:46 +01:00
import { Hotkey, HotkeysService } from 'angular2-hotkeys'
2018-10-03 10:11:26 +02:00
import { I18n } from '@ngx-translate/i18n-polyfill'
import { fromEvent } from 'rxjs'
2019-08-22 16:13:26 +02:00
import { PlatformLocation, ViewportScroller } from '@angular/common'
2019-07-08 15:54:08 +02:00
import { PluginService } from '@app/core/plugins/plugin.service'
2019-07-22 15:40:13 +02:00
import { HooksService } from '@app/core/plugins/hooks.service'
2019-08-22 16:13:26 +02:00
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { POP_STATE_MODAL_DISMISS } from '@app/shared/misc/constants'
2019-08-28 14:40:06 +02:00
import { WelcomeModalComponent } from '@app/modal/welcome-modal.component'
import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component'
2019-12-18 15:31:54 +01:00
import { ServerConfig, UserRole } from '@shared/models'
2019-08-28 14:40:06 +02:00
import { User } from '@app/shared'
import { InstanceService } from '@app/shared/instance/instance.service'
import { MenuService } from './core/menu/menu.service'
2016-03-14 13:50:19 +01:00
@Component({
2016-11-04 16:23:18 +01:00
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.scss' ]
2016-03-14 13:50:19 +01:00
})
export class AppComponent implements OnInit {
2020-02-07 10:00:34 +01:00
@ViewChild('welcomeModal') welcomeModal: WelcomeModalComponent
@ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent
2019-08-28 14:40:06 +02:00
customCSS: SafeHtml
2019-12-18 15:31:54 +01:00
private serverConfig: ServerConfig
constructor (
2018-10-03 10:11:26 +02:00
private i18n: I18n,
2019-03-21 16:49:46 +01:00
private viewportScroller: ViewportScroller,
2016-11-04 16:23:18 +01:00
private router: Router,
private authService: AuthService,
private serverService: ServerService,
2019-07-08 15:54:08 +02:00
private pluginService: PluginService,
2019-08-28 14:40:06 +02:00
private instanceService: InstanceService,
2018-03-01 13:57:29 +01:00
private domSanitizer: DomSanitizer,
private redirectService: RedirectService,
private screenService: ScreenService,
2018-09-06 12:00:53 +02:00
private hotkeysService: HotkeysService,
2019-07-22 15:40:13 +02:00
private themeService: ThemeService,
2019-08-22 16:13:26 +02:00
private hooks: HooksService,
private location: PlatformLocation,
private modalService: NgbModal,
public menu: MenuService
2018-05-31 18:12:15 +02:00
) { }
2016-05-24 23:00:58 +02:00
2018-01-31 17:47:36 +01:00
get instanceName () {
2019-12-18 15:31:54 +01:00
return this.serverConfig.instance.name
2018-01-31 17:47:36 +01:00
}
get defaultRoute () {
return RedirectService.DEFAULT_ROUTE
}
ngOnInit () {
2018-03-08 12:04:10 +01:00
document.getElementById('incompatible-browser').className += ' browser-ok'
2019-12-18 15:31:54 +01:00
this.serverConfig = this.serverService.getTmpConfig()
this.serverService.getConfig()
.subscribe(config => this.serverConfig = config)
2019-10-25 10:57:23 +02:00
this.loadPlugins()
this.themeService.initialize()
this.authService.loadClientCredentials()
2018-03-27 16:18:25 +02:00
if (this.isUserLoggedIn()) {
// The service will automatically redirect to the login page if the token is not valid anymore
2017-10-25 17:31:11 +02:00
this.authService.refreshUserInformation()
}
2017-03-22 21:15:55 +01:00
2019-03-21 16:49:46 +01:00
this.initRouteEvents()
this.injectJS()
this.injectCSS()
this.initHotkeys()
this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS))
2019-08-28 14:40:06 +02:00
this.openModalsIfNeeded()
2019-03-21 16:49:46 +01:00
}
isUserLoggedIn () {
return this.authService.isLoggedIn()
}
private initRouteEvents () {
let resetScroll = true
const eventsObs = this.router.events
const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll))
scrollEvent.subscribe(e => {
if (e.position) {
return this.viewportScroller.scrollToPosition(e.position)
}
2019-03-21 16:49:46 +01:00
if (e.anchor) {
return this.viewportScroller.scrollToAnchor(e.anchor)
}
if (resetScroll) {
return this.viewportScroller.scrollToPosition([ 0, 0 ])
}
})
const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd))
2019-03-21 16:49:46 +01:00
// When we add the a-state parameter, we don't want to alter the scroll
navigationEndEvent.pipe(pairwise())
.subscribe(([ e1, e2 ]) => {
try {
resetScroll = false
const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects)
const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects)
2019-03-21 16:49:46 +01:00
if (previousUrl.pathname !== nextUrl.pathname) {
resetScroll = true
return
}
const nextSearchParams = nextUrl.searchParams
nextSearchParams.delete('a-state')
const previousSearchParams = previousUrl.searchParams
nextSearchParams.sort()
previousSearchParams.sort()
if (nextSearchParams.toString() !== previousSearchParams.toString()) {
resetScroll = true
}
} catch (e) {
console.error('Cannot parse URL to check next scroll.', e)
resetScroll = true
}
})
navigationEndEvent.pipe(
map(() => window.location.pathname),
filter(pathname => !pathname || pathname === '/' || is18nPath(pathname))
).subscribe(() => this.redirectService.redirectToHomepage(true))
2019-07-25 19:02:54 +02:00
navigationEndEvent.subscribe(e => {
this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url })
})
2019-03-21 16:49:46 +01:00
eventsObs.pipe(
filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart),
filter(() => this.screenService.isInSmallView())
).subscribe(() => this.menu.isMenuDisplayed = false) // User clicked on a link in the menu, change the page
2019-03-21 16:49:46 +01:00
}
private injectJS () {
// Inject JS
2019-12-18 15:31:54 +01:00
this.serverService.getConfig()
.subscribe(config => {
if (config.instance.customizations.javascript) {
try {
// tslint:disable:no-eval
eval(config.instance.customizations.javascript)
} catch (err) {
console.error('Cannot eval custom JavaScript.', err)
}
}
})
2019-03-21 16:49:46 +01:00
}
2019-03-21 16:49:46 +01:00
private injectCSS () {
// Inject CSS if modified (admin config settings)
2019-12-18 15:31:54 +01:00
this.serverService.configReloaded
.subscribe(() => {
const headStyle = document.querySelector('style.custom-css-style')
if (headStyle) headStyle.parentNode.removeChild(headStyle)
// We test customCSS if the admin removed the css
2019-12-18 15:31:54 +01:00
if (this.customCSS || this.serverConfig.instance.customizations.css) {
const styleTag = '<style>' + this.serverConfig.instance.customizations.css + '</style>'
this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
}
})
2019-03-21 16:49:46 +01:00
}
2019-07-08 15:54:08 +02:00
private async loadPlugins () {
this.pluginService.initializePlugins()
2019-07-23 12:16:34 +02:00
this.hooks.runAction('action:application.init', 'common')
2019-07-08 15:54:08 +02:00
}
2019-08-28 14:40:06 +02:00
private async openModalsIfNeeded () {
2019-12-18 15:31:54 +01:00
this.authService.userInformationLoaded
2019-08-28 14:40:06 +02:00
.pipe(
map(() => this.authService.getUser()),
filter(user => user.role === UserRole.ADMINISTRATOR)
2019-12-18 15:31:54 +01:00
).subscribe(user => setTimeout(() => this._openAdminModalsIfNeeded(user))) // setTimeout because of ngIf in template
2019-08-28 14:40:06 +02:00
}
2019-12-18 15:31:54 +01:00
private async _openAdminModalsIfNeeded (user: User) {
2019-08-28 14:40:06 +02:00
if (user.noWelcomeModal !== true) return this.welcomeModal.show()
2019-12-18 15:31:54 +01:00
if (user.noInstanceConfigWarningModal === true || !this.serverConfig.signup.allowed) return
2019-08-28 15:46:56 +02:00
this.instanceService.getAbout()
.subscribe(about => {
if (
2019-12-18 15:31:54 +01:00
this.serverConfig.instance.name.toLowerCase() === 'peertube' ||
2019-08-28 15:46:56 +02:00
!about.instance.terms ||
!about.instance.administrator ||
!about.instance.maintenanceLifetime
) {
this.instanceConfigWarningModal.show(about)
}
})
2019-08-28 14:40:06 +02:00
}
2019-03-21 16:49:46 +01:00
private initHotkeys () {
this.hotkeysService.add([
new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
document.getElementById('search-video').focus()
return false
2018-10-03 10:11:26 +02:00
}, undefined, this.i18n('Focus the search bar')),
2019-08-28 14:40:06 +02:00
new Hotkey('b', (event: KeyboardEvent): boolean => {
this.menu.toggleMenu()
return false
2018-10-03 10:11:26 +02:00
}, undefined, this.i18n('Toggle the left menu')),
2019-08-28 14:40:06 +02:00
new Hotkey('g o', (event: KeyboardEvent): boolean => {
this.router.navigate([ '/videos/overview' ])
return false
2019-08-22 10:02:32 +02:00
}, undefined, this.i18n('Go to the discover videos page')),
2019-08-28 14:40:06 +02:00
new Hotkey('g t', (event: KeyboardEvent): boolean => {
this.router.navigate([ '/videos/trending' ])
return false
2018-10-03 10:11:26 +02:00
}, undefined, this.i18n('Go to the trending videos page')),
2019-08-28 14:40:06 +02:00
new Hotkey('g r', (event: KeyboardEvent): boolean => {
this.router.navigate([ '/videos/recently-added' ])
return false
2018-10-03 10:11:26 +02:00
}, undefined, this.i18n('Go to the recently added videos page')),
2019-08-28 14:40:06 +02:00
new Hotkey('g l', (event: KeyboardEvent): boolean => {
this.router.navigate([ '/videos/local' ])
return false
2018-10-03 10:11:26 +02:00
}, undefined, this.i18n('Go to the local videos page')),
2019-08-28 14:40:06 +02:00
new Hotkey('g u', (event: KeyboardEvent): boolean => {
this.router.navigate([ '/videos/upload' ])
return false
2019-07-10 14:06:19 +02:00
}, undefined, this.i18n('Go to the videos upload page'))
])
2017-04-26 21:22:00 +02:00
}
2016-03-14 13:50:19 +01:00
}