Mercurial > hg > toolbox
comparison toolbox/static/js/jquery.timeago.js @ 0:b0942f44413f
import from git://github.com/mozilla/toolbox.git
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 11 May 2014 09:15:35 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b0942f44413f |
---|---|
1 /* | |
2 * timeago: a jQuery plugin, version: 0.9.3 (2011-01-21) | |
3 * @requires jQuery v1.2.3 or later | |
4 * | |
5 * Timeago is a jQuery plugin that makes it easy to support automatically | |
6 * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago"). | |
7 * | |
8 * For usage and examples, visit: | |
9 * http://timeago.yarp.com/ | |
10 * | |
11 * Licensed under the MIT: | |
12 * http://www.opensource.org/licenses/mit-license.php | |
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 distanceMillis = Math.abs(distanceMillis); | |
61 } | |
62 | |
63 var seconds = distanceMillis / 1000; | |
64 var minutes = seconds / 60; | |
65 var hours = minutes / 60; | |
66 var days = hours / 24; | |
67 var years = days / 365; | |
68 | |
69 function substitute(stringOrFunction, number) { | |
70 var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction; | |
71 var value = ($l.numbers && $l.numbers[number]) || number; | |
72 return string.replace(/%d/i, value); | |
73 } | |
74 | |
75 var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || | |
76 seconds < 90 && substitute($l.minute, 1) || | |
77 minutes < 45 && substitute($l.minutes, Math.round(minutes)) || | |
78 minutes < 90 && substitute($l.hour, 1) || | |
79 hours < 24 && substitute($l.hours, Math.round(hours)) || | |
80 hours < 48 && substitute($l.day, 1) || | |
81 days < 30 && substitute($l.days, Math.floor(days)) || | |
82 days < 60 && substitute($l.month, 1) || | |
83 days < 365 && substitute($l.months, Math.floor(days / 30)) || | |
84 years < 2 && substitute($l.year, 1) || | |
85 substitute($l.years, Math.floor(years)); | |
86 | |
87 return $.trim([prefix, words, suffix].join(" ")); | |
88 }, | |
89 parse: function(iso8601) { | |
90 var s = $.trim(iso8601); | |
91 s = s.replace(/\.\d\d\d+/,""); // remove milliseconds | |
92 s = s.replace(/-/,"/").replace(/-/,"/"); | |
93 s = s.replace(/T/," ").replace(/Z/," UTC"); | |
94 s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400 | |
95 return new Date(s); | |
96 }, | |
97 datetime: function(elem) { | |
98 // jQuery's `is()` doesn't play well with HTML5 in IE | |
99 var isTime = $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time"); | |
100 var iso8601 = isTime ? $(elem).attr("datetime") : $(elem).attr("title"); | |
101 return $t.parse(iso8601); | |
102 } | |
103 }); | |
104 | |
105 $.fn.timeago = function() { | |
106 var self = this; | |
107 self.each(refresh); | |
108 | |
109 var $s = $t.settings; | |
110 if ($s.refreshMillis > 0) { | |
111 setInterval(function() { self.each(refresh); }, $s.refreshMillis); | |
112 } | |
113 return self; | |
114 }; | |
115 | |
116 function refresh() { | |
117 var data = prepareData(this); | |
118 if (!isNaN(data.datetime)) { | |
119 $(this).text(inWords(data.datetime)); | |
120 } | |
121 return this; | |
122 } | |
123 | |
124 function prepareData(element) { | |
125 element = $(element); | |
126 if (!element.data("timeago")) { | |
127 element.data("timeago", { datetime: $t.datetime(element) }); | |
128 var text = $.trim(element.text()); | |
129 if (text.length > 0) { | |
130 element.attr("title", text); | |
131 } | |
132 } | |
133 return element.data("timeago"); | |
134 } | |
135 | |
136 function inWords(date) { | |
137 return $t.inWords(distance(date)); | |
138 } | |
139 | |
140 function distance(date) { | |
141 return (new Date().getTime() - date.getTime()); | |
142 } | |
143 | |
144 // fix for IE6 suckage | |
145 document.createElement("abbr"); | |
146 document.createElement("time"); | |
147 }(jQuery)); |