Skip to content

build: add toggle for OnPush change detection in demo app #3675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/demo-app/demo-app-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {HttpModule} from '@angular/http';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {RouterModule} from '@angular/router';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {DemoApp, Home} from './demo-app/demo-app';
import {DemoApp, Home, DemoAppOnPush} from './demo-app/demo-app';
import {
MaterialModule,
OverlayContainer,
Expand Down Expand Up @@ -65,6 +65,7 @@ import {StyleDemo} from './style/style-demo';
ChipsDemo,
CheckboxDemo,
DemoApp,
DemoAppOnPush,
DialogDemo,
GesturesDemo,
GridListDemo,
Expand Down
29 changes: 20 additions & 9 deletions src/demo-app/demo-app/demo-app.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,29 @@
</button>
<div class="demo-toolbar">
<h1>Angular Material Demos</h1>
<button md-button (click)="dark = !dark">{{dark ? 'Light' : 'Dark'}} theme</button>
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
Fullscreen
</button>
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
{{root.dir.toUpperCase()}}
</button>
<div>
<button md-button (click)="dark = !dark">{{dark ? 'Light' : 'Dark'}} theme</button>
<button md-button (click)="toggleChangeDetection()" title="Toggle change detection">
Change detection: {{changeDetectionStrategy}}
</button>
<button md-button (click)="toggleFullscreen()" title="Toggle fullscreen">
Fullscreen
</button>
<button md-button (click)="root.dir = (root.dir == 'rtl' ? 'ltr' : 'rtl')" title="Toggle between RTL and LTR">
{{root.dir.toUpperCase()}}
</button>
</div>
</div>
</md-toolbar>

<div #root="$implicit" dir="ltr" class="demo-content">
<router-outlet></router-outlet>
<div #root="$implicit" dir="ltr" class="demo-content" [ngSwitch]="changeDetectionStrategy">
<div *ngSwitchDefault>
<router-outlet></router-outlet>
</div>

<demo-app-on-push *ngSwitchCase="'OnPush'">
<router-outlet></router-outlet>
</demo-app-on-push>
</div>
</div>
</md-sidenav-container>
1 change: 1 addition & 0 deletions src/demo-app/demo-app/demo-app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ body {
.demo-toolbar {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
}
Expand Down
34 changes: 30 additions & 4 deletions src/demo-app/demo-app/demo-app.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import {Component, ViewEncapsulation, ElementRef} from '@angular/core';
import {Component, ViewEncapsulation, ElementRef, ChangeDetectionStrategy} from '@angular/core';

const changeDetectionKey = 'mdDemoChangeDetection';

@Component({
selector: 'home',
template: `
<p>Welcome to the development demos for Angular Material!</p>
<p>Open the sidenav to select a demo. </p>
<p>Open the sidenav to select a demo.</p>
`
})
export class Home {}

@Component({
moduleId: module.id,
selector: 'demo-app-on-push',
template: '<ng-content></ng-content>',
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class DemoAppOnPush {}

@Component({
moduleId: module.id,
selector: 'demo-app',
Expand All @@ -22,7 +32,7 @@ export class Home {}
})
export class DemoApp {
dark = false;

changeDetectionStrategy: string;
navItems = [
{name: 'Autocomplete', route: 'autocomplete'},
{name: 'Button', route: 'button'},
Expand Down Expand Up @@ -57,7 +67,12 @@ export class DemoApp {
];

constructor(private _element: ElementRef) {

// Some browsers will throw when trying to access localStorage in incognito.
try {
this.changeDetectionStrategy = window.localStorage.getItem(changeDetectionKey) || 'Default';
} catch (error) {
console.error(error);
}
}

toggleFullscreen() {
Expand All @@ -72,4 +87,15 @@ export class DemoApp {
elem.msRequestFullScreen();
}
}

toggleChangeDetection() {
try {
this.changeDetectionStrategy = this.changeDetectionStrategy === 'Default' ?
'OnPush' : 'Default';
window.localStorage.setItem(changeDetectionKey, this.changeDetectionStrategy);
window.location.reload();
} catch (error) {
console.error(error);
}
}
}
3 changes: 1 addition & 2 deletions src/demo-app/tooltip/tooltip-demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, ChangeDetectionStrategy} from '@angular/core';
import {Component} from '@angular/core';
import {TooltipPosition} from '@angular/material';


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