-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtodomvc.js
100 lines (82 loc) · 1.95 KB
/
todomvc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { create, reduce, filter } from "microstates";
export const SHOW_ALL = "";
export const SHOW_COMPLETED = "show_completed";
export const SHOW_ACTIVE = "show_active";
export class Todo {
id = Number
text = String
completed = Boolean
}
export class EditableTodo extends Todo {
editing = Boolean
edit() {
return this.editing.set(true);
}
save() {
return this.editing.set(false);
}
}
export default class TodoMVC {
todos = [EditableTodo] // Contains array of todo items
newTodo = String
filter = String
get nextId() {
return reduce(this.todos, (acc, todo) => Math.max(todo.id.state, acc), 0) + 1;
}
get completed() {
return filter(this.todos, todo => todo.completed.state);
}
get active() {
return filter(this.todos, todo => !todo.completed.state);
}
get isAllComplete() {
return this.hasTodos && this.active.length === 0;
}
get hasTodos() {
return this.todos.length > 0;
}
get hasCompleted() {
return this.completed.length > 0;
}
get filters() {
let option = (key, label) => ({
key,
label,
selected: this.filter.state === key,
select: () => this.filter.set(key)
});
return [
option(SHOW_ALL, 'All'),
option(SHOW_ACTIVE, 'Active'),
option(SHOW_COMPLETED, 'Completed')
];
}
get filtered() {
switch (this.filter.state) {
case SHOW_COMPLETED: return this.completed;
case SHOW_ACTIVE: return this.active;
case SHOW_ALL:
default:
return this.todos;
}
}
insertNewTodo() {
if (this.newTodo.state === "") {
return this;
} else {
return this.todos
.push({
text: this.newTodo.state,
id: this.nextId,
completed: false
})
.newTodo.set("");
}
}
clearCompleted() {
return this.todos.filter(({ completed }) => !completed.state);
}
toggleAll() {
return this.todos.map(todo => todo.completed.set(true));
}
}