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