-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainjs.js
229 lines (196 loc) · 7.99 KB
/
mainjs.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
var todoTitleInput = document.querySelector('#todo-title-input');
var todoAddBtn = document.querySelector('#todo-add-btn');
var todoList = document.querySelector('#todo-list')
var todos = JSON.parse(localStorage.getItem('todos'));
// Constructor Class
function todoItemTemplate(title, done = false){
this.title = title;
this.done = done;
};
// messages DOM Selection for the manipulation later in the javascript document
var startEditingMsg = document.getElementById('start-editing');
var savedMsg = document.getElementById('saved');
var deletedMsg = document.getElementById('deleted');
var startEditingMsgST, savedMsgST, deletedMsgST;
function startEditingMsgSTF(){
startEditingMsgST = setTimeout(function(){
startEditingMsg.style.opacity = '0';
}, 8000);
}
function savedMsgSTF(){
savedMsgST = setTimeout(function(){
savedMsg.style.opacity = '0';
}, 5000);
}
function deletedMsgSTF(){
deletedMsgST = setTimeout(function(){
deletedMsg.style.opacity = '0';
}, 5000);
}
function startEditingMsgCTF(){
clearTimeout(startEditingMsgST);
}
function savedMsgCTF(){
clearTimeout(savedMsgST);
}
function deletedMsgCTF(){
clearTimeout(deletedMsgST);
}
if(todos !== null){
todos.forEach(todo => {
addListItem(todo['title']);
if(todo['done']){
todoList.children[todos.indexOf(todo)].classList.add('done-assistance');
todoList.children[todos.indexOf(todo)].firstElementChild.classList.add('done');
}
});
}else if(todos === null){
todos = new Array();
localStorage.setItem('todos', JSON.stringify(todos));
};
// Add event listeners
todoAddBtn.addEventListener('click', (e) => {
e.preventDefault();
if(todoTitleInput.value !== ""){
var todos = JSON.parse(localStorage.getItem('todos'));
todos.push(new todoItemTemplate(todoTitleInput.value));
localStorage.setItem('todos', JSON.stringify(todos));
addListItem(todoTitleInput.value);
}
todoTitleInput.value = "";
})
// console.log(todos);
function addListItem(todoTitleValue){
// creating the list item
var todoListItem = document.createElement('li');
todoListItem.classList.add('todo-list-item');
// creating the todo list title
var todoListTitle = document.createElement('h1');
todoListTitle.classList.add('todo-list-title');
todoListItem.appendChild(todoListTitle);
todoListTitle.innerText = todoTitleValue;
// creating div for buttons
var listBtns = document.createElement('div');
listBtns.classList.add('list-btns');
var editBtn = document.createElement('button');
editBtn.classList.add('edit-btn');
listBtns.appendChild(editBtn);
editBtn.innerHTML = '<i class="fa fa-edit"></i>';
var updateBtn = document.createElement('button');
updateBtn.classList.add('update-btn');
listBtns.appendChild(updateBtn);
updateBtn.innerHTML = '<i class="fa fa-save"></i>';
var doneBtn = document.createElement('button');
doneBtn.classList.add('done-btn');
listBtns.appendChild(doneBtn);
doneBtn.innerHTML = '<i class="fa fa-check"></i>';
var dltBtn = document.createElement('button');
dltBtn.classList.add('dlt-btn');
listBtns.appendChild(dltBtn);
dltBtn.innerHTML = '<i class="fa fa-trash"></i>';
todoListItem.appendChild(listBtns);
// appending the entire list item inside the todo list
todoList.appendChild(todoListItem);
// console.log(todoListItem);
};
todoList.addEventListener('click', (e)=> {
if(e.target.classList[0] == "edit-btn"){
// if done is true, cant edit
var reqIndex;
for (var i = 0; i < todoList.children.length; i++){
if(e.target.parentElement.parentElement === todoList.children[i]){
reqIndex = i;
break;
}
}
var todos = JSON.parse(localStorage.getItem('todos'));
if(todos[reqIndex]['done']){
window.alert('Error! Can\'t edit if done');
}else{
e.target.nextElementSibling.style.display = "flex";
e.target.style.display = "none";
e.target.parentElement.parentElement.firstElementChild.contentEditable = true;
e.target.nextElementSibling.nextElementSibling.disabled = true;
// start editing message alert
startEditingMsg.style.opacity = '1';
savedMsg.style.opacity = '0';
deletedMsg.style.opacity = '0';
startEditingMsgCTF();
savedMsgCTF();
deletedMsgCTF();
startEditingMsgSTF();
}
};
if(e.target.classList[0] == "update-btn"){
if(e.target.parentElement.parentElement.firstElementChild.innerText !== ""){
e.target.previousElementSibling.style.display = "flex";
e.target.style.display = "none";
e.target.parentElement.parentElement.firstElementChild.contentEditable = false;
e.target.nextElementSibling.disabled = false;
// localStorage work
var reqIndex;
for (var i = 0; i < todoList.children.length; i++){
if(e.target.parentElement.parentElement === todoList.children[i]){
reqIndex = i;
break;
}
}
var todos = JSON.parse(localStorage.getItem('todos'));
// localStorage.removeItem('todos');
todos[reqIndex] = new todoItemTemplate(e.target.parentElement.parentElement.firstElementChild.innerText);
localStorage.setItem('todos', JSON.stringify(todos));
// saved message alert
startEditingMsg.style.opacity = '0';
savedMsg.style.opacity = '1';
deletedMsg.style.opacity = '0';
startEditingMsgCTF();
savedMsgCTF();
deletedMsgCTF();
savedMsgSTF();
}
if(e.target.parentElement.parentElement.firstElementChild.innerText == ""){
window.alert('Error! You can\'t leave the space empty');
}
}
if(e.target.classList[0] == "done-btn"){
e.target.parentElement.parentElement.firstElementChild.classList.toggle('done');
e.target.parentElement.parentElement.classList.toggle('done-assistance');
// localStorage work
var reqIndex;
for (var i = 0; i < todoList.children.length; i++){
if(e.target.parentElement.parentElement === todoList.children[i]){
reqIndex = i;
break;
}
}
var todos = JSON.parse(localStorage.getItem('todos'));
if (todos[reqIndex]['done']){
todos[reqIndex] = new todoItemTemplate(e.target.parentElement.parentElement.firstElementChild.innerText, false);
}else{
todos[reqIndex] = new todoItemTemplate(e.target.parentElement.parentElement.firstElementChild.innerText, true);
};
localStorage.setItem('todos', JSON.stringify(todos));
}
if(e.target.classList[0] == "dlt-btn"){
// localStorage work
for (var i = 0; i < todoList.children.length; i++){
if(e.target.parentElement.parentElement === todoList.children[i]){
reqIndex = i;
break;
};
};
var todos = JSON.parse(localStorage.getItem('todos'));
todos.splice(reqIndex, 1);
localStorage.setItem('todos', JSON.stringify(todos));
//real function
e.target.parentElement.parentElement.remove();
// delete message alert
startEditingMsg.style.opacity = '0';
savedMsg.style.opacity = '0';
deletedMsg.style.opacity = '1';
startEditingMsgCTF();
savedMsgCTF();
deletedMsgCTF();
deletedMsgSTF();
}
});