Skip to content

Commit

Permalink
Cleanup JS for current event button
Browse files Browse the repository at this point in the history
  • Loading branch information
cycomachead committed Jul 31, 2024
1 parent 99506d0 commit 50c05fc
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions app/assets/javascripts/osem-schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,27 @@ $(document).ready( function() {

$('#current-event-btn').on('click', function() {
var now = new Date();
var closestEventId = null;
var closestEvent = null;
var smallestDiff = Infinity;
var i = 0;

$('.event-item').each(function() {
let $event = $(this), eventTimeStr = $event.data('time');

var eventTimeStr = $(this).data('time');
if (!eventTimeStr) { return; }

if (eventTimeStr) {
var eventTime = new Date(eventTimeStr);
var diff = Math.abs(eventTime - now);

if (diff < smallestDiff) {
smallestDiff = diff;
closestEventId = $(this).attr('class').split(' ')[1];
}
}
var eventTime = new Date(eventTimeStr);
var diff = Math.abs(eventTime - now);
if (diff < smallestDiff) {
smallestDiff = diff;
closestEvent = $event;
}
});

if (closestEventId) {
//Instead of relying on hash it's probably better to scroll using javascript
//Since the users and click button->scroll->click again, which won't re-scroll
// Instead of relying on hash it's probably better to scroll using javascript
// Since the users and click button->scroll->click again, which won't re-scroll
$('.highlighted').removeClass('highlighted');
$('.' + closestEventId).addClass('highlighted').get(0).scrollIntoView({ behavior: 'smooth', block: 'start' });
$(closestEvent).addClass('highlighted').get(0).scrollIntoView({ behavior: 'smooth', block: 'start' });
}
});

Expand Down

0 comments on commit 50c05fc

Please sign in to comment.