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

Commit d8403be

Browse files
jelbournandrewseguin
authored andcommitted
Disambiguate between material and cdk docs (#286)
* Disambiguate between material and cdk docs * Refactor rxjs bits
1 parent 1752a47 commit d8403be

File tree

7 files changed

+64
-63
lines changed

7 files changed

+64
-63
lines changed

src/app/pages/component-viewer/component-api.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
API for {{componentViewer.componentDocItem.id}}
33
</span>
44
<doc-viewer
5-
documentUrl="/assets/documents/api/{{componentViewer.componentDocItem.id}}.html"
5+
documentUrl="/assets/documents/api/{{componentViewer.componentDocItem.packageName}}-{{componentViewer.componentDocItem.id}}.html"
66
class="docs-component-view-text-content docs-component-api"
77
(contentLoaded)="toc.updateScrollPosition()"></doc-viewer>
8-
<table-of-contents #toc
9-
headerSelectors=".docs-api-h3,.docs-api-h4"
8+
<table-of-contents #toc
9+
headerSelectors=".docs-api-h3,.docs-api-h4"
1010
container=".mat-drawer-content"></table-of-contents>

src/app/pages/component-viewer/component-overview.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Overview for {{componentViewer.componentDocItem.id}}
33
</span>
44
<doc-viewer
5-
documentUrl="/assets/documents/overview/{{componentViewer.componentDocItem.id}}.html"
5+
documentUrl="/assets/documents/overview/{{componentViewer.componentDocItem.packageName}}-{{componentViewer.componentDocItem.id}}.html"
66
class="docs-component-view-text-content docs-component-overview"
77
(contentLoaded)="toc.updateScrollPosition()">
88
</doc-viewer>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('ComponentViewer', () => {
4242
it('should set page title correctly', () => {
4343
const component = fixture.componentInstance;
4444
fixture.detectChanges();
45-
const expected = `${component.docItems.getItemById(docItemsId).name}`;
45+
const expected = `${component.docItems.getItemById(docItemsId, 'material').name}`;
4646
expect(component._componentPageTitle.title).toEqual(expected);
4747
});
4848
});

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {Component, OnInit, NgModule, ElementRef, ViewEncapsulation, ViewChild} from '@angular/core';
2-
import {ActivatedRoute, Router, RouterModule} from '@angular/router';
2+
import {ActivatedRoute, Params, Router, RouterModule} from '@angular/router';
33
import {DocumentationItems, DocItem} from '../../shared/documentation-items/documentation-items';
44
import {ComponentPageTitle} from '../page-title/page-title';
55
import {MatTabsModule} from '@angular/material';
66
import {DocViewerModule} from '../../shared/doc-viewer/doc-viewer-module';
77
import {CommonModule} from '@angular/common';
88
import {TableOfContentsModule} from '../../shared/table-of-contents/table-of-contents.module';
9+
import {Observable} from 'rxjs/Observable';
10+
911

1012
@Component({
1113
selector: 'app-component-viewer',
@@ -22,19 +24,23 @@ export class ComponentViewer {
2224
private router: Router,
2325
public _componentPageTitle: ComponentPageTitle,
2426
public docItems: DocumentationItems) {
25-
this._route.params.subscribe(params => {
26-
this.componentDocItem = docItems.getItemById(params['id']);
27-
28-
if (this.componentDocItem) {
29-
this._componentPageTitle.title = `${this.componentDocItem.name}`;
30-
this.componentDocItem.examples.length ?
31-
this.sections.add('examples') :
32-
this.sections.delete('examples');
27+
// Listen to changes on the current route for the doc id (e.g. button/checkbox) and the
28+
// parent route for the section (material/cdk).
29+
Observable.combineLatest(_route.params, _route.parent.params)
30+
.map((p: [Params, Params]) => ({id: p[0]['id'], section: p[1]['section']}))
31+
.map(p => docItems.getItemById(p.id, p.section))
32+
.subscribe(d => {
33+
this.componentDocItem = d;
34+
if (this.componentDocItem) {
35+
this._componentPageTitle.title = `${this.componentDocItem.name}`;
36+
this.componentDocItem.examples.length ?
37+
this.sections.add('examples') :
38+
this.sections.delete('examples');
3339

34-
} else {
35-
this.router.navigate(['/components']);
36-
}
37-
});
40+
} else {
41+
this.router.navigate(['/components']);
42+
}
43+
});
3844
}
3945
}
4046

src/app/shared/documentation-items/documentation-items.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ describe('DocViewer', () => {
3737
});
3838

3939
it('should get a doc item by id', () => {
40-
expect(docsItems.getItemById('button')).toBeDefined();
40+
expect(docsItems.getItemById('button', 'material')).toBeDefined();
4141
});
4242
});

src/app/shared/documentation-items/documentation-items.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Injectable} from '@angular/core';
33
export interface DocItem {
44
id: string;
55
name: string;
6+
packageName?: string;
67
examples?: string[];
78
}
89

@@ -107,51 +108,47 @@ const DOCS: {[key: string]: DocCategory[]} = {
107108
}
108109
],
109110
[CDK] : [
110-
{
111-
id: 'components',
112-
name: 'Components',
113-
items: [
114-
{id: 'table', name: 'Table', examples: []},
115-
{id: 'stepper', name: 'Stepper', examples: []},
116-
117-
]
118-
},
119111
{
120112
id: 'component-composition',
121-
name: 'Component Composition',
113+
name: 'Common Behaviors',
122114
items: [
115+
{id: 'a11y', name: 'Accessibility', examples: []},
123116
{id: 'observers', name: 'Observers', examples: []},
124117
{id: 'layout', name: 'Layout', examples: []},
125118
{id: 'overlay', name: 'Overlay', examples: []},
126119
{id: 'portal', name: 'Portal', examples: []},
127120
{id: 'bidi', name: 'Bidirectionality', examples: []},
128121
{id: 'scrolling', name: 'Scrolling', examples: []},
129-
{id: 'viewport', name: 'Viewport', examples: []},
130-
]
131-
},
132-
{
133-
id: 'utilities',
134-
name: 'Utilities',
135-
items: [
136-
{id: 'coercion', name: 'Coercion', examples: []},
137-
{id: 'collections', name: 'Collections', examples: []},
138-
{id: 'keycodes', name: 'Keycodes', examples: []},
139-
{id: 'platform', name: 'Platform', examples: []},
140122
]
141123
},
142124
{
143-
id: 'accessibility',
144-
name: 'Accessibility',
125+
id: 'components',
126+
name: 'Components',
145127
items: [
146-
{id: 'focus-key-manager', name: 'Focus Key Manager', examples: []},
147-
{id: 'focus-trap', name: 'Focus Trap', examples: []},
148-
{id: 'interactivity-checker', name: 'Interactivity Checker', examples: []},
149-
{id: 'list-key-manager', name: 'List Key Manager', examples: []},
150-
{id: 'live-announcer', name: 'Live Announcer', examples: []},
128+
{id: 'table', name: 'Table', examples: []},
129+
{id: 'stepper', name: 'Stepper', examples: []},
130+
151131
]
152132
},
133+
// TODO(jelbourn): re-add utilities and a11y as top-level categories once we can generate
134+
// their API docs with dgeni. Currently our setup doesn't generate API docs for constants
135+
// and standalone functions (much of the utilities) and we have no way of generating API
136+
// docs more granularly than directory-level (within a11y) (same for viewport).
153137
]
154138
};
139+
140+
for (let category of DOCS[COMPONENTS]) {
141+
for (let doc of category.items) {
142+
doc.packageName = 'material';
143+
}
144+
}
145+
146+
for (let category of DOCS[CDK]) {
147+
for (let doc of category.items) {
148+
doc.packageName = 'cdk';
149+
}
150+
}
151+
155152
const ALL_COMPONENTS = DOCS[COMPONENTS].reduce(
156153
(result, category) => result.concat(category.items), []);
157154
const ALL_CDK = DOCS[CDK].reduce((result, cdk) => result.concat(cdk.items), []);
@@ -174,8 +171,9 @@ export class DocumentationItems {
174171
return [];
175172
}
176173

177-
getItemById(id: string): DocItem {
178-
return ALL_DOCS.find(i => i.id === id);
174+
getItemById(id: string, section: string): DocItem {
175+
const sectionLookup = section == 'cdk' ? 'cdk' : 'material';
176+
return ALL_DOCS.find(doc => doc.id === id && doc.packageName == sectionLookup);
179177
}
180178

181179
getCategoryById(id: string): DocCategory {

src/app/shared/table-of-contents/table-of-contents.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import {Component, Input, Inject, ElementRef, OnInit} from '@angular/core';
1+
import {Component, ElementRef, Inject, Input, OnInit} from '@angular/core';
22
import {DOCUMENT} from '@angular/platform-browser';
3-
import {Router, ActivatedRoute, NavigationEnd} from '@angular/router';
4-
import {Observable} from 'rxjs/Observable';
5-
import {Subscription} from 'rxjs/Subscription';
3+
import {ActivatedRoute, NavigationEnd, Router} from '@angular/router';
64
import 'rxjs/add/observable/fromEvent';
75
import 'rxjs/add/operator/debounceTime';
6+
import 'rxjs/add/operator/takeUntil';
7+
import {Observable} from 'rxjs/Observable';
8+
import {Subject} from 'rxjs/Subject';
89

910
interface Link {
1011
/* id of the section*/
@@ -36,17 +37,15 @@ export class TableOfContents implements OnInit {
3637

3738
_rootUrl: string;
3839
private _scrollContainer: any;
39-
private _scrollSubscription: Subscription;
40-
private _routeSubscription: Subscription;
41-
private _fragmentSubscription: Subscription;
40+
private _destroyed = new Subject();
4241
private _urlFragment = '';
4342

4443
constructor(private _router: Router,
4544
private _route: ActivatedRoute,
4645
private _element: ElementRef,
4746
@Inject(DOCUMENT) private _document: Document) {
4847

49-
this._routeSubscription = this._router.events.subscribe((event) => {
48+
this._router.events.takeUntil(this._destroyed).subscribe((event) => {
5049
if (event instanceof NavigationEnd) {
5150
const rootUrl = _router.url.split('#')[0];
5251
if (rootUrl !== this._rootUrl) {
@@ -56,7 +55,7 @@ export class TableOfContents implements OnInit {
5655
}
5756
});
5857

59-
this._fragmentSubscription = this._route.fragment.subscribe(fragment => {
58+
this._route.fragment.takeUntil(this._destroyed).subscribe(fragment => {
6059
this._urlFragment = fragment;
6160

6261
const target = document.getElementById(this._urlFragment);
@@ -73,17 +72,15 @@ export class TableOfContents implements OnInit {
7372
this._scrollContainer = this.container ?
7473
this._document.querySelectorAll(this.container)[0] : window;
7574

76-
this._scrollSubscription = Observable
77-
.fromEvent(this._scrollContainer, 'scroll')
75+
Observable.fromEvent(this._scrollContainer, 'scroll')
76+
.takeUntil(this._destroyed)
7877
.debounceTime(10)
7978
.subscribe(() => this.onScroll());
8079
});
8180
}
8281

8382
ngOnDestroy(): void {
84-
this._routeSubscription.unsubscribe();
85-
this._scrollSubscription.unsubscribe();
86-
this._fragmentSubscription.unsubscribe();
83+
this._destroyed.next();
8784
}
8885

8986
updateScrollPosition(): void {

0 commit comments

Comments
 (0)