Files
owl/examples/todoapp/components/TodoApp.js
T

57 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-03-19 14:10:05 +01:00
import { TodoItem } from "./TodoItem.js";
const { StoreMixin, Component } = odoo.core;
2019-03-20 13:36:37 +01:00
const ENTER_KEY = 13;
2019-03-19 14:10:05 +01:00
export class TodoApp extends StoreMixin(Component) {
template = "todoapp";
widgets = { TodoItem };
state = { filter: "all" };
get todos() {
return this.env.store.state.todos;
}
2019-03-20 13:36:37 +01:00
get visibleTodos() {
let todos = this.todos;
if (this.state.filter === "active") {
todos = todos.filter(t => !t.completed);
}
if (this.state.filter === "completed") {
todos = todos.filter(t => t.completed);
}
return todos;
}
2019-03-19 14:10:05 +01:00
get allChecked() {
2019-03-20 13:36:37 +01:00
return this.todos.every(todo => todo.completed);
2019-03-19 14:10:05 +01:00
}
get remaining() {
2019-03-20 13:36:37 +01:00
return this.todos.filter(todo => !todo.completed).length;
2019-03-19 14:10:05 +01:00
}
get remainingText() {
return (this.remaining < 2 ? "item" : "items") + " left";
}
addTodo(ev) {
2019-03-20 13:36:37 +01:00
if (ev.keyCode === ENTER_KEY) {
const title = ev.target.value;
if (title.trim()) {
this.env.store.dispatch("addTodo", title);
2019-03-19 14:10:05 +01:00
}
ev.target.value = "";
}
}
clearCompleted() {
this.env.store.dispatch("clearCompleted");
}
toggleAll() {
this.env.store.dispatch("toggleAll", !this.allChecked);
}
}