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

Scroll calendar to current week on first load #276

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion resources/views/calendar/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

@section('js')
<script type="text/javascript">

document.addEventListener('DOMContentLoaded', function () {
let firstRender = true;

const calendarEl = document.getElementById('calendar');

const calendar = new FullCalendar.Calendar(calendarEl, {
Expand All @@ -30,6 +31,8 @@
$(".loading").fadeIn('fast');
} else {
$(".loading").fadeOut('fast');

if (firstRender) setTimeout(maybeMoveToCurrentWeek, 100);
}
},
eventClick: function (info) {
Expand Down Expand Up @@ -90,6 +93,31 @@
}
});

function maybeMoveToCurrentWeek() {
let layoutRoot = calendarEl.querySelector('.fc-dayGridMonth-view');

if (layoutRoot === null) {
console.warn('layout root not found');
return;
}

let thisWeek = layoutRoot.querySelector('.fc-today').closest('.fc-week');

// current week can be found at end of last month, within this month, or beginning of next month
if (thisWeek === null) {
console.warn('Current week is not on this page.');
return;
}

document.querySelector('.fc-scroller')
.scrollTo({
top: thisWeek.offsetTop,
behavior: 'smooth',
});

firstRender = false;
}

calendar.render();
});
</script>
Expand Down