Skip to content

Commit d653c6c

Browse files
committed
feat(docs): Added history and back button to demonstrate replaceUrl
1 parent fce4ec2 commit d653c6c

4 files changed

Lines changed: 92 additions & 27 deletions

File tree

projects/ngqp-demo/src/app/demo-browser/demo-browser.component.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<div class="demo-browser__toolbar">
2+
<button class="demo-browser__button" title="{{ routerAdapter.history.length }} previous routes"
3+
(click)="routerAdapter.goBack()" [disabled]="routerAdapter.history.length === 0">
4+
<fa-icon [icon]="faArrowLeft" size="sm"></fa-icon>
5+
</button>
6+
27
<div class="demo-browser__url">
38
<span class="demo-browser__url__domain" (click)="urlInput.focus()">https://www.app.io</span>
49
<input #urlInput type="text" class="demo-browser__url__input"
5-
[ngModel]="routerAdapter.url" (keyup.enter)="updateQueryParams($event.target.value)" />
10+
[ngModel]="routerAdapter.url" (keyup.enter)="routerAdapter.navigateToQueryParamString($event.target.value)" />
611
</div>
712
</div>
813

projects/ngqp-demo/src/app/demo-browser/demo-browser.component.scss

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
padding: 4px 8px;
3333
}
3434

35+
&__button {
36+
flex-grow: 0;
37+
}
38+
3539
&__url {
3640
flex-grow: 1;
3741

@@ -80,4 +84,33 @@
8084
display: flex;
8185
justify-content: space-between;
8286
}
87+
}
88+
89+
button.demo-browser__button {
90+
@include btn-reset();
91+
outline: 0;
92+
cursor: pointer;
93+
margin-right: 8px;
94+
95+
width: 24px;
96+
height: 24px;
97+
display: flex;
98+
justify-content: center;
99+
align-items: center;
100+
background: transparent;
101+
border-radius: 50%;
102+
103+
&:not(:disabled) {
104+
&:hover {
105+
background: #e0e0e0;
106+
}
107+
108+
&:active {
109+
background: #bdbdbd;
110+
}
111+
}
112+
113+
&:disabled {
114+
color: #9e9e9e;
115+
}
83116
}
Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Component, Inject, Input, OnDestroy, OnInit } from '@angular/core';
22
import { Params } from '@angular/router';
33
import { Subject } from 'rxjs';
4-
import { NGQP_ROUTER_ADAPTER, QueryParamGroup, RouterAdapter } from '@ngqp/core';
5-
import { TestRouterAdapter } from '../test-router-adapter.service';
64
import { takeUntil } from 'rxjs/operators';
5+
import { faArrowLeft } from '@fortawesome/free-solid-svg-icons';
6+
import { NGQP_ROUTER_ADAPTER, QueryParamGroup } from '@ngqp/core';
7+
import { TestRouterAdapter } from '../test-router-adapter.service';
78

89
@Component({
910
selector: 'demo-browser',
@@ -15,10 +16,10 @@ import { takeUntil } from 'rxjs/operators';
1516
})
1617
export class DemoBrowserComponent implements OnInit, OnDestroy {
1718

19+
public faArrowLeft = faArrowLeft;
20+
1821
@Input()
19-
public set initialQueryParams(value: string) {
20-
this.updateQueryParams(value);
21-
}
22+
public initialQueryParams: string;
2223

2324
@Input()
2425
public group: QueryParamGroup;
@@ -28,7 +29,7 @@ export class DemoBrowserComponent implements OnInit, OnDestroy {
2829

2930
private destroy$ = new Subject<void>();
3031

31-
constructor(@Inject(NGQP_ROUTER_ADAPTER) public routerAdapter: RouterAdapter) {
32+
constructor(@Inject(NGQP_ROUTER_ADAPTER) public routerAdapter: TestRouterAdapter) {
3233
}
3334

3435
public ngOnInit() {
@@ -38,26 +39,13 @@ export class DemoBrowserComponent implements OnInit, OnDestroy {
3839
this.lastChange = JSON.stringify(value);
3940
});
4041
}
42+
43+
this.routerAdapter.navigateToQueryParamString(this.initialQueryParams);
4144
}
4245

4346
public ngOnDestroy() {
4447
this.destroy$.next();
4548
this.destroy$.complete();
4649
}
4750

48-
public updateQueryParams(value: string) {
49-
const params: Params = {};
50-
51-
const searchParams = new URLSearchParams(value);
52-
const it = (<any>searchParams).keys();
53-
let current = it.next();
54-
while (!current.done) {
55-
const paramName = current.value;
56-
params[ paramName ] = searchParams.getAll(paramName);
57-
current = it.next();
58-
}
59-
60-
return this.routerAdapter.navigate(params);
61-
}
62-
6351
}

projects/ngqp-demo/src/app/test-router-adapter.service.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
22
import { convertToParamMap, ParamMap, Params } from '@angular/router';
33
import { ReplaySubject } from 'rxjs';
44
import { RouterAdapter } from '@ngqp/core';
5+
import { RouterAdapterOptions } from '../../../ngqp/core/src/lib/router-adapter/router-adapter.interface';
56

67
@Injectable()
78
export class TestRouterAdapter implements RouterAdapter {
@@ -10,25 +11,63 @@ export class TestRouterAdapter implements RouterAdapter {
1011
public readonly queryParamMap = this._queryParamMap.asObservable();
1112

1213
public url: string;
14+
public history: string[] = [];
1315

1416
private _params: Params;
1517

16-
constructor() {
17-
this.params = {};
18-
this.emitQueryParamMap();
19-
}
18+
public navigate(queryParams: Params, extras: RouterAdapterOptions = {}): Promise<boolean> {
19+
const previousUrl = this.url;
2020

21-
public navigate(queryParams: Params): Promise<boolean> {
2221
const newParams = {
2322
...this.params,
2423
...queryParams,
2524
};
2625

2726
this.params = newParams;
27+
if (previousUrl && extras.replaceUrl !== true) {
28+
this.history.push(previousUrl);
29+
}
30+
2831
this.emitQueryParamMap();
2932
return Promise.resolve(true);
3033
}
3134

35+
public navigateToQueryParamString(value: string) {
36+
const params: Params = {};
37+
38+
const searchParams = new URLSearchParams(value);
39+
const it = (<any>searchParams).keys();
40+
let current = it.next();
41+
while (!current.done) {
42+
const paramName = current.value;
43+
params[ paramName ] = searchParams.getAll(paramName);
44+
current = it.next();
45+
}
46+
47+
return this.navigate(params);
48+
}
49+
50+
public goBack(): Promise<boolean> {
51+
if (this.history.length === 0) {
52+
return Promise.resolve(false);
53+
}
54+
55+
// Get the previous URL
56+
const previous = this.history.pop();
57+
58+
// We need this to not be a "queryParamsHandling: merge" navigation,
59+
// so we fake this by removing the currently set params first.
60+
this._params = {};
61+
62+
const result = this.navigateToQueryParamString(previous);
63+
64+
// Since this navigation added another state to the history, we
65+
// have to remove it again
66+
this.history.pop();
67+
68+
return result;
69+
}
70+
3271
private set params(params: Params) {
3372
this._params = params;
3473

0 commit comments

Comments
 (0)