-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
258 lines (221 loc) · 8.57 KB
/
script.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
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
(function() {
class TodoTask {
constructor(taskDesc) {
this.taskDesc = taskDesc;
this.isComplete = false;
}
complete() {
this.isComplete = true;
}
}
class TodoList {
constructor() {
this.todoList = [];
}
}
class TodoListElement extends HTMLUListElement {
constructor() {
super();
}
}
class TodoListItemElement extends HTMLLIElement {
constructor() {
super();
}
}
function createNewTask(taskDesc) {
const todoListEL = document.getElementById('todoList');
const li = document.createElement('li');
li.setAttribute('class', 'todo-box todo-list-item');
li.draggable = true;
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
li.appendChild(checkbox);
const checkIcon = document.createElement('span');
checkIcon.setAttribute('class', 'icon empty-circle');
checkIcon.setAttribute('data-action', 'complete');
li.appendChild(checkIcon);
const taskText = document.createElement('span');
taskText.setAttribute('class', 'todo-text');
taskText.setAttribute('data-action', 'complete');
taskText.innerText = taskDesc;
li.appendChild(taskText);
const deleteIcon = document.createElement('span');
deleteIcon.setAttribute('class', 'icon cross');
deleteIcon.setAttribute('data-action', 'delete');
li.appendChild(deleteIcon);
todoListEL.prepend(li);
updateTodoCount();
}
function updateTodoCount() {
const todoListEL = document.querySelectorAll('.todo-list-item:not(.completed)');
const countEl = document.getElementById('count');
countEl.innerText = todoListEL.length;
}
function clearCompleted() {
}
function getTheme() {
return document.documentElement.getAttribute('data-theme');
}
function setTheme(theme) {
const documentEL = document.documentElement;
documentEL.setAttribute('data-theme', theme);
}
function setTodoState(state) {
const todoListEL = document.getElementById('todoList');
todoListEL.setAttribute('data-state', state);
}
function registerServiceWorker() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
notificationSetup();
}, function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
}
}
function notificationSetup() {
if (!('Notification' in window)) {
console.log('Notifications not suppported!!');
return;
}
if (Notification.permission === 'granted') {
console.log('Notifications are allowed!!');
return;
}
if (Notification.permission !== 'denied') {
Notification.requestPermission().then(p => {
if (p === 'granted') {
console.log('Notifications are allowed!!');
}
});
}
}
function showNotification(title, body) {
let notif = new Notification(title, {
body: body ? body : ''
});
notif.addEventListener('error', ev => {
console.error('There was a problem', ev);
});
notif.addEventListener('click', ev => {
console.log('Notification clicked');
});
}
function initializeApp() {
registerServiceWorker();
const todoList = new TodoList();
const todoInputEL = document.getElementById('todoInput');
const todoListEL = document.getElementById('todoList');
const todoTabEL = document.getElementById('todoTab');
const todoTabDesktopEL = document.getElementById('todoTabDesktop');
const themeBtn = document.getElementById('themeBtn');
const clearCompletedBtn = document.getElementById('clearCompleted');
todoInputEL.addEventListener('keyup', function(e) {
if (e.key === 'Enter') {
const taskDesc = String(todoInputEL.value).trim();
if (taskDesc) {
try {
createNewTask(taskDesc);
todoInputEL.value = '';
} catch(err) {
}
} else {
alert('Input is required');
}
}
});
todoListEL.addEventListener('click', function(e){
if(e.target.tagName === 'SPAN') {
const dataset = e.target.dataset;
if (dataset && dataset.action === 'complete') {
const parentLI = e.target.parentElement;
const checkbox = parentLI.firstElementChild;
checkbox.checked = !checkbox.checked;
parentLI.classList.toggle('completed');
}
if (dataset && dataset.action === 'delete') {
e.target.parentElement.animate([
// keyframes
{ transform: `translateX(-${ e.target.parentElement.offsetWidth}px) scale(0, 0)` }
], {
// timing options
duration: 200,
iterations: 1
}).finished.then(() => {
e.target.parentElement.remove();
updateTodoCount();
});
}
}
});
todoListEL.addEventListener('drag', function (e) {
if (e.target.tagName === 'LI') {
e.target.classList.add('dragged');
// e.dataTransfer.setData("text/plain", e.target.x);
// this.setAttribute('data-dragged', e.target);
e.dataTransfer.dropEffect = 'move';
this.draggedElement = e.target;
}
});
todoListEL.addEventListener('dragover',function(e) {
if (e.target.tagName === 'LI' && this.draggedElement && e.target !== this.draggedElement) {
if (this.lastElementChild === e.target) {
this.appendChild(this.draggedElement);
} else {
this.insertBefore(this.draggedElement, e.target);
}
}
});
todoListEL.addEventListener('dragend', function(e) {
if (e.target.tagName === 'LI') {
e.target.classList.remove('dragged');
delete this.draggedElement;
}
});
function handleTodoStates(e) {
if(e.target.tagName === 'SPAN' && !e.target.classList.contains('active')) {
const items = document.querySelectorAll('.todo-states span.state');
Array.from(items).forEach(el => {
if (el.innerText === e.target.innerText) {
el.classList.toggle('active');
} else {
el.classList.remove('active');
}
});
const state = String(e.target.innerText).toLowerCase();
setTodoState(state);
}
}
todoTabEL.addEventListener('click', handleTodoStates);
todoTabDesktopEL.addEventListener('click', handleTodoStates);
themeBtn.addEventListener('click', function (e) {
if(e.target.tagName === 'SPAN') {
const theme = getTheme();
if (!theme || theme === 'light') {
setTheme('dark');
} else {
setTheme('light');
}
}
});
clearCompletedBtn.addEventListener('click', e => {
if(e.target.tagName === 'SPAN') {
document.querySelectorAll('.todo-list-item.completed')
.forEach(el => el.remove());
updateTodoCount();
}
})
function consumeRestAPI() {
fetch('https://jsonplaceholder.typicode.com/todos')
.then(response => response.json())
.then(json => json.slice(0, 5).forEach(item => createNewTask(item.title)));
}
updateTodoCount();
consumeRestAPI();
}
window.addEventListener('load', initializeApp);
})();