-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.js
86 lines (81 loc) · 2.69 KB
/
calendar.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
document.addEventListener("DOMContentLoaded", function() {
let addEventForm = document.getElementById("myForm");
let filter = document.getElementById("filter");
let calendarTable = document.getElementById("calendar-table")
filter.addEventListener("change", (event) => {
clearTable();
showEvents(event.target.value);
});
addEventForm.addEventListener("submit", addEvent)
calendarTable.addEventListener("click", function(e) {
if (e.target && e.target.classList.contains('delete-event')) {
localStorage.removeItem(e.target.dataset.id);
clearTable();
showEvents();
}
})
showEvents();
});
const days = {
"monday": "2",
"tuesday": "3",
"wednesday": "4",
"thursday": "5",
"friday": "6"
};
const times = {
"10:00": "1",
"11:00": "2",
"12:00": "3",
"13:00": "4",
"14:00": "5",
"15:00": "6",
"16:00": "7",
"17:00": "8",
"18:00": "9"
};
function addEvent(e) {
e.preventDefault();
let formData = new FormData(e.target);
let formDataObject = {};
formData.forEach((value, key) => { formDataObject[key] = value });
if (!localStorage.getItem(formDataObject.day + "_" + formDataObject.time)) {
localStorage.setItem(formDataObject.day + "_" + formDataObject.time, JSON.stringify(formDataObject));
e.target.reset();
} else {
alert(`Wrong data`);
}
showEvents();
}
function clearTable() {
events_item = document.querySelectorAll(".table-active");
if (events_item) {
events_item.forEach((event_item) => {
event_item.innerHTML = "";
event_item.classList.remove("table-active");
});
}
}
function showEvents(member = null) {
let event = {};
for (var i = 0; i < localStorage.length; i++) {
event = JSON.parse(localStorage.getItem(localStorage.key(i)));
console.log(event.member);
if (!member || member === "allMembers" || (event.member === member || event.member === "allMembers")) {
if (times[event.time] && days[event.day]) {
let td = document.querySelector(`.table tr:nth-child(${times[event.time]}) td:nth-child(${days[event.day]})`);
td.innerHTML = `
<div class="row">
<div class="col-8">
${event.eventName}
</div>
<div class="col">
<button type="button" class="btn-close delete-event" data-id="${localStorage.key(i)}" aria-label="Close"></button>
</div>
</div>
`;
td.classList.add("table-active");
}
}
}
}