-
Notifications
You must be signed in to change notification settings - Fork 4
/
script2.js
367 lines (317 loc) · 10.8 KB
/
script2.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
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
let allTasks = [];
let innerTasks = [];
let count = 1; //for identical collapse id
const upperLi = document.getElementById("upperLi");
const innerLi = document.getElementById("innerLi");
const mainForm = document.getElementById("addingForm");
const mainInput = addingForm.children[0];
const mainList = document.getElementById("mainList");
const delAll = document.getElementById("delAll");
eventListeners();
loadLS();
loadInnerTasks();
// all Listeners
function eventListeners() {
//submit event
mainForm.addEventListener("submit", addNewItem);
//task stufs
mainList.addEventListener("click", taskStufs);
//delete all tasks
delAll.addEventListener("click", deleteAll);
}
//to creat new task
function createNewTask(text, check) {
//to clone upper visible li in html page
const newLi = upperLi.cloneNode(true);
//making visible the new li
newLi.classList.remove("d-none");
//removing id
newLi.setAttribute("id", "");
newLi.children[0].setAttribute("href", `#collapse${count}`); //identical colapseId
newLi.children[0].classList.remove("line-throught");
newLi.children[0].innerText = text;
if (check === true) {
newLi.children[0].classList.add("line-throught");
} else {
newLi.children[0].classList.remove("line-throught");
}
newLi.children[3].setAttribute("id", `collapse${count}`);
mainList.children[0].appendChild(newLi);
count++; //for different value of collapseId
mainInput.value = "";
}
//add New Task
function addNewItem(e) {
e.preventDefault();
//entered text
const taskText = mainInput.value;
if (taskText != "") {
allTasks = JSON.parse(localStorage.getItem("allTasks"));
let c = false;
allTasks.forEach(function (item) {
if (item.text == taskText) {
c = true;
alert("Task Already Added");
}
});
if (c == false) {
createNewTask(taskText);
saveLS(taskText, 1);
}
}
}
//to add Inner Tasks
function addInnerTask(upperText, mainText, check) {
//to clone inner visible li from html page
let tempLi = innerLi.cloneNode(true);
tempLi.setAttribute("id", "");
//making visible check and delete icons.
tempLi.childNodes.forEach(function (item, index) {
if (index == 3 || index == 5) {
item.classList.remove("d-none");
}
});
//to add the value to new li text
tempLi.children[0].innerText = mainText;
//check
if (check == true) {
tempLi.children[0].classList.add("line-throught");
} else {
tempLi.children[0].classList.remove("line-throught");
}
//adding new inner task to last of the list before form
let tempList;
for (let i = 1; i < mainList.children[0].children.length; i++) {
if (upperText == mainList.children[0].children[i].children[0].innerText) {
tempList = mainList.children[0].children[i];
break;
}
}
//adding inner to upper
tempList.lastElementChild.firstElementChild.firstElementChild.insertBefore(
tempLi,
tempList.lastElementChild.firstElementChild.firstElementChild
.lastElementChild
);
}
//delete and check a task - add new inner task
function taskStufs(e) {
e.preventDefault();
//delete item
if (e.target.classList.contains("delete-item")) {
//remove LS
const text = e.target.parentElement.children[0].innerText;
saveLS(text, 0);
//remove inner tasks of it
let tempInnerList = JSON.parse(localStorage.getItem("innerTasks"));
tempInnerList.forEach(function (item) {
if (text == item.upperTaskText) {
saveInnerTasks(item.mainText, 0, text);
}
});
//remove from page
e.target.parentElement.remove();
// to check an inner task
} else if (e.target.classList.contains("delete-inner-item")) {
const upperText =
e.target.parentElement.parentElement.parentElement.parentElement
.parentElement.children[0].innerText;
//removing LS
saveInnerTasks(e.target.parentElement.children[0].innerText, 0, upperText);
//Removing Page
e.target.parentElement.remove();
} else if (e.target.classList.contains("check")) {
let tmpList = JSON.parse(localStorage.getItem("allTasks"));
if (
e.target.parentElement.children[0].classList.contains("line-throught")
) {
for (let i = 0; i < tmpList.length; i++) {
if (tmpList[i].text == e.target.parentElement.children[0].innerText) {
tmpList[i].check = false;
localStorage.setItem("allTasks", JSON.stringify(tmpList));
break;
}
}
e.target.parentElement.children[0].classList.remove("line-throught");
} else {
for (let i = 0; i < tmpList.length; i++) {
if (tmpList[i].text == e.target.parentElement.children[0].innerText) {
tmpList[i].check = true;
localStorage.setItem("allTasks", JSON.stringify(tmpList));
break;
}
}
e.target.parentElement.children[0].classList.add("line-throught");
}
//adding inner task with button
} else if (e.target.classList.contains("inner-check")) {
let tmp = JSON.parse(localStorage.getItem("innerTasks"));
const upperText =
e.target.parentElement.parentElement.parentElement.parentElement
.parentElement.children[0].innerText;
if (
e.target.parentElement.children[0].classList.contains("line-throught")
) {
tmp.forEach(function (item) {
if (
item.mainText == e.target.parentElement.children[0].innerText &&
item.upperTaskText == upperText
) {
item.check = false;
localStorage.setItem("innerTasks", JSON.stringify(tmp));
}
});
e.target.parentElement.children[0].classList.remove("line-throught");
} else {
tmp.forEach(function (item) {
if (
item.mainText == e.target.parentElement.children[0].innerText &&
item.upperTaskText == upperText
) {
item.check = true;
localStorage.setItem("innerTasks", JSON.stringify(tmp));
}
});
e.target.parentElement.children[0].classList.add("line-throught");
}
} else if (e.target.classList.contains("btn-add-inner-task")) {
//getting value of input
const text = e.target.parentElement.childNodes[1].value;
if (text != "") {
innerTasks = JSON.parse(localStorage.getItem("innerTasks"));
//getting upper text
const upperText =
e.target.parentElement.parentElement.parentElement.parentElement
.parentElement.parentElement.children[0].innerText;
let c = true;
innerTasks.forEach(function (item) {
if (item.mainText == text && item.upperTaskText == upperText) {
c = false;
alert("Task Already Added");
}
});
if (c == true) {
//adding innerTask List
innerTasks.push({
upperTaskText: upperText,
mainText: text,
check: false,
});
addInnerTask(upperText, text);
saveInnerTasks(innerTasks[innerTasks.length - 1], 1);
}
}
//clear input
e.target.parentElement.childNodes[1].value = "";
//adding inner task with i tag that in the button
//same things with adding with btn
} else if (e.target.classList.contains("i-add-inner-task")) {
let text = e.target.parentElement.parentElement.childNodes[1].value;
if (text != "") {
//getting upper text
const upperText =
e.target.parentElement.parentElement.parentElement.parentElement
.parentElement.parentElement.parentElement.children[0].innerText;
//adding innerTask List
innerTasks.push({ upperTaskText: upperText, mainText: text });
addInnerTask(upperText, text);
saveInnerTasks(innerTasks[innerTasks.length - 1], 1);
//clear input
e.target.parentElement.parentElement.childNodes[1].value = "";
}
}
}
//delete all
function deleteAll(e) {
e.preventDefault();
let list =
e.target.parentElement.parentElement.children[1].children[0].childNodes[1]
.children;
while (list.length != 1) {
//block the remove main invisible li in html
list[list.length - 1].remove();
}
saveLS("", -1);
saveInnerTasks("", -1);
}
//loading Main Tasks
function loadLS() {
if (localStorage.getItem("allTasks") === null) {
localStorage.setItem("allTasks", JSON.stringify(allTasks));
} else {
allTasks = JSON.parse(localStorage.getItem("allTasks"));
allTasks.forEach(function (item) {
if (item != "") {
createNewTask(item.text, item.check);
}
});
}
}
//loading Inner Tasks
function loadInnerTasks() {
if (localStorage.getItem("innerTasks") === null) {
localStorage.setItem("innerTasks", JSON.stringify(innerTasks));
} else {
innerTasks = JSON.parse(localStorage.getItem("innerTasks"));
innerTasks.forEach(function (item) {
if (item.mainText != "") {
addInnerTask(item.upperTaskText, item.mainText, item.check);
}
});
}
}
//saving Main Tasks
function saveLS(text, index) {
//if index = 1 add, = 0 remove, =-1 remove all
if (index == -1) {
localStorage.removeItem("allTasks");
allTasks.length = 0;
loadLS();
} else if (index == 0) {
const tmp = JSON.parse(localStorage.getItem("allTasks"));
let c;
tmp.forEach(function (item, ind) {
if (item.text == text) c = ind;
});
tmp.splice(c, 1);
localStorage.setItem("allTasks", JSON.stringify(tmp));
//delete inner tasks whose upper text that
const tmp2 = JSON.parse(localStorage.getItem("innerTasks"));
for (let i = 0; i < tmp2.length; i++) {
if (tmp2[i].upperTaskText == text) {
saveInnerTasks(tmp[i], 0);
}
}
} else if (index == 1) {
const tmp = JSON.parse(localStorage.getItem("allTasks"));
tmp.push({ text: text, check: false });
localStorage.setItem("allTasks", JSON.stringify(tmp));
}
}
//saving Inner Tasks
function saveInnerTasks(item, index, upperText = "") {
//if index = 1 add, = 0 remove, =-1 remove all, 2 check, =3 removeCheck
if (index == -1) {
localStorage.removeItem("innerTasks");
innerTasks.length = 0;
loadInnerTasks();
} else if (index == 0) {
const tmp = JSON.parse(localStorage.getItem("innerTasks"));
let cont = false;
let c;
tmp.forEach(function (e, ind) {
if (e.mainText == item && e.upperTaskText == upperText) {
c = ind;
cont = true;
}
});
if (cont) {
tmp.splice(c, 1);
localStorage.setItem("innerTasks", JSON.stringify(tmp));
}
} else if (index == 1) {
const tmp = JSON.parse(localStorage.getItem("innerTasks"));
tmp.push(item);
localStorage.setItem("innerTasks", JSON.stringify(tmp));
}
}