-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.js
105 lines (85 loc) · 2.58 KB
/
main.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
101
102
103
104
105
// import choo's template helper
const html = require('choo/html')
const STATUS_IN_PROGRESS = 0
const STATUS_DONE = 1
module.exports = function (state, emit) {
return html`
<div class="app">
<div class="box">
<h1>TODOS</h1>
</div>
<ul id="todos" class="box">
${state.todos.map(todo)}
</ul>
<div class="box">
<input id="searchString" type="text">
<button onclick=${runSearch}>SEARCH</button>
<button onclick=${runGetAll}>ALL</button>
</div>
<div class="box">
<input id="filterString" type="text" value=${state.filterString}>
<button onclick=${runFilter}>FILTER</button>
<input type="checkbox" onclick=${runFilterDone} ${state.filterDone === 1 ? 'checked' : ''}> ONLY DONE
</div>
<div class="box">
<input id="newTodoText" type="text">
<button onclick=${runAdd}>ADD</button>
</div>
<div id="testError" class="box">
<button onclick=${runTestError}>TEST ERROR</button>
<ul class="errors">
${state.errors.map(error)}
</ul>
</div>
<hr />
</div>
`
function error (error) {
return html`
<li class="error">
${error}
</li>
`
}
function todo (todo) {
return html`
<div class="todo">
<span class="remove" data-id=${todo.id} onclick=${runDelete}>×</span>
<span class="task" data-id=${todo.id}>${todo.task}</span>
<span class="status" data-id=${todo.id} data-status=${todo.status}
onclick=${runToggleStatus}>${todo.status === STATUS_IN_PROGRESS ? 'in progress' : 'done'}</span>
</div>
`
}
function runDelete (e) {
const id = e.target.dataset.id
FBP.log(`running delete on id: ${id}`)
emit('deleteTodo', id)
}
function runToggleStatus (e) {
const id = parseInt(e.target.dataset.id, 10)
const status = parseInt(e.target.dataset.status, 10) === STATUS_IN_PROGRESS ? STATUS_DONE : STATUS_IN_PROGRESS
emit('toggleStatus', id, status)
}
function runSearch () {
const query = document.getElementById('searchString').value
emit('search', query)
}
function runGetAll () {
emit('getAll')
}
function runFilter () {
const filterString = document.getElementById('filterString').value
emit('filter', 'task', filterString)
}
function runFilterDone () {
emit('filter', 'status', state.filterDone === 1 ? 0 : 1)
}
function runAdd () {
const text = document.getElementById('newTodoText').value
emit('add', text)
}
function runTestError () {
emit('testError')
}
}