-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
214 lines (183 loc) · 7.32 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var i = 0;
document.addEventListener('DOMContentLoaded', function () {
var datePicker = document.getElementById('datePicker');
// got to know that the date fecthed is not in the normal format and does not have splits
var currentDate = new Date().toISOString().slice(0, 10);
// Yup the upper fn is take from https://stackoverflow.com/questions/1531093/get-current-date-in-yyyy-mm-dd-
datePicker.setAttribute('min', currentDate);
});
//Hard stuff all done by me is here
document.getElementById('forms').addEventListener('submit', function (event) {
event.preventDefault();
var mainText = document.getElementById('main-text').value;
var date = document.getElementById('datePicker').value;
if (mainText === '') {
alert('You have to enter something');
} else {
var uli1 = document.getElementById('uli1');
var li = document.createElement('li');
i++;
li.id = 'p' + i;
uli1.appendChild(li);
var doc = document.getElementById(`p${i}`);
var divs = document.createElement('div');
divs.style.display = 'flex';
divs.style.gap = "10px";
var int = document.createElement('input');
int.className = "Siri";
int.id = 'doom' + i;
int.type = 'checkbox';
var newdiv = document.createElement('div');
var text = document.createElement('p');
text.className = "Siri1";
text.innerHTML = mainText;
document.getElementById('main-text').value = '';
newdiv.className = 'stars';
var button = document.createElement('button');
newdiv.style.width = '50vw';
button.innerHTML = '✖';
button.style.position = 'relative';
button.style.alignSelf = 'flex-end';
button.className = "Button";
var ebutton = document.createElement('button');
ebutton.innerHTML = 'Edit';
ebutton.className = "edit-button";
ebutton.id = 'b' + i;
ebutton.style.position = 'relative';
ebutton.style.alignSelf = 'flex-end';
doc.appendChild(divs);
divs.appendChild(int);
divs.appendChild(newdiv);
newdiv.appendChild(text);
divs.appendChild(ebutton)
divs.appendChild(button);
if (date === '') {
var duedate = document.createElement('p');
duedate.className = "duedate";
duedate.innerHTML = 'No Due Date Selected';
duedate.style.fontSize = '20px';
li.appendChild(duedate);
} else {
var duedate = document.createElement('p');
duedate.innerHTML = 'Due date: ' + ' ' + date;
duedate.className = "duedate";
duedate.style.fontSize = '20px';
li.appendChild(duedate);
}
saveData();
}
});
document.getElementById('uli1').addEventListener('click', function (event) {
var target = event.target;
if (target.type === 'checkbox') {
handleButtonClick(target);
saveData();
}
if (target.className === 'edit-button') {
var li = target.closest('li');
var text = li.querySelector('.Siri1');
var date = li.querySelector('.duedate');
var cdate ='';
var newText = prompt('Edit task:', text.innerHTML);
var newDate = prompt('Edit Date:', cdate.innerHTML);
if (newText !== null) {
text.innerHTML = newText;
if (newDate !== null) {
date.innerHTML = 'Due Date:' + ' ' + newDate;
}
saveData();
}
}
if (target.className === 'Button') {
var li = target.closest('li');
var uli1 = document.getElementById('uli1');
var date = li.querySelector('.duedate');
uli1.removeChild(li);
if (date) {
uli1.removeChild(date);
}
saveData();
}
});
function handleButtonClick(checkbox) {
var texty = checkbox.nextElementSibling;
texty.style.textDecoration = checkbox.checked ? "line-through" : "none";
saveData();
}
// learned from Chat-Gpt for how to work with local storage in JS
function saveData() {
var tasks = [];
var uli1 = document.getElementById('uli1');
var listItems = uli1.querySelectorAll('li');
listItems.forEach(function (li) {
var taskText = li.querySelector('.Siri1').innerHTML;
var isChecked = li.querySelector('.Siri').checked;
var dateElement = li.querySelector('.duedate');
var date = dateElement ? dateElement.innerHTML : '';
tasks.push({ text: taskText, checked: isChecked, date: date });
});
localStorage.setItem('tasks', JSON.stringify(tasks));
}
function loadData() {
var uli1 = document.getElementById('uli1');
var savedTasks = localStorage.getItem('tasks');
if (savedTasks) {
var tasks = JSON.parse(savedTasks);
tasks.forEach(function (task) {
var li = document.createElement('li');
var divs = document.createElement('div');
divs.style.display = 'flex';
divs.style.gap = "10px";
uli1.appendChild(li);
li.appendChild(divs);
var int = document.createElement('input');
int.className = "Siri";
int.type = 'checkbox';
int.checked = task.checked;
var newdiv = document.createElement('div');
var text = document.createElement('p');
text.className = "Siri1";
text.innerHTML = task.text;
newdiv.className = 'stars';
var button = document.createElement('button');
newdiv.style.width = '50vw';
button.innerHTML = '✖';
button.style.position = 'relative';
button.style.alignSelf = 'flex-end';
button.className = "Button";
var ebutton = document.createElement('button');
ebutton.innerHTML = 'Edit';
ebutton.className = "edit-button";
ebutton.style.position = 'relative';
ebutton.style.alignSelf = 'flex-end';
divs.appendChild(int);
divs.appendChild(newdiv);
newdiv.appendChild(text);
divs.appendChild(ebutton)
divs.appendChild(button);
if (task.date === '') {
var duedate = document.createElement('p');
duedate.className = "duedate";
duedate.innerHTML = 'No Due Date Selected';
duedate.style.fontSize = '20px';
li.appendChild(duedate);
} else {
var duedate = document.createElement('p');
duedate.innerHTML = task.date;
duedate.className = "duedate";
duedate.style.fontSize = '20px';
li.appendChild(duedate);
}
int.addEventListener('change', function () {
handleButtonClick(int);
saveData();
});
button.addEventListener('click', function () {
uli1.removeChild(li);
saveData();
});
text.style.textDecoration = int.checked ? "line-through" : "none";
});
}
}
loadData();