-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
141 lines (127 loc) · 4.42 KB
/
todo.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
let newTodo = document.querySelector(".inp");
let infoButton = document.querySelector(".infoButton");
let submit = document.querySelector(".submit");
let ul = document.querySelector("ul");
let list;
const url = "https://script.google.com/macros/s/AKfycbwYvYtFQvf0eqP8ybtvjurQOhtOxAddO-W_7Eu0v1i1d-TP42j5bGUGmoFV52fiLTXvkg/exec";
if (JSON.parse(localStorage.getItem("list")) == null) {
list = [];
ul.innerHTML = "<h4>Your Todo List is empty!</h4>";
} else {
list = JSON.parse(localStorage.getItem("list"));
if (list.length == 0) {
ul.innerHTML = "<h4>Your Todo List is empty!</h4>";
}
}
submit.addEventListener("click", function () {
// alert(newTodo.value);
if (newTodo.value != "") {
if(localStorage.getItem("password")){
fetch(url+`?password=${localStorage.getItem("password")}&type=post&todo=${newTodo.value}&dash=false`)
.then((response) => {
return response.json();
})
.then((todoData) => {
newTodo.value = "";
list = [];
displayTodo();
setTodoSavedFromGoogleSheetsToList();
});
return;
}
list.push({ todo: newTodo.value, dash: false });
localStorage.setItem("list", JSON.stringify(list));
while (ul.hasChildNodes()) {
ul.removeChild(ul.firstChild);
}
displayTodo();
newTodo.value = "";
}
});
newTodo.addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
event.preventDefault();
submit.click();
}
});
function displayTodo() {
if (list.length == 0) {
ul.innerHTML = "<h4>Your Todo List is empty!</h4>";
} else {
ul.innerHTML = "";
}
for (let i = 0; i < list.length; i++) {
let newli = document.createElement("li");
newli.classList.add("list-group-item");
if ("hash" in list[i]){
newli.id = list[i].hash;
}
newli.innerText = list[i].todo;
let tick = document.createElement("span");
tick.innerHTML = "<button type='button' class='btn btn-outline-primary space '> <svg width='1em' height='1em' viewBox='0 0 16 16' class='bi bi-check2' fill='currentColor' xmlns='http://www.w3.org/2000/svg'> <path fill-rule='evenodd' d='M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0z'></path></svg></button>"
newli.classList.add("separate");
newli.appendChild(tick);
ul.appendChild(newli);
if (list[i].dash) {
newli.classList.add("strikeThrough");
} else {
newli.classList.remove("strikeThrough");
}
tick.addEventListener("click", () => done(newli));
newli.addEventListener("click", () => strike(newli));
}
}
function strike(li) {
li.classList.toggle("strikeThrough");
for (let i = 0; i < list.length; i++) {
if (li.innerText == list[i].todo) {
if (li.classList.contains("strikeThrough")) {
list[i].dash = true;
} else {
list[i].dash = false;
}
break;
}
}
localStorage.setItem("list", JSON.stringify(list));
}
function done(li) {
for (let i = 0; i < list.length; i++) {
if (li.innerText == list[i].todo) {
list.splice(i, 1);
break;
}
}
while (ul.hasChildNodes()) {
ul.removeChild(ul.firstChild);
}
localStorage.setItem("list", JSON.stringify(list));
displayTodo();
}
infoButton.addEventListener("click", function(){
let password = prompt("Please Enter password, d to delete:");
localStorage.setItem("password", password);
if (password == "d"){
localStorage.removeItem("password");
list = JSON.parse(localStorage.getItem("list"));
displayTodo();
return;
}
setTodoSavedFromGoogleSheetsToList();
})
if (localStorage.getItem("password")){
setTodoSavedFromGoogleSheetsToList();
} else {
// display localStorage todo's only if password not present
displayTodo();
}
async function setTodoSavedFromGoogleSheetsToList(){
fetch(url+`?password=${localStorage.getItem("password")}&type=get`)
.then((response) => {
return response.json();
})
.then((todoData) => {
list = todoData.data;
displayTodo();
});
}