PageSourceSearch

http://www.neroargento.info/js/jquery.timeago.js

js neroargento.info collected 2026-10-04 02:43:08 UTC 6,350 bytes, 195 lines download raw bytes

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

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.