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