-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
524 lines (474 loc) · 13.3 KB
/
app.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
window.onload = function () {
getCategories();
allTasks();
getDefaultTodos()
};
// General Functions
function getITem(key) {
return JSON.parse(localStorage.getItem(key));
}
function setITem(key, value) {
localStorage.setItem(key, JSON.stringify(value));
}
function delITem(key) {
localStorage.removeItem(key);
}
function getId(id) {
return document.getElementById(id);
}
function queryS(id) {
return document.querySelector(id);
}
function querySA(id) {
return document.querySelectorAll(id);
}
function noHyphen(str) {
let st = str.replaceAll("--", "");
return st;
}
function noSpace(str) {
let strn = str.replaceAll(" ", "");
return strn;
}
function counter(key) {
return Array.from(JSON.parse(localStorage.getItem(key))).length;
}
function message(message) {
alert(message);
}
// ****CATEGORIES****
// Add category
function AddCategory() {
const cName = noSpace(noHyphen(getId("category-name").value.toUpperCase()));
if (cName.length == 0 || cName.length > 16) {
message("min 1 max 16 character!");
} else {
const cList = getITem("toDos") || [];
if (cList.includes(cName)) {
alert("This category is already registered!");
getId("category-name").focus();
} else {
const newList = [cName, ...cList];
setITem("toDos", newList);
setITem(cName, []);
getId("category-name").value = "";
getId("category-name").focus();
getCategories();
}
}
}
getId("category-add").addEventListener("click", () => {
AddCategory();
});
// add category with enter
getId("category-name").addEventListener("keypress", (e) => {
if (e.key === "Enter") {
AddCategory();
}
});
// get categories
function getCategories() {
const addLinks = getId("addCategory");
const cList = getITem("toDos") || [];
addLinks.innerHTML = "";
for (let i = 0; i < cList.length; i++) {
let cName = cList[i];
let count = counter(cName);
addLinks.innerHTML += `
<li class="categoryItem">
<i onclick='deleteCategory("${i}")' class="fa-solid fa-xmark"></i>
<div class="category-link" id="${i}" onclick="sortItem('${cName}','${i}')" class="task">${cName} <span class="tasks">${count}</span></div>
</li> `;
}
}
getCategories()
// Delete category
function deleteCategory(id) {
const warnng = confirm("Do you want to delete?");
if (warnng) {
const cList = getITem("toDos");
const deleted = cList[id];
cList.splice(id, 1);
setITem("toDos", cList);
delITem(deleted);
getCategories();
allTasks();
getId("add-list").innerText = "";
getId("catgry-name").value = "";
getId("task-name").innerText = "";
getId("plus-add-item").classList.add("v-none");
!cList.length ? delITem("toDos") : "";
}
window.location.reload();
}
// ****LISTS****
// Add list
function addList() {
const cName = getId("catgry-name").value;
if (cName == "") {
alert("category name not found!");
$("#add-item").modal("hide");
}
const todoList = getITem(cName) || [];
const itemName = getId("item-name").value;
if (!itemName || itemName.replaceAll(" ", "") == "") {
alert("text not found!");
return;
}
const importance = getId("importance").value;
let icolor;
const checkbox = document.querySelectorAll(".check");
checkbox.forEach((check) => {
check.checked ? (icolor = check.value) : null;
});
const todo = {
itemName: itemName,
importance: importance,
completed: false,
color: icolor,
};
todoList.unshift(todo);
setITem(cName, todoList);
const todoListh = getITem(cName);
getId("item-name").value = "";
getId("item-name").focus();
cName ? sortItem(cName) : "";
getId("add-ok").classList.remove("visible");
getId("add-ok").innerText = "Added";
setTimeout(function () {
getId("add-ok").innerText = "";
getId("add-ok").classList.add("visible");
}, 2000);
getCategories();
}
//? keydown Event Handler
getId("item-name").addEventListener("keypress", (event) => {
if (event.key === "Enter") {
addList();
}
});
// get task item list
function sortItem(cName,id) {
let list = getITem(cName) || [];
getId("catgry-name").value = cName;
getId("plus-add-item").classList.remove("v-none");
getId("task-name").innerText = cName;
list = colorsGroup(list)
getId("add-list").innerHTML = "";
getId("add-list-done").innerHTML = "";
let itemList;
const todoList = getITem(cName) || [];
list.forEach((item) => {
const itemName = todoList[item].itemName;
const importance = todoList[item].importance;
const completed = todoList[item].completed;
let colors = ["color-1", "color-1", "color-2", "color-3", "color-4"];
const color = colors[todoList[item].color];
let importan = "";
importance == "important"
? (importan = 'important')
: (importan = "");
// select main list done?
completed ? itemList = getId("add-list-done") : itemList = getId("add-list");
itemList.innerHTML += ` <li id="${item}" class="list-group-item ${importan}">
<div class="left-items">
<div class="todo-indicator ${color}"></div>
<div class="widget-heading ${completed ? 'line-through' : null}">${itemName}</div>
</div>
<div class="left-items">
<input
type="checkbox"
id="3"
onclick="changeImportant('${item}--${cName}')"
class="task-check imp" ${importance == 'important' ? `checked` : ""}
title="important ?"/>
<input
type="checkbox"
id="3"
onclick="completeTask('${item}--${cName}')"
class="task-check cmpl" ${completed ? `checked` : ""}
title="compledet ?" />
<i onclick='deleteList("${item}--${cName}")' class="fa fa-trash"></i>
</div>
</li>
`;
});
getCategories();
allTasks();
colorMenu(id) // tıklanan kategorinin rengi
}
// colors group
function colorsGroup(list) {
let colors1 = list.reduce(
(acc, item, i) =>
item.color == 1 && item.completed == true ? [...acc, i] : [...acc],
[]
);
let colors1_c = list.reduce(
(acc, item, i) =>
item.color == 1 && item.completed == false ? [...acc, i] : [...acc],
[]
);
let colors2 = list.reduce(
(acc, item, i) =>
item.color == 2 && item.completed == true ? [...acc, i] : [...acc],
[]
);
let colors2_c = list.reduce(
(acc, item, i) =>
item.color == 2 && item.completed == false ? [...acc, i] : [...acc],
[]
);
let colors3 = list.reduce(
(acc, item, i) =>
item.color == 3 && item.completed == true ? [...acc, i] : [...acc],
[]
);
let colors3_c = list.reduce(
(acc, item, i) =>
item.color == 3 && item.completed == false ? [...acc, i] : [...acc],
[]
);
let noColors = list.reduce(
(acc, item, i) =>
item.color != 1 &&
item.color != 2 &&
item.color != 3 &&
item.completed == true
? [...acc, i]
: [...acc],
[]
);
let noColors_c = list.reduce(
(acc, item, i) =>
item.color != 1 &&
item.color != 2 &&
item.color != 3 &&
item.completed == false
? [...acc, i]
: [...acc],
[]
);
list = [
...colors1_c,
...colors2_c,
...colors3_c,
...noColors_c,
...colors1,
...colors2,
...colors3,
...noColors,
];
return list
}
// delete list item
function deleteList(item) {
let ok = confirm("Do you want to delete?");
if (ok) {
const pieces = item.split("--", 2);
let todo = getITem(pieces[1]);
let count = todo.length;
todo.splice(pieces[0], 1);
setITem(pieces[1], todo);
sortItem(pieces[1]);
count == 1
? (getId(
"add-list"
).innerHTML = `<h2 style="margin:auto">list ${pieces[1]} is empty</h2>`)
: null;
allTasks();
getCategories();
}
}
// completed task
function completeTask(item) {
const pieces = item.split("--", 2);
let todo = getITem(pieces[1]);
let last = todo[pieces[0]];
todo[pieces[0]].completed
? (last.completed = false)
: (last.completed = true);
todo.splice(pieces[0], 1, last);
setITem(pieces[1], todo);
sortItem(pieces[1]);
}
// change important task
function changeImportant(item) {
const pieces = item.split("--", 2);
let todo = getITem(pieces[1]);
let last = todo[pieces[0]];
todo[pieces[0]].importance
? (last.importance = "")
: (last.importance = "important");
todo.splice(pieces[0], 1, last);
setITem(pieces[1], todo);
sortItem(pieces[1]);
}
// All tasks number
function allTasks() {
let todos = getITem("toDos") || [];
let count = 0;
todos.forEach((todo) => {
count += getITem(todo).length;
});
getId("task").innerHTML = `<strong>${count}</strong>`;
}
// save json formating
function saveJson() {
let saveObj = [];
let todos = getITem("toDos") || [];
let count = 0;
todos.forEach((todo) => {
let todoValue = getITem(todo);
let todoPack = [];
todoPack = [todo, ...todoValue];
saveObj.unshift(todoPack);
});
// file setting
const text = JSON.stringify(saveObj);
const name = "toDo.json";
const type = "text/plain";
// create file
const a = document.createElement("a");
const file = new Blob([text], {
type: type,
});
a.href = URL.createObjectURL(file);
a.download = name;
document.body.appendChild(a);
a.click();
a.remove();
}
// upload json data modal
function getUploadJson() {
$("#json-upload").modal("show");
getId("json-upload").focus();
}
// get files for upload json data
let myFiles = getId("myFile")
function tik() {
getId("myFile").click()
}
myFiles.addEventListener("change", function (e) {
let filee = e.target.files[0];
let filem = new FileReader();
filem.onload = function () {
upLoadJson(JSON.parse(filem.result))
}
filem.readAsText(filee)
})
// upload json data
async function upLoadJson(data) {
await data.forEach((items) => {
let [, ...lists] = items; console.log(lists);
let oldLists = getITem(items[0]) || [];
let newLists = [...oldLists, ...lists];
setITem(items[0], newLists);
let newTasks;
let OldTasks = getITem("toDos") || [];
if (!OldTasks.includes(items[0])) {
newTasks = [...OldTasks, items[0]];
setITem("toDos", newTasks);
}
});
window.location.reload()
}
// Add default todo
function getDefaultTodos() {
if (!getITem("toDos")) {
let question = confirm("Upload sample tasks ?")
if (question === true) {
//one
setITem("toDos", ["SHOPPING"]);
let firstTodos = [
["Milk", "important", true, 1],
["Butter", "", false, 2],
["Magazine", "important", false, 1],
["Bred", "", true, 1],
["Ice", "important", false, 1],
["Cream", "important", false, 2],
["Jam", "important", false, 2],
];
firstTodos.forEach((ftd) => {
const [ToDo, important, booleans, color] = ftd;
const firstTodo = {
itemName: ToDo,
importance: important,
completed: booleans,
color: color,
};
let projects = getITem("SHOPPING") || [];
let firstTodos = [firstTodo, ...projects];
setITem("SHOPPING", firstTodos);
});
// two
let Categories = getITem("toDos");
let newCategoies = ["BIRDTHDAY", ...Categories];
setITem("toDos", newCategoies);
firstTodos = [
["Dinner reservation", "important", true, 1],
["Gifts", "", false, 2],
["Music Band", "important", false, 1],
["Dry cleaner", "important", false, 1],
["Haircut", "important", false, 2],
];
firstTodos.forEach((ftd) => {
const [ToDo, important, booleans, color] = ftd;
const firstTodo = {
itemName: ToDo,
importance: important,
completed: booleans,
color: color,
};
let projects = getITem("BIRDTHDAY") || [];
let firstTodos = [firstTodo, ...projects];
setITem("BIRDTHDAY", firstTodos);
});
//three
Categories = getITem("toDos");
newCategoies = ["DAILY", ...Categories];
setITem("toDos", newCategoies);
firstTodos = [
["Call mom", "important", true, 1],
["Pay bills", "", false, 2],
["Get doctor appointment", "important", false, 1],
["Go to parent meeting", "", true, 1],
["Passport check", "important", false, 1],
["Veterinary for dog", "important", false, 2],
["Mediaciton", "important", false, 2],
["Work js", "important", true, 2],
];
firstTodos.forEach((ftd) => {
const [ToDo, important, booleans, color] = ftd;
const firstTodo = {
itemName: ToDo,
importance: important,
completed: booleans,
color: color,
};
let projects = getITem("DAILY") || [];
let firstTodos = [firstTodo, ...projects];
setITem("DAILY", firstTodos);
});
window.location.reload();
}
}
}
// modal show setting
$(".modal").on("shown.bs.modal", function () {
$(this).find("[autofocus]").focus();
});
function colorMenu(id){
querySA(".categoryItem").forEach(item =>{
item.classList.remove("selected-color")
})
getId(id).parentElement.classList.add("selected-color") ;
}
// sidebar menu slide
document.getElementById("mobile-check").addEventListener("click", (e) => {
if (e.target.checked == true) {
e.target.parentElement.parentElement.classList.add("mobile-active")
} else {
e.target.parentElement.parentElement.classList.remove("mobile-active")
}
})