2016-11-29 23:21:41 -08:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 12:08:49 -07:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-11-29 23:21:41 -08:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {ComponentRef} from '@angular/core';
|
|
|
|
|
|
2017-05-17 17:47:34 -07:00
|
|
|
import {OutletContext} from './router_outlet_context';
|
2016-11-29 23:21:41 -08:00
|
|
|
import {ActivatedRoute, ActivatedRouteSnapshot} from './router_state';
|
|
|
|
|
import {TreeNode} from './utils/tree';
|
|
|
|
|
|
|
|
|
|
/**
|
2018-04-05 11:51:21 +01:00
|
|
|
* @description
|
|
|
|
|
*
|
|
|
|
|
* Represents the detached route tree.
|
2016-11-29 23:21:41 -08:00
|
|
|
*
|
|
|
|
|
* This is an opaque value the router will give to a custom route reuse strategy
|
|
|
|
|
* to store and retrieve later on.
|
|
|
|
|
*
|
2018-10-19 12:12:20 +01:00
|
|
|
* @publicApi
|
2016-11-29 23:21:41 -08:00
|
|
|
*/
|
|
|
|
|
export type DetachedRouteHandle = {};
|
|
|
|
|
|
2016-12-09 10:44:46 -08:00
|
|
|
/** @internal */
|
2016-11-29 23:21:41 -08:00
|
|
|
export type DetachedRouteHandleInternal = {
|
2017-05-17 17:47:34 -07:00
|
|
|
contexts: Map<string, OutletContext>,
|
2016-11-29 23:21:41 -08:00
|
|
|
componentRef: ComponentRef<any>,
|
2016-12-09 10:44:46 -08:00
|
|
|
route: TreeNode<ActivatedRoute>,
|
2016-11-29 23:21:41 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2018-04-05 11:51:21 +01:00
|
|
|
* @description
|
|
|
|
|
*
|
|
|
|
|
* Provides a way to customize when activated routes get reused.
|
2016-11-29 23:21:41 -08:00
|
|
|
*
|
2018-10-19 12:12:20 +01:00
|
|
|
* @publicApi
|
2016-11-29 23:21:41 -08:00
|
|
|
*/
|
|
|
|
|
export abstract class RouteReuseStrategy {
|
2016-12-09 10:44:46 -08:00
|
|
|
/** Determines if this route (and its subtree) should be detached to be reused later */
|
2016-11-29 23:21:41 -08:00
|
|
|
abstract shouldDetach(route: ActivatedRouteSnapshot): boolean;
|
|
|
|
|
|
2017-05-17 17:47:34 -07:00
|
|
|
/**
|
|
|
|
|
* Stores the detached route.
|
|
|
|
|
*
|
|
|
|
|
* Storing a `null` value should erase the previously stored value.
|
|
|
|
|
*/
|
2017-04-17 11:13:13 -07:00
|
|
|
abstract store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle|null): void;
|
2016-11-29 23:21:41 -08:00
|
|
|
|
2016-12-09 10:44:46 -08:00
|
|
|
/** Determines if this route (and its subtree) should be reattached */
|
2016-11-29 23:21:41 -08:00
|
|
|
abstract shouldAttach(route: ActivatedRouteSnapshot): boolean;
|
|
|
|
|
|
2016-12-09 10:44:46 -08:00
|
|
|
/** Retrieves the previously stored route */
|
2017-04-17 11:13:13 -07:00
|
|
|
abstract retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle|null;
|
2016-11-29 23:21:41 -08:00
|
|
|
|
2016-12-09 10:44:46 -08:00
|
|
|
/** Determines if a route should be reused */
|
2016-11-29 23:21:41 -08:00
|
|
|
abstract shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean;
|
2017-05-03 11:17:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-15 23:00:01 +02:00
|
|
|
* @description
|
|
|
|
|
*
|
|
|
|
|
* This base route reuse strategy only reuses routes when the matched router configs are
|
|
|
|
|
* identical. This prevents components from being destroyed and recreated
|
|
|
|
|
* when just the fragment or query parameters change
|
|
|
|
|
* (that is, the existing component is _reused_).
|
|
|
|
|
*
|
|
|
|
|
* This strategy does not store any routes for later reuse.
|
|
|
|
|
*
|
|
|
|
|
* Angular uses this strategy by default.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* It can be used as a base class for custom route reuse strategies, i.e. you can create your own
|
|
|
|
|
* class that extends the `BaseRouteReuseStrategy` one.
|
|
|
|
|
* @publicApi
|
2017-05-03 11:17:27 +02:00
|
|
|
*/
|
2019-07-15 23:00:01 +02:00
|
|
|
export abstract class BaseRouteReuseStrategy implements RouteReuseStrategy {
|
|
|
|
|
/**
|
|
|
|
|
* Whether the given route should detach for later reuse.
|
|
|
|
|
* Always returns false for `BaseRouteReuseStrategy`.
|
|
|
|
|
* */
|
2020-04-13 16:40:21 -07:00
|
|
|
shouldDetach(route: ActivatedRouteSnapshot): boolean {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-07-15 23:00:01 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A no-op; the route is never stored since this strategy never detaches routes for later re-use.
|
|
|
|
|
*/
|
2017-05-03 11:17:27 +02:00
|
|
|
store(route: ActivatedRouteSnapshot, detachedTree: DetachedRouteHandle): void {}
|
2019-07-15 23:00:01 +02:00
|
|
|
|
|
|
|
|
/** Returns `false`, meaning the route (and its subtree) is never reattached */
|
2020-04-13 16:40:21 -07:00
|
|
|
shouldAttach(route: ActivatedRouteSnapshot): boolean {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-07-15 23:00:01 +02:00
|
|
|
|
|
|
|
|
/** Returns `null` because this strategy does not store routes for later re-use. */
|
2020-04-13 16:40:21 -07:00
|
|
|
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle|null {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-07-15 23:00:01 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines if a route should be reused.
|
|
|
|
|
* This strategy returns `true` when the future route config and current route config are
|
|
|
|
|
* identical.
|
|
|
|
|
*/
|
2017-05-03 11:17:27 +02:00
|
|
|
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
|
|
|
|
|
return future.routeConfig === curr.routeConfig;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-15 23:00:01 +02:00
|
|
|
|
|
|
|
|
export class DefaultRouteReuseStrategy extends BaseRouteReuseStrategy {}
|