-
Notifications
You must be signed in to change notification settings - Fork 4
/
todo.html
165 lines (159 loc) · 5.53 KB
/
todo.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Lightue • TodoMVC</title>
<link rel="stylesheet" href="node_modules/todomvc-common/base.css" />
<link rel="stylesheet" href="node_modules/todomvc-app-css/index.css" />
</head>
<body>
<script src="node_modules/todomvc-common/base.js"></script>
<script src="node_modules/director/build/director.js"></script>
<script src="dist/lightue.min.js"></script>
<script>
var L = Lightue,
STORAGE_KEY = 'todos-lightue',
S = L.useState({
todos: JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]'),
editedTodo: null,
visibility: '',
}),
beforeEditCache = '',
router = new Router(),
routes = [
['', 'All'],
['active', 'Active'],
['completed', 'Completed'],
]
routes.forEach(function (route) {
router.on(route[0], function () {
S.visibility = route[0]
})
})
router.init()
L.watchEffect(() => localStorage.setItem(STORAGE_KEY, JSON.stringify(S.todos)))
function removeTodo(todo) {
S.todos = S.todos.filter((t) => t != todo)
}
function doneEdit(todo, value) {
if (S.editedTodo) {
S.editedTodo = null
todo.title = value.trim()
if (!todo.title) removeTodo(todo)
}
}
function getRemainingString(count) {
return count + ' item' + (count == 1 ? '' : 's') + ' left'
}
L({
todoapp: L.section({
header: L.header({
$$: {
$tag: 'h1',
$$: 'todos',
},
newTodo: L.input({
_placeholder: 'What needs to be done?',
_autofocus: 'autofocus',
onkeydown: (e) => {
var newTodo = e.target.value.trim()
if (e.key == 'Enter' && newTodo) {
S.todos.push({
id: Date.now(),
title: newTodo,
completed: false,
})
e.target.value = ''
}
},
}),
}),
main: L.section({
_hidden: () => S.todos.length == 0,
toggleAll: L.input({
_id: 'toggle-all',
_type: 'checkbox',
$checked: () => S.todos.filter((todo) => !todo.completed).length == 0,
onchange: (e) => {
var checked = e.target.checked
S.todos.forEach((todo) => (todo.completed = checked))
},
}),
$$: L.label({
_for: 'toggle-all',
$$: 'Mark all as complete',
}),
todoList: L.ul({
$$: () =>
S.todos.map((todo) =>
L.li.todo({
$if: () => S.visibility == '' || S.visibility == 'active' && !todo.completed || S.visibility == 'completed' && todo.completed,
$class: {
completed: () => todo.completed,
editing: () => todo == S.editedTodo,
},
view: {
toggle: L.input({
_type: 'checkbox',
$checked: () => todo.completed,
onchange: (e) => {
todo.completed = e.target.checked
},
}),
todoLabel: L.label({
ondblclick: (e) => {
beforeEditCache = todo.title
S.editedTodo = todo
e.target.parentElement.nextElementSibling.focus()
},
$$: todo.$title,
}),
destroy: L.button({
onclick: (e) => removeTodo(todo),
}),
},
edit: L.input({
_type: 'text',
$value: todo.$title,
onkeydown: (e) => {
if (e.key == 'Enter') {
doneEdit(todo, e.target.value)
} else if (e.key == 'Escape') {
S.editedTodo = null
todo.title = beforeEditCache
}
},
onblur: (e) => doneEdit(todo, e.target.value),
}),
})
),
}),
}),
footer: L.footer({
_hidden: () => S.todos.length == 0,
$_todoCount: () => getRemainingString(S.todos.filter((todo) => !todo.completed).length),
filters: L.ul(
routes.map((route) =>
L.li({
$$: L.a({
_href: '#/' + route[0],
$class: { selected: () => S.visibility == route[0] },
$$: route[1],
}),
})
)
),
clearCompleted: L.button({
onclick: (e) => {
S.todos = S.todos.filter((todo) => !todo.completed)
},
_hidden: () => S.todos.filter((todo) => todo.completed).length == 0,
$$: 'Clear completed',
}),
}),
}),
info: L.footer([L.p('Double-click to edit a todo'), L.p('Created by Smalllong'), L.p('Part of TodoMVC')]),
})
</script>
</body>
</html>