-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
197 lines (183 loc) · 4.53 KB
/
index.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Todo using AlpineJs</title>
<link rel="stylesheet" href="css/base.css" />
<link rel="stylesheet" href="css/index.css" />
<link rel="stylesheet" href="css/app.css" />
<script defer src="js/alpine.min.js"></script>
<style type="text/css">
[x-cloak] {
display: none;
}
</style>
</head>
<body>
<section
class="todoapp"
x-data="todoApp"
x-init="$watch('todos', value => localStorage.setItem('todos', JSON.stringify(value)))"
>
<header class="header">
<h1>AlpineTodo</h1>
<form @submit.prevent="create">
<input
class="new-todo"
placeholder="What needs to be done?"
autofocus
x-model="new_todo"
required
/>
</form>
</header>
<section class="main" x-show="todos.length">
<input
id="toggle-all"
class="toggle-all"
type="checkbox"
x-model="allComplete"
@click="toggleTodos"
/>
<label for="toggle-all" x-cloak>Mark all as complete</label>
<ul class="todo-list">
<template x-for="(todo, i) in filteredTodos" :key="i">
<li :class="{ completed: todo.completed, editing: todo.editing }">
<div class="view">
<input
class="toggle"
type="checkbox"
@click="completeToggle(todo)"
x-model="todo.completed"
/>
<label x-text="todo.label" @dblclick="edit(todo)"></label>
<button class="destroy" @click="destroy(todo)"></button>
</div>
<input
class="edit"
x-model="todo.label"
@keyup.enter="update(todo)"
@keyup.escape="updateCacnel(todo)"
@click.away="update(todo)"
required
/>
</li>
</template>
</ul>
</section>
<footer class="footer" x-show="todos.length" x-cloak>
<span class="todo-count"
><strong x-text="active.length"></strong>
<span x-text="active.length == 1 ? 'item' : 'items'"></span>
left</span
>
<ul class="filters">
<li>
<a
@click.prevent="filter = 'all'"
:class="{ selected: filter === 'all' }"
href="#/"
>All</a
>
</li>
<li>
<a
@click.prevent="filter = 'active'"
:class="{ selected: filter === 'active' }"
href="#/active"
>Active</a
>
</li>
<li>
<a
@click.prevent="filter = 'completed'"
:class="{ selected: filter === 'completed' }"
href="#/completed"
>Completed</a
>
</li>
</ul>
<!-- Hidden if no completed items are left ↓ -->
<button
class="clear-completed"
x-show="completed.length"
@click="todos = active"
>
Clear completed
</button>
</footer>
</section>
<footer class="info">
<p>
Created by
<a href="https://iammuttaqi.github.io" target="_blank">Muttaqi</a>
</p>
</footer>
<!-- Scripts here. Don't remove ↓ -->
<script src="js/base.js"></script>
<script src="js/app.js"></script>
<script>
const todoApp = {
todos: JSON.parse(localStorage.getItem("todos") || "[]"),
new_todo: "",
existing_todo: null,
filter: "all",
get filteredTodos() {
if (this.filter === "active") {
return this.active;
} else if (this.filter === "completed") {
return this.completed;
} else {
return this.todos;
}
},
get active() {
return this.todos.filter((todo) => !todo.completed);
},
get completed() {
return this.todos.filter((todo) => todo.completed);
},
get allComplete() {
return this.todos.length == this.completed.length;
},
create() {
this.todos.push({
id: Date.now(),
label: this.new_todo,
completed: false,
editing: false,
});
this.new_todo = "";
},
edit(todo) {
todo.cached_label = todo.label;
todo.editing = true;
this.existing_todo = todo;
},
update(todo) {
if (todo.label != "") {
this.existing_todo = null;
todo.editing = false;
}
},
updateCacnel(todo) {
todo.label = todo.cached_label;
todo.editing = false;
todo.cached_label = null;
},
completeToggle(todo) {
todo.completed = !todo.completed;
},
toggleTodos() {
let allComplete = this.allComplete;
return this.todos.forEach((todo) => (todo.completed = !allComplete));
},
destroy(todo) {
let position = this.todos.indexOf(todo);
this.todos.splice(position, 1);
},
};
</script>
</body>
</html>