Skip to content

Commit 226bb25

Browse files
committed
feat(core): Added an abstraction for the router access
fixes #21
1 parent 128c233 commit 226bb25

7 files changed

Lines changed: 71 additions & 13 deletions

File tree

projects/ngqp/core/src/lib/core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ export {
2020
DEFAULT_BOOLEAN_DESERIALIZER,
2121
} from './serializers';
2222

23-
export * from './accessors/accessors';
23+
export * from './accessors/accessors';
24+
export * from './router-adapter/router-adapter';

projects/ngqp/core/src/lib/query-param-group.directive.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Directive, Input, OnDestroy, OnInit } from '@angular/core';
2-
import { ActivatedRoute, Params, Router } from '@angular/router';
1+
import { Directive, Inject, Input, OnDestroy, OnInit } from '@angular/core';
2+
import { Params } from '@angular/router';
33
import { Subject } from 'rxjs';
44
import { bufferTime, concatMap, debounceTime, map, takeUntil, tap } from 'rxjs/operators';
5+
import { NGQP_ROUTER_ADAPTER, RouterAdapter } from './router-adapter/router-adapter.interface';
56
import { QueryParamNameDirective } from './query-param-name.directive';
67
import { QueryParamControl, QueryParamGroup } from './model';
78
import { isMissing } from './util';
@@ -25,10 +26,7 @@ export class QueryParamGroupDirective implements OnInit, OnDestroy {
2526
private queue$ = new Subject<Params>();
2627
private destroy$ = new Subject<void>();
2728

28-
constructor(
29-
private router: Router,
30-
private route: ActivatedRoute,
31-
) {
29+
constructor(@Inject(NGQP_ROUTER_ADAPTER) private routerAdapter: RouterAdapter) {
3230
this.setupNavigationQueue();
3331
}
3432

@@ -38,7 +36,7 @@ export class QueryParamGroupDirective implements OnInit, OnDestroy {
3836
control.registerOnChange((newModel: any) => this.enqueueNavigation(this.getParamsForModel(control, newModel)));
3937
});
4038

41-
this.route.queryParamMap.subscribe(queryParamMap => {
39+
this.routerAdapter.queryParamMap.subscribe(queryParamMap => {
4240
Object.keys(this.queryParamGroup.controls).forEach(controlName => {
4341
const control: QueryParamControl<any> = this.queryParamGroup.get(controlName);
4442
const newModel = control.deserialize(queryParamMap.get(control.name));
@@ -107,11 +105,7 @@ export class QueryParamGroupDirective implements OnInit, OnDestroy {
107105
return { ...a, ...b };
108106
}, {})),
109107

110-
concatMap(params => this.router.navigate([], {
111-
relativeTo: this.route,
112-
queryParamsHandling: 'merge',
113-
queryParams: params,
114-
})),
108+
concatMap(params => this.routerAdapter.navigate(params)),
115109
).subscribe();
116110
}
117111

projects/ngqp/core/src/lib/query-param.module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
RangeControlValueAccessorDirective,
1010
SelectControlValueAccessorDirective
1111
} from './accessors/accessors';
12+
import { DefaultRouterAdapter, NGQP_ROUTER_ADAPTER } from './router-adapter/router-adapter';
1213

1314
const DIRECTIVES: Type<any>[] = [
1415
QueryParamNameDirective,
@@ -27,6 +28,9 @@ const DIRECTIVES: Type<any>[] = [
2728
imports: [],
2829
declarations: [ DIRECTIVES ],
2930
exports: [ DIRECTIVES ],
31+
providers: [
32+
{ provide: NGQP_ROUTER_ADAPTER, useClass: DefaultRouterAdapter },
33+
],
3034
})
3135
export class QueryParamModule {
3236
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRoute, Params, Router } from '@angular/router';
3+
import { RouterAdapter } from './router-adapter.interface';
4+
5+
@Injectable()
6+
export class DefaultRouterAdapter implements RouterAdapter {
7+
8+
constructor(private router: Router, private route: ActivatedRoute) {
9+
}
10+
11+
public get url() {
12+
return this.router.url;
13+
}
14+
15+
public get queryParamMap() {
16+
return this.route.queryParamMap;
17+
}
18+
19+
public navigate(queryParams: Params): Promise<boolean> {
20+
return this.router.navigate([], {
21+
relativeTo: this.route,
22+
queryParamsHandling: 'merge',
23+
queryParams: queryParams,
24+
});
25+
}
26+
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { InjectionToken } from '@angular/core';
2+
import { ParamMap, Params } from '@angular/router';
3+
import { Observable } from 'rxjs';
4+
5+
/**
6+
* TODO Documentation
7+
*/
8+
export interface RouterAdapter {
9+
10+
/**
11+
* TODO Documentation
12+
*/
13+
url: string;
14+
15+
/**
16+
* TODO Documentation
17+
*/
18+
queryParamMap: Observable<ParamMap>;
19+
20+
/**
21+
* TODO Documentation
22+
*/
23+
navigate(queryParams: Params): Promise<boolean>;
24+
25+
}
26+
27+
/**
28+
* TODO Documentation
29+
*/
30+
export const NGQP_ROUTER_ADAPTER = new InjectionToken<RouterAdapter>('NGQP_ROUTER_ADAPTER');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { RouterAdapter, NGQP_ROUTER_ADAPTER } from './router-adapter.interface';
2+
export { DefaultRouterAdapter } from './default-router-adapter.service';

projects/ngqp/core/src/lib/router-adapter/test-router-adapter.service.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)