This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathma2.js
86 lines (78 loc) · 1.59 KB
/
ma2.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
/*globals m, document, Alt */
var alt = new Alt();
const actions = alt.generateActions("addItem", "checkItem", "saveDescr", "undo");
var undoStack = [];
class TodoStore {
constructor () {
this.list = [];
this.descr = "";
this.bindActions(actions);
}
onAddItem (descr) {
this.descr = "";
this.list.push({
descr,
done: false
});
undoStack.push(alt.takeSnapshot());
}
onCheckItem (data) {
this.list[data.index].done = data.done;
undoStack.push(alt.takeSnapshot());
}
onSaveDescr (descr) {
this.descr = descr;
}
onUndo () {
if (undoStack.length) {
alt.bootstrap(undoStack.pop());
}
}
}
let todoStore = alt.createStore(TodoStore, "TodoStore");
class Todo {
constructor() {
this.state = todoStore.state;
}
static view(todo) {
return [
m("input", {
onchange: ev => actions.saveDescr(ev.target.value),
value: todo.state.descr
}),
m("button", {
onclick: () => actions.addItem(todo.state.descr)
}, "Add"),
m("table", todo.state.list.map((task, index) => m("tr", [
m("td",
m("input[type=checkbox]", {
onclick: ev => {
actions.checkItem({
index,
done: ev.target.checked
});
},
checked: task.done
})
),
m("td", {
style: {
textDecoration: task.done ? "line-through" : "none"
}
}, task.descr)
])
)
),
m("hr"),
m("button", {
onclick: () => actions.undo(),
disabled: undoStack.length === 0
}, "Undo"),
m("ul", undoStack.map(item => m("li", item)))
];
}
}
m.mount(document.body, {
controller: Todo,
view: Todo.view
});