/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {CommonModule, NgForOf, NgIf} from '@angular/common'; import {ChangeDetectionStrategy, Component, EventEmitter, InjectFlags, Injectable, Input, IterableDiffers, NgModule, Output, createInjector, defineInjector, inject, ɵComponentDef as ComponentDef, ɵComponentType as ComponentType, ɵDirectiveDef as DirectiveDef, ɵDirectiveType as DirectiveType, ɵNgOnChangesFeature as NgOnChangesFeature, ɵdefaultIterableDiffers as defaultIterableDiffers, ɵdefineDirective as defineDirective, ɵinjectTemplateRef as injectTemplateRef, ɵinjectViewContainerRef as injectViewContainerRef, ɵmarkDirty as markDirty, ɵrenderComponent as renderComponent} from '@angular/core'; export class Todo { editing: boolean; private _title: string; get title() { return this._title; } set title(value: string) { this._title = value.trim(); } constructor(title: string, public completed: boolean = false) { this.editing = false; this.title = title; } } @Injectable() export class TodoStore { todos: Array = [ new Todo('Demonstrate Components'), new Todo('Demonstrate Structural Directives', true), new Todo('Demonstrate NgModules'), new Todo('Demonstrate zoneless change detection'), new Todo('Demonstrate internationalization'), ]; private getWithCompleted(completed: boolean) { return this.todos.filter((todo: Todo) => todo.completed === completed); } allCompleted() { return this.todos.length === this.getCompleted().length; } setAllTo(completed: boolean) { this.todos.forEach((t: Todo) => t.completed = completed); } removeCompleted() { this.todos = this.getWithCompleted(false); } getRemaining() { return this.getWithCompleted(false); } getCompleted() { return this.getWithCompleted(true); } toggleCompletion(todo: Todo) { todo.completed = !todo.completed; } remove(todo: Todo) { this.todos.splice(this.todos.indexOf(todo), 1); } add(title: string) { this.todos.push(new Todo(title)); } } @Component({ selector: 'todo-app', // TODO(misko) remove all `foo && foo.something` once `ViewContainerRef` can separate creation and // update block. // TODO(misko): make this work with `[(ngModel)]` template: `

todos

`, // TODO(misko): switch oven to OnPush // changeDetection: ChangeDetectionStrategy.OnPush }) export class ToDoAppComponent { todoStore: TodoStore; newTodoText = ''; // TODO(misko) Fix injection // constructor(todoStore: TodoStore) { this.todoStore = todoStore; } constructor() { this.todoStore = new TodoStore(); } stopEditing(todo: Todo, editedTitle: string) { todo.title = editedTitle; todo.editing = false; } cancelEditingTodo(todo: Todo) { todo.editing = false; } updateEditingTodo(todo: Todo, editedTitle: string) { editedTitle = editedTitle.trim(); todo.editing = false; if (editedTitle.length === 0) { return this.todoStore.remove(todo); } todo.title = editedTitle; } editTodo(todo: Todo) { todo.editing = true; } removeCompleted() { this.todoStore.removeCompleted(); } toggleCompletion(todo: Todo) { this.todoStore.toggleCompletion(todo); } remove(todo: Todo) { this.todoStore.remove(todo); } addTodo() { if (this.newTodoText.trim().length) { this.todoStore.add(this.newTodoText); this.newTodoText = ''; } } } // TODO(misko): This hack is here because common is not compiled with Ivy flag turned on. (CommonModule as any).ngInjectorDef = defineInjector({factory: () => new CommonModule}); // TODO(misko): This hack is here because common is not compiled with Ivy flag turned on. (NgForOf as any).ngDirectiveDef = defineDirective({ type: NgForOf, selectors: [['', 'ngFor', '', 'ngForOf', '']], factory: () => new NgForOf( injectViewContainerRef(), injectTemplateRef(), // TODO(misko): inject does not work since it needs to be directiveInject // inject(IterableDiffers, defaultIterableDiffers) defaultIterableDiffers), features: [NgOnChangesFeature({ ngForOf: 'ngForOf', ngForTrackBy: 'ngForTrackBy', ngForTemplate: 'ngForTemplate', })], inputs: { ngForOf: 'ngForOf', ngForTrackBy: 'ngForTrackBy', ngForTemplate: 'ngForTemplate', } }); // TODO(misko): This hack is here because common is not compiled with Ivy flag turned on. (NgIf as any).ngDirectiveDef = defineDirective({ type: NgIf, selectors: [['', 'ngIf', '']], factory: () => new NgIf(injectViewContainerRef(), injectTemplateRef()), inputs: {ngIf: 'ngIf', ngIfThen: 'ngIfThen', ngIfElse: 'ngIfElse'} }); @NgModule({declarations: [ToDoAppComponent, ToDoAppComponent], imports: [CommonModule]}) export class ToDoAppModule { } // TODO(misko): create cleaner way to publish component into global location for tests. (window as any).toDoAppComponent = renderComponent(ToDoAppComponent);