You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

185 lines
5.7 KiB

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