From b38936055e66fde8085af5ba1a312a4ab267a7ea Mon Sep 17 00:00:00 2001 From: Markerov Date: Sun, 21 Dec 2014 01:59:24 +0800 Subject: [PATCH] initial commit --- js/relative-time.js | 88 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 js/relative-time.js diff --git a/js/relative-time.js b/js/relative-time.js new file mode 100644 index 00000000..6c258a57 --- /dev/null +++ b/js/relative-time.js @@ -0,0 +1,88 @@ +/* + * relative-time.js + * Replaces the timestamps in posts to show 'x minutes/hours/days ago', + * while displaying the absolute time in tooltip + * + * Usage: + * $config['additional_javascript'][] = 'js/jquery.min.js'; + * $config['additional_javascript'][] = 'js/relative-time.js'; + */ +if (active_page == 'index' || active_page == 'thread') { + $(document).ready(function () { + 'use strict'; + + var selector, event_type; + if (window.Options && Options.get_tab('general')) { + selector = '#show-relative-time>input'; + event_type = 'change'; + Options.extend_tab('general', ''); + } else { + selector = '#show-relative-time'; + event_type = 'click'; + $('hr:first').before('
'+_('Show relative time')+'
'); + } + + $(selector).on(event_type, function() { + if (localStorage.show_relative_time === 'true') { + localStorage.show_relative_time = 'false'; + } else { + localStorage.show_relative_time = 'true'; + } + }); + + + function update() { + var currentTime = Date.now(); + $('div.post time').each(function () { + var postTime = new Date($(this).attr('datetime')); + + $(this) + .text( timeDifference(currentTime, postTime.getTime()) ) + .attr('title', postTime.toLocaleString('en-GB', { + weekday: 'short', + day: 'numeric', + month: 'numeric', + year: 'numeric', + hour: 'numeric', + minute: 'numeric', + second: 'numeric', + hour12: false + })); + }); + } + + function timeDifference(current, previous) { + + var msPerMinute = 60 * 1000; + var msPerHour = msPerMinute * 60; + var msPerDay = msPerHour * 24; + var msPerMonth = msPerDay * 30; + var msPerYear = msPerDay * 365; + + var elapsed = current - previous; + + if (elapsed < msPerMinute) { + return 'Just now'; + } else if (elapsed < msPerHour) { + return Math.round(elapsed/msPerMinute) + (Math.round(elapsed/msPerMinute)<=1 ? ' minute ago':' minutes ago'); + } else if (elapsed < msPerDay ) { + return Math.round(elapsed/msPerHour ) + (Math.round(elapsed/msPerHour)<=1 ? ' hour ago':' hours ago'); + } else if (elapsed < msPerMonth) { + return Math.round(elapsed/msPerDay) + (Math.round(elapsed/msPerDay)<=1 ? ' day ago':' days ago'); + } else if (elapsed < msPerYear) { + return Math.round(elapsed/msPerMonth) + (Math.round(elapsed/msPerMonth)<=1 ? ' month ago':' months ago'); + } else { + return Math.round(elapsed/msPerYear ) + (Math.round(elapsed/msPerYear)<=1 ? ' year ago':' years ago'); + } + } + + if (!localStorage.show_relative_time || localStorage.show_relative_time === 'false') { + return; + } else { + $('#show-relative-time>input').attr('checked','checked'); + setInterval(update, 30000); + $(document).on('new_post', update); + update(); + } + }); +}