root / cloudcms / static / cloudcms / js / twitter / jquery.timeago.js @ c1468bcc
History | View | Annotate | Download (4.4 kB)
1 |
/**
|
---|---|
2 |
* Timeago is a jQuery plugin that makes it easy to support automatically
|
3 |
* updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
|
4 |
*
|
5 |
* @name timeago
|
6 |
* @version 0.10.0
|
7 |
* @requires jQuery v1.2.3+
|
8 |
* @author Ryan McGeary
|
9 |
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
|
10 |
*
|
11 |
* For usage and examples, visit:
|
12 |
* http://timeago.yarp.com/
|
13 |
*
|
14 |
* Copyright (c) 2008-2011, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
|
15 |
*/
|
16 |
(function($) { |
17 |
$.timeago = function(timestamp) { |
18 |
if (timestamp instanceof Date) { |
19 |
return inWords(timestamp);
|
20 |
} else if (typeof timestamp === "string") { |
21 |
return inWords($.timeago.parse(timestamp)); |
22 |
} else {
|
23 |
return inWords($.timeago.datetime(timestamp)); |
24 |
} |
25 |
}; |
26 |
var $t = $.timeago; |
27 |
|
28 |
$.extend($.timeago, { |
29 |
settings: {
|
30 |
refreshMillis: 60000, |
31 |
allowFuture: false, |
32 |
strings: {
|
33 |
prefixAgo: null, |
34 |
prefixFromNow: null, |
35 |
suffixAgo: "ago", |
36 |
suffixFromNow: "from now", |
37 |
seconds: "less than a minute", |
38 |
minute: "about a minute", |
39 |
minutes: "%d minutes", |
40 |
hour: "about an hour", |
41 |
hours: "about %d hours", |
42 |
day: "a day", |
43 |
days: "%d days", |
44 |
month: "about a month", |
45 |
months: "%d months", |
46 |
year: "about a year", |
47 |
years: "%d years", |
48 |
numbers: []
|
49 |
} |
50 |
}, |
51 |
inWords: function(distanceMillis) { |
52 |
var $l = this.settings.strings; |
53 |
var prefix = $l.prefixAgo; |
54 |
var suffix = $l.suffixAgo; |
55 |
if (this.settings.allowFuture) { |
56 |
if (distanceMillis < 0) { |
57 |
prefix = $l.prefixFromNow;
|
58 |
suffix = $l.suffixFromNow;
|
59 |
} |
60 |
} |
61 |
|
62 |
var seconds = Math.abs(distanceMillis) / 1000; |
63 |
var minutes = seconds / 60; |
64 |
var hours = minutes / 60; |
65 |
var days = hours / 24; |
66 |
var years = days / 365; |
67 |
|
68 |
function substitute(stringOrFunction, number) { |
69 |
var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction; |
70 |
var value = ($l.numbers && $l.numbers[number]) || number; |
71 |
return string.replace(/%d/i, value); |
72 |
} |
73 |
|
74 |
var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || |
75 |
seconds < 90 && substitute($l.minute, 1) || |
76 |
minutes < 45 && substitute($l.minutes, Math.round(minutes)) || |
77 |
minutes < 90 && substitute($l.hour, 1) || |
78 |
hours < 24 && substitute($l.hours, Math.round(hours)) || |
79 |
hours < 48 && substitute($l.day, 1) || |
80 |
days < 30 && substitute($l.days, Math.floor(days)) || |
81 |
days < 60 && substitute($l.month, 1) || |
82 |
days < 365 && substitute($l.months, Math.floor(days / 30)) || |
83 |
years < 2 && substitute($l.year, 1) || |
84 |
substitute($l.years, Math.floor(years));
|
85 |
|
86 |
return $.trim([prefix, words, suffix].join(" ")); |
87 |
}, |
88 |
parse: function(iso8601) { |
89 |
var s = $.trim(iso8601); |
90 |
s = s.replace(/\.\d\d\d+/,""); // remove milliseconds |
91 |
s = s.replace(/-/,"/").replace(/-/,"/"); |
92 |
s = s.replace(/T/," ").replace(/Z/," UTC"); |
93 |
s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400 |
94 |
return new Date(s); |
95 |
}, |
96 |
datetime: function(elem) { |
97 |
// jQuery's `is()` doesn't play well with HTML5 in IE
|
98 |
var isTime = $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time"); |
99 |
var iso8601 = isTime ? $(elem).attr("datetime") : $(elem).attr("title"); |
100 |
return $t.parse(iso8601); |
101 |
} |
102 |
}); |
103 |
|
104 |
$.fn.timeago = function() { |
105 |
var self = this; |
106 |
self.each(refresh); |
107 |
|
108 |
var $s = $t.settings; |
109 |
if ($s.refreshMillis > 0) { |
110 |
setInterval(function() { self.each(refresh); }, $s.refreshMillis); |
111 |
} |
112 |
return self;
|
113 |
}; |
114 |
|
115 |
function refresh() { |
116 |
var data = prepareData(this); |
117 |
if (!isNaN(data.datetime)) {
|
118 |
$(this).text(inWords(data.datetime)); |
119 |
} |
120 |
return this; |
121 |
} |
122 |
|
123 |
function prepareData(element) { |
124 |
element = $(element);
|
125 |
if (!element.data("timeago")) { |
126 |
element.data("timeago", { datetime: $t.datetime(element) }); |
127 |
var text = $.trim(element.text()); |
128 |
if (text.length > 0) { |
129 |
element.attr("title", text);
|
130 |
} |
131 |
} |
132 |
return element.data("timeago"); |
133 |
} |
134 |
|
135 |
function inWords(date) { |
136 |
return $t.inWords(distance(date)); |
137 |
} |
138 |
|
139 |
function distance(date) { |
140 |
return (new Date().getTime() - date.getTime()); |
141 |
} |
142 |
|
143 |
// fix for IE6 suckage
|
144 |
document.createElement("abbr");
|
145 |
document.createElement("time");
|
146 |
}(jQuery)); |