-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
203 lines (164 loc) · 6.09 KB
/
Copy pathmain.js
File metadata and controls
203 lines (164 loc) · 6.09 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
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
// Selectors
const toDoInput = document.querySelector('.todo-input');
const toDoBtn = document.querySelector('.todo-btn');
const toDoList = document.querySelector('.todo-list');
const standardTheme = document.querySelector('.standard-theme');
const lightTheme = document.querySelector('.light-theme');
const darkerTheme = document.querySelector('.darker-theme');
// Event Listeners
toDoBtn.addEventListener('click', addToDo);
toDoList.addEventListener('click', deletecheck);
document.addEventListener("DOMContentLoaded", getTodos);
standardTheme.addEventListener('click', () => changeTheme('standard'));
lightTheme.addEventListener('click', () => changeTheme('light'));
darkerTheme.addEventListener('click', () => changeTheme('darker'));
// Check if one theme has been set previously and apply it (or std theme if not found):
let savedTheme = localStorage.getItem('savedTheme');
savedTheme === null ?
changeTheme('standard')
: changeTheme(localStorage.getItem('savedTheme'));
// Functions;
function addToDo(event) {
// Prevents form from submitting / Prevents form from relaoding;
event.preventDefault();
// toDo DIV;
const toDoDiv = document.createElement("div");
toDoDiv.classList.add('todo', `${savedTheme}-todo`);
// Create LI
const newToDo = document.createElement('li');
if (toDoInput.value === '') {
alert("You must write something!");
}
else {
// newToDo.innerText = "hey";
newToDo.innerText = toDoInput.value;
newToDo.classList.add('todo-item');
toDoDiv.appendChild(newToDo);
// Adding to local storage;
savelocal(toDoInput.value);
// check btn;
const checked = document.createElement('button');
checked.innerHTML = '<i class="fas fa-check"></i>';
checked.classList.add('check-btn', `${savedTheme}-button`);
toDoDiv.appendChild(checked);
// delete btn;
const deleted = document.createElement('button');
deleted.innerHTML = '<i class="fas fa-trash"></i>';
deleted.classList.add('delete-btn', `${savedTheme}-button`);
toDoDiv.appendChild(deleted);
// Append to list;
toDoList.appendChild(toDoDiv);
// CLearing the input;
toDoInput.value = '';
}
}
function deletecheck(event){
// console.log(event.target);
const item = event.target;
// delete
if(item.classList[0] === 'delete-btn')
{
// item.parentElement.remove();
// animation
item.parentElement.classList.add("fall");
//removing local todos;
removeLocalTodos(item.parentElement);
item.parentElement.addEventListener('transitionend', function(){
item.parentElement.remove();
})
}
// check
if(item.classList[0] === 'check-btn')
{
item.parentElement.classList.toggle("completed");
}
}
// Saving to local storage:
function savelocal(todo){
//Check: if item/s are there;
let todos;
if(localStorage.getItem('todos') === null) {
todos = [];
}
else {
todos = JSON.parse(localStorage.getItem('todos'));
}
todos.push(todo);
localStorage.setItem('todos', JSON.stringify(todos));
}
function getTodos() {
//Check: if item/s are there;
let todos;
if(localStorage.getItem('todos') === null) {
todos = [];
}
else {
todos = JSON.parse(localStorage.getItem('todos'));
}
todos.forEach(function(todo) {
// toDo DIV;
const toDoDiv = document.createElement("div");
toDoDiv.classList.add("todo", `${savedTheme}-todo`);
// Create LI
const newToDo = document.createElement('li');
newToDo.innerText = todo;
newToDo.classList.add('todo-item');
toDoDiv.appendChild(newToDo);
// check btn;
const checked = document.createElement('button');
checked.innerHTML = '<i class="fas fa-check"></i>';
checked.classList.add("check-btn", `${savedTheme}-button`);
toDoDiv.appendChild(checked);
// delete btn;
const deleted = document.createElement('button');
deleted.innerHTML = '<i class="fas fa-trash"></i>';
deleted.classList.add("delete-btn", `${savedTheme}-button`);
toDoDiv.appendChild(deleted);
// Append to list;
toDoList.appendChild(toDoDiv);
});
}
function removeLocalTodos(todo){
//Check: if item/s are there;
let todos;
if(localStorage.getItem('todos') === null) {
todos = [];
}
else {
todos = JSON.parse(localStorage.getItem('todos'));
}
const todoIndex = todos.indexOf(todo.children[0].innerText);
// console.log(todoIndex);
todos.splice(todoIndex, 1);
// console.log(todos);
localStorage.setItem('todos', JSON.stringify(todos));
}
// Change theme function:
function changeTheme(color) {
localStorage.setItem('savedTheme', color);
savedTheme = localStorage.getItem('savedTheme');
document.body.className = color;
// Change blinking cursor for darker theme:
color === 'darker' ?
document.getElementById('title').classList.add('darker-title')
: document.getElementById('title').classList.remove('darker-title');
document.querySelector('input').className = `${color}-input`;
// Change todo color without changing their status (completed or not):
document.querySelectorAll('.todo').forEach(todo => {
Array.from(todo.classList).some(item => item === 'completed') ?
todo.className = `todo ${color}-todo completed`
: todo.className = `todo ${color}-todo`;
});
// Change buttons color according to their type (todo, check or delete):
document.querySelectorAll('button').forEach(button => {
Array.from(button.classList).some(item => {
if (item === 'check-btn') {
button.className = `check-btn ${color}-button`;
} else if (item === 'delete-btn') {
button.className = `delete-btn ${color}-button`;
} else if (item === 'todo-btn') {
button.className = `todo-btn ${color}-button`;
}
});
});
}