-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
113 lines (87 loc) · 2.56 KB
/
Copy pathscript.js
File metadata and controls
113 lines (87 loc) · 2.56 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
let todos=JSON.parse(localStorage.getItem('todos'))||[];
let filter='all';
showTodos();
document.querySelector('#todo-input').addEventListener('keydown',function(e){
if(e.key==='Enter') addTodo();
});
function addTodo(){
let task=document.querySelector('#todo-input').value.trim();
let date=document.querySelector('#todo-date').value;
if(task===''){
alert('Enter a task');
return;
}
todos.push({task:task,date:date,done:false});
save();
document.querySelector('#todo-input').value='';
document.querySelector('#todo-date').value='';
showTodos();
}
function save(){
localStorage.setItem('todos',JSON.stringify(todos));
}
function deleteTodo(i){
todos.splice(i,1);
save();
showTodos();
}
function markTodo(i){
todos[i].done=!todos[i].done;
save();
showTodos();
}
function changeFilter(f){
filter=f;
showTodos();
}
function clearDone(){
todos=todos.filter(t=>!t.done);
save();
showTodos();
}
function getTitle(date){
if(date==='') return 'No Date';
let today=new Date().toISOString().split('T')[0];
let y=new Date();
y.setDate(y.getDate()-1);
let yesterday=y.toISOString().split('T')[0];
if(date===today) return 'Today';
if(date===yesterday) return 'Yesterday';
return date;
}
function showTodos(){
let box=document.querySelector('#todo-list');
let html='';
let list=todos.filter(t=>{
if(filter==='pending') return !t.done;
if(filter==='done') return t.done;
return true;
});
document.querySelector('#count').innerText='Total: '+todos.length;
if(list.length===0){
box.innerHTML='<p class="empty">No task found</p>';
return;
}
let group={};
list.forEach(t=>{
let title=getTitle(t.date);
if(!group[title]) group[title]=[];
group[title].push(t);
});
for(let title in group){
html+=`<h3 class="date-title">${title}</h3>`;
group[title].forEach(t=>{
let i=todos.indexOf(t);
html+=`
<div class="todo ${t.done?'done':''}">
<input type="checkbox" ${t.done?'checked':''} onclick="markTodo(${i})">
<div class="task">
${t.task}<br>
<small>${t.date || 'No date'}</small>
</div>
<button class="del" onclick="deleteTodo(${i})">Del</button>
</div>`;
});
}
box.innerHTML=html;
}