-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
154 lines (135 loc) · 3.74 KB
/
Copy pathscript.js
File metadata and controls
154 lines (135 loc) · 3.74 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
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
// when document loads
let todos = [];
$(document).ready(function () {
if (localStorage.getItem("todos") !== null) {
const t = JSON.parse(localStorage.getItem("todos"));
if (t.length !== 0) {
for (i = 0; i < t.length; i++) {
todos.push(t[i]);
}
}
}
render();
});
//for submitted
$("form").submit(function (e) {
e.preventDefault();
saveAndAddDiv();
clearInput();
});
function saveAndAddDiv(index) {
let title = $("#task").val();
let priority = $("#priority").val();
let isDone = false;
let taskId = getRandomId();
if (title !== "") {
todos.push({
title: title,
priority: priority,
status: isDone,
id: taskId,
});
insertToLocal(todos);
addTodoDiv(title, priority, taskId);
showSuccessAlert();
} else {
showDangerAlert();
}
}
function insertToLocal(arr) {
let stringArr = JSON.stringify(arr);
localStorage.setItem("todos", stringArr);
}
function getRandomId() {
const d = Date.now();
return d;
}
// render
function clearInput() {
$("#task").val("");
}
// delete button working
$(document).on("click", "#delBtn", function () {
let ind = $(this).parent().index();
remove(ind);
$(this).parent().fadeOut();
$(this).parent().remove();
});
// done button working
$(document).on("click", "#statusBtn", function () {
console.log("done clicked");
let index = $(this).parent().index();
if (todos[index].status === false) {
todos[index].status = true;
console.log(todos[index].status);
insertToLocal(todos);
$(this).parent().css("text-decoration", "line-through");
$(this).parent().css("opacity","0.4")
} else {
todos[index].status = false;
console.log(todos[index].status);
insertToLocal(todos);
$(this).parent().css("text-decoration", "none");
}
});
function render() {
if (todos.length === 0) {
console.log("nothing");
} else {
for (i = 0; i < todos.length; i++) {
let taskid = todos[i].id;
let title = todos[i].title;
let priority = todos[i].priority;
let status = todos[i].status;
if (status === false) {
let div = `<div id="${taskid}" class="todo"> <h3>${title}</h3><p>${priority}</p><p id="statusBtn">Done</p><p id="delBtn">delete</p></div>`;
$(".todo-list").append(div);
} else {
let div = `<div style ="text-decoration:line-through" id="${taskid}" class="todo"> <h3>${title}</h3><p>${priority}</p><p id="statusBtn">Done</p><p id="delBtn">delete</p></div>`;
$(".todo-list").append(div);
}
}
}
}
function showDangerAlert() {
$(".alert-danger").show();
setTimeout(function () {
$(".alert-danger").hide();
}, 2000);
}
function showSuccessAlert() {
$(".alert-success").show();
setTimeout(function () {
$(".alert-success").hide();
}, 2000);
}
function addTodoDiv(title, priority, taskId) {
let div = `<div id="${taskId}" class="todo"> <h3>${title}</h3><p>${priority}</p><p id="statusBtn">Done</p><p id="delBtn">delete</p></div>`;
$(".todo-list").append(div);
}
function remove(n) {
console.log(n);
todos.splice(n, 1);
console.log("item removes");
insertToLocal(todos);
console.log(todos);
}
$("#sortBtn").click(function () {
console.log("sort clicked");
todos.sort(function (a, b) {
if (a.title > b.title) {
return 1;
}
if (a.title < b.title) {
return -1;
}
});
$(".todo-list").empty();
insertToLocal(todos);
render();
});
$(document).on("dblclick", ".todo", function () {
let index = $(this).index();
$("#task").val(todos[index].title);
$("#priority").val(todos[index].priority);
});