ago.js fixes
This commit is contained in:
@@ -1,27 +1,36 @@
|
|||||||
function Ago(nodes, options) {
|
function Ago(nodes, options) {
|
||||||
|
if (!(nodes instanceof Array)) {
|
||||||
|
options = nodes;
|
||||||
|
nodes = undefined;
|
||||||
|
}
|
||||||
nodes = nodes || document.querySelectorAll("time");
|
nodes = nodes || document.querySelectorAll("time");
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
|
|
||||||
var default_opts = {
|
var default_opts = {
|
||||||
interval: 10000, // 10 secs
|
interval: 10000, // 10 secs
|
||||||
units: {
|
units: [
|
||||||
minute: 60,
|
["minute", 60],
|
||||||
hour: 3600,
|
["hour", 3600],
|
||||||
day: 86400,
|
["day", 86400],
|
||||||
week: 604800,
|
["week", 604800],
|
||||||
month: 2592000,
|
["month", 2592000],
|
||||||
year: 31536000
|
["year", 31536000]
|
||||||
},
|
],
|
||||||
date: function(node) {
|
date: function(node) {
|
||||||
// works on HTML 'time' nodes
|
// works on HTML "time" nodes
|
||||||
return new Date(node.dateTime);
|
return new Date(node.dateTime);
|
||||||
},
|
},
|
||||||
format: "{v} {u} {r}",
|
format: function(time, unit) {
|
||||||
words: {
|
if (!unit) {
|
||||||
now: "just now",
|
return "just now";
|
||||||
ago: "ago",
|
}
|
||||||
ahead: "ahead"
|
if (time < 0) {
|
||||||
|
var tail = " ahead";
|
||||||
|
} else {
|
||||||
|
var tail = " ago";
|
||||||
|
}
|
||||||
|
return Math.abs(time) + " " + unit + tail;
|
||||||
},
|
},
|
||||||
plural: {
|
plural: {
|
||||||
minute: "minutes",
|
minute: "minutes",
|
||||||
@@ -43,37 +52,30 @@ function Ago(nodes, options) {
|
|||||||
// use callback to get date
|
// use callback to get date
|
||||||
var ago_date = options.date(node);
|
var ago_date = options.date(node);
|
||||||
// get seconds ago
|
// get seconds ago
|
||||||
ago_time = (new Date().getTime() - ago_date.getTime()) / 1000;
|
var ago_time = (new Date().getTime() - ago_date.getTime()) / 1000;
|
||||||
ago_time = Math.floor(Math.abs(ago_time));
|
var abs_time = Math.floor(Math.abs(ago_time));
|
||||||
|
|
||||||
// find greatest unit
|
// find greatest unit
|
||||||
var unit = null;
|
var unit = null;
|
||||||
var unit_time = null;
|
var unit_time = null;
|
||||||
for (var u in options.units) {
|
for (var u in options.units) {
|
||||||
var secs = options.units[u];
|
var secs = options.units[u][1];
|
||||||
if (ago_time >= secs) {
|
if (abs_time >= secs) {
|
||||||
unit = u;
|
unit = options.units[u][0];
|
||||||
unit_time = secs;
|
unit_time = secs;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var output = null;
|
|
||||||
if (unit_time !== null) {
|
if (unit_time !== null) {
|
||||||
ago_time = Math.floor(ago_time/unit_time);
|
abs_time = Math.floor(abs_time/unit_time);
|
||||||
// plural
|
// plural
|
||||||
if (ago_time != 1) unit = options.plural[unit];
|
if (abs_time != 1) {
|
||||||
// future or past?
|
unit = options.plural[unit];
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
node.textContent = options.format(ago_time < 0 ? -abs_time : abs_time, unit);
|
||||||
node.textContent = output;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -42,5 +42,5 @@ $(function(){
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var ago = new Ago();
|
Ago();
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user