2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2016-08-16 11:15:01 -07:00
|
|
|
import {Component, NgModule} from '@angular/core';
|
|
|
|
import {BrowserModule} from '@angular/platform-browser';
|
|
|
|
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
2016-08-05 09:56:53 -07:00
|
|
|
|
2016-05-01 22:54:19 -07:00
|
|
|
import {Store, Todo, TodoFactory} from './app/TodoStore';
|
2015-05-22 14:39:00 +02:00
|
|
|
|
2016-08-16 11:15:01 -07:00
|
|
|
@Component({selector: 'todo-app', viewProviders: [Store, TodoFactory], templateUrl: 'todo.html'})
|
2019-02-01 14:27:13 +01:00
|
|
|
export class TodoApp {
|
2015-05-22 14:39:00 +02:00
|
|
|
todoEdit: Todo = null;
|
|
|
|
|
2016-02-19 11:49:31 -08:00
|
|
|
constructor(public todoStore: Store<Todo>, public factory: TodoFactory) {}
|
2015-05-22 14:39:00 +02:00
|
|
|
|
2016-10-23 16:21:18 +02:00
|
|
|
enterTodo(inputElement: HTMLInputElement): void {
|
2015-05-22 14:39:00 +02:00
|
|
|
this.addTodo(inputElement.value);
|
|
|
|
inputElement.value = '';
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:40:21 -07:00
|
|
|
editTodo(todo: Todo): void {
|
|
|
|
this.todoEdit = todo;
|
|
|
|
}
|
2015-05-22 14:39:00 +02:00
|
|
|
|
2016-10-23 16:21:18 +02:00
|
|
|
doneEditing($event: KeyboardEvent, todo: Todo): void {
|
|
|
|
const which = $event.which;
|
|
|
|
const target = $event.target as HTMLInputElement;
|
2015-05-22 14:39:00 +02:00
|
|
|
if (which === 13) {
|
|
|
|
todo.title = target.value;
|
|
|
|
this.todoEdit = null;
|
|
|
|
} else if (which === 27) {
|
|
|
|
this.todoEdit = null;
|
|
|
|
target.value = todo.title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 16:40:21 -07:00
|
|
|
addTodo(newTitle: string): void {
|
|
|
|
this.todoStore.add(this.factory.create(newTitle, false));
|
|
|
|
}
|
2015-05-22 14:39:00 +02:00
|
|
|
|
2020-04-13 16:40:21 -07:00
|
|
|
completeMe(todo: Todo): void {
|
|
|
|
todo.completed = !todo.completed;
|
|
|
|
}
|
2015-05-22 14:39:00 +02:00
|
|
|
|
2020-04-13 16:40:21 -07:00
|
|
|
deleteMe(todo: Todo): void {
|
|
|
|
this.todoStore.remove(todo);
|
|
|
|
}
|
2015-05-22 14:39:00 +02:00
|
|
|
|
2016-10-23 16:21:18 +02:00
|
|
|
toggleAll($event: MouseEvent): void {
|
|
|
|
const isComplete = ($event.target as HTMLInputElement).checked;
|
2020-04-13 16:40:21 -07:00
|
|
|
this.todoStore.list.forEach((todo: Todo) => {
|
|
|
|
todo.completed = isComplete;
|
|
|
|
});
|
2015-05-22 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2020-04-13 16:40:21 -07:00
|
|
|
clearCompleted(): void {
|
|
|
|
this.todoStore.removeBy((todo: Todo) => todo.completed);
|
|
|
|
}
|
2015-05-22 14:39:00 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 15:59:50 -07:00
|
|
|
@NgModule({declarations: [TodoApp], bootstrap: [TodoApp], imports: [BrowserModule]})
|
2019-02-01 14:27:13 +01:00
|
|
|
export class ExampleModule {
|
2016-08-16 11:15:01 -07:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:27:13 +01:00
|
|
|
platformBrowserDynamic().bootstrapModule(ExampleModule);
|