Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2154 | arune | 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.11.4 |
||
| 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-2012, Ryan McGeary (ryan -[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 if (typeof timestamp === "number") { |
||
| 23 | return inWords(new Date(timestamp)); |
||
| 24 | } else { |
||
| 25 | return inWords($.timeago.datetime(timestamp)); |
||
| 26 | } |
||
| 27 | }; |
||
| 28 | var $t = $.timeago; |
||
| 29 | |||
| 30 | $.extend($.timeago, { |
||
| 31 | settings: { |
||
| 32 | refreshMillis: 60000, |
||
| 33 | allowFuture: false, |
||
| 34 | strings: { |
||
| 35 | prefixAgo: null, |
||
| 36 | prefixFromNow: null, |
||
| 37 | suffixAgo: "ago", |
||
| 38 | suffixFromNow: "from now", |
||
| 39 | seconds: "less than a minute", |
||
| 40 | minute: "about a minute", |
||
| 41 | minutes: "%d minutes", |
||
| 42 | hour: "about an hour", |
||
| 43 | hours: "about %d hours", |
||
| 44 | day: "a day", |
||
| 45 | days: "%d days", |
||
| 46 | month: "about a month", |
||
| 47 | months: "%d months", |
||
| 48 | year: "about a year", |
||
| 49 | years: "%d years", |
||
| 50 | wordSeparator: " ", |
||
| 51 | numbers: [] |
||
| 52 | } |
||
| 53 | }, |
||
| 54 | inWords: function(distanceMillis) { |
||
| 55 | var $l = this.settings.strings; |
||
| 56 | var prefix = $l.prefixAgo; |
||
| 57 | var suffix = $l.suffixAgo; |
||
| 58 | if (this.settings.allowFuture) { |
||
| 59 | if (distanceMillis < 0) { |
||
| 60 | prefix = $l.prefixFromNow; |
||
| 61 | suffix = $l.suffixFromNow; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | var seconds = Math.abs(distanceMillis) / 1000; |
||
| 66 | var minutes = seconds / 60; |
||
| 67 | var hours = minutes / 60; |
||
| 68 | var days = hours / 24; |
||
| 69 | var years = days / 365; |
||
| 70 | |||
| 71 | function substitute(stringOrFunction, number) { |
||
| 72 | var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction; |
||
| 73 | var value = ($l.numbers && $l.numbers[number]) || number; |
||
| 74 | return string.replace(/%d/i, value); |
||
| 75 | } |
||
| 76 | |||
| 77 | var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) || |
||
| 78 | seconds < 90 && substitute($l.minute, 1) || |
||
| 79 | minutes < 45 && substitute($l.minutes, Math.round(minutes)) || |
||
| 80 | minutes < 90 && substitute($l.hour, 1) || |
||
| 81 | hours < 24 && substitute($l.hours, Math.round(hours)) || |
||
| 82 | hours < 42 && substitute($l.day, 1) || |
||
| 83 | days < 30 && substitute($l.days, Math.round(days)) || |
||
| 84 | days < 45 && substitute($l.month, 1) || |
||
| 85 | days < 365 && substitute($l.months, Math.round(days / 30)) || |
||
| 86 | years < 1.5 && substitute($l.year, 1) || |
||
| 87 | substitute($l.years, Math.round(years)); |
||
| 88 | |||
| 89 | var separator = $l.wordSeparator === undefined ? " " : $l.wordSeparator; |
||
| 90 | return $.trim([prefix, words, suffix].join(separator)); |
||
| 91 | }, |
||
| 92 | parse: function(iso8601) { |
||
| 93 | var s = $.trim(iso8601); |
||
| 94 | s = s.replace(/\.\d+/,""); // remove milliseconds |
||
| 95 | s = s.replace(/-/,"/").replace(/-/,"/"); |
||
| 96 | s = s.replace(/T/," ").replace(/Z/," UTC"); |
||
| 97 | s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400 |
||
| 98 | return new Date(s); |
||
| 99 | }, |
||
| 100 | datetime: function(elem) { |
||
| 101 | var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title"); |
||
| 102 | return $t.parse(iso8601); |
||
| 103 | }, |
||
| 104 | isTime: function(elem) { |
||
| 105 | // jQuery's `is()` doesn't play well with HTML5 in IE |
||
| 106 | return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time"); |
||
| 107 | } |
||
| 108 | }); |
||
| 109 | |||
| 110 | $.fn.timeago = function() { |
||
| 111 | var self = this; |
||
| 112 | self.each(refresh); |
||
| 113 | |||
| 114 | var $s = $t.settings; |
||
| 115 | if ($s.refreshMillis > 0) { |
||
| 116 | setInterval(function() { self.each(refresh); }, $s.refreshMillis); |
||
| 117 | } |
||
| 118 | return self; |
||
| 119 | }; |
||
| 120 | |||
| 121 | function refresh() { |
||
| 122 | var data = prepareData(this); |
||
| 123 | if (!isNaN(data.datetime)) { |
||
| 124 | $(this).text(inWords(data.datetime)); |
||
| 125 | } |
||
| 126 | return this; |
||
| 127 | } |
||
| 128 | |||
| 129 | function prepareData(element) { |
||
| 130 | element = $(element); |
||
| 131 | if (!element.data("timeago")) { |
||
| 132 | element.data("timeago", { datetime: $t.datetime(element) }); |
||
| 133 | var text = $.trim(element.text()); |
||
| 134 | if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) { |
||
| 135 | element.attr("title", text); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | return element.data("timeago"); |
||
| 139 | } |
||
| 140 | |||
| 141 | function inWords(date) { |
||
| 142 | return $t.inWords(distance(date)); |
||
| 143 | } |
||
| 144 | |||
| 145 | function distance(date) { |
||
| 146 | return (new Date().getTime() - date.getTime()); |
||
| 147 | } |
||
| 148 | |||
| 149 | // fix for IE6 suckage |
||
| 150 | document.createElement("abbr"); |
||
| 151 | document.createElement("time"); |
||
| 152 | }(jQuery)); |