|
1 | | -# NgDialogRouter |
| 1 | +# ng-dialog-router |
2 | 2 |
|
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. |
4 | 4 |
|
5 | | -## Development server |
| 5 | +## Getting Started |
| 6 | +In your angular app, run `npm install ng-dialog-router --save` OR `yarn add ng-dialog-router` |
6 | 7 |
|
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";` |
8 | 14 |
|
9 | | -## Code scaffolding |
| 15 | +### Usage |
10 | 16 |
|
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. |
12 | 20 |
|
13 | | -## Build |
| 21 | +It should be provided to the `dlgRef` resolve property. |
14 | 22 |
|
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'; |
16 | 26 |
|
17 | | -## Running unit tests |
| 27 | +const route = { |
| 28 | + path: '0', |
| 29 | + component: SampleDialogComponent, |
| 30 | + resolve: { dlgRef: DialogResolverService }, |
| 31 | +} |
| 32 | +``` |
18 | 33 |
|
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'; |
20 | 40 |
|
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 | +``` |
22 | 54 |
|
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 |
24 | 56 |
|
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. |
26 | 59 |
|
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 |
0 commit comments