scala-news-reader

rss/atom news reader in scala

git clone https://9o.is/git/scala-news-reader.git

jquery.timeago.js

(6111B)


      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) { separator = " "; }
    102       return $.trim([prefix, words, suffix].join(separator));
    103     },
    104     parse: function(iso8601) {
    105       var s = $.trim(iso8601);
    106       s = s.replace(/\.\d+/,""); // remove milliseconds
    107       s = s.replace(/-/,"/").replace(/-/,"/");
    108       s = s.replace(/T/," ").replace(/Z/," UTC");
    109       s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
    110       return new Date(s);
    111     },
    112     datetime: function(elem) {
    113       var iso8601 = $t.isTime(elem) ? $(elem).attr("datetime") : $(elem).attr("title");
    114       return $t.parse(iso8601);
    115     },
    116     isTime: function(elem) {
    117       // jQuery's `is()` doesn't play well with HTML5 in IE
    118       return $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
    119     }
    120   });
    121 
    122   // functions that can be called via $(el).timeago('action')
    123   // init is default when no action is given
    124   // functions are called with context of a single element
    125   var functions = {
    126     init: function(){
    127       var refresh_el = $.proxy(refresh, this);
    128       refresh_el();
    129       var $s = $t.settings;
    130       if ($s.refreshMillis > 0) {
    131         setInterval(refresh_el, $s.refreshMillis);
    132       }
    133     },
    134     update: function(time){
    135       $(this).data('timeago', { datetime: $t.parse(time) });
    136       refresh.apply(this);
    137     },
    138     updateFromDOM: function(){
    139       $(this).data('timeago', { datetime: $t.parse( $t.isTime(this) ? $(this).attr("datetime") : $(this).attr("title") ) });
    140       refresh.apply(this);
    141     }
    142   };
    143 
    144   $.fn.timeago = function(action, options) {
    145     var fn = action ? functions[action] : functions.init;
    146     if(!fn){
    147       throw new Error("Unknown function name '"+ action +"' for timeago");
    148     }
    149     // each over objects here and call the requested function
    150     this.each(function(){
    151       fn.call(this, options);
    152     });
    153     return this;
    154   };
    155 
    156   function refresh() {
    157     var data = prepareData(this);
    158     var $s = $t.settings;
    159 
    160     if (!isNaN(data.datetime)) {
    161       if ( $s.cutoff == 0 || distance(data.datetime) < $s.cutoff) {
    162         $(this).text(inWords(data.datetime));
    163       }
    164     }
    165     return this;
    166   }
    167 
    168   function prepareData(element) {
    169     element = $(element);
    170     if (!element.data("timeago")) {
    171       element.data("timeago", { datetime: $t.datetime(element) });
    172       var text = $.trim(element.text());
    173       if ($t.settings.localeTitle) {
    174         element.attr("title", element.data('timeago').datetime.toLocaleString());
    175       } else if (text.length > 0 && !($t.isTime(element) && element.attr("title"))) {
    176         element.attr("title", text);
    177       }
    178     }
    179     return element.data("timeago");
    180   }
    181 
    182   function inWords(date) {
    183     return $t.inWords(distance(date));
    184   }
    185 
    186   function distance(date) {
    187     return (new Date().getTime() - date.getTime());
    188   }
    189 
    190   // fix for IE6 suckage
    191   document.createElement("abbr");
    192   document.createElement("time");
    193 }));