|
| 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.0.2 |
| 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 | + |
| 17 | +(function (factory) { |
| 18 | + if (typeof define === 'function' && define.amd) { |
| 19 | + // AMD. Register as an anonymous module. |
| 20 | + define(['jquery'], factory); |
| 21 | + } else { |
| 22 | + // Browser globals |
| 23 | + factory(jQuery); |
| 24 | + } |
| 25 | +}(function ($) { |
| 26 | + $.timeago = function(timestamp) { |
| 27 | + if (timestamp instanceof Date) { |
| 28 | + return inWords(timestamp); |
| 29 | + } else if (typeof timestamp === "string") { |
| 30 | + return inWords($.timeago.parse(timestamp)); |
| 31 | + } else if (typeof timestamp === "number") { |
| 32 | + return inWords(new Date(timestamp)); |
| 33 | + } else { |
| 34 | + return inWords($.timeago.datetime(timestamp)); |
| 35 | + } |
| 36 | + }; |
| 37 | + var $t = $.timeago; |
| 38 | + |
| 39 | + $.extend($.timeago, { |
| 40 | + settings: { |
| 41 | + refreshMillis: 60000, |
| 42 | + allowFuture: false, |
| 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 | + |
| 74 | + var seconds = Math.abs(distanceMillis) / 1000; |
| 75 | + var minutes = seconds / 60; |
| 76 | + var hours = minutes / 60; |
| 77 | + var days = hours / 24; |
| 78 | + var years = days / 365; |
| 79 | + |
| 80 | + function substitute(stringOrFunction, number) { |
| 81 | + var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction; |
| 82 | + var value = ($l.numbers && $l.numbers[number]) || number; |
| 83 | + return string.replace(/%d/i, value); |
| 84 | + } |
| 85 | + |
| 86 | + var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || |
| 87 | + seconds < 90 && substitute($l.minute, 1) || |
| 88 | + minutes < 45 && substitute($l.minutes, Math.round(minutes)) || |
| 89 | + minutes < 90 && substitute($l.hour, 1) || |
| 90 | + hours < 24 && substitute($l.hours, Math.round(hours)) || |
| 91 | + hours < 42 && substitute($l.day, 1) || |
| 92 | + days < 30 && substitute($l.days, Math.round(days)) || |
| 93 | + days < 45 && substitute($l.month, 1) || |
| 94 | + days < 365 && substitute($l.months, Math.round(days / 30)) || |
| 95 | + years < 1.5 && substitute($l.year, 1) || |
| 96 | + substitute($l.years, Math.round(years)); |
| 97 | + |
| 98 | + var separator = $l.wordSeparator || ""; |
| 99 | + if ($l.wordSeparator === undefined) { separator = " "; } |
| 100 | + return $.trim([prefix, words, suffix].join(separator)); |
| 101 | + }, |
| 102 | + parse: function(iso8601) { |
| 103 | + var s = $.trim(iso8601); |
| 104 | + s = s.replace(/\.\d+/,""); // remove milliseconds |
| 105 | + s = s.replace(/-/,"/").replace(/-/,"/"); |
| 106 | + s = s.replace(/T/," ").replace(/Z/," UTC"); |
| 107 | + s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400 |
| 108 | + return new Date(s); |
| 109 | + }, |
| 110 | + datetime: function(elem) { |
| 111 | + var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title"); |
| 112 | + return $t.parse(iso8601); |
| 113 | + }, |
| 114 | + isTime: function(elem) { |
| 115 | + // jQuery's `is()` doesn't play well with HTML5 in IE |
| 116 | + return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time"); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + $.fn.timeago = function() { |
| 121 | + var self = this; |
| 122 | + self.each(refresh); |
| 123 | + |
| 124 | + var $s = $t.settings; |
| 125 | + if ($s.refreshMillis > 0) { |
| 126 | + setInterval(function() { self.each(refresh); }, $s.refreshMillis); |
| 127 | + } |
| 128 | + return self; |
| 129 | + }; |
| 130 | + |
| 131 | + function refresh() { |
| 132 | + var data = prepareData(this); |
| 133 | + if (!isNaN(data.datetime)) { |
| 134 | + $(this).text(inWords(data.datetime)); |
| 135 | + } |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + function prepareData(element) { |
| 140 | + element = $(element); |
| 141 | + if (!element.data("timeago")) { |
| 142 | + element.data("timeago", { datetime: $t.datetime(element) }); |
| 143 | + var text = $.trim(element.text()); |
| 144 | + if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) { |
| 145 | + element.attr("title", text); |
| 146 | + } |
| 147 | + } |
| 148 | + return element.data("timeago"); |
| 149 | + } |
| 150 | + |
| 151 | + function inWords(date) { |
| 152 | + return $t.inWords(distance(date)); |
| 153 | + } |
| 154 | + |
| 155 | + function distance(date) { |
| 156 | + return (new Date().getTime() - date.getTime()); |
| 157 | + } |
| 158 | + |
| 159 | + // fix for IE6 suckage |
| 160 | + document.createElement("abbr"); |
| 161 | + document.createElement("time"); |
| 162 | +})); |
0 commit comments