diff options
Diffstat (limited to 'client/src/app/core/routing/peertube-router.service.ts')
-rw-r--r-- | client/src/app/core/routing/peertube-router.service.ts | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/client/src/app/core/routing/peertube-router.service.ts b/client/src/app/core/routing/peertube-router.service.ts new file mode 100644 index 000000000..35716cc79 --- /dev/null +++ b/client/src/app/core/routing/peertube-router.service.ts | |||
@@ -0,0 +1,78 @@ | |||
1 | import { filter } from 'rxjs/operators' | ||
2 | import { Injectable } from '@angular/core' | ||
3 | import { ActivatedRoute, ActivatedRouteSnapshot, Event, NavigationEnd, Router, Scroll } from '@angular/router' | ||
4 | import { ServerService } from '../server' | ||
5 | |||
6 | export const enum RouterSetting { | ||
7 | NONE = 0, | ||
8 | REUSE_COMPONENT = 1 << 0, | ||
9 | DISABLE_SCROLL_RESTORE = 1 << 1 | ||
10 | } | ||
11 | |||
12 | @Injectable() | ||
13 | export class PeerTubeRouterService { | ||
14 | static readonly ROUTE_SETTING_NAME = 's' | ||
15 | |||
16 | constructor ( | ||
17 | private route: ActivatedRoute, | ||
18 | private router: Router, | ||
19 | private server: ServerService | ||
20 | ) { } | ||
21 | |||
22 | addRouteSetting (toAdd: RouterSetting) { | ||
23 | if (this.hasRouteSetting(toAdd)) return | ||
24 | |||
25 | const current = this.getRouteSetting() | ||
26 | |||
27 | this.setRouteSetting(current | toAdd) | ||
28 | } | ||
29 | |||
30 | deleteRouteSetting (toDelete: RouterSetting) { | ||
31 | const current = this.getRouteSetting() | ||
32 | |||
33 | this.setRouteSetting(current & ~toDelete) | ||
34 | } | ||
35 | |||
36 | getRouteSetting (snapshot?: ActivatedRouteSnapshot) { | ||
37 | return (snapshot || this.route.snapshot).queryParams[PeerTubeRouterService.ROUTE_SETTING_NAME] | ||
38 | } | ||
39 | |||
40 | setRouteSetting (value: number) { | ||
41 | let path = window.location.pathname | ||
42 | if (!path || path === '/') path = this.server.getHTMLConfig().instance.defaultClientRoute | ||
43 | |||
44 | const queryParams = { [PeerTubeRouterService.ROUTE_SETTING_NAME]: value } | ||
45 | |||
46 | this.router.navigate([ path ], { queryParams, replaceUrl: true, queryParamsHandling: 'merge' }) | ||
47 | } | ||
48 | |||
49 | hasRouteSetting (setting: RouterSetting, snapshot?: ActivatedRouteSnapshot) { | ||
50 | return !!(this.getRouteSetting(snapshot) & setting) | ||
51 | } | ||
52 | |||
53 | getNavigationEndEvents () { | ||
54 | return this.router.events.pipe( | ||
55 | filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd) | ||
56 | ) | ||
57 | } | ||
58 | |||
59 | getScrollEvents () { | ||
60 | return this.router.events.pipe( | ||
61 | filter((e: Event): e is Scroll => e instanceof Scroll) | ||
62 | ) | ||
63 | } | ||
64 | |||
65 | silentNavigate (baseRoute: string[], queryParams: { [id: string]: string }) { | ||
66 | let routeSetting = this.getRouteSetting() ?? RouterSetting.NONE | ||
67 | routeSetting |= RouterSetting.DISABLE_SCROLL_RESTORE | ||
68 | |||
69 | queryParams = { | ||
70 | ...queryParams, | ||
71 | |||
72 | [PeerTubeRouterService.ROUTE_SETTING_NAME]: routeSetting | ||
73 | } | ||
74 | |||
75 | return this.router.navigate(baseRoute, { queryParams }) | ||
76 | } | ||
77 | |||
78 | } | ||