jquery.timeago.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 1.3.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-2013, Ryan McGeary (ryan -[at]- mcgeary [*dot*] org)
  15. */
  16. (function (factory) {
  17. if (typeof define === 'function' && define.amd) {
  18. // AMD. Register as an anonymous module.
  19. define(['jquery'], factory);
  20. } else {
  21. // Browser globals
  22. factory(jQuery);
  23. }
  24. }(function ($) {
  25. $.timeago = function(timestamp) {
  26. if (timestamp instanceof Date) {
  27. return inWords(timestamp);
  28. } else if (typeof timestamp === "string") {
  29. return inWords($.timeago.parse(timestamp));
  30. } else if (typeof timestamp === "number") {
  31. return inWords(new Date(timestamp));
  32. } else {
  33. return inWords($.timeago.datetime(timestamp));
  34. }
  35. };
  36. var $t = $.timeago;
  37. $.extend($.timeago, {
  38. settings: {
  39. refreshMillis: 60000,
  40. allowFuture: false,
  41. localeTitle: false,
  42. cutoff: 0,
  43. strings: {
  44. prefixAgo: null,
  45. prefixFromNow: null,
  46. suffixAgo: "ago",
  47. suffixFromNow: "from now",
  48. seconds: "less than a minute",
  49. minute: "about a minute",
  50. minutes: "%d minutes",
  51. hour: "about an hour",
  52. hours: "about %d hours",
  53. day: "a day",
  54. days: "%d days",
  55. month: "about a month",
  56. months: "%d months",
  57. year: "about a year",
  58. years: "%d years",
  59. wordSeparator: " ",
  60. numbers: []
  61. }
  62. },
  63. inWords: function(distanceMillis) {
  64. var $l = this.settings.strings;
  65. var prefix = $l.prefixAgo;
  66. var suffix = $l.suffixAgo;
  67. if (this.settings.allowFuture) {
  68. if (distanceMillis < 0) {
  69. prefix = $l.prefixFromNow;
  70. suffix = $l.suffixFromNow;
  71. }
  72. }
  73. var seconds = Math.abs(distanceMillis) / 1000;
  74. var minutes = seconds / 60;
  75. var hours = minutes / 60;
  76. var days = hours / 24;
  77. var years = days / 365;
  78. function substitute(stringOrFunction, number) {
  79. var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
  80. var value = ($l.numbers && $l.numbers[number]) || number;
  81. return string.replace(/%d/i, value);
  82. }
  83. var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
  84. seconds < 90 && substitute($l.minute, 1) ||
  85. minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
  86. minutes < 90 && substitute($l.hour, 1) ||
  87. hours < 24 && substitute($l.hours, Math.round(hours)) ||
  88. hours < 42 && substitute($l.day, 1) ||
  89. days < 30 && substitute($l.days, Math.round(days)) ||
  90. days < 45 && substitute($l.month, 1) ||
  91. days < 365 && substitute($l.months, Math.round(days / 30)) ||
  92. years < 1.5 && substitute($l.year, 1) ||
  93. substitute($l.years, Math.round(years));
  94. var separator = $l.wordSeparator || "";
  95. if ($l.wordSeparator === undefined) { separator = " "; }
  96. return $.trim([prefix, words, suffix].join(separator));
  97. },
  98. parse: function(iso8601) {
  99. var s = $.trim(iso8601);
  100. s = s.replace(/\.\d+/,""); // remove milliseconds
  101. s = s.replace(/-/,"/").replace(/-/,"/");
  102. s = s.replace(/T/," ").replace(/Z/," UTC");
  103. s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
  104. return new Date(s);
  105. },
  106. datetime: function(elem) {
  107. var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title");
  108. return $t.parse(iso8601);
  109. },
  110. isTime: function(elem) {
  111. // jQuery's `is()` doesn't play well with HTML5 in IE
  112. return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
  113. }
  114. });
  115. // functions that can be called via $(el).timeago('action')
  116. // init is default when no action is given
  117. // functions are called with context of a single element
  118. var functions = {
  119. init: function(){
  120. var refresh_el = $.proxy(refresh, this);
  121. refresh_el();
  122. var $s = $t.settings;
  123. if ($s.refreshMillis > 0) {
  124. setInterval(refresh_el, $s.refreshMillis);
  125. }
  126. },
  127. update: function(time){
  128. $(this).data('timeago', { datetime: $t.parse(time) });
  129. refresh.apply(this);
  130. },
  131. updateFromDOM: function(){
  132. $(this).data('timeago', { datetime: $t.parse( $t.isTime(this) ? $(this).attr("datetime") : $(this).attr("title") ) });
  133. refresh.apply(this);
  134. }
  135. };
  136. $.fn.timeago = function(action, options) {
  137. var fn = action ? functions[action] : functions.init;
  138. if(!fn){
  139. throw new Error("Unknown function name '"+ action +"' for timeago");
  140. }
  141. // each over objects here and call the requested function
  142. this.each(function(){
  143. fn.call(this, options);
  144. });
  145. return this;
  146. };
  147. function refresh() {
  148. var data = prepareData(this);
  149. var $s = $t.settings;
  150. if (!isNaN(data.datetime)) {
  151. if ( $s.cutoff == 0 || distance(data.datetime) < $s.cutoff) {
  152. $(this).text(inWords(data.datetime));
  153. }
  154. }
  155. return this;
  156. }
  157. function prepareData(element) {
  158. element = $(element);
  159. if (!element.data("timeago")) {
  160. element.data("timeago", { datetime: $t.datetime(element) });
  161. var text = $.trim(element.text());
  162. if ($t.settings.localeTitle) {
  163. element.attr("title", element.data('timeago').datetime.toLocaleString());
  164. } else if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
  165. element.attr("title", text);
  166. }
  167. }
  168. return element.data("timeago");
  169. }
  170. function inWords(date) {
  171. return $t.inWords(distance(date));
  172. }
  173. function distance(date) {
  174. return (new Date().getTime() - date.getTime());
  175. }
  176. // fix for IE6 suckage
  177. document.createElement("abbr");
  178. document.createElement("time");
  179. }));