diff options
author | Kevin Hamer <kevin@imarc.com> | 2020-10-11 17:12:45 -0400 |
---|---|---|
committer | Mantas Vilčinskas <hi@mnts.lt> | 2020-10-27 21:38:46 +0200 |
commit | 78b3b15525d756c5b522b07e98049dcb9c024a6a (patch) | |
tree | 57f0eb54db845ca730e54d1e087f63c10f53dd78 /layouts/partials/js.html | |
parent | e8e50381292a6532cdc3c2802f37262b279fe30b (diff) |
Add .relative-time class and JS logic
Diffstat (limited to 'layouts/partials/js.html')
-rw-r--r-- | layouts/partials/js.html | 93 |
1 files changed, 48 insertions, 45 deletions
diff --git a/layouts/partials/js.html b/layouts/partials/js.html index fbe102c..7971bc5 100644 --- a/layouts/partials/js.html +++ b/layouts/partials/js.html @@ -41,58 +41,61 @@ } /** - * Timer + * Returns a relative date string for the given date. */ + function timeSince(date) { + var seconds = Math.floor((new Date() - date) / 1000); - function hasClass(element, className) { - return (' ' + element.className + ' ').indexOf(' ' + className+ ' ') > -1; - } - - if (hasClass(document.querySelector('body'), 'status-homepage')) { - var lastUpdated = document.querySelector('.summary__date'); - lastUpdated.innerHTML = '{{ T "lastChecked" }} {{ T "justNow" }}'; - - var lastUpdate = new Date(); - - function timeSince(date) { - var seconds = Math.floor((new Date() - date) / 1000); + var interval = Math.floor(seconds / 31536000); - var interval = Math.floor(seconds / 31536000); - - if (interval > 1) { - return interval + ' {{ T "yearsAgo" }}'; - } - interval = Math.floor(seconds / 2592000); - if (interval > 1) { - return interval + ' {{ T "monthsAgo" }}'; - } - interval = Math.floor(seconds / 86400); - if (interval > 1) { - return interval + '{{ T "daysAgo" }}'; - } - interval = Math.floor(seconds / 3600); - if (interval > 1) { - return interval + '{{ T "hoursAgo" }}'; - } - interval = Math.floor(seconds / 60); - if (interval > 1) { - return interval + ' {{ T "minsAgo" }}'; - } - return Math.floor(seconds) + '{{ T "secondsAgo" }}'; + if (interval > 1) { + return interval + ' {{ T "yearsAgo" }}'; + } + interval = Math.floor(seconds / 2592000); + if (interval > 1) { + return interval + ' {{ T "monthsAgo" }}'; + } + interval = Math.floor(seconds / 86400); + if (interval > 1) { + return interval + '{{ T "daysAgo" }}'; } - var aDay = 24*60*60*1000; + interval = Math.floor(seconds / 3600); + if (interval > 1) { + return interval + '{{ T "hoursAgo" }}'; + } + interval = Math.floor(seconds / 60); + if (interval > 1) { + return interval + ' {{ T "minsAgo" }}'; + } + return Math.floor(seconds) + '{{ T "secondsAgo" }}'; } - window.setInterval(function() { - if (hasClass(document.querySelector('body'), 'status-homepage')) { - lastUpdated.innerHTML = '{{ T "lastChecked" }} ' + timeSince(lastUpdate) + ' {{ T "someTimeAgo" }}'; + /** + * Changes elements with the class 'relative-time' into relative times and + * moves the timestamp to a title attribute tooltip. + */ + function updateRelativeTimes() { + document.querySelectorAll('.relative-time') + .forEach(function(element) { + var time = Date.parse(element.getAttribute('title')); + var html = element.getAttribute('data-prefix') || ''; + if (!time) { + time = element.innerText; + element.setAttribute('title', time || new Date); + html += '{{ T "justNow" }}'; + } else { + html += timeSince(time) + ' {{ T "someTimeAgo" }}'; + } + element.innerHTML = html.trim(); + }) + } + updateRelativeTimes(); + setInterval(updateRelativeTimes, 5000); - // Refresh almost every 5m - if (Math.floor(new Date() - lastUpdate) > 290000) { - location.reload(); - } - } - }, 5000) + // Reload homepage eevery 290 seconds + if (document.querySelector('body.status-homepage')) { + setInterval(location.reload, 290000) + } </script> |