-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodolist.html
executable file
·68 lines (61 loc) · 1.81 KB
/
todolist.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<title>devoidfury Todo</title>
<style type="text/css">
* { box-sizing: border-box; margin: 0; padding: 0 }
h1 { text-transform: capitalize; text-align: center; margin-bottom: 1em }
form { text-align: center }
main { max-width: 330px; margin: 2em auto 0; padding: 1em; background: #DDD }
button {
background: #9109a3;
color: #eee;
padding: 0.2em 2em;
border: none; border-radius: 5px;
cursor: pointer;
}
li {margin: 0 0 1em 2em}
[disabled] + span {text-decoration: line-through}
</style>
<main>
<h1>Todo List</h1>
<ul id="container"></ul>
<form id="add-todo">
<label>Todo <input type="text" id="new-text"></label>
<button type="submit">Add</button>
</form>
</main>
<script type="text/template" id="todo-item-tmpl">
<li>
<label><input type="checkbox"> <span>{{text}}</span></label>
<button class="remove">remove</button>
</li>
</script>
<script>
void function() {
function tmpl(str, ctx={}) {
return str.replace(/\{\{([^}]+)\}\}/g, (s) => { with (ctx) return eval(s) })
}
const item_tmpl = document.getElementById('todo-item-tmpl').textContent
const $UI = {
form: document.getElementById('add-todo'),
input: document.getElementById('new-text'),
container: document.getElementById('container'),
}
$UI.form.addEventListener('submit', function(e) {
e.preventDefault()
let text = $UI.input.value.trim()
if (!text) return false
$UI.container.insertAdjacentHTML('beforeend', tmpl(item_tmpl, {text}))
$UI.input.value = ''
}, false)
$UI.container.addEventListener('change', function(e) {
e.target.disabled = true
e.stopPropagation()
}, false)
$UI.container.addEventListener('click', function(e) {
if (!e.target.classList.contains('remove')) return
let $item = e.target.parentElement
$item.parentElement.removeChild($item)
}, false)
}()
</script>