Skip to content
This repository was archived by the owner on Oct 1, 2018. It is now read-only.

Commit f560418

Browse files
committed
refactor(operators): Separate operators in multiple pages
1 parent a72064d commit f560418

File tree

5 files changed

+81
-40
lines changed

5 files changed

+81
-40
lines changed

src/app/operators/components/operator/operator.component.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,46 @@ import {
22
Component,
33
Input,
44
OnInit,
5-
ChangeDetectionStrategy
6-
} from "@angular/core";
7-
import { OperatorDoc } from "../../../../operator-docs/operator.model";
5+
ChangeDetectionStrategy,
6+
Inject,
7+
InjectionToken
8+
} from '@angular/core';
9+
import { ActivatedRoute } from '@angular/router';
10+
import { OperatorDoc } from '../../../../operator-docs/operator.model';
11+
import 'rxjs/add/operator/pluck';
12+
13+
export const OPERATOR_TOKEN = new InjectionToken<string>('operators');
814

915
@Component({
10-
selector: "app-operator",
11-
templateUrl: "./operator.component.html",
12-
styleUrls: ["./operator.component.scss"],
13-
changeDetection: ChangeDetectionStrategy.OnPush
16+
selector: 'app-operator',
17+
templateUrl: './operator.component.html',
18+
styleUrls: ['./operator.component.scss']
1419
})
15-
export class OperatorComponent {
16-
@Input() operator: OperatorDoc;
17-
18-
private readonly baseSourceUrl = "https://github.com/ReactiveX/rxjs/blob/master/src/operators/";
19-
private readonly baseSpecUrl = "http://reactivex.io/rxjs/test-file/spec-js/operators";
20+
export class OperatorComponent implements OnInit {
21+
public operator: OperatorDoc;
22+
23+
private readonly baseSourceUrl = 'https://github.com/ReactiveX/rxjs/blob/master/src/operators/';
24+
private readonly baseSpecUrl = 'http://reactivex.io/rxjs/test-file/spec-js/operators';
25+
26+
constructor(
27+
private _activatedRoute: ActivatedRoute,
28+
@Inject(OPERATOR_TOKEN) public operators: OperatorDoc[]
29+
) {}
30+
31+
ngOnInit() {
32+
this._activatedRoute.params.pluck('operator').subscribe((name: string) => {
33+
this.operator = this.operators.filter(
34+
(operator: OperatorDoc) => operator.name === name
35+
)[0];
36+
});
37+
}
2038

2139
get operatorName() {
2240
return this.operator.name;
2341
}
2442

2543
get signature() {
26-
return this.operator.signature || "Signature Placeholder";
44+
return this.operator.signature || 'Signature Placeholder';
2745
}
2846

2947
get marbleUrl() {

src/app/operators/operators-routing.module.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@ import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
33

44
import { OperatorsComponent } from './operators.component';
5+
import { OperatorComponent } from './components/operator/operator.component';
56

6-
const routes: Routes = [{ path: '', component: OperatorsComponent }];
7+
const routes: Routes = [
8+
{
9+
path: '',
10+
component: OperatorsComponent,
11+
children: [{ path: ':operator', component: OperatorComponent }]
12+
}
13+
];
714

815
@NgModule({
916
imports: [RouterModule.forChild(routes)],

src/app/operators/operators.component.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,12 @@
1111
<h3 mat-subheader class="category-subheader">{{ category }}</h3>
1212
<a mat-list-item
1313
*ngFor="let operator of groupedOperators[category]"
14-
[href]="'/operators#' + operator.name">
14+
[routerLink]="['/operators', operator.name]">
1515
{{ operator.name }}
1616
</a>
1717
</mat-nav-list>
1818
</mat-sidenav>
19-
<app-operator
20-
*ngFor="let operator of operators"
21-
[operator]="operator">
22-
</app-operator>
19+
<router-outlet></router-outlet>
2320
</mat-sidenav-container>
2421
<button
2522
*ngIf="smallScreen"

src/app/operators/operators.component.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
import { Component, Inject, InjectionToken, OnInit, AfterViewInit, ChangeDetectionStrategy } from '@angular/core';
2-
import { trigger, state, style, animate, transition } from '@angular/animations';
1+
import {
2+
Component,
3+
Inject,
4+
InjectionToken,
5+
OnInit,
6+
AfterViewInit,
7+
ChangeDetectionStrategy
8+
} from '@angular/core';
9+
import {
10+
trigger,
11+
state,
12+
style,
13+
animate,
14+
transition
15+
} from '@angular/animations';
316
import { Router, ActivatedRoute } from '@angular/router';
417
import { BreakpointObserver } from '@angular/cdk/layout';
518
import { Subscription } from 'rxjs/Subscription';
@@ -21,7 +34,7 @@ interface OperatorDocMap {
2134
styleUrls: ['./operators.component.scss'],
2235
animations: [
2336
trigger('growInOut', [
24-
state('in', style({opacity: 1})),
37+
state('in', style({ opacity: 1 })),
2538
transition('void => *', [
2639
style({
2740
opacity: 0,
@@ -30,10 +43,13 @@ interface OperatorDocMap {
3043
animate(`150ms ease-in`)
3144
]),
3245
transition('* => void', [
33-
animate(`150ms ease-out`, style({
34-
opacity: 0,
35-
transform: 'scale3d(.3, .3, .3)'
36-
}))
46+
animate(
47+
`150ms ease-out`,
48+
style({
49+
opacity: 0,
50+
transform: 'scale3d(.3, .3, .3)'
51+
})
52+
)
3753
])
3854
])
3955
]
@@ -49,14 +65,14 @@ export class OperatorsComponent implements OnInit, AfterViewInit {
4965
private _router: Router,
5066
private _activatedRoute: ActivatedRoute,
5167
@Inject(OPERATORS_TOKEN) public operators: OperatorDoc[]
52-
) { }
68+
) {}
5369

5470
ngOnInit() {
5571
this.groupedOperators = groupOperatorsByType(this.operators);
5672
this.categories = Object.keys(this.groupedOperators);
57-
this._subscription = this._activatedRoute
58-
.fragment
59-
.subscribe(name => this.scrollToOperator(name));
73+
this._subscription = this._activatedRoute.fragment.subscribe(name =>
74+
this.scrollToOperator(name)
75+
);
6076
}
6177

6278
ngAfterViewInit() {
@@ -69,10 +85,6 @@ export class OperatorsComponent implements OnInit, AfterViewInit {
6985
}
7086
}
7187

72-
updateUrl(name: string) {
73-
this._router.navigate([ '/operators' ], { fragment: name });
74-
}
75-
7688
scrollToOperator(name: string) {
7789
const element = document.getElementById(name);
7890

@@ -90,20 +102,21 @@ export class OperatorsComponent implements OnInit, AfterViewInit {
90102
}
91103

92104
get operatorMenuGap() {
93-
return this.extraSmallScreen ? OPERATOR_MENU_GAP_SMALL : OPERATOR_MENU_GAP_LARGE;
105+
return this.extraSmallScreen
106+
? OPERATOR_MENU_GAP_SMALL
107+
: OPERATOR_MENU_GAP_LARGE;
94108
}
95109

96110
get sidenavMode() {
97111
return this.smallScreen ? 'over' : 'side';
98112
}
99-
100113
}
101114

102115
export function groupOperatorsByType(operators: OperatorDoc[]): OperatorDocMap {
103116
return operators.reduce((acc: OperatorDocMap, curr: OperatorDoc) => {
104117
if (acc[curr.operatorType]) {
105-
return { ...acc, [ curr.operatorType ] : [ ...acc[ curr.operatorType ], curr ] };
118+
return { ...acc, [curr.operatorType]: [...acc[curr.operatorType], curr] };
106119
}
107-
return { ...acc, [ curr.operatorType ]: [ curr ] };
120+
return { ...acc, [curr.operatorType]: [curr] };
108121
}, {});
109122
}

src/app/operators/operators.module.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import { ClipboardModule } from 'ngx-clipboard';
99
import { ALL_OPERATORS, OperatorDoc } from '../../operator-docs';
1010
import { OperatorsRoutingModule } from './operators-routing.module';
1111
import { OperatorsComponent, OPERATORS_TOKEN } from './operators.component';
12-
import { OperatorComponent } from './components/operator/operator.component';
12+
import {
13+
OperatorComponent,
14+
OPERATOR_TOKEN
15+
} from './components/operator/operator.component';
1316
import { OperatorHeaderComponent } from './components/operator-header/operator-header.component';
1417
import { OperatorParametersComponent } from './components/operator-parameters/operator-parameters.component';
1518
import { OperatorExamplesComponent } from './components/operator-examples/operator-examples.component';
@@ -43,7 +46,10 @@ import { SharedModule } from '../shared.module';
4346
ClipboardModule,
4447
LayoutModule
4548
],
46-
providers: [{ provide: OPERATORS_TOKEN, useValue: ALL_OPERATORS }],
49+
providers: [
50+
{ provide: OPERATORS_TOKEN, useValue: ALL_OPERATORS },
51+
{ provide: OPERATOR_TOKEN, useValue: ALL_OPERATORS }
52+
],
4753
schemas: [CUSTOM_ELEMENTS_SCHEMA]
4854
})
4955
export class OperatorsModule {}

0 commit comments

Comments
 (0)