-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
514 lines (411 loc) · 15.9 KB
/
app.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import * as db from "./db.js";
import { setFileForTask, getFileForTask, deleteFileForTask } from "./files.js";
import { convertDateTimeToEpoch, convertEpochToDateTime } from "./utils.js";
const mainElement = document.querySelector("main");
const listsElement = document.querySelector('.lists');
const autoListsElement = document.querySelector('.auto-lists');
const tasksElement = document.querySelector('main .tasks');
const newTaskForm = document.querySelector('.new-task-form');
const newTaskInput = document.querySelector('#new-task');
const newListForm = document.querySelector('.new-list-form');
const searchTasksForm = document.querySelector('.search-form');
const searchQueryInput = document.querySelector('#search-query');
const editTaskForm = document.querySelector('.edit-task-form');
const editTaskTitleInput = document.querySelector('#edit-task-title');
const editTaskCompletedInput = document.querySelector('#edit-task-completed');
const editTaskNotesInput = document.querySelector('#edit-task-notes');
const editTaskFileInput = document.querySelector('#edit-task-file');
const editTaskDueByInput = document.querySelector('#edit-task-dueby');
const editTaskIdInput = document.querySelector('#edit-task-id');
const editTaskButton = document.querySelector('#edit-task-button');
const editTaskUploadSection = editTaskForm.querySelector('.upload');
const editTaskDownloadSection = editTaskForm.querySelector('.download');
const closeTaskForm = document.querySelector('#close-edit-task-form');
const editListForm = document.querySelector('.edit-list-form');
const editListTitleInput = document.querySelector('#edit-list-title');
const editListIdInput = document.querySelector('#edit-list-id');
const deleteListButton = document.querySelector('#delete-list-button');
const deleteTaskButton = document.querySelector('#delete-task-button');
const searchResultsDialog = document.querySelector('.search-results');
const searchResultsList = searchResultsDialog.querySelector('.tasks');
const moreListActionsButton = document.querySelector('.more-list-actions');
const moreListActionsDialog = document.querySelector('.more-list-actions-dialog');
const moreListActionsForm = moreListActionsDialog.querySelector('form');
const taskFileDownloadLink = document.querySelector('#task-file-download');
const taskFileDeleteButton = document.querySelector('#delete-task-file-button');
const learnMoreLink = document.querySelector('h1 a');
const learnMoreDialog = document.querySelector('.learn-more-dialog');
const exportDBButton = document.querySelector('#db-export-button');
// const importDBInput = document.querySelector('#db-import-file');
const persistDBButton = document.querySelector('#make-db-persistent');
const LIST_COLORS = [...moreListActionsDialog.querySelectorAll('.list-colors input')].map(input => input.value);
function getRandomListColor() {
return LIST_COLORS[Math.floor(Math.random() * LIST_COLORS.length)];
}
function createListItem(id, title, color, nbTasks) {
const listItem = document.createElement('li');
listItem.classList.add('list');
listItem.dataset.id = id;
listItem.dataset.color = color;
listItem.style.setProperty('--list-color', color);
const listLink = document.createElement('a');
listLink.href = 'load-tasks';
listLink.innerText = title;
listItem.appendChild(listLink);
const numberOfTasks = document.createElement('span');
numberOfTasks.classList.add('number-of-tasks');
numberOfTasks.innerText = nbTasks;
listItem.appendChild(numberOfTasks);
listLink.addEventListener('click', e => {
e.preventDefault();
selectList(listItem);
});
return listItem;
}
function createTaskItem(id, title, completed, notes, hasFile, dueBy, readOnly) {
const taskItem = document.createElement('li');
taskItem.classList.add('task');
taskItem.dataset.id = id;
if (notes) {
taskItem.classList.add('has-notes');
}
if (completed) {
taskItem.classList.add('is-completed');
}
if (hasFile) {
taskItem.classList.add('has-file');
}
if (dueBy) {
taskItem.classList.add('has-due-date');
}
const completedBox = document.createElement('input');
completedBox.type = 'checkbox';
completedBox.checked = !!completed;
if (readOnly) {
completedBox.disabled = true;
} else {
completedBox.addEventListener('change', () => {
db.completeTask(id, completedBox.checked);
taskItem.classList.toggle('is-completed');
updateAutoListsCount();
});
}
taskItem.appendChild(completedBox);
const titleEl = document.createElement('span');
titleEl.classList.add('title');
titleEl.innerText = title;
taskItem.appendChild(titleEl);
if (!readOnly) {
const editButton = document.createElement('button');
editButton.textContent = 'Edit task';
editButton.title = 'Edit task';
editButton.classList.add('edit-button', 'no-label');
editButton.addEventListener('click', () => {
document.querySelectorAll('.task').forEach(task => task.classList.remove('selected'));
taskItem.classList.add('selected');
fillEditTaskForm(id, title, notes, hasFile, completedBox.checked, dueBy);
});
taskItem.appendChild(editButton);
}
return taskItem;
}
function fillEditTaskForm(id, title, notes, hasFile, completed, dueBy) {
editTaskTitleInput.value = title;
editTaskNotesInput.value = notes;
editTaskFileInput.value = '';
editTaskDueByInput.value = convertEpochToDateTime(dueBy);
editTaskCompletedInput.checked = completed;
editTaskIdInput.value = id;
editTaskButton.disabled = false;
deleteTaskButton.disabled = false;
if (hasFile) {
editTaskUploadSection.style.display = 'none';
editTaskDownloadSection.style.display = 'block';
getFileForTask(id).then(file => {
taskFileDownloadLink.href = URL.createObjectURL(file);
taskFileDownloadLink.download = file.name;
taskFileDownloadLink.innerText = file.name;
});
} else {
editTaskUploadSection.style.display = 'block';
editTaskDownloadSection.style.display = 'none';
}
document.documentElement.classList.add('is-editing');
}
function clearEditTaskForm() {
editTaskTitleInput.value = '';
editTaskNotesInput.value = '';
editTaskFileInput.value = '';
editTaskDueByInput.value = '';
editTaskCompletedInput.checked = false;
editTaskIdInput.value = '';
editTaskButton.disabled = true;
deleteTaskButton.disabled = true;
editTaskUploadSection.style.display = 'none';
editTaskDownloadSection.style.display = 'none';
document.documentElement.classList.remove('is-editing');
}
async function getListTasks(listId) {
switch (listId) {
case 'overdue':
return await db.getOverdueTasks();
case 'today':
return await db.getTodaysTasks();
default:
return await db.getListTasks(listId);
}
}
function isListEditable(listId) {
return listId !== 'overdue' && listId !== 'today';
}
async function selectList(listItem, highlightedTaskId) {
moreListActionsButton.style.display = isListEditable(listItem.dataset.id) ? 'block' : 'none';
editListTitleInput.disabled = !isListEditable(listItem.dataset.id);
newTaskForm.style.display = isListEditable(listItem.dataset.id) ? 'flex' : 'none';
document.querySelectorAll('.task').forEach(task => task.classList.remove('selected'));
clearEditTaskForm();
document.querySelectorAll('.list').forEach(list => list.classList.remove('selected'));
listItem.classList.add('selected');
const listId = listItem.dataset.id;
mainElement.style.backgroundColor = listItem.dataset.color;
editListTitleInput.value = listItem.querySelector('a').innerText;
editListIdInput.value = listId;
tasksElement.innerHTML = '';
let scrollTo = null;
const tasks = await getListTasks(listId);
tasks.forEach(task => {
const taskItem = createTaskItem(task.id, task.title, task.completed, task.notes, task.has_file, task.due_by);
if (task.id === highlightedTaskId) {
taskItem.classList.add('highlighted');
setTimeout(() => taskItem.classList.remove('highlighted'), 2000);
scrollTo = taskItem;
}
tasksElement.appendChild(taskItem);
if (task.has_file) {
const fileLink = document.createElement('a');
fileLink.classList.add('file-link');
}
});
if (scrollTo) {
scrollTo.scrollIntoView();
}
}
newTaskForm.onsubmit = async function (event) {
event.preventDefault();
const selectedList = document.querySelector('.list.selected');
const title = newTaskInput.value;
const listId = selectedList.dataset.id;
await db.createTask(title, listId);
event.target.reset();
// FIXME: instead of refreshing the list, just add the new task.
await selectList(selectedList);
selectedList.querySelector('.number-of-tasks').innerText = tasksElement.querySelectorAll('.task').length;
}
newListForm.onsubmit = async function (event) {
event.preventDefault();
const [newList] = await db.createList('Untitled list', getRandomListColor());
const listItem = createListItem(newList.id, newList.title, newList.color, 0);
document.querySelector('.lists').appendChild(listItem);
selectList(listItem);
}
searchTasksForm.onsubmit = async function (event) {
event.preventDefault();
const query = searchQueryInput.value;
const tasks = await db.searchTasks(query);
searchResultsList.innerHTML = '';
for (const task of tasks) {
const li = createTaskItem(task.id, task.title, task.completed, task.notes, task.has_file, true);
const listTitle = document.createElement('a');
listTitle.href = 'go-to-list';
listTitle.classList.add('list-title');
listTitle.innerText = task.list_title;
listTitle.style.backgroundColor = task.list_color;
li.appendChild(listTitle);
listTitle.addEventListener('click', e => {
e.preventDefault();
const listItem = document.querySelector(`.list[data-id="${task.list_id}"]`);
selectList(listItem, task.id);
searchResultsDialog.close();
});
searchResultsList.appendChild(li);
}
searchResultsDialog.showModal();
}
editTaskForm.onsubmit = async function (event) {
event.preventDefault();
const id = editTaskIdInput.value;
const title = editTaskTitleInput.value;
const completed = editTaskCompletedInput.checked;
const notes = editTaskNotesInput.value;
const dueBy = convertDateTimeToEpoch(editTaskDueByInput.value);
// Get the file from the input.
const file = editTaskFileInput.files[0];
if (file) {
await setFileForTask(id, file);
await db.setTaskHasFile(id, !!file);
}
await db.addTaskNotes(id, notes);
await db.renameTask(id, title);
await db.completeTask(id, completed);
await db.setTaskDueBy(id, dueBy);
// FIXME: instead of refreshing the list, just update the task.
const selectedList = document.querySelector('.list.selected');
await selectList(selectedList);
// Also update the auto-lists' counts.
updateAutoListsCount();
}
async function updateAutoListsCount() {
const overdueCount = (await db.getOverdueTasks()).length;
document.querySelector('.list[data-id="overdue"] .number-of-tasks').innerText = overdueCount;
const todayCount = (await db.getTodaysTasks()).length;
document.querySelector('.list[data-id="today"] .number-of-tasks').innerText = todayCount;
await updateAppBadge();
}
async function updateAppBadge() {
const todayCount = (await db.getTodaysTasks()).length;
if (navigator.setAppBadge) {
if (todayCount > 0) {
navigator.setAppBadge(todayCount);
} else {
navigator.clearAppBadge();
}
}
}
setInterval(updateAutoListsCount, 1000 * 60 * 5);
editListForm.onsubmit = async function (event) {
event.preventDefault();
const id = editListIdInput.value;
const title = editListTitleInput.value;
await db.renameList(id, title);
document.querySelector('.list.selected a').innerText = title;
}
deleteListButton.addEventListener('click', async () => {
const id = editListIdInput.value;
await db.deleteList(id);
await reInitUI();
});
deleteTaskButton.addEventListener('click', async () => {
const id = editTaskIdInput.value;
const taskItem = document.querySelector(`.task[data-id="${id}"]`);
await db.deleteTask(id);
await deleteFileForTask(id);
taskItem.remove();
const selectedList = document.querySelector('.list.selected');
selectedList.querySelector('.number-of-tasks').innerText = tasksElement.querySelectorAll('.task').length;
clearEditTaskForm();
});
taskFileDeleteButton.addEventListener('click', async () => {
const id = editTaskIdInput.value;
await deleteFileForTask(id);
await db.setTaskHasFile(id, false);
clearEditTaskForm();
document.documentElement.classList.remove('is-editing');
});
closeTaskForm.addEventListener('click', () => {
document.documentElement.classList.remove('is-editing');
});
addEventListener('mousedown', event => {
if (document.documentElement.classList.contains('is-editing') &&
!event.target.closest('.edit-task-form')) {
document.documentElement.classList.remove('is-editing');
}
});
moreListActionsButton.addEventListener('click', () => {
if (moreListActionsDialog.open) {
moreListActionsDialog.close();
} else {
moreListActionsDialog.showModal();
}
});
moreListActionsForm.addEventListener('change', async () => {
const id = editListIdInput.value;
const color = moreListActionsForm['list-color'].value;
await db.changeListColor(id, color);
const selectedList = document.querySelector('.list.selected');
selectedList.style.setProperty('--list-color', color);
mainElement.style.backgroundColor = color;
mainElement.style.accentColor = color;
moreListActionsDialog.close();
});
learnMoreLink.addEventListener('click', e => {
e.preventDefault();
learnMoreDialog.showModal();
});
persistDBButton.addEventListener('click', async () => {
if (navigator.storage && navigator.storage.persist) {
const isPersisted = await navigator.storage.persisted();
if (!isPersisted) {
try {
await navigator.storage.persist();
persistDBButton.textContent = "Database is now persistent";
} catch (e) {
persistDBButton.textContent = "Database could not be persisted";
}
} else {
persistDBButton.textContent = "Database was already persisted";
}
}
});
exportDBButton.addEventListener('click', async () => {
const data = await db.getDBFile();
const blob = new Blob([data], { type: 'application/x-sqlite3' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
const today = new Date();
const date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
a.download = `pwa-to-do-${date}.db`;
a.href = url;
a.click();
});
// TODO
// importDBInput.addEventListener('change', e => {
// const file = e.target.files[0];
// if (!file) {
// return;
// }
// const reader = new FileReader();
// reader.onload = async () => {
// const data = reader.result;
// await db.importDBFile(data);
// await startApp();
// };
// reader.readAsArrayBuffer(file);
// });
async function reInitUI() {
listsElement.innerHTML = '';
autoListsElement.innerHTML = '';
tasksElement.innerHTML = '';
const lists = await db.getLists();
lists.forEach(list => {
const listItem = createListItem(list.id, list.title, list.color, list.nb_tasks);
listsElement.appendChild(listItem);
});
// Select the first list.
if (lists.length) {
selectList(listsElement.querySelector('.list'));
}
await initAutoLists();
}
async function initAutoLists() {
// Overdue tasks.
const overdueTasks = await db.getOverdueTasks();
const overdueListItem = createListItem('overdue', 'Overdue', 'coral', overdueTasks.length);
autoListsElement.appendChild(overdueListItem);
// Due today.
const dueTodayTasks = await db.getTodaysTasks();
const dueTodayListItem = createListItem('today', 'Today', 'gainsboro', dueTodayTasks.length);
autoListsElement.appendChild(dueTodayListItem);
}
async function startApp() {
try {
await db.initDB();
} catch (error) {
console.error(`Error initializing database`, error);
// Reload the page.
// window.location.reload();
return;
}
await reInitUI();
await updateAppBadge();
}
startApp();