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

Commit dd820e5

Browse files
Splaktarjelbourn
authored andcommitted
Lazy load documentation modules (#698)
Also updates a couple services to use `providedIn: 'root'` Relates to #176
1 parent 0d5c311 commit dd820e5

File tree

15 files changed

+118
-132
lines changed

15 files changed

+118
-132
lines changed

src/app/app-module.ts

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +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 {MatNativeDateModule} from '@angular/material/core';
8-
import {ExampleModule} from '@angular/components-examples';
96

107
import {MaterialDocsApp} from './material-docs-app';
11-
import {HomepageModule} from './pages/homepage';
128
import {MATERIAL_DOCS_ROUTES} from './routes';
13-
import {ComponentListModule} from './pages/component-list';
14-
import {ComponentViewerModule} from './pages/component-viewer/component-viewer';
15-
import {ComponentCategoryListModule} from './pages/component-category-list/component-category-list';
16-
import {ComponentSidenavModule} from './pages/component-sidenav/component-sidenav';
17-
import {FooterModule} from './shared/footer/footer';
18-
import {ComponentPageTitle} from './pages/page-title/page-title';
19-
import {ComponentHeaderModule} from './pages/component-page-header/component-page-header';
20-
import {StyleManager} from './shared/style-manager';
21-
import {SvgViewerModule} from './shared/svg-viewer/svg-viewer';
22-
import {ThemePickerModule} from './shared/theme-picker';
23-
import {StackBlitzButtonModule} from './shared/stack-blitz';
249
import {NavBarModule} from './shared/navbar';
25-
import {ThemeStorage} from './shared/theme-picker/theme-storage/theme-storage';
26-
import {GuideItems} from './shared/guide-items/guide-items';
27-
import {DocumentationItems} from './shared/documentation-items/documentation-items';
28-
import {GuideListModule} from './pages/guide-list';
29-
import {GuideViewerModule} from './pages/guide-viewer';
30-
import {DocViewerModule} from './shared/doc-viewer/doc-viewer-module';
31-
import {
32-
CanActivateComponentSidenav
33-
} from './pages/component-sidenav/component-sidenav-can-load-guard';
34-
import {HttpClientModule} from '@angular/common/http';
35-
import {GaService} from './shared/ga/ga';
3610

3711
@NgModule({
3812
imports: [
3913
BrowserModule,
4014
BrowserAnimationsModule,
41-
ExampleModule,
42-
FormsModule,
43-
HttpClientModule,
44-
MatNativeDateModule,
4515
RouterModule.forRoot(MATERIAL_DOCS_ROUTES, {
4616
scrollPositionRestoration: 'enabled',
4717
anchorScrolling: 'enabled',
4818
relativeLinkResolution: 'corrected'
4919
}),
50-
ComponentCategoryListModule,
51-
ComponentHeaderModule,
52-
ComponentListModule,
53-
ComponentSidenavModule,
54-
ComponentViewerModule,
55-
DocViewerModule,
56-
FooterModule,
57-
GuideListModule,
58-
GuideViewerModule,
59-
HomepageModule,
6020
NavBarModule,
61-
StackBlitzButtonModule,
62-
SvgViewerModule,
63-
ThemePickerModule,
6421
],
6522
declarations: [MaterialDocsApp],
66-
providers: [
67-
ComponentPageTitle,
68-
DocumentationItems,
69-
GaService,
70-
GuideItems,
71-
StyleManager,
72-
ThemeStorage,
73-
CanActivateComponentSidenav,
74-
{provide: LocationStrategy, useClass: PathLocationStrategy},
75-
],
23+
providers: [{provide: LocationStrategy, useClass: PathLocationStrategy}],
7624
bootstrap: [MaterialDocsApp],
7725
})
7826
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Component, NgModule, OnInit} from '@angular/core';
22
import {GuideItems} from '../../shared/guide-items/guide-items';
33
import {MatListModule} from '@angular/material/list';
4-
import {RouterModule} from '@angular/router';
4+
import {RouterModule, Routes} from '@angular/router';
55
import {FooterModule} from '../../shared/footer/footer';
66
import {CommonModule} from '@angular/common';
77
import {ComponentPageTitle} from '../page-title/page-title';
@@ -19,11 +19,12 @@ export class GuideList implements OnInit {
1919
}
2020
}
2121

22+
const routes: Routes = [ {path : '', component : GuideList} ];
2223

2324
@NgModule({
24-
imports: [MatListModule, RouterModule, FooterModule, CommonModule],
25+
imports: [CommonModule, MatListModule, FooterModule, RouterModule.forChild(routes)],
2526
exports: [GuideList],
2627
declarations: [GuideList],
27-
providers: [GuideItems, ComponentPageTitle],
28+
providers: [GuideItems],
2829
})
2930
export class GuideListModule { }

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Component, NgModule, OnInit} from '@angular/core';
2-
import {ActivatedRoute, RouterModule, Router} from '@angular/router';
2+
import {ActivatedRoute, Router, RouterModule, Routes} from '@angular/router';
33
import {GuideItem, GuideItems} from '../../shared/guide-items/guide-items';
44
import {FooterModule} from '../../shared/footer/footer';
55
import {DocViewerModule} from '../../shared/doc-viewer/doc-viewer-module';
@@ -37,10 +37,12 @@ export class GuideViewer implements OnInit {
3737
}
3838
}
3939

40+
const routes: Routes = [ {path : '', component : GuideViewer} ];
41+
4042
@NgModule({
41-
imports: [DocViewerModule, FooterModule, RouterModule, TableOfContentsModule],
43+
imports: [DocViewerModule, FooterModule, TableOfContentsModule, RouterModule.forChild(routes)],
4244
exports: [GuideViewer],
4345
declarations: [GuideViewer],
44-
providers: [GuideItems, ComponentPageTitle],
46+
providers: [GuideItems],
4547
})
4648
export class GuideViewerModule {}

src/app/pages/homepage/homepage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Component, NgModule, OnInit} from '@angular/core';
22
import {SvgViewerModule} from '../../shared/svg-viewer/svg-viewer';
33
import {MatButtonModule} from '@angular/material/button';
44
import {FooterModule} from '../../shared/footer/footer';
5-
import {RouterModule} from '@angular/router';
5+
import {RouterModule, Routes} from '@angular/router';
66
import {ComponentPageTitle} from '../page-title/page-title';
77

88
@Component({
@@ -20,8 +20,10 @@ export class Homepage implements OnInit {
2020
}
2121
}
2222

23+
const routes: Routes = [ {path : '', component : Homepage} ];
24+
2325
@NgModule({
24-
imports: [SvgViewerModule, MatButtonModule, FooterModule, RouterModule],
26+
imports: [SvgViewerModule, MatButtonModule, FooterModule, RouterModule.forChild(routes)],
2527
exports: [Homepage],
2628
declarations: [Homepage],
2729
})

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';

0 commit comments

Comments
 (0)