angularcountdown
Version:
UI Component For Angular 1.x Applications
7 lines • 7.49 kB
JavaScript
/**
* UI Component For Angular 1.x Applications
* @version v0.1.1 - 2016-01-07 * @link https://github.com/jacobscarter/angular-countdown
* @author Jacob Carter <jacob@ieksolutions.com>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
;angular.module("angular-countdown",["templates-angularcountdown"]),angular.module("templates-angularcountdown",["angularcountdown.html"]),angular.module("angularcountdown.html",[]).run(["$templateCache",function(a){a.put("angularcountdown.html",'<div id="countdown" ng-if="timespan">\n <div class="redballdays" id="days"></div>\n <div class="redballhours" id="hours"></div>\n <div class="redballminutes" id="minutes"></div>\n <div class="redballseconds" id="seconds"></div>\n</div>')}]),angular.module("angular-countdown",["templates-angularcountdown"]),angular.module("angular-countdown").directive("countdown",["$interval",function(a){var b,c=function(a){function b(a,b){var c=a.getTime();return a.setMonth(a.getMonth()+b),Math.round((a.getTime()-c)/J)}function c(a){var b=a.getTime(),c=new Date(b);return c.setMonth(a.getMonth()+1),Math.round((c.getTime()-b)/J)}function d(a){var b=a.getTime(),c=new Date(b);return c.setFullYear(a.getFullYear()+1),Math.round((c.getTime()-b)/J)}function e(a,b){return a+" "+(1===a?o[b]:p[b])}function f(){}function g(a,b){switch(b){case"seconds":if(a.seconds!==G||isNaN(a.minutes))return;a.minutes++,a.seconds=0;case"minutes":if(a.minutes!==H||isNaN(a.hours))return;a.hours++,a.minutes=0;case"hours":if(a.hours!==I||isNaN(a.days))return;a.days++,a.hours=0;case"days":if(a.days!==K||isNaN(a.weeks))return;a.weeks++,a.days=0;case"weeks":if(a.weeks!==c(a.refMonth)/K||isNaN(a.months))return;a.months++,a.weeks=0;case"months":if(a.months!==L||isNaN(a.years))return;a.years++,a.months=0;case"years":if(a.years!==M||isNaN(a.decades))return;a.decades++,a.years=0;case"decades":if(a.decades!==N||isNaN(a.centuries))return;a.centuries++,a.decades=0;case"centuries":if(a.centuries!==O||isNaN(a.millennia))return;a.millennia++,a.centuries=0}}function h(a,b,c,d,e,f){return a[c]>=0&&(b+=a[c],delete a[c]),b/=e,1>=b+1?0:a[d]>=0?(a[d]=+(a[d]+b).toFixed(f),g(a,d),0):b}function i(a,b){var e=h(a,0,"milliseconds","seconds",F,b);if(e&&(e=h(a,e,"seconds","minutes",G,b),e&&(e=h(a,e,"minutes","hours",H,b),e&&(e=h(a,e,"hours","days",I,b),e&&(e=h(a,e,"days","weeks",K,b),e&&(e=h(a,e,"weeks","months",c(a.refMonth)/K,b),e&&(e=h(a,e,"months","years",d(a.refMonth)/c(a.refMonth),b),e&&(e=h(a,e,"years","decades",M,b),e&&(e=h(a,e,"decades","centuries",N,b),e&&(e=h(a,e,"centuries","millennia",O,b)))))))))))throw new Error("Fractional unit overflow")}function j(a){var c;for(a.milliseconds<0?(c=P(-a.milliseconds/F),a.seconds-=c,a.milliseconds+=c*F):a.milliseconds>=F&&(a.seconds+=Q(a.milliseconds/F),a.milliseconds%=F),a.seconds<0?(c=P(-a.seconds/G),a.minutes-=c,a.seconds+=c*G):a.seconds>=G&&(a.minutes+=Q(a.seconds/G),a.seconds%=G),a.minutes<0?(c=P(-a.minutes/H),a.hours-=c,a.minutes+=c*H):a.minutes>=H&&(a.hours+=Q(a.minutes/H),a.minutes%=H),a.hours<0?(c=P(-a.hours/I),a.days-=c,a.hours+=c*I):a.hours>=I&&(a.days+=Q(a.hours/I),a.hours%=I);a.days<0;)a.months--,a.days+=b(a.refMonth,1);a.days>=K&&(a.weeks+=Q(a.days/K),a.days%=K),a.months<0?(c=P(-a.months/L),a.years-=c,a.months+=c*L):a.months>=L&&(a.years+=Q(a.months/L),a.months%=L),a.years>=M&&(a.decades+=Q(a.years/M),a.years%=M,a.decades>=N&&(a.centuries+=Q(a.decades/N),a.decades%=N,a.centuries>=O&&(a.millennia+=Q(a.centuries/O),a.centuries%=O)))}function k(a,c,d,e){var f=0;!(c&D)||f>=d?(a.centuries+=a.millennia*O,delete a.millennia):a.millennia&&f++,!(c&C)||f>=d?(a.decades+=a.centuries*N,delete a.centuries):a.centuries&&f++,!(c&B)||f>=d?(a.years+=a.decades*M,delete a.decades):a.decades&&f++,!(c&A)||f>=d?(a.months+=a.years*L,delete a.years):a.years&&f++,!(c&z)||f>=d?(a.months&&(a.days+=b(a.refMonth,a.months)),delete a.months,a.days>=K&&(a.weeks+=Q(a.days/K),a.days%=K)):a.months&&f++,!(c&y)||f>=d?(a.days+=a.weeks*K,delete a.weeks):a.weeks&&f++,!(c&x)||f>=d?(a.hours+=a.days*I,delete a.days):a.days&&f++,!(c&w)||f>=d?(a.minutes+=a.hours*H,delete a.hours):a.hours&&f++,!(c&v)||f>=d?(a.seconds+=a.minutes*G,delete a.minutes):a.minutes&&f++,!(c&u)||f>=d?(a.milliseconds+=a.seconds*F,delete a.seconds):a.seconds&&f++,(!(c&t)||f>=d)&&i(a,e)}function l(a,b,c,d,e,f){var g=new Date;if(a.start=b=b||g,a.end=c=c||g,a.units=d,a.value=c.getTime()-b.getTime(),a.value<0){var h=c;c=b,b=h}a.refMonth=new Date(b.getFullYear(),b.getMonth(),15,12,0,0);try{a.millennia=0,a.centuries=0,a.decades=0,a.years=c.getFullYear()-b.getFullYear(),a.months=c.getMonth()-b.getMonth(),a.weeks=0,a.days=c.getDate()-b.getDate(),a.hours=c.getHours()-b.getHours(),a.minutes=c.getMinutes()-b.getMinutes(),a.seconds=c.getSeconds()-b.getSeconds(),a.milliseconds=c.getMilliseconds()-b.getMilliseconds(),j(a),k(a,d,e,f)}finally{delete a.refMonth}return a}function m(a){return a&t?F/30:a&u?F:a&v?F*G:a&w?F*G*H:a&x?F*G*H*I:F*G*H*I*K}function n(a,b,c,d,e){var g;if(c=+c||E,d=d>0?d:NaN,e=e>0?20>e?Math.round(e):20:0,"function"==typeof a?(g=a,a=null):a instanceof Date||(a=null!==a&&isFinite(a)?new Date(a):null),"function"==typeof b?(g=b,b=null):b instanceof Date||(b=null!==b&&isFinite(b)?new Date(b):null),!a&&!b)return new f;if(!g)return l(new f,a,b,c,d,e);var h,i=m(c),j=function(){g(l(new f,a,b,c,d,e),h)};return j(),h=setInterval(j,i)}var o,p,q,r,s,t=1,u=2,v=4,w=8,x=16,y=32,z=64,A=128,B=256,C=512,D=1024,E=A|z|x|w|v|u,F=1e3,G=60,H=60,I=24,J=I*H*G*F,K=7,L=12,M=10,N=10,O=10,P=Math.ceil,Q=Math.floor,R=0,S=1,T=2,U=3,V=4,W=5,X=6,Y=7,Z=8,$=9,_=10;f.prototype.toString=function(){var a=s(this),b=a.length;return b?(b>1&&(a[b-1]=q+a[b-1]),a.join(r)):""},f.prototype.toHTML=function(a){a=a||"span";var b=s(this),c=b.length;if(!c)return"";for(var d=0;c>d;d++)b[d]="<"+a+">"+b[d]+"</"+a+">";return--c&&(b[c]=q+b[c]),b.join(r)},s=function(a){var b=[],c=a.millennia;return c&&b.push(e(c,_)),c=a.centuries,c&&b.push(e(c,$)),c=a.decades,c&&b.push(e(c,Z)),c=a.years,c&&b.push(e(c,Y)),c=a.months,c&&b.push(e(c,X)),c=a.weeks,c&&b.push(e(c,W)),c=a.days,c&&b.push(e(c,V)),c=a.hours,c&&b.push(e(c,U)),c=a.minutes,c&&b.push(e(c,T)),c=a.seconds,c&&b.push(e(c,S)),c=a.milliseconds,c&&b.push(e(c,R)),b},n.MILLISECONDS=t,n.SECONDS=u,n.MINUTES=v,n.HOURS=w,n.DAYS=x,n.WEEKS=y,n.MONTHS=z,n.YEARS=A,n.DECADES=B,n.CENTURIES=C,n.MILLENNIA=D,n.DEFAULTS=E,n.ALL=D|C|B|A|z|y|x|w|v|u|t;var aa=(n.setLabels=function(a,b,c,d){a=a||[],a.split&&(a=a.split("|")),b=b||[],b.split&&(b=b.split("|"));for(var e=R;_>=e;e++)o[e]=a[e]||o[e],p[e]=b[e]||p[e];q="string"==typeof c?c:q,r="string"==typeof d?d:r},n.resetLabels=function(){o="millisecond|second|minute|hour|day|week|month|year|decade|century|millennium".split("|"),p="milliseconds|seconds|minutes|hours|days|weeks|months|years|decades|centuries|millennia".split("|"),q="and ",r=", "});return aa(),a&&a.exports?a.exports=n:"function"==typeof window.define&&"undefined"!=typeof window.define.amd&&window.define("countdown",[],function(){return n}),n}(b);return{restrict:"EA",replace:!0,transclude:!0,scope:{stopdate:"@"},templateUrl:function(a,b){return"angularcountdown.html"},link:function(b){var d=new Date(b.stopdate).getTime();a(function(){b.timespan=c(null,d,c.DAYS|c.HOURS|c.MINUTES|c.SECONDS,5),angular.element("#days").html(b.timespan.days.toString()),angular.element("#hours").html(b.timespan.hours.toString()),angular.element("#minutes").html(b.timespan.minutes.toString()),angular.element("#seconds").html(b.timespan.seconds.toString())},1e3)}}}]);