From b0cf681ab667a00f2d488f781a52278f0f1d630e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Fri, 19 Feb 2021 12:41:22 +0100 Subject: [PATCH] MOBILE-3636 core: Check route parent params on navigator --- src/core/services/navigator.ts | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/core/services/navigator.ts b/src/core/services/navigator.ts index 2e795d519..683a366a9 100644 --- a/src/core/services/navigator.ts +++ b/src/core/services/navigator.ts @@ -243,12 +243,34 @@ export class CoreNavigatorService { return CoreUrlUtils.instance.removeUrlParams(this.previousPath || ''); } + /** + * Iterately get the params checking parent routes. + * + * @param route Current route. + * @param name Name of the parameter. + * @return Value of the parameter, undefined if not found. + */ + protected getRouteSnapshotParam(name: string, route?: ActivatedRoute): T | undefined { + if (!route?.snapshot) { + return; + } + + const value = route.snapshot.queryParams[name] ?? route.snapshot.params[name]; + + if (typeof value != 'undefined') { + return value; + } + + return this.getRouteSnapshotParam(name, route.parent || undefined); + } + /** * Get a parameter for the current route. * Please notice that objects can only be retrieved once. You must call this function only once per page and parameter, * unless there's a new navigation to the page. * * @param name Name of the parameter. + * @param params Optional params to get the value from. If missing, it will autodetect. * @return Value of the parameter, undefined if not found. */ getRouteParam(name: string, params?: Params): T | undefined { @@ -256,15 +278,16 @@ export class CoreNavigatorService { if (!params) { const route = this.getCurrentRoute(); - if (!route.snapshot) { - return; - } - value = route.snapshot.queryParams[name] ?? route.snapshot.params[name]; + value = this.getRouteSnapshotParam(name, route); } else { value = params[name]; } + if (typeof value == 'undefined') { + return; + } + let storedParam = this.storedParams[value]; // Remove the parameter from our map if it's in there. @@ -286,6 +309,7 @@ export class CoreNavigatorService { * Angular router automatically converts numbers to string, this function automatically converts it back to number. * * @param name Name of the parameter. + * @param params Optional params to get the value from. If missing, it will autodetect. * @return Value of the parameter, undefined if not found. */ getRouteNumberParam(name: string, params?: Params): number | undefined { @@ -299,6 +323,7 @@ export class CoreNavigatorService { * Angular router automatically converts booleans to string, this function automatically converts it back to boolean. * * @param name Name of the parameter. + * @param params Optional params to get the value from. If missing, it will autodetect. * @return Value of the parameter, undefined if not found. */ getRouteBooleanParam(name: string, params?: Params): boolean | undefined {