-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
109 lines (88 loc) · 2.2 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
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
106
107
108
109
;(function() {
'use strict'
//
// Variables
//
// The new todo input field
const newTodo = document.querySelector('#new-todo')
//
// Methods
//
/**
* Create a todo component
*/
const app = new Reef('#app', {
data: {
todos: []
},
template: function({ todos }) {
// If there are no todos, ask the user to add some
if (todos.length < 1) {
return "<p>You don't have any todos yet. Add some using the form above.</p>"
}
// Generate markup for todo items
return (
'<ul class="todos">' +
todos
.map(
(todo, index) =>
`<li ${
todo.completed ? 'class="todo-completed"' : ''
}><label for='${index}'><input type="checkbox" id="todo-${index}" data-todo="${index}" ${
todo.completed ? 'checked' : ''
}>${todo.text}</label></li>`
)
.join('') +
'</ul>'
)
}
})
const addTodo = function(todo) {
// Get a copy of the todos and update data object
const todos = [...app.data.todos, { text: todo.value, completed: false }]
// Render fresh UI
app.setData({ todos: todos })
// Clear the field and return focus
todo.value = ''
todo.focus()
}
/**
* Handle form submit events
* @param {Event} event The Event object
*/
const submitHandler = function(e) {
// Only run for #add-todos form
if (e.target.id !== 'add-todos') return
// Stop the form from reloading the page
e.preventDefault()
// If the #new-todo input has no value, do nothing
if (newTodo.value.length < 1) return
addTodo(newTodo)
}
/**
* Handle click events
* @param {Event} event The Event object
*/
const clickHandler = function(e) {
// Only run on todo items
const todo = event.target.getAttribute('data-todo')
if (!todo) return
// Get a copy of the todos
const todos = [...app.data.todos]
if (!todos[todo]) return
// Update the todo state
todos[todo].completed = e.target.checked
console.log(todos[todo])
// Render fresh UI
app.render()
}
//
// Inits & Event Listeners
//
// Render the initial UI
app.render()
// Listen for form submit events
document.addEventListener('submit', submitHandler, false)
// Listen for click events
document.addEventListener('click', clickHandler, false)
})()