relative timestamps
This commit is contained in:
31
app/assets/javascripts/ago.js
Normal file
31
app/assets/javascripts/ago.js
Normal file
@@ -0,0 +1,31 @@
|
||||
$.fn.ago = function(callback) {
|
||||
units = [
|
||||
['m', 60],
|
||||
['h', 3600],
|
||||
['d', 86400],
|
||||
['w', 604800],
|
||||
['y', 31536000]
|
||||
];
|
||||
this.each(function() {
|
||||
ago_date = callback ? callback(this) : new Date($(this).text());
|
||||
ago_time = Math.floor((new Date().getTime() - ago_date.getTime())/1000);
|
||||
ago_unit = null;
|
||||
units.forEach(function(time, i) {
|
||||
if (Math.abs(ago_time) >= time[1]) {
|
||||
ago_unit = i;
|
||||
} else {
|
||||
// we found the greatest unit
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
if (ago_unit !== null) {
|
||||
unit = units[ago_unit];
|
||||
ago_str = Math.abs(Math.floor(ago_time/unit[1])).toString() + unit[0] + (ago_time < 0 ? " ahead" : " ago");
|
||||
} else {
|
||||
ago_str = "just now";
|
||||
}
|
||||
|
||||
$(this).text(ago_str);
|
||||
});
|
||||
};
|
||||
@@ -8,7 +8,7 @@ $(function(){
|
||||
height: 0
|
||||
}, 'slow', function(){
|
||||
$(this).hide();
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
setTimeout(function(){
|
||||
@@ -19,7 +19,7 @@ $(function(){
|
||||
height: 0
|
||||
}, 'slow', function(){
|
||||
$(this).hide();
|
||||
})
|
||||
});
|
||||
});
|
||||
}, 4000);
|
||||
var pressed = new Array(10);
|
||||
@@ -37,7 +37,15 @@ $(function(){
|
||||
if ($(this).attr("class")) {
|
||||
$(this).parent().attr("lang", $(this).attr("class").replace("hljs", "").trim());
|
||||
} else {
|
||||
$(this).parent().attr("lang", "(language unknown)")
|
||||
$(this).parent().attr("lang", "(language unknown)");
|
||||
}
|
||||
});
|
||||
});
|
||||
updateTimestamps();
|
||||
setInterval(updateTimestamps, 1000*10);
|
||||
});
|
||||
|
||||
function updateTimestamps() {
|
||||
$('time').ago(function(elem){
|
||||
return new Date($(elem).attr('datetime'));
|
||||
});
|
||||
}
|
||||
@@ -12,6 +12,5 @@
|
||||
//= require jquery
|
||||
//= require jquery_ujs
|
||||
//= require app
|
||||
//= require moment
|
||||
//= require ago
|
||||
//= require highlight
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user