From e3a721ade08cfdbab25edf219d3e1bd3c5732ac6 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Fri, 15 Feb 2013 22:01:58 +0100 Subject: [PATCH] Add months and years support in relative dates. We don't need to flush on node if it's a clone. Prevent theoretical "60 seconds ago" and such. --- 4chan_x.user.js | 22 +++++++++++----------- src/config.coffee | 2 +- src/features.coffee | 45 ++++++++++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 27 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 3398002c4..fdd816fe7 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -55,7 +55,7 @@ '404 Redirect': [true, 'Redirect dead threads and images.'], 'Keybinds': [true, 'Bind actions to keyboard shortcuts.'], 'Time Formatting': [true, 'Localize and format timestamps arbitrarily.'], - 'Relative Post Dates': [false, 'Display dates like "3 minutes ago". Tooltip shows the timestamp.'], + 'Relative Post Dates': [true, 'Display dates like "3 minutes ago". Tooltip shows the timestamp.'], 'File Info Formatting': [true, 'Reformat the file information.'], 'Comment Expansion': [true, 'Can expand too long comments.'], 'Thread Expansion': [true, 'Can expand threads to view all replies.'], @@ -3329,16 +3329,15 @@ node: function() { var dateEl; if (this.isClone) { - RelativeDates.flush(); return; } dateEl = this.nodes.date; dateEl.title = dateEl.textContent; return RelativeDates.setUpdate(this); }, - relative: function(diff) { - var number, rounded, unit; - unit = (number = diff / $.DAY) > 1 ? 'day' : (number = diff / $.HOUR) > 1 ? 'hour' : (number = diff / $.MINUTE) > 1 ? 'minute' : (number = Math.max(0, diff) / $.SECOND, 'second'); + relative: function(diff, now, date) { + var days, months, number, rounded, unit, years; + unit = (number = diff / $.DAY) >= 1 ? (years = now.getYear() - date.getYear(), months = now.getMonth() - date.getMonth(), days = now.getDate() - date.getDate(), years > 1 ? (number = years - (months < 0 || months === 0 && days < 0), 'year') : years === 1 && (months > 0 || months === 0 && days >= 0) ? (number = years, 'year') : (months = (months + 12) % 12) > 1 ? (number = months - (days < 0), 'month') : months === 1 && days >= 0 ? (number = months, 'month') : 'day') : (number = diff / $.HOUR) >= 1 ? 'hour' : (number = diff / $.MINUTE) >= 1 ? 'minute' : (number = Math.max(0, diff) / $.SECOND, 'second'); rounded = Math.round(number); if (rounded !== 1) { unit += 's'; @@ -3351,7 +3350,7 @@ if (d.hidden) { return; } - now = Date.now(); + now = new Date(); _ref = RelativeDates.stale; for (_i = 0, _len = _ref.length; _i < _len; _i++) { update = _ref[_i]; @@ -3365,13 +3364,14 @@ var markStale, setOwnTimeout, update; setOwnTimeout = function(diff) { var delay; - delay = diff > $.DAY ? diff % $.DAY : diff > $.HOUR ? diff % $.HOUR : diff > $.MINUTE ? diff % $.MINUTE : diff % $.SECOND; + delay = diff >= $.DAY ? diff % $.DAY : diff >= $.HOUR ? diff % $.HOUR : diff >= $.MINUTE ? diff % $.MINUTE : diff % $.SECOND; return setTimeout(markStale, delay); }; update = function(now) { - var diff, relative, singlePost, _i, _len, _ref; - diff = now - post.info.date; - relative = RelativeDates.relative(diff); + var date, diff, relative, singlePost, _i, _len, _ref; + date = post.info.date; + diff = now - date; + relative = RelativeDates.relative(diff, now, date); _ref = [post].concat(post.clones); for (_i = 0, _len = _ref.length; _i < _len; _i++) { singlePost = _ref[_i]; @@ -3382,7 +3382,7 @@ markStale = function() { return RelativeDates.stale.push(update); }; - return update(Date.now()); + return update(new Date()); } }; diff --git a/src/config.coffee b/src/config.coffee index a97ed93bd..1c57d8a84 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -5,7 +5,7 @@ Config = '404 Redirect': [true, 'Redirect dead threads and images.'] 'Keybinds': [true, 'Bind actions to keyboard shortcuts.'] 'Time Formatting': [true, 'Localize and format timestamps arbitrarily.'] - 'Relative Post Dates': [false, 'Display dates like "3 minutes ago". Tooltip shows the timestamp.'] + 'Relative Post Dates': [true, 'Display dates like "3 minutes ago". Tooltip shows the timestamp.'] 'File Info Formatting': [true, 'Reformat the file information.'] 'Comment Expansion': [true, 'Can expand too long comments.'] 'Thread Expansion': [true, 'Can expand threads to view all replies.'] diff --git a/src/features.coffee b/src/features.coffee index 061cffd3e..b1e1df929 100644 --- a/src/features.coffee +++ b/src/features.coffee @@ -2009,9 +2009,7 @@ RelativeDates = name: 'Relative Post Dates' cb: @node node: -> - if @isClone - RelativeDates.flush() - return + return if @isClone # Show original absolute time as tooltip so users can still know exact times # Since "Time Formatting" runs `node` before us, the title tooltip will @@ -2022,12 +2020,28 @@ RelativeDates = RelativeDates.setUpdate @ # diff is milliseconds from now - relative: (diff) -> - unit = if (number = (diff / $.DAY)) > 1 - 'day' - else if (number = (diff / $.HOUR)) > 1 + relative: (diff, now, date) -> + unit = if (number = (diff / $.DAY)) >= 1 + years = now.getYear() - date.getYear() + months = now.getMonth() - date.getMonth() + days = now.getDate() - date.getDate() + if years > 1 + number = years - (months < 0 or months is 0 and days < 0) + 'year' + else if years is 1 and (months > 0 or months is 0 and days >= 0) + number = years + 'year' + else if (months = (months+12)%12 ) > 1 + number = months - (days < 0) + 'month' + else if months is 1 and days >= 0 + number = months + 'month' + else + 'day' + else if (number = (diff / $.HOUR)) >= 1 'hour' - else if (number = (diff / $.MINUTE)) > 1 + else if (number = (diff / $.MINUTE)) >= 1 'minute' else # prevent "-1 seconds ago" @@ -2052,7 +2066,7 @@ RelativeDates = # no point in changing the dates until the user sees them return if d.hidden - now = Date.now() + now = new Date() update now for update in RelativeDates.stale RelativeDates.stale = [] @@ -2065,19 +2079,20 @@ RelativeDates = # re-add `update()` to the stale list later. setUpdate: (post) -> setOwnTimeout = (diff) -> - delay = if diff > $.DAY + delay = if diff >= $.DAY diff % $.DAY - else if diff > $.HOUR + else if diff >= $.HOUR diff % $.HOUR - else if diff > $.MINUTE + else if diff >= $.MINUTE diff % $.MINUTE else diff % $.SECOND setTimeout markStale, delay update = (now) -> - diff = now - post.info.date - relative = RelativeDates.relative diff + {date} = post.info + diff = now - date + relative = RelativeDates.relative diff, now, date for singlePost in [post].concat post.clones singlePost.nodes.date.textContent = relative setOwnTimeout diff @@ -2085,7 +2100,7 @@ RelativeDates = markStale = -> RelativeDates.stale.push update # kick off initial timeout with current diff - update Date.now() + update new Date() FileInfo = init: ->