From 12eb19fa3c34bb48b28da8bd33d94d7cc0526cf5 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Thu, 13 Oct 2016 17:59:00 +0100 Subject: [PATCH 1/4] feat(cb-ts-to-js): add es6 examples update docshredder to shred .es6 optimized focus gulp task convert imports and metadate sections add DI section add host and query metadata section add intro fix capitalization and grammar --- gulpfile.js | 11 +- public/docs/_examples/.gitignore | 1 + .../docs/_examples/_boilerplate/package.json | 1 + .../cb-ts-to-js/js-es6-decorators/.babelrc | 6 + .../js-es6-decorators/app/data.service.es6 | 10 + .../hero-di-inject-additional.component.es6 | 50 ++ .../app/hero-di-inject.component.es6 | 22 + .../app/hero-di.component.es6 | 26 + .../app/hero-io.component.es6 | 73 ++ .../app/hero-lifecycle.component.es6 | 27 + .../js-es6-decorators/app/hero.component.es6 | 30 + .../app/heroes-bindings.component.es6 | 42 + .../app/heroes-queries.component.es6 | 88 ++ .../js-es6-decorators/app/main.es6 | 30 + .../js-es6-decorators/example-config.json | 4 + .../cb-ts-to-js/js-es6-decorators/index.html | 51 ++ .../cb-ts-to-js/js-es6-decorators/plnkr.json | 8 + .../_examples/cb-ts-to-js/js-es6/.babelrc | 5 + .../cb-ts-to-js/js-es6/app/data.service.es6 | 13 + .../hero-di-inject-additional.component.es6 | 61 ++ .../js-es6/app/hero-di-inject.component.es6 | 32 + .../js-es6/app/hero-di.component.es6 | 35 + .../js-es6/app/hero-io.component.es6 | 85 ++ .../js-es6/app/hero-lifecycle.component.es6 | 31 + .../cb-ts-to-js/js-es6/app/hero.component.es6 | 37 + .../js-es6/app/heroes-bindings.component.es6 | 48 ++ .../js-es6/app/heroes-queries.component.es6 | 98 +++ .../_examples/cb-ts-to-js/js-es6/app/main.es6 | 30 + .../cb-ts-to-js/js-es6/example-config.json | 4 + .../_examples/cb-ts-to-js/js-es6/index.html | 52 ++ .../_examples/cb-ts-to-js/js-es6/plnkr.json | 8 + .../hero-di-inject-additional.component.js | 7 +- .../js/app/hero-di-inject.component.js | 13 +- .../js/app/hero-di-inline.component.js | 11 +- .../cb-ts-to-js/js/app/hero-di.component.js | 2 + .../cb-ts-to-js/js/app/hero-dsl.component.js | 3 +- .../js/app/hero-lifecycle.component.js | 7 +- .../cb-ts-to-js/js/app/hero.component.js | 7 +- .../docs/_examples/cb-ts-to-js/js/app/main.js | 9 +- .../hero-di-inject-additional.component.ts | 9 +- .../ts/app/hero-di-inject.component.ts | 5 +- .../cb-ts-to-js/ts/app/hero-di.component.ts | 1 + .../ts/app/hero-lifecycle.component.ts | 3 +- .../ts/app/heroes-bindings.component.ts | 11 +- public/docs/_examples/package.json | 5 +- public/docs/js/latest/cookbook/ts-to-js.jade | 801 +++++++++--------- tools/doc-shredder/doc-shredder.js | 2 +- tools/doc-shredder/regionExtractor.js | 1 + 48 files changed, 1470 insertions(+), 446 deletions(-) create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/.babelrc create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/data.service.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject-additional.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-io.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-lifecycle.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/heroes-bindings.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/heroes-queries.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/main.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/example-config.json create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/index.html create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6-decorators/plnkr.json create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/.babelrc create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/data.service.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/hero-io.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/hero-lifecycle.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-bindings.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-queries.component.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/app/main.es6 create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/example-config.json create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/index.html create mode 100644 public/docs/_examples/cb-ts-to-js/js-es6/plnkr.json diff --git a/gulpfile.js b/gulpfile.js index d24edb1f16..765dd3e006 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -247,15 +247,16 @@ function findAndRunE2eTests(filter, outputFile) { e2eSpecPaths.forEach(function(specPath) { // get all of the examples under each dir where a pcFilename is found localExamplePaths = getExamplePaths(specPath, true); - // Filter by language - localExamplePaths = localExamplePaths.filter(function (fn) { - return fn.match('/'+lang+'$') != null; - }); + // Filter by example name if (filter) { localExamplePaths = localExamplePaths.filter(function (fn) { return fn.match(filter) != null; }) } + // Filter by language, also supports variations like js-es6 + localExamplePaths = localExamplePaths.filter(function (fn) { + return fn.match('/'+lang+'(?:-[^/]*)?$') != null; + }); localExamplePaths.forEach(function(examplePath) { examplePaths.push(examplePath); }) @@ -1270,7 +1271,7 @@ function apiExamplesWatch(postShredAction) { } function devGuideExamplesWatch(shredOptions, postShredAction, focus) { - var watchPattern = focus ? '**/{' + focus + ',cb-' + focus+ '}/**/*.*' : '**/*.*'; + var watchPattern = focus ? '{' + focus + ',cb-' + focus+ '}/**/*.*' : '**/*.*'; var includePattern = path.join(shredOptions.examplesDir, watchPattern); // removed this version because gulp.watch has the same glob issue that dgeni has. // var excludePattern = '!' + path.join(shredOptions.examplesDir, '**/node_modules/**/*.*'); diff --git a/public/docs/_examples/.gitignore b/public/docs/_examples/.gitignore index b6733dc6ea..b1a1357472 100644 --- a/public/docs/_examples/.gitignore +++ b/public/docs/_examples/.gitignore @@ -11,6 +11,7 @@ wallaby.js _test-output **/ts/**/*.js +**/js-es6*/**/*.js **/ts-snippets/**/*.js *.d.ts diff --git a/public/docs/_examples/_boilerplate/package.json b/public/docs/_examples/_boilerplate/package.json index 3f255fcbd6..bcccdd978a 100644 --- a/public/docs/_examples/_boilerplate/package.json +++ b/public/docs/_examples/_boilerplate/package.json @@ -18,6 +18,7 @@ "build:webpack": "rimraf dist && webpack --config config/webpack.prod.js --bail", "build:cli": "ng build", "build:aot": "ngc -p tsconfig-aot.json && rollup -c rollup-config.js", + "build:babel": "babel app -d app --extensions \".es6\" --source-maps", "copy-dist-files": "node ./copy-dist-files.js", "i18n": "ng-xi18n" }, diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/.babelrc b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/.babelrc new file mode 100644 index 0000000000..3aaf507508 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/.babelrc @@ -0,0 +1,6 @@ +{ + "presets": [ + "es2015", + "angular2" + ] +} diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/data.service.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/data.service.es6 new file mode 100644 index 0000000000..7e9c7456c6 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/data.service.es6 @@ -0,0 +1,10 @@ +import { Injectable } from '@angular/core'; + +@Injectable() +export class DataService { + constructor() { + } + getHeroName() { + return 'Windstorm'; + } +} diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject-additional.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject-additional.component.es6 new file mode 100644 index 0000000000..8cd77b51f6 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject-additional.component.es6 @@ -0,0 +1,50 @@ +import { + Attribute, + Component, + Inject, + Optional, + NgModule +} from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +// #docregion +@Component({ + selector: 'hero-title', + template: ` +

{{titlePrefix}} {{title}}

+ +

{{ msg }}

+ ` +}) +class TitleComponent { + msg = ''; + constructor( + @Inject('titlePrefix') @Optional() titlePrefix, + @Attribute('title') title + ) { + this.titlePrefix = titlePrefix; + this.title = title; + } + + ok() { + this.msg = 'OK!'; + } +} +// #enddocregion + +@Component({ + selector: 'hero-di-inject-additional', + template: ` + ` +}) +class AppComponent { } + +@NgModule({ + imports: [ BrowserModule ], + declarations: [ + AppComponent, + TitleComponent + ], + bootstrap: [ AppComponent ] +}) +export class HeroesDIInjectAdditionalModule { } diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject.component.es6 new file mode 100644 index 0000000000..3a89867051 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di-inject.component.es6 @@ -0,0 +1,22 @@ +import { Component, Inject, NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +// #docregion +@Component({ + selector: 'hero-di-inject', + template: `

Hero: {{name}}

` +}) +class HeroComponent { + constructor(@Inject('heroName') name) { + this.name = name; + } +} +// #enddocregion + +@NgModule({ + imports: [ BrowserModule ], + providers: [ { provide: 'heroName', useValue: 'Windstorm' } ], + declarations: [ HeroComponent ], + bootstrap: [ HeroComponent ] +}) +export class HeroesDIInjectModule { } diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di.component.es6 new file mode 100644 index 0000000000..e8341509a0 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-di.component.es6 @@ -0,0 +1,26 @@ +import { Component, NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +import { DataService } from './data.service'; + +// #docregion +@Component({ + selector: 'hero-di', + template: `

Hero: {{name}}

` +}) + +class HeroComponent { + name; + constructor(dataService: DataService) { + this.name = dataService.getHeroName(); + } +} +// #enddocregion + +@NgModule({ + imports: [ BrowserModule ], + providers: [ DataService ], + declarations: [ HeroComponent ], + bootstrap: [ HeroComponent ] +}) +export class HeroesDIModule { } diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-io.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-io.component.es6 new file mode 100644 index 0000000000..2b954b138a --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-io.component.es6 @@ -0,0 +1,73 @@ +import { + Component, + EventEmitter, + Input, + Output, + NgModule +} from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +// #docregion +@Component({ + selector: 'my-confirm', + template: ` + + + ` +}) +class ConfirmComponent { + @Input() okMsg; + @Input('cancelMsg') notOkMsg; + @Output() ok = + new EventEmitter(); + @Output('cancel') notOk = + new EventEmitter(); + + onOkClick() { + this.ok.next(true); + } + onNotOkClick() { + this.notOk.next(true); + } +} +// #enddocregion + + +@Component({ + selector: 'hero-io', + template: ` + + + OK clicked + Cancel clicked + ` +}) +class AppComponent { + okClicked; + cancelClicked; + + onOk() { + this.okClicked = true; + } + onCancel() { + this.cancelClicked = true; + } +} + + +@NgModule({ + imports: [ BrowserModule ], + declarations: [ + AppComponent, + ConfirmComponent + ], + bootstrap: [ AppComponent ] +}) +export class HeroesIOModule { } diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-lifecycle.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-lifecycle.component.es6 new file mode 100644 index 0000000000..6a935731b5 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero-lifecycle.component.es6 @@ -0,0 +1,27 @@ +// #docplaster +// #docregion +import { Component, OnInit } from '@angular/core'; +// #enddocregion +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +@Component({ + selector: 'hero-lifecycle', + template: `

Hero: {{name}}

` +}) +// #docregion +class HeroComponent{ + name; + ngOnInit() { + this.name = 'Windstorm'; + } +} +// #enddocregion + +@NgModule({ + imports: [ BrowserModule ], + declarations: [ HeroComponent ], + bootstrap: [ HeroComponent ] +}) +export class HeroesLifecycleModule { } + diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero.component.es6 new file mode 100644 index 0000000000..4b4ba8529d --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/hero.component.es6 @@ -0,0 +1,30 @@ +// #docplaster +// #docregion metadata +import { Component } from '@angular/core'; + +@Component({ + selector: 'hero-view', + template: + '

Hero: {{getName()}}

' +}) +// #docregion appexport +// #docregion class +export class HeroComponent { + title = 'Hero Detail'; + getName() {return 'Windstorm'; } +} +// #enddocregion class +// #enddocregion appexport +// #enddocregion metadata + +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +@NgModule({ + imports: [ BrowserModule ], + declarations: [ HeroComponent ], + bootstrap: [ HeroComponent ] +}) +export class HeroesModule { } + + diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/heroes-bindings.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/heroes-bindings.component.es6 new file mode 100644 index 0000000000..ef959eabbb --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/heroes-bindings.component.es6 @@ -0,0 +1,42 @@ +import { + Component, + HostBinding, + HostListener, + NgModule +} from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +// #docregion +@Component({ + selector: 'heroes-bindings', + template: ` +

+ Tour of Heroes +

+ ` +}) +class HeroesComponent { + @HostBinding() title = 'Tooltip content'; + @HostBinding('class.heading') hClass = true; + active; + + constructor() {} + + @HostListener('click') + clicked() { + this.active = !this.active; + } + + @HostListener('dblclick', ['$event']) + doubleClicked(evt) { + this.active = true; + } +} +// #enddocregion + +@NgModule({ + imports: [ BrowserModule ], + declarations: [ HeroesComponent ], + bootstrap: [ HeroesComponent ] +}) +export class HeroesHostBindingsModule { } diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/heroes-queries.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/heroes-queries.component.es6 new file mode 100644 index 0000000000..bc55ccef24 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/heroes-queries.component.es6 @@ -0,0 +1,88 @@ +import { + Component, + ViewChildren, + ContentChild, + QueryList, + Input, + NgModule +} from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +@Component({ + selector: 'active-label', + template: ` + Active + ` +}) +class ActiveLabelComponent { + active; + + activate() { + this.active = true; + } +} + +// #docregion content +@Component({ + selector: 'a-hero', + template: `

+ {{hero.name}} + +

` +}) +class HeroComponent { + @Input() hero; + active; + + @ContentChild(ActiveLabelComponent) + label; + + activate() { + this.active = true; + this.label.activate(); + } +} +// #enddocregion content + + +// #docregion view +@Component({ + selector: 'heroes-queries', + template: ` + + + + + ` +}) +class HeroesQueriesComponent { + heroData = [ + {id: 1, name: 'Windstorm'}, + {id: 2, name: 'Superman'} + ]; + + @ViewChildren(HeroComponent) + heroCmps; + + activate() { + this.heroCmps.forEach( + (cmp) => cmp.activate() + ); + } +} +// #enddocregion view + +@NgModule({ + imports: [ BrowserModule ], + declarations: [ + HeroesQueriesComponent, + HeroComponent, + ActiveLabelComponent + ], + bootstrap: [ HeroesQueriesComponent ] +}) +export class HeroesQueriesModule { } diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/main.es6 b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/main.es6 new file mode 100644 index 0000000000..92f5af5e1a --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/main.es6 @@ -0,0 +1,30 @@ +/* tslint:disable no-unused-variable */ +// #docregion ng2import +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; +import { + LocationStrategy, + HashLocationStrategy +} from '@angular/common'; +// #enddocregion ng2import + +// #docregion appimport +import { HeroComponent } from './hero.component'; +// #enddocregion appimport + +import { HeroesModule } from './hero.component'; +import { HeroesLifecycleModule } from './hero-lifecycle.component'; +import { HeroesDIModule } from './hero-di.component'; +import { HeroesDIInjectModule } from './hero-di-inject.component'; +import { HeroesDIInjectAdditionalModule } from './hero-di-inject-additional.component'; +import { HeroesIOModule } from './hero-io.component'; +import { HeroesHostBindingsModule } from './heroes-bindings.component'; +import { HeroesQueriesModule } from './heroes-queries.component'; + +platformBrowserDynamic().bootstrapModule(HeroesModule); +platformBrowserDynamic().bootstrapModule(HeroesLifecycleModule); +platformBrowserDynamic().bootstrapModule(HeroesDIModule); +platformBrowserDynamic().bootstrapModule(HeroesDIInjectModule); +platformBrowserDynamic().bootstrapModule(HeroesDIInjectAdditionalModule); +platformBrowserDynamic().bootstrapModule(HeroesIOModule); +platformBrowserDynamic().bootstrapModule(HeroesHostBindingsModule); +platformBrowserDynamic().bootstrapModule(HeroesQueriesModule); diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/example-config.json b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/example-config.json new file mode 100644 index 0000000000..391bd1a766 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/example-config.json @@ -0,0 +1,4 @@ +{ + "build": "build:babel", + "run": "http-server:e2e" +} \ No newline at end of file diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/index.html b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/index.html new file mode 100644 index 0000000000..005fe67c31 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/index.html @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + +

TypeScript to JavaScript

+ Classes and Class Metadata
+ Input and Output Metadata
+ Dependency Injection
+ Host and Query Metadata
+ +
+

Classes and Class Metadata

+ Loading hero-view... + Loading hero-lifecycle... + +
+

Input and Output Metadata

+ Loading hero-io... + +
+

Dependency Injection

+ Loading hero-di... + Loading hero-di-inject... + Loading hero-di-inject-additional... + +
+

Host and Query Metadata

+ Loading heroes-bindings... + Loading heroes-queries... + + + diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/plnkr.json b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/plnkr.json new file mode 100644 index 0000000000..5c7a5acd44 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6-decorators/plnkr.json @@ -0,0 +1,8 @@ +{ + "description": "TypeScript to JavaScript", + "files":[ + "!**/*.d.ts", + "!**/*.js" + ], + "tags":["cookbook"] +} diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/.babelrc b/public/docs/_examples/cb-ts-to-js/js-es6/.babelrc new file mode 100644 index 0000000000..3c078e9f99 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/.babelrc @@ -0,0 +1,5 @@ +{ + "presets": [ + "es2015" + ] +} diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/data.service.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/data.service.es6 new file mode 100644 index 0000000000..de023ce5a0 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/data.service.es6 @@ -0,0 +1,13 @@ +import { Injectable } from '@angular/core'; + +export class DataService { + constructor() { + } + getHeroName() { + return 'Windstorm'; + } +} + +DataService.annotations = [ + new Injectable() +]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6 new file mode 100644 index 0000000000..295d305ac4 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6 @@ -0,0 +1,61 @@ +import { + Attribute, + Component, + Inject, + Optional, + NgModule +} from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +// #docregion +class TitleComponent { + constructor(titlePrefix, title) { + this.titlePrefix = titlePrefix; + this.title = title; + this.msg = ''; + } + + ok() { + this.msg = 'OK!'; + } +} + +TitleComponent.annotations = [ + new Component({ + selector: 'hero-title', + template: ` +

{{titlePrefix}} {{title}}

+ +

{{ msg }}

+ ` + }) +]; + +TitleComponent.parameters = [ + [new Optional(), new Inject('titlePrefix')], + [new Attribute('title')] +]; +// #enddocregion + +class AppComponent { +} +AppComponent.annotations = [ + new Component({ + selector: 'hero-di-inject-additional', + template: ` + ` + }) +]; + +export class HeroesDIInjectAdditionalModule { } + +HeroesDIInjectAdditionalModule.annotations = [ + new NgModule({ + imports: [ BrowserModule ], + declarations: [ + AppComponent, + TitleComponent + ], + bootstrap: [ AppComponent ] + }) +]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject.component.es6 new file mode 100644 index 0000000000..cc8df60abc --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject.component.es6 @@ -0,0 +1,32 @@ +import { Component, Inject, NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +// #docregion +class HeroComponent { + constructor(name) { + this.name = name; + } +} + +HeroComponent.annotations = [ + new Component({ + selector: 'hero-di-inject', + template: `

Hero: {{name}}

` + }) +]; + +HeroComponent.parameters = [ + [new Inject('heroName')] +]; +// #enddocregion + +export class HeroesDIInjectModule { } + +HeroesDIInjectModule.annotations = [ + new NgModule({ + imports: [ BrowserModule ], + providers: [ { provide: 'heroName', useValue: 'Windstorm' } ], + declarations: [ HeroComponent ], + bootstrap: [ HeroComponent ] + }) +]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di.component.es6 new file mode 100644 index 0000000000..bb569e6e2b --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di.component.es6 @@ -0,0 +1,35 @@ +import { Component, NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +import { DataService } from './data.service'; + +// #docregion +class HeroComponent { + constructor(dataService) { + this.name = dataService.getHeroName(); + } +} + +HeroComponent.annotations = [ + new Component({ + selector: 'hero-di', + template: `

Hero: {{name}}

` + }) +]; + +HeroComponent.parameters = [ + [DataService] +]; + +// #enddocregion + +export class HeroesDIModule { } + +HeroesDIModule.annotations = [ + new NgModule({ + imports: [ BrowserModule ], + providers: [ DataService ], + declarations: [ HeroComponent ], + bootstrap: [ HeroComponent ] + }) +]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-io.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-io.component.es6 new file mode 100644 index 0000000000..549d75ce7c --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-io.component.es6 @@ -0,0 +1,85 @@ +import { + Component, + EventEmitter, + Input, + Output, + NgModule +} from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +// #docregion +class ConfirmComponent { + constructor(){ + this.ok = new EventEmitter(); + this.notOk = new EventEmitter(); + } + onOkClick() { + this.ok.next(true); + } + onNotOkClick() { + this.notOk.next(true); + } +} + +ConfirmComponent.annotations = [ + new Component({ + selector: 'my-confirm', + template: ` + + + `, + inputs: [ + 'okMsg', + 'notOkMsg: cancelMsg' + ], + outputs: [ + 'ok', + 'notOk: cancel' + ] + }) +]; +// #enddocregion + +class AppComponent { + constructor(){ + } + onOk() { + this.okClicked = true; + } + onCancel() { + this.cancelClicked = true; + } +} + +AppComponent.annotations = [ + new Component({ + selector: 'hero-io', + template: ` + + + OK clicked + Cancel clicked + ` + }) +]; + + +export class HeroesIOModule { } + +HeroesIOModule.annotations = [ + new NgModule({ + imports: [ BrowserModule ], + declarations: [ + AppComponent, + ConfirmComponent + ], + bootstrap: [ AppComponent ] + }) +]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-lifecycle.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-lifecycle.component.es6 new file mode 100644 index 0000000000..8a9c6c6c4e --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-lifecycle.component.es6 @@ -0,0 +1,31 @@ +// #docplaster +// #docregion +import { Component } from '@angular/core'; +// #enddocregion +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +// #docregion +class HeroComponent { + ngOnInit() { + this.name = 'Windstorm'; + } +} +// #enddocregion + +HeroComponent.annotations = [ + new Component({ + selector: 'hero-lifecycle', + template: `

Hero: {{name}}

` + }) +]; + +export class HeroesLifecycleModule { } + +HeroesLifecycleModule.annotations = [ + new NgModule({ + imports: [ BrowserModule ], + declarations: [ HeroComponent ], + bootstrap: [ HeroComponent ] + }) +]; \ No newline at end of file diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 new file mode 100644 index 0000000000..4b5c20057a --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 @@ -0,0 +1,37 @@ +// #docplaster +// #docregion metadata +import { Component } from '@angular/core'; + +// #docregion class +// #docregion appexport +export class HeroComponent { + construct() { + this.title = 'Hero Detail'; + } + getName() {return 'Windstorm'; } +} +// #enddocregion appexport +// #enddocregion class + +HeroComponent.annotations = [ + new Component({ + selector: 'hero-view', + template: '

Hero: {{getName()}}

' + }) +]; +// #enddocregion metadata + +import { NgModule } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +export class HeroesModule { } + +HeroesModule.annotations = [ + new NgModule({ + imports: [ BrowserModule ], + declarations: [ HeroComponent ], + bootstrap: [ HeroComponent ] + }) +]; + + diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-bindings.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-bindings.component.es6 new file mode 100644 index 0000000000..5c13e586a6 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-bindings.component.es6 @@ -0,0 +1,48 @@ +import { + Component, + HostBinding, + HostListener, + NgModule +} from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +// #docregion +class HeroesComponent { + constructor() { + this.title = 'Tooltip content'; + this.hClass = true; + } + + clicked() { + this.active = !this.active; + } + + doubleClicked(evt) { + this.active = true; + } +} +HeroesComponent.annotations = [ + new Component({ + selector: 'heroes-bindings', + template: `

+ Tour of Heroes +

`, + host: { + '[title]': 'title', + '[class.heading]': 'hClass', + '(click)': 'clicked()', + '(dblclick)': 'doubleClicked($event)' + } + }) +]; +// #enddocregion + +export class HeroesHostBindingsModule { } + +HeroesHostBindingsModule.annotations = [ + new NgModule({ + imports: [ BrowserModule ], + declarations: [ HeroesComponent ], + bootstrap: [ HeroesComponent ] + }) +]; \ No newline at end of file diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-queries.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-queries.component.es6 new file mode 100644 index 0000000000..4cec0c3bbd --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-queries.component.es6 @@ -0,0 +1,98 @@ +import { + Component, + ViewChildren, + ContentChild, + QueryList, + Input, + NgModule +} from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +class ActiveLabelComponent { + activate() { + this.active = true; + } +} + +ActiveLabelComponent.annotations = [ + new Component({ + selector: 'active-label', + template: ` + + Active + ` + }) +]; + +// #docregion content +class HeroComponent { + activate() { + this.active = true; + this.label.activate(); + } +} + +HeroComponent.annotations = [ + new Component({ + selector: 'a-hero', + template: `

+ {{hero.name}} + +

`, + inputs: ['hero'], + queries: { + label: new ContentChild(ActiveLabelComponent) + } + }) +]; +// #enddocregion content + + +// #docregion view +class HeroesQueriesComponent { + constructor(){ + this.heroData = [ + {id: 1, name: 'Windstorm'}, + {id: 2, name: 'Superman'} + ]; + } + + activate() { + this.heroCmps.forEach( + (cmp) => cmp.activate() + ); + } +} + +HeroesQueriesComponent.annotations = [ + new Component({ + selector: 'heroes-queries', + template: ` + + + + + `, + queries: { + heroCmps: new ViewChildren(HeroComponent) + } + }) +]; +// #enddocregion view + +export class HeroesQueriesModule { } + +HeroesQueriesModule.annotations = [ + new NgModule({ + imports: [ BrowserModule ], + declarations: [ + HeroesQueriesComponent, + HeroComponent, + ActiveLabelComponent + ], + bootstrap: [ HeroesQueriesComponent ] + }) +]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/main.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/main.es6 new file mode 100644 index 0000000000..92f5af5e1a --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/main.es6 @@ -0,0 +1,30 @@ +/* tslint:disable no-unused-variable */ +// #docregion ng2import +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; +import { + LocationStrategy, + HashLocationStrategy +} from '@angular/common'; +// #enddocregion ng2import + +// #docregion appimport +import { HeroComponent } from './hero.component'; +// #enddocregion appimport + +import { HeroesModule } from './hero.component'; +import { HeroesLifecycleModule } from './hero-lifecycle.component'; +import { HeroesDIModule } from './hero-di.component'; +import { HeroesDIInjectModule } from './hero-di-inject.component'; +import { HeroesDIInjectAdditionalModule } from './hero-di-inject-additional.component'; +import { HeroesIOModule } from './hero-io.component'; +import { HeroesHostBindingsModule } from './heroes-bindings.component'; +import { HeroesQueriesModule } from './heroes-queries.component'; + +platformBrowserDynamic().bootstrapModule(HeroesModule); +platformBrowserDynamic().bootstrapModule(HeroesLifecycleModule); +platformBrowserDynamic().bootstrapModule(HeroesDIModule); +platformBrowserDynamic().bootstrapModule(HeroesDIInjectModule); +platformBrowserDynamic().bootstrapModule(HeroesDIInjectAdditionalModule); +platformBrowserDynamic().bootstrapModule(HeroesIOModule); +platformBrowserDynamic().bootstrapModule(HeroesHostBindingsModule); +platformBrowserDynamic().bootstrapModule(HeroesQueriesModule); diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/example-config.json b/public/docs/_examples/cb-ts-to-js/js-es6/example-config.json new file mode 100644 index 0000000000..391bd1a766 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/example-config.json @@ -0,0 +1,4 @@ +{ + "build": "build:babel", + "run": "http-server:e2e" +} \ No newline at end of file diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/index.html b/public/docs/_examples/cb-ts-to-js/js-es6/index.html new file mode 100644 index 0000000000..3a8a4e7960 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/index.html @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + +

TypeScript to JavaScript

+ Classes and Class Metadata
+ Input and Output Metadata
+ Dependency Injection
+ Host and Query Metadata
+ + +
+

Classes and Class Metadata

+ Loading hero-view... + Loading hero-lifecycle... + +
+

Input and Output Metadata

+ Loading hero-io... + +
+

Dependency Injection

+ Loading hero-di... + Loading hero-di-inject... + Loading hero-di-inject-additional... + +
+

Host and Query Metadata

+ Loading heroes-bindings... + Loading heroes-queries... + + + diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/plnkr.json b/public/docs/_examples/cb-ts-to-js/js-es6/plnkr.json new file mode 100644 index 0000000000..5c7a5acd44 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/plnkr.json @@ -0,0 +1,8 @@ +{ + "description": "TypeScript to JavaScript", + "files":[ + "!**/*.d.ts", + "!**/*.js" + ], + "tags":["cookbook"] +} diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject-additional.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject-additional.component.js index fa9684e7a1..64009317a9 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject-additional.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject-additional.component.js @@ -9,10 +9,7 @@ '

{{ msg }}

' }).Class({ constructor: [ - [ - new ng.core.Optional(), - new ng.core.Inject('titlePrefix') - ], + [ new ng.core.Optional(), new ng.core.Inject('titlePrefix') ], new ng.core.Attribute('title'), function(titlePrefix, title) { this.titlePrefix = titlePrefix; @@ -33,7 +30,7 @@ }).Class({ constructor: function() { } }); - + app.HeroesDIInjectAdditionalModule = ng.core.NgModule({ imports: [ ng.platformBrowser.BrowserModule ], diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject.component.js index dfda83e1f0..743af3b987 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject.component.js @@ -25,7 +25,7 @@ .Class({ constructor: function() {} }); - + })(window.app = window.app || {}); (function(app) { @@ -35,11 +35,12 @@ template: '

Hero: {{name}}

' }) .Class({ - constructor: - [new ng.core.Inject('heroName'), - function(name) { - this.name = name; - }] + constructor: [ + new ng.core.Inject('heroName'), + function(name) { + this.name = name; + } + ] }); // #enddocregion ctor diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inline.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inline.component.js index 1fe9c38cb1..e5ef79bc4b 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inline.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inline.component.js @@ -5,11 +5,12 @@ template: '

Hero: {{name}}

' }) .Class({ - constructor: - [app.DataService, - function(service) { - this.name = service.getHeroName(); - }] + constructor: [ + app.DataService, + function(service) { + this.name = service.getHeroName(); + } + ] }); // #enddocregion diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-di.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-di.component.js index 55c576b836..c4e7d6735e 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-di.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-di.component.js @@ -6,9 +6,11 @@ function HeroComponent(dataService) { this.name = dataService.getHeroName(); } + HeroComponent.parameters = [ app.DataService ]; + HeroComponent.annotations = [ new ng.core.Component({ selector: 'hero-di', diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js index 5dd84733f3..a073138cff 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js @@ -5,8 +5,7 @@ // #docregion component var HeroComponent = ng.core.Component({ selector: 'hero-view-2', - template: - '

Name: {{getName()}}

', + template: '

Name: {{getName()}}

', }) .Class({ constructor: function() { diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-lifecycle.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-lifecycle.component.js index 3e81c72e4e..1a2028d316 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-lifecycle.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-lifecycle.component.js @@ -10,10 +10,9 @@ }) ]; // #docregion - HeroComponent.prototype.ngOnInit = - function() { - this.name = 'Windstorm'; - }; + HeroComponent.prototype.ngOnInit = function() { + this.name = 'Windstorm'; + }; // #enddocregion app.HeroesLifecycleModule = diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js index c6a58acc56..745a450096 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js @@ -15,13 +15,12 @@ HeroComponent.annotations = [ new ng.core.Component({ selector: 'hero-view', - template: - '

Hero: {{getName()}}

' + template: '

Hero: {{getName()}}

' }) ]; // #docregion constructorproto - HeroComponent.prototype.getName = - function() {return 'Windstorm';}; + + HeroComponent.prototype.getName = function() {return 'Windstorm';}; // #enddocregion constructorproto // #enddocregion metadata diff --git a/public/docs/_examples/cb-ts-to-js/js/app/main.js b/public/docs/_examples/cb-ts-to-js/js/app/main.js index 9091452599..015a8d2126 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/main.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/main.js @@ -4,12 +4,9 @@ // #enddocregion appimport // #docregion ng2import - var platformBrowserDynamic = - ng.platformBrowserDynamic.platformBrowserDynamic; - var LocationStrategy = - ng.common.LocationStrategy; - var HashLocationStrategy = - ng.common.HashLocationStrategy; + var platformBrowserDynamic = ng.platformBrowserDynamic.platformBrowserDynamic; + var LocationStrategy = ng.common.LocationStrategy; + var HashLocationStrategy = ng.common.HashLocationStrategy; // #enddocregion ng2import // #docregion appimport diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts index 146c6cbd83..d36b3eff19 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts @@ -19,12 +19,9 @@ import { BrowserModule } from '@angular/platform-browser'; class TitleComponent { private msg: string = ''; constructor( - @Inject('titlePrefix') - @Optional() - private titlePrefix: string, - @Attribute('title') - private title: string) { - } + @Inject('titlePrefix') @Optional() private titlePrefix: string, + @Attribute('title') private title: string + ) { } ok() { this.msg = 'OK!'; diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject.component.ts index 7dc2bc84a2..eb2cb78971 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject.component.ts @@ -7,10 +7,7 @@ import { BrowserModule } from '@angular/platform-browser'; template: `

Hero: {{name}}

` }) class HeroComponent { - constructor( - @Inject('heroName') - private name: string) { - } + constructor(@Inject('heroName') private name: string) { } } // #enddocregion diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di.component.ts index a20453ef0a..c1bb927219 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di.component.ts @@ -8,6 +8,7 @@ import { DataService } from './data.service'; selector: 'hero-di', template: `

Hero: {{name}}

` }) + class HeroComponent { name: string; constructor(dataService: DataService) { diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-lifecycle.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-lifecycle.component.ts index 1181c71cc9..64bdc9e927 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-lifecycle.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-lifecycle.component.ts @@ -10,8 +10,7 @@ import { BrowserModule } from '@angular/platform-browser'; template: `

Hero: {{name}}

` }) // #docregion -class HeroComponent - implements OnInit { +class HeroComponent implements OnInit { name: string; ngOnInit() { this.name = 'Windstorm'; diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/heroes-bindings.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/heroes-bindings.component.ts index 407b8b0e29..0ce026a42f 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/heroes-bindings.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/heroes-bindings.component.ts @@ -9,14 +9,15 @@ import { BrowserModule } from '@angular/platform-browser'; // #docregion @Component({ selector: 'heroes-bindings', - template: `

- Tour of Heroes -

` + template: ` +

+ Tour of Heroes +

+ ` }) class HeroesComponent { @HostBinding() title = 'Tooltip content'; - @HostBinding('class.heading') - hClass = true; + @HostBinding('class.heading') hClass = true; active: boolean; constructor() {} diff --git a/public/docs/_examples/package.json b/public/docs/_examples/package.json index a46ac1b718..5aeaa55c1f 100644 --- a/public/docs/_examples/package.json +++ b/public/docs/_examples/package.json @@ -27,7 +27,7 @@ "@angular/router": "~3.1.1", "@angular/upgrade": "~2.1.1", - "angular-in-memory-web-api": "~0.1.13", + "angular-in-memory-web-api": "~0.1.14", "core-js": "^2.4.1", "reflect-metadata": "^0.1.8", "rollup": "^0.36.0", @@ -51,6 +51,9 @@ "@types/selenium-webdriver": "^2.53.32", "angular2-template-loader": "^0.4.0", "awesome-typescript-loader": "^2.2.4", + "babel-cli": "^6.16.0", + "babel-preset-angular2": "^0.0.2", + "babel-preset-es2015": "^6.16.0", "canonical-path": "0.0.2", "concurrently": "^3.0.0", "css-loader": "^0.25.0", diff --git a/public/docs/js/latest/cookbook/ts-to-js.jade b/public/docs/js/latest/cookbook/ts-to-js.jade index a76d90bce7..1ed9ac2489 100644 --- a/public/docs/js/latest/cookbook/ts-to-js.jade +++ b/public/docs/js/latest/cookbook/ts-to-js.jade @@ -16,6 +16,8 @@ include ../../../../_includes/_util-fns :marked ## Table of contents + [From TS to ES6 to ES5](#from-ts) + [Modularity: imports and exports](#modularity) [Classes and Class Metadata](#class-metadata) @@ -24,90 +26,127 @@ include ../../../../_includes/_util-fns [Dependency Injection](#dependency-injection) - [Host and Query Metadata](#other-property-metadata) + [Host and Query Metadata](#host-query-metadata) **Run and compare the live TypeScript and JavaScript code shown in this cookbook.** +a(id="from-ts") +.l-main-section +:marked + ## From TS to ES6 to ES5 + + Since TypeScript is a superset of ES6 JavaScript, and ES6 itself is a superset of ES5, the + transformation of Typescript code all the way to ES5 javascript can be seen as "shedding" + features. + + When going from TypeScript to ES6 with decorators, we mostly remove + [type annotations](https://www.typescriptlang.org/docs/handbook/basic-types.html) + , such as `string` or `boolean`, and + [class property modifiers](http://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers) + such as `public` and `private`. + The exception is type annotations used for dependency injection, which can be kept. + + Going from ES6 with decorators to plain ES6 JavaScript we lose all + [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) + and the remaining type annotations. + We also lose class properties, which now have to be declared in the class constructor. + + Finally, in the transition from ES6 to ES5 JavaScript the main missing features are `import` + statements and `class` declarations. + + For ES6 transpilation we recommend using a similar setup as our TypeScript quickstart, + replacing TypeScript with [Babel](https://babeljs.io/) using the `es2015` preset. + To use decorators and annotations with Babel, install the + [`angular2`](https://github.com/shuhei/babel-plugin-angular2-annotations) preset as well. + a(id="modularity") .l-main-section :marked ## Importing and Exporting -- var top="vertical-align:top" -table(width="100%") - col(width="50%") - col(width="50%") - tr - th TypeScript - th ES5 JavaScript - tr(style=top) - td - :marked - ### Importing Angular Code - - In TypeScript code, Angular classes, functions, and other members - are imported with TypeScript `import` statements: - - +makeExample('cb-ts-to-js/ts/app/main.ts', 'ng2import')(format="." ) - - td - :marked - ### Accessing Angular Code through the ng global - - In JavaScript code, when using - [the Angular packages](../glossary.html#!#scoped-package), - we can access Angular code through the global `ng` object. In the - nested members of this object we'll find everything we would import - from `@angular` in TypeScript: - - +makeExample('cb-ts-to-js/js/app/main.js', 'ng2import')(format="." ) - - tr(style=top) - td - :marked - ### Importing and Exporting Application Code - - Each file in an Angular TypeScript application constitutes a - TypeScript module. When we want to make something from a module available - to other modules, we `export` it. - - +makeExample('cb-ts-to-js/ts/app/hero.component.ts', 'appexport')(format="." ) - - :marked - In other modules we can then `import` things that have been exported - elsewhere. - - +makeExample('cb-ts-to-js/ts/app/main.ts', 'appimport')(format="." ) + ### Importing Angular Code + + In TypeScript and ES6 JavaScript, Angular classes, functions, and other members are imported + with ES6 `import` statements. + + In ES5 JavaScript code, when using [the Angular packages](../glossary.html#!#scoped-package), + we can access Angular code through the global `ng` object. In the nested members of this + object we'll find everything we would import from `@angular` in TypeScript: + ++makeTabs(` + cb-ts-to-js/ts/app/main.ts, + cb-ts-to-js/js-es6-decorators/app/main.es6, + cb-ts-to-js/js-es6/app/main.es6, + cb-ts-to-js/js/app/main.js + `,` + ng2import, + ng2import, + ng2import, + ng2import + `,` + Typescript, + ES6 JavaScript with decorators, + ES6 JavaScript, + ES5 JavaScript + `) - td - :marked - ### Sharing Application Code - - In an Angular JavaScript application, we load each file to the page - using a ` - diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/confirm.component.html b/public/docs/_examples/cb-ts-to-js/ts/app/confirm.component.html new file mode 100644 index 0000000000..917bd1696f --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/confirm.component.html @@ -0,0 +1,6 @@ + + diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts index d36b3eff19..37e00960c9 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts @@ -8,14 +8,13 @@ import { import { BrowserModule } from '@angular/platform-browser'; // #docregion +// #docregion metadata @Component({ + moduleId: module.id, selector: 'hero-title', - template: ` -

{{titlePrefix}} {{title}}

- -

{{ msg }}

- ` + templateUrl: 'title.component.html' }) +// #enddocregion metadata class TitleComponent { private msg: string = ''; constructor( diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-io.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-io.component.ts index 8cf430c000..8105d98919 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-io.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-io.component.ts @@ -9,15 +9,9 @@ import { BrowserModule } from '@angular/platform-browser'; // #docregion @Component({ + moduleId: module.id, selector: 'my-confirm', - template: ` - - - ` + templateUrl: 'confirm.component.html' }) class ConfirmComponent { @Input() okMsg: string; diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/title.component.html b/public/docs/_examples/cb-ts-to-js/ts/app/title.component.html new file mode 100644 index 0000000000..0e93122d5c --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/title.component.html @@ -0,0 +1,4 @@ + +

{{titlePrefix}} {{title}}

+ +

{{ msg }}

diff --git a/public/docs/ts/latest/cookbook/ts-to-js.jade b/public/docs/ts/latest/cookbook/ts-to-js.jade index 966d1383fe..0c3ba61544 100644 --- a/public/docs/ts/latest/cookbook/ts-to-js.jade +++ b/public/docs/ts/latest/cookbook/ts-to-js.jade @@ -80,7 +80,7 @@ a#modularity In both _TypeScript_ and _ES6_, you import Angular classes, functions, and other members with _ES6_ `import` statements. - In _ES5_, you access the Angular entities of the [the Angular packages](../glossary.html#!#scoped-package), + In _ES5_, you access the Angular entities of the [the Angular packages](../glossary.html#!#scoped-package) through the global `ng` object. Everything you would have imported from `@angular` is a nested member of this `ng` object: @@ -102,23 +102,29 @@ a#modularity `) :marked - ### Importing and Exporting Application Code + ### Exporting Application Code Each file in a _TypeScript_ or _ES6_ Angular application constitutes an _ES6_ module. When you want to make something available to other modules, you `export` it. _ES5_ lacks native support for modules. - In an Angular _ES5_ application, you load each file manually by adding a ` @@ -20,32 +21,7 @@ - -

TypeScript to JavaScript

- Classes and Class Metadata
- Input and Output Metadata
- Dependency Injection
- Host and Query Metadata
- -
-

Classes and Class Metadata

- Loading hero-view... - Loading hero-lifecycle... - -
-

Input and Output Metadata

- Loading hero-io... - -
-

Dependency Injection

- Loading hero-di... - Loading hero-di-inject... - Loading hero-di-inject-additional... - -
-

Host and Query Metadata

- Loading heroes-bindings... - Loading heroes-queries... + Loading... diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/app.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/app.component.es6 new file mode 100644 index 0000000000..9615b98a1f --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/app.component.es6 @@ -0,0 +1,20 @@ +import { Component } from '@angular/core'; + +export class AppComponent { + constructor() { + this.title = 'Plain ES6 JavaScript'; + } +} + +AppComponent.annotations = [ + new Component({ + moduleId: module.id, + selector: 'my-app', + templateUrl: 'app.component.html', + styles: [ + // See hero-di-inject-additional.component + 'hero-host { border: 1px dashed black; display: block; padding: 4px;}', + '.heading {font-style: italic}' + ] + }) +]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/app.component.html b/public/docs/_examples/cb-ts-to-js/js-es6/app/app.component.html new file mode 100644 index 0000000000..8fbe65ade6 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/app.component.html @@ -0,0 +1,30 @@ + +

{{title}}

+Classes and Class Metadata
+Input and Output Metadata
+Dependency Injection
+Host Metadata
+View and Child Metadata
+ +
+

Classes and Class Metadata

+ + + +
+

Input and Output Metadata

+ + +
+

Dependency Injection

+ + + + +
+

Host Metadata

+ + +
+

View and Child Metadata

+ diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/app.module.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/app.module.es6 new file mode 100644 index 0000000000..e8c7a8f9c8 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/app.module.es6 @@ -0,0 +1,56 @@ +import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +import { AppComponent } from './app.component'; +import { ConfirmComponent } from './confirm.component'; +// #docregion appimport +import { HeroComponent } from './hero.component'; + +// #enddocregion appimport +import { HeroComponent as HeroDIComponent } from './hero-di.component'; +import { HeroComponent as HeroDIInjectComponent } from './hero-di-inject.component'; +import { HeroComponent as HeroDIInjectAdditionalComponent } from './hero-di-inject-additional.component'; +import { HeroHostComponent } from './hero-host.component'; +import { HeroIOComponent } from './hero-io.component'; +import { HeroComponent as HeroLifecycleComponent } from './hero-lifecycle.component'; +import { HeroQueriesComponent, ViewChildComponent, ContentChildComponent } from './hero-queries.component'; +import { HeroTitleComponent } from './hero-title.component'; + +import { DataService } from './data.service'; + +export class AppModule { } + +AppModule.annotations = [ + new NgModule({ + imports: [ BrowserModule], + declarations: [ + AppComponent, + ConfirmComponent, + HeroComponent, + HeroDIComponent, + HeroDIInjectComponent, + HeroDIInjectAdditionalComponent, + HeroHostComponent, + HeroIOComponent, + HeroLifecycleComponent, + HeroQueriesComponent, ViewChildComponent, ContentChildComponent, + HeroTitleComponent + ], + providers: [ + DataService, + { provide: 'heroName', useValue: 'Windstorm' } + ], + bootstrap: [ AppComponent ], + + // schemas: [ NO_ERRORS_SCHEMA ] // helpful for debugging + }) +] + +/* tslint:disable no-unused-variable */ +// #docregion ng2import +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; +import { + LocationStrategy, + HashLocationStrategy +} from '@angular/common'; +// #enddocregion ng2import diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/confirm.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/confirm.component.es6 new file mode 100644 index 0000000000..d42a553688 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/confirm.component.es6 @@ -0,0 +1,32 @@ +import { Component, EventEmitter } from '@angular/core'; + +// #docregion +export class ConfirmComponent { + constructor(){ + this.ok = new EventEmitter(); + this.notOk = new EventEmitter(); + } + onOkClick() { + this.ok.emit(true); + } + onNotOkClick() { + this.notOk.emit(true); + } +} + +ConfirmComponent.annotations = [ + new Component({ + moduleId: module.id, + selector: 'app-confirm', + templateUrl: 'confirm.component.html', + inputs: [ + 'okMsg', + 'notOkMsg: cancelMsg' + ], + outputs: [ + 'ok', + 'notOk: cancel' + ] + }) +]; +// #enddocregion diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6 index 398e38d352..5eb9bab815 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6 +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6 @@ -1,60 +1,10 @@ -import { - Attribute, - Component, - Inject, - Optional, - NgModule -} from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; +import { Component } from '@angular/core'; -// #docregion -class TitleComponent { - constructor(titlePrefix, title) { - this.titlePrefix = titlePrefix; - this.title = title; - this.msg = ''; - } +export class HeroComponent { } - ok() { - this.msg = 'OK!'; - } -} - -// #docregion metadata -TitleComponent.annotations = [ - new Component({ - moduleId: module.id, - selector: 'hero-title', - templateUrl: 'title.component.html' - }) -]; -// #enddocregion metadata - -TitleComponent.parameters = [ - [new Optional(), new Inject('titlePrefix')], - [new Attribute('title')] -]; -// #enddocregion - -class AppComponent { -} -AppComponent.annotations = [ +HeroComponent.annotations = [ new Component({ selector: 'hero-di-inject-additional', - template: ` - ` - }) -]; - -export class HeroesDIInjectAdditionalModule { } - -HeroesDIInjectAdditionalModule.annotations = [ - new NgModule({ - imports: [ BrowserModule ], - declarations: [ - AppComponent, - TitleComponent - ], - bootstrap: [ AppComponent ] + template: `` }) ]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject.component.es6 index cc8df60abc..2f95a0b981 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject.component.es6 +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di-inject.component.es6 @@ -1,8 +1,7 @@ -import { Component, Inject, NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; +import { Component, Inject } from '@angular/core'; // #docregion -class HeroComponent { +export class HeroComponent { constructor(name) { this.name = name; } @@ -19,14 +18,3 @@ HeroComponent.parameters = [ [new Inject('heroName')] ]; // #enddocregion - -export class HeroesDIInjectModule { } - -HeroesDIInjectModule.annotations = [ - new NgModule({ - imports: [ BrowserModule ], - providers: [ { provide: 'heroName', useValue: 'Windstorm' } ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) -]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di.component.es6 index bb569e6e2b..dfbecf0707 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di.component.es6 +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-di.component.es6 @@ -1,10 +1,8 @@ -import { Component, NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - +import { Component } from '@angular/core'; import { DataService } from './data.service'; // #docregion -class HeroComponent { +export class HeroComponent { constructor(dataService) { this.name = dataService.getHeroName(); } @@ -22,14 +20,3 @@ HeroComponent.parameters = [ ]; // #enddocregion - -export class HeroesDIModule { } - -HeroesDIModule.annotations = [ - new NgModule({ - imports: [ BrowserModule ], - providers: [ DataService ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) -]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-host.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-host.component.es6 new file mode 100644 index 0000000000..092cd1a1ce --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-host.component.es6 @@ -0,0 +1,50 @@ +import { Component } from '@angular/core'; + +// #docregion +export class HeroHostComponent { + constructor() { + this.active = false; + this.clicks = 0; + this.headingClass = true; + this.title = 'Hero Host Tooltip'; + } + + clicked() { + this.clicks += 1; + } + + enter(event) { + this.active = true; + this.headingClass = false; + } + + leave(event) { + this.active = false; + this.headingClass = true; + } +} + +// #docregion metadata +HeroHostComponent.annotations = [ + new Component({ + selector: 'hero-host', + template: ` +

Hero Host

+
Heading clicks: {{clicks}}
+ `, + host: { + // HostBindings to the element + '[title]': 'title', + '[class.heading]': 'headingClass', + '(click)': 'clicked()', + + // HostListeners on the entire element + '(mouseenter)': 'enter($event)', + '(mouseleave)': 'leave($event)' + }, + // Styles within (but excluding) the element + styles: ['.active {background-color: yellow;}'] + }) +]; +// #docregion metadata +// #enddocregion diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-io.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-io.component.es6 index 3ed92792db..0dc8c08f2d 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-io.component.es6 +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-io.component.es6 @@ -1,79 +1,31 @@ -import { - Component, - EventEmitter, - Input, - Output, - NgModule -} from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; +import { Component } from '@angular/core'; -// #docregion -class ConfirmComponent { - constructor(){ - this.ok = new EventEmitter(); - this.notOk = new EventEmitter(); +export class HeroIOComponent { + constructor() { + this.okClicked = false; + this.cancelClicked = false; } - onOkClick() { - this.ok.next(true); - } - onNotOkClick() { - this.notOk.next(true); - } -} - -ConfirmComponent.annotations = [ - new Component({ - moduleId: module.id, - selector: 'my-confirm', - templateUrl: 'confirm.component.html', - inputs: [ - 'okMsg', - 'notOkMsg: cancelMsg' - ], - outputs: [ - 'ok', - 'notOk: cancel' - ] - }) -]; -// #enddocregion -class AppComponent { - constructor(){ - } onOk() { this.okClicked = true; } + onCancel() { this.cancelClicked = true; } } -AppComponent.annotations = [ +HeroIOComponent.annotations = [ new Component({ selector: 'hero-io', template: ` - - + + OK clicked Cancel clicked ` }) ]; - - -export class HeroesIOModule { } - -HeroesIOModule.annotations = [ - new NgModule({ - imports: [ BrowserModule ], - declarations: [ - AppComponent, - ConfirmComponent - ], - bootstrap: [ AppComponent ] - }) -]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-lifecycle.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-lifecycle.component.es6 index 8a9c6c6c4e..b394ff59aa 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-lifecycle.component.es6 +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-lifecycle.component.es6 @@ -1,17 +1,11 @@ -// #docplaster // #docregion import { Component } from '@angular/core'; -// #enddocregion -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - -// #docregion -class HeroComponent { +export class HeroComponent { ngOnInit() { - this.name = 'Windstorm'; + // todo: fetch from server async + setTimeout(() => this.name = 'Windstorm', 0); } } -// #enddocregion HeroComponent.annotations = [ new Component({ @@ -19,13 +13,3 @@ HeroComponent.annotations = [ template: `

Hero: {{name}}

` }) ]; - -export class HeroesLifecycleModule { } - -HeroesLifecycleModule.annotations = [ - new NgModule({ - imports: [ BrowserModule ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) -]; \ No newline at end of file diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-queries.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-queries.component.es6 new file mode 100644 index 0000000000..bf3b914406 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-queries.component.es6 @@ -0,0 +1,97 @@ +import { + Component, + ContentChild, + Input, + QueryList, + ViewChildren +} from '@angular/core'; + +export class ContentChildComponent { + constructor() { + this.active = false; + } + + activate() { + this.active = !this.active; + } +} + +ContentChildComponent.annotations = [ + new Component({ + selector: 'content-child', + template: ` + + Active + ` + }) +]; + +//////////////////// + +// #docregion content +export class ViewChildComponent { + constructor() { + this.active = false; + } + + activate() { + this.active = !this.active; + this.content.activate(); + } +} + +ViewChildComponent.annotations = [ + new Component({ + selector: 'view-child', + template: `

+ {{hero.name}} + +

`, + styles: ['.active {font-weight: bold; background-color: skyblue;}'], + inputs: ['hero'], + queries: { + content: new ContentChild(ContentChildComponent) + } + }) +]; +// #enddocregion content + +//////////////////// + +// #docregion view +export class HeroQueriesComponent { + constructor(){ + this.active = false; + this.heroData = [ + {id: 1, name: 'Windstorm'}, + {id: 2, name: 'LaughingGas'} + ]; + } + + activate() { + this.active = !this.active; + this.views.forEach( + view => view.activate() + ); + } + + get buttonLabel() { + return this.active ? 'Deactivate' : 'Activate'; + } +} + +HeroQueriesComponent.annotations = [ + new Component({ + selector: 'hero-queries', + template: ` + + + + + `, + queries: { + views: new ViewChildren(ViewChildComponent) + } + }) +]; +// #enddocregion view diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-title.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-title.component.es6 new file mode 100644 index 0000000000..ff1e6ce026 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-title.component.es6 @@ -0,0 +1,29 @@ +import { Attribute, Component, Inject, Optional } from '@angular/core'; + +// #docregion +export class HeroTitleComponent { + constructor(titlePrefix, title) { + this.titlePrefix = titlePrefix; + this.title = title; + this.msg = ''; + } + + ok() { + this.msg = 'OK!'; + } +} + +// #docregion templateUrl +HeroTitleComponent.annotations = [ + new Component({ + moduleId: module.id, + selector: 'hero-title', + templateUrl: 'hero-title.component.html' + }) +]; +// #enddocregion templateUrl + +HeroTitleComponent.parameters = [ + [new Optional(), new Inject('titlePrefix')], + [new Attribute('title')] +]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/title.component.html b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-title.component.html similarity index 78% rename from public/docs/_examples/cb-ts-to-js/js-es6/app/title.component.html rename to public/docs/_examples/cb-ts-to-js/js-es6/app/hero-title.component.html index 06d6f3db91..0e93122d5c 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/title.component.html +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero-title.component.html @@ -1,3 +1,4 @@ +

{{titlePrefix}} {{title}}

{{ msg }}

diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 index c998d31a6f..10b92c2878 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/hero.component.es6 @@ -2,16 +2,14 @@ // #docregion metadata import { Component } from '@angular/core'; -// #docregion class -// #docregion appexport +// #docregion appexport, class export class HeroComponent { constructor() { this.title = 'Hero Detail'; } getName() {return 'Windstorm'; } } -// #enddocregion appexport -// #enddocregion class +// #enddocregion appexport, class HeroComponent.annotations = [ new Component({ @@ -20,18 +18,3 @@ HeroComponent.annotations = [ }) ]; // #enddocregion metadata - -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - -export class HeroesModule { } - -HeroesModule.annotations = [ - new NgModule({ - imports: [ BrowserModule ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) -]; - - diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-bindings.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-bindings.component.es6 deleted file mode 100644 index 5c13e586a6..0000000000 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-bindings.component.es6 +++ /dev/null @@ -1,48 +0,0 @@ -import { - Component, - HostBinding, - HostListener, - NgModule -} from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - -// #docregion -class HeroesComponent { - constructor() { - this.title = 'Tooltip content'; - this.hClass = true; - } - - clicked() { - this.active = !this.active; - } - - doubleClicked(evt) { - this.active = true; - } -} -HeroesComponent.annotations = [ - new Component({ - selector: 'heroes-bindings', - template: `

- Tour of Heroes -

`, - host: { - '[title]': 'title', - '[class.heading]': 'hClass', - '(click)': 'clicked()', - '(dblclick)': 'doubleClicked($event)' - } - }) -]; -// #enddocregion - -export class HeroesHostBindingsModule { } - -HeroesHostBindingsModule.annotations = [ - new NgModule({ - imports: [ BrowserModule ], - declarations: [ HeroesComponent ], - bootstrap: [ HeroesComponent ] - }) -]; \ No newline at end of file diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-queries.component.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-queries.component.es6 deleted file mode 100644 index 4cec0c3bbd..0000000000 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/heroes-queries.component.es6 +++ /dev/null @@ -1,98 +0,0 @@ -import { - Component, - ViewChildren, - ContentChild, - QueryList, - Input, - NgModule -} from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - -class ActiveLabelComponent { - activate() { - this.active = true; - } -} - -ActiveLabelComponent.annotations = [ - new Component({ - selector: 'active-label', - template: ` - - Active - ` - }) -]; - -// #docregion content -class HeroComponent { - activate() { - this.active = true; - this.label.activate(); - } -} - -HeroComponent.annotations = [ - new Component({ - selector: 'a-hero', - template: `

- {{hero.name}} - -

`, - inputs: ['hero'], - queries: { - label: new ContentChild(ActiveLabelComponent) - } - }) -]; -// #enddocregion content - - -// #docregion view -class HeroesQueriesComponent { - constructor(){ - this.heroData = [ - {id: 1, name: 'Windstorm'}, - {id: 2, name: 'Superman'} - ]; - } - - activate() { - this.heroCmps.forEach( - (cmp) => cmp.activate() - ); - } -} - -HeroesQueriesComponent.annotations = [ - new Component({ - selector: 'heroes-queries', - template: ` - - - - - `, - queries: { - heroCmps: new ViewChildren(HeroComponent) - } - }) -]; -// #enddocregion view - -export class HeroesQueriesModule { } - -HeroesQueriesModule.annotations = [ - new NgModule({ - imports: [ BrowserModule ], - declarations: [ - HeroesQueriesComponent, - HeroComponent, - ActiveLabelComponent - ], - bootstrap: [ HeroesQueriesComponent ] - }) -]; diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/app/main.es6 b/public/docs/_examples/cb-ts-to-js/js-es6/app/main.es6 index 92f5af5e1a..2470c9595e 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/app/main.es6 +++ b/public/docs/_examples/cb-ts-to-js/js-es6/app/main.es6 @@ -1,30 +1,4 @@ -/* tslint:disable no-unused-variable */ -// #docregion ng2import import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; -import { - LocationStrategy, - HashLocationStrategy -} from '@angular/common'; -// #enddocregion ng2import +import { AppModule } from './app.module'; -// #docregion appimport -import { HeroComponent } from './hero.component'; -// #enddocregion appimport - -import { HeroesModule } from './hero.component'; -import { HeroesLifecycleModule } from './hero-lifecycle.component'; -import { HeroesDIModule } from './hero-di.component'; -import { HeroesDIInjectModule } from './hero-di-inject.component'; -import { HeroesDIInjectAdditionalModule } from './hero-di-inject-additional.component'; -import { HeroesIOModule } from './hero-io.component'; -import { HeroesHostBindingsModule } from './heroes-bindings.component'; -import { HeroesQueriesModule } from './heroes-queries.component'; - -platformBrowserDynamic().bootstrapModule(HeroesModule); -platformBrowserDynamic().bootstrapModule(HeroesLifecycleModule); -platformBrowserDynamic().bootstrapModule(HeroesDIModule); -platformBrowserDynamic().bootstrapModule(HeroesDIInjectModule); -platformBrowserDynamic().bootstrapModule(HeroesDIInjectAdditionalModule); -platformBrowserDynamic().bootstrapModule(HeroesIOModule); -platformBrowserDynamic().bootstrapModule(HeroesHostBindingsModule); -platformBrowserDynamic().bootstrapModule(HeroesQueriesModule); +platformBrowserDynamic().bootstrapModule(AppModule); diff --git a/public/docs/_examples/cb-ts-to-js/js-es6/index.html b/public/docs/_examples/cb-ts-to-js/js-es6/index.html index 3a8a4e7960..1b9e3b5211 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6/index.html +++ b/public/docs/_examples/cb-ts-to-js/js-es6/index.html @@ -5,6 +5,7 @@ + TypeScript to JavaScript @@ -20,33 +21,7 @@ - -

TypeScript to JavaScript

- Classes and Class Metadata
- Input and Output Metadata
- Dependency Injection
- Host and Query Metadata
- - -
-

Classes and Class Metadata

- Loading hero-view... - Loading hero-lifecycle... - -
-

Input and Output Metadata

- Loading hero-io... - -
-

Dependency Injection

- Loading hero-di... - Loading hero-di-inject... - Loading hero-di-inject-additional... - -
-

Host and Query Metadata

- Loading heroes-bindings... - Loading heroes-queries... + Loading... diff --git a/public/docs/_examples/cb-ts-to-js/js/app/app.component.html b/public/docs/_examples/cb-ts-to-js/js/app/app.component.html new file mode 100644 index 0000000000..6681d60672 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js/app/app.component.html @@ -0,0 +1,47 @@ + +

{{title}}

+Classes and Class Metadata
+Interfaces
+Input and Output Metadata
+Dependency Injection
+Host Metadata
+View and Child Metadata
+ +
+

Classes and Class Metadata

+ +

Classes and Class Metadata (DSL)

+ + +
+

Interfaces

+ +

Interfaces (DSL)

+ + +
+

Input and Output Metadata

+ +

Input and Output Metadata (DSL)

+ + +
+

Dependency Injection

+ + + + +

Dependency Injection (DSL)

+ + + + +
+

Host Metadata

+ +

Host Metadata (DSL)

+ + +
+

View and Child Metadata (DSL)

+ diff --git a/public/docs/_examples/cb-ts-to-js/js/app/app.component.js b/public/docs/_examples/cb-ts-to-js/js/app/app.component.js new file mode 100644 index 0000000000..9e8c5da535 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js/app/app.component.js @@ -0,0 +1,20 @@ +(function(app) { + +app.AppComponent = AppComponent; +function AppComponent() { + this.title = 'ES5 JavaScript'; +} + +AppComponent.annotations = [ + new ng.core.Component({ + selector: 'my-app', + templateUrl: 'app/app.component.html', + styles: [ + // See hero-di-inject-additional.component + 'hero-host, hero-host-dsl { border: 1px dashed black; display: block; padding: 4px;}', + '.heading {font-style: italic}' + ] + }) +]; + +})(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/app.module.js b/public/docs/_examples/cb-ts-to-js/js/app/app.module.js new file mode 100644 index 0000000000..3791fe16da --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js/app/app.module.js @@ -0,0 +1,46 @@ +(function(app) { + +app.AppModule = AppModule; +function AppModule() { } + +AppModule.annotations = [ + new ng.core.NgModule({ + imports: [ ng.platformBrowser.BrowserModule ], + declarations: [ + app.AppComponent, + app.ConfirmComponent, app.ConfirmDslComponent, + app.HeroComponent, app.HeroDslComponent, + app.HeroDIComponent, app.HeroDIDslComponent, + app.HeroDIInjectComponent, app.HeroDIInjectDslComponent, + app.HeroDIInjectAdditionalComponent, app.HeroDIInjectAdditionalDslComponent, + app.HeroHostComponent, app.HeroHostDslComponent, + app.HeroIOComponent, app.HeroIODslComponent, + app.HeroLifecycleComponent, app.HeroLifecycleDslComponent, + app.heroQueries.HeroQueriesComponent, app.heroQueries.ViewChildComponent, app.heroQueries.ContentChildComponent, + app.HeroTitleComponent, app.HeroTitleDslComponent + ], + providers: [ + app.DataService, + { provide: 'heroName', useValue: 'Windstorm' } + ], + bootstrap: [ app.AppComponent ], + + // schemas: [ ng.core.NO_ERRORS_SCHEMA ] // helpful for debugging! + }) +] + +})(window.app = window.app || {}); + + +///// For documentation only ///// +(function () { + // #docregion appimport + var HeroComponent = app.HeroComponent; + // #enddocregion appimport + + // #docregion ng2import + var platformBrowserDynamic = ng.platformBrowserDynamic.platformBrowserDynamic; + var LocationStrategy = ng.common.LocationStrategy; + var HashLocationStrategy = ng.common.HashLocationStrategy; + // #enddocregion ng2import +}) diff --git a/public/docs/_examples/cb-ts-to-js/js/app/confirm.component.js b/public/docs/_examples/cb-ts-to-js/js/app/confirm.component.js new file mode 100644 index 0000000000..4a4cd0a470 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js/app/confirm.component.js @@ -0,0 +1,73 @@ +(function(app) { + +// #docregion +app.ConfirmComponent = ConfirmComponent; + +ConfirmComponent.annotations = [ + new ng.core.Component({ + selector: 'app-confirm', + templateUrl: 'app/confirm.component.html', + inputs: [ + 'okMsg', + 'notOkMsg: cancelMsg' + ], + outputs: [ + 'ok', + 'notOk: cancel' + ] + }) +]; + +function ConfirmComponent() { + this.ok = new ng.core.EventEmitter(); + this.notOk = new ng.core.EventEmitter(); +} + +ConfirmComponent.prototype.onOkClick = function() { + this.ok.emit(true); +} + +ConfirmComponent.prototype.onNotOkClick = function() { + this.notOk.emit(true); +} + + +// #enddocregion + +})(window.app = window.app || {}); + +/////// DSL version //////// + +(function(app) { + +// #docregion dsl +app.ConfirmDslComponent = ng.core.Component({ + selector: 'app-confirm-dsl', + templateUrl: 'app/confirm.component.html', + inputs: [ + 'okMsg', + 'notOkMsg: cancelMsg' + ], + outputs: [ + 'ok', + 'notOk: cancel' + ] + }) + .Class({ + constructor: function ConfirmDslComponent() { + this.ok = new ng.core.EventEmitter(); + this.notOk = new ng.core.EventEmitter(); + }, + + onOkClick: function() { + this.ok.emit(true); + }, + + onNotOkClick: function() { + this.notOk.emit(true); + } + }); + +// #enddocregion dsl + +})(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/data.service.js b/public/docs/_examples/cb-ts-to-js/js/app/data.service.js index c060e2b39d..643bb57dca 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/data.service.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/data.service.js @@ -1,13 +1,10 @@ (function(app) { - function DataService() { - } - DataService.annotations = [ - new ng.core.Injectable() - ]; + app.DataService = DataService; + function DataService() { } + DataService.prototype.getHeroName = function() { return 'Windstorm'; }; - app.DataService = DataService; })(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject-additional.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject-additional.component.js index 127bf524a8..1a88cfa355 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject-additional.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject-additional.component.js @@ -1,47 +1,26 @@ (function(app) { - // #docregion - // #docregion metadata - var TitleComponent = ng.core.Component({ - selector: 'hero-title', - templateUrl: 'app/title.component.html' - }) - // #enddocregion metadata - .Class({ - constructor: [ - [ new ng.core.Optional(), new ng.core.Inject('titlePrefix') ], - new ng.core.Attribute('title'), - function(titlePrefix, title) { - this.titlePrefix = titlePrefix; - this.title = title; - this.msg = ''; - } - ], - ok: function() { - this.msg = 'OK!'; - } - }); - // #enddocregion +app.HeroDIInjectAdditionalComponent = HeroDIInjectAdditionalComponent; - var AppComponent = ng.core.Component({ +HeroDIInjectAdditionalComponent.annotations = [ + new ng.core.Component({ selector: 'hero-di-inject-additional', - template: '' + - '' - }).Class({ - constructor: function() { } - }); + template: '' + }) +]; + +function HeroDIInjectAdditionalComponent() {} + +})(window.app = window.app || {}); + +////// DSL Version ///////// +(function(app) { - app.HeroesDIInjectAdditionalModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - declarations: [ - AppComponent, - TitleComponent - ], - bootstrap: [ AppComponent ] - }) - .Class({ - constructor: function() {} - }); +app.HeroDIInjectAdditionalDslComponent = ng.core.Component({ + selector: 'hero-di-inject-additional-dsl', + template: '' +}).Class({ + constructor: function HeroDIInjectAdditionalDslComponent() { } +}); })(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject.component.js index 743af3b987..2e04c9246e 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inject.component.js @@ -1,58 +1,41 @@ (function(app) { -// #docregion parameters - function HeroComponent(name) { - this.name = name; - } - HeroComponent.parameters = [ - 'heroName' - ]; - HeroComponent.annotations = [ - new ng.core.Component({ - selector: 'hero-di-inject', - template: '

Hero: {{name}}

' - }) - ]; -// #enddocregion parameters - - app.HeroesDIInjectModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - providers: [ { provide: 'heroName', useValue: 'Windstorm' } ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) - .Class({ - constructor: function() {} - }); +// #docregion +app.HeroDIInjectComponent = HeroDIInjectComponent; + +HeroDIInjectComponent.annotations = [ + new ng.core.Component({ + selector: 'hero-di-inject', + template: '

Hero: {{name}}

' + }) +]; + +HeroDIInjectComponent.parameters = [ 'heroName' ]; + +function HeroDIInjectComponent(name) { + this.name = name; +} +// #enddocregion })(window.app = window.app || {}); +/////// DSL version //////// + (function(app) { -// #docregion ctor - var HeroComponent = ng.core.Component({ - selector: 'hero-di-inline2', - template: '

Hero: {{name}}

' - }) - .Class({ - constructor: [ - new ng.core.Inject('heroName'), - function(name) { - this.name = name; - } - ] - }); -// #enddocregion ctor - - app.HeroesDIInjectModule2 = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - providers: [ { provide: 'heroName', useValue: 'Bombasto' } ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) - .Class({ - constructor: function() {} - }); + +// #docregion dsl +app.HeroDIInjectDslComponent = ng.core.Component({ + selector: 'hero-di-inject-dsl', + template: '

Hero: {{name}}

' +}) +.Class({ + constructor: [ + new ng.core.Inject('heroName'), + function HeroDIInjectDslComponent(name) { + this.name = name; + } + ] +}); +// #enddocregion dsl })(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inline.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inline.component.js deleted file mode 100644 index e5ef79bc4b..0000000000 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-di-inline.component.js +++ /dev/null @@ -1,28 +0,0 @@ -(function(app) { - // #docregion - var HeroComponent = ng.core.Component({ - selector: 'hero-di-inline', - template: '

Hero: {{name}}

' - }) - .Class({ - constructor: [ - app.DataService, - function(service) { - this.name = service.getHeroName(); - } - ] - }); - // #enddocregion - - app.HeroDIInlineModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - providers: [ app.DataService ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) - .Class({ - constructor: function() {} - }); - -})(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-di.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-di.component.js index c4e7d6735e..c80efe193e 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-di.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-di.component.js @@ -1,33 +1,43 @@ (function(app) { // #docregion - app.HeroDIComponent = HeroComponent; - - function HeroComponent(dataService) { - this.name = dataService.getHeroName(); - } - - HeroComponent.parameters = [ - app.DataService - ]; + app.HeroDIComponent = HeroDIComponent; - HeroComponent.annotations = [ + HeroDIComponent.annotations = [ new ng.core.Component({ selector: 'hero-di', template: '

Hero: {{name}}

' }) ]; + + HeroDIComponent.parameters = [ app.DataService ]; + + function HeroDIComponent(dataService) { + this.name = dataService.getHeroName(); + } + // #enddocregion - app.HeroesDIModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - providers: [ app.DataService ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) - .Class({ - constructor: function() {} - }); + +})(window.app = window.app || {}); + +////// DSL Version ///// + +(function(app) { + +// #docregion dsl +app.HeroDIDslComponent = ng.core.Component({ + selector: 'hero-di-dsl', + template: '

Hero: {{name}}

' +}) +.Class({ + constructor: [ + app.DataService, + function HeroDIDslComponent(service) { + this.name = service.getHeroName(); + } + ] +}); +// #enddocregion dsl })(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js deleted file mode 100644 index 6d53bc2850..0000000000 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-dsl.component.js +++ /dev/null @@ -1,29 +0,0 @@ -// #docplaster -// #docregion appexport -(function(app) { - - // #docregion component - var HeroComponent = ng.core.Component({ - selector: 'hero-view-2', - template: '

{{title}}: {{getName()}}

', - }) - .Class({ - constructor: function() { - this.title = "Hero Detail"; - }, - getName: function() { return 'Windstorm'; } - }); - // #enddocregion component - - app.HeroesDslModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) - .Class({ - constructor: function() {} - }); - -})(window.app = window.app || {}); -// #enddocregion appexport diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-host.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-host.component.js new file mode 100644 index 0000000000..0b04d5efae --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-host.component.js @@ -0,0 +1,97 @@ +(function(app) { + +// #docregion +app.HeroHostComponent = HeroHostComponent; + +HeroHostComponent.annotations = [ + new ng.core.Component({ + selector: 'hero-host', + template: + '

Hero Host

' + + '
Heading clicks: {{clicks}}
', + host: { + // HostBindings to the element + '[title]': 'title', + '[class.heading]': 'headingClass', + '(click)': 'clicked()', + + // HostListeners on the entire element + '(mouseenter)': 'enter($event)', + '(mouseleave)': 'leave($event)' + }, + // Styles within (but excluding) the element + styles: ['.active {background-color: yellow;}'] + }) +]; + +function HeroHostComponent() { + this.clicks = 0; + this.headingClass = true; + this.title = 'Hero Host Tooltip content'; +} + +HeroHostComponent.prototype.clicked = function() { + this.clicks += 1; +} + +HeroHostComponent.prototype.enter = function(event) { + this.active = true; + this.headingClass = false; +} + +HeroHostComponent.prototype.leave = function(event) { + this.active = false; + this.headingClass = true; +} +// #enddocregion + +})(window.app = window.app || {}); + +//// DSL Version //// + +(function(app) { + +// #docregion dsl +app.HeroHostDslComponent = ng.core.Component({ + selector: 'hero-host-dsl', + template: ` +

Hero Host (DSL)

+
Heading clicks: {{clicks}}
+ `, + host: { + // HostBindings to the element + '[title]': 'title', + '[class.heading]': 'headingClass', + '(click)': 'clicked()', + + // HostListeners on the entire element + '(mouseenter)': 'enter($event)', + '(mouseleave)': 'leave($event)' + }, + // Styles within (but excluding) the element + styles: ['.active {background-color: coral;}'] +}) +.Class({ + constructor: function HeroHostDslComponent() { + this.clicks = 0; + this.headingClass = true; + this.title = 'Hero Host Tooltip DSL content'; + }, + + clicked() { + this.clicks += 1; + }, + + enter(event) { + this.active = true; + this.headingClass = false; + }, + + leave(event) { + this.active = false; + this.headingClass = true; + } +}); +// #enddocregion dsl + +})(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-io-dsl.component.html b/public/docs/_examples/cb-ts-to-js/js/app/hero-io-dsl.component.html new file mode 100644 index 0000000000..48c491be35 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-io-dsl.component.html @@ -0,0 +1,7 @@ + + +OK clicked +Cancel clicked diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-io.component.html b/public/docs/_examples/cb-ts-to-js/js/app/hero-io.component.html new file mode 100644 index 0000000000..e6eae2e804 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-io.component.html @@ -0,0 +1,7 @@ + + +OK clicked +Cancel clicked diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-io.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-io.component.js index 611d3b842b..d35f3762da 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-io.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-io.component.js @@ -1,62 +1,42 @@ (function(app) { - // #docregion - var ConfirmComponent = ng.core.Component({ - selector: 'my-confirm', - templateUrl: 'app/confirm.component.html', - inputs: [ - 'okMsg', - 'notOkMsg: cancelMsg' - ], - outputs: [ - 'ok', - 'notOk: cancel' - ] - }).Class({ - constructor: function() { - this.ok = new ng.core.EventEmitter(); - this.notOk = new ng.core.EventEmitter(); - }, - onOkClick: function() { - this.ok.next(true); + +app.HeroIOComponent = HeroComponent; + +HeroComponent.annotations = [ + new ng.core.Component({ + selector: 'hero-io', + templateUrl: 'app/hero-io.component.html' + }) +]; + +function HeroComponent() { } + +HeroComponent.prototype.onOk = function() { + this.okClicked = true; +} + +HeroComponent.prototype.onCancel = function() { + this.cancelClicked = true; +} + +})(window.app = window.app || {}); + +///// DSL Version //// + +(function(app) { + +app.HeroIODslComponent = ng.core.Component({ + selector: 'hero-io-dsl', + templateUrl: 'app/hero-io-dsl.component.html' + }) + .Class({ + constructor: function HeroIODslComponent() { }, + onOk: function() { + this.okClicked = true; }, - onNotOkClick: function() { - this.notOk.next(true); + onCancel: function() { + this.cancelClicked = true; } }); - // #enddocregion - - function AppComponent() { - } - AppComponent.annotations = [ - new ng.core.Component({ - selector: 'hero-io', - template: '' + - '' + - 'OK clicked' + - 'Cancel clicked' - }) - ]; - AppComponent.prototype.onOk = function() { - this.okClicked = true; - } - AppComponent.prototype.onCancel = function() { - this.cancelClicked = true; - } - - app.HeroesIOModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - declarations: [ - AppComponent, - ConfirmComponent - ], - bootstrap: [ AppComponent ] - }) - .Class({ - constructor: function() {} - }); })(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-lifecycle.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-lifecycle.component.js index 1a2028d316..94aa3514b2 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero-lifecycle.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-lifecycle.component.js @@ -1,28 +1,42 @@ // #docplaster (function(app) { - // #docregion - function HeroComponent() {} - // #enddocregion - HeroComponent.annotations = [ - new ng.core.Component({ - selector: 'hero-lifecycle', - template: '

Hero: {{name}}

' - }) - ]; - // #docregion - HeroComponent.prototype.ngOnInit = function() { - this.name = 'Windstorm'; - }; - // #enddocregion - - app.HeroesLifecycleModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) - .Class({ - constructor: function() {} - }); + +// #docregion +app.HeroLifecycleComponent = HeroComponent; + +HeroComponent.annotations = [ + new ng.core.Component({ + selector: 'hero-lifecycle', + template: '

Hero: {{name}}

' + }) +]; + +function HeroComponent() { } + +HeroComponent.prototype.ngOnInit = function() { + // todo: fetch from server async + setTimeout(() => this.name = 'Windstorm', 0); +}; +// #enddocregion + +})(window.app = window.app || {}); + +/////// DSL version //// + +(function(app) { + +// #docregion dsl +app.HeroLifecycleDslComponent = ng.core.Component({ + selector: 'hero-lifecycle-dsl', + template: '

Hero: {{name}}

' + }) + .Class({ + constructor: function HeroLifecycleDslComponent() { }, + ngOnInit: function() { + // todo: fetch from server async + setTimeout(() => this.name = 'Windstorm', 0); + } + }); +// #enddocregion dsl })(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-queries.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-queries.component.js new file mode 100644 index 0000000000..5e21cfcb0b --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-queries.component.js @@ -0,0 +1,92 @@ +(function(app) { + +app.heroQueries = app.heroQueries || {}; + +app.heroQueries.ContentChildComponent = ng.core.Component({ + selector: 'content-child', + template: + '' + + 'Active' + + '' +}).Class({ + constructor: function ContentChildComponent() { + this.active = false; + }, + + activate: function() { + this.active = !this.active; + } +}); + +//////////////////// + +// #docregion content +app.heroQueries.ViewChildComponent = ng.core.Component({ + selector: 'view-child', + template: + '

' + + '{{hero.name}} ' + + '' + + '

', + styles: ['.active {font-weight: bold; background-color: skyblue;}'], + inputs: ['hero'], + queries: { + content: new ng.core.ContentChild(app.heroQueries.ContentChildComponent) + } +}) +.Class({ + constructor: function HeroQueriesHeroComponent() { + this.active = false; + }, + + activate: function() { + this.active = !this.active; + this.content.activate(); + } +}); +// #enddocregion content + +//////////////////// + +// #docregion view +app.heroQueries.HeroQueriesComponent = ng.core.Component({ + selector: 'hero-queries', + template: + '' + + '' + + '' + + '', + queries: { + views: new ng.core.ViewChildren(app.heroQueries.ViewChildComponent) + } +}) +.Class({ + constructor: function HeroQueriesComponent() { + this.active = false; + this.heroData = [ + {id: 1, name: 'Windstorm'}, + {id: 2, name: 'LaughingGas'} + ]; + }, + + activate: function() { + this.active = !this.active; + this.views.forEach(function(view) { + view.activate(); + }); + }, +}); + +// #docregion defined-property +// add prototype property w/ getter outside the DSL +var proto = app.heroQueries.HeroQueriesComponent.prototype; +Object.defineProperty(proto, "buttonLabel", { + get: function () { + return this.active ? 'Deactivate' : 'Activate'; + }, + enumerable: true +}); +// #enddocregion defined-property +// #enddocregion view + +})(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/title.component.html b/public/docs/_examples/cb-ts-to-js/js/app/hero-title.component.html similarity index 78% rename from public/docs/_examples/cb-ts-to-js/js/app/title.component.html rename to public/docs/_examples/cb-ts-to-js/js/app/hero-title.component.html index 06d6f3db91..0e93122d5c 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/title.component.html +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-title.component.html @@ -1,3 +1,4 @@ +

{{titlePrefix}} {{title}}

{{ msg }}

diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero-title.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero-title.component.js new file mode 100644 index 0000000000..9a8ce7e578 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero-title.component.js @@ -0,0 +1,60 @@ +(function(app) { + +// #docregion +app.HeroTitleComponent = HeroTitleComponent; + +// #docregion templateUrl +HeroTitleComponent.annotations = [ + new ng.core.Component({ + selector: 'hero-title', + templateUrl: 'app/hero-title.component.html' + }) +]; +// #enddocregion templateUrl + +function HeroTitleComponent(titlePrefix, title) { + this.titlePrefix = titlePrefix; + this.title = title; + this.msg = ''; +} + +HeroTitleComponent.prototype.ok = function() { + this.msg = 'OK!'; +} + +HeroTitleComponent.parameters = [ + [new ng.core.Optional(), new ng.core.Inject('titlePrefix')], + [new ng.core.Attribute('title')] +]; + +// #enddocregion + +})(window.app = window.app || {}); + +////////// DSL version //////////// + +(function(app) { + +// #docregion dsl +app.HeroTitleDslComponent = ng.core.Component({ + selector: 'hero-title-dsl', + templateUrl: 'app/hero-title.component.html' +}) +.Class({ + constructor: [ + [ new ng.core.Optional(), new ng.core.Inject('titlePrefix') ], + new ng.core.Attribute('title'), + function HeroTitleDslComponent(titlePrefix, title) { + this.titlePrefix = titlePrefix; + this.title = title; + this.msg = ''; + } + ], + + ok: function() { + this.msg = 'OK!'; + } +}); +// #enddocregion dsl + +})(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js b/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js index ec5b706624..346d6f03d8 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/hero.component.js @@ -1,38 +1,48 @@ // #docplaster +(function(app) { + +// #docregion // #docregion appexport +// #docregion metadata +app.HeroComponent = HeroComponent; // "export" + +HeroComponent.annotations = [ + new ng.core.Component({ + selector: 'hero-view', + template: '

{{title}}: {{getName()}}

' + }) +]; + +// #docregion constructorproto +function HeroComponent() { + this.title = "Hero Detail"; +} + +HeroComponent.prototype.getName = function() { return 'Windstorm'; }; +// #enddocregion constructorproto + +// #enddocregion metadata +// #enddocregion appexport +// #enddocregion + +})(window.app = window.app || {}); + +//////////// DSL version /////////// + (function(app) { - // #enddocregion appexport - - // #docregion metadata - // #docregion appexport - // #docregion constructorproto - function HeroComponent() { - this.title = "Hero Detail"; - } - HeroComponent.prototype.getName = function() { return 'Windstorm'; }; - // #enddocregion constructorproto - - // #enddocregion appexport - HeroComponent.annotations = [ - new ng.core.Component({ - selector: 'hero-view', - template: '

{{title}}: {{getName()}}

' - }) - ]; - // #enddocregion metadata - - app.HeroesModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] - }) - .Class({ - constructor: function() {} - }); - - // #docregion appexport - app.HeroComponent = HeroComponent; + +// #docregion dsl +app.HeroDslComponent = ng.core.Component({ + selector: 'hero-view-dsl', + template: '

{{title}}: {{getName()}}

', + }) + .Class({ + constructor: function HeroDslComponent() { + this.title = "Hero Detail"; + }, + + getName: function() { return 'Windstorm'; } + }); +// #enddocregion dsl })(window.app = window.app || {}); -// #enddocregion appexport diff --git a/public/docs/_examples/cb-ts-to-js/js/app/heroes-bindings.component.js b/public/docs/_examples/cb-ts-to-js/js/app/heroes-bindings.component.js deleted file mode 100644 index 3a562d6912..0000000000 --- a/public/docs/_examples/cb-ts-to-js/js/app/heroes-bindings.component.js +++ /dev/null @@ -1,39 +0,0 @@ -(function(app) { - - // #docregion - var HeroesComponent = ng.core.Component({ - selector: 'heroes-bindings', - template: '

' + - 'Tour of Heroes' + - '

', - host: { - '[title]': 'title', - '[class.heading]': 'hClass', - '(click)': 'clicked()', - '(dblclick)': 'doubleClicked($event)' - } - }).Class({ - constructor: function() { - this.title = 'Tooltip content'; - this.hClass = true; - }, - clicked: function() { - this.active = !this.active; - }, - doubleClicked: function(evt) { - this.active = true; - } - }); - // #enddocregion - - app.HeroesHostBindingsModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - declarations: [ HeroesComponent ], - bootstrap: [ HeroesComponent ] - }) - .Class({ - constructor: function() {} - }); - -})(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/heroes-queries.component.js b/public/docs/_examples/cb-ts-to-js/js/app/heroes-queries.component.js deleted file mode 100644 index f1bda77cb1..0000000000 --- a/public/docs/_examples/cb-ts-to-js/js/app/heroes-queries.component.js +++ /dev/null @@ -1,82 +0,0 @@ -(function(app) { - - var ActiveLabelComponent = ng.core.Component({ - selector: 'active-label', - template: '' + - 'Active' + - '' - }).Class({ - constructor: [function() { }], - activate: function() { - this.active = true; - } - }); - - // #docregion content - var HeroComponent = ng.core.Component({ - selector: 'a-hero', - template: '

' + - '{{hero.name}} ' + - '' + - '

', - inputs: ['hero'], - queries: { - label: new ng.core.ContentChild( - ActiveLabelComponent) - } - }).Class({ - constructor: [function() { }], - activate: function() { - this.active = true; - this.label.activate(); - } - }); - app.HeroQueriesComponent = HeroComponent; - // #enddocregion content - - // #docregion view - var AppComponent = ng.core.Component({ - selector: 'heroes-queries', - template: - '' + - '' + - '' + - '', - queries: { - heroCmps: new ng.core.ViewChildren( - HeroComponent) - } - }).Class({ - constructor: function() { - this.heroData = [ - {id: 1, name: 'Windstorm'}, - {id: 2, name: 'Superman'} - ]; - }, - activate: function() { - this.heroCmps.forEach(function(cmp) { - cmp.activate(); - }); - } - }); - // #enddocregion view - - app.HeroesQueriesModule = - ng.core.NgModule({ - imports: [ ng.platformBrowser.BrowserModule ], - declarations: [ - AppComponent, - HeroComponent, - ActiveLabelComponent - ], - bootstrap: [ AppComponent ] - }) - .Class({ - constructor: function() {} - }); - -})(window.app = window.app || {}); diff --git a/public/docs/_examples/cb-ts-to-js/js/app/main.js b/public/docs/_examples/cb-ts-to-js/js/app/main.js index 015a8d2126..fd3e737c9e 100644 --- a/public/docs/_examples/cb-ts-to-js/js/app/main.js +++ b/public/docs/_examples/cb-ts-to-js/js/app/main.js @@ -1,32 +1,9 @@ -// #docplaster -// #docregion appimport (function(app) { - // #enddocregion appimport - // #docregion ng2import - var platformBrowserDynamic = ng.platformBrowserDynamic.platformBrowserDynamic; - var LocationStrategy = ng.common.LocationStrategy; - var HashLocationStrategy = ng.common.HashLocationStrategy; - // #enddocregion ng2import +var platformBrowserDynamic = ng.platformBrowserDynamic.platformBrowserDynamic; - // #docregion appimport - var HeroComponent = app.HeroComponent; - // #enddocregion appimport +document.addEventListener('DOMContentLoaded', function() { + platformBrowserDynamic().bootstrapModule(app.AppModule); +}); - document.addEventListener('DOMContentLoaded', function() { - platformBrowserDynamic().bootstrapModule(app.HeroesModule); - platformBrowserDynamic().bootstrapModule(app.HeroesDslModule); - platformBrowserDynamic().bootstrapModule(app.HeroesLifecycleModule); - platformBrowserDynamic().bootstrapModule(app.HeroesDIModule); - platformBrowserDynamic().bootstrapModule(app.HeroDIInlineModule); - platformBrowserDynamic().bootstrapModule(app.HeroesDIInjectModule); - platformBrowserDynamic().bootstrapModule(app.HeroesDIInjectModule2); - platformBrowserDynamic().bootstrapModule(app.HeroesDIInjectAdditionalModule); - platformBrowserDynamic().bootstrapModule(app.HeroesIOModule); - platformBrowserDynamic().bootstrapModule(app.HeroesHostBindingsModule); - platformBrowserDynamic().bootstrapModule(app.HeroesQueriesModule); - }); - - // #docregion appimport })(window.app = window.app || {}); -// #enddocregion appimport diff --git a/public/docs/_examples/cb-ts-to-js/js/index.html b/public/docs/_examples/cb-ts-to-js/js/index.html index 3c2dd6eeca..7d2105bf78 100644 --- a/public/docs/_examples/cb-ts-to-js/js/index.html +++ b/public/docs/_examples/cb-ts-to-js/js/index.html @@ -4,12 +4,14 @@ + TypeScript to JavaScript + @@ -17,50 +19,26 @@ + + + - - - - - + + + + + + - -

TypeScript to JavaScript

- Classes and Class Metadata
- Input and Output Metadata
- Dependency Injection
- Host and Query Metadata
- -
-

Classes and Class Metadata

- Loading hero-view... - Loading hero-view2... - Loading hero-lifecycle... - -
-

Input and Output Metadata

- Loading hero-io... - -
-

Dependency Injection

- Loading hero-di... - Loading hero-di-inline... - Loading hero-di-inline2... - Loading hero-di-inject... - Loading hero-di-inject-additional... - -
-

Host and Query Metadata

- Loading heroes-bindings... - Loading heroes-queries... + Loading... diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/app.component.html b/public/docs/_examples/cb-ts-to-js/ts/app/app.component.html new file mode 100644 index 0000000000..7f1efd31a0 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/app.component.html @@ -0,0 +1,31 @@ + +

{{title}}

+Classes and Class Metadata
+Input and Output Decorators
+Dependency Injection
+Host Metadata
+View and Child Metadata
+ +
+

Classes and Class Metadata

+ + + +
+

Input and Output Metadata

+ + +
+

Dependency Injection

+ + + + +
+

Host Metadata

+ + + +
+

View and Child Metadata

+ diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/app.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/app.component.ts new file mode 100644 index 0000000000..722d812f63 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/app.component.ts @@ -0,0 +1,15 @@ +import { Component } from '@angular/core'; + +@Component({ + moduleId: module.id, + selector: 'my-app', + templateUrl: 'app.component.html', + styles: [ + // See hero-di-inject-additional.component + 'hero-host, hero-host-meta { border: 1px dashed black; display: block; padding: 4px;}', + '.heading {font-style: italic}' + ] +}) +export class AppComponent { + title = 'TypeScript'; +} diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/app.module.ts b/public/docs/_examples/cb-ts-to-js/ts/app/app.module.ts new file mode 100644 index 0000000000..8aafe9c9bb --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/app.module.ts @@ -0,0 +1,56 @@ +/* tslint:disable-next-line:no-unused-variable */ +import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core'; +import { BrowserModule } from '@angular/platform-browser'; + +import { AppComponent } from './app.component'; +import { ConfirmComponent } from './confirm.component'; +// #docregion appimport +import { HeroComponent } from './hero.component'; +// #enddocregion appimport +import { HeroComponent as HeroDIComponent } from './hero-di.component'; +import { HeroComponent as HeroDIInjectComponent } from './hero-di-inject.component'; +import { HeroComponent as HeroDIInjectAdditionalComponent } from './hero-di-inject-additional.component'; +import { HeroHostComponent } from './hero-host.component'; +import { HeroHostMetaComponent } from './hero-host-meta.component'; +import { HeroIOComponent } from './hero-io.component'; +import { HeroComponent as HeroLifecycleComponent } from './hero-lifecycle.component'; +import { HeroQueriesComponent, ViewChildComponent, ContentChildComponent } from './hero-queries.component'; +import { HeroTitleComponent } from './hero-title.component'; + +import { DataService } from './data.service'; + +@NgModule({ + imports: [ + BrowserModule + ], + declarations: [ + AppComponent, + ConfirmComponent, + HeroComponent, + HeroDIComponent, + HeroDIInjectComponent, + HeroDIInjectAdditionalComponent, + HeroHostComponent, HeroHostMetaComponent, + HeroIOComponent, + HeroLifecycleComponent, + HeroQueriesComponent, ViewChildComponent, ContentChildComponent, + HeroTitleComponent + ], + providers: [ + DataService, + { provide: 'heroName', useValue: 'Windstorm' } + ], + bootstrap: [ AppComponent ], + + // schemas: [ NO_ERRORS_SCHEMA ] // helpful for debugging! +}) +export class AppModule { } + +/* tslint:disable no-unused-variable */ +// #docregion ng2import +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; +import { + LocationStrategy, + HashLocationStrategy +} from '@angular/common'; +// #enddocregion ng2import diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/confirm.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/confirm.component.ts new file mode 100644 index 0000000000..08a0ed6c60 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/confirm.component.ts @@ -0,0 +1,22 @@ +import { Component, EventEmitter, Input, Output } from '@angular/core'; + +// #docregion +@Component({ + moduleId: module.id, + selector: 'app-confirm', + templateUrl: 'confirm.component.html' +}) +export class ConfirmComponent { + @Input() okMsg = ''; + @Input('cancelMsg') notOkMsg = ''; + @Output() ok = new EventEmitter(); + @Output('cancel') notOk = new EventEmitter(); + + onOkClick() { + this.ok.emit(true); + } + onNotOkClick() { + this.notOk.emit(true); + } +} +// #enddocregion diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/data.service.ts b/public/docs/_examples/cb-ts-to-js/ts/app/data.service.ts index 7e9c7456c6..cd7f9e1aae 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/data.service.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/data.service.ts @@ -2,8 +2,8 @@ import { Injectable } from '@angular/core'; @Injectable() export class DataService { - constructor() { - } + constructor() { } + getHeroName() { return 'Windstorm'; } diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts index 37e00960c9..ec460a9dbc 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts @@ -1,46 +1,7 @@ -import { - Attribute, - Component, - Inject, - Optional, - NgModule -} from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - -// #docregion -// #docregion metadata -@Component({ - moduleId: module.id, - selector: 'hero-title', - templateUrl: 'title.component.html' -}) -// #enddocregion metadata -class TitleComponent { - private msg: string = ''; - constructor( - @Inject('titlePrefix') @Optional() private titlePrefix: string, - @Attribute('title') private title: string - ) { } - - ok() { - this.msg = 'OK!'; - } -} -// #enddocregion +import { Component } from '@angular/core'; @Component({ selector: 'hero-di-inject-additional', - template: ` - ` -}) -class AppComponent { } - -@NgModule({ - imports: [ BrowserModule ], - declarations: [ - AppComponent, - TitleComponent - ], - bootstrap: [ AppComponent ] + template: `` }) -export class HeroesDIInjectAdditionalModule { } +export class HeroComponent { } diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject.component.ts index eb2cb78971..c583a1b0f6 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di-inject.component.ts @@ -1,20 +1,11 @@ -import { Component, Inject, NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; +import { Component, Inject } from '@angular/core'; // #docregion @Component({ selector: 'hero-di-inject', template: `

Hero: {{name}}

` }) -class HeroComponent { +export class HeroComponent { constructor(@Inject('heroName') private name: string) { } } // #enddocregion - -@NgModule({ - imports: [ BrowserModule ], - providers: [ { provide: 'heroName', useValue: 'Windstorm' } ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] -}) -export class HeroesDIInjectModule { } diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di.component.ts index c1bb927219..0cc78d277e 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-di.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-di.component.ts @@ -1,6 +1,4 @@ -import { Component, NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - +import { Component } from '@angular/core'; import { DataService } from './data.service'; // #docregion @@ -8,19 +6,10 @@ import { DataService } from './data.service'; selector: 'hero-di', template: `

Hero: {{name}}

` }) - -class HeroComponent { - name: string; +export class HeroComponent { + name = ''; constructor(dataService: DataService) { this.name = dataService.getHeroName(); } } // #enddocregion - -@NgModule({ - imports: [ BrowserModule ], - providers: [ DataService ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] -}) -export class HeroesDIModule { } diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-host-meta.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-host-meta.component.ts new file mode 100644 index 0000000000..fefe4a5470 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-host-meta.component.ts @@ -0,0 +1,44 @@ +import { Component } from '@angular/core'; + +// #docregion +@Component({ + selector: 'hero-host-meta', + template: ` +

Hero Host in Metadata

+
Heading clicks: {{clicks}}
+ `, + host: { + // HostBindings to the element + '[title]': 'title', + '[class.heading]': 'headingClass', + + // HostListeners on the entire element + '(click)': 'clicked()', + '(mouseenter)': 'enter($event)', + '(mouseleave)': 'leave($event)' + }, + // Styles within (but excluding) the element + styles: ['.active {background-color: coral;}'] +}) +export class HeroHostMetaComponent { + title = 'Hero Host in Metadata Tooltip'; + headingClass = true; + + active = false; + clicks = 0; + + clicked() { + this.clicks += 1; + } + + enter(event: Event) { + this.active = true; + this.headingClass = false; + } + + leave(event: Event) { + this.active = false; + this.headingClass = true; + } +} +// #enddocregion diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-host.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-host.component.ts new file mode 100644 index 0000000000..e8d72233c8 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-host.component.ts @@ -0,0 +1,39 @@ +import { Component, HostBinding, HostListener } from '@angular/core'; + +// #docregion +@Component({ + selector: 'hero-host', + template: ` +

Hero Host in Decorators

+
Heading clicks: {{clicks}}
+ `, + // Styles within (but excluding) the element + styles: ['.active {background-color: yellow;}'] +}) +export class HeroHostComponent { + // HostBindings to the element + @HostBinding() title = 'Hero Host in Decorators Tooltip'; + @HostBinding('class.heading') headingClass = true; + + active = false; + clicks = 0; + + // HostListeners on the entire element + @HostListener('click') + clicked() { + this.clicks += 1; + } + + @HostListener('mouseenter', ['$event']) + enter(event: Event) { + this.active = true; + this.headingClass = false; + } + + @HostListener('mouseleave', ['$event']) + leave(event: Event) { + this.active = false; + this.headingClass = true; + } +} +// #enddocregion diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-io.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-io.component.ts index 8105d98919..4b36411e78 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-io.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-io.component.ts @@ -1,67 +1,26 @@ -import { - Component, - EventEmitter, - Input, - Output, - NgModule -} from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - -// #docregion -@Component({ - moduleId: module.id, - selector: 'my-confirm', - templateUrl: 'confirm.component.html' -}) -class ConfirmComponent { - @Input() okMsg: string; - @Input('cancelMsg') notOkMsg: string; - @Output() ok = - new EventEmitter(); - @Output('cancel') notOk = - new EventEmitter(); - - onOkClick() { - this.ok.next(true); - } - onNotOkClick() { - this.notOk.next(true); - } -} -// #enddocregion - +import { Component } from '@angular/core'; @Component({ selector: 'hero-io', template: ` - - + + OK clicked Cancel clicked ` }) -class AppComponent { - okClicked: boolean; - cancelClicked: boolean; +export class HeroIOComponent { + okClicked = false; + cancelClicked = false; onOk() { this.okClicked = true; } + onCancel() { this.cancelClicked = true; } } - - -@NgModule({ - imports: [ BrowserModule ], - declarations: [ - AppComponent, - ConfirmComponent - ], - bootstrap: [ AppComponent ] -}) -export class HeroesIOModule { } diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-lifecycle.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-lifecycle.component.ts index 64bdc9e927..2629c85a1a 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero-lifecycle.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-lifecycle.component.ts @@ -1,27 +1,14 @@ -// #docplaster // #docregion import { Component, OnInit } from '@angular/core'; -// #enddocregion -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; @Component({ selector: 'hero-lifecycle', template: `

Hero: {{name}}

` }) -// #docregion -class HeroComponent implements OnInit { +export class HeroComponent implements OnInit { name: string; ngOnInit() { - this.name = 'Windstorm'; + // todo: fetch from server async + setTimeout(() => this.name = 'Windstorm', 0); } } -// #enddocregion - -@NgModule({ - imports: [ BrowserModule ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] -}) -export class HeroesLifecycleModule { } - diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-queries.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-queries.component.ts new file mode 100644 index 0000000000..8b2d91ea85 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-queries.component.ts @@ -0,0 +1,83 @@ +import { + Component, + ContentChild, + Input, + QueryList, + ViewChildren +} from '@angular/core'; + +@Component({ + selector: 'content-child', + template: ` + + Active + ` +}) +export class ContentChildComponent { + active = false; + + activate() { + this.active = !this.active; + } +} + +//////////////////// + +// #docregion content +@Component({ + selector: 'view-child', + template: ` +

+ {{hero.name}} + +

`, + styles: ['.active {font-weight: bold; background-color: skyblue;}'] +}) +export class ViewChildComponent { + @Input() hero: any; + active = false; + + @ContentChild(ContentChildComponent) content: ContentChildComponent; + + activate() { + this.active = !this.active; + this.content.activate(); + } +} +// #enddocregion content + +//////////////////// + +// #docregion view +@Component({ + selector: 'hero-queries', + template: ` + + + + + ` +}) +export class HeroQueriesComponent { + active = false; + heroData = [ + {id: 1, name: 'Windstorm'}, + {id: 2, name: 'LaughingGas'} + ]; + + @ViewChildren(ViewChildComponent) views: QueryList; + + activate() { + this.active = !this.active; + this.views.forEach( + view => view.activate() + ); + } + + // #docregion defined-property + get buttonLabel() { + return this.active ? 'Deactivate' : 'Activate'; + } + // #enddocregion defined-property +} +// #enddocregion view diff --git a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/title.component.html b/public/docs/_examples/cb-ts-to-js/ts/app/hero-title.component.html similarity index 78% rename from public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/title.component.html rename to public/docs/_examples/cb-ts-to-js/ts/app/hero-title.component.html index 06d6f3db91..0e93122d5c 100644 --- a/public/docs/_examples/cb-ts-to-js/js-es6-decorators/app/title.component.html +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-title.component.html @@ -1,3 +1,4 @@ +

{{titlePrefix}} {{title}}

{{ msg }}

diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero-title.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero-title.component.ts new file mode 100644 index 0000000000..074f2edd61 --- /dev/null +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero-title.component.ts @@ -0,0 +1,22 @@ +import { Attribute, Component, Inject, Optional } from '@angular/core'; + +// #docregion +// #docregion templateUrl +@Component({ + moduleId: module.id, + selector: 'hero-title', + templateUrl: 'hero-title.component.html' +}) +// #enddocregion templateUrl +export class HeroTitleComponent { + msg: string = ''; + constructor( + @Inject('titlePrefix') @Optional() private titlePrefix: string, + @Attribute('title') private title: string + ) { } + + ok() { + this.msg = 'OK!'; + } +} +// #enddocregion diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/hero.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/hero.component.ts index 83dc6d409d..4ea4c11611 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/hero.component.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/hero.component.ts @@ -1,4 +1,3 @@ -// #docplaster // #docregion metadata import { Component } from '@angular/core'; @@ -6,24 +5,10 @@ import { Component } from '@angular/core'; selector: 'hero-view', template: '

{{title}}: {{getName()}}

' }) -// #docregion appexport -// #docregion class +// #docregion appexport, class export class HeroComponent { title = 'Hero Detail'; getName() {return 'Windstorm'; } } -// #enddocregion class -// #enddocregion appexport +// #enddocregion appexport, class // #enddocregion metadata - -import { NgModule } from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - -@NgModule({ - imports: [ BrowserModule ], - declarations: [ HeroComponent ], - bootstrap: [ HeroComponent ] -}) -export class HeroesModule { } - - diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/heroes-bindings.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/heroes-bindings.component.ts deleted file mode 100644 index 0ce026a42f..0000000000 --- a/public/docs/_examples/cb-ts-to-js/ts/app/heroes-bindings.component.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { - Component, - HostBinding, - HostListener, - NgModule -} from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - -// #docregion -@Component({ - selector: 'heroes-bindings', - template: ` -

- Tour of Heroes -

- ` -}) -class HeroesComponent { - @HostBinding() title = 'Tooltip content'; - @HostBinding('class.heading') hClass = true; - active: boolean; - - constructor() {} - - @HostListener('click') - clicked() { - this.active = !this.active; - } - - @HostListener('dblclick', ['$event']) - doubleClicked(evt: Event) { - this.active = true; - } -} -// #enddocregion - -@NgModule({ - imports: [ BrowserModule ], - declarations: [ HeroesComponent ], - bootstrap: [ HeroesComponent ] -}) -export class HeroesHostBindingsModule { } diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/heroes-queries.component.ts b/public/docs/_examples/cb-ts-to-js/ts/app/heroes-queries.component.ts deleted file mode 100644 index 97003766e6..0000000000 --- a/public/docs/_examples/cb-ts-to-js/ts/app/heroes-queries.component.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { - Component, - ViewChildren, - ContentChild, - QueryList, - Input, - NgModule -} from '@angular/core'; -import { BrowserModule } from '@angular/platform-browser'; - -@Component({ - selector: 'active-label', - template: ` - Active - ` -}) -class ActiveLabelComponent { - active: boolean; - - activate() { - this.active = true; - } -} - -// #docregion content -@Component({ - selector: 'a-hero', - template: `

- {{hero.name}} - -

` -}) -class HeroComponent { - @Input() hero: any; - active: boolean; - - @ContentChild(ActiveLabelComponent) - label: ActiveLabelComponent; - - activate() { - this.active = true; - this.label.activate(); - } -} -// #enddocregion content - - -// #docregion view -@Component({ - selector: 'heroes-queries', - template: ` - - - - - ` -}) -class HeroesQueriesComponent { - heroData = [ - {id: 1, name: 'Windstorm'}, - {id: 2, name: 'Superman'} - ]; - - @ViewChildren(HeroComponent) - heroCmps: QueryList; - - activate() { - this.heroCmps.forEach( - (cmp) => cmp.activate() - ); - } -} -// #enddocregion view - -@NgModule({ - imports: [ BrowserModule ], - declarations: [ - HeroesQueriesComponent, - HeroComponent, - ActiveLabelComponent - ], - bootstrap: [ HeroesQueriesComponent ] -}) -export class HeroesQueriesModule { } diff --git a/public/docs/_examples/cb-ts-to-js/ts/app/main.ts b/public/docs/_examples/cb-ts-to-js/ts/app/main.ts index 92f5af5e1a..7cc80b5964 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/app/main.ts +++ b/public/docs/_examples/cb-ts-to-js/ts/app/main.ts @@ -1,30 +1,4 @@ -/* tslint:disable no-unused-variable */ -// #docregion ng2import -import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; -import { - LocationStrategy, - HashLocationStrategy -} from '@angular/common'; -// #enddocregion ng2import - -// #docregion appimport -import { HeroComponent } from './hero.component'; -// #enddocregion appimport - -import { HeroesModule } from './hero.component'; -import { HeroesLifecycleModule } from './hero-lifecycle.component'; -import { HeroesDIModule } from './hero-di.component'; -import { HeroesDIInjectModule } from './hero-di-inject.component'; -import { HeroesDIInjectAdditionalModule } from './hero-di-inject-additional.component'; -import { HeroesIOModule } from './hero-io.component'; -import { HeroesHostBindingsModule } from './heroes-bindings.component'; -import { HeroesQueriesModule } from './heroes-queries.component'; - -platformBrowserDynamic().bootstrapModule(HeroesModule); -platformBrowserDynamic().bootstrapModule(HeroesLifecycleModule); -platformBrowserDynamic().bootstrapModule(HeroesDIModule); -platformBrowserDynamic().bootstrapModule(HeroesDIInjectModule); -platformBrowserDynamic().bootstrapModule(HeroesDIInjectAdditionalModule); -platformBrowserDynamic().bootstrapModule(HeroesIOModule); -platformBrowserDynamic().bootstrapModule(HeroesHostBindingsModule); -platformBrowserDynamic().bootstrapModule(HeroesQueriesModule); +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; +import { AppModule } from './app.module'; + +platformBrowserDynamic().bootstrapModule(AppModule); diff --git a/public/docs/_examples/cb-ts-to-js/ts/index.html b/public/docs/_examples/cb-ts-to-js/ts/index.html index c635706f38..5791134d4f 100644 --- a/public/docs/_examples/cb-ts-to-js/ts/index.html +++ b/public/docs/_examples/cb-ts-to-js/ts/index.html @@ -5,6 +5,7 @@ + TypeScript to JavaScript @@ -20,32 +21,7 @@ - -

TypeScript to JavaScript

- Classes and Class Metadata
- Input and Output Metadata
- Dependency Injection
- Host and Query Metadata
- -
-

Classes and Class Metadata

- Loading hero-view... - Loading hero-lifecycle... - -
-

Input and Output Metadata

- Loading hero-io... - -
-

Dependency Injection

- Loading hero-di... - Loading hero-di-inject... - Loading hero-di-inject-additional... - -
-

Host and Query Metadata

- Loading heroes-bindings... - Loading heroes-queries... + Loading... diff --git a/public/docs/ts/latest/cookbook/ts-to-js.jade b/public/docs/ts/latest/cookbook/ts-to-js.jade index 0c3ba61544..d34dbf8868 100644 --- a/public/docs/ts/latest/cookbook/ts-to-js.jade +++ b/public/docs/ts/latest/cookbook/ts-to-js.jade @@ -15,19 +15,16 @@ a#toc :marked ## Table of contents - [_TypeScript_ to _ES6_ to _ES5_](#from-ts) - - [Modularity: imports and exports](#modularity) - - [Classes and Class Metadata](#class-metadata) - - [Input and Output Metadata](#property-metadata) - - [Dependency Injection](#dependency-injection) - - [Host and Query Metadata](#host-query-metadata) - - [AoT compilation in _TypeScript_ Only](#aot) + [_TypeScript_ to _ES6_ to _ES5_](#from-ts)
+ [Modularity: imports and exports](#modularity)
+ [Classes and Class Metadata](#class-metadata)
+ [_ES5_ DSL](#dsl)
+ [Interfaces](#interfaces)
+ [Input and Output Metadata](#io-decorators)
+ [Dependency Injection](#dependency-injection)
+ [Host Binding](#host-binding)
+ [View and Child Decorators](#view-child-decorators)
+ [AoT compilation in _TypeScript_ Only](#aot)
**Run and compare the live _TypeScript_ and JavaScript code shown in this cookbook.** @@ -38,7 +35,7 @@ a#from-ts ## _TypeScript_ to _ES6_ to _ES5_ _TypeScript_ - is a typed superset of _ES6 JavaScript_. + is a typed superset of _ES6 JavaScript_.   _ES6 JavaScript_ is a superset of _ES5 JavaScript_.   _ES5_ is the kind of JavaScript that runs natively in all modern browsers. The transformation of _TypeScript_ code all the way down to _ES5_ code can be seen as "shedding" features. @@ -48,17 +45,17 @@ a#from-ts * _ES6-without-decorators_ to _ES5_ When translating from _TypeScript_ to _ES6-with-decorators_, remove - [class property access modifiers](http://www._TypeScript_lang.org/docs/handbook/classes.html#public-private-and-protected-modifiers) + [class property access modifiers](http://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers) such as `public` and `private`. Remove most of the - [type annotations](https://www._TypeScript_lang.org/docs/handbook/basic-types.html), - such as `string` and `boolean` - but **keep type annotations used for dependency injection**. + [type declarations](https://www.typescriptlang.org/docs/handbook/basic-types.html), + such as `:string` and `:boolean` + but **keep the constructor parameter types which are used for dependency injection**. From _ES6-with-decorators_ to _plain ES6_, remove all - [decorators](https://www._TypeScript_lang.org/docs/handbook/decorators.html) - and the remaining type annotations. + [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) + and the remaining types. You must declare properties in the class constructor (`this.title = '...'`) rather than in the body of the class. Finally, from _plain ES6_ to _ES5_, the main missing features are `import` @@ -82,24 +79,20 @@ a#modularity In _ES5_, you access the Angular entities of the [the Angular packages](../glossary.html#!#scoped-package) through the global `ng` object. - Everything you would have imported from `@angular` is a nested member of this `ng` object: + Anything you can import from `@angular` is a nested member of this `ng` object: +makeTabs(` - cb-ts-to-js/ts/app/main.ts, - cb-ts-to-js/js-es6-decorators/app/main.es6, - cb-ts-to-js/js-es6/app/main.es6, - cb-ts-to-js/js/app/main.js - `,` - ng2import, - ng2import, - ng2import, - ng2import - `,` - TypeScript, + cb-ts-to-js/ts/app/app.module.ts, + cb-ts-to-js/js-es6-decorators/app/app.module.es6, + cb-ts-to-js/js-es6/app/app.module.es6, + cb-ts-to-js/js/app/app.module.js + `, + 'ng2import,ng2import,ng2import,ng2import', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, ES5 JavaScript - `) + `)(format='.') :marked ### Exporting Application Code @@ -118,9 +111,9 @@ a#modularity Add one application namespace object such as `app` to the global `document`. Then each code file "exports" public entities by attaching them to that namespace object, e.g., `app.HeroComponent`. You could factor a large application into several sub-namespaces - which leads to "exports" along the lines of `app.heroes.HeroComponent`. + which leads to "exports" along the lines of `app.heroQueries.HeroComponent`. - Every file should wrap _ES5_ code in an + Every _ES5_ file should wrap code in an [Immediately Invoked Function Expression (IIFE)](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression) to limit unintentional leaking of private symbols into the global scope. @@ -131,13 +124,9 @@ a#modularity cb-ts-to-js/js-es6-decorators/app/hero.component.es6, cb-ts-to-js/js-es6/app/hero.component.es6, cb-ts-to-js/js/app/hero.component.js - `,` - appexport, - appexport, - appexport, - appexport - `,` - TypeScript, + `, + 'appexport,appexport,appexport,appexport', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, ES5 JavaScript @@ -151,17 +140,13 @@ a#modularity In _ES5_ you use the shared namespace object to access "exported" entities from other files. +makeTabs(` - cb-ts-to-js/ts/app/main.ts, - cb-ts-to-js/js-es6-decorators/app/main.es6, - cb-ts-to-js/js-es6/app/main.es6, - cb-ts-to-js/js/app/main.js - `,` - appimport, - appimport, - appimport, - appimport - `,` - TypeScript, + cb-ts-to-js/ts/app/app.module.ts, + cb-ts-to-js/js-es6-decorators/app/app.module.es6, + cb-ts-to-js/js-es6/app/app.module.es6, + cb-ts-to-js/js/app/app.module.js + `, + 'appimport,appimport,appimport,appimport', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, ES5 JavaScript @@ -183,6 +168,20 @@ a#class-metadata ### Classes Most Angular _TypeScript_ and _ES6_ code is written as classes. + + Properties and method parameters of _TypeScript_ classes may be marked with the access modifiers + `private`, `internal`, and `public`. + Remove these modifiers when translating to JavaScript. + + Most type declarations (e.g, `:string` and `:boolean`) should be removed when translating to JavaScript. + When translating to _ES6-with-decorators_, ***do not remove types from constructor parameters!*** + + Look for types in _TypeScript_ property declarations. + In general it is better to initialize such properties with default values because + many browser JavaScript engines can generate more performant code. + When _TypeScript_ code follows this same advice, it can infer the property types + and there is nothing to remove during translation. + In _ES6-without-decorators_, properties of classes must be assigned inside the constructor. _ES5_ JavaScript has no classes. @@ -193,13 +192,9 @@ a#class-metadata cb-ts-to-js/js-es6-decorators/app/hero.component.es6, cb-ts-to-js/js-es6/app/hero.component.es6, cb-ts-to-js/js/app/hero.component.js - `,` - class, - class, - class, - constructorproto - `,` - TypeScript, + `, + 'class,class,class,constructorproto', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, ES5 JavaScript @@ -226,13 +221,9 @@ a#class-metadata cb-ts-to-js/js-es6-decorators/app/hero.component.es6, cb-ts-to-js/js-es6/app/hero.component.es6, cb-ts-to-js/js/app/hero.component.js - `,` - metadata, - metadata, - metadata, - metadata - `,` - TypeScript, + `, + 'metadata,metadata,metadata,metadata', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, ES5 JavaScript @@ -242,62 +233,95 @@ a#class-metadata ***External Template file*** A large component template is often kept in a separate template file. -+makeExample('cb-ts-to-js/ts/app/title.component.html', '', 'app/title.component.html')(format='.') ++makeExample('cb-ts-to-js/ts/app/hero-title.component.html', '', 'app/hero-title.component.html')(format='.') :marked - The component (`TitleComponent` in this case) then references the template file in its metadata `templateUrl` property: + The component (`HeroTitleComponent` in this case) then references the template file in its metadata `templateUrl` property: +makeTabs(` - cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts, - cb-ts-to-js/js-es6-decorators/app/hero-di-inject-additional.component.es6, - cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6, - cb-ts-to-js/js/app/hero-di-inject-additional.component.js`, - 'metadata, metadata, metadata, metadata', + cb-ts-to-js/ts/app/hero-title.component.ts, + cb-ts-to-js/js-es6-decorators/app/hero-title.component.es6, + cb-ts-to-js/js-es6/app/hero-title.component.es6, + cb-ts-to-js/js/app/hero-title.component.js`, + 'templateUrl, templateUrl, templateUrl, templateUrl', `TypeScript, - ES6 JavaScript with decorators, - ES6 JavaScript, - ES5 JavaScript`)(format='.') + ES6 JavaScript with decorators, + ES6 JavaScript, + ES5 JavaScript + `)(format='.') :marked - Note that the _TypeScript_ and both _ES6_ file `templateUrl` properties identify the location of the template file _relative to the component module_. + Note that the _TypeScript_ and both _ES6_ `templateUrl` properties identify the location of the template file _relative to the component module_. All three metadata configurations specify the `moduleId` property so that Angular can calculate the proper module address. -.l-sub-section - :marked - The `moduleId` may not be needed with certain tooling but it's safest to provide it anyway. -:marked + The _ES5_ approach shown here does not support modules and therefore there is no way to calculate a _module-relative URL_. The `templateUrl` for the _ES5_ code must specify the _path from the project root_ and omits the irrelevant `moduleId` property. +.l-sub-section + :marked + With the right tooling, the `moduleId` may not be needed in the other JavaScript dialects either. + But it's safest to provide it anyway. - ***Angular class API*** +a#dsl +.l-main-section +:marked + ## _ES5_ DSL This _ES5_ pattern of creating a constructor and annotating it with metadata is so common that Angular provides a convenience API to make it a little more compact and locates the metadata above the constructor, as you would if you wrote in _TypeScript_ or _ES6-with-decorators_. - Set a component variable to the result of an `ng.core.Component` function call. + This _API_ (_Application Programming Interface_) is commonly known as the _ES5 DSL_ (_Domain Specific Language_). + + Set an application namespace property (e.g., `app.HeroDslComponent`) to the result of an `ng.core.Component` function call. Pass the same metadata object to `ng.core.Component` as you did before. - Then chain a call to the `Class` method which takes an object defining the class constructor and instance methods. - Here is an example of the component class API next to the annotated function version for comparison: + Here is an example of the `HeroComponent`, re-written with the DSL, + next to the original _ES5_ version for comparison: +makeTabs(` - cb-ts-to-js/js/app/hero-dsl.component.js, + cb-ts-to-js/js/app/hero.component.js, cb-ts-to-js/js/app/hero.component.js - `,` - component, - metadata - `,` - ES5 JavaScript with Class API, + `, + 'dsl,', + ` + ES5 JavaScript with DSL, ES5 JavaScript `) + +.callout.is-helpful + header Name the constructor + :marked + A **named** constructor displays clearly in the console log + if the component throws a runtime error. + An **unnamed** constructor displays as an anonymous function (e.g., `class0`) + which is impossible to find in the source code. + +:marked + ### Properties with getters and setters + + _TypeScript_ and _ES6_ support with getters and setters. + Here's an example of a read-only _TypeScript_ property with a getter + that prepares a toggle-button label for the next clicked state: ++makeExample('cb-ts-to-js/ts/app/hero-queries.component.ts', 'defined-property', 'ts/app/hero-queries.component.ts')(format='.') + +:marked + This _TypeScript_ "getter" property is transpiled to an _ES5_ + defined property. + The _ES5 DSL_ does not support _defined properties_ directly + but you can still create them by extracting the "class" prototype and + adding the _defined property_ in raw JavaScript like this: ++makeExample('cb-ts-to-js/js/app/hero-queries.component.js', 'defined-property','js/app/hero-queries.component.ts')(format='.') + :marked - There are similar APIs for other decorated classes. + ### DSL for other classes + There are similar DSLs for other decorated classes. You can define a directive with `ng.core.Directive`: code-example. - var MyDirective = ng.core.Directive({ + app.MyDirective = ng.core.Directive({ selector: '[myDirective]' }).Class({ ... @@ -305,18 +329,20 @@ code-example. :marked and a pipe with `ng.core.Pipe`: code-example. - var MyPipe = ng.core.Pipe({ + app.MyPipe = ng.core.Pipe({ name: 'myPipe' }).Class({ ... }); +a#interfaces +.l-main-section :marked - ### Interfaces + ## Interfaces A _TypeScript_ interface helps ensure that a class implements the interface's members correctly. We strongly recommend Angular interfaces where appropriate. - For example, a component that implements the `ngOnInit` lifecycle hook method + For example, the component class that implements the `ngOnInit` lifecycle hook method should implement the `OnInit` interface. _TypeScript_ interfaces exist for developer convenience and are not used by Angular at runtime. @@ -327,16 +353,18 @@ code-example. cb-ts-to-js/ts/app/hero-lifecycle.component.ts, cb-ts-to-js/js-es6-decorators/app/hero-lifecycle.component.es6, cb-ts-to-js/js-es6/app/hero-lifecycle.component.es6, + cb-ts-to-js/js/app/hero-lifecycle.component.js, cb-ts-to-js/js/app/hero-lifecycle.component.js - `,` - `,` - TypeScript, + `, + ',,,,dsl', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, - ES5 JavaScript + ES5 JavaScript, + ES5 JavaScript with DSL `) -a#property-metadata +a#io-decorators .l-main-section :marked ## Input and Output Metadata @@ -357,16 +385,18 @@ a#property-metadata combined in the metadata `inputs` and `outputs` _arrays_. +makeTabs(` - cb-ts-to-js/ts/app/hero-io.component.ts, - cb-ts-to-js/js-es6-decorators/app/hero-io.component.es6, - cb-ts-to-js/js-es6/app/hero-io.component.es6, - cb-ts-to-js/js/app/hero-io.component.js - `,` - `,` - TypeScript, + cb-ts-to-js/ts/app/confirm.component.ts, + cb-ts-to-js/js-es6-decorators/app/confirm.component.es6, + cb-ts-to-js/js-es6/app/confirm.component.es6, + cb-ts-to-js/js/app/confirm.component.js, + cb-ts-to-js/js/app/confirm.component.js + `, + ',,,,dsl', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, - ES5 JavaScript + ES5 JavaScript, + ES5 JavaScript with DSL `) :marked In the previous example, one of the public-facing binding names (`cancelMsg`) @@ -376,53 +406,76 @@ a#property-metadata In _TypeScript_ and _ES6-with-decorators_, you specify the special binding name in the argument to the property decorator. + In _ES5_ and _plain ES6_ code, convey this pairing with the `propertyName: bindingName` syntax in the class metadata. .l-main-section :marked ## Dependency Injection + Angular relies heavily on [Dependency Injection](../guide/dependency-injection.html) to provide services to the objects it creates. + When Angular creates a new component, directive, pipe or another service, + it sets the class constructor parameters to instances of services provided by an _Injector_. - ### Injection by Type + The developer must tell Angular what to inject into each parameter. - Angular can often use _TypeScript_ type information to determine what needs to be injected. - _ES6-with-decorators_ can also make use of type information. + ### Injection by Class Type - Since no type information is available in _ES5_/_ES6_ JavaScript, you must identify "injectables" in - some other way. + The easiest and most popular technique in _TypeScript_ and _ES6-with-decorators_ is to set the constructor parameter type + to the class associated with the service to inject. + + The _TypeScript_ transpiler writes parameter type information into the generated JavaScript. + Angular reads that information at runtime and locates the corresponding service in the appropriate _Injector_.. + The _ES6-with-decorators_ transpiler does essentially the same thing using the same parameter-typing syntax. - Attach a `parameters` array to the constructor function. - Each array item is the dependency injection token that identifies the thing to be injected. - Often the token is the constructor function for the class-like dependency. + _ES5_ and _plain ES6_ lack types so you must identify "injectables" by attaching a **`parameters`** array to the constructor function. + Each item in the array specifies the service's injection token. + + As with _TypeScript_ the most popular token is a class, + or rather a _constructor function_ that represents a class in _ES5_ and _plain ES6_. + The format of the `parameters` array varies: - In _ES6_ the decorators need to be inside a nested array. - When writing in the _ES5_ class convenience API, you can supply the parameter tokens by wrapping - the constructor in an array. + * _plain ES6_ — nest each constructor function in a sub-array. + + * _ES5_ — simply list the constructor functions. + + When writing with _ES5 DSL_, set the `Class.constructor` property to + an array whose first parameters are the injectable constructor functions and whose + last parameter is the class constructor itself. + This format should be familiar to Angular 1 developers. +makeTabs(` cb-ts-to-js/ts/app/hero-di.component.ts, cb-ts-to-js/js-es6-decorators/app/hero-di.component.es6, cb-ts-to-js/js-es6/app/hero-di.component.es6, cb-ts-to-js/js/app/hero-di.component.js, - cb-ts-to-js/js/app/hero-di-inline.component.js - `,` - `,` - TypeScript, + cb-ts-to-js/js/app/hero-di.component.js + `, + ',,,,dsl', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, ES5 JavaScript, - ES5 JavaScript with Class API + ES5 JavaScript with DSL `) :marked ### Injection with the @Inject decorator - When the thing being injected doesn't correspond directly to a type, you use the - `@Inject()` decorator to supply the injection token. - In this example, you are injecting a string identified by the "heroName" token. + Sometimes the dependency injection token isn't a class or constructor function. + + In _TypeScript_ and _ES6-with-decorators_, you precede the class constructor parameter + by calling the `@Inject()` decorator with the injection token. + In the following example, the token is the string `'heroName'`. + + The other JavaScript dialogs add a `parameters` array to the class contructor function. + Each item constains a new instance of `Inject('heroName')`: + + * _plain ES6_ — each item is a new instance of `Inject(token)` in a sub-array. + + * _ES5_ — simply list the string tokens. - In _ES5_/_ES6_ JavaScript you add the token string to the injection parameters array. - Alternatively, when using the _ES5_ convenience class API you can create a token with the - `Inject` method and add that to the constructor array in the annotations. + When writing with _ES5 DSL_, set the `Class.constructor` property to a function definition + array as before. Create a `new ng.core.Inject(token)` for each parameter. +makeTabs(` cb-ts-to-js/ts/app/hero-di-inject.component.ts, @@ -430,67 +483,74 @@ a#property-metadata cb-ts-to-js/js-es6/app/hero-di-inject.component.es6, cb-ts-to-js/js/app/hero-di-inject.component.js, cb-ts-to-js/js/app/hero-di-inject.component.js - `,` - , - , - , - parameters, - ctor - `,` - TypeScript, + `, + ',,,,dsl', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, ES5 JavaScript, - ES5 JavaScript with Class API + ES5 JavaScript with DSL `) :marked ### Additional Injection Decorators - You can attach additional decorators to constructor parameters to qualify the injection behavior. - You can mark optional dependencies with the [`@Optional`](../api/core/index/Optional-decorator.html), - inject host element attributes with [`@Attribute`](../api/core/index/Attribute-interface.html), - inject content child queries with [`@ContentChild`](../api/core/index/ContentChild-decorator.html) - and inject view child queries with [`@ViewChild`](../api/core/index/ViewChild-decorator.html)). - - In ES6 JavaScript you just add the extra decorators to the nested injection parameters array. + You can qualify injection behavior with injection decorators from `@angular/core`. - To achieve the same effect in _ES5_ JavaScript, use a nested array with the constructor - array notation in which the injection information precedes the constructor function itself. + In _TypeScript_ and _ES6-with-decorators_, + you precede the constructor parameters with injection qualifiers such as: + * [`@Optional`](../api/core/index/Optional-decorator.html) sets the parameter to `null` if the service is missing + * [`@Attribute`](../api/core/index/Attribute-interface.html) to inject a host element attribute value + * [`@ContentChild`](../api/core/index/ContentChild-decorator.html) to inject a content child + * [`@ViewChild`](../api/core/index/ViewChild-decorator.html) to inject a view child + * [`@Host`](../api/core/index/Host-decorator.html) to inject a service in this component or its host + * [`@SkipSelf`](../api/core/index/SkipSelf-decorator.html) to inject a service provided in an ancestor of this component + + In _plain ES6_ and _ES5_, create an instance of the equivalent injection qualifier in a nested array within the `parameters` array. + For example, you'd write `new Optional()` in _plain ES6_ and `new ng.core.Optional()` in _ES5_. - You can apply other additional parameter decorators such as - [`@Host`](../api/core/index/Host-decorator.html) and - [`@SkipSelf`](../api/core/index/SkipSelf-decorator.html) in the same way - - by adding `new ng.core.Host()` or `ng.core.SkipSelf()` in the - parameters array. + + When writing with _ES5 DSL_, set the `Class.constructor` property to a function definition + array as before. Use a nested array to define a parameter's complete injection specification. + +makeTabs(` - cb-ts-to-js/ts/app/hero-di-inject-additional.component.ts, - cb-ts-to-js/js-es6-decorators/app/hero-di-inject-additional.component.es6, - cb-ts-to-js/js-es6/app/hero-di-inject-additional.component.es6, - cb-ts-to-js/js/app/hero-di-inject-additional.component.js - `,` - `,` - TypeScript, + cb-ts-to-js/ts/app/hero-title.component.ts, + cb-ts-to-js/js-es6-decorators/app/hero-title.component.es6, + cb-ts-to-js/js-es6/app/hero-title.component.es6, + cb-ts-to-js/js/app/hero-title.component.js, + cb-ts-to-js/js/app/hero-title.component.js + `, + ',,,,dsl', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, - ES5 JavaScript - `) + ES5 JavaScript, + ES5 JavaScript with DSL + `) +.l-sub-section + :marked + In the example above, there is no provider for the `'titlePrefix'` token. + Without `Optional`, Angular would raise an error. + With `Optional`, Angular sets the constructor parameter to `null` + and the component displays the title without a prefix. -a#host-query-metadata +a#host-binding .l-main-section :marked - ## Host and Query Metadata + ## Host Binding + Angular supports bindings to properties and events of the _host element_ which is the + element whose tag matches the component selector. ### Host Decorators - In _TypeScript_ and _ES6-with-decorators_ you can use host property decorators to bind a host + In _TypeScript_ and _ES6-with-decorators_, you can use host property decorators to bind a host element to a component or directive. The [`@HostBinding`](../api/core/index/HostBinding-interface.html) decorator binds host element properties to component data properties. The [`@HostListener`](../api/core/index/HostListener-interface.html) decorator binds host element events to component event handlers. - When using _ES5_/_ES6_, add a `host` attribute to the component metadata to achieve the + In _plain ES6_ or _ES5_, add a `host` attribute to the component metadata to achieve the same effect as `@HostBinding` and `@HostListener`. The `host` value is an object whose properties are host property and listener bindings: @@ -500,54 +560,74 @@ a#host-query-metadata * Each value identifies the corresponding component property or method. +makeTabs(` - cb-ts-to-js/ts/app/heroes-bindings.component.ts, - cb-ts-to-js/js-es6-decorators/app/heroes-bindings.component.es6, - cb-ts-to-js/js-es6/app/heroes-bindings.component.es6, - cb-ts-to-js/js/app/heroes-bindings.component.js - `,` - `,` - TypeScript, + cb-ts-to-js/ts/app/hero-host.component.ts, + cb-ts-to-js/js-es6-decorators/app/hero-host.component.es6, + cb-ts-to-js/js-es6/app/hero-host.component.es6, + cb-ts-to-js/js/app/hero-host.component.js, + cb-ts-to-js/js/app/hero-host.component.js + `, + ',,,,dsl', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, - ES5 JavaScript + ES5 JavaScript, + ES5 JavaScript with DSL + `) + +:marked + ### Host Metadata + Some developers prefer to specify host properties and listeners + in the component metadata. + They'd _rather_ do it the way you _must_ do it _ES5_ and _plain ES6_. + + The following re-implementation of the `HeroComponent` reminds us that _any property metadata decorator_ + can be expressed as component or directive metadata in both _TypeScript_ and _ES6-with-decorators_. + These particular _TypeScript_ and _ES6_ code snippets happen to be identical. ++makeTabs(` + cb-ts-to-js/ts/app/hero-host-meta.component.ts, + cb-ts-to-js/js-es6-decorators/app/hero-host-meta.component.es6 + `, + '', + ` TypeScript, + ES6 JavaScript with decorators `) -.alert.is-helpful - :marked - In _TypeScript_ and _ES6-with-decorators_ you can also use the `queries` metadata - instead of the `@ViewChild` and `@ContentChild` property decorators. +a#view-child-decorators +.l-main-section :marked - ### Query Decorators + ### View and Child Decorators - There are several property decorators for querying the descendants of - a component or directive. + Several _property_ decorators query a component's nested view and content components. +.l-sub-section + :marked + _View_ children are associated with element tags that appear _within_ the component's template. + + _Content_ children are associated with elements that appear _between_ the component's element tags; + they are projected into an `` slot in the component's template. +:marked The [`@ViewChild`](../api/core/index/ViewChild-decorator.html) and [`@ViewChildren`](../api/core/index/ViewChildren-decorator.html) property decorators allow a component to query instances of other components that are used in its view. - In _ES5_/_ES6_ JavaScript you access a component's view children by adding a `queries` attribute to - the component metadata. It should be an object where: - - * Each key is the name of a component property that will hold the view children - * Each value is an instance of either `ViewChild` or `ViewChildren`. + In _ES5_ and _ES6_, you access a component's view children by adding a `queries` property to the component metadata. + The `queries` property value is a hash map. + + * each _key_ is the name of a component property that will hold the view child or children. + * each _value_ is a new instance of either `ViewChild` or `ViewChildren`. +makeTabs(` - cb-ts-to-js/ts/app/heroes-queries.component.ts, - cb-ts-to-js/js-es6-decorators/app/heroes-queries.component.es6, - cb-ts-to-js/js-es6/app/heroes-queries.component.es6, - cb-ts-to-js/js/app/heroes-queries.component.js - `,` - view, - view, - view, - view - `,` - TypeScript, + cb-ts-to-js/ts/app/hero-queries.component.ts, + cb-ts-to-js/js-es6-decorators/app/hero-queries.component.es6, + cb-ts-to-js/js-es6/app/hero-queries.component.es6, + cb-ts-to-js/js/app/hero-queries.component.js + `, + 'view, view, view, view', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, - ES5 JavaScript + ES5 JavaScript with DSL `) :marked @@ -560,22 +640,23 @@ a#host-query-metadata [`@ViewChildren`](../api/core/index/ViewChildren-decorator.html). +makeTabs(` - cb-ts-to-js/ts/app/heroes-queries.component.ts, - cb-ts-to-js/js-es6-decorators/app/heroes-queries.component.es6, - cb-ts-to-js/js-es6/app/heroes-queries.component.es6, - cb-ts-to-js/js/app/heroes-queries.component.js - `,` - content, - content, - content, - content - `,` - TypeScript, + cb-ts-to-js/ts/app/hero-queries.component.ts, + cb-ts-to-js/js-es6-decorators/app/hero-queries.component.es6, + cb-ts-to-js/js-es6/app/hero-queries.component.es6, + cb-ts-to-js/js/app/hero-queries.component.js + `, + 'content, content, content, content', + ` TypeScript, ES6 JavaScript with decorators, ES6 JavaScript, - ES5 JavaScript + ES5 JavaScript with DSL `) +.alert.is-helpful + :marked + In _TypeScript_ and _ES6-with-decorators_ you can also use the `queries` metadata + instead of the `@ViewChild` and `@ContentChild` property decorators. + a#aot .l-main-section :marked