Skip to content

Commit 2cf19a3

Browse files
crisbetotinayuangao
authored andcommitted
build: add toggle for OnPush change detection in demo app (#3675)
* build: add toggle for OnPush change detection in demo app Adds a toggle for enabling `OnPush` change detection in the demo app. This should make it easier to test out components with the different change detection strategies. * chore: refer to global variables via the window
1 parent e2f67f5 commit 2cf19a3

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

src/demo-app/demo-app-module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {HttpModule} from '@angular/http';
44
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
55
import {RouterModule} from '@angular/router';
66
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
7-
import {DemoApp, Home} from './demo-app/demo-app';
7+
import {DemoApp, Home, DemoAppOnPush} from './demo-app/demo-app';
88
import {
99
FullscreenOverlayContainer,
1010
MaterialModule,
@@ -69,6 +69,7 @@ import {DatepickerDemo} from './datepicker/datepicker-demo';
6969
CheckboxDemo,
7070
DatepickerDemo,
7171
DemoApp,
72+
DemoAppOnPush,
7273
DialogDemo,
7374
GesturesDemo,
7475
GridListDemo,

src/demo-app/demo-app/demo-app.html

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,29 @@
2525
</button>
2626
<div class="demo-toolbar">
2727
<h1>Angular Material Demos</h1>
28-
<button md-button (click)="dark = !dark">{{dark ? 'Light' : 'Dark'}} theme</button>
29-
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
30-
Fullscreen
31-
</button>
32-
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
33-
{{root.dir.toUpperCase()}}
34-
</button>
28+
<div>
29+
<button md-button (click)="dark = !dark">{{dark ? 'Light' : 'Dark'}} theme</button>
30+
<button md-button (click)="toggleChangeDetection()" title="Toggle change detection">
31+
Change detection: {{changeDetectionStrategy}}
32+
</button>
33+
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
34+
Fullscreen
35+
</button>
36+
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
37+
{{root.dir.toUpperCase()}}
38+
</button>
39+
</div>
3540
</div>
3641
</md-toolbar>
3742

38-
<div #root="$implicit" dir="ltr" class="demo-content">
39-
<router-outlet></router-outlet>
43+
<div #root="$implicit" dir="ltr" class="demo-content" [ngSwitch]="changeDetectionStrategy">
44+
<div *ngSwitchDefault>
45+
<router-outlet></router-outlet>
46+
</div>
47+
48+
<demo-app-on-push *ngSwitchCase="'OnPush'">
49+
<router-outlet></router-outlet>
50+
</demo-app-on-push>
4051
</div>
4152
</div>
4253
</md-sidenav-container>

src/demo-app/demo-app/demo-app.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ body {
3232
.demo-toolbar {
3333
display: flex;
3434
justify-content: space-between;
35+
align-items: center;
3536
width: 100%;
3637
}
3738
}

src/demo-app/demo-app/demo-app.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import {Component, ViewEncapsulation, ElementRef} from '@angular/core';
1+
import {Component, ViewEncapsulation, ElementRef, ChangeDetectionStrategy} from '@angular/core';
22

3+
const changeDetectionKey = 'mdDemoChangeDetection';
34

45
@Component({
56
selector: 'home',
67
template: `
78
<p>Welcome to the development demos for Angular Material!</p>
8-
<p>Open the sidenav to select a demo. </p>
9+
<p>Open the sidenav to select a demo.</p>
910
`
1011
})
1112
export class Home {}
1213

14+
@Component({
15+
moduleId: module.id,
16+
selector: 'demo-app-on-push',
17+
template: '<ng-content></ng-content>',
18+
changeDetection: ChangeDetectionStrategy.OnPush,
19+
encapsulation: ViewEncapsulation.None,
20+
})
21+
export class DemoAppOnPush {}
22+
1323
@Component({
1424
moduleId: module.id,
1525
selector: 'demo-app',
@@ -22,7 +32,7 @@ export class Home {}
2232
})
2333
export class DemoApp {
2434
dark = false;
25-
35+
changeDetectionStrategy: string;
2636
navItems = [
2737
{name: 'Autocomplete', route: 'autocomplete'},
2838
{name: 'Button', route: 'button'},
@@ -58,7 +68,12 @@ export class DemoApp {
5868
];
5969

6070
constructor(private _element: ElementRef) {
61-
71+
// Some browsers will throw when trying to access localStorage in incognito.
72+
try {
73+
this.changeDetectionStrategy = window.localStorage.getItem(changeDetectionKey) || 'Default';
74+
} catch (error) {
75+
console.error(error);
76+
}
6277
}
6378

6479
toggleFullscreen() {
@@ -73,4 +88,15 @@ export class DemoApp {
7388
elem.msRequestFullScreen();
7489
}
7590
}
91+
92+
toggleChangeDetection() {
93+
try {
94+
this.changeDetectionStrategy = this.changeDetectionStrategy === 'Default' ?
95+
'OnPush' : 'Default';
96+
window.localStorage.setItem(changeDetectionKey, this.changeDetectionStrategy);
97+
window.location.reload();
98+
} catch (error) {
99+
console.error(error);
100+
}
101+
}
76102
}

src/demo-app/tooltip/tooltip-demo.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ChangeDetectionStrategy} from '@angular/core';
1+
import {Component} from '@angular/core';
22
import {TooltipPosition} from '@angular/material';
33

44

@@ -7,7 +7,6 @@ import {TooltipPosition} from '@angular/material';
77
selector: 'tooltip-demo',
88
templateUrl: 'tooltip-demo.html',
99
styleUrls: ['tooltip-demo.css'],
10-
changeDetection: ChangeDetectionStrategy.OnPush // make sure tooltip also works OnPush
1110
})
1211
export class TooltipDemo {
1312
position: TooltipPosition = 'below';

0 commit comments

Comments
 (0)