Skip to content

Commit a2f8a5c

Browse files
authored
Merge pull request #1824 from atmire/w2p-94242_Add-optional-NavigationExtras-argument-to-PaginationService-methods
Add an optional `NavigationExtras` argument to `PaginationService.updateRoute`
2 parents a13f4ee + 6e5fe96 commit a2f8a5c

2 files changed

Lines changed: 54 additions & 16 deletions

File tree

src/app/core/pagination/pagination.service.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ describe('PaginationService', () => {
120120

121121
expect(router.navigate).toHaveBeenCalledWith([], {queryParams: navigateParams, queryParamsHandling: 'merge'});
122122
});
123+
it('should pass on navigationExtras to router.navigate', () => {
124+
service.updateRoute('test', {page: 2}, undefined, undefined, { queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
125+
126+
const navigateParams = {};
127+
navigateParams[`test.page`] = `2`;
128+
navigateParams[`test.rpp`] = `10`;
129+
navigateParams[`test.sf`] = `score`;
130+
navigateParams[`test.sd`] = `ASC`;
131+
132+
expect(router.navigate).toHaveBeenCalledWith([], {queryParams: navigateParams, queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
133+
});
123134
});
124135
describe('updateRouteWithUrl', () => {
125136
it('should update the route with the provided page params and url', () => {
@@ -144,7 +155,17 @@ describe('PaginationService', () => {
144155

145156
expect(router.navigate).toHaveBeenCalledWith(['someUrl'], {queryParams: navigateParams, queryParamsHandling: 'merge'});
146157
});
158+
it('should pass on navigationExtras to router.navigate', () => {
159+
service.updateRouteWithUrl('test',['someUrl'], {page: 2}, undefined, undefined, { queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
147160

161+
const navigateParams = {};
162+
navigateParams[`test.page`] = `2`;
163+
navigateParams[`test.rpp`] = `10`;
164+
navigateParams[`test.sf`] = `score`;
165+
navigateParams[`test.sd`] = `ASC`;
166+
167+
expect(router.navigate).toHaveBeenCalledWith(['someUrl'], {queryParams: navigateParams, queryParamsHandling: 'preserve', replaceUrl: true, preserveFragment: true });
168+
});
148169
});
149170
describe('clearPagination', () => {
150171
it('should clear the pagination next time the updateRoute/updateRouteWithUrl method is called', () => {

src/app/core/pagination/pagination.service.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Injectable } from '@angular/core';
2-
import { Router } from '@angular/router';
2+
import { NavigationExtras, Router } from '@angular/router';
33
import { RouteService } from '../services/route.service';
44
import { PaginationComponentOptions } from '../../shared/pagination/pagination-component-options.model';
55
import { combineLatest as observableCombineLatest, Observable } from 'rxjs';
@@ -117,15 +117,22 @@ export class PaginationService {
117117
* @param params - The page related params to update in the route
118118
* @param extraParams - Addition params unrelated to the pagination that need to be added to the route
119119
* @param retainScrollPosition - Scroll to the pagination component after updating the route instead of the top of the page
120+
* @param navigationExtras - Extra parameters to pass on to `router.navigate`. Can be used to override values set by this service.
120121
*/
121-
updateRoute(paginationId: string, params: {
122-
page?: number
123-
pageSize?: number
124-
sortField?: string
125-
sortDirection?: SortDirection
126-
}, extraParams?, retainScrollPosition?: boolean) {
122+
updateRoute(
123+
paginationId: string,
124+
params: {
125+
page?: number
126+
pageSize?: number
127+
sortField?: string
128+
sortDirection?: SortDirection
129+
},
130+
extraParams?,
131+
retainScrollPosition?: boolean,
132+
navigationExtras?: NavigationExtras,
133+
) {
127134

128-
this.updateRouteWithUrl(paginationId, [], params, extraParams, retainScrollPosition);
135+
this.updateRouteWithUrl(paginationId, [], params, extraParams, retainScrollPosition, navigationExtras);
129136
}
130137

131138
/**
@@ -135,13 +142,21 @@ export class PaginationService {
135142
* @param params - The page related params to update in the route
136143
* @param extraParams - Addition params unrelated to the pagination that need to be added to the route
137144
* @param retainScrollPosition - Scroll to the pagination component after updating the route instead of the top of the page
145+
* @param navigationExtras - Extra parameters to pass on to `router.navigate`. Can be used to override values set by this service.
138146
*/
139-
updateRouteWithUrl(paginationId: string, url: string[], params: {
140-
page?: number
141-
pageSize?: number
142-
sortField?: string
143-
sortDirection?: SortDirection
144-
}, extraParams?, retainScrollPosition?: boolean) {
147+
updateRouteWithUrl(
148+
paginationId: string,
149+
url: string[],
150+
params: {
151+
page?: number
152+
pageSize?: number
153+
sortField?: string
154+
sortDirection?: SortDirection
155+
},
156+
extraParams?,
157+
retainScrollPosition?: boolean,
158+
navigationExtras?: NavigationExtras,
159+
) {
145160
this.getCurrentRouting(paginationId).subscribe((currentFindListOptions) => {
146161
const currentParametersWithIdName = this.getParametersWithIdName(paginationId, currentFindListOptions);
147162
const parametersWithIdName = this.getParametersWithIdName(paginationId, params);
@@ -152,12 +167,14 @@ export class PaginationService {
152167
this.router.navigate(url, {
153168
queryParams: queryParams,
154169
queryParamsHandling: 'merge',
155-
fragment: `p-${paginationId}`
170+
fragment: `p-${paginationId}`,
171+
...navigationExtras,
156172
});
157173
} else {
158174
this.router.navigate(url, {
159175
queryParams: queryParams,
160-
queryParamsHandling: 'merge'
176+
queryParamsHandling: 'merge',
177+
...navigationExtras,
161178
});
162179
}
163180
this.clearParams = {};

0 commit comments

Comments
 (0)