-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
320 lines (274 loc) · 7.98 KB
/
Copy pathscript.js
File metadata and controls
320 lines (274 loc) · 7.98 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
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
const addTodo = document.getElementById('add-todo');
const todoInput = document.getElementById('new-todo-input');
const footer = document.getElementById('footer');
const submitTodo = document.getElementById('submit-todo');
const todos = document.getElementById('todos');
const checkbox = document.getElementById('completedCheck');
const close = document.getElementById('close');
const info = document.getElementById('info');
const modal = document.getElementById('modal');
const modalClose = document.getElementById('closeModal');
const todoCount = document.getElementById('todoCount');
const toggleSwitch = document.querySelector(
'.theme-switch input[type="checkbox"]'
);
// Check if Current theme is saved in local storage
const currentTheme = localStorage.getItem('todoSideTheme')
? localStorage.getItem('todoSideTheme')
: null;
// If current theme is saved
if (currentTheme) {
// Set the theme accordingly
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
}
}
// Switch theme
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('todoSideTheme', 'dark');
} else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('todoSideTheme', 'light');
}
}
let todosData = JSON.parse(localStorage.getItem('todoSideTodos'));
// Dummy data
dummyData = [
{ todo: 'Welcome to TodoSide. 📝', completed: false, id: generateID() },
{
todo: 'Click New Todo to add a new Todo. ➕',
completed: false,
id: generateID(),
},
{
todo:
'On Desktop Hover on a todo to show the delete button. 🚮 but Mobile Devices show it by default',
completed: false,
id: generateID(),
},
{
todo: 'Click on the check button to mark an item as completed. ✅',
completed: true,
id: generateID(),
},
{
todo:
'Enter Multiple Todos at onces by pressing the enter key after each Todo',
completed: false,
id: generateID(),
},
];
// Check if there's data in the localstorage and display if there is
function checkLocalStorage() {
if ('todoSideTodos' in localStorage) {
if (todosData.length > 0) {
// Update DOM with todo data
updateDOM(todosData);
} else {
// Update DOM with dummy data
updateDOM(dummyData);
}
} else {
// Update DOM with dummy data
updateDOM(dummyData);
console.log('No Local Storage Item Available');
}
}
// Update DOM with passed in data
function updateDOM(data) {
todos.innerHTML = '';
data.forEach((el) => {
let checked;
if (el.completed) {
checked = true;
} else {
checked = false;
}
const todo = `
<li id="${el.id}" class="${el.completed ? 'completed' : ''}">
<label class='checkbox-label'>
<input type='checkbox' id='completedCheck' ${checked ? 'checked' : ''}/>
<span class='checkbox-custom'></span>
</label>
${el.todo}
<i class="fas fa-trash-alt" id="delete"></i>
</li>
`;
if (el.id === 'undefined') {
el.id = generateID();
}
addtoDOM(todo);
});
}
// Show Input field to add todo
addTodo.addEventListener('click', () => {
todoInput.classList.add('show');
});
// Save todo to storage and display in DOM
submitTodo.addEventListener('click', (e) => {
const parent = e.target.parentElement;
const todoItem = parent.children[2].value;
if (todoItem) {
let todoArray = [];
todoArray = todoItem.split('\n');
todoArray.forEach((el) => {
const todoSingle = new Todo(el, false);
const todo = `
<li>
<input type='checkbox' />
${todoSingle.todo}
<i class="fas fa-trash-alt" id="delete"></i>
</li>
`;
// Check if todosData array exists
todosData ? todosData : (todosData = []);
todosData.push(todoSingle);
localStorage.setItem('todoSideTodos', JSON.stringify(todosData));
// Update todo Count
todoCountUpdate();
addtoDOM(todo);
});
todoInput.style.transition = 'all 0.3s ease-in-out;';
todoInput.classList.remove('show');
parent.children[2].value = '';
// Call checkLocalStorage function after adding new Data
setTimeout(checkLocalStorage, 1000);
} else {
const textarea = parent.children[2];
textarea.classList.add('error');
textarea.placeholder = 'Please enter a Todo!';
}
});
// Adding new Element to DOm
function addtoDOM(el) {
todos.insertAdjacentHTML('beforeend', el);
}
// Toggle completed and delete todo
todos.addEventListener('click', (e) => {
if ('todoSideTodos' in localStorage) {
if (todosData.length > 0) {
toggleCompleted(e, todosData);
} else {
toggleCompleted(e, dummyData);
}
} else {
toggleCompleted(e, dummyData);
}
// If the delete button is clicked
if (e.target && e.target.matches('i')) {
const el = e.target.parentElement;
// Check if element selected is in the array and delete if found
for (let i = 0; i < todosData.length; i++) {
if (todosData[i].id === el.id) {
todosData.splice(i, 1);
// Update Todo Count
todoCountUpdate();
// Update local storage
updateLocalStorage();
}
}
// Remove todo from DOM
setTimeout(function () {
el.remove();
}, 300);
}
});
// Close input modal when close button is clicked
close.addEventListener('click', () => {
todoInput.classList.remove('show');
});
// Close the input modal if anywhere else on the screen is clicked
document.addEventListener('mouseup', (e) => {
if (!todoInput.contains(e.target)) {
todoInput.classList.remove('show');
}
});
// Toggle Switch
toggleSwitch.addEventListener('change', switchTheme, false);
// Toggle Completed Class
function toggleCompleted(e, data) {
// console.log(e.target.parentElement);
const todoCheck = e.target.parentElement.parentElement;
if (data === dummyData) {
if (e.target && e.target.matches('input')) {
// Loop through storage
for (i = 0; i < data.length; i++) {
// console.log(todoCheck);
if (data[i].id === todoCheck.id) {
let completed = data[i].completed;
// console.log(e.target, completed);
if (completed) {
todoCheck.classList.toggle('completed');
data[i].completed = false;
} else {
todoCheck.classList.toggle('completed');
data[i].completed = true;
}
}
}
}
} else if (data === todosData) {
// If the list item is clicked
if (e.target && e.target.matches('input')) {
// Loop through storage
for (i = 0; i < data.length; i++) {
if (data[i].id === todoCheck.id) {
let completed = data[i].completed;
// console.log(e.target, completed);
if (completed) {
todoCheck.classList.toggle('completed');
data[i].completed = false;
updateLocalStorage();
} else {
todoCheck.classList.toggle('completed');
data[i].completed = true;
updateLocalStorage();
}
}
}
}
}
}
// Todo Count
function todoCountUpdate() {
if ('todoSideTodos' in localStorage) {
const count = todosData.length;
if (count === 1) {
todoCount.innerText = `- ${count} Todo`;
} else {
todoCount.innerText = `- ${count} Todos`;
}
} else {
todoCount.innerText = '- 0 Todos';
console.log(todosData);
}
}
// Generate ID
function generateID() {
return 'ts-' + Math.random().toString(36).substr(2, 16);
}
// Todo Object Constructor
function Todo(todo, completed = false, id = generateID()) {
this.todo = todo;
this.completed = completed;
this.id = id;
}
// Update Local Storage
function updateLocalStorage() {
localStorage.removeItem('todoSideTodos');
localStorage.setItem('todoSideTodos', JSON.stringify(todosData));
setTimeout(checkLocalStorage, 1000);
}
// Open Modal
info.addEventListener('click', () => {
modal.classList.add('show-modal');
});
// Close Modal
modalClose.addEventListener('click', () => {
modal.classList.remove('show-modal');
});
checkLocalStorage();
todoCountUpdate();