-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.html
More file actions
406 lines (359 loc) · 16.8 KB
/
todo.html
File metadata and controls
406 lines (359 loc) · 16.8 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>To‑Do List</title>
<style>
:root{
--bg: #0f172a; /* slate-900 */
--panel: #111827; /* gray-900 */
--muted: #94a3b8; /* slate-400 */
--text: #e5e7eb; /* gray-200 */
--brand: #6366f1; /* indigo-500 */
--brand-2: #8b5cf6; /* violet-500 */
--ok: #10b981; /* emerald-500 */
--danger: #ef4444; /* red-500 */
--warning: #f59e0b; /* amber-500 */
--ring: #334155; /* slate-700 */
--chip: #1f2937; /* gray-800 */
--chip-active: #0ea5e9;/* sky-500 */
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body{
margin:0; font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
background: radial-gradient(1000px 500px at 10% -10%, #1e293b 0%, transparent 60%),
radial-gradient(1000px 500px at 110% 0%, #1e293b 0%, transparent 60%),
var(--bg);
color: var(--text);
display:flex; align-items:center; justify-content:center; padding: 24px;
}
.app{
width: min(850px, 100%);
background: linear-gradient(180deg, rgba(255,255,255,0.04), rgba(255,255,255,0.02));
border: 1px solid rgba(148,163,184,0.15);
border-radius: 20px;
box-shadow: 0 10px 30px rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.06);
padding: 20px 20px 16px;
backdrop-filter: blur(8px);
}
.header{
display:flex; align-items:center; gap:14px; justify-content:space-between; margin-bottom: 16px;
}
.title{
display:flex; align-items:center; gap:10px;
font-weight:700; letter-spacing: 0.2px;
}
.title .logo{
width: 36px; height:36px; border-radius: 12px;
background: conic-gradient(from 180deg at 50% 50%, var(--brand), var(--brand-2));
display:grid; place-items:center; color:white; font-weight:800;
box-shadow: 0 6px 18px rgba(99,102,241,0.5);
}
.title span{ font-size: 1.2rem; }
.controls{
display:flex; gap:8px; align-items:center;
}
.btn{ cursor:pointer; border:none; border-radius: 10px; padding:10px 12px; color:var(--text); background:#0b1220; border:1px solid var(--ring);
display:inline-flex; align-items:center; gap:8px; font-weight:600; transition: transform .06s ease, background .2s ease, border-color .2s ease;
}
.btn:hover{ transform: translateY(-1px); border-color:#475569; }
.btn:active{ transform: translateY(0); }
.input-row{ display:flex; gap:10px; margin: 8px 0 14px; }
.input{ flex:1; position:relative; }
.input input{
width: 100%; padding: 14px 16px 14px 44px; border-radius: 12px; border: 1px solid var(--ring);
background: #0b1220; color: var(--text); outline: none; font-size: 15px;
box-shadow: inset 0 1px 0 rgba(255,255,255,0.04);
}
.input input:focus{ border-color: var(--brand); box-shadow: 0 0 0 4px rgba(99,102,241,0.2); }
.input .icon{ position:absolute; left:12px; top:50%; transform: translateY(-50%); opacity:.8; }
.pill-tabs{ display:flex; gap:8px; flex-wrap:wrap; }
.pill{ padding:8px 12px; border-radius: 999px; background: var(--chip); border:1px solid var(--ring); cursor:pointer; font-weight:600; }
.pill[aria-current="true"]{ outline: none; border-color: var(--chip-active); box-shadow: 0 0 0 3px rgba(14,165,233,0.25); }
.list{
list-style:none; margin: 14px 0 8px; padding: 0; display: grid; gap:8px;
}
.item{
display:grid; grid-template-columns: auto 1fr auto; align-items:center; gap:12px;
padding: 12px; background: #0b1220; border:1px solid var(--ring); border-radius: 14px;
}
.item[draggable="true"]{ cursor: grab; }
.item.dragging{ opacity:.5; }
.checkbox{ width:22px; height:22px; appearance:none; border:2px solid #475569; border-radius:6px; display:grid; place-items:center; background:transparent; cursor:pointer; }
.checkbox:checked{ background: linear-gradient(135deg, var(--ok), #22c55e); border-color: transparent; }
.checkbox:checked::after{ content:"✓"; font-weight:800; color:white; font-size: 14px; }
.text{ line-height:1.4; }
.text.completed{ text-decoration: line-through; color: var(--muted); }
.actions{ display:flex; gap:8px; }
.icon-btn{ width:34px; height:34px; border-radius: 10px; border:1px solid var(--ring); background:#0b1220; display:grid; place-items:center; cursor:pointer; }
.icon-btn:hover{ border-color:#64748b; }
.footer{
display:flex; align-items:center; justify-content:space-between; gap:8px; margin-top: 10px; flex-wrap: wrap;
color: var(--muted); font-size: 14px;
}
.sr-only{ position:absolute; width:1px; height:1px; padding:0; margin:-1px; overflow:hidden; clip:rect(0,0,0,0); border:0; }
@media (max-width: 540px){
.actions{ gap:6px; }
.btn{ padding: 9px 10px; }
}
</style>
</head>
<body>
<div class="app" role="application" aria-label="To‑Do app">
<div class="header">
<div class="title">
<div class="logo" aria-hidden="true">✓</div>
<span>To‑Do List</span>
</div>
<div class="controls">
<button id="clearCompleted" class="btn" title="Clear completed tasks">🧹 Clear Completed</button>
<button id="exportJson" class="btn" title="Download tasks as JSON">⬇️ Export</button>
<button id="importJson" class="btn" title="Import tasks from JSON">⬆️ Import</button>
<button id="toggleTheme" class="btn" title="Toggle theme">🌗 Theme</button>
</div>
</div>
<div class="input-row">
<label class="input">
<span class="icon">📝</span>
<input id="newTask" type="text" placeholder="Add a task and press Enter…" aria-label="Add new task" />
</label>
<button id="addBtn" class="btn" aria-label="Add task">Add</button>
</div>
<div class="pill-tabs" role="tablist" aria-label="Filters">
<button class="pill" data-filter="all" aria-current="true">All</button>
<button class="pill" data-filter="active">Active</button>
<button class="pill" data-filter="completed">Completed</button>
<button class="pill" data-filter="today">Today</button>
<button class="pill" data-filter="overdue">Overdue</button>
</div>
<ul id="todoList" class="list" aria-live="polite"></ul>
<div class="footer">
<div><strong id="count">0</strong> items • <span id="status">All</span></div>
<div>Tip: drag to reorder • double‑click a task to edit</div>
</div>
</div>
<template id="itemTemplate">
<li class="item" draggable="true">
<input type="checkbox" class="checkbox" />
<div class="text" contenteditable="false"></div>
<div class="actions">
<button class="icon-btn" data-action="edit" title="Edit">✏️</button>
<button class="icon-btn" data-action="schedule" title="Set due date">📅</button>
<button class="icon-btn" data-action="delete" title="Delete">🗑️</button>
</div>
<span class="sr-only" data-due></span>
</li>
</template>
<script>
(function(){
const list = document.getElementById('todoList');
const input = document.getElementById('newTask');
const addBtn = document.getElementById('addBtn');
const filterTabs = document.querySelectorAll('.pill');
const countEl = document.getElementById('count');
const statusEl = document.getElementById('status');
const clearCompletedBtn = document.getElementById('clearCompleted');
const exportBtn = document.getElementById('exportJson');
const importBtn = document.getElementById('importJson');
const themeBtn = document.getElementById('toggleTheme');
const STORAGE_KEY = 'todo-items-v1';
let items = load();
let filter = 'all';
function uid(){ return Math.random().toString(36).slice(2, 10); }
function load(){
try{ return JSON.parse(localStorage.getItem(STORAGE_KEY)) || []; }
catch{ return []; }
}
function save(){ localStorage.setItem(STORAGE_KEY, JSON.stringify(items)); }
function render(){
list.innerHTML = '';
const now = new Date(); now.setHours(0,0,0,0);
const frag = document.createDocumentFragment();
let visibleCount = 0;
for(const item of items){
if(!matchesFilter(item)) continue;
visibleCount++;
const el = createItemElement(item);
frag.appendChild(el);
}
list.appendChild(frag);
countEl.textContent = items.filter(i=>!i.completed).length;
statusEl.textContent = filter[0].toUpperCase() + filter.slice(1);
}
function matchesFilter(item){
const today = new Date(); today.setHours(0,0,0,0);
if(filter === 'all') return true;
if(filter === 'active') return !item.completed;
if(filter === 'completed') return item.completed;
if(filter === 'today'){
if(!item.due) return false;
const d = new Date(item.due); d.setHours(0,0,0,0);
return d.getTime() === today.getTime();
}
if(filter === 'overdue'){
if(!item.due) return false;
const d = new Date(item.due); d.setHours(0,0,0,0);
return d.getTime() < today.getTime() && !item.completed;
}
return true;
}
function createItemElement(item){
const tpl = document.getElementById('itemTemplate');
const node = tpl.content.firstElementChild.cloneNode(true);
const checkbox = node.querySelector('.checkbox');
const textDiv = node.querySelector('.text');
const dueLive = node.querySelector('[data-due]');
node.dataset.id = item.id;
textDiv.textContent = item.text;
if(item.completed) textDiv.classList.add('completed');
checkbox.checked = !!item.completed;
node.title = item.due ? `Due: ${new Date(item.due).toLocaleDateString()}` : 'No due date';
dueLive.textContent = item.due ? `Due ${new Date(item.due).toDateString()}` : '';
checkbox.addEventListener('change', () => {
item.completed = checkbox.checked;
textDiv.classList.toggle('completed', item.completed);
save();
render();
});
node.addEventListener('dblclick', () => startEdit(textDiv, item));
node.querySelector('[data-action="edit"]').addEventListener('click', () => startEdit(textDiv, item));
node.querySelector('[data-action="schedule"]').addEventListener('click', () => setDueDate(item));
node.querySelector('[data-action="delete"]').addEventListener('click', () => removeItem(item.id));
// Drag & drop
node.addEventListener('dragstart', (e)=>{
node.classList.add('dragging');
e.dataTransfer.setData('text/plain', item.id);
});
node.addEventListener('dragend', ()=> node.classList.remove('dragging'));
node.addEventListener('dragover', (e)=>{
e.preventDefault();
const dragging = list.querySelector('.dragging');
if(!dragging || dragging === node) return;
const rect = node.getBoundingClientRect();
const after = (e.clientY - rect.top) / rect.height > 0.5;
list.insertBefore(dragging, after ? node.nextSibling : node);
});
node.addEventListener('drop', ()=>{
const ids = [...list.children].map(li=>li.dataset.id);
items.sort((a,b)=> ids.indexOf(a.id) - ids.indexOf(b.id));
save();
render();
});
return node;
}
function startEdit(textDiv, item){
textDiv.contentEditable = 'true';
textDiv.focus();
const range = document.createRange();
range.selectNodeContents(textDiv); range.collapse(false);
const sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range);
function finish(commit){
textDiv.contentEditable = 'false';
textDiv.removeEventListener('keydown', onKey);
textDiv.removeEventListener('blur', onBlur);
if(commit){
const val = textDiv.textContent.trim();
if(val){ item.text = val; save(); render(); }
else { removeItem(item.id); }
} else {
textDiv.textContent = item.text; // revert
}
}
function onKey(e){
if(e.key === 'Enter'){ e.preventDefault(); finish(true); }
if(e.key === 'Escape'){ e.preventDefault(); finish(false); }
}
function onBlur(){ finish(true); }
textDiv.addEventListener('keydown', onKey);
textDiv.addEventListener('blur', onBlur);
}
function setDueDate(item){
const current = item.due ? new Date(item.due).toISOString().slice(0,10) : '';
const picked = prompt('Set due date (YYYY-MM-DD). Leave empty to clear.', current) || '';
if(picked === ''){ item.due = null; }
else if(/^\d{4}-\d{2}-\d{2}$/.test(picked)){
const d = new Date(picked + 'T00:00:00');
if(!isNaN(+d)) item.due = d.toISOString();
} else {
alert('Invalid date format. Use YYYY-MM-DD');
return;
}
save(); render();
}
function removeItem(id){
items = items.filter(i=>i.id !== id);
save(); render();
}
function addItem(){
const text = input.value.trim();
if(!text) return;
items.push({ id: uid(), text, completed:false, due:null, createdAt: new Date().toISOString() });
input.value = '';
save(); render();
}
function clearCompleted(){
items = items.filter(i=>!i.completed);
save(); render();
}
function setFilter(next){
filter = next; render();
for(const tab of filterTabs){ tab.setAttribute('aria-current', tab.dataset.filter===filter ? 'true' : 'false'); }
}
function exportJSON(){
const blob = new Blob([JSON.stringify(items, null, 2)], {type:'application/json'});
const url = URL.createObjectURL(blob);
const a = Object.assign(document.createElement('a'), { href:url, download: 'todos.json' });
document.body.appendChild(a); a.click(); a.remove();
URL.revokeObjectURL(url);
}
function importJSON(){
const input = document.createElement('input');
input.type = 'file'; input.accept = '.json,application/json';
input.onchange = () => {
const file = input.files[0]; if(!file) return;
const reader = new FileReader();
reader.onload = () => {
try{
const data = JSON.parse(reader.result);
if(Array.isArray(data)){
items = data.map(x=>({ id: x.id || uid(), text: String(x.text||'').trim(), completed: !!x.completed, due: x.due || null, createdAt: x.createdAt || new Date().toISOString() })).filter(x=>x.text);
save(); render();
} else throw new Error('Invalid format');
}catch(err){ alert('Invalid JSON file'); }
};
reader.readAsText(file);
};
input.click();
}
function toggleTheme(){
const dark = document.documentElement.dataset.theme !== 'light';
if(dark){
document.documentElement.dataset.theme = 'light';
// simple light palette
document.documentElement.style.setProperty('--bg', '#f1f5f9');
document.documentElement.style.setProperty('--panel', '#ffffff');
document.documentElement.style.setProperty('--text', '#0f172a');
document.documentElement.style.setProperty('--muted', '#475569');
document.documentElement.style.setProperty('--ring', '#cbd5e1');
document.documentElement.style.setProperty('--chip', '#e2e8f0');
}else{
document.documentElement.dataset.theme = 'dark';
document.documentElement.style.cssText = '';
}
}
// Events
addBtn.addEventListener('click', addItem);
input.addEventListener('keydown', (e)=>{ if(e.key==='Enter') addItem(); });
clearCompletedBtn.addEventListener('click', clearCompleted);
exportBtn.addEventListener('click', exportJSON);
importBtn.addEventListener('click', importJSON);
themeBtn.addEventListener('click', toggleTheme);
filterTabs.forEach(tab=> tab.addEventListener('click', ()=> setFilter(tab.dataset.filter)) );
// Init
render();
})();
</script>
</body>
</html>