jquery.epiclock.min.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*!
  2. * epiClock 3.0
  3. *
  4. * Copyright (c) 2008 Eric Garside (http://eric.garside.name)
  5. * Dual licensed under:
  6. * MIT: http://www.opensource.org/licenses/mit-license.php
  7. * GPLv3: http://www.opensource.org/licenses/gpl-3.0.html
  8. */
  9. "use strict";(function($){var months=['January','February','March','April','May','June','July','August','September','October','November','December'],days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],counts=[31,28,31,30,31,30,31,31,30,31,30,31],suffix=[null,'st','nd','rd'],_;function pad(format,string,length)
  10. {format=format+'';length=length||2;return format.length<length?new Array(1+length-format.length).join(string)+format:format;}
  11. function rpad(format,string,length)
  12. {format=format+'';length=length||2;return format.length<length?format+new Array(1+length-format.length).join(string):format;}
  13. function modCalc(date,mod1,mod2)
  14. {return(Math.floor(Math.floor(date.valueOf()/1e3)/mod1)%mod2);}
  15. function formatDate(date,format)
  16. {format=format.split('');var output='',buffering=false,char='',index=0;for(;index<format.length;index++)
  17. {char=format[index]+'';switch(char)
  18. {case' ':output+=char;break;case'{':case'}':buffering=char==='{';break;default:if(!buffering&&_[char])
  19. {output+=_[char].apply(date);}
  20. else
  21. {output+=char;}
  22. break;}}
  23. return output;}
  24. _={V:function()
  25. {return modCalc(this,864e2,1e5);},v:function()
  26. {return pad(_.V.apply(this),0);},K:function()
  27. {return _.V.apply(this)%365;},k:function()
  28. {return pad(_.K.apply(this),0);},X:function()
  29. {return modCalc(this,36e2,24);},x:function()
  30. {return pad(_.X.apply(this),0);},p:function()
  31. {return modCalc(this,60,60);},C:function()
  32. {return pad(_.p.apply(this),0);},e:function()
  33. {return(_.X.apply(this)*60)+_.p.apply(this);},E:function()
  34. {return pad(_.e.apply(this),0);},d:function()
  35. {return pad(this.getDate(),0);},D:function()
  36. {return days[this.getDay()].substring(0,3);},j:function()
  37. {return this.getDate();},l:function()
  38. {return days[this.getDay()];},N:function()
  39. {return this.getDay()+1;},S:function()
  40. {return suffix[this.getDate()]||'th';},w:function()
  41. {return this.getDay();},z:function()
  42. {return Math.round((this-_.f.apply(this))/864e5);},W:function()
  43. {return Math.ceil(((((this-_.f.apply(this))/864e5)+_.w.apply(_.f.apply(this)))/7));},F:function()
  44. {return months[this.getMonth()];},m:function()
  45. {return pad((this.getMonth()+1),0);},M:function()
  46. {return months[this.getMonth()].substring(0,3);},n:function()
  47. {return this.getMonth()+1;},t:function()
  48. {if(this.getMonth()===1&&_.L.apply(this)===1)
  49. {return 29;}
  50. return counts[this.getMonth()];},L:function()
  51. {var Y=_.Y.apply(this);return Y%4?0:Y%100?1:Y%400?0:1;},f:function()
  52. {return new Date(this.getFullYear(),0,1);},Y:function()
  53. {return this.getFullYear();},y:function()
  54. {return(''+this.getFullYear()).substr(2);},a:function()
  55. {return this.getHours()<12?'am':'pm';},A:function()
  56. {return _.a.apply(this).toUpperCase();},B:function()
  57. {return pad(Math.floor((((this.getHours())*36e5)+(this.getMinutes()*6e4)+(this.getSeconds()*1e3))/864e2),0,3);},g:function()
  58. {return this.getHours()%12||12;},G:function()
  59. {return this.getHours();},h:function()
  60. {return pad(_.g.apply(this),0);},H:function()
  61. {return pad(this.getHours(),0);},i:function()
  62. {return pad(this.getMinutes(),0);},s:function()
  63. {return pad(this.getSeconds(),0);},u:function()
  64. {return this.getTime()%1e3;},O:function()
  65. {var t=this.getTimezoneOffset()/60;return rpad(pad((t>=0?'+':'-')+Math.abs(t),0),0,4);},P:function()
  66. {var t=_.O.apply(this);return t.subst(0,3)+':'+t.substr(3);},Z:function()
  67. {return this.getTimezoneOffset()*60;},c:function()
  68. {return _.Y.apply(this)+'-'+_.m.apply(this)+'-'+_.d.apply(this)+'T'+_.H.apply(this)+':'+_.i.apply(this)+':'+_.s.apply(this)+_.P.apply(this);},r:function()
  69. {return this.toString();},U:function()
  70. {return this.getTime()/1e3;}};$.extend(Date.prototype,{format:function(format)
  71. {return formatDate(this,format);}});$.dateformat={rules:function(custom)
  72. {if(custom!==undefined)
  73. {_=$.extend(_,custom);}
  74. return _;},hasRule:function(rule)
  75. {return _[rule]!==undefined;},get:function(type,date)
  76. {return _[type].apply(date||new Date());},format:function(format,date)
  77. {return formatDate(date||new Date(),format);},pad:pad,rpad:rpad};}(jQuery));"use strict";(function($){var mode={clock:'clock',explicit:'explicit',countdown:'countdown',countup:'countup',rollover:'rollover',expire:'expire',loop:'loop',stopwatch:'stopwatch',holdup:'holdup',timer:'timer'},_=function(selector)
  78. {return $(selector).data('epiclock');},events={},instances={},uid=0,now,zero=new Date(0),intervalID,Clock=function()
  79. {this.__uid=uid++;instances[this.__uid]={clock:this,frame:{}};};function triggerEvent(uid,event,params)
  80. {instances[uid].clock.container.triggerHandler(event,params);if(events[uid]===undefined||events[uid][event]===undefined)
  81. {return;}
  82. $.each(events[uid][event],function(index,value)
  83. {if($.isFunction(value))
  84. {value.apply(instances[uid].clock,params||[]);}});}
  85. function terminate(clock,current)
  86. {triggerEvent(clock.__uid,'timer');switch(clock.mode)
  87. {case mode.holdup:case mode.rollover:clock.mode=mode.countup;clock.restart(0);return zero;case mode.expire:case mode.countdown:case mode.timer:clock.destroy();return zero;case mode.loop:clock.restart();return zero;}}
  88. function tick(clock)
  89. {if(clock.__paused!==undefined&&clock.mode)
  90. {return false;}
  91. var current=now+clock.__displacement,days;switch(clock.mode)
  92. {case mode.holdup:current-=clock.time;if(current>0||current>-1e3)
  93. {return terminate(clock,current);}
  94. return zero;case mode.countup:case mode.stopwatch:current-=clock.time;break;case mode.explicit:current+=clock.time;break;case mode.rollover:case mode.loop:case mode.expire:case mode.countdown:case mode.timer:current=clock.time-current;if(current<1e3)
  95. {return terminate(clock,current);}
  96. break;}
  97. if(clock.displayOffset!==undefined)
  98. {days=parseInt($.dateformat.get('V',current),10);if(days>clock.__days_added)
  99. {clock.__days_added=days;clock.displayOffset.days+=days;if(clock.displayOffset.days>=365)
  100. {clock.displayOffset.years+=Math.floor(clock.displayOffset.days/365.4%365.4);clock.displayOffset.days=Math.floor(clock.displayOffset.days%365.4);}}}
  101. return new Date(current);}
  102. function evaluate(clock,symbol,current)
  103. {switch(symbol)
  104. {case'Q':return clock.displayOffset.years;case'E':return clock.displayOffset.days;case'e':return $.dateformat.pad(clock.displayOffset.days,0);default:return $.dateformat.get(symbol,current);}}
  105. function defaultRenderer(frame,value)
  106. {frame.text(value);}
  107. function render(clock)
  108. {var time=tick(clock);if(time===false)
  109. {return false;}
  110. $.each(instances[clock.__uid].frame,function(symbol,frame)
  111. {var value=evaluate(clock,symbol,time)+'';if(frame.data('epiclock-last')!==value)
  112. {(clock.__render||defaultRenderer)(frame,value);}
  113. frame.data('epiclock-last',value);});triggerEvent(clock.__uid,'rendered');if(clock.container.hasClass('epiclock-wait-for-render'))
  114. {clock.container.removeClass('epiclock-wait-for-render');}}
  115. function cycleClocks()
  116. {now=new Date().valueOf();$.each(instances,function()
  117. {render(this.clock);if(this.clock.__destroy)
  118. {this.clock.container.removeData('epiclock');delete instances[this.clock.__uid];}});}
  119. function startManager()
  120. {if(intervalID!==undefined)
  121. {return;}
  122. $.each(instances,function()
  123. {this.clock.resume();});intervalID=setInterval(cycleClocks,_.precision);}
  124. function haltManager()
  125. {clearInterval(intervalID);intervalID=undefined;$.each(instances,function()
  126. {this.clock.pause();});}
  127. function defaultFormat(format)
  128. {switch(format)
  129. {case mode.clock:case mode.explicit:return'F j, Y g:i:s a';case mode.countdown:return'V{d} x{h} i{m} s{s}';case mode.countup:return'Q{y} K{d} x{h} i{m} s{s}';case mode.rollover:return'V{d} x{h} i{m} s{s}';case mode.expire:case mode.timer:return'x{h} i{m} s{s}';case mode.loop:return'i{m} s{s}';case mode.stopwatch:return'x{h} C{m} s{s}';case mode.holdup:return'Q{y} K{d} x{h} i{m} s{s}';}}
  130. function configureInstance(properties)
  131. {var clock=new Clock(),modifier=1;clock.mode=properties.mode||mode.clock;if(properties.offset!==undefined)
  132. {clock.__offset=((properties.offset.years||0)*3157056e4+
  133. (properties.offset.days||0)*864e5+
  134. (properties.offset.hours||0)*36e5+
  135. (properties.offset.minutes||0)*6e4+
  136. (properties.offset.seconds||0)*1e3);}
  137. if(properties.startpaused)
  138. {clock.__paused=new Date().valueOf();}
  139. clock.__displacement=properties.utc===true||properties.gmt===true?new Date().getTimezoneOffset()*6e4:0;clock.__render=$.isFunction(properties.renderer)?properties.renderer:_.renderers[properties.renderer];clock.__days_added=0;clock.__tare=properties.tare||false;clock.format=properties.format||defaultFormat(clock.mode);clock.time=(properties.time||properties.target?new Date(properties.time||properties.target):new Date()).valueOf();clock.displayOffset=$.extend({years:0,days:0},properties.displayOffset);switch(clock.mode)
  140. {case mode.clock:case mode.countup:break;case mode.explicit:clock.__displacement-=new Date().valueOf();break;case mode.timer:case mode.loop:modifier=-1;clock.__tare=true;clock.__displacement-=1e3;break;case mode.expire:case mode.countdown:case mode.rollover:modifier=-1;clock.__displacement-=1e3;break;case mode.holdup:if(clock.time<new Date().valueOf())
  141. {clock.mode=mode.countup;}
  142. else
  143. {clock.__displacement-=1e3;}
  144. break;case mode.stopwatch:clock.__tare=true;break;default:throw'EPICLOCK_INVALID_MODE';}
  145. clock.__displacement+=modifier*clock.__offset;return clock;}
  146. function printTick(clock)
  147. {now=new Date().valueOf();return tick(clock);}
  148. $.extend(_,{precision:500,modes:mode,renderers:{},addRenderer:function(key,renderer,setup)
  149. {_.renderers[key]=renderer;if($.isFunction(setup))
  150. {_.renderers[key].setup=setup;}
  151. return _;},make:function(properties,container,template)
  152. {var clock=configureInstance(properties),output='',buffering=false,char='',index=0,format=clock.format.split(''),containerClass=typeof properties.renderer==="string"?' epiclock-'+properties.renderer:'';container=$(container).data('epiclock',clock).addClass('epiclock-container epiclock-wait-for-render'+containerClass);template=$(template||'<span></span>');for(;index<format.length;index++)
  153. {char=format[index]+'';switch(char)
  154. {case' ':if(buffering)
  155. {output+=char;}
  156. else
  157. {template.clone(true).addClass('epiclock epiclock-spacer').appendTo(container);}
  158. break;case'{':case'}':buffering=char==='{';if(!buffering)
  159. {template.clone(true).addClass('epiclock').html(output).appendTo(container);output='';}
  160. break;default:if(!buffering&&$.dateformat.hasRule(char))
  161. {instances[clock.__uid].frame[char]=template.clone(true).addClass('epiclock epiclock-digit').data('epiclock-encoding',char).appendTo(container);}
  162. else if(!buffering)
  163. {template.clone(true).addClass('epiclock epiclock-separator').html(char).appendTo(container);}
  164. else
  165. {output+=char;}
  166. break;}}
  167. clock.container=container;if(clock.__render!==undefined&&$.isFunction(clock.__render.setup))
  168. {clock.__render.setup.apply(clock,[container]);}
  169. startManager();return clock;},pause:function()
  170. {haltManager();},resume:function()
  171. {startManager();},destroy:function()
  172. {clearInterval(intervalID);$.each(instances,function()
  173. {this.clock.destroy();});},restart:function()
  174. {$.each(instances,function()
  175. {this.clock.restart();});}});$.dateformat.rules({Q:function()
  176. {return'%displayOffset-years%';},E:function()
  177. {return'%displayOffset-days%';},e:function()
  178. {return'%displayOffset-days-pad%';}});$.extend(Clock.prototype,{__uid:undefined,__render:undefined,__displacement:undefined,__days_added:undefined,__paused:undefined,__offset:0,__tare:false,__destroy:false,displayOffset:undefined,time:undefined,format:undefined,mode:undefined,container:undefined,bind:function(event,listener)
  179. {if(events[this.__uid]===undefined)
  180. {events[this.__uid]={};}
  181. if(events[this.__uid][event]===undefined)
  182. {events[this.__uid][event]=[listener];}
  183. else
  184. {events[this.__uid][event].push(listener);}
  185. return this;},pause:function()
  186. {if(this.__paused===undefined)
  187. {this.__paused=new Date().valueOf();}
  188. triggerEvent(this.__uid,'pause');},resume:function()
  189. {if(this.__paused===undefined)
  190. {return;}
  191. triggerEvent(this.__uid,'resume');if(this.__tare)
  192. {this.__displacement+=(this.__paused-new Date().valueOf());}
  193. this.__paused=undefined;},toggle:function()
  194. {if(this.__paused===undefined)
  195. {this.pause();}
  196. else
  197. {this.resume();}},destroy:function()
  198. {this.__destroy=true;triggerEvent(this.__uid,'destroy');},restart:function(displacement)
  199. {if(displacement!==undefined)
  200. {this.__displacement=displacement;}
  201. this.time=now.valueOf();},print:function(format)
  202. {var value=$.dateformat.format(this.format,printTick(this));if(this.displayOffset!==undefined)
  203. {return value.replace('%displayOffset-days%',this.displayOffset.years).replace('%displayOffset-days%',this.displayOffset.days).replace('%displayOffset-days-pad%',$.dateformat.pad(this.displayOffset.days,0));}
  204. return value;}});$.epiclock=_;$.fn.epiclock=function(properties)
  205. {return this.each(function()
  206. {var container=$(this),template;properties=properties||{};if(properties.template!==undefined)
  207. {template=properties.template;delete properties.template;}
  208. else if(container.children().length>0)
  209. {template=container.children().remove().eq(0);}
  210. $.epiclock.make(properties,container,template);});};}(jQuery));