forked from Raynos/mercury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.js
99 lines (80 loc) · 1.96 KB
/
update.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
var State = require("./state.js")
module.exports = {
setRoute: setRoute,
toggleAll: toggleAll,
add: add,
setTodoField: setTodoField,
toggle: toggle,
destroy: destroy,
startEdit: startEdit,
finishEdit: finishEdit,
cancelEdit: cancelEdit,
clearCompleted: clearCompleted
}
function setRoute(state, route) {
state.route.set(route.substr(2) || "all")
}
function toggleAll(state, value) {
state.todos.forEach(function (todo) {
todo.completed.set(value.toggle)
})
}
function add(state, data) {
state.todos.push(State.todoItem({
title: data.newTodo.trim()
}))
state.todoField.set("")
}
function setTodoField(state, data) {
state.todoField.set(data.newTodo)
}
function toggle(state, data) {
var item = find(state.todos, data.id)
item.completed.set(data.completed)
}
function startEdit(state, data) {
var item = find(state.todos, data.id)
item.editing.set(true)
}
function destroy(state, data) {
var index = findIndex(state.todos, data.id)
state.todos.splice(index, 1)
}
function finishEdit(state, data) {
var item = find(state.todos, data.id)
if (!item || item.editing === false) return
item.editing.set(false)
item.title.set(data.title)
if (data.title.trim() === "") {
destroy(state, data)
}
}
function cancelEdit(state, data) {
var item = find(state.todos, data.id)
item.editing.set(false)
}
function clearCompleted(state) {
state.todos().forEach(function (todo, index) {
if (todo.completed) {
destroy(state, todo)
}
})
}
function find(list, id) {
for (var i = 0; i < list.getLength(); i++) {
var item = list.get(i)
if (item.id === id) {
return item
}
}
return null
}
function findIndex(list, id) {
for (var i = 0; i < list.getLength(); i++) {
var item = list.get(i)
if (item.id === id) {
return i
}
}
return null
}