Skip to content

Commit 26a2000

Browse files
committed
[#2, #3] - update README and add redirectPath option to dlg cfg
1 parent 8196a4b commit 26a2000

24 files changed

+381
-61
lines changed

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [2019] [Ante Grgić]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 135 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,148 @@
1-
# NgDialogRouter
1+
# ng-dialog-router
22

3-
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.3.1.
3+
A small angular library that allows you to configure instances of @angular/material's MatDialog as part of your routing configuration.
44

5-
## Development server
5+
## Getting Started
6+
In your angular app, run `npm install ng-dialog-router --save` OR `yarn add ng-dialog-router`
67

7-
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
8+
### Prerequisites
9+
Starting from scratch, please do the following:
10+
- Generate Angular App Using [Angular CLI](https://cli.angular.io/)
11+
- Install Angular Material by running `npm install @angular/material --save` OR `yarn add @angular/material`
12+
- Install ng-dialog-router by running `npm install ng-dialog-router --save` OR `yarn add ng-dialog-router`
13+
- Finally, in src/styles.scss, import the Angular Material core styles as `@import "~@angular/material/prebuilt-themes/indigo-pink.css";`
814

9-
## Code scaffolding
15+
### Usage
1016

11-
Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`.
17+
#### Essentials
18+
ng-dialog-router simply uses an angular route resolver to convert your
19+
normal route configuration into one that is displayed in a dialog.
1220

13-
## Build
21+
It should be provided to the `dlgRef` resolve property.
1422

15-
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.
23+
```ts
24+
import { DialogResolverService } from 'ng-dialog-router';
25+
import { SampleDialogComponent } from '../sample-dialog/sample-dialog.component';
1626

17-
## Running unit tests
27+
const route = {
28+
path: '0',
29+
component: SampleDialogComponent,
30+
resolve: { dlgRef: DialogResolverService },
31+
}
32+
```
1833

19-
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
34+
#### Managing the dialog config settings
35+
The typical angular material options can be passed to the `dlg` property of the route `data`.
36+
The Angular Material Dialog documentation can be found here [Angular Material Dialog](https://material.angular.io/components/dialog/overview).
37+
```ts
38+
import { DialogResolverService, DialogRouteConfig } from 'ng-dialog-router';
39+
import { SampleDialogComponent } from '../sample-dialog/sample-dialog.component';
2040

21-
## Running end-to-end tests
41+
const route = {
42+
path: '0',
43+
component: SampleDialogComponent,
44+
resolve: { dlgRef: DialogResolverService },
45+
data: {
46+
dlg: {
47+
data: { name: 'Sample Dialog #a.0' },
48+
position: { left: '0' },
49+
width: '500px',
50+
} as DialogRouteConfig,
51+
},
52+
}
53+
```
2254

23-
Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/).
55+
#### Additional config settings provided by this library
2456

25-
## Further help
57+
For best results, using the strongly typed `DialogRouteConfig` interface for the `route.data.dlg` property
58+
is recommended via the `as DialogRouteConfig` syntax.
2659

27-
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
60+
##### `redirectPath: string[]`
61+
By default, when the dialog is closed it will redirect up one level in the url tree.
62+
So if we are at `/home/0/a` it will navigate back to `/home/0`. ng-dialog-router has an additional
63+
option to allow for a custom redirect path, in case we want the dialog to go back to `/home`
64+
upon closing via the property `redirectPath`.
65+
66+
```ts
67+
const route = {
68+
path: '0',
69+
component: SampleDialogComponent,
70+
resolve: { dlgRef: DialogResolverService },
71+
data: {
72+
dlg: {
73+
data: { name: 'Sample Dialog #a.0' },
74+
position: { left: '0' },
75+
width: '500px',
76+
redirectPath: [ 'home' ],
77+
} as DialogRouteConfig,
78+
},
79+
}
80+
```
81+
82+
### Full Usage Example (implemented in app's routing module)
83+
84+
#### app/routing/routes.ts
85+
```ts
86+
import { Routes } from '@angular/router';
87+
import { DialogResolverService, DialogRouteConfig } from 'ng-dialog-router';
88+
import { SampleDialogComponent } from '../sample-dialog/sample-dialog.component';
89+
import { HomeComponent } from '../home/home.component';
90+
91+
export const routes: Routes = [
92+
{
93+
path: 'home',
94+
component: HomeComponent,
95+
children: [
96+
{
97+
path: '0',
98+
component: SampleDialogComponent,
99+
resolve: { dlgRef: DialogResolverService },
100+
data: {
101+
dlg: {
102+
data: { name: 'Sample Dialog #a.0' },
103+
position: { left: '0' },
104+
redirectPath: ['home'],
105+
width: '500px',
106+
} as DialogRouteConfig,
107+
},
108+
},
109+
],
110+
},
111+
];
112+
```
113+
114+
#### app/routing/routing.module.ts
115+
```ts
116+
import { NgModule } from '@angular/core';
117+
import { CommonModule } from '@angular/common';
118+
import { RouterModule } from '@angular/router';
119+
import { DialogRouterModule } from 'ng-dialog-router';
120+
import { routes } from './routes';
121+
122+
@NgModule({
123+
imports: [
124+
CommonModule,
125+
RouterModule.forRoot(routes),
126+
DialogRouterModule,
127+
],
128+
exports: [
129+
RouterModule,
130+
DialogRouterModule,
131+
]
132+
})
133+
export class RoutingModule { }
134+
135+
```
136+
137+
## Built With
138+
* [Angular CLI](https://cli.angular.io/)
139+
* [Angular Material](https://material.angular.io/)
140+
141+
## Versioning
142+
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/agrgic16/ng-dialog-router/tags).
143+
144+
## Authors
145+
* **Ante Grgić** - *Initial work* - [GitHub](https://github.com/agrgic16)
146+
147+
## License
148+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

e2e/src/app.e2e-spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ describe('workspace-project App', () => {
1313
expect(page.getTitleText()).toEqual('Welcome to ng-dialog-router!');
1414
});
1515

16+
it('should navigate to a dialog route', () => {
17+
page.navigateToDialog0();
18+
expect(page.getDialog0TitleText()).toEqual('sample-dialog works!');
19+
});
20+
1621
afterEach(async () => {
1722
// Assert that there are no errors emitted from the browser
1823
const logs = await browser.manage().logs().get(logging.Type.BROWSER);

e2e/src/app.po.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ export class AppPage {
88
getTitleText() {
99
return element(by.css('app-root h1')).getText() as Promise<string>;
1010
}
11+
12+
navigateToDialog0() {
13+
return browser.get(`${browser.baseUrl}/home/0`);
14+
}
15+
16+
getDialog0TitleText() {
17+
return element(by.css('h3')).getText() as Promise<string>;
18+
}
1119
}

package.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
{
22
"name": "ng-dialog-router-app",
33
"version": "0.0.0",
4+
"license": "MIT",
5+
"author": {
6+
"name": "Ante Grgić",
7+
"email": "ante16@gmail.com"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/agrgic16/ng-dialog-router"
12+
},
13+
"keywords": [
14+
"angular",
15+
"@angular",
16+
"@angular/material",
17+
"angular/router",
18+
"ng",
19+
"dialog",
20+
"modal",
21+
"router",
22+
"routing",
23+
"material",
24+
"mat-dialog",
25+
"ng-dialog-router"
26+
],
427
"scripts": {
528
"ng": "ng",
629
"start": "ng serve",
730
"build": "ng build",
831
"build_lib": "ng build ng-dialog-router",
32+
"copy_readme": "cp README.md dist/ng-dialog-router",
933
"npm_pack": "cd dist/ng-dialog-router && npm pack",
10-
"package": "npm run build_lib && npm run npm_pack",
34+
"package": "npm run build_lib && npm run copy_readme && npm run npm_pack",
1135
"test": "ng test",
1236
"lint": "ng lint",
1337
"e2e": "ng e2e"

projects/ng-dialog-router/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-dialog-router",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"license": "MIT",
55
"author": {
66
"name": "Ante Grgić",

projects/ng-dialog-router/src/lib/dialog-resolver.service.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
import { Injectable } from '@angular/core';
2-
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot, ActivatedRoute, Router } from '@angular/router';
3-
import { of } from 'rxjs';
4-
import { tap, takeUntil, take } from 'rxjs/operators';
52
import { MatDialog, MatDialogRef } from '@angular/material';
6-
import { DialogRouterComponent } from './dialog-router.component';
3+
import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
4+
import { tap, takeUntil, take, mapTo } from 'rxjs/operators';
5+
import { DialogRouteConfig } from './models/dialog-route-config.model';
76

87
@Injectable()
98
export class DialogResolverService implements Resolve<MatDialogRef<any>> {
10-
dialogRef: MatDialogRef<DialogRouterComponent>;
11-
constructor(
12-
public dialog: MatDialog,
13-
public route: ActivatedRoute,
14-
public router: Router,
15-
) { }
9+
dialogRef: MatDialogRef<any>;
10+
constructor(public dialog: MatDialog, public router: Router) { }
1611

1712
resolve(nextRoute: ActivatedRouteSnapshot, routerState: RouterStateSnapshot) {
18-
this.dialogRef = this.dialog.open( DialogRouterComponent, { ...nextRoute.data.dlg } );
13+
let redirect: string[];
14+
let cfg: DialogRouteConfig;
15+
const { data } = nextRoute;
16+
if (!!data && !!data.dlg) {
17+
const { redirectPath, ...dlgCfg } = data.dlg as DialogRouteConfig;
18+
redirect = redirectPath;
19+
cfg = dlgCfg;
20+
} else {
21+
const { redirectPath, ...dlgCfg } = { } as DialogRouteConfig;
22+
redirect = redirectPath;
23+
cfg = dlgCfg;
24+
}
25+
const navigateAfterClose = redirect || routerState.url.split('/').filter(p => !nextRoute.url.map(u => u.path).includes(p) && p !== '');
26+
this.dialogRef = this.dialog.open(nextRoute.routeConfig.component, { ...cfg, closeOnNavigation: true });
1927
this.dialogRef.afterClosed().pipe(
20-
tap(() => this.router.navigate(['..'], { relativeTo: this.route })),
28+
tap(() => this.router.navigate(navigateAfterClose)),
2129
take(1),
2230
).subscribe();
2331

24-
return of(this.dialogRef).pipe(
32+
return this.dialogRef.afterOpened().pipe(
33+
mapTo(this.dialogRef),
2534
takeUntil(this.dialogRef.afterClosed()),
2635
);
2736
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { MatDialogConfig } from '@angular/material';
2+
3+
export class DialogRouteConfig extends MatDialogConfig {
4+
redirectPath?: string[];
5+
}

projects/ng-dialog-router/src/public_api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
export * from './lib/dialog-resolver.service';
66
export * from './lib/dialog-router.component';
77
export * from './lib/dialog-router.module';
8+
export * from './lib/models/dialog-route-config.model';

src/app/app.component.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,5 @@ <h1>
55
</h1>
66
<img width="300" alt="Angular Logo" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
77
</div>
8+
<a [routerLink]="['home']">home</a>
89
<router-outlet></router-outlet>
9-
<ul>
10-
<li *ngFor="let route of dialogRoutes">
11-
<a [routerLink]="route">{{route.join('/')}}</a>
12-
</li>
13-
</ul>

0 commit comments

Comments
 (0)