7 lines
6.3 KiB
JavaScript
7 lines
6.3 KiB
JavaScript
/* http://keith-wood.name/countdown.html
|
|
Countdown for jQuery v1.2.2.
|
|
Written by Keith Wood (kbwood@virginbroadband.com.au) January 2008.
|
|
Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
|
|
MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
|
|
Please attribute the author if you use it. */
|
|
(function($){function Countdown(){this._nextId=0;this._inst=[];this.regional=[];this.regional['']={labels:['Years','Months','Weeks','Days','Hours','Minutes','Seconds'],labelsSingle:['Year','Month','Week','Day','Hour','Minute','Second'],compactLabels:['y','m','w','d'],compactLabelsSingle:['y','m','w','d'],timeSeparator:':'};this._defaults={format:'dHMS',compact:false,description:'',expiryUrl:null,alwaysExpire:false,onExpiry:null,onTick:null,serverTime:null};$.extend(this._defaults,this.regional[''])}$.extend(Countdown.prototype,{markerClassName:'hasCountdown',_register:function(a){var b=this._nextId++;this._inst[b]=a;return b},_getInst:function(a){return this._inst[a]||a},setDefaults:function(a){extendRemove(this._defaults,a||{})},_attachCountdown:function(a,b){a=$(a);if(a.is('.'+this.markerClassName)){return}a.addClass(this.markerClassName);a[0]._cdnId=b._id;b._target=a;this._updateCountdown(b._id)},_updateCountdown:function(a){var b=this._getInst(a);b._target.html(b._generateHTML());var c=b._get('onTick');if(c){c.apply(b._target[0],[b._periods])}var d=(b._since?b._now.getTime()<=b._since.getTime():b._now.getTime()>=b._until.getTime());if(d){if(b._timer||b._get('alwaysExpire')){var e=b._get('onExpiry');if(e){e.apply(b._target[0],[])}var f=b._get('expiryUrl');if(f){window.location=f}}b._timer=null}else{var g=b._get('format');b._timer=setTimeout('$.countdown._updateCountdown('+b._id+')',(g.match('s|S')?1:(g.match('m|M')?30:600))*980)}},_changeCountdown:function(a,b){var c=this._getInst(a._cdnId);if(c){extendRemove(c._settings,b||{});c._adjustSettings();this._updateCountdown(c._id)}},_destroyCountdown:function(a){a=$(a);if(!a.is('.'+this.markerClassName)){return}a.removeClass(this.markerClassName);a.empty();clearTimeout(this._inst[a[0]._cdnId]._timer);this._inst[a[0]._cdnId]=null;a[0]._cdnId=undefined}});var Y=0;var O=1;var W=2;var D=3;var H=4;var M=5;var S=6;function CountdownInstance(a){this._id=$.countdown._register(this);this._target=null;this._timer=null;this._now=null;this._periods=[0,0,0,0,0,0,0];this._settings=extendRemove({},a||{});this._adjustSettings()}$.extend(CountdownInstance.prototype,{_get:function(a){return(this._settings[a]!=null?this._settings[a]:$.countdown._defaults[a])},_adjustSettings:function(){var a=new Date();var b=this._get('serverTime');this._offset=(b?b.getTime()-a.getTime():0);this._since=this._get('since');if(this._since){this._since=this._determineTime(this._since,null);this._since.setMilliseconds(0)}this._until=this._determineTime(this._get('until'),a);this._until.setMilliseconds(0)},_determineTime:function(k,l){var m=function(a){var b=new Date();b.setTime(b.getTime()+a*1000);return b};var n=function(a,b){return 32-new Date(a,b,32).getDate()};var o=function(a){var b=new Date();var c=b.getFullYear();var d=b.getMonth();var e=b.getDate();var f=b.getHours();var g=b.getMinutes();var h=b.getSeconds();var i=/([+-]?[0-9]+)\s*(s|S|m|M|h|H|d|D|w|W|o|O|y|Y)?/g;var j=i.exec(a);while(j){switch(j[2]||'s'){case's':case'S':h+=parseInt(j[1]);break;case'm':case'M':g+=parseInt(j[1]);break;case'h':case'H':f+=parseInt(j[1]);break;case'd':case'D':e+=parseInt(j[1]);break;case'w':case'W':e+=parseInt(j[1])*7;break;case'o':case'O':d+=parseInt(j[1]);e=Math.min(e,n(c,d));break;case'y':case'Y':c+=parseInt(j[1]);e=Math.min(e,n(c,d));break}j=i.exec(a)}b=new Date(c,d,e,f,g,h,0);return b};return(k==null?l:(typeof k=='string'?o(k):(typeof k=='number'?m(k):k)))},_generateHTML:function(){var b=this._get('format');var c=[];c[Y]=(b.match('y')?'?':(b.match('Y')?'!':null));c[O]=(b.match('o')?'?':(b.match('O')?'!':null));c[W]=(b.match('w')?'?':(b.match('W')?'!':null));c[D]=(b.match('d')?'?':(b.match('D')?'!':null));c[H]=(b.match('h')?'?':(b.match('H')?'!':null));c[M]=(b.match('m')?'?':(b.match('M')?'!':null));c[S]=(b.match('s')?'?':(b.match('S')?'!':null));this._periods=periods=this._calculatePeriods(c,new Date());var d=false;var e=0;for(var f=0;f<c.length;f++){d|=(c[f]=='?'&&periods[f]>0);c[f]=(c[f]=='?'&&!d?null:c[f]);e+=(c[f]?1:0)}var g=this._get('compact');var h=(g?this._get('compactLabels'):this._get('labels'));var i=(g?this._get('compactLabelsSingle'):this._get('labelsSingle'))||h;var j=this._get('timeSeparator');var k=this._get('description')||'';var l=function(a){return(a<10?'0':'')+a};var m=function(a){return(c[a]?periods[a]+(periods[a]==1?i[a]:h[a])+' ':'')};var n=function(a){return(c[a]?'<div class="countdown_section"><span class="countdown_amount">'+periods[a]+'</span><br/>'+(periods[a]==1?i[a]:h[a])+'</div>':'')};return(g?'<div class="countdown_row countdown_amount">'+m(Y)+m(O)+m(W)+m(D)+(c[H]?l(this._periods[H]):'')+(c[M]?(c[H]?j:'')+l(this._periods[M]):'')+(c[S]?(c[H]||c[M]?j:'')+l(this._periods[S]):''):'<div class="countdown_row countdown_show'+e+'">'+n(Y)+n(O)+n(W)+n(D)+n(H)+n(M)+n(S))+'</div>'+(k?'<div class="countdown_row countdown_descr">'+k+'</div>':'')},_calculatePeriods:function(c,d){this._now=d;this._now.setMilliseconds(0);var e=new Date(this._now.getTime());if(this._since&&d.getTime()<this._since.getTime()){this._now=d=e}else if(this._since){d=this._since}else{e=new Date(this._until.getTime());if(d.getTime()>this._until.getTime()){this._now=d=e}}e.setTime(e.getTime()-this._offset);var f=[0,0,0,0,0,0,0];if(c[Y]||c[O]){var g=Math.max(0,(e.getFullYear()-d.getFullYear())*12+e.getMonth()-d.getMonth()+(e.getDate()<d.getDate()?-1:0));f[Y]=(c[Y]?Math.floor(g/12):0);f[O]=(c[O]?g-f[Y]*12:0);d=new Date(d.getTime());d.setFullYear(d.getFullYear()+f[Y]);d.setMonth(d.getMonth()+f[O])}var h=Math.floor((e.getTime()-d.getTime())/1000);var i=function(a,b){f[a]=(c[a]?Math.floor(h/b):0);h-=f[a]*b};i(W,604800);i(D,86400);i(H,3600);i(M,60);i(S,1);return f}});function extendRemove(a,b){$.extend(a,b);for(var c in b){if(b[c]==null){a[c]=null}}return a}$.fn.countdown=function(a){var b=Array.prototype.slice.call(arguments,1);return this.each(function(){if(typeof a=='string'){$.countdown['_'+a+'Countdown'].apply($.countdown,[this].concat(b))}else{$.countdown._attachCountdown(this,new CountdownInstance(a))}})};$(function(){$.countdown=new Countdown()})})(jQuery); |