From 110bc2a248fe9cf0cbda737e8b9075090a80c0c2 Mon Sep 17 00:00:00 2001 From: jomo Date: Sat, 15 Nov 2014 21:00:00 +0100 Subject: [PATCH] redo ago.js, remove therubyracer gem --- Gemfile | 1 - Gemfile.lock | 6 -- app/assets/javascripts/ago.js | 106 ++++++++++++++++++++++++---------- app/assets/javascripts/app.js | 11 +--- 4 files changed, 79 insertions(+), 45 deletions(-) diff --git a/Gemfile b/Gemfile index cc45f66..5bf77ea 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,6 @@ gem 'rails', '4.1.0' gem 'mysql2' gem 'jquery-rails' -gem 'therubyracer' gem 'bcrypt-ruby' # To use ActiveModel's has_secure_password gem 'sanitize' gem 'redcarpet' diff --git a/Gemfile.lock b/Gemfile.lock index ecad53f..34c384e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,7 +79,6 @@ GEM thor (>= 0.14, < 2.0) json (1.8.1) kgio (2.9.2) - libv8 (3.16.14.3) mail (2.5.4) mime-types (~> 1.16) treetop (~> 1.4.8) @@ -125,7 +124,6 @@ GEM rake (10.2.2) rb-readline (0.5.1) redcarpet (3.1.1) - ref (1.0.5) rest-client (1.6.7) mime-types (>= 1.16) ruby-graphviz (1.0.9) @@ -146,9 +144,6 @@ GEM actionpack (>= 3.0) activesupport (>= 3.0) sprockets (~> 2.8) - therubyracer (0.12.1) - libv8 (~> 3.16.14.0) - ref thor (0.19.1) thread_safe (0.3.3) tilt (1.4.1) @@ -188,7 +183,6 @@ DEPENDENCIES rest-client sanitize sass-rails - therubyracer uglifier unicorn webrick diff --git a/app/assets/javascripts/ago.js b/app/assets/javascripts/ago.js index 1e8c432..3ac48fa 100644 --- a/app/assets/javascripts/ago.js +++ b/app/assets/javascripts/ago.js @@ -1,44 +1,92 @@ -$.fn.ago = function(callback) { - units = { // seconds - minute: 60, - //ocd: 100, - hour: 3600, - day: 86400, - week: 604800, - month: 2592000, - year: 31536000 +function Ago(nodes, options) { + nodes = nodes || document.querySelectorAll("time"); + options = options || {}; + + + var default_opts = { + interval: 10000, // 10 secs + units: { + 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() { - // use the callback or parse time from the node's text - ago_date = callback ? callback(this) : new Date($(this).text()); + // override default options + for (var key in default_opts) { + 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 ago_time = (new Date().getTime() - ago_date.getTime()) / 1000; ago_time = Math.floor(Math.abs(ago_time)); - // find unit - var unit_string = null; - var unit_time = null; - for (var unit in units) { - var secs = units[unit]; + // find greatest unit + var unit = null; + var unit_time = null; + for (var u in options.units) { + var secs = options.units[u]; if (ago_time >= secs) { - unit_string = unit; - unit_time = secs; - } else { - // we found the greatest unit - break; + unit = u; + unit_time = secs; } } - var ago_str = "just now"; + var output = null; if (unit_time !== null) { ago_time = Math.floor(ago_time/unit_time); - if (ago_time != 1) unit_string += "s"; // plural - ago_str = ago_time.toString() + " " + unit_string; + // plural + if (ago_time != 1) unit = options.plural[unit]; // 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); - }); -}; \ No newline at end of file + 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); + +} \ No newline at end of file diff --git a/app/assets/javascripts/app.js b/app/assets/javascripts/app.js index 9337f79..e91bbc8 100644 --- a/app/assets/javascripts/app.js +++ b/app/assets/javascripts/app.js @@ -42,12 +42,5 @@ $(function(){ } }); - updateTimestamps(); - setInterval(updateTimestamps, 1000*10); -}); - -function updateTimestamps() { - $('time').ago(function(elem){ - return new Date($(elem).attr('datetime')); - }); -} \ No newline at end of file + var ago = new Ago(); +}); \ No newline at end of file