[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/js/vendor/ -> livestamp.js (source)

   1  // Livestamp.js / v1.1.2 / (c) 2012 Matt Bradley / MIT License
   2  (function($, moment) {
   3    var updateInterval = 1e3,
   4        paused = false,
   5        $livestamps = $([]),
   6  
   7    init = function() {
   8      livestampGlobal.resume();
   9    },
  10  
  11    prep = function($el, timestamp) {
  12      var oldData = $el.data('livestampdata');
  13      if (typeof timestamp == 'number')
  14        timestamp *= 1e3;
  15  
  16      $el.removeAttr('data-livestamp')
  17        .removeData('livestamp');
  18  
  19      timestamp = moment(timestamp);
  20      if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
  21        var newData = $.extend({ }, { 'original': $el.contents() }, oldData);
  22        newData.moment = moment(timestamp);
  23  
  24        $el.data('livestampdata', newData).empty();
  25        $livestamps.push($el[0]);
  26      }
  27    },
  28  
  29    run = function() {
  30      if (paused) return;
  31      livestampGlobal.update();
  32      setTimeout(run, updateInterval);
  33    },
  34  
  35    livestampGlobal = {
  36      update: function() {
  37        $('[data-livestamp]').each(function() {
  38          var $this = $(this);
  39          prep($this, $this.data('livestamp'));
  40        });
  41  
  42        var toRemove = [];
  43        $livestamps.each(function() {
  44          var $this = $(this),
  45              data = $this.data('livestampdata');
  46  
  47          if (data === undefined)
  48            toRemove.push(this);
  49          else if (moment.isMoment(data.moment)) {
  50            var from = $this.html(),
  51                to = data.moment.fromNow();
  52  
  53            if (from != to) {
  54              var e = $.Event('change.livestamp');
  55              $this.trigger(e, [from, to]);
  56              if (!e.isDefaultPrevented())
  57                $this.html(to);
  58            }
  59          }
  60        });
  61  
  62        $livestamps = $livestamps.not(toRemove);
  63      },
  64  
  65      pause: function() {
  66        paused = true;
  67      },
  68  
  69      resume: function() {
  70        paused = false;
  71        run();
  72      },
  73  
  74      interval: function(interval) {
  75        if (interval === undefined)
  76          return updateInterval;
  77        updateInterval = interval;
  78      }
  79    },
  80  
  81    livestampLocal = {
  82      add: function($el, timestamp) {
  83        if (typeof timestamp == 'number')
  84          timestamp *= 1e3;
  85        timestamp = moment(timestamp);
  86  
  87        if (moment.isMoment(timestamp) && !isNaN(+timestamp)) {
  88          $el.each(function() {
  89            prep($(this), timestamp);
  90          });
  91          livestampGlobal.update();
  92        }
  93  
  94        return $el;
  95      },
  96  
  97      destroy: function($el) {
  98        $livestamps = $livestamps.not($el);
  99        $el.each(function() {
 100          var $this = $(this),
 101              data = $this.data('livestampdata');
 102  
 103          if (data === undefined)
 104            return $el;
 105  
 106          $this
 107            .html(data.original ? data.original : '')
 108            .removeData('livestampdata');
 109        });
 110  
 111        return $el;
 112      },
 113  
 114      isLivestamp: function($el) {
 115        return $el.data('livestampdata') !== undefined;
 116      }
 117    };
 118  
 119    $.livestamp = livestampGlobal;
 120    $(init);
 121    $.fn.livestamp = function(method, options) {
 122      if (!livestampLocal[method]) {
 123        options = method;
 124        method = 'add';
 125      }
 126  
 127      return livestampLocal[method](this, options);
 128    };
 129  })(jQuery, moment);


Generated: Wed Jul 24 01:01:43 2019 Cross-referenced by PHPXref 0.7.1