redo ago.js, remove therubyracer gem

This commit is contained in:
jomo
2014-11-15 21:00:00 +01:00
parent ebab21445b
commit 110bc2a248
4 changed files with 79 additions and 45 deletions

View File

@@ -4,7 +4,6 @@ gem 'rails', '4.1.0'
gem 'mysql2' gem 'mysql2'
gem 'jquery-rails' gem 'jquery-rails'
gem 'therubyracer'
gem 'bcrypt-ruby' # To use ActiveModel's has_secure_password gem 'bcrypt-ruby' # To use ActiveModel's has_secure_password
gem 'sanitize' gem 'sanitize'
gem 'redcarpet' gem 'redcarpet'

View File

@@ -79,7 +79,6 @@ GEM
thor (>= 0.14, < 2.0) thor (>= 0.14, < 2.0)
json (1.8.1) json (1.8.1)
kgio (2.9.2) kgio (2.9.2)
libv8 (3.16.14.3)
mail (2.5.4) mail (2.5.4)
mime-types (~> 1.16) mime-types (~> 1.16)
treetop (~> 1.4.8) treetop (~> 1.4.8)
@@ -125,7 +124,6 @@ GEM
rake (10.2.2) rake (10.2.2)
rb-readline (0.5.1) rb-readline (0.5.1)
redcarpet (3.1.1) redcarpet (3.1.1)
ref (1.0.5)
rest-client (1.6.7) rest-client (1.6.7)
mime-types (>= 1.16) mime-types (>= 1.16)
ruby-graphviz (1.0.9) ruby-graphviz (1.0.9)
@@ -146,9 +144,6 @@ GEM
actionpack (>= 3.0) actionpack (>= 3.0)
activesupport (>= 3.0) activesupport (>= 3.0)
sprockets (~> 2.8) sprockets (~> 2.8)
therubyracer (0.12.1)
libv8 (~> 3.16.14.0)
ref
thor (0.19.1) thor (0.19.1)
thread_safe (0.3.3) thread_safe (0.3.3)
tilt (1.4.1) tilt (1.4.1)
@@ -188,7 +183,6 @@ DEPENDENCIES
rest-client rest-client
sanitize sanitize
sass-rails sass-rails
therubyracer
uglifier uglifier
unicorn unicorn
webrick webrick

View File

@@ -1,44 +1,92 @@
$.fn.ago = function(callback) { function Ago(nodes, options) {
units = { // seconds nodes = nodes || document.querySelectorAll("time");
minute: 60, options = options || {};
//ocd: 100,
hour: 3600,
day: 86400, var default_opts = {
week: 604800, interval: 10000, // 10 secs
month: 2592000, units: {
year: 31536000 minute: 60,
hour: 3600,
day: 86400,
week: 604800,
month: 2592000,
year: 31536000
},
date: function(node) {
// works on HTML 'time' nodes
return new Date(node.dateTime);
},
format: "{v} {u} {r}",
words: {
now: "just now",
ago: "ago",
ahead: "ahead"
},
plural: {
minute: "minutes",
hour: "hours",
day: "days",
week: "weeks",
month: "months",
year: "years"
}
}; };
this.each(function() { // override default options
// use the callback or parse time from the node's text for (var key in default_opts) {
ago_date = callback ? callback(this) : new Date($(this).text()); options[key] = options[key] || default_opts[key];
}
var ago = function(node) {
// use callback to get date
var ago_date = options.date(node);
// get seconds ago // get seconds ago
ago_time = (new Date().getTime() - ago_date.getTime()) / 1000; ago_time = (new Date().getTime() - ago_date.getTime()) / 1000;
ago_time = Math.floor(Math.abs(ago_time)); ago_time = Math.floor(Math.abs(ago_time));
// find unit // find greatest unit
var unit_string = null; var unit = null;
var unit_time = null; var unit_time = null;
for (var unit in units) { for (var u in options.units) {
var secs = units[unit]; var secs = options.units[u];
if (ago_time >= secs) { if (ago_time >= secs) {
unit_string = unit; unit = u;
unit_time = secs; unit_time = secs;
} else {
// we found the greatest unit
break;
} }
} }
var ago_str = "just now"; var output = null;
if (unit_time !== null) { if (unit_time !== null) {
ago_time = Math.floor(ago_time/unit_time); ago_time = Math.floor(ago_time/unit_time);
if (ago_time != 1) unit_string += "s"; // plural // plural
ago_str = ago_time.toString() + " " + unit_string; if (ago_time != 1) unit = options.plural[unit];
// future or past? // future or past?
ago_str += (ago_time < 0 ? " ahead" : " ago"); relative = (ago_time < 0 ? options.words.ahead : options.words.ago);
output = options.format
.replace("{v}", ago_time)
.replace("{u}", unit)
.replace("{r}", relative);
} else {
output = options.words.now;
} }
$(this).text(ago_str); node.textContent = output;
}); };
};
var update_all = function() {
for (var i = 0; i < nodes.length; i++) {
ago(nodes[i]);
}
};
update_all();
setInterval(function() {
update_all();
}, options.interval);
}

View File

@@ -42,12 +42,5 @@ $(function(){
} }
}); });
updateTimestamps(); var ago = new Ago();
setInterval(updateTimestamps, 1000*10);
}); });
function updateTimestamps() {
$('time').ago(function(elem){
return new Date($(elem).attr('datetime'));
});
}