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.
This commit is contained in:
Nicolas Stepien 2013-02-15 22:01:58 +01:00
parent 2712a8b61e
commit e3a721ade0
3 changed files with 42 additions and 27 deletions

View File

@ -55,7 +55,7 @@
'404 Redirect': [true, 'Redirect dead threads and images.'], '404 Redirect': [true, 'Redirect dead threads and images.'],
'Keybinds': [true, 'Bind actions to keyboard shortcuts.'], 'Keybinds': [true, 'Bind actions to keyboard shortcuts.'],
'Time Formatting': [true, 'Localize and format timestamps arbitrarily.'], '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.'], 'File Info Formatting': [true, 'Reformat the file information.'],
'Comment Expansion': [true, 'Can expand too long comments.'], 'Comment Expansion': [true, 'Can expand too long comments.'],
'Thread Expansion': [true, 'Can expand threads to view all replies.'], 'Thread Expansion': [true, 'Can expand threads to view all replies.'],
@ -3329,16 +3329,15 @@
node: function() { node: function() {
var dateEl; var dateEl;
if (this.isClone) { if (this.isClone) {
RelativeDates.flush();
return; return;
} }
dateEl = this.nodes.date; dateEl = this.nodes.date;
dateEl.title = dateEl.textContent; dateEl.title = dateEl.textContent;
return RelativeDates.setUpdate(this); return RelativeDates.setUpdate(this);
}, },
relative: function(diff) { relative: function(diff, now, date) {
var number, rounded, unit; var days, months, number, rounded, unit, years;
unit = (number = diff / $.DAY) > 1 ? 'day' : (number = diff / $.HOUR) > 1 ? 'hour' : (number = diff / $.MINUTE) > 1 ? 'minute' : (number = Math.max(0, diff) / $.SECOND, 'second'); 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); rounded = Math.round(number);
if (rounded !== 1) { if (rounded !== 1) {
unit += 's'; unit += 's';
@ -3351,7 +3350,7 @@
if (d.hidden) { if (d.hidden) {
return; return;
} }
now = Date.now(); now = new Date();
_ref = RelativeDates.stale; _ref = RelativeDates.stale;
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
update = _ref[_i]; update = _ref[_i];
@ -3365,13 +3364,14 @@
var markStale, setOwnTimeout, update; var markStale, setOwnTimeout, update;
setOwnTimeout = function(diff) { setOwnTimeout = function(diff) {
var delay; 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); return setTimeout(markStale, delay);
}; };
update = function(now) { update = function(now) {
var diff, relative, singlePost, _i, _len, _ref; var date, diff, relative, singlePost, _i, _len, _ref;
diff = now - post.info.date; date = post.info.date;
relative = RelativeDates.relative(diff); diff = now - date;
relative = RelativeDates.relative(diff, now, date);
_ref = [post].concat(post.clones); _ref = [post].concat(post.clones);
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
singlePost = _ref[_i]; singlePost = _ref[_i];
@ -3382,7 +3382,7 @@
markStale = function() { markStale = function() {
return RelativeDates.stale.push(update); return RelativeDates.stale.push(update);
}; };
return update(Date.now()); return update(new Date());
} }
}; };

View File

@ -5,7 +5,7 @@ Config =
'404 Redirect': [true, 'Redirect dead threads and images.'] '404 Redirect': [true, 'Redirect dead threads and images.']
'Keybinds': [true, 'Bind actions to keyboard shortcuts.'] 'Keybinds': [true, 'Bind actions to keyboard shortcuts.']
'Time Formatting': [true, 'Localize and format timestamps arbitrarily.'] '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.'] 'File Info Formatting': [true, 'Reformat the file information.']
'Comment Expansion': [true, 'Can expand too long comments.'] 'Comment Expansion': [true, 'Can expand too long comments.']
'Thread Expansion': [true, 'Can expand threads to view all replies.'] 'Thread Expansion': [true, 'Can expand threads to view all replies.']

View File

@ -2009,9 +2009,7 @@ RelativeDates =
name: 'Relative Post Dates' name: 'Relative Post Dates'
cb: @node cb: @node
node: -> node: ->
if @isClone return if @isClone
RelativeDates.flush()
return
# Show original absolute time as tooltip so users can still know exact times # Show original absolute time as tooltip so users can still know exact times
# Since "Time Formatting" runs `node` before us, the title tooltip will # Since "Time Formatting" runs `node` before us, the title tooltip will
@ -2022,12 +2020,28 @@ RelativeDates =
RelativeDates.setUpdate @ RelativeDates.setUpdate @
# diff is milliseconds from now # diff is milliseconds from now
relative: (diff) -> relative: (diff, now, date) ->
unit = if (number = (diff / $.DAY)) > 1 unit = if (number = (diff / $.DAY)) >= 1
'day' years = now.getYear() - date.getYear()
else if (number = (diff / $.HOUR)) > 1 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' 'hour'
else if (number = (diff / $.MINUTE)) > 1 else if (number = (diff / $.MINUTE)) >= 1
'minute' 'minute'
else else
# prevent "-1 seconds ago" # prevent "-1 seconds ago"
@ -2052,7 +2066,7 @@ RelativeDates =
# no point in changing the dates until the user sees them # no point in changing the dates until the user sees them
return if d.hidden return if d.hidden
now = Date.now() now = new Date()
update now for update in RelativeDates.stale update now for update in RelativeDates.stale
RelativeDates.stale = [] RelativeDates.stale = []
@ -2065,19 +2079,20 @@ RelativeDates =
# re-add `update()` to the stale list later. # re-add `update()` to the stale list later.
setUpdate: (post) -> setUpdate: (post) ->
setOwnTimeout = (diff) -> setOwnTimeout = (diff) ->
delay = if diff > $.DAY delay = if diff >= $.DAY
diff % $.DAY diff % $.DAY
else if diff > $.HOUR else if diff >= $.HOUR
diff % $.HOUR diff % $.HOUR
else if diff > $.MINUTE else if diff >= $.MINUTE
diff % $.MINUTE diff % $.MINUTE
else else
diff % $.SECOND diff % $.SECOND
setTimeout markStale, delay setTimeout markStale, delay
update = (now) -> update = (now) ->
diff = now - post.info.date {date} = post.info
relative = RelativeDates.relative diff diff = now - date
relative = RelativeDates.relative diff, now, date
for singlePost in [post].concat post.clones for singlePost in [post].concat post.clones
singlePost.nodes.date.textContent = relative singlePost.nodes.date.textContent = relative
setOwnTimeout diff setOwnTimeout diff
@ -2085,7 +2100,7 @@ RelativeDates =
markStale = -> RelativeDates.stale.push update markStale = -> RelativeDates.stale.push update
# kick off initial timeout with current diff # kick off initial timeout with current diff
update Date.now() update new Date()
FileInfo = FileInfo =
init: -> init: ->