-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
198 lines (169 loc) · 5.1 KB
/
script.js
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
var date= new Date();
var month=date.getMonth();
var year=date.getFullYear();
//array of months
m=['JANUARY','FEBURARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER'];
//shows current time
function currentTime(){
var date= new Date();
var hour=date.getHours();
var min=date.getMinutes();
var sec=date.getSeconds();
document.getElementById('clock').innerHTML=pad(hour)+':'+pad(min)+':'+pad(sec);
var t= setTimeout(function(){
currentTime()
},1000);
}
//coverting into two digits
function pad(val){
var valstr=val+"";
if(val<10){
return 0+valstr;
}
else{
return valstr;
}
}
currentTime();
loadCalender(month,year);
//load previous month
function previous(){
document.getElementById('dates').innerHTML="";
year=(month===0)?year-1: year;
month=(month===0)? 11 : month-1;
document.getElementById('mname').textContent=m[month]+' '+year;
loadCalender(month,year);
}
//load next month
function next(){
document.getElementById('dates').innerHTML="";
year = (month==11)? year=year+1 : year;
month = (month + 1) % 12;
document.getElementById('mname').textContent=m[month]+' '+year;
loadCalender(month,year);
}
//to find no of days in a month
function daysInMonth(m,y){
return 32 - new Date(y,m,32).getDate();
}
//to find first day of a month
function firstDay(m,y){
return new Date(y,m,1).getDay();
}
//load calender
function loadCalender(m,y){
var num = daysInMonth(m, y);
var firstday = firstDay(m,y); // find where to start calendar day of week
console.log(firstday);
// create day without date
for(var i = 0; i < firstday; i++)
{
var d = document.createElement("div");
d.classList.add("date");
d.classList.add("blank");
document.getElementById("dates").appendChild(d);
}
//other days
for(var i = 0; i < num; i++)
{
var tdate= new Date().getDate();
var tmp = i + 1;
var d = document.createElement("div");
d.className = "date";
d.innerHTML = tmp;
document.getElementById("dates").appendChild(d);
}
}
//mark today date
var da=new Date().getDate();
const d=document.getElementsByClassName('date');
Array.from(d).forEach(function(e){
if(e.innerHTML==da){
e.style.backgroundColor='purple';
e.style.color='white';
}
});
//selecting Date
addEventListener('click',function(e){
if(e.target.className==='date'){
e.target.style.backgroundColor='teal';
e.target.style.color='white';
}
})
//open to-do list
var list=document.getElementById('todo');
function todo(){
console.log("tlist");
document.getElementById('overlay').style.display='block';
list.style.display= 'block';
}
//closes t-do list
function shut(){
console.log("clicked");
document.getElementById('overlay').style.display='none';
list.style.display='none';
}
//adding tasks in to-do list
const addTask=document.forms['addtask'];
addTask.addEventListener('submit',function(e){
e.preventDefault();
const val=addTask.querySelector('input[type="text"]').value;
const tdate=addTask.querySelector('input[type="date"]').value;
if(val===''){
alert('add some event');
}
else if(tdate===' '){
alert('Add Date');
}
else if(val==='' && tdate===''){
alert('Add Event and Date');
}
else{
const list=document.querySelector("#list");
const li=document.createElement("li");
const taskdate=document.createElement("span");
const taskName=document.createElement("span");
const del=document.createElement("span");
const space=document.createElement("span");
const chk=document.createElement("span")
del.textContent='Delete';
taskdate.textContent=tdate;
space.textContent=' ';
taskName.textContent=val;
chk.textContent='Done';
taskName.classList.add('name');
del.classList.add('delete');
del.onclick=remove;
chk.classList.add('complete');
chk.onclick=done;
li.appendChild(taskdate);
li.appendChild(space);
li.appendChild(taskName);
li.appendChild(del);
li.appendChild(chk);
list.appendChild(li);
localStorage.setItem(val,tdate);
addTask.querySelector('input[type="text"]').value="";
addTask.querySelector('input[type="date"]').value="";
}
});
//removing tasks from to-do list
function remove(){
addEventListener('click',function(e){
if(e.target.className==='delete'){
const li= e.target.parentElement;
li.parentNode.removeChild(li);
}
});
}
//Task Completed
function done(){
addEventListener('click',function(e){
console.log(e.target.className);
if(e.target.className==='complete'){
const li= e.target.parentElement;
li.style.textDecoration= 'line-through';
li.style.backgroundColor='lavenderblush';
}
});
}