From 50c05fc732c4d87481fcc3b24983c79ccb979471 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Wed, 31 Jul 2024 16:35:00 -0700 Subject: [PATCH] Cleanup JS for current event button --- app/assets/javascripts/osem-schedule.js | 27 +++++++++++-------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/osem-schedule.js b/app/assets/javascripts/osem-schedule.js index 5e4822bf9..f79dfa2a0 100644 --- a/app/assets/javascripts/osem-schedule.js +++ b/app/assets/javascripts/osem-schedule.js @@ -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' }); } });