-
Notifications
You must be signed in to change notification settings - Fork 54
/
TodoDay.js
52 lines (43 loc) · 1.36 KB
/
TodoDay.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
import { TodoList } from './TodoList.js';
import { formatDate, formatDayOfWeek } from './util.js';
/**
* @param {HTMLElement} el
*/
export function TodoDay(el) {
const dateId = el.dataset.key;
let items = [];
el.innerHTML = /* html */ `
<div class="header">
<h2 class="dayofweek"></h3>
<h3 class="date"></h4>
</div>
<div class="todo-list"></div>
`;
TodoList(el.querySelector('.todo-list'));
el.addEventListener('addTodoItem', (e) => {
e.detail.listId = dateId;
});
el.addEventListener('moveTodoItem', (e) => {
e.detail.listId = dateId;
e.detail.index = e.detail.index ?? 0;
});
el.addEventListener('todoDay', (e) => {
items = e.detail.items;
update();
});
function update() {
const date = new Date(`${dateId}T00:00:00`);
const today = new Date();
today.setHours(0, 0, 0, 0);
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
el.classList.toggle('-past', date < today);
el.classList.toggle('-today', date >= today && date < tomorrow);
el.classList.toggle('-future', date >= tomorrow);
el.querySelector('.header > .dayofweek').innerText = formatDayOfWeek(date);
el.querySelector('.header > .date').innerText = formatDate(date);
el.querySelector('.todo-list').dispatchEvent(
new CustomEvent('todoItems', { detail: items }),
);
}
}