1 import { Injectable } from '@angular/core'
2 import { NavigationEnd, Router } from '@angular/router'
3 import { ServerService } from '../server'
6 export class RedirectService {
7 // Default route could change according to the instance configuration
8 static INIT_DEFAULT_ROUTE = '/videos/trending'
9 static DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
11 private previousUrl: string
12 private currentUrl: string
15 private router: Router,
16 private serverService: ServerService
18 // The config is first loaded from the cache so try to get the default route
19 const tmpConfig = this.serverService.getTmpConfig()
20 if (tmpConfig && tmpConfig.instance && tmpConfig.instance.defaultClientRoute) {
21 RedirectService.DEFAULT_ROUTE = tmpConfig.instance.defaultClientRoute
25 this.serverService.getConfig()
26 .subscribe(config => {
27 const defaultRouteConfig = config.instance.defaultClientRoute
29 if (defaultRouteConfig) {
30 RedirectService.DEFAULT_ROUTE = defaultRouteConfig
35 this.currentUrl = this.router.url
36 router.events.subscribe(event => {
37 if (event instanceof NavigationEnd) {
38 this.previousUrl = this.currentUrl
39 this.currentUrl = event.url
44 redirectToPreviousRoute () {
50 if (this.previousUrl) {
51 const isException = exceptions.find(e => this.previousUrl.startsWith(e))
52 if (!isException) return this.router.navigateByUrl(this.previousUrl)
55 return this.redirectToHomepage()
58 redirectToHomepage (skipLocationChange = false) {
59 console.log('Redirecting to %s...', RedirectService.DEFAULT_ROUTE)
61 this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })
64 'Cannot navigate to %s, resetting default route to %s.',
65 RedirectService.DEFAULT_ROUTE,
66 RedirectService.INIT_DEFAULT_ROUTE
69 RedirectService.DEFAULT_ROUTE = RedirectService.INIT_DEFAULT_ROUTE
70 return this.router.navigate([ RedirectService.DEFAULT_ROUTE ], { skipLocationChange })