-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (89 loc) · 2.61 KB
/
script.js
File metadata and controls
116 lines (89 loc) · 2.61 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
import { formatDate, getCalendarDates, getMonthFromNumber } from './calendar.js';
const date = new Date();
let year = date.getFullYear();
let month = date.getMonth();
let weekStart = 1;
const [prevBtn, monthBtn, nextBtn] = document.querySelectorAll('header button');
const tbody = document.querySelector('tbody');
const dayContainer = document.getElementById('day');
const [dayHeader, dayNote] = dayContainer.children;
const [dayBack, dayTitle] = dayHeader.children;
dayBack.addEventListener('click', () => {
const db = getDB();
const ctx = dayTitle.textContent;
const d = document.querySelector(`[data-date="${ctx}"]`);
if (dayNote.value) {
db[ctx] = dayNote.value;
if (!d.classList.contains('logged'))
d.classList.add('logged');
}
else {
delete db[ctx];
if (d.classList.contains('logged'))
d.classList.remove('logged');
}
localStorage.setItem('db', JSON.stringify(db));
dayContainer.classList.toggle('hide');
});
prevBtn.addEventListener('click', () => {
if (month === 0) {
year--;
month = 12;
}
month--;
renderer(getCalendarDates(year, month, weekStart));
})
nextBtn.addEventListener('click', () => {
if (month === 11) {
year++;
month = -1;
}
month++;
renderer(getCalendarDates(year, month, weekStart));
})
monthBtn.addEventListener('click', () => {
weekStart--;
if (weekStart === 7)
weekStart = 0;
console.log(weekStart);
renderer(getCalendarDates(year, month, weekStart));
})
function renderer(seed) {
tbody.innerHTML = '';
monthBtn.textContent = getMonthFromNumber(month).toUpperCase();
seed.forEach((dt) => {
const mth = dt.getMonth();
const dayOfWeek = dt.getDay();
const displayDay = dt.getDate();
if (dayOfWeek === 1)
tbody.appendChild(document.createElement('tr'));
const td = document.createElement('td');
td.textContent = displayDay;
if (month !== mth)
td.className = 'inactive';
if (mth === date.getMonth() && displayDay === date.getDate())
td.className = 'active';
const title = formatDate(dt);
if (title in getDB())
td.classList.add('logged');
td.dataset.date = title;
td.addEventListener('click', () => {
dayContainer.classList.toggle('hide');
dayTitle.textContent = title;
const db = getDB();
if (title in db)
dayNote.value = db[title];
else
dayNote.value = '';
});
tbody.lastElementChild.appendChild(td);
});
}
renderer(
getCalendarDates(year, month, weekStart)
);
function getDB() {
const baseString = localStorage.getItem('db');
const database = JSON.parse(baseString || '{}');
return database;
}