-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo_app.html
251 lines (212 loc) · 6.45 KB
/
todo_app.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Signals TODO App</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 2rem;
}
ul {
list-style: none;
padding: 0;
}
</style>
</head>
<body>
<h1>Signals TODO App</h1>
<form id="todoForm">
<input type="text" id="todoInput" placeholder="Add a new task" />
<button type="submit">Add</button>
</form>
<h2>Tasks</h2>
<div id="taskFilter">
<label><input type="radio" name="filter" value="all" checked /> All</label>
<label><input type="radio" name="filter" value="active" /> Active</label>
<label><input type="radio" name="filter" value="completed" /> Completed</label>
</div>
<ul id="todoList"></ul>
<div id="taskCounter"></div>
<script>
// Include the Signals library code here
// Global variable to keep track of the currently accessed computed or effect
let currentAccessed = null;
const effectQueue = [];
class Signal {
constructor(initialValue) {
this._value = initialValue;
this._dependents = [];
}
get value() {
if (currentAccessed) {
this._addDependent(currentAccessed);
}
return this._value;
}
set value(newValue) {
if (this._value !== newValue) {
this._value = newValue;
this._notifyDependents();
executeEffects();
}
}
_addDependent(dependent) {
if (!this._dependents.includes(dependent)) {
this._dependents.push(dependent);
}
}
_removeDependent(dependent) {
this._dependents = this._dependents.filter((dep) => dep !== dependent);
}
_notifyDependents() {
for (const dependent of this._dependents) {
dependent._update();
}
}
}
class Computed {
constructor(computeFn) {
this._computeFn = computeFn;
this._value = undefined;
this._isStale = true;
this._dependents = [];
}
get value() {
if (this._isStale) {
const previousContext = currentAccessed;
currentAccessed = this;
this._recomputeValue();
currentAccessed = previousContext;
}
if (currentAccessed) {
this._addDependent(currentAccessed);
}
return this._value;
}
_recomputeValue() {
this._value = this._computeFn();
this._isStale = false;
}
_addDependent(dependent) {
if (!this._dependents.includes(dependent)) {
this._dependents.push(dependent);
}
}
_update() {
if (!this._isStale) {
this._isStale = true;
for (const dependent of this._dependents) {
dependent._update();
}
}
}
}
// Store the current context (a Computed or Effect instance) for dependency tracking
class Effect {
constructor(effectFn) {
this._effectFn = effectFn;
this._isStale = true;
this._execute();
}
_execute() {
if (this._isStale) {
currentAccessed = this;
this._effectFn();
currentAccessed = null;
}
this._isStale = false;
}
_update() {
if (!this._isStale) {
this._isStale = true;
effectQueue.push(this);
}
}
}
function executeEffects() {
while (effectQueue.length > 0) {
const effect = effectQueue.shift();
effect._execute();
}
}
function createSignal(initialValue) {
return new Signal(initialValue);
}
function createComputed(computeFn) {
return new Computed(computeFn);
}
function createEffect(effectFn) {
return new Effect(effectFn);
}
// Application logic
const todoForm = document.getElementById("todoForm");
const todoInput = document.getElementById("todoInput");
const todoList = document.getElementById("todoList");
const taskFilter = document.getElementById("taskFilter");
const taskCounter = document.getElementById("taskCounter");
const tasks = createSignal([]);
const filter = createSignal("all");
const filteredTasks = createComputed(() => {
const currentFilter = filter.value;
const currentTasks = tasks.value;
if (currentFilter === "all") {
return currentTasks;
} else if (currentFilter === "active") {
return currentTasks.filter((task) => !task.completed);
} else {
return currentTasks.filter((task) => task.completed);
}
});
const taskCount = createComputed(() => {
return tasks.value.length;
});
const activeTaskCount = createComputed(() => {
return tasks.value.filter((task) => !task.completed).length;
});
const completedTaskCount = createComputed(() => {
return tasks.value.filter((task) => task.completed).length;
});
todoForm.addEventListener("submit", (event) => {
event.preventDefault();
const taskTitle = todoInput.value.trim();
if (taskTitle) {
const newTask = { title: taskTitle, completed: false };
tasks.value = [...tasks.value, newTask];
todoInput.value = "";
}
});
taskFilter.addEventListener("change", (event) => {
filter.value = event.target.value;
});
createEffect(() => {
const currentTasks = filteredTasks.value;
todoList.innerHTML = "";
currentTasks.forEach((task, index) => {
const listItem = document.createElement("li");
const label = document.createElement("label");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = task.completed;
checkbox.addEventListener("change", () => {
tasks.value[index].completed = checkbox.checked;
tasks.value = [...tasks.value];
});
label.appendChild(checkbox);
label.appendChild(document.createTextNode(" " + task.title));
listItem.appendChild(label);
todoList.appendChild(listItem);
});
});
createEffect(() => {
taskCounter.textContent = `
Total: ${taskCount.value},
Active: ${activeTaskCount.value},
Completed: ${completedTaskCount.value}
`;
});
</script>
</body>
</html>