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

Commit 53f5538

Browse files
committed
docs(cb-ts-to-js): refactor sample code
1 parent b11438f commit 53f5538

File tree

83 files changed

+2002
-1752
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+2002
-1752
lines changed

public/docs/_examples/cb-ts-to-js/e2e-spec.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'use strict'; // necessary for es6 output in node
1+
'use strict'; // necessary for es6 output in node
22

33
import { browser, element, by } from 'protractor';
44

@@ -9,7 +9,7 @@ describe('TypeScript to Javascript tests', function () {
99
});
1010

1111
it('should display the basic component example', function () {
12-
testTag('hero-view', 'Hero: Windstorm');
12+
testTag('hero-view', 'Hero Detail: Windstorm');
1313
});
1414

1515
it('should display the component example with lifecycle methods', function () {
@@ -36,7 +36,7 @@ describe('TypeScript to Javascript tests', function () {
3636

3737
it('should support component with inputs and outputs', function () {
3838
let app = element(by.css('hero-io'));
39-
let confirmComponent = app.element(by.css('my-confirm'));
39+
let confirmComponent = app.element(by.css('app-confirm'));
4040

4141
confirmComponent.element(by.buttonText('OK')).click();
4242
expect(app.element(by.cssContainingText('span', 'OK clicked')).isPresent()).toBe(true);
@@ -46,11 +46,11 @@ describe('TypeScript to Javascript tests', function () {
4646
});
4747

4848
it('should support host bindings and host listeners', function() {
49-
let app = element(by.css('heroes-bindings'));
49+
let app = element(by.css('hero-host'));
5050
let h1 = app.element(by.css('h1'));
5151

5252
expect(app.getAttribute('class')).toBe('heading');
53-
expect(app.getAttribute('title')).toBe('Tooltip content');
53+
expect(app.getAttribute('title')).toContain('Tooltip');
5454

5555
h1.click();
5656
expect(h1.getAttribute('class')).toBe('active');
@@ -61,12 +61,12 @@ describe('TypeScript to Javascript tests', function () {
6161
});
6262

6363
it('should support content and view queries', function() {
64-
let app = element(by.css('heroes-queries'));
65-
let windstorm = app.element(by.css('a-hero:first-child'));
64+
let app = element(by.css('hero-queries'));
65+
let windstorm = app.element(by.css('view-child:first-child'));
6666

67-
app.element(by.buttonText('Activate')).click();
67+
app.element(by.css('button')).click();
6868
expect(windstorm.element(by.css('h2')).getAttribute('class')).toBe('active');
69-
expect(windstorm.element(by.css('active-label')).getText()).toBe('Active');
69+
expect(windstorm.element(by.css('content-child')).getText()).toBe('Active');
7070
});
7171

7272
function testTag(selector: string, expectedText: string) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
moduleId: module.id,
5+
selector: 'my-app',
6+
templateUrl: 'app.component.html',
7+
styles: [
8+
// See hero-di-inject-additional.component
9+
'hero-host, hero-host-meta { border: 1px dashed black; display: block; padding: 4px;}',
10+
'.heading {font-style: italic}'
11+
]
12+
})
13+
export class AppComponent {
14+
title = 'ES6 JavaScript with Decorators';
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<a id="toc"></a>
2+
<h1>{{title}}</h1>
3+
<a href="#class-metadata">Classes and Class Metadata</a><br>
4+
<a href="#io-metadata">Input and Output Decorators</a><br>
5+
<a href="#dependency-injection">Dependency Injection</a><br>
6+
<a href="#host-metadata">Host Metadata</a><br>
7+
<a href="#view-child-metadata">View and Child Metadata</a><br>
8+
9+
<hr>
10+
<h4 id="class-metadata">Classes and Class Metadata</h4>
11+
<hero-view></hero-view>
12+
<hero-lifecycle></hero-lifecycle>
13+
14+
<hr>
15+
<h4 id="io-metadata">Input and Output Metadata</h4>
16+
<hero-io></hero-io>
17+
18+
<hr>
19+
<h4 id="dependency-injection">Dependency Injection</h4>
20+
<hero-di></hero-di>
21+
<hero-di-inject></hero-di-inject>
22+
<hero-di-inject-additional></hero-di-inject-additional>
23+
24+
<hr>
25+
<h4 id="host-metadata">Host Metadata</h4>
26+
<hero-host></hero-host>
27+
<hero-host-meta></hero-host-meta>
28+
29+
<hr>
30+
<h4 id="view-child-metadata">View and Child Metadata</h4>
31+
<hero-queries></hero-queries>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core';
2+
import { BrowserModule } from '@angular/platform-browser';
3+
4+
import { AppComponent } from './app.component';
5+
import { ConfirmComponent } from './confirm.component';
6+
// #docregion appimport
7+
import { HeroComponent } from './hero.component';
8+
// #enddocregion appimport
9+
import { HeroComponent as HeroDIComponent } from './hero-di.component';
10+
import { HeroComponent as HeroDIInjectComponent } from './hero-di-inject.component';
11+
import { HeroComponent as HeroDIInjectAdditionalComponent } from './hero-di-inject-additional.component';
12+
import { HeroHostComponent } from './hero-host.component';
13+
import { HeroHostMetaComponent } from './hero-host-meta.component';
14+
import { HeroIOComponent } from './hero-io.component';
15+
import { HeroComponent as HeroLifecycleComponent } from './hero-lifecycle.component';
16+
import { HeroQueriesComponent, ViewChildComponent, ContentChildComponent } from './hero-queries.component';
17+
import { HeroTitleComponent } from './hero-title.component';
18+
19+
import { DataService } from './data.service';
20+
21+
@NgModule({
22+
imports: [
23+
BrowserModule
24+
],
25+
declarations: [
26+
AppComponent,
27+
ConfirmComponent,
28+
HeroComponent,
29+
HeroDIComponent,
30+
HeroDIInjectComponent,
31+
HeroDIInjectAdditionalComponent,
32+
HeroHostComponent, HeroHostMetaComponent,
33+
HeroIOComponent,
34+
HeroLifecycleComponent,
35+
HeroQueriesComponent, ViewChildComponent, ContentChildComponent,
36+
HeroTitleComponent
37+
],
38+
providers: [
39+
DataService,
40+
{ provide: 'heroName', useValue: 'Windstorm' }
41+
],
42+
bootstrap: [ AppComponent ],
43+
44+
// schemas: [ NO_ERRORS_SCHEMA ] // helpful for debugging
45+
})
46+
export class AppModule { }
47+
48+
/* tslint:disable no-unused-variable */
49+
// #docregion ng2import
50+
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
51+
import {
52+
LocationStrategy,
53+
HashLocationStrategy
54+
} from '@angular/common';
55+
// #enddocregion ng2import
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Component, EventEmitter, Input, Output } from '@angular/core';
2+
3+
// #docregion
4+
@Component({
5+
moduleId: module.id,
6+
selector: 'app-confirm',
7+
templateUrl: 'confirm.component.html'
8+
})
9+
export class ConfirmComponent {
10+
@Input() okMsg = '';
11+
@Input('cancelMsg') notOkMsg = '';
12+
@Output() ok = new EventEmitter();
13+
@Output('cancel') notOk = new EventEmitter();
14+
15+
onOkClick() {
16+
this.ok.emit(true);
17+
}
18+
onNotOkClick() {
19+
this.notOk.emit(true);
20+
}
21+
}
22+
// #enddocregion

public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/data.service.es6

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Injectable } from '@angular/core';
22

33
@Injectable()
44
export class DataService {
5-
constructor() {
6-
}
5+
constructor() { }
6+
77
getHeroName() {
88
return 'Windstorm';
99
}
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,7 @@
1-
import {
2-
Attribute,
3-
Component,
4-
Inject,
5-
Optional,
6-
NgModule
7-
} from '@angular/core';
8-
import { BrowserModule } from '@angular/platform-browser';
9-
10-
// #docregion
11-
// #docregion metadata
12-
@Component({
13-
moduleId: module.id,
14-
selector: 'hero-title',
15-
templateUrl: 'title.component.html'
16-
})
17-
// #enddocregion metadata
18-
class TitleComponent {
19-
msg = '';
20-
constructor(
21-
@Inject('titlePrefix') @Optional() titlePrefix,
22-
@Attribute('title') title
23-
) {
24-
this.titlePrefix = titlePrefix;
25-
this.title = title;
26-
}
27-
28-
ok() {
29-
this.msg = 'OK!';
30-
}
31-
}
32-
// #enddocregion
1+
import { Component } from '@angular/core';
332

343
@Component({
354
selector: 'hero-di-inject-additional',
36-
template: `<hero-title title="Tour of Heroes">
37-
</hero-title>`
38-
})
39-
class AppComponent { }
40-
41-
@NgModule({
42-
imports: [ BrowserModule ],
43-
declarations: [
44-
AppComponent,
45-
TitleComponent
46-
],
47-
bootstrap: [ AppComponent ]
5+
template: `<hero-title title="Tour of Heroes"></hero-title>`
486
})
49-
export class HeroesDIInjectAdditionalModule { }
7+
export class HeroComponent { }
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
import { Component, Inject, NgModule } from '@angular/core';
2-
import { BrowserModule } from '@angular/platform-browser';
1+
import { Component, Inject } from '@angular/core';
32

43
// #docregion
54
@Component({
65
selector: 'hero-di-inject',
76
template: `<h1>Hero: {{name}}</h1>`
87
})
9-
class HeroComponent {
8+
export class HeroComponent {
109
constructor(@Inject('heroName') name) {
1110
this.name = name;
1211
}
1312
}
1413
// #enddocregion
15-
16-
@NgModule({
17-
imports: [ BrowserModule ],
18-
providers: [ { provide: 'heroName', useValue: 'Windstorm' } ],
19-
declarations: [ HeroComponent ],
20-
bootstrap: [ HeroComponent ]
21-
})
22-
export class HeroesDIInjectModule { }
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
1-
import { Component, NgModule } from '@angular/core';
2-
import { BrowserModule } from '@angular/platform-browser';
3-
1+
import { Component } from '@angular/core';
42
import { DataService } from './data.service';
53

64
// #docregion
75
@Component({
86
selector: 'hero-di',
97
template: `<h1>Hero: {{name}}</h1>`
108
})
11-
12-
class HeroComponent {
13-
name;
9+
export class HeroComponent {
10+
name = '';
1411
constructor(dataService: DataService) {
1512
this.name = dataService.getHeroName();
1613
}
1714
}
1815
// #enddocregion
19-
20-
@NgModule({
21-
imports: [ BrowserModule ],
22-
providers: [ DataService ],
23-
declarations: [ HeroComponent ],
24-
bootstrap: [ HeroComponent ]
25-
})
26-
export class HeroesDIModule { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Component } from '@angular/core';
2+
3+
// #docregion
4+
@Component({
5+
selector: 'hero-host-meta',
6+
template: `
7+
<h1 [class.active]="active">Hero Host in Metadata</h1>
8+
<div>Heading clicks: {{clicks}}</div>
9+
`,
10+
host: {
11+
// HostBindings to the <hero-host-meta> element
12+
'[title]': 'title',
13+
'[class.heading]': 'headingClass',
14+
15+
// HostListeners on the entire <hero-host-meta> element
16+
'(click)': 'clicked()',
17+
'(mouseenter)': 'enter($event)',
18+
'(mouseleave)': 'leave($event)'
19+
},
20+
// Styles within (but excluding) the <hero-host-meta> element
21+
styles: ['.active {background-color: coral;}']
22+
})
23+
export class HeroHostMetaComponent {
24+
title = 'Hero Host in Metadata Tooltip';
25+
headingClass = true;
26+
27+
active = false;
28+
clicks = 0;
29+
30+
clicked() {
31+
this.clicks += 1;
32+
}
33+
34+
enter(event: Event) {
35+
this.active = true;
36+
this.headingClass = false;
37+
}
38+
39+
leave(event: Event) {
40+
this.active = false;
41+
this.headingClass = true;
42+
}
43+
}
44+
// #enddocregion
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Component, HostBinding, HostListener } from '@angular/core';
2+
3+
// #docregion
4+
@Component({
5+
selector: 'hero-host',
6+
template: `
7+
<h1 [class.active]="active">Hero Host in Decorators</h1>
8+
<div>Heading clicks: {{clicks}}</div>
9+
`,
10+
// Styles within (but excluding) the <hero-host> element
11+
styles: ['.active {background-color: yellow;}']
12+
})
13+
export class HeroHostComponent {
14+
// HostBindings to the <hero-host> element
15+
@HostBinding() title = 'Hero Host in Decorators Tooltip';
16+
@HostBinding('class.heading') headingClass = true;
17+
18+
active = false;
19+
clicks = 0;
20+
21+
// HostListeners on the entire <hero-host> element
22+
@HostListener('click')
23+
clicked() {
24+
this.clicks += 1;
25+
}
26+
27+
@HostListener('mouseenter', ['$event'])
28+
enter(event: Event) {
29+
this.active = true;
30+
this.headingClass = false;
31+
}
32+
33+
@HostListener('mouseleave', ['$event'])
34+
leave(event: Event) {
35+
this.active = false;
36+
this.headingClass = true;
37+
}
38+
}
39+
// #enddocregion

0 commit comments

Comments
 (0)