-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (55 loc) · 1.47 KB
/
app.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
'use strict'
// define an app component with the above and an URL-state synchronizing middleware
nx.components.app({
params: {
status: {history: true, url: true, default: 'all'}
}
}).use(setup).register('todo-app')
// this is a middleware function, which is used to add functionality to components
function setup(elem, state) {
state.todos = {
all: JSON.parse(localStorage.getItem('todos-nx')) || [],
get completed() {
return this.all.filter(todo => todo.completed)
},
get active() {
return this.all.filter(todo => !todo.completed)
},
get allCompleted() {
return this.all.every(todo => todo.completed)
},
set allCompleted(value) {
if (value) {
this.all.forEach(todo => todo.completed = true)
} else {
this.all.forEach(todo => todo.completed = false)
}
},
create(event) {
const title = event.target.value.trim()
if (title) {
this.all.push({title})
}
event.target.value = ''
},
edit(todo, event) {
todo.title = event.target.value.trim()
if (!todo.title) {
this.remove(todo)
}
todo.editing = false
},
remove(todo) {
const index = this.all.indexOf(todo)
this.all.splice(index, 1)
},
clearCompleted() {
this.all = this.active
},
toJSON() {
return this.all.map(todo => ({title: todo.title, completed: todo.completed}))
}
}
// auto save todos when the todos array or a todo title/completed is mutated/changed
elem.$observe(() => localStorage.setItem('todos-nx', JSON.stringify(state.todos)))
}