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

Commit fd7d5d4

Browse files
josephperrottjelbourn
authored andcommitted
Add CDK section to material.angular.io. (#244)
1 parent 1f537eb commit fd7d5d4

19 files changed

+1624
-818
lines changed

package-lock.json

Lines changed: 1327 additions & 672 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/app-module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ import {DocumentationItems} from './shared/documentation-items/documentation-ite
2929
import {GuideListModule} from './pages/guide-list/guide-list';
3030
import {GuideViewerModule} from './pages/guide-viewer/guide-viewer';
3131
import {DocViewerModule} from './shared/doc-viewer/doc-viewer-module';
32+
import {
33+
CanActivateComponentSidenav
34+
} from './pages/component-sidenav/component-sidenav-can-load-guard';
3235

3336
@NgModule({
3437
imports: [
@@ -62,6 +65,7 @@ import {DocViewerModule} from './shared/doc-viewer/doc-viewer-module';
6265
GuideItems,
6366
StyleManager,
6467
ThemeStorage,
68+
CanActivateComponentSidenav,
6569
{provide: LocationStrategy, useClass: PathLocationStrategy},
6670
{provide: MATERIAL_COMPATIBILITY_MODE, useValue: true},
6771
],

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="docs-component-category-list">
2-
<a *ngFor="let category of docItems.getItemsInCategories()"
3-
class="docs-component-category-list-item"
4-
[routerLink]="['/categories/', category.id]">
2+
<a *ngFor="let category of docItems.getCategories((params | async)?.section)"
3+
class="docs-component-category-list-item"
4+
[routerLink]="['../', category.id]">
55
<mat-card class="docs-component-category-list-card">
66
<mat-card-title>{{category.name}}</mat-card-title>
77
</mat-card>

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
1+
import {async, ComponentFixture, TestBed, inject} from '@angular/core/testing';
2+
import {Router} from '@angular/router';
3+
import {Observable} from 'rxjs/Observable'
24
import {ComponentCategoryList, ComponentCategoryListModule} from './component-category-list';
35
import {DocsAppTestingModule} from '../../testing/testing-module';
46

@@ -16,18 +18,21 @@ describe('ComponentCategoryList', () => {
1618
fixture = TestBed.createComponent(ComponentCategoryList);
1719
});
1820

19-
it('should set page title on init', () => {
21+
it('should set set up base param observable on init', () => {
2022
const component = fixture.componentInstance;
2123
spyOn(component, 'ngOnInit').and.callThrough();
2224
fixture.detectChanges();
2325
expect(component.ngOnInit).toHaveBeenCalled();
24-
expect(component._componentPageTitle.title).toEqual('Component Categories');
26+
expect(component.params).toBeDefined();
2527
});
2628

2729
it('should render a card for every category', () => {
28-
const component = fixture.componentInstance;
2930
fixture.detectChanges();
30-
const categories = component.docItems.getItemsInCategories();
31+
// Params is replaced after ngOnit runs since params is set on init.
32+
fixture.componentInstance.params = Observable.of({'section': 'components'});
33+
fixture.detectChanges();
34+
const component = fixture.componentInstance;
35+
const categories = component.docItems.getCategories('components');
3136
const cards = fixture
3237
.nativeElement.querySelectorAll('.docs-component-category-list-card');
3338
expect(cards.length).toEqual(categories.length);

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
import {Component, NgModule} from '@angular/core';
1+
import {Component, NgModule, OnInit} from '@angular/core';
22
import {MatCardModule} from '@angular/material';
33
import {CommonModule} from '@angular/common';
4-
import {RouterModule} from '@angular/router';
5-
import {DocumentationItems} from '../../shared/documentation-items/documentation-items';
4+
import {ActivatedRoute, Params, RouterModule} from '@angular/router';
5+
import {DocumentationItems, SECTIONS} from '../../shared/documentation-items/documentation-items';
66
import {ComponentPageTitle} from '../page-title/page-title';
77
import {SvgViewerModule} from '../../shared/svg-viewer/svg-viewer';
8+
import {Observable} from 'rxjs/Observable';
9+
import 'rxjs/add/observable/combineLatest';
810

911

1012
@Component({
1113
selector: 'app-component-category-list',
1214
templateUrl: './component-category-list.html',
1315
styleUrls: ['./component-category-list.scss']
1416
})
15-
export class ComponentCategoryList {
17+
export class ComponentCategoryList implements OnInit {
18+
params: Observable<Params>;
19+
1620
constructor(public docItems: DocumentationItems,
17-
public _componentPageTitle: ComponentPageTitle) {}
21+
public _componentPageTitle: ComponentPageTitle,
22+
private _route: ActivatedRoute) {}
1823

1924
ngOnInit() {
20-
this._componentPageTitle.title = 'Component Categories';
25+
// Combine params from all of the path into a single object.
26+
this.params = Observable.combineLatest(
27+
this._route.pathFromRoot.map(route => route.params),
28+
Object.assign);
2129
}
2230
}
2331

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="docs-component-list-category">
22
<a *ngFor="let component of category.items"
33
class="docs-component-list-item"
4-
[routerLink]="['/components/', component.id]">
4+
[routerLink]="'/' + section + '/' + component.id">
55
<mat-card class="docs-component-list-card">
66
<mat-card-title class="docs-component-list-card-title">{{component.name}}</mat-card-title>
77
<div class="docs-component-list-icon-spacer"></div>

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import {DocsAppTestingModule} from '../../testing/testing-module';
66

77
const CATEGORY_ID = 'forms';
88
const mockActivatedRoute = {
9-
params: Observable.create(observer => {
10-
observer.next({id: CATEGORY_ID});
9+
pathFromRoot: Observable.create(observer => {
10+
observer.next({
11+
params: {
12+
section: 'components',
13+
id: CATEGORY_ID,
14+
}
15+
});
1116
observer.complete();
1217
})
1318
};

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {ComponentPageTitle} from '../page-title/page-title';
88
import {SvgViewerModule} from '../../shared/svg-viewer/svg-viewer';
99
import {CommonModule} from '@angular/common';
1010
import {MatCardModule} from '@angular/material';
11+
import {Observable} from 'rxjs/Observable';
12+
import 'rxjs/add/observable/combineLatest';
1113

1214
@Component({
1315
selector: 'app-components',
@@ -16,20 +18,24 @@ import {MatCardModule} from '@angular/material';
1618
})
1719
export class ComponentList {
1820
category: DocCategory;
21+
section: string;
1922

2023
constructor(public docItems: DocumentationItems,
2124
private _componentPageTitle: ComponentPageTitle,
2225
private _route: ActivatedRoute,
23-
private router: Router) {
24-
_route.params.subscribe(p => {
25-
this.category = docItems.getCategoryById(p['id']);
26+
public router: Router) {
27+
Observable
28+
.combineLatest(_route.pathFromRoot.map(route => route.params), Object.assign)
29+
.subscribe(p => {
30+
this.category = docItems.getCategoryById(p['id']);
31+
this.section = p['section'];
2632

27-
if (this.category) {
28-
this._componentPageTitle.title = this.category.name;
29-
} else {
30-
this.router.navigate(['/categories']);
31-
}
32-
});
33+
if (this.category) {
34+
this._componentPageTitle.title = this.category.name;
35+
} else {
36+
this.router.navigate(['../'], {relativeTo: this._route});
37+
}
38+
});
3339
}
3440
}
3541

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {Injectable} from '@angular/core';
2+
import {CanActivate, ActivatedRouteSnapshot, Router, RouterStateSnapshot} from '@angular/router';
3+
import {SECTIONS} from '../../shared/documentation-items/documentation-items';
4+
5+
/**
6+
* Guard to determine if the sidenav can load, based on if the section exists in documentation
7+
* items.
8+
*/
9+
@Injectable()
10+
export class CanActivateComponentSidenav implements CanActivate {
11+
constructor(private router: Router) {}
12+
13+
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 '/'.
17+
const sectionFound = Object.keys(SECTIONS).find(
18+
(val => val.toLowerCase() === route.url[0].path.toLowerCase()));
19+
if (sectionFound) { return true; }
20+
this.router.navigateByUrl('/');
21+
return false;
22+
}
23+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<mat-sidenav #sidenav class="docs-component-viewer-sidenav"
33
[opened]="!isScreenSmall()"
44
[mode]="isScreenSmall() ? 'over' : 'side'">
5-
<nav *ngFor="let category of docItems.getItemsInCategories()">
5+
<nav *ngFor="let category of docItems.getCategories((params | async)?.section)">
66
<h3>{{category.name}}</h3>
77
<ul>
88
<li *ngFor="let component of category.items">
9-
<a [routerLink]="['/components/', component.id]"
9+
<a [routerLink]="'/' + (params | async)?.section+ '/' + component.id"
1010
routerLinkActive="docs-component-viewer-sidenav-item-selected">
1111
{{component.name}}
1212
</a>

0 commit comments

Comments
 (0)