Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Full Calendar to Calendar Page #1

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions assets/js/calendar.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
fetchCalendarData().then(data => {
var e = document.getElementById("calendar");
e.innerHTML = e.innerHTML || "";
var locale = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
data.forEach(el => {
e.innerHTML += `<div class="list-group-item">
<div class="d-flex w-100 justify-content-between">
<h2 class="mb-1">${el.name}</h5>
<small>in ${el.daysUntil} days</small>
</div>
<p class="mb-1 ${el.membersOnly ? "text-bg-warning" : "text-bg-success"}">${el.membersOnly ? "Members Only" : "Open To The Public"}</p>
<p class="mb-1">${el.start.toLocaleDateString("en-us",locale)} from ${String(el.start.getHours()).padStart(2,'0')}:${String(el.start.getMinutes()).padStart(2,'0')} until ${String(el.end.getHours()).padStart(2,'0')}:${String(el.end.getMinutes()).padStart(2,'0')}</p>
<small>${el.description}</small>
</div>`;
});
fetchCalendarData().then(events => {
const calendarEl = document.getElementById("calendar");
const calendar = new FullCalendar.Calendar(calendarEl, {
initialView: "dayGridMonth",
events,
headerToolbar: {
left: 'prev,next today',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay'
},
});
calendar.render();
});

async function fetchCalendarData() {
Expand All @@ -30,26 +27,25 @@ async function fetchCalendarData() {
.then(data => {
var now = new Date(Date.now());
var today = new Date(now.getFullYear(),now.getMonth(),now.getDate());
data.forEach(element => {
var start = new Date(element.start * 1000);
var end = new Date(element.end * 1000);
var durationDay = new Date(start.getFullYear(),start.getMonth(), start.getDate());
var diffTime = Math.abs(durationDay - today);
var diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
element.start = start;
element.end = end;
element.daysUntil = diffDays;
});
data = data.sort((a,b) => {

return data.map(element => {
const title = element.name;
const start = new Date(element.start * 1000);
const end = new Date(element.end * 1000);
const durationDay = new Date(start.getFullYear(),start.getMonth(), start.getDate());
const diffTime = Math.abs(durationDay - today);
const daysUntil = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

return {title, start, end, daysUntil};
}).sort((a,b) => {
if (a.daysUntil > b.daysUntil) return 1;
else if (b.daysUntil > a.daysUntil) return -1;
else if (a.name > b.name) return -1;
else if (b.name > a.name) return 1;
return 0;
});
return data;
})
.catch(error => {
console.error('Error:', error);
});
}
}
6 changes: 3 additions & 3 deletions pages/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ hero:
enabled: false
---

<div id="calendar" class="list-group"></div>

<script type="text/javascript" src="{{ '/assets/js/calendar.js' | relative_url }}"></script>
<div id="calendar" style="width:150%; max-width: 95vw; height: 100%;"></div>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/index.global.min.js'></script>
<script type="text/javascript" src="{{ '/assets/js/calendar.js' | relative_url }}"></script>
Loading