Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 0d08b11

Browse files
committed
feat: lazy load component sidenav
- update a couple services to use `providedIn: 'root'` Relates to #176
1 parent 7a099d4 commit 0d08b11

File tree

14 files changed

+94
-107
lines changed

14 files changed

+94
-107
lines changed

src/app/app-module.ts

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,25 @@ import {BrowserModule} from '@angular/platform-browser';
22
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
33
import {NgModule} from '@angular/core';
44
import {LocationStrategy, PathLocationStrategy} from '@angular/common';
5-
import {FormsModule} from '@angular/forms';
65
import {RouterModule} from '@angular/router';
7-
import {ExampleModule} from '@angular/components-examples';
86

97
import {MaterialDocsApp} from './material-docs-app';
108
import {MATERIAL_DOCS_ROUTES} from './routes';
11-
import {ComponentListModule} from './pages/component-list';
12-
import {ComponentViewerModule} from './pages/component-viewer/component-viewer';
13-
import {ComponentCategoryListModule} from './pages/component-category-list/component-category-list';
14-
import {ComponentSidenavModule} from './pages/component-sidenav/component-sidenav';
15-
import {FooterModule} from './shared/footer/footer';
16-
import {ComponentPageTitle} from './pages/page-title/page-title';
17-
import {ComponentHeaderModule} from './pages/component-page-header/component-page-header';
18-
import {StyleManager} from './shared/style-manager';
19-
import {SvgViewerModule} from './shared/svg-viewer/svg-viewer';
20-
import {ThemePickerModule} from './shared/theme-picker';
21-
import {StackBlitzButtonModule} from './shared/stack-blitz';
229
import {NavBarModule} from './shared/navbar';
23-
import {ThemeStorage} from './shared/theme-picker/theme-storage/theme-storage';
24-
import {GuideItems} from './shared/guide-items/guide-items';
25-
import {DocumentationItems} from './shared/documentation-items/documentation-items';
26-
import {DocViewerModule} from './shared/doc-viewer/doc-viewer-module';
27-
import {CanActivateComponentSidenav} from './pages/component-sidenav/component-sidenav-can-load-guard';
28-
import {HttpClientModule} from '@angular/common/http';
29-
import {GaService} from './shared/ga/ga';
3010

3111
@NgModule({
3212
imports: [
3313
BrowserModule,
3414
BrowserAnimationsModule,
35-
ExampleModule,
36-
FormsModule,
37-
HttpClientModule,
3815
RouterModule.forRoot(MATERIAL_DOCS_ROUTES, {
3916
scrollPositionRestoration: 'enabled',
4017
anchorScrolling: 'enabled',
4118
relativeLinkResolution: 'corrected'
4219
}),
43-
ComponentCategoryListModule,
44-
ComponentHeaderModule,
45-
ComponentListModule,
46-
ComponentSidenavModule,
47-
ComponentViewerModule,
48-
DocViewerModule,
49-
FooterModule,
5020
NavBarModule,
51-
StackBlitzButtonModule,
52-
SvgViewerModule,
53-
ThemePickerModule,
5421
],
5522
declarations: [MaterialDocsApp],
56-
providers: [
57-
ComponentPageTitle,
58-
DocumentationItems,
59-
GaService,
60-
GuideItems,
61-
StyleManager,
62-
ThemeStorage,
63-
CanActivateComponentSidenav,
64-
{provide: LocationStrategy, useClass: PathLocationStrategy},
65-
],
23+
providers: [{provide: LocationStrategy, useClass: PathLocationStrategy}],
6624
bootstrap: [MaterialDocsApp],
6725
})
6826
export class AppModule {}

src/app/pages/component-category-list/component-category-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export class ComponentCategoryList implements OnInit, OnDestroy {
4343
}
4444

4545
@NgModule({
46-
imports: [SvgViewerModule, MatCardModule, CommonModule, RouterModule],
46+
imports: [CommonModule, SvgViewerModule, MatCardModule, RouterModule],
4747
exports: [ComponentCategoryList],
4848
declarations: [ComponentCategoryList],
49-
providers: [DocumentationItems, ComponentPageTitle],
49+
providers: [DocumentationItems],
5050
})
5151
export class ComponentCategoryListModule { }

src/app/pages/component-list/component-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ export class ComponentList {
3838
}
3939

4040
@NgModule({
41-
imports: [SvgViewerModule, RouterModule, CommonModule, MatCardModule],
41+
imports: [CommonModule, SvgViewerModule, MatCardModule, RouterModule],
4242
exports: [ComponentList],
4343
declarations: [ComponentList],
44-
providers: [DocumentationItems, ComponentPageTitle],
44+
providers: [DocumentationItems],
4545
})
4646
export class ComponentListModule { }

src/app/pages/component-page-header/component-page-header.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,5 @@ export class ComponentPageHeader {
2323
imports: [MatButtonModule, MatIconModule, NavigationFocusModule],
2424
exports: [ComponentPageHeader],
2525
declarations: [ComponentPageHeader],
26-
providers: [ComponentPageTitle],
2726
})
2827
export class ComponentHeaderModule { }

src/app/pages/component-sidenav/component-sidenav-can-load-guard.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import {CanActivate, ActivatedRouteSnapshot, Router, RouterStateSnapshot} from '
33
import {SECTIONS} from '../../shared/documentation-items/documentation-items';
44

55
/**
6-
* Guard to determine if the sidenav can load, based on if the section exists in documentation
6+
* Guard to determine if the sidenav can load, based on whether the section exists in documentation
77
* items.
88
*/
9-
@Injectable()
9+
@Injectable({providedIn: 'root'})
1010
export class CanActivateComponentSidenav implements CanActivate {
1111
constructor(private router: Router) {}
1212

1313
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
14-
// Searches if the section defined the base UrlSegment is a valid section from the documentation
15-
// items. If found, returns true to allow activation, otherwise blocks activation and navigates
16-
// to '/'.
14+
// Searches if the section defined in the base UrlSegment is a valid section from the
15+
// documentation items. If found, returns true to allow activation, otherwise blocks activation
16+
// and navigates to '/'.
1717
const sectionFound = Object.keys(SECTIONS).find(
1818
(val => val.toLowerCase() === route.url[0].path.toLowerCase()));
1919
if (sectionFound) { return true; }

src/app/pages/component-sidenav/component-sidenav.ts

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
11
import {
2-
Component, Input, NgZone, ViewEncapsulation, ViewChild, OnInit, NgModule, OnDestroy
2+
Component,
3+
Input,
4+
NgModule,
5+
NgZone,
6+
OnDestroy,
7+
OnInit,
8+
ViewChild,
9+
ViewEncapsulation
310
} from '@angular/core';
411
import {DocumentationItems} from '../../shared/documentation-items/documentation-items';
512
import {MatIconModule} from '@angular/material/icon';
613
import {MatSidenav, MatSidenavModule} from '@angular/material/sidenav';
7-
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
8-
import {ActivatedRoute, Params, Router, RouterModule} from '@angular/router';
14+
import {ActivatedRoute, Params, Router, RouterModule, Routes} from '@angular/router';
915
import {CommonModule} from '@angular/common';
1016
import {ComponentHeaderModule} from '../component-page-header/component-page-header';
1117
import {FooterModule} from '../../shared/footer/footer';
12-
import {Observable, Subject, combineLatest} from 'rxjs';
13-
import {switchMap, takeUntil, startWith, map} from 'rxjs/operators';
14-
import {trigger, animate, state, style, transition} from '@angular/animations';
18+
import {combineLatest, Observable, Subject} from 'rxjs';
19+
import {map, startWith, switchMap, takeUntil} from 'rxjs/operators';
20+
import {animate, state, style, transition, trigger} from '@angular/animations';
1521
import {CdkAccordionModule} from '@angular/cdk/accordion';
1622
import {BreakpointObserver} from '@angular/cdk/layout';
23+
import {
24+
ComponentCategoryList,
25+
ComponentCategoryListModule
26+
} from '../component-category-list/component-category-list';
27+
import {ComponentList, ComponentListModule} from '../component-list';
28+
import {
29+
ComponentApi,
30+
ComponentExamples,
31+
ComponentOverview,
32+
ComponentViewer, ComponentViewerModule
33+
} from '../component-viewer/component-viewer';
34+
import {DocViewerModule} from '../../shared/doc-viewer/doc-viewer-module';
35+
import {FormsModule} from '@angular/forms';
36+
import {HttpClientModule} from '@angular/common/http';
37+
import {StackBlitzButtonModule} from '../../shared/stack-blitz';
38+
import {SvgViewerModule} from '../../shared/svg-viewer/svg-viewer';
39+
import {ExampleModule} from '@angular/components-examples';
1740

1841
const SMALL_WIDTH_BREAKPOINT = 720;
1942

@@ -56,7 +79,6 @@ export class ComponentSidenav implements OnInit {
5679
],
5780
})
5881
export class ComponentNav implements OnInit, OnDestroy {
59-
6082
@Input() params: Observable<Params>;
6183
expansions: {[key: string]: boolean} = {};
6284
private _onDestroy = new Subject<void>();
@@ -113,20 +135,54 @@ export class ComponentNav implements OnInit, OnDestroy {
113135
getExpanded(category: string): boolean {
114136
return this.expansions[category] === undefined ? true : this.expansions[category];
115137
}
116-
117138
}
118139

140+
const routes: Routes = [ {
141+
path : '',
142+
component : ComponentSidenav,
143+
children : [
144+
{path : '', redirectTo : 'categories', pathMatch : 'full'},
145+
{path : 'component/:id', redirectTo : ':id', pathMatch : 'full'},
146+
{path : 'category/:id', redirectTo : '/categories/:id', pathMatch : 'full'},
147+
{
148+
path : 'categories',
149+
children : [
150+
{path : '', component : ComponentCategoryList},
151+
{path : ':id', component : ComponentList},
152+
],
153+
},
154+
{
155+
path : ':id',
156+
component : ComponentViewer,
157+
children : [
158+
{path : '', redirectTo : 'overview', pathMatch : 'full'},
159+
{path : 'overview', component : ComponentOverview, pathMatch : 'full'},
160+
{path : 'api', component : ComponentApi, pathMatch : 'full'},
161+
{path : 'examples', component : ComponentExamples, pathMatch : 'full'},
162+
{path : '**', redirectTo : 'overview'},
163+
],
164+
},
165+
]
166+
} ];
119167

120168
@NgModule({
121169
imports: [
122-
MatSidenavModule,
123-
RouterModule,
124170
CommonModule,
171+
ComponentCategoryListModule,
125172
ComponentHeaderModule,
173+
ComponentListModule,
174+
ComponentViewerModule,
175+
DocViewerModule,
176+
ExampleModule,
126177
FooterModule,
127-
BrowserAnimationsModule,
178+
FormsModule,
179+
HttpClientModule,
180+
CdkAccordionModule,
128181
MatIconModule,
129-
CdkAccordionModule
182+
MatSidenavModule,
183+
StackBlitzButtonModule,
184+
SvgViewerModule,
185+
RouterModule.forChild(routes)
130186
],
131187
exports: [ComponentSidenav],
132188
declarations: [ComponentSidenav, ComponentNav],

src/app/pages/component-viewer/component-viewer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,6 @@ export class ComponentExamples extends ComponentBaseView {
151151
],
152152
exports: [ComponentViewer],
153153
declarations: [ComponentViewer, ComponentOverview, ComponentApi, ComponentExamples],
154-
providers: [DocumentationItems, ComponentPageTitle],
154+
providers: [DocumentationItems],
155155
})
156156
export class ComponentViewerModule {}

src/app/pages/guide-list/guide-list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ const routes: Routes = [ {path : '', component : GuideList} ];
2525
imports: [CommonModule, MatListModule, FooterModule, RouterModule.forChild(routes)],
2626
exports: [GuideList],
2727
declarations: [GuideList],
28-
providers: [GuideItems, ComponentPageTitle],
28+
providers: [GuideItems],
2929
})
3030
export class GuideListModule { }

src/app/pages/guide-viewer/guide-viewer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ const routes: Routes = [ {path : '', component : GuideViewer} ];
4343
imports: [DocViewerModule, FooterModule, TableOfContentsModule, RouterModule.forChild(routes)],
4444
exports: [GuideViewer],
4545
declarations: [GuideViewer],
46-
providers: [GuideItems, ComponentPageTitle],
46+
providers: [GuideItems],
4747
})
4848
export class GuideViewerModule {}

src/app/pages/page-title/page-title.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Title} from '@angular/platform-browser';
44
/**
55
* Service responsible for setting the title that appears above the components and guide pages.
66
*/
7-
@Injectable()
7+
@Injectable({providedIn: 'root'})
88
export class ComponentPageTitle {
99
_title = '';
1010
_originalTitle = 'Angular Material UI component library';

src/app/routes.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import {ComponentList} from './pages/component-list';
21
import {Routes} from '@angular/router';
3-
import {
4-
ComponentApi,
5-
ComponentExamples,
6-
ComponentOverview,
7-
ComponentViewer
8-
} from './pages/component-viewer/component-viewer';
9-
import {ComponentCategoryList} from './pages/component-category-list/component-category-list';
10-
import {ComponentSidenav} from './pages/component-sidenav/component-sidenav';
112
import {CanActivateComponentSidenav} from './pages/component-sidenav/component-sidenav-can-load-guard';
123

134
export const MATERIAL_DOCS_ROUTES: Routes = [
@@ -30,30 +21,8 @@ export const MATERIAL_DOCS_ROUTES: Routes = [
3021
{
3122
path: ':section',
3223
canActivate: [CanActivateComponentSidenav],
33-
component: ComponentSidenav,
34-
children: [
35-
{path: '', redirectTo: 'categories', pathMatch: 'full'},
36-
{path: 'component/:id', redirectTo: ':id', pathMatch: 'full'},
37-
{path: 'category/:id', redirectTo: '/categories/:id', pathMatch: 'full'},
38-
{
39-
path: 'categories',
40-
children: [
41-
{path: '', component: ComponentCategoryList},
42-
{path: ':id', component: ComponentList},
43-
],
44-
},
45-
{
46-
path: ':id',
47-
component: ComponentViewer,
48-
children: [
49-
{path: '', redirectTo: 'overview', pathMatch: 'full'},
50-
{path: 'overview', component: ComponentOverview, pathMatch: 'full'},
51-
{path: 'api', component: ComponentApi, pathMatch: 'full'},
52-
{path: 'examples', component: ComponentExamples, pathMatch: 'full'},
53-
{path: '**', redirectTo: 'overview'},
54-
],
55-
},
56-
],
24+
loadChildren: () =>
25+
import('./pages/component-sidenav/component-sidenav').then(m => m.ComponentSidenavModule)
5726
},
5827
{path: '**', redirectTo: ''},
5928
];

src/app/shared/doc-viewer/doc-viewer-module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {DocViewer} from './doc-viewer';
22
import {ExampleViewer} from '../example-viewer/example-viewer';
3-
import {StackBlitzButtonModule} from '../stack-blitz/stack-blitz-button';
3+
import {StackBlitzButtonModule} from '../stack-blitz';
44
import {MatButtonModule} from '@angular/material/button';
55
import {MatIconModule} from '@angular/material/icon';
66
import {MatSnackBarModule} from '@angular/material/snack-bar';
@@ -16,12 +16,12 @@ import {CopierService} from '../copier/copier.service';
1616
// ExampleViewer is included in the DocViewerModule because they have a circular dependency.
1717
@NgModule({
1818
imports: [
19+
CommonModule,
1920
MatButtonModule,
2021
MatIconModule,
2122
MatTooltipModule,
2223
MatSnackBarModule,
2324
MatTabsModule,
24-
CommonModule,
2525
PortalModule,
2626
StackBlitzButtonModule
2727
],

src/app/shared/ga/ga.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import {Injectable} from '@angular/core';
22

33
import {environment} from '../../../environments/environment';
44

5-
@Injectable()
65
/**
76
* Google Analytics Service - captures app behaviors and sends them to Google Analytics (GA).
87
* Presupposes that GA script has been loaded from a script on the host web page.
98
* Associates data with a GA "property" from the environment (`gaId`).
109
*/
10+
@Injectable({providedIn: 'root'})
1111
export class GaService {
1212

1313
private previousUrl: string;

src/app/shared/navbar/navbar.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import {CommonModule} from '@angular/common';
33
import {MatButtonModule} from '@angular/material/button';
44
import {MatMenuModule} from '@angular/material/menu';
55
import {RouterModule} from '@angular/router';
6-
import {ThemePickerModule} from '../theme-picker/theme-picker';
6+
import {ThemePickerModule} from '../theme-picker';
77
import {VersionPickerModule} from '../version-picker';
88
import {SECTIONS} from '../documentation-items/documentation-items';
9+
import {ThemeStorage} from '../theme-picker/theme-storage/theme-storage';
10+
import {StyleManager} from '../style-manager';
11+
import {HttpClientModule} from '@angular/common/http';
912

1013
const SECTIONS_KEYS = Object.keys(SECTIONS);
1114

@@ -28,14 +31,16 @@ export class NavBar {
2831

2932
@NgModule({
3033
imports: [
34+
CommonModule,
35+
HttpClientModule,
3136
MatButtonModule,
3237
MatMenuModule,
3338
RouterModule,
3439
ThemePickerModule,
3540
VersionPickerModule,
36-
CommonModule
3741
],
3842
exports: [NavBar],
3943
declarations: [NavBar],
44+
providers: [StyleManager, ThemeStorage]
4045
})
4146
export class NavBarModule {}

0 commit comments

Comments
 (0)