forked from mattbradley/livestampjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
livestamp.min.js
1 lines (1 loc) · 1.48 KB
/
livestamp.min.js
1
(function(a){if(typeof define==="function"&&define.amd){define(["jquery","moment"],a)}else{a(jQuery,moment)}}(function(d,j){var a=1000,g=false,f=d([]),i=function(){h.resume()},e=function(k,m){var n=k.data("livestampdata");if(typeof m=="number"){m*=1000}k.removeAttr("data-livestamp").removeData("livestamp");m=j(m);if(j.isMoment(m)&&!isNaN(+m)){var l=d.extend({},{original:k.contents()},n);l.moment=j(m);k.data("livestampdata",l).empty();f.push(k[0])}},c=function(){if(g){return}h.update();setTimeout(c,a)},h={update:function(){d("[data-livestamp]").each(function(){var l=d(this);e(l,l.data("livestamp"))});var k=[];f.each(function(){var n=d(this),l=n.data("livestampdata");if(l===undefined){k.push(this)}else{if(j.isMoment(l.moment)){var p=n.html(),o=l.moment.fromNow();if(p!=o){var m=d.Event("change.livestamp");n.trigger(m,[p,o]);if(!m.isDefaultPrevented()){n.html(o)}}}}});f=f.not(k);delete f.prevObject},pause:function(){g=true},resume:function(){g=false;c()},interval:function(k){if(k===undefined){return a}a=k}},b={add:function(k,l){if(typeof l=="number"){l*=1000}l=j(l);if(j.isMoment(l)&&!isNaN(+l)){k.each(function(){e(d(this),l)});h.update()}return k},destroy:function(k){f=f.not(k);k.each(function(){var m=d(this),l=m.data("livestampdata");if(l===undefined){return k}m.html(l.original?l.original:"").removeData("livestampdata")});return k},isLivestamp:function(k){return k.data("livestampdata")!==undefined}};d.livestamp=h;d(i);d.fn.livestamp=function(l,k){if(!b[l]){k=l;l="add"}return b[l](this,k)}}));