aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/services/RouterService.ts
blob: 4e2999b1d29efca6742320e26d0aa2b540a3fecb (plain)
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
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import type { Route, Location } from 'vue-router'
import type VueRouter from 'vue-router'

export default class RouterService {

	// typescript compiles this to `#router` to make it private even in JS,
	// but in TS it needs to be called without the visibility specifier
	private router: VueRouter

	constructor(router: VueRouter) {
		this.router = router
	}

	get name(): string | null | undefined {
		return this.router.currentRoute.name
	}

	get query(): Record<string, string | (string | null)[] | null | undefined> {
		return this.router.currentRoute.query || {}
	}

	get params(): Record<string, string> {
		return this.router.currentRoute.params || {}
	}

	/**
	 * This is a protected getter only for internal use
	 * @private
	 */
	get _router() {
		return this.router
	}

	/**
	 * Trigger a route change on the files app
	 *
	 * @param path the url path, eg: '/trashbin?dir=/Deleted'
	 * @param replace replace the current history
	 * @see https://router.vuejs.org/guide/essentials/navigation.html#navigate-to-a-different-location
	 */
	goTo(path: string, replace = false): Promise<Route> {
		return this.router.push({
			path,
			replace,
		})
	}

	/**
	 * Trigger a route change on the files App
	 *
	 * @param name the route name
	 * @param params the route parameters
	 * @param query the url query parameters
	 * @param replace replace the current history
	 * @see https://router.vuejs.org/guide/essentials/navigation.html#navigate-to-a-different-location
	 */
	goToRoute(
		name?: string,
		params?: Record<string, string>,
		query?: Record<string, string | (string | null)[] | null | undefined>,
		replace?: boolean,
	): Promise<Route> {
		return this.router.push({
			name,
			query,
			params,
			replace,
		} as Location)
	}

}