This repository has been archived by the owner on Aug 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (88 loc) · 2.38 KB
/
index.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
function toggleClass(e, cls) {
let clsName = ["all", "remaining", "completed"]
clsName.forEach(n => {
if (n !== cls) {
e.classList.remove(n)
}
})
e.classList.add(cls)
}
let todos = [{
title: 'hello',
done: true,
hide: false,
}, {
title: 'WoW',
done: false,
hide: false,
}, {
title: 'test',
done: false,
hide: false,
}]
let app = new Seed('#app', {
data: {
todos,
remaining: todos.reduce((count, todo) => {
return count + (todo.done ? 0 : 1)
}, 0)
},
methods: {
addTodo(e) {
let title = e.el.value
if (title) {
e.el.value = ''
this.scope.todos.push({ title, done: false })
this.scope.remaining++
}
},
filter(e) {
let cls = e.el.className.replace('selected', '').trim()
toggleClass(this.root, cls)
},
markAll() {
let mark = this.scope.todos.reduce((flag, e) => {
return flag && e.done
}, true)
this.scope.todos.forEach(todo => {
let old = todo.done
todo.done = !mark
this.scope.remaining += old !== todo.done ? (todo.done ? -1 : 1) : 0
})
},
clearCompleted() {
let todos = this.scope.todos
let length = todos.length
let removed = []
for (let i = 0; i < length; i++) {
if (todos[i].done) {
removed.push(i)
}
}
let count = 0
removed.forEach(index => {
todos.splice(index - count, 1)
count++
})
},
removeTodo(e) {
let i = e.seed.options.eachIndex
let scope = this.options.parent.scope
scope.todos.splice(i, 1)
scope.remaining -= e.seed.scope.done ? 0 : 1
},
toggleTodo(e) {
let scope = this.options.parent.scope
scope.remaining += e.seed.scope.done ? -1 : 1
},
editTodo(e) {
e.seed.scope.hide = true
let input = e.seed.root.querySelector('input.edit')
input.focus()
},
saveTodo(e) {
e.seed.scope.title = e.el.value
e.seed.scope.hide = false
}
}
})