aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/routing/peertube-router.service.ts
blob: 35716cc7942bf0a8a401e6ccc48f8c93aaa4aeff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { filter } from 'rxjs/operators'
import { Injectable } from '@angular/core'
import { ActivatedRoute, ActivatedRouteSnapshot, Event, NavigationEnd, Router, Scroll } from '@angular/router'
import { ServerService } from '../server'

export const enum RouterSetting {
  NONE = 0,
  REUSE_COMPONENT = 1 << 0,
  DISABLE_SCROLL_RESTORE = 1 << 1
}

@Injectable()
export class PeerTubeRouterService {
  static readonly ROUTE_SETTING_NAME = 's'

  constructor (
    private route: ActivatedRoute,
    private router: Router,
    private server: ServerService
  ) { }

  addRouteSetting (toAdd: RouterSetting) {
    if (this.hasRouteSetting(toAdd)) return

    const current = this.getRouteSetting()

    this.setRouteSetting(current | toAdd)
  }

  deleteRouteSetting (toDelete: RouterSetting) {
    const current = this.getRouteSetting()

    this.setRouteSetting(current & ~toDelete)
  }

  getRouteSetting (snapshot?: ActivatedRouteSnapshot) {
    return (snapshot || this.route.snapshot).queryParams[PeerTubeRouterService.ROUTE_SETTING_NAME]
  }

  setRouteSetting (value: number) {
    let path = window.location.pathname
    if (!path || path === '/') path = this.server.getHTMLConfig().instance.defaultClientRoute

    const queryParams = { [PeerTubeRouterService.ROUTE_SETTING_NAME]: value }

    this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' })
  }

  hasRouteSetting (setting: RouterSetting, snapshot?: ActivatedRouteSnapshot) {
    return !!(this.getRouteSetting(snapshot) & setting)
  }

  getNavigationEndEvents () {
    return this.router.events.pipe(
      filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd)
    )
  }

  getScrollEvents () {
    return this.router.events.pipe(
      filter((e: Event): e is Scroll => e instanceof Scroll)
    )
  }

  silentNavigate (baseRoute: string[], queryParams: { [id: string]: string }) {
    let routeSetting = this.getRouteSetting() ?? RouterSetting.NONE
    routeSetting |= RouterSetting.DISABLE_SCROLL_RESTORE

    queryParams = {
      ...queryParams,

      [PeerTubeRouterService.ROUTE_SETTING_NAME]: routeSetting
    }

    return this.router.navigate(baseRoute, { queryParams })
  }

}